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

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 jj for untyp ed terms and a trans-

formation jjjj 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)

.x :(x) (var)

;x: .e : 

1 2

(x 62 dom()) (abs)

.x:e:  ! 

1 2

.e :  !  .e : 

1 2 2 2

(app)

.e e : 

1 2

.e :  ;x: .e : 

1 1 1 2

(x 62 dom()) (mono-let)

. let x be e in e : 

1 2

!

The typ e system  + cont is de ned by adding the typ e expression

 cont and the following typing rules for the continuation-passing primitives:

. callcc :( cont !  ) !  (callcc)

0

. throw :  cont !  !  (throw)

!

De nition 5 (Call-by-Value Typ e Transform for  )

j j = (jj jj ! ) !

cbv cbv

jjbjj = b

cbv

jj ! jj = jj jj !j j

1 2 cbv 1 cbv 2 cbv

POLYMORPHIC TYPE ASSIGNMENT AND CPS CONVERSION 7

The typ e transform is extended to contexts by de ning jjjj (x)= jj(x)jj

cbv cbv

for each x 2 dom().

Theorem 2 (Meyer-Wand)

! !

1. If  ` .v :  , then  `jjjj . jjv jj : jj jj .

cbv cbv cbv

! !

2. If  ` .e :  ,then `jjjj . jej : j j .

cbv cbv cbv

!

The call-by-value typ e transform is extended to  + cont by de ning

jj cont jj = jj jj ! . It is straightforwardtoverify that Theorem 2

cbv cbv

!

extends to  + cont in this way[4].

! 3

De nition 6 (Call-by-Name Typ e Transform for  )

j j = (jj jj ! ) !

cbn cbn

jjbjj = b

cbn

jj ! jj = j j !j j

1 2 cbn 1 cbn 2 cbn

The typ e transform is extended to contexts by de ning jj (x)= j(x)j

cbn cbn

for each x 2 dom().

Theorem 3

! !

1. If  ` .n :  ,then `jj . jjnjj : jj jj .

cbn cbn cbn

! !

2. If  ` .e :  ,then `jj . jej : j j .

cbn cbn cbn

!

The call-by-name CPS transform is extended to  + cont by de ning

jj cont jj = jj jj ! , just as for call-by-value. It is straightforward

cbn cbn

!

to verify that Theorem 3 extends to  + cont in this way.

4. Polymorphic Typ e Assignment

In this section we study the extension of the Meyer-Wand typing prop erty

to Damas and Milner's p olymorphic typ e assignment system (DM).

The syntax of typ es and contextsin(DM) is de ned by the following

grammar:

3

The term \call-by-name typ e transform" is something of a misnomer since there

exists a by-value CPS transform that validates the by-name typing prop erty [20, 7 ].

Nevertheless we stick with the suggestive, if somewhat misleading, terminology.

8 HARPER AND LILLIBRIDGE

De nition 7 (DM Typ es and Contexts)

monoty pes  ::= t j b j  ! 

1 2

pol y ty pes  ::=  j8t:

contexts ::= j;x:

Here t ranges over a countably in nite set of typ e variables. The typing

!

rules of the Damas-Milner system extend those of  as follows:

De nition 8 (Additional DM Typing Rules)

.e: 

(t 62 FTV ()) (gen)

.e: 8t:

.e : 8t:

(inst)

.e:[=t]

.e :  ;x: .e : 

1 1 1 2 2

(x 62 dom()) (poly-let)

. let x be e in e : 

1 2 2

The system DM + cont is de ned by adding the typ e expression  cont,

as b efore, and adding the following typing rules:

. callcc :  (callcc')

callcc

. throw :  (throw')

throw

where  = 8t:(t cont ! t) ! t and  = 8s:8t:s cont ! s ! t.

callcc throw

4.1. Restricted Call-by-Value

Let DM denote the sub-system of DM obtained by restricting let ex-

pressions so that the b ound expression is a call-by-value value. The Meyer-

Wand typing theorem may b e extended to terms of DM , provided that we

0

use the variant call-by-value CPS transform (cbv ) given in Section 2.

De nition 9 (Call-by-Value Typ e Transform for DM )

j j = (jj jj ! ) !

cbv cbv

j8t: j = 8t:j j

cbv cbv

jjtjj = t

cbv

jjbjj = b

cbv

jj !  jj = jj jj !j j

1 2 cbv 1 cbv 2 cbv

jj8t: jj = 8t:jj jj

cbv cbv

POLYMORPHIC TYPE ASSIGNMENT AND CPS CONVERSION 9

This de nition extends the Meyer-Wand typ e transform to p olymorphic

typ es. In the terminology of Reynolds [19], p olymorphic instantiation is

given a \trivial" interpretation in that no interesting computation can o ccur

as a result of p olymorphic instantiation. The de nition of j8t: j re ects

cbv

the fact that in DM there is no need of continuations whose domain is a

p olymorphic typ e.

Lemma 4

1. jj[=t] jj =[jj jj =t] jj jj .

cbv cbv cbv

2. j[=t] j =[jj jj =t] j j .

cbv cbv cbv

Pro of: By induction on the structure of  .

With this in mind wemay establish typ e preservation for the variant

call-by-value CPS transform on the sublanguage DM .

Theorem 4

0

1. If DM ` .v :  ,thenDM `jjjj . jjv jj : jj jj .

cbv cbv cbv

0

2. If DM ` .e:  , then DM `jjjj . jej : j j .

cbv cbv

cbv

Pro of: Both parts are proved simultaneously by induction on the

structure of typing derivations. Wegive a few illustrative cases; the

rest follow a similar pattern.

var Supp ose that DM ` .x :  by an application of rule var.

0

: jj jj But then (x)=  ,andthus DM `jjjj . jjxjj

cbv cbv cbv

0

since jjxjj = x.

cbv

abs Supp ose that DM ` . x:e :  ! by an application of

1 2

rule abs. Then DM ` ;x: .e :  , where x=2 dom(),

1 2

by a smaller typing derivation. Therefore wehave, byinduc-

0

: j j , and consequently that tion, DM `jjjj . jej

2 cbv cbv cbv

0

jjjj .x:jej : jj jj !j j . The result follows from

cbv cbv 1 cbv 2 cbv

the de nitions of the term and typ e transforms.

let Supp ose that DM ` . let x be v in e :  byanapplica-

1 2 2

tion of rule poly-let.ThenDM ` .v :  and DM `

1 1

;x: .e :  , where x=2 dom(), by smaller typing deriva-

1 2 2

tions. (Recall that the restriction on DM ensures that the

let-b ound expression must b e a value.) By induction it follows

0

that DM `jjjj . jjv jj : jj jj and that DM `

cbv 1 cbv 1 cbv

0

jjjj ;x:jj jj . je j : j j . It is easy to check that

cbv 1 cbv 2 cbv 2 cbv

0

jjv jj is always a value, and hence it follows that DM `

1 cbv

0 0

k :let x be jjv jj in je j k : j j , from which the result

1 cbv 1 cbv 2 cbv

follows by the de nition of the alternate term transform.

10 HARPER AND LILLIBRIDGE

gen Supp ose that DM ` .e: 8t: by an application of rule gen.

Then DM ` .e :  by a smaller derivation, where t=2

0

: j j ,from FTV (). By induction DM `jjjj . jej

cbv cbv cbv

0

: 8t:j j by whichitfollows that DM `jjjj . jej

cbv cbv cbv

an application of rule gen, from which the result follows by

observing that j8t: j = 8t:j j .

cbv cbv

inst Supp ose that DM ` .e:[=t] by an application of rule inst.

Then DM ` .e: 8t: by a smaller derivation, and thus, by

0

: j8t: j . Consequently, induction, DM `jjjj . jej

cbv cbv cbv

0

:[jj jj=t]jj, which is sucient for the DM `jjjj . jej

cbv cbv

result, by an application of Lemma 4.

Theorem 4 extends to DM + cont by de ning jj contjj = jj jj ! .

cbv cbv

0 0

We need only verify that jjcallccjj and jjthrowjj given in Section 2,

cbv cbv

have the typ es jj jj and jj jj , resp ectively. The soundness

cbv cbv

callcc throw

of DM + cont under call-by-value follows by the same reasoning as for

!

 + cont .

4.2. Call-by-Name

Theorem 3 (the Meyer-Wand-liketyping theorem for call-by-name) can

b e extended to the unrestricted DM language.

De nition 10 (Call-by-Name Typ e Transform for DM)

j j = (jj jj ! ) !

cbn cbn

j8t: j = 8t:j j

cbn cbn

jjtjj = t

cbn

jjbjj = b

cbn

jj ! jj = j j !j j

1 2 cbn 1 cbn 2 cbn

jj8t: jj = 8t:jj jj

cbn cbn

Lemma 5

1. jj [=t] jj =[jj jj =t] jj jj .

cbn cbn cbn

2. j [=t] j =[jj jj =t] j j .

cbn cbn cbn

Pro of: By induction on the structure of  . Theorem 5

POLYMORPHIC TYPE ASSIGNMENT AND CPS CONVERSION 11

1. If DM ` .n :  , then DM `jj . jjnjj : jj jj .

cbn cbn cbn

2. If DM ` .e :  , then DM `jj . jej : j j .

cbn cbn cbn

Pro of: Similar to that of Theorem 4.

Theorem 5 extends to DM + cont by de ning jj cont jj = jj jj ! .

cbn cbn

We need only verify that jjcallccjj and jjthrowjj ,given in Section 2,

cbn cbn

havetyp es jj jj and jj jj , resp ectively. The soundness of

cbn cbn

callcc throw

DM + cont under call-by-name op erational semantics follows from the ex-

tended theorem in a manner similar to that of the call-by-value case for

DM + cont .

4.3. Unrestricted Call-by-Value

Having established suitable typing prop erties for the variant call-by-value

transform for DM and the call-by-name transform for full DM, it is natural

to consider whether there is a call-by-value CPS transform for full DM that

satis es a Meyer-Wand-liketyping prop erty. The problem is to extend

the CPS transform to general let expressions, i.e. those not necessarily

satisfying the restriction that let-b ound expressions b e values.

Let us consider a nave attempt to extend Theorem 4 to full DM by

0

considering the cbv term transform instead of the sp ecialized cbv trans-

form. Consider the p olymorphic let case. By induction wehave DM `

jjjj . je j : j j and DM `jjjj ;x:jj jj . je j : j j ; and

cbv 1 cbv 1 cbv cbv 1 cbv 2 cbv 2 cbv

we are to show that DM `jjjj .k:je j (x:je j k ):j j : Since

cbv 1 cbv 2 cbv 2 cbv

e is a general computation, a continuation to receive its value (should

1

it havea value) is required. But since e may b e p olymorphic, the def-

1

inition of j j given earlier is no longer appropriate | we require that

cbv

j j =(jj jj ! ) ! , irresp ective of whether or not  is a quanti-

cbv cbv

ed typ e. The de nition of jj jj remains the same, in keeping with the

cbv

intuition that a value of p olymorphic typ e is a value of all instances of that

typ e.

These mo di cations to the typ e transform takeusbeyond the Damas-

Milner typ e system since now quanti ed typ es can o ccur as the domains of

functions. Accordingly we generalize the target language of the CPS trans-

+

form to the language DM which is de ned by eliminating the distinction

between monotyp es and p olytyp es in DM. The resulting system is equiva-

lent to full p olymorphic typ e assignment[16]. Although the decidabilityof

+

typ e inference in DM remains an op en problem, this is not imp ortantfor

+

our purp oses. The main prop ertyofDM that we require is closure under

-reduction [16].

12 HARPER AND LILLIBRIDGE

With these changes to the typ e transformation and the asso ciated en-

richment of the target typ e system, the putative pro of of typ e preservation

go es through in the case of let expressions, but now p olymorphic general-

ization is problematic. Sp eci cally,ifDM ` .e:  with t 62 FTV (),

+

then by induction DM `jjjj . jej : j j , and t 62 FTV (jjjj ). It

cbv cbv cbv cbv

+

follows that DM `jjjj . jej : 8t:j j , but this is not enough | we

cbv cbv cbv

+

must show that DM `jjjj . jej : j8t: j , and there is no evidentway

cbv cbv

to pro ceed.

In fact typing is not preserved. The argument pro ceeds byway of the

extension of DM with continuation passing primitives. Under the call-by-

value evaluation strategy, DM + cont is unsound in that there is a term e

such that e has a typ e  , but whose value when evaluated under call-by-

value fails to havetyp e  . Assuming that wehavebasetyp es int and bool,

and constants 0 : int and true : bool, the following term is well-typ ed with

typ e bool in DM + cont but evaluates under call-by-value to 0, a constant

4

of typ e int:

e = let f b e callcc (k :x:throw ky:x)

0

in (x:y :y )(f 0) (f true )

Using the typing rules of DM + cont ,thelet-b ound identi er f is assigned

the typ e 8t:t! t, and hence maybeusedattyp es int !int and bool! bool

in the b o dy. But the binding for f grabs the continuation asso ciated with

the b o dy of the let expression and saves it. Up on evaluation of f 0, the

continuation is invoked and f is e ectively re-b ound to a constant function

returning 0. The b o dy is re-entered, f 0isevaluated once again (without

diculty), but then f true is evaluated, resulting in 0.

It follows that the call-by-value CPS transform for DM + cont do es not

preservetypability in the sense of Meyer & Wand, for by the correctness of

the call-by-value CPS transform for DM + cont , the expression je j x:x also

0

evaluates to 0 (under call-by-name or call-by-value), yet has typ e bool,a

+

violation of the sub ject reduction prop ertyof DM [16]. Consequently the

call-by-value CPS transform do es not verify the Meyer-Wand typing prop-

ertyeven in the absence of continuation-passing primitives. For otherwise

we could extend such a transform for the pure DM language to DM + cont

by rst regarding callcc and throw as variables, and then replacing them

by the expressions jjcallccjj and jjthrowjj . The substitution preserves

cbv cbv

typing, and results in a correct call-by-value CPS transform for DM + cont ,

which is imp ossible.

4

This argument can b e made without constants but at the cost of increased complexity.

Constants of base typ e can easily b e added to any of the transforms presented in this

pap er by de ning jjcjj = c, where c is a constant. Constants of non-base typ e must b e

handled on a case-by-case basis.

POLYMORPHIC TYPE ASSIGNMENT AND CPS CONVERSION 13

It is natural to inquire whether there is some other CPS transform for

the pure DM language (without continuation-passing primitives) that is

correct for the call-by-value interpretation and satis es the Meyer-Wand

typing prop erty. Although we are unaware of such a transform, it is dicult

to prove that none exists, in part b ecause it is unclear what, in general,

constitutes a correct CPS transform of an expression. We are assured,

however, that anysuch transform must b e substantially di erent from the

standard call-by-value transform and its close relatives.

Theorem 6 There isnocal l-by-value CPS transform jej for DM satisfying

the fol lowing two conditions:

1. Equivalence: jej is operational ly equivalent to jej under either the

cbv

5

cal l-by-value or cal l-by-name operational semantics.

+

2. Typing: If DM ` .e:  , then DM `jjjj . jej : j j .

cbv cbv

0

Pro of: Let e =[c=callcc ;t=throw]e ,where e is as in the coun-

0 0

0

0

terexample ab ove, so that e =[callcc=c; throw =t]e . Under the as-

0

0

0 0

sumptions of the theorem, je j is op erationally equivalenttoje j ,

cbv

0 0

and

+

0

j : jboolj : DM ` c:jj jj ;t:jj jj . je

cbv cbv cbv

0

callcc throw

Now since

+

DM `. jjcallccjj : jj jj

cbv cbv

callcc

and

+

DM `. jjthrow jj : jj jj ;

cbv cbv

throw

it follows that

+

0

DM ` [jjcallccjj =c; jjthrowjj =tjj] je j : jboolj :

cbv cbv cbv

0

Now

0

j [jjcallccjj =c; jjthrowjj =tjj] je

cbv cbv cbv

0

is op erationally equivalentto

0

[jjcallccjj =c; jjthrowjj =tjj] je j:

cbv cbv

0

But the former term is just je j as de ned in Section 2. By the cor-

0 cbv

rectness of this transform with resp ect to the call-by-value op erational

semantics, wehave that je j (x:x)evaluates to 0, and hence

0 cbv

0

[jjcallccjj =c; jjthrowjj =tjj]je j (x:x)

cbv cbv

0

must also evaluate to 0. But the latter term has typ e bool,incontra-

+

diction to the sub ject reduction prop ertyofDM .

5

Twoterms e and e are operationally equivalent i they are indisting ui shab le in all

1 2

program contexts [18].

14 HARPER AND LILLIBRIDGE

The conditions of the theorem leave op en the p ossibility of there b e-

ing a call-by-value transform for DM that is op erationally inequivalentto

the standard one, or of there b eing a typ e transform for whichaMeyer-

Wand-liketyp e preservation theorem can b e proved but for which either

jjcallccjj or jjthrowjj fails to have the required typ e. The typing con-

cbv cbv

dition seems natural and well-motivated; weknow of no other uniform

assignmentoftyp es for which the call-by-value CPS transform validates

a Meyer-Wand-liketyp e preservation theorem. On the other hand, the

requirement of op erational equivalence is rather strong. In particular, it

imp oses stringent conditions on the transformation of terms with free vari-

ables whichwe exploited in the pro of of the theorem. It seems plausible

that a variant transform that fails to satisfy the op erational equivalence

criterion may exist, but wehave no evidence to supp ort this conjecture.

4.4. Related Transforms

It seems worthwhile, however, to p oint out that there is a varianttyp e

transform that \almost" works. This transform is de ned by taking jj8t: jj =

8t:j j, and j j =(jj jj ! ) ! .Theintuition b ehind this choice is to

regard p olymorphic instantiation as a \serious" computation (in roughly

the sense of Reynolds [19]). This interpretation is at variance with the

usual semantics of ML p olymorphism since it admits primitives that have

non-trivial computational e ects when p olymorphically instantiated. Nev-

ertheless, we can use this typ e transform to extend the Meyer-Wand the-

orem to a variant call-by-value CPS transform for DM and to a variant

call-by-name CPS transform for DM, provided that we restrict attention

to programs of monomorphic typ e. It do es not provide a variant call-by-

value CPS transform for full DM b ecause of the way in which p olymorphic

generalization is handled.

Tomake these observations precise, wesketch the de nitions of variant

CPS transforms based on this typ e interpretation. The main idea is to

de ne the CPS transform by induction on typing derivations so that the

e ect of p olymorphic generalization and instantiation can b e prop erly han-

dled. Wegive here only the two most imp ortant clauses, those governing

the rules gen and inst:

j .e : 8t: j = k :k jej; where

j .e:  j = jej

j .e :[=t] j = k :jej (x:x k); where

j .e : 8t: j = jej

This de nition may b e extended to the other inference rules in suchaway

as to implement either a call-by-name or call-by-value interpretation of

POLYMORPHIC TYPE ASSIGNMENT AND CPS CONVERSION 15

application. However, the transform fails (in general) to agree with the

usual (call-by-value or call-by-name) ML semantics on terms of p olymor-

phic typ e. Sp eci call y, the transformation of a gen rule applies the current

continuation to the susp ended computation of e. If this continuation is not

strict, then an expression that would ab ort in ML terminates normally

after transformation into CPS. For example, supp ose that hd and nil are

constants with p olymorphic typ es 8t:t list ! t and 8t:t list , resp ectively.We

may derive the judgement  . hd nil : 8t:t list using the given typings for

hd and nil, and the rules for application and p olymorphic generalization.

Using the variant rules given ab ove, the transform of hd nil determined by

this typing derivation has the form k :k jhd nilj. But this expression eval-

uates normally on a non-strict continuation k , whereas under the usal ML

semantics the expression should ab ort regardless of whether it is used in a

subsequent computation.

By restricting attention to programs of monomorphic typ e, wemayob-

tain correct variant CPS transforms for DM (under call-by-value) and

DM (under call-by-name). This is essentially b ecause in DM under call-

by-value there are no non-trivial p olymorphic computations, and b ecause

in DM under call-by-name the semantics is de ned by substitution. But the

ab ove argument shows that the variant call-by-value transform is incorrect

for DM under call-by-value. Sp eci cally, it fails to correctly implement the

usual ML semantics for expressions suchaslet x b e hd nil in 0 (which, under

the ab ove transformation, yields result 0). This suggests that it would b e

advantageous to separate the two roles of let in ML. Sp eci cally, our re-

sults imply the soundness of the variant of ML obtained byevaluating let's

sequentially but admitting no p olymorphism, and by adding a new de ni-

tion form, def x is e in e , which admits p olymorphism but is evaluated \by

1 2

name".

5. Conclusion

The Meyer-Wand typing theorem for the call-by-value CPS transform for

the simply-typ ed -calculus establishes a simple and natural relationship

between the typ e of a term and the typ e of its call-by-value CPS transform.

Meyer and Wand exploited this relationship in their pro of of the equivalence

!

of the direct and continuation semantics of  [13]. A minor extension of

!

this result may b e used to establish the soundness of typing for  + cont,

!

the extension of  with continuation-passing primitives [4], under call-by-

value.

In this pap er wehave presented a systematic study of the extension of

the Meyer-Wand theorem to the Damas-Milner system of p olymorphic typ e

assignment. Our main p ositive results are the extension of the Meyer-Wand

16 HARPER AND LILLIBRIDGE

theorem to the call-by-value interpretation of a restricted form of p olymor-

phism, and to the call-by-name interpretation of the unrestricted language.

These results have as a consequence the soundness (in the sense of Damas

and Milner [2]) of these programming languages. Wehave also argued that

there is no \natural" call-by-value CPS transform for the unrestricted lan-

guage, but this leaves op en the p ossibility of nding a transformation that

is radically di erentincharacter from the usual one.

Our investigation makes clear that there is a fundamental tension b e-

tween implicit p olymorphism and the call-by-value interpretation of let.

This is consistent with earlier results establishing the inconsistency b e-

tween unrestricted p olymorphism and p olymorphic reference typ es [22,12]

and rst-class continuations [9, 10]. The source of the inconsistency may

b e traced to con icting motivations for the static and dynamic semantics

of the language. The p olymorphic typing rule for let is motivated bya

substitution principle | a let-b ound variable is deemed to have exactly

those typ es that may b e ascrib ed to the expression to which it is b ound. In

particular, if a let-b ound variable maybeusedatmultiple typ es, provided

that each of these may b e ascrib ed to the b ound expression separately.

But this principle, app ealing though it may b e in isolation, is inconsistent

with the \evaluate-once" semantics of let expressions in the call-by-value

dynamic semantics. In a pure functional language it is p ossible to combine

b oth principles without complication, achieving p olymorphism without sac-

ri cing co de re-use. Once control or store e ects are admitted, however,

it b ecomes untenable to maintain b oth principles without restriction. We

have outlined a CPS transform for the full Damas-Milner language that ex-

tends to continuation-passing primitives, but which is not quite equivalent

to the usual call-by-value semantics. This suggests that a language in which

p olymorphic generalization and instantiation are semantically signi cant

would b e a well-b ehaved alternative to ML-style implicit p olymorphism.

This p ersp ective has b een explored in some detail in subsequentwork of

the authors [11].

6. Acknowledgments

We are grateful to Olivier Danvy, Bruce Duba, Tim Grin, Mark Leone,

and the anonymous referees for their helpful comments and suggestions.

References

1. William Clinger, Daniel P.Friedman, and Mitchell Wand. A scheme for

higher-level semantic algebra. In Maurice Nivat and John C. Reynolds,

POLYMORPHIC TYPE ASSIGNMENT AND CPS CONVERSION 17

editors, Algebraic Methods in Semantics, pages 237{250. Cambridge

University Press, Cambridge, 1985.

2. Luis Damas and . Principal typ e schemes for functional

programs. In Ninth ACM Symposium on Principles of Programming

Languages, pages 207{212, 1982.

3. Olivier Danvy and John Hatcli . Thunks (continued). In Proceed-

ings of the Workshop on Static Analysis WSA'92,volume 81-82 of Bi-

gre Journal, pages 3{11, Bordeaux, , Septemb er 1992. IRISA,

Rennes, France. Extended version available as Technical Rep ort CIS-

92-28, Kansas State University.

4. Bruce Duba, Rob ert Harp er, and David MacQueen. Typing rst-class

continuations in ML. In Eighteenth ACM Symposium on Principles of

Programming Languages,January 1991.

5. Matthias Felleisen, Daniel Friedman, Eugene Kohlb ecker, and Bruce

Duba. A syntactic theory of sequential control. Theoretical Computer

Science, 52(3):205{237, 1987.

6. Timothy Grin. A formulae-as-typ es notion of control. In Seventeenth

ACM Symposium on Principles of Programming Languages,SanFran-

cisco, CA, January 1990. ACM, ACM.

7. Timothy Grin. Private communication, January 1992.

8. Rob ert Harp er, Bruce Duba, and David MacQueen. Typing rst-class

continuations in ML. Revised and expanded version of [4]. To app ear,

Journal of Functional Programming.

9. Rob ert Harp er and Mark Lillib ridge. Announcementonthetypes

electronic forum, July 1991.

10. Rob ert Harp er and Mark Lillibridge. Polymorphic typ e assignment

and CPS conversion. In Olivier Danvy and Carolyn Talcott, editors,

Proceedings of the ACM SIGPLAN Workshop on Continuations CW92,

pages 13{22, Stanford, CA 94305, June 1992. Department of Computer

Science, Stanford University. Published as technical rep ort STAN{CS{

92{1426.

11. Rob ert Harp er and Mark Lillibridge. Explicit p olymorphism and CPS

conversion. In Twentieth ACM Symposium on Principles of Program-

ming Languages, pages 206{219, Charleston, SC, January 1993. ACM,

ACM.

18 HARPER AND LILLIBRIDGE

12. Xavier Leroy and Pierre Weis. Polymorphic typ e inference and as-

signment. In Eighteenth ACM Symposium on Principles of Program-

ming Languages, pages 291{302, Orlando, FL, January 1991. ACM

SIGACT/SIGPLAN.

13. Alb ert R. Meyer and Mitchell Wand. Continuation semantics in typ ed

lamb da calculi (summary). In Rohit Parikh, editor, Logics of Pro-

grams,volume 193 of Lecture Notes in Computer Science, pages 219{

224. Springer-Verlag, 1985.

14. Robin Milner. A theory of typ e p olymorphism in programming lan-

guages. Journal of Computer and System Sciences, 17:348{375, 1978.

15. Robin Milner, Mads Tofte, and Rob ert Harp er. The De nition of Stan-

dardML. MIT Press, 1990.

16. John C. Mitchell. A typ e-inference approach to reduction prop erties

and semantics of p olymorphic expressions. In 1986 ACM Conference

on LISP and Functional Programming, pages 308{319, August 1986.

17. . Call-by-name, call-by-value, and the lamb da calculus.

Theoretical Computer Science, 1:125{159, 1975.

18. Gordon Plotkin. LCF considered as a . Theo-

retical Computer Science, 5:223{257, 1977.

19. John C. Reynolds. De nitional interpreters for higher-order program-

ming languages. In ConferenceRecord of the 25th National ACM Con-

ference, pages 717{740, Boston, August 1972. ACM.

20. John C. Reynolds. On the relation b etween direct and continuation

semantics. In Jacques Lo eckx, editor, Second Col loquium on Automata,

Languages, and Programming,volume 14 of Lecture Notes in Computer

Science, pages 141{156, Saarbruc  ken, West Germany, 1974. Springer-

Verlag.

21. Amr Sabry and Matthias Felleisen. Reasoning ab out programs in

continuation-passing style. In 1992 ACM Conference on LISP and

Functional Programming, pages 288{298, San Francisco, CA, June

1992. ACM.

22. Mads Tofte. Typ e inference for p olymorphic references. Information

and Computation, 89:1{34, November 1990.

POLYMORPHIC TYPE ASSIGNMENT AND CPS CONVERSION 19

23. Andrew K. Wright and Matthias Felleisen. Asyntactic approachto

typ e soundness. Technical Rep ort TR91{160, DepartmentofCom-

puter Science, Rice University, July 1991. To app ear, Information and

Computation.