LISP AND SYMBOLIC COMPUTATION: An International Journal, ?, ??{ ??, 1993
c
1993 Kluwer Academic Publishers { Manufactured in The Netherlands
Polymorphic Typ e Assignment and CPS Conversion
y
ROBERT HARPER ([email protected] )
z
MARK LILLIBRIDGE (md [email protected] )
School of Computer Science
Carnegie Mel lon University
5000 Forbes Avenue
Pittsburgh, PA15213
Keywords: Polymorphism, continuations
Abstract. Meyer and Wand established that the typ e of a term in the simply typ ed -
calculus may b e related in a straightforward manner to the typ e of its call-by-value CPS
transform. This typing prop ertymay b e extended to Scheme-likecontinuation-pas sin g
primitives, from which the soundness of these extensions follows. We study the extension
of these results to the Damas-Milner p olymorphic typ e assignment system under b oth
the call-by-value and call-by-name interpretations. We obtain CPS transforms for the
call-by-value interpretation, provided that the p olymorphic let is restricted to values,
and for the call-by-name interpretation with no restrictions. We provethatthereis
no call-by-value CPS transform for the full Damas-Milner language that validates the
Meyer-Wand typing prop erty and is equivalent to the standard call-by-value transform
up to op erational equivalence.
1. Intro duction
In their study of the relationship b etween direct and continuation seman-
!
tics for the simply typ ed -calculus ( ), Meyer and Wand note that the
!
typ e of a term in may b e related in a simple and natural way to the
typ e of its call-by-value continuation passing style (CPS) transform [13].
This result may b e extended to the calculus that results from extend-
!
ing with Scheme-likecontinuation-passing primitives callcc and throw
! !
( + cont )[1, 4]. Since under a call-by-value op erational semantics is
\typ e safe" in the sense of Milner [14, 2], and since the call-by-value CPS
transform faithfully mimics the call-by-value semantics [17], it follows that
This is a revised version of a pap er presented at the ACM SIGPLAN Workshop on
Continuations, San Francisco, June 1992.
y
This work was sp onsored by the Defense Advanced Research Pro jects Agency, CSTO,
under the title \The Fox Pro ject: Advanced Development of Systems Software", ARPA
Order No. 8313, issued by ESD/AVS under Contract No. F19628{91{C{0168.
z
Supp orted by a National Science Foundation Graduate Fellowship.
2 HARPER AND LILLIBRIDGE
!
+ cont under a call-by-value op erational semantics is also typ e safe.
In a subsequent study Duba, Harp er, and MacQueen studied the addition
of callcc and throw to Standard ML [15]. The extension of the Meyer-Wand
!
transform to + cont establishes the soundness of the monomorphic frag-
ment of the language, but the soundness of the p olymorphic language with
continuation-passing primitives was left op en. It was subsequently proved
by the authors [9] that the full p olymorphic language is unsound when
extended with callcc and throw. The source of this discrepancy maybe
traced to the interaction b etween the p olymorphic let construct and the
typing rules for callcc.Several ad-hoc metho ds for restricting the language
to recover soundness have b een prop osed [8, 23].
In this pap er we undertake a systematic study of the interaction b etween
continuations and p olymorphism by considering the typing prop erties of
the CPS transform for b oth the call-by-value and call-by-name variants of
the Damas-Milner language [2] and its extension with continuation-passing
primitives. We obtain suitable extensions of the Meyer-Wand theorem for
the call-by-value CPS transform, provided that the p olymorphic let is re-
stricted to values, and for the call-by-name transform, under no restrictions.
Finally,we prove that there is no call-by-value CPS transform for the full
Damas-Milner language that b oth satis es the Meyer-Wand typing prop-
erty and is equivalent to the usual transform up to op erational equivalence.
In particular, the standard call-by-value CPS transform fails to preserve
typability.
2. Untyp ed Terms
The language of untyp ed terms is given by the following grammar:
e ::= x j x:e j e e j let x be e in e j callcc j throw
1 2 1 2
Here x ranges over a countably in nite set of variables. We include the let
construct as a primitive b ecause it is needed in the discussion of p olymor-
phic typ e assignment. The constants callcc and throw stand for continuation-
passing primitives whose de nitions are derived from analogous constructs
in Scheme [1] and Standard ML of New Jersey [4].
We consider two CPS transforms for untyp ed terms, corresp onding to
the call-by-value and call-by-name op erational semantics [17]. EachCPS
transform consists of a transformation j j for untyp ed terms and a trans-
formation jj jj for untyp ed values. Exactly what is considered a value de-
p ends on which op erational semantics is b eing used. Under a call-by-value
interpretation, the set of values is given by the following grammar:
v ::= x j x:e j callcc j throw
POLYMORPHIC TYPE ASSIGNMENT AND CPS CONVERSION 3
Since throw is a two-argument function, it is p ossible to regard throw v as
avalue. Wecho ose to not regard it as a value b ecause to do so compli-
cates somewhat b oth the transform and the asso ciated theorems due to the
resulting overlap b etween the application and value cases. Under a call-by-
name interpretation, the set of values is given by the following grammar:
n ::= x:e j callcc j throw
1
De nition 1 (Call-by-Value CPS Transform)
jv j = k :k jjv jj
cbv cbv
je e j = k :je j (x :je j (x :x x k ))
1 2 cbv 1 cbv 1 2 cbv 2 1 2
jlet x be e in e j = k :je j (x:je j k )
1 2 cbv 1 cbv 2 cbv
jjxjj = x
cbv
jjx:ejj = x:jej
cbv cbv
jjcallccjj = f :k :f k k
cbv
jjthrowjj = c:k :k (x:l :c x)
cbv
The transformation of let expressions is chosen to re ect the usual \sequen-
tial" evaluation strategy for let's in a call-by-value language in which the
let-b ound expression, e , is fully evaluated prior to evaluation of the b o dy,
1
e . (Compare this with the call-by-name transform in De nition 2 b elow.)
2
The transformation of callcc di ers from that in Scheme since continuations
are not here represented as functions which are applied to their arguments,
but rather are represented directly as continuations which are invoked us-
ing the throw primitive. (See Harp er, Duba, and MacQueen [8] for further
discussion of this p oint.)
The call-by-value CPS transform is comp ositional in the sense that it
commutes with substitution.
Lemma 1
0 0
1. jj [v=x]v jj =[jjv jj =x ] jjv jj .
cbv cbv cbv
2. j [v=x]e j =[jjv jj =x ] jej .
cbv cbv cbv
Pro of: The pro of pro ceeds by simultaneous induction on the struc-
0
ture of v and e.Wegive here a few illustrative cases; the remainder
followby a similar argument. We drop the \cbv " subscript for the
sake of readability.
1
In each equation any b ound variable o ccurring on the right that do es not also o ccur
on the left is assumed to b e chosen so as to avoid capture.
4 HARPER AND LILLIBRIDGE
0
v = x :
0 0
Wehaveby de nition jjxjj = x, and hence [jjv jj=x]jjxjj = jjv jj =
0
jj[v =x]xjj.
0 0
v = x 6= x :
0 0 0 0 0
Wehaveby de nition jjx jj = x , and hence [jjv jj=x]jjx jj = x =
0 0
jj[v =x]x jj.
0
v = y :e :
Wemay assume without loss of generality that y 6= x and that
y do es not o ccur freely in jjv jj.
jj[v=x]y :ejj = jjy :[v=x]ejj
= y :j[v=x]ej
= y :[jjv jj=x]jej (by induction)
= [jjv jj=x]y :jej
= [jjv jj=x] jjy :ejj
0
e = v :
Wemay assume without loss of generalitythat k do es not o ccur
0
freely in jjv jj. Note that values are stable under substitution of
avalue for a variable.
0 0 0
j[v=x]v j = k :k jj[v=x]v jj ( [v=x]v is a value)
0
= k :k [jjv jj=x]jjv jj (by induction)
0
= [jjv jj=x]k :k jjv jj
0
= [jjv jj=x] jjv jj
e = e e :
1 2
Wemay assume without loss of generalitythatthevariables x ,
1
x , and k do not o ccur freely in jjv jj.
2
j[v=x](e e )j = j[v=x]e [v=x]e j
1 2 1 2
= k :j[v=x]e j (x :j[v=x]e j (x :x x k ))
1 1 2 2 1 2
= k :[jjv jj=x]je j (x :[jjv jj=x]je j (x :x x k ))
1 1 2 2 1 2
= [jjv jj=x]k :je j (x :je j (x :x x k ))
1 1 2 2 1 2
= [jjv jj=x]je e j
1 2
0
We shall also haveneedofavariant call-by-value CPS transform (cbv )
de ned on untyp ed terms satisfying the restriction that all let expressions
are of the form let x be v in e | i.e.,thelet-b ound expression is required to
b e a (call-by-value) value. Because of this restriction, a simpler rule can b e
given for the let case:
0 0 0
jlet x be v in ej = k :let x be jjv jj in (jej k )
cbv cbv cbv
This simpler rule for let expressions is the only di erence b etween the two transforms.
POLYMORPHIC TYPE ASSIGNMENT AND CPS CONVERSION 5
0
Lemma 2 Let v and v be values obeying the restriction on let expressions
and e be a term obeying the restriction on let expressions. Then
0 0
0 0 0
1. jj [v=x]v jj =[jjv jj =x ] jjv jj .
cbv cbv cbv
0 0 0
. =x ] jej =[jjv jj 2. j [v=x]e j
cbv cbv cbv
2
De nition 2 (Call-by-Name CPS Transform)
jnj = k :k jjnjj
cbn cbn
jxj = x
cbn
je e j = k :je j (x :x je j k )
1 2 cbn 1 cbn 1 1 2 cbn
jlet x be e in e j = k :let x be je j in (je j k )
1 2 cbn 1 cbn 2 cbn
jjx:ejj = x:jej
cbn cbn
0 0
jjcallccjj = f :k :f (f :f (l :l k ) k )
cbn
0 0 0 0
jjthrowjj = c:k :k (x:l :c (c :x (x :c x )))
cbn
The reader may nd it helpful to b ear in mind that in call-by-name variables
are b ound to susp ensions, rather than to values. The rather complicated
de nitions of jjcallccjj and jjthrowjj may b e attributed to this fact.
cbn cbn
Lemma 3
1. jj [e=x]n jj =[jej =x ] jjnjj .
cbn cbn cbn
0 0
2. j [e=x]e j =[jej =x ] je j .
cbn cbn cbn
0
Pro of: By simultaneous induction on the structure of n and e .
The relationship b etween the call-by-value and call-by-name CPS trans-
forms and the corresp onding call-by-value and call-by-name op erational se-
mantics was established for pure -terms by Plotkin [17] and was extended
to continuation-passing primitives (for the call-by-value case) by Grin [6].
(See also [5, 19, 20,21, 3].)
Theorem 1 (Plotkin, Grin) The closedexpression e evaluates to v
under cal l-by-value i jej (x:x) evaluates to jjv jj under either cal l-
cbv cbv
by-value or cal l-by-name.
In particular, if e evaluates to a constant c, then jej (x:x)evaluates
cbv
to c, and vice-versa, assuming that jjcjj = c. A similar result for the
cbv
call-by-name interpretation may also b e proved [17].
2
In each equation any b ound variable o ccurring on the right that do es not also o ccur
on the left is assumed to b e chosen so as to avoid capture.
6 HARPER AND LILLIBRIDGE
3. Simple Typ e Assignment
In this section we review Meyer and Wand's typing theorem for the call-
!
by-value CPS transform for the simply-typ ed -calculus ( ), and present
an analogous result for the call-by-name CPS transform.
!
De nition 3 ( Typ es and Contexts)
ty pes ::= b j !
1 2
contexts ::= j ;x:
Here b ranges over a countable set of base typ es. We assume that among
the base typ es there is a distinguished typ e , which will b e used in what
follows to represent the \answer" typ e of a CPS transform.
!
De nition 4 ( Typing Rules)