Completeness of continuation models for -calculus Martin Hofmann and Thomas Streicher Synopsis
We show that a certain simple call-by-name continuation semantics of Parigot's -calculus is complete. More precisely, for every -theory we construct a cartesian closed category such that the ensuing continuation-style interpretation of , which maps terms to functions sending abstract continuations to responses, is full and faithful. Thus, any -category in the sense of [8] is isomorphic to a continuation model [6] derived from a cartesian-closed category of continuations. We also extend this result to a later call-by-value version of developed by Ong and Stewart [7].
1 Introduction and Summary Parigot's -calculus [9] is a proof term assignment system for propositional classical logic and can at the same time be considered as a prototype for a functional programming language incorporating explicit handling of continuations. The original motivation for this calculus was to give a functional interpretation for proofs in classical AF2 |a certain system of second-order arithmetic [5]. Ong [8] has de ned a categorical notion of model for this calculus for which the usual categorical completeness theorem holds. In this sense Ong's semantics can be seen as a variable-free reformulation of the syntax of . On the other hand, there exists a class of rather concrete continuation models for where terms are interpreted as functions1 mapping abstract continuations to answers. We prove in this paper that every -theory (thus every model in the sense of Ong) is induced by a particular continuation model. A similar result for call-by-value lambda-calculus with control operators has been obtained in [4] by category-theoretic means and independently by Felleisen and Sabry using syntactic back-and-forth translations [13]. The technique we use here is inspired by the method used in [4] in the sense that the morphisms of the continuation category to be constructed arise as special terms of a -theory. Whereas in loc. cit. these special terms are de ned by their syntactic form we use an equational description involving quanti cation over all observations. Unlike in the case of [4] or [13] the equational axiomatisation of under consideration was not specially tailored towards completeness for continuation models, which were apparently not known to Parigot at the time, but rather arose from syntactic considerations. For instance, it gives rise to a con uent and strongly normalising rewrite system [9]. The fact that by our result this axiomatisation is complete for continuation models thus provides evidence that these models are a very natural semantics for \proof-relevant" classical logic. TH Darmstadt, FB 4, Schlossgartenstr. 7, 64289 Darmstadt, Germany, phone: +49-6151162794, fax: +49-6151-164011, e-mail:
[email protected] 1 In the sense of cartesian-closed categories.
(Axiom)
?j ` x : A
(Const)
?j ` c : A
() -intro) () -elim)
if x: A 2 ? if c: A 2 K
?; x: A j ` t : B ? j ` x: A:t : A ) B
?j ` t : A ) B ?j ` s : A ? j ` ts : B
(?-elim) (?-intro)
? j ; : A ` t : ? ? j ` : A:t : A
?j ` t : A ? j ` []t : ?
if : A 2
Figure 1: Typing rules of A consequence of our result is that -equality without nonlogical axioms can be reduced to equality of terms of simply-typed lambda calculus with products via a certain CPS translation derived from our semantics. Later on, Ong and Stewart [7] have formulated a call-by-value version of . The second main result in this paper is that a certain continuation semantics is complete for the latter system. Furthermore, we demonstrate that this system is isomorphic to the calculus in [4]. Therefore, our result can be transported to the latter system and thus provides a generalisation of the result in [4] as op. cit. was concerned with the bare calculus whereas we consider arbitrary equational theories.
2 The -calculus The presentation of we use follows Ong's account in [8]. It diers from Parigot's original formulation only in the aspect that we omit continuation variables of type ?. See loc. cit. for a more detailed comparison. Assume a set B of base types. The types of are the simple types over B[f?g, i.e. every base type is a type, ? is a type, and if A; B are types so is A ) B . There are two sorts of variables. Object variables ranged over by Roman letters x; y; z; : : : and continuation variables ranged over by Greek letters ; ; : : : . An object context is a partial type assignment to object variables of the form x1 : A1 ; : : : ; xn : An . A continuation context is a partial type assignment to continuation variables of the form 1 : A1 ; : : : ; n : An where all the Ai are dierent from ?. Assume a set K of typed constants. The typing judgements of take the form ? j ` t : A where ? is an object context, is a continuation context, A is a type, and t is a term. The precise form of the terms is given implicitly together with the rules de ning the typing judgement set out in Figure 1. As usual we identify terms 2
( )
(x: A:t)s = t[s=x]:
()
x: A:tx = t; when x is not free in t:
(- ) [] : A:t = t[= ]: (-) : A:[]t = t; not free in t: (- )
: A ) B:t = x: A: : B:t[x:: = ]; when B 6 ?: : A ) ?:t = x: A:t[x:: ? = ]: Figure 2: Equality axioms for
up to renaming of both object and continuation variables. Notice that and bind variables as indicated, but that continuation variable occurs free in a term of the form []t. The typing rules are such that we have x1 : A1 ; : : : ; xn : An j 1 : B1 ; : : : m : Bm ` t : A for some t i A1 ^ ^ An ^ :B1 ^ ^ :Bm ! A is a tautology of classical propositional logic. Under this analogy the rule (?-elim) corresponds to proof by contradiction: in order to prove A it suces to deduce a contradiction (?) from the assumption that A is false ( : A). Rule (?-intro), on the other hand, is the canonical way of constructing contradictions: from a proof of A and an assumption that A is false ( : A). We can also relate to classical sequent calculus as follows. We have
x1 : A1 ; : : : ; xn : An j 1 : B1 ; : : : m : Bm ` t : A for some t i the sequent A1 ; : : : ; An ` B1 ; : : : ; Bm ; A is derivable in Gentzen's sequent calculus LK. Under this analogy the two rules (?-intro) and (?-elim) correspond to addition and removal of ? on the right hand side of the \turnstile". The rules of are such that logical rules always have the last conclusion as main formula. Permuting a conclusion into this \active" position is recorded by an instance of (?-elim). Thus, the purpose of the ?-rules is to display (the otherwise implicit) switching of focus, where \being in focus" means to be the main formula of the next logical rule. We dier from Ong's presentation in that we allow for side context in rule (Axiom), i.e., the fact that variables other than x may be declared in ?, and that may appear free in t in rule (?-intro). Our (stronger) rules are derivable in Ong's system using the structural rules (weakening and contraction) which in turn are admissible in our system. Therefore, the same sequents are derivable in either system. De nition 2.1 A -theory (over a signature (B; K)) is a set E of typed equations of the form ? j ` s = t : A where ? j ` s : A and ? j ` t : A such that E is a congruence stable under weakening, E contains all well-typed instances of the basic equality laws depicted in Figure 2. 3
x[s:: = ] (tt0 )[s:: = ] (y: C:t)[s:: = ] ( : C:t)[s:: = ] ([ ]t)[s:: = ] ([]t)[s:: = ]
= = = = = =
x (t[s:: = ])(t0 [s:: = ]) y: C:(t[s:: = ]) y not free in s : C:(t[s:: = ])
6 and not free in s [ ](t[s:: = ])
6 [ ]((t[s:: = ])s)
Figure 3: De nition of mixed substitution
The notation used in the equations deserves some explanation. The term t[s=x] in rule ( ) denotes the capture-free substitution of s for x in t and t[= ] denotes the capture-free substitution of continuation variable for . The term t[s:: =] called mixed substitution of s : A and : B for continuation variable : A ) B (where B 6 ?) is de ned inductively by the clauses in Figure 3. The substituted term has the same type as t; the substituted variable () does not occur in t[s:: =] unless is free in s. Mixed substitution of continuation variables of type A ) ? is de ned analogously the key clause being ([]t)[s:: ? = ] = (t[s:: ? = ])s Here ? is part of the operation symbol. The idea behind this so-called mixed substitution is that a continuation for a function of type A ) B can be understood as an argument s and a continuation for the ensuing result. The substitution operation t[s:: =] allows one to substitute such an \intended" continuation for a continuation variable. Since there are no continuations of type ?, an \intended" continuation of type A ) ? is simply an object of type A.
3 Continuation models of The -calculus admits a simple and intuitive continuation semantics in an arbitrary category with enough products and exponentials, in particular in any cartesian closed category with a distinguished object R of responses. De nition 3.1 [Category of continuations] A category of continuations is given by the following data: 1. A category C with a distinguished class T of objects of C called type objects. 2. A distinguished type object R of responses. 3. For every object ? and type object A a chosen cartesian product ? A. 4. A chosen terminal object [] (for the empty context). 5. A chosen terminal object 1 2 T (to interpret ?). 6. For every type object A a chosen exponential RA 2 T of R by A. 4
7. For any two type objects A and B a chosen cartesian product RA B 2 T of RA and B .
A A Clearly, [] = 1 and R B = R B . The presence of these isomorphic copies
of terminal objects and cartesian products is not strictly necessary, for instance, we could postulate that a product RA B must be a type object if A and B are. However, they re ect syntactic distinctions and facilitate the formulation of term models. Particular examples of continuation categories are the category of sets and various categories of domains where a natural choice for R is the set (domain) of screen outputs or alternatively truth values (in the case of sets) and the Sierpinski space (two element poset) in the case of domains. A further important example is furnished by the term model of a simply-typed lambda calculus together with a distinguished base type R. This model is generic in the sense that if a certain equation holds in it then it must hold in any other continuation category. Assume for the rest of this section a xed category of continuations. Any assignment of type objects [ B ] to base types B extends to an assignment of type objects to all types by the following two clauses. [ ?] = 1 [ A ) B ] = R[ A] [ B ] The intention is that [ A] is the space of abstract continuations of type A. Accordingly, we call R[ A] the (type) object of denotations of type A. This may explain the de nition of [ A ) B ] : A continuation for a function is given by an argument (a denotation of type A) and a continuation for the result. Let ? x1 : A1 ; : : : ; xn : An be an object context and 1 : B1 ; : : : ; m : Bm be a continuation context. We use the notation R[ ?]] [ ]] for the object [] R[ A1 ] : : : R[ An ] [ B1 ] : : : [ Bm ] The subsequent interpretation of is motivated by the following two natural isomorphisms familiar from the more special case of CCC's. The reader is invited to keep those in mind when going through the semantic clauses below. Proposition 3.2 Let A; B be type objects and X any object of a continuation category C. We have the following two isomorphisms natural in X . C(X RA; RB ) = C(X; RRAB ) C(X A; R1) = C(X; RA) Assume an assignment of denotations to the constants, i.e. a morphism [ c] : [] ! R[ A] if c: A is in K. To each sequent ? j ` t : A we associate an arrow [ ? j ` t : A] : R[ ?]] [ ]] ! R[ A] by the clauses in Figure 4 where an informal lambda calculus (\internal language") is employed to simplify the notation. Notice that we use pattern matching for abstractions over product and unit types. Furthermore, we use ? to denote the unique element of [ ?] = 1. For the case of untyped such semantics has been de ned in [12]. The crucial observation that RA B is an exponential of B by A in the category CR which has the same objects as C and homsets given by CR(X; Y ) = C(RX ; RY ) has been made by several people around 1990, including [1] and [6]. Theorem 3.3 (Soundness) The -calculus is sound with respect to this interpretation in the sense that the set of equations ? j ` t1 = t2 : A where t1 ; t2 are appropriately typed terms and [ ? j ` t1 : A] = [ ? j ` t2 : A] is a -theory. 5
[ ? j ` xi : A] (~x j ~ ) [ ? j ` x: A:t : A ) B ] (~x j ~ ) [ ? j ` ts : B ] (~x j ~ ) [ ? j ` : A:t : A] (~x j ~ ) [ ? j ` [i ]t : ?] (~x j ~ )
= = = = =
xi hx; i: R[ A] [ B ] :[ ?; x: A j ` t : B ] (~x; x j ~ ) : [ B ] :[ ? j ` t : A ) B ] (~x j ~ )h[ ? j ` s : A] (~x j ~ ); i : [ A] :[ ? j ; : A ` t : ?] (~x j ~ ; )? ?: 1:[ ? j ` t : A] (~x j ~ )i
Figure 4: Interpretation of in a category of continuations
Proof. Induction on derivations using appropriate substitution lemmas.
4 Completeness of continuation semantics Our aim in this section is to establish the following completeness result for the continuation semantics. Theorem 4.1 For every -theory E over a signature (B; K) there exists a continuation category (C; R) and an interpretation of base types and constants with the following two properties. 1. E is the theory induced by this continuation model (C; R). 2. Let ? be an object context, be a continuation context and A be a type. Every C-morphism f : R[ ?]] [ ]] ! R[ A] arises as the interpretation of some -term ? j ` t : A. For the rest of this section assume a xed signature (B; K) and a theory E . We will use the notation from Def. 2.1 to refer to the continuation category which we are going to construct from these data. Before embarking on the actual construction let us rst provide some intuition. Assume for the moment that E happens to be induced by a hypothetical continuation category C. It suggests itself to recover C from the -theory by using the continuation contexts as objects. Unfortunately, morphisms from [ ]] to [ A] do not arise as meanings of terms so that there is no straightforward way to recover the C-morphisms from terms. However, the meaning of a term j ` t : A ) ? is [ A] a C-morphism from [ ]] to RR[ A] 1 =[ A]RR . Now every morphism f : [ ]] ! [ A] induces a morphism from [ ]] to RR by composition with the curried evaluation map [ A] (a: [ A] ) = p: R[ A] :pa. Alas, in general, it seems to be impossible to tell whether a given morphism h : [ ]] ! RR[ A] factors through . However, those h which do, satisfy a certain equation. Namely2 , let R2 ([[A] ) and R2 ([ A] ) be the two canonical maps from R2 ([[A] ) ! R4 ([[A] ), i.e., (in -calculus notation) R2 ([[A] ) (') = : R3 ([[A] ):(')
R2 ([ A] )(') = : R3 ([[A] ):'(x: [ A] :(k: R([[A] ):kx)) We follow Paul Taylor in using the notation Rn (X ) for an R-tower of height n, so R2 (X ) stands for RRX . 2
6
Then, since R2 ([[A] ) [ A] = R2 ([ A] ) [ A] by -calculus, we have R2 ([[A] ) h = R2 ([ A] ) h, whenever h factors through [ A] , .e., can be written in the form [ A] h0 . We can also argue element-wise and conclude that if F : R2 ([[A] ) is of the form [ A] (a) for some a : A, i.e., F = k:ka, then (y) (F ) = F (x: [ A] :([ A] (x))) for all : R3 ([[A] ) So we are led to decree that a morphism from to A is a -term j ` t : A ) ? satisfying this property (which, of course, has to be translated into -equations). The morphisms into contexts rather than merely types are then constructed as tuples of these. Unfortunately, since [ A] is in general not the equaliser of R2 ([[A] ) ; R2 ([ A] ) this equational condition is necessary, but not sucient for factoring through [ A] . Therefore, the category thus obtained need not be equivalent to a possibly already existing (or hypothetically assumed) category C, but fortunately does the job, nevertheless. In the sequel we will carry out the construction of this syntactic category explicitly and demonstrate that it meets the requirement of the main theorem. Surprisingly, the main eort consists of showing that the C-maps (as we will call them) compose and thus form a category at all. The reason is that composition cannot be de ned as syntactic substitution. Convention 4.2 In order to avoid messy case analyses we shall henceforth adopt the convention that lower case Greek letters range over continuation variables as well as ?. We extend -abstraction and -application by the settings ?:M := M and [?]M = M . It is clear that this preserves typing and that equations involving and [?]? generalise accordingly. With this convention the two parts of rule (- ) can be subsumed under the rst one. Indeed, if we formally extend the de nition of mixed substitution to : ? then under the above convention the terms t[s:: = ] and t[s:: ? = ] are identical.
4.1 The generic continuation category
For : A let v stand for the term x: A:[]x. De nition 4.3 Let be a continuation context and A be a type. A continuation term (C-term for short) in context and of type A is a -term j ` t : A ) ? such that for every -term ? j ; 0 ` o : (A ) ?) ) ? (subsequently called an observer ) we have ? j ; 0 ` ot = t(: A:o(v )) : ? More generally, a continuation map (C-map ) from to 1 : A1 ; : : : ; n : An is an n-tuple (t1 ; : : : ; tn ) such that ti is a C-term of type Ai in context . [ A ] We remark that t is a C-term of type A i for each ~ : [ ]] the element d: R :[ t] ( j ~ )hd; ?i (that is [ t] ( j ~ ) transported along the isomorphism RR[ A] 1 = R2 ([[A] )) satis es condition (y) above. Notice that by Convention 4.2 a term t : ? ) ? is a C-term of type ? i ? j ; 0 ` ot = t(o(x: ?:x)) : ? for every observer ? j ; 0 ` o : (? ) ?) ) ?. The desired continuation category C will have the continuation contexts as objects and the C-maps as morphisms. In order to de ne composition of C-maps we need to develop some machinery rst. 7
Proposition 4.4 If : A is a continuation variable (declared in ) then the term v := x: A:[]x is a C-term of type A.
Proof. Assume ? j ; 0 ` o : (A ) ?) ) ?. We must show that (in context ? j ; 0 ) we have ov = v (: A:o(v )). By ( ) the right hand side equals [](: A:o(v )) which equals the left hand side by (- ). Henceforth, we will use capital letters M; N; U; V to range over C-terms. The following characterises C-terms of negated type. Lemma 4.5 The C-terms of type (A ) ?) are precisely those of the form f : A ) ?:ft for arbitrary term t : A. In particular, if M is a C-term of type A ) ? then M = f : A ) ?:f (: A:M (v ))
Proof. To prove the equation let f : A ) ? be a fresh variable. We calculate as follows. Employing the observer o = m:f (: A:m(v )) we can rewrite the body of the left-hand side as M (': A ) ?:o(v' )) which equals M (x: A:f (:[]x)) by (- ) and nally Mf by (-) and (). We conclude by abstracting from f . In order to prove that f:ft is a C-map let o be an observer for f:ft. Then we have (f:ft)(': A ) ?:o(v' )) = (':o(v' ))t by (- )and ( ) = o(f:ft) so f:ft is a C-term.
Lemma 4.6 If M : A ) ? is a C-term of type A and t : ? does not contain the continuation variable : A then M (: A:t) = t.
Proof. We use the observer o = m:t. So oM = t by ( ) and M (:o(v )) = M (:t) as required. De nition 4.7 Let t be a term of type B ) ? possibly containing a free continuation variable : A and s be a term of type A ) ?. The term t[:= s] of type B ) ? is de ned as x: B:s(: A:tx). Proposition 4.8 If M is a C-term of type B (possibly containing : A) and N is a C-term of type A then M [:= N ] is a C-term of type B .
Proof. Let o : (B ) ?) ) ? be an observer. We demonstrate the required
identity o(M [:= N ]) = M [:= N ]( : B:o(v )) by showing that both sides equal N (:o(M )). Using the observer o0 = n: A ) ?:o(x: B:n(: A:Mx)) for the C -term N we obtain (o0 N =)o(M [:= N ]) = N (: A:o0 (v )) = N (:o(M )) where the last equality involves (- ). On the other hand, M [:= N ]( : B:o(v )) equals N (:o(M )) using the observer o applied to M . 8
We will now show that the operations on C-terms v? and ?[?:= ?] behave like variables and substitution in ordinary -calculus. Lemma 4.9 The following equations hold whenever they are well-typed and M; N; U; V are C-terms as indicated. 1. v [:= M ] = M . 2. v [:= N ] = v , if 6 . 3. M [:= v ] = M . 4. M [:= U ] = M , if not free in M . 5. M [:= U ][ := V ] = M [ := V ][:= U [ := V ]], if is not free in V . 6. M [:= U ][ := V ] = M [ := V ][:= U ] if ; are not free in U; V . 7. M [:= U ][ := V ] = M [:= U [ := V ]], if not free in M .
Proof. First, observe that if : A is a continuation variable and M is any term of type ? then v (: A:M ) = M by ( ) and (- ). We will omit type annotations as they depend upon the unspeci ed typing of the equations. Ad 1. v [:= M ] = x:M (:v x) = x:Mx = M by (-) and (). Ad 3. M [:= v ] = x:v (:Mx) = M by the above observation followed by (- ) and (). Ad 5. The right hand side expands to x:o(V ) where o = v:v( :U (:v( :Mx))) Since V is a C-term this equals
x:V ( :v ( :U (:v ( :Mx)))) By the above observation this equals x:V ( :U (:Mx)) which is a ( )-contraction of the left hand side. Ad 4. By de nition M [:= U ] = x:U (:Mx). By Lemma 4.6 applied to U and t := Mx this equals x:Mx. The conclusion follows by (). The remaining parts 2, 6, 7 are immediate consequences of 5 and 4. This allows us to carry out the usual construction [10] of a category with nite products from a substitution calculus. Let M be a C-term of type A in context 1 : A1 ; : : : ; n : An . Furthermore, let f (N1 ; : : : ; Nn) be a C-map from to . We de ne the C-term M f of type A in context as M [1 := N1 ] : : : [n := Nn ]. More generally, if g = (M1 ; : : : ; Mm ) is a C-map from to , we de ne the composition g f as (M1 f; : : : ; Mm f ). Finally, the identity C-map id : ! is de ned as (v1 ; : : : ; vn ). The proof [10] that the term model of an equational theory forms a category can now be copied word for word so as to demonstrate that the continuation contexts and (E -equivalence classes of) C-maps form a category C. Following common practice, we will refer to morphisms in C via representatives, i.e., C-maps. The continuation contexts of length one which we will henceforth identify with types form the subset T of C. It also follows from this proof that the empty context [] forms a terminal object and that the extended context ; : A forms a cartesian product of and type A if A 6= ?. We de ne the cartesian product ? as . 9
The projection on ? is given by v? . By abuse of notation we decree that ; : ? means . The type object of responses R is de ned as the type ? ) ?. This setting is motivated by the observation that the meaning of type ? ) ? in an arbitrary continuation category equals R1 1 which is isomorphic to R. Next, we show that C-maps of type R are precisely the terms of the form f:M where f is not free in M . Proposition 4.10 Let A be a type, i.e. a type object of C. The type A ) ? is an exponential of R by A with evaluation map evA given by
j ': A ) ?; : A ` f : ? ) ?:f (['](v )) : R ) ?
Proof. First note that evA is a C-map by Lemma 4.5. We have to show that the operation uncurA : C(; RA ) ! C( A; R) sending a C-map j ` M : (A ) ?) ) ? to evA [':= M ] = f : ? ) ?:M (x: A:f ([]x)) by (- ) is a bijection. The candidate for the inverse to uncur sends a C-map j ; : A ` N : (? ) ?) ) ?, i.e. a C-morphism from A to R to the C-map cur:A (N ) given by
j ` f : A ) ?:f (: A:N (v? )) : (A ) ?) ) ? Notice that cur:A (t) binds in t. Assume a C-map j ; : A ` N : (? ) ?) ) ?. The expression uncur(cur(N )) expands to
f : ? ) ?:f (N (x:x)) This equals N by Lemma 4.5. For the other direction assume a C-map j ` M : (A ) ?) ) ?. The required equation cur(uncur(M )) = M is after ( )-contraction an instance of Lemma 4.5
Proposition 4.11 Let A; B be types. The type A ) B is a cartesian product of RA (= A ) ?) and B with projections 2 C(A ) B; RA ) and 0 2 C(A ) B; B ) given by
j ': A ) B ` f : A ) ?:f (: A:['](x: A: : B:[]x)) : (A ) ?) ) ? and j ': A ) B ` b: B:['](x: A:b) : B ) ?.
Proof. The rst projection is a C-map by Lemma 4.5. To see that the second projection is a C-map let o be an observer. We calculate as follows. = = = =
o(0 ) o(b: B:['](x: A:b)) ['] : A ) B:o(b:[ ](x:b)) [']x: A: : B:o(b:[ ]b) 0 ( :o(v ))
by (- )-expansion by (- )and ( )
Let j ` M : RA ) ? and j ` N : B ) ? be C-terms of type RA and B , resp. In view of Lemma 4.5 we can write M as f : RA:ft for some term j ` t : A. We 10
de ne the C-term hM; N i of type A ) B in context as f : A ) B:N (ft). To see that this is a C-term assume an observer o : ((A ) B ) ) ?) ) ?. Now o(hM; N i) equals u := N ( : B:o(f : A ) B:[ ](ft))) using the fact that N is a C-term and its surrounding context as observer. To show that hM; N i(': A ) B:o(v' )) equals u one uses (- ) on ' and ( )-steps. Now we have hM; N i =def= [':= hM; N i] = M by ( ) and (- ) and Lemma 4.6. Similarly, we have hM; N i[':= 0 ] = N using (-) and (). For the uniqueness of pairing assume j ` P : (A ) B ) ) ? be a C-term of type RA B . We must show that h[':= P ]; 0 [':= P ]if = Pf where f : A ) B is a fresh variable. Writing the left hand side as o(P ) we can rewrite it to P (': A ) B:h[':= v' ]; 0 [':= v' ]hf ). In view of Lemma 4.9 (1) the desired equation follows if we can demonstrate that ':h; 0 if = f . But this follows easily using (- ) on ', (-), and ().
4.1.1 Interpretation of types and contexts We have thus shown that C with the described settings furnishes a continuation
category. Interpreting base types by themselves we obtain immediately Proposition 4.12 For any -type X the interpretation [ X ] in C equals X .
Proof. Immediate from [ A ) B] = R[ A] [ B] = [ A] ) [ B] and [ ?] = ?.
We will henceforth often make implicit use of this proposition by omitting semantic brackets around types. Accordingly, the semantics of a continuation context can be chosen as itself (the choice only aects the names of continuation variables). Next, we examine the interpretation of combined contexts ? j where ? x1 : A1 ; : : : ; xn : An is an object context. Its interpretation R[ ?]] [ ]] is the following continuation context
'1 : A1 ) ?; : : : ; 'n : An ) ?; where the 'i are freshly chosen continuation variables. We introduce the notation 'x for the continuation variable in R[ ?]] [ ]] corresponding to object variable x in ?, in other words we have 'xi = 'i . If ~x is a sequence of object variables we write '~x for the corresponding sequence of continuation variables.
4.1.2 Interpretation of terms
If c: A is a constant then [ c] := f : A ) ?:fc is a C-term of type R[ A] yielding an interpretation for the constants. We thus obtain an interpretation of our -calculus in the continuation category C which associates with every -term ? j ` t : A a C-term [ t] of type R[ A] = A ) ? in context R[ ?]] [ ]] = '1 : A1 ) ?; : : : ; 'n : An ) ?; . Our aim is to exhibit a direct relationship between [ t] and t. To that end we rst introduce the following notation. If ': A ) ? is a continuation variable then ' := : A:[']v is a -term of type A. If t is an -term of type B containing the object variable x: A then we can form the -term t['=x] also of type B which does not contain x anymore but ' instead. More generally, we write t['~ =~x] for t['1 =x1 ] : : : ['n =xn ]. Thus, in particular, if ? j ` t : B is a -term and ~x is the sequence of variables in ? then we have
R[ ?]] ` t['~x = ~x] : B 11
Lemma 4.13 Let t be a -term of type ? containing object variable x: A and let ': A ) ? be a fresh continuation variable. Then ': A ) ?:t['=x] = x: A:t. Proof. We calculate as follows: = = = =
':t['=x] x: A:t['=x][x:: ? = '] x:t['[x:: ? = '] = x] x:t[x=x] x:t
(- ) Def. of substitution (-)
We are now ready to state the desired relationship Theorem 4.14 Whenever ? j ` t : A then [ ? j ` t : A] is equal, w.r.t. E , to the following term
R[ ?]] [ ]] ` f : A ) ?:f (t['~x = ~x]) : RA ) ? where t['~x = ~x] denotes the simultaneous substitution of 'x for x in t for every object variable x in ?.
Proof. For the proof it is appropriate to make explicit the meanings in C of the
informal metalanguage used in the de nition of the semantics of in an arbitrary continuation category. In addition to the already de ned combinators for abstraction and pairing (cur and h?; ?i) we need a combinator for application (de ned from uncur) and projection (de ned by composition from the projection morphisms ; 0 ). The de nition of these combinators on pseudoterms is as follows. app(M; N ) = ev[':= M ][:= N ] = f:N (:M (x:f ([]x))) cur:A (M ) = f : A ) ?:f (: A:M (v? )) hf : RA:ft; N i = f : A ) B:N (ft) M:1 = [':= M ] = f : RA :M (x: A: : B:fx) M:2 = b: B:M (x: A:b) ? = v?
Now we will prove the theorem by induction on the structure of t. In the course of the calculations we omit contexts and those continuation variables which are not explicitly introduced.
Case t = c. Immediate from the de nition. Case t = x: A. We have = = = = =
[ x] v 'x f : A ) ?:[']f f:f (: A:[']v ) f:f ('x ) f:f (x['x = x])
by Lemma 4.5
12
Case t = x: A:s. We write s~ for s['~x = ~x] where the ~x are the free object variables of t. Thus the induction hypothesis reads [ s] = f : B ) ?:f (~s['x =x]). Now we
calculate as follows. = = = = = = = = = = = =
[ x: A:s] cur :RA B (app([[s] ['x := v :1] ; v :2)) by de nition of [ x:s] f : (A ) B ) ) ?:f ( : A ) B:app([[s] ['x := v :1] ; v :2)v? )expansion of cur f:f ( :v :2( : B:[ s] ['x := v :1]v )) expansion of app f:f ( :v :2( :v :1('x :[ s] v ))) expansion of [ s] ['x := v :1] induction hypothesis f:f ( :v :2( :v :1('x :[ ]~s['x =x]))) f:f ( :v :2( :v :1(x:[ ]~s))) by Lemma 4.13 f:f ( :v :2( :[ ]x: 0 :[ ]~s)) expansion of v :1 f:f ( :[ ](x: A: :[ ]x: 0 :[ ]~s)) expansion of v :2 f:f (x: A: : B:[ ]~s) (- ) on , (- ) f:f (x:s~) (-) f:f (x:s['~x =~x]) f:f (t['~x =~x])
Case t = s1s2 . Again, we write s~i for si ['~x=~x]. = = = = = = =
[ s 1 s2 ] cur :B (app([[s1 ] ; h[ s2 ] ; v i)) f:f ( : B:h[ s2 ] ; v i( : A ) B:[ s1 ] v )) f:f ( :hf:f s~2 ; v i( :[ s1 ] v )) f:f ( :v (( :[ s1 ] v )~s2 )) f:f (( :[ s1 ] v )~s2 ) f:f (( :[ ]~s1 )~s2 ) f:f (~s1 s~2 )
induction hypothesis expansion of h?; ?i (-)
Case t = :s. We write s~ for s['~x=~x]. = = = =
[ : A:s] cur:A (app([[s] ; ?)) f:f (:app([[s] ; ?)v? ) f:f (: ? ([[s] (v? ))) f:f (:s~)
Case t = []s. We write s~ for s['~x=~x]. = = = =
[ []s] cur? (app([[s] ; v )) f:f (v (:[ s] v )) f:f ([[s] v ) f:f ([]~s)
This syntactic characterisation of the interpretation of in C now enables us to quickly conclude the main result. 13
Proof of Theorem 4.1. For the rst part assume that ? j ` ti : A for i = 1; 2 are two terms of with equal semantics in the generic continuation category C constructed from E . By Theorem 4.14 this implies that we have f : A ) ?:ft1['~x =~x] = f : A ) ?:ft2['~x =~x] Introducing a fresh variable f : A ) ? we get ft1['~x =~x] = ft2 ['~x =~x] Using Lemma 4.13 iteratively on all the continuation variables in '~x we obtain
'n : : : ('2 :('1 :fti ['~x =~x])x1 )x2 ) : : : xn = fti for i = 1; 2. Therefore, by congruence, we obtain ft1 = ft2 . Now, if : A is a fresh continuation variable we can replace f by v (formally by -abstracting f and using ( )). This yields []t1 = []t2 from which t1 = t2 follows by (-). For the second part assume that F is a C-morphism from R[ ?]] [ ]] to R[ A] . By de nition of C this means that F is a C-map of type A ) ? in context R[ ?]] [ ]]. In view of Lemma 4.5 this means that F takes the form R[ ?]] [ ]] ` f : A ) ?:ft : (A ) ?) ) ? for some -term t of type A. From t we construct another term t~ of type A in context ? j as
t~ := t[x1 :: ? = '1 ][x2 :: ? = '2 ] : : : [xn :: ? = 'n ] We claim that F = [ ? j ` t~] . By Theorem 4.14 this is equivalent to demonstrating that
f:ft = f:f t~['~x =~x] For this it is sucient to show that
t = t~['~x =~x] which is immediate by n-fold application of the following sublemma.
Sublemma If s : C is a -term containing continuation variable ': B ) ? and x: B is a fresh object variable then s[x:: ? = ']['=x] = s. Proof of sublemma. If C ? then s[x:: ? = ']['=x] equals ['](x: B:s[x:: ? = ']) by applying v' to an instance of Lemma 4.13. By (- ) this equals [']':s, thus s by (- ). If C 6 ? then we expand s as : C:[ ]s and apply the previous case to [ ]s. This completes the proof of the main result.
4.2 Representation of Ong's categorical models
In [8] is de ned a categorical semantics of called -categories. By the categorical completeness result of loc. cit. these models are in 1-1-correspondence with -theories. Thus, our results can be stated in category-theoretic terms as follows. The soundness theorem (Thm. 3.3) says that every category of continuations can be organised into a -category with the same objects and whose homsets E ((A1 ; : : : ; An ) ; B ) are given by C( RA1 : : : RAn ; RB ). The completeness theorem (Thm. 4.1), on the other hand, expresses that every -category is (up to 14
isomorphism) of this form. Namely, by Ong's categorical completeness result every
-category is isomorphic to the term model of its theory which in turn (by our main result) is isomorphic to the - category induced by the associated syntactic
category of continuations. Thus, every model of , i.e. any -category is isomorphic to a continuation model. We have preferred to stick to the more traditional syntactic presentation of our results as this simpli es the calculations. It might, however, be instructive to explicitly compute the continuation model which induces the game-theoretic category described in loc. cit.
4.3 Completeness of for CPS-translation
Of particular interest is the \free" -calculus over some signature (B; K) without non-logical axioms. It can be interpreted in the (cartesian closed category associated with) simply-typed lambda calculus with products and terminal object over base types B [ fRg and constants of appropriate type. In this particular case the interpretation gives rise to the following CPS translation of . [ x] = x [ c] = c [ x:t] = p:((x:[ t] ) p:1)p:2 [ ts] = :[ t] h[ s] ; i [ :t] = :[ t] ? [ []t] = x: 1:[ t] These clauses are derived by instantiating the de ning clauses in Section 3 by the term model of simply typed lambda calculus where we keep using the same name for a syntactic and semantic variables. This particular semantics is already complete for free . Proposition 4.15 We have ? j ` t1 = t2 : A if and only if [ t1] = [ t2] .
Proof. If [ t1] = [ t2] then t1 and t2 have equal interpretation in any continuation category because simply-typed lambda calculus is initial for those. Thus ? j ` t1 = t2 : A by Thm. 4.1. The other direction is immediate from soundness (Thm. 3.3). This result means that the free -theory can be decided by way of the above CPS translation.
5 Call-by-value In [7] has been de ned a call-by-value version of which is validated by the usual continuation semantics for call-by-value lambda calculus as described e.g. in [4]. Our aim in this section is to establish a completeness result analogous to Thm 4.1 above for this system. As it happens the terms and typing rules of this call-by-value variant are exactly the same as for the call-by-name version. In order to state the equational axioms of the call-by-value version we need some syntactic machinery beforehand. The variables and -abstractions are called values and are ranged over by letters u; v; : : : . The evaluation contexts are inductively de ned by
E ::= [] j vE j Et j []E 15
( V )
(x: A:t)v = t[v=x]:
(V )
x: A:vx = v; when x is not free in v:
(- )
[] : A:t = t[= ]:
(-)
: A:[]t = t; not free in t: E [: A:t] = : B:t[[ ]E = ], if B 6= ? (-V ) E [: A:t] = t[E = ] (? ) (0? )
v t = : A:t where 6= ? v t = t if v : ? ) ? and t : ?
(E )
(x: A:E [x])t = E [t] Figure 5: Equality axioms for
We write E [t] for the substitution of t for [] in evaluation context E . Note that since evaluation contexts do not involve binders no free variables in t can ever be captured in E [t] so no renaming of variables in t is necessary. Besides usual (capture-free) substitution t[s=x] of a term s for a variable x in a term t we also have a substitution of evaluation contexts for continuation variables t[E=] where the key clauses are ([]t)[E=] = E [t[E=]] ([ ]t)[E=] = [ ](t[E=]), if 6= This is homomorphically extended to the other term formers. When substituting into a -abstraction capture of free continuation variables in E must be avoided by appropriate renaming. This substitution is type correct only if the type of and the type of the \hole" [] in E agree and, moreover, E itself is of type ?. Notice that in [7] this substitution is available only for evaluation contexts of the form [ ]E and written t[ ; E=] in this case. De nition 5.1 A cbv--theory (over a signature (B; K)) is a set E of typed equations of the form ? j ` s = t : A where ? j ` s : A and ? j ` t : A such that E is a congruence stable under weakening, E contains all well-typed instances of the basic equality laws depicted in Figure 5.
Apart from minor simpli cations in notation the main dierence to Ong-Stewart is that our evaluation contexts are closed under labelling (i.e. []E is an evaluation context if E is). The eect of this extension is concentrated in the following special case of axiom (E): (x: A:[]x)t = []t 16
Would we add this equation explicitly we could stick to the Ong-Stewart formulation. The more liberal notion of evaluation context is adopted because it is validated by the standard continuation semantics given below. As before we will henceforth adopt Convention 4.2. Again, this allows us to formally subsume both cases of (-V ) under the rst one. Similarly, Axiom (0? ) is subsumed under (? ).
5.1 Continuation models for the call-by-value case
De nition 5.2 [Category of values] A category of values is given by the following data: 1. A category V with a distinguished class T of objects of V called type objects. 2. A distinguished type object R of responses. 3. For every object ? and type object A a chosen cartesian product ? A. 4. A chosen terminal object [] (for the empty context). 5. A chosen initial object 0 (to interpret ?). 6. For every type object A a chosen exponential RA 2 T of R by A.
7. For any two type objects A and B a chosen exponential (RRB )A 2 T of RRB by A. Particular examples are the category of sets (with 0 = ;) and the category of cpos with or without bottom (\predomains") with Scott-continuous maps. In this case the choice of an actual domain, i.e., with bottom element, for R guarantees the availability of a least xpoint operator in the ensuing continuation model. Assume a xed signature (B; K) and a category of values V . Any assignment of type objects [ B ] to base types B is extended to compound types as follows. [ ?] = 0 [ A ) B ] = (R2 ([[B ] ))[ A] Here the intuition is that [ A] is the space of abstract values of type A. An arbitrary term of type A will not be interpreted as an element of [ A] but rather as an element of R2 ([[A] ). Since [ ?] = 0 is an initial object we have R2 ([[?] ) = R. Therefore, denotations of terms of type ? correspond to elements of R, i.e., responses. Let ? x1 : A1 ; : : : ; xn : An be an object context and 1 : B1 ; : : : ; m : Bm be a continuation context. We use the notation [ ?]] R[ ]] for the object [] [ A1 ] : : : [ An ] R[ B1 ] : : : R[ Bm ] Assume an assignment of denotations to the constants, i.e. a morphism [ c] : [] ! R2 ([[A] ) if c: A is in K. To each sequent ? j ` t : A we associate a morphism [ ? j ` t : A] : [ ?]] R[ ]] ! R2 ([[A] ) by the clauses in Figure 6. Again, we use an informal lambda calculus to denote constructions in V . We write X : X ! R2 (X ) for the V -morphism de ned by X (x: X ) = k: RX :kx. We use ?X : 0 ! X for the unique morphism from the initial object 0 to X .
17
[ ? j ` xi : A] (~x j ~ )
= [ A] (xi )
[ ? j ` x: A:t : A ) B ] (~x j ~ ) = [ A)B] (v: [ A] :[ ?; x: A j ` t : B ] (~x; x j ~ ))
k: R[ B] :[ ? j ` t : A ) B ] (~x j ~ ) (f : [ A ) B ] :[ ? j ` s : A] (~x j ~ )(v: [ A] :fvk))
[ ? j ` ts : B ] (~x j ~ )
=
[ ? j ` : A:t : A] (~x j ~ )
= k: R[ A] :[ ? j ; : A ` t : ?] (~x j ~ ; k)(x: 0:?R (x))
[ ? j ` [i ]t : ?] (~x j ~ )
= k: R0 :[ ? j ` t : A] (~x j ~ )i
Figure 6: Interpretation of in a category of values We remark that the interpretation of the lambda calculus fragment coincides with the usual cbv continuation semantics of or equivalently the cps translation of lambda calculus given, e.g., in[11]. The following soundness theorem is proved by induction on typing derivations where we maintain the additional invariant that the interpretation of a value ? j ` v : A factors through [ A] . As a sublemma we use that [ ? j ` E [t] : B ] (~x j ) = k: R[ B] :[ ? j ` t] (~x j ~ )(v: [ A] :[ ?; x: A j ` E [x]]](~x; v j ~ )k which is easily established by induction on the structure of E . Theorem 5.3 (Soundness) The set of equations ? j ` t1 = t2 : A where t1; t2 are appropriately typed terms and [ ? j ` t1 : A] = [ ? j ` t2 : A] is a cbv- theory.
5.2 Completeness of continuation semantics
Again, our goal is to establish the following result. Theorem 5.4 For every cbv- -theory E over a signature (B; K) there exists a category of values (V ; R) and an interpretation of base types and constants with the following two properties. 1. E is the theory induced by this interpretation. 2. Let ? be an object context, be a continuation context and A be a type. Every V -morphism f : [ ?]] R[ ]] ! R2 ([[A] ) arises as the interpretation of some -term ? j ` t : A. Assume a xed signature (B; K) and a theory E . We are going to describe a particular category of values V from these data. De nition 5.5 Let ? be an object context and A be a type. A semantic value term (V-term for short) of type A in context ? is a term ? j ` t : A such that ?; ?0 j ` (x: A:s)t = s[t=x] : C for every term ?; ?0 j ` x: A:s : A ! C . More generally, a semantic value map (V-map for short) from ? to x1 : A1 ; : : : ; xn : An is an n-tuple (t1 ; : : : ; tn ) such that ti is a V-term of type Ai in context ?. By ( V ) every syntactic value (term ranged over by u; v; : : : ) is a V-term. Depending on the theory there may be more. We will use capital letters U; V; to range over semantic values. For a V-term we refer to the de ning property (x:t)V = t[V=x] by ( V ) as well. 18
Theorem 5.6 The object contexts with E -equivalence classes of V -maps as morph-
isms and componentwise substitution form a category with cartesian products given by iuxtaposition.
Proof. We only have to show that substitution is well-de ned on equivalence classes. This, however, is immediate from ( V ): If ?; x: A j V1 = V2 : C and ? j ` U : A are V-maps as indicated then (in ? j ) we have V1 [U=x] = (x: A:V1 )U = (x: A:V2 )U = V2 [U=x] The rest literally follows the standard proof [10] that contexts and substitutions form a category with nite products. Although the morphisms in V formally are equivalence classes we will mostly refer to them via representatives without explicitly saying so as this simpli es the exposition. Lemma 5.7 If ? j ` U : ? then ?; ?0 j ` s = t : C for all terms s; t.
Proof. We have t = (x: ?:t)U = : C:U by ( V ) and (? ).
Corollary 5.8 The context 0 x: ? is an initial object in V . Proof. The morphism ?x1:A1;:::;xn:An is the tuple consisting of i : Ai :x. The required instance of ( V ) as well as the uniqueness are special cases of the previous lemma. The type object T are the contexts of length one. So 0 is a type object as required. We will henceforth notationally identify type objects and types thus writing e.g. ? rather than x: ? for 0. Theorem 5.9 The category V together with R = (? ) ?) ) ? is a category of values. The exponential RA is given by A ) ?; the exponential R2 (B )A is given by A ) B.
Proof. We only give the raw data establishing the required structure; the veri c-
ations consist of lengthy but essentially straightforward equational reasoning. For a very similar system they are explicitly carried out in [4]. Let U : A ) ? and V : A be V-terms (in some implicit ambient context ?). We de ne the application app(U; V ) : R as k: ? ) ?:k(U V ). Conversely, if x: A ` V : R then we de ne cur x:A (V ) : A ) ? as x: A:V (y: ?:y). Now let U : A ) B and V : A be V-terms. We de ne the application app(U; V ) : (B ) ?) ) ? as k: B ) ?:k(U V ). If x: A ` U : (B ) ?) ) ? then we de ne curx:A (V ) : A ) B as x: A: : B:V (v ) where as before v = x: B:[ ]x.
In view of Convention 4.2 application and abstraction can be given by literally the same formulas which justi es the use of the same operator names for both. Lemma 5.10 Let U : C ) D and V : C be V-terms possibly containing a free variable x : A. Then curx:A(app(U; V ) = x: A:UV
19
Proof. = = = = =
curx:A(app(U; V ) x: A:(: D:app(U; V )v ) x: A:(: D:(k: D ) ?:k(UV ))v ) x: A:: D:(y: D:[]y)(UV ) x: A:: D:[](UV ) x: A:UV
by ( V ) by (E) by (-)
Now we consider the interpretation of in V induced by [ B ] = B for base types and [ c] = k: A ) ?:kc when c : A in K. If ? x1 : A1 ; : : : ; xn : An and 1 : B1 ; ; m : Bm then we write [ ?]] R[ ]] as x1 : A1 ; : : : ; xn : An ; f 1 : B1 ) ?; : : : ; f m : Bm j . Theorem 5.11 For ? j ` t : A we have [ ?]] R[ ]] ` [ ? j ` t : A] = k: A ) ? : (A ) ?) ) ?:kt where t is t[f 1 [] = 1 ] : : : [f n [] = m ].
Proof. By induction on the structure of t. We write ? x1 : A1 ; : : : ; xn : An and 1 : B1 ; : : : ; m : Bm . The cases where t is a variable or a constant are immediate from the de nition as x = x and c = c. Case t = x: C:s and A = C ) D. = = = = = = =
[ ? j ` x: C:s] curk:[[C )D] )? (app(k; curx:C ([[?; x: C j ` s : D] ))) k:k(x: C:: D:[ ? j ` s : D] v ) k:k(x: C:: D:(q: D ) ?:q t )v k:k(x: C:: D:v t ) k:k(x: C:: D:[]t ) k:k(x: C:t ) k:k(x: C:t)
by Lemma 5.10 by IH by ( V ) by (E) by (-) by def. of ?
Case t = t1 t2. We assume that t1 : C ) A and t2 : C . Now we calculate as
follows. = = = = = =
[ ? j ` t1 t2 : A] curk:A)? (app([[? j ` t1 ] ; curf :C )A (app([[? j ` t2 : C ] ; curv:C (app(app(f; v); k)))))) k:([[? j ` t1 : C ) A] (f:([[? j ` t2 : A] (v:k(fv))))) by Lemma 5.10 k:(q:qt1 )(f:(r:rt2 )(v:k(fv))) by IH k:(q:qt1 )(f:(k(ft2 ))) by ( V ),(E) k:k(t1 t2 ) by ( V ),(E) k:k(t1 t2 )
20
Case t = : A:t1 . Let us write t~1 for t1 [f 1 [] = 1] : : : [f m [] = m]. We have t1 =
t~1 [f [] = ] and furthermore (: A:t1 ) = : A:t~1 . Now we calculate as follows. = = = = = =
[ ? j ` : A:t1 ] curf :A)? (app([[? j ; : A ` t1 : ?] ; curx:? (: R:x))) f :[ ? j ; : A ` t1 : ?] (x: ?:x) Lemma 5.10+Cor. 5.8 f :t1 by IH, ( V ),(E) f :t~1 [f [] = ] f :f (: A:t~1 ) (- ) f :f (: A:t1 )
Case t = [ i ]t1. = = = = =
[ ? j ` [ i ]t1 ] curk:?)? (app([[? j ` t1 : Bi ] ; f i )) k: ? ) ?:[ ? j ` t1 ] f i k: ? ) ?:f i t1 k: ? ) ?:([ i ]t1 ) k: ? ) ?:kt
IH; ( V ) Def. of ? (? )
Proof of Theorem 5.4 Ad 1. Let t : C be a term possibly containing a continuation variable : A. By (-V ) we have
t[f [] = ] = : C:f (:[ ]t) where is a fresh continuation variable. It follows that if t1 = t2 2 E then t1 = t2 2 E and hence [ t1 ] = [ t2 ] by Thm. 5.11. Conversely, suppose ? j ` t1 ; t2 : A. Then for i = 1; 2 ti = :[]ti = :(k:kti )v = :[ ti ] v by Thm. 5.11. So, if [ t1 ] = [ t2 ] then t1 = t2 . Substituting v i for f i yields t1 = t2 using ( V ) and (E).
Ad 2. Let [ ?]] R[ ]] ` V : (A ) ?) ) ? be a V-term. We de ne a term t with [ ? j ` t : A] = V by t :(V [v 1 =f 1 ] : : : [v 1 =f 1 ])v . Now t = :V v by (E). Now = = = = =
[ t] k:kt k:k(:V v ) k:V (v [k[]=]) k:V k V
by Thm. 5.11 (-V )
It can be shown that our axiomatisation of call-by-value without non-logical axioms is complete for the usual continuation-passing style translation of call-byvalue -calculus into simply typed lambda calculus. We omit the details as they closely follow the development in Section 4.3. 21
Types: A ::= B j ? j A1 ) A2 Contexts: ? x1 : A1 ; : : : ; xn : An Terms: t ::= K j CA j x j x: A:t j t1 t2 Values: v ::= x j x: A:t Evaluation contexts: E ::= [] j vE j Et Typing rules:
x1 : A 1 ; : : : x n : An ` xi : A i ? ` t1 : A ) B ? ` t 2 : A ? ` t1 t2 : B
c: A 2 K ?`c:A
? ` CA : ((A ) ?) ) ?) ) A
?; x: A ` t : B ? ` x: A:t : A ) B
Equations: ( V ) (x: A:v)s = t[v=x]: (V ) x: A:vx = v; when x is not free in v: (C - ) CA (k: A ) ?:kt) = t (C - ) E [CA t] = CB (k: B ) ?:t(x: A:kE [x])) (C? ) C? t = t (x: ?:x) (E )
(x: A:E [x])t = E [t] Figure 7: Syntax and equations for C
5.3 Equivalence of call-by-value and call-by-value C
In a series of papers (see e.g., [3, 13]) Felleisen and his co-workers have studied extensions of the untyped lambda calculus by a control operator C which allows one to access the current continuation of a term. A typed version of this system called C has been introduced in [4]. The main result of that paper was that a certain equational aiomatisation of C is complete for the usual continuation semantics albeit over arbitrary cartesian closed categories of values. We will now describe a slightly simpli ed version of C and give back-and-forth translations to call-by-value . The types of C are the same as the -types. Accordingly, the notion of signature is not changed either. Assume a signature (B; K). The terms of C are the terms of simply-typed lambda calculus over this signature extended by a family of constants CA : ((A ) ?) ) ?) ) A for each type A. Figure 7 contains a formal de nition. De nition 5.12 [C -theory] A C -theory is a set of typed equations ? ` t1 = t2 : A 22
closed under weakening and congruence rules containing all well-typed instances of the basic equations in Fig. 7.
De nition 5.13 If is a continuation context of then let stand for the context which contains a binding f : A ) ? for every binding : A in . To each -term ? j ` t : A we associate a C -term t of type A in context ?; by the following inductive de nition. x = x c = c (x: A:t) = x: A:t (t1 t2 ) = t1 t2 (: A:t) = CA (f : A ) ?:t ) ([]t) = f t
In order to formulate an inverse translation it is convenient to assume two kinds of variables in C ; ordinary ones which become object variables under the translation and special ones of the form f which become continuation variable under the translation. In order that this is possible we require that an f -variable always has negated type, i.e. one of the form A ) ?. This re nement is merely for convenience. It does not aect the equational theory and the typing rules which do not distinguish between the two kinds of variables. It would be possible to avoid the distinction between the two kinds of variables by formulating the translation relative to a list of variables which are to be translated into continuation variables. This, however, would clutter the subsequent proofs. If ? is a C context let ?ob be the -object context consisting of all bindings x: A in ? where x is an ordinary variable; let ?cont be the continuation context consisting of all bindings : A where f : A ) ? is in ? and f is a \special" variable. Note that ? is a permutation of ?ob ; ?cont and also (? j )ob = ? and (? j )cont = . Now, for a C -term ? ` t : A we construct a -term ?ob j ?cont ` t : A by
x = x c = c f = v (x: A:t) = x: A:t (t1 t2 ) = t1 t2 CA = f : (A ) ?) ) ?::f (v )
The task of continuation context is to identify those variables in t which are to become continuation variables. If we merely want to map a C -term into then we can always choose to be the empty context. We can also translate theories as follows. If E is a -theory then let E stand for the set of equations
f? ` t1 = t2 : A j ?ob j ?cont ` t1 = t2 : A 2 Eg Conversely, if E is a C -theory then let E stand for the set of equations f? j ` t1 = t2 j ?; ` t1 = t2 : A 2 Eg Theorem 5.14 If E be a cbv- -theory and ? j ` t : A then ? j ` t = (t) : A is in E . Moreover, E is a C -theory. Conversely, if E is a C -theory and ? ` t : A then ? ` t = (t ) is contained in E . Moreover, E is a cbv- -theory. 23
Proof. The fact that the translations and are mutually inverse up to any theory is an immediate induction on the structure of t. The fact that E and E
are theories amounts to checking that the basic equality axioms are mapped to theorems. For axioms ( V ); (V ); (E ) this is immediate from the de nition. For axiom (- ) we notice that (t[E = ]) = t [x:E [x]=f ]. Equation (- ) then follows from (C - ) and ( V ); (E ). All other axioms are direct.
This translation allows us to transport our completeness result Thm. 5.4 to C thus extending the results in [4] to arbitrary theories. This might be of interest as many applications of control operators involve general recursion and recursive datatypes both of which can be subsumed under appropriate equational theories. We close this section by remarking that a similar translation for the original, i.e., call-by-name, -calculus does not seem possible. The reason is that C does not distinguish between the application of a variable to a term and \naming" of a term, i.e., the operation t 7! []t. However, in call-by-name the latter operation can be moved inside a -abstraction (by (- )) whereas the former cannot. This is not in con ict with the translation described in [2] as the latter only validates the computational rules of , i.e., ( ),(- ), but not rules ()and (- ),(-). Since (- ) is not part of the equations to be translated the abovementioned dif culty does not occur. We agree with de Groote that rules like () or (- ) are irrelevant from a computational point of view. They are, however, important logical principles for reasoning about open terms.
6 Conclusions and further work In the rst four sections we have presented a natural continuation style interpretation of call-by-name -calculus and demonstrated that the equational theory of is complete with respect to this interpretation. In Section 5 we have extended our results to a call-by-value version of and| via a back-and-forth translation|extended previous results in [4] and [13] to arbitrary theories. However, we wish to stress that the main contribution of the paper is the completeness proof for the call-by-name calculus because unlike in [13, 4] the equational theory for call-by-name predated the continuation style interpretation which provides evidence for its canonicity. Indeed, although known for a while, the present CPS translation for call-by-name using pairs does not seem to have received the attention it deserves. We hope that this paper will help to popularize it. It should be stressed that the precise formulation of our notions of model, e.g. the restriction of exponentiation to certain rather peculiar objects and the duplication of products and terminal object, are not essential for completeness. As mentioned above, every cartesian-closed category together with a distinguished object R de nes a category of continuations. Conversely, given an arbitrary category of continuations (C ; R) the category of presheaves C^ is cartesian closed and the full and faithful Yoneda embedding Y : C ! C^ preserves existing products and exponentials. Therefore, the continuation semantics with respect to C^ and Y (R) agrees up to isomorphism with the interpretation in (C ; R) composed with Y . Thus, the completeness result Thm. 4.1 continues to hold for the restricted class of cartesian closed -categories. A similar construction can be carried out in the call-by-value situation; in this case a certain complication arises through the fact that the Yoneda embedding does not preserve initial object. This can be remedied by restricting V^ to the full subcategory consisting of those presheaves F for which F (0) is a singleton. See [4] for details. Some directions for further work suggest themselves. 24
Thielecke [14] has described a notion of categorical model for call-by-value lambda calculi with control operators. We believe that using our methods one can show that every instance of this notion is equivalent to a model in which continuations are interpreted as functions1 , i.e., a standard model in the terminology of [14]. The details of such a completeness proof may be messy but we expect no intrinsic diculty. Although our formulation of -syntax is very general and emcompasses, e.g., xpoint operators and recursive types (via fold/unfold constants), it does not immediately extend to an untyped formulation of . The reason is that equations (- ) and (? ) (as well as the Convention 4.2) are type dependent and would become unsound if all types were identi ed. Another interesting topic for future research might be to derive complete axiomatisations for with additional structure such as inductively de ned datatypes. More concretely, consider the class of those continuation categories which support natural numbers and lists (in a suitably formalised sense). The continuation semantics allows one to model extended by natural numbers and lists in any such category. The task would be to nd an axiomatisation of the -theory arising from these interpretations. This might be of use for equational transformation of -programs manipulating concrete datatypes.
References [1] Boris Agapiev and Eugenio Moggi. Declarative Continuations and Monads. unpublished draft, July 1991. [2] Philippe de Groote. On the relation between -calculus and the syntactic theory of sequential control. In F. Pfenning, editor, Proc. 5th International Conference on Logic Programming and Automated Reasoning, LPAR'94, pages 31{43. Springer Verlag, 1994. volume 822 of Lecture Notes in Arti cial Intelligence. [3] Matthias Felleisen et al. A Syntactic Theory of Sequential Control. TCS, 52:205{237, 1987. [4] Martin Hofmann. Sound and complete axiomatisations of call-by-value control operators. Math. Struct. Comp. Sci, 5:461{482, 1995. [5] Jean-Louis Krivine. Classical logic, storage operators, and second-order lambda calculus. Annals of Pure and Applied Logic, 68:53{78, 1994. [6] Y. Lafont, B. Reus, and T. Streicher. Continuation semantics or expressing implication by negation. Technical Report 93-21, University of Munich, 1993. [7] C.-H. L. Ong and C. A. Stewart. A curry-howard foundation for functional computation with control. In Proceedings of ACM SIGPLAN-SIGACT Symposium on Principle of Programming Languages, Paris, January 1997. ACM Press, 1997. [8] Luke Ong. A semantic view of classical proofs. In Proceedings of LICS '96. IEEE Press, 1996. [9] Michel Parigot. -calculus: an algorithmic interpretation of classical matural deduction. In Proc. LPAR '92, vol. 624 of LNCS. Springer, 1992. [10] Andrew Pitts. Categorical logic. In Handbook of Logic in Computer Science (Vol. VI). Oxford University Press, 1997. To appear. 25
[11] Gordon Plotkin. Call-by-name, call-by-value and the -calculus. Theoretical Computer Science, 1:125{159, 1975. [12] B. Reus and T. Streicher. Continuation Semantics : Abstract Machines and Control Operators, 1996. submitted. [13] Amr Sabry and Matthias Felleisen. Reasoning about programs in continuationpassing style. Lisp and Symbolic Computation, 6(3/4):287{358, 1993. Preliminary version in Proceedings of the ACM Conference on Lisp and Functional Programming, 1992. Also Technical Report 92-180, Rice University. [14] Hayo Thielecke. Categorical Structure of Continuation Passing Style. PhD thesis, University of Edinburgh, 1997. also as LFCS technical report ECSLFCS-97-376.
26