Reasoning ab out Continuations
with Control E ects
1;2
Pierre Jouvelot
2
David K. Gi ord
Categories and Sub ject Descriptions: D.1.3 [Program- Abstract
ming Techniques] { Concurrent Programming: E ect
We present a new static analysis metho d for rst-class
systems; D.1.m [Programming Techniques] { Mis-
continuations that uses an e ect system to classify the
cellaneous: First-class continuations; D.3.1 [Program-
control domain b ehavior of expressions in a typ ed p oly-
ming Languages] { Formal De nitions and Theory;
morphic language. We intro duce two new control ef-
D.3.3 [Programming Languages] { Language Con-
fects, goto and comefrom, that describ e the control ow
structs: Control structures, e ect systems; D.3.4 [Pro-
prop erties of expressions. An expression that do es not
gramming Languages] { Pro cessors: Compilers, op-
havea goto e ect is said to be continuation fol lowing
timization.
b ecause it will always call its passed return continua-
tion. An expression that do es not havea comefrom ef-
General Terms: Languages, Theory,Veri cation.
fect is said to b e continuation discarding b ecause it will
never preserve its return continuation for later use. Un-
Additional Key Words and Phrases: e ect systems, typ e
observable control e ects can be masked by the e ect
systems, control e ects, e ect masking, control ow
system. Control e ect soundness theorems guarantee
analysis, FX-87.
that the e ects computed statically by the e ect sys-
tem are a conservative approximation of the dynamic
b ehavior of an expression.
1 Intro duction
The e ect system that we describ e p erforms certain
kinds of control ow analysis that were not previously
First-class continuations add a great deal of expressive
feasible. We discuss how this analysis can enable a va-
power to a programming language as they p ermit the
riety of compiler optimizations, including parallel ex-
implementation of a wide varietyofcontrol structures,
pression scheduling in the presence of complex control
including jumps, error-handlers, and coroutines [F87].
structures, and stack allo cation of continuations. The
With this p ower comes substantial semantic [MR88] and
e ect system we describ e has b een implemented as an
implementation [CHO88] complexities. Thus it would
extension to the FX-87 programming language.
be very useful to be able to precisely identify which
expressions in a program use rst-class continuations
and in what manner.
We present a new static metho d for control ow anal-
This researchwas supp orted by the Defense Advanced Research
ysis that p erforms certain kinds of analysis that were
Pro jects Agency of the Department of Defense and was monitored
by the Oce of Naval Research under contract numb er N00014-
not previously feasible. Sp eci cally,wehave develop ed
83-K-0125.
the rst static metho d of determining which expressions
may not exhibit sequential control ow in a program-
1
CAI, Ecole des Mines, 60 bvd Saint-Michel, 75272, PARIS,
ming language with rst-class continuations.
France E-mail: [email protected]
Our static analysis technique is based on the use of an
2
LCS, Massachusetts Institute of Technology , 545 Techno-
e ect system [LG88] to classify the p ossible control do-
logy Square, Cambridge, MA 02139, USA E-mail:
main b ehavior of expressions. An e ect system is based
[email protected]. EDU
on a kinded typ e system for the second-order lambda
calculus [M79]. Kinds are the \typ es" of descriptions
which include typ es and e ects. Our typ e and e ect
system has three base kinds: types, which describ e the
value that an expression may return; e ects, which de-
scrib e the side-e ects that an expression mayhave; and 1
regions, which are used to describ e where side-e ects or goto e ects, the problem of a variable \redef-
may o ccur. An expression that do es not have an ob- inition" by a \return" inside its define form is
servable e ect is said to b e pure. Expressions that are avoided. In the same manner, mutations that are
pure are referentially transparent. p erformed by taking advantage of the implementa-
Typ es, e ects and regions are closely interrelated; in tion of recursive de nitions by letrec [B89] can b e
prohibited.
particular, a function typ e incorp orates a latent e ect
that describ es the side-e ects that the function may
p erform when it is applied, and a reference typ e in-
Control e ects let the compiler writer p erform safe
corp orates a region that describ es where the reference
optimizations in the presence of rst-class continu-
is allo cated. The kind system is used to verify the well-
ations. For instance, if a given cwcc expression has
formedness of descriptions; the typ e and e ect system
a masked control e ect, then the internal contin-
is used to verify the well-formedness of expressions.
uation will be used only as a \downward funarg"
We can use an e ect system for control ow analysis
[S78] and thus the expression's continuation struc-
by intro ducing two typ es of control e ects, goto and
ture control frames can b e stack allo cated.
comefrom, that describ e the control ow prop erties of
expressions. An expression that do es not havea goto
e ect is said to b e continuation fol lowing b ecause it will
Control e ects also allow sequential semantics to b e pre-
always call its return continuation in the usual way. An
served in the presence of b oth rst-class continuations
expression that do es not havea comefrom e ect is said
and automatic compile-time detection of parallelism.
to b e continuation discarding b ecause it will never pre-
When compiling for a parallel target machine, the com-
serve its return continuation for later use. This \dou-
piler can guarantee sequential semantics which is in-
ble negation" style of de nitions is necessary when one
timately related to the notion of continuations, which
wants to express conservative approximations of run-
represent the state of a sequential evaluation by con-
time program b ehaviors.
sidering that control e ects interfere with all e ects.
Unobservable control e ects can b e masked by the ef-
In the remainder of this pap er we describ e the kernel
fect system. Our masking rule applies to expressions
language KFX of FX-87 Section 2, integrate control
that are externally well-b ehaved, even if they use con-
e ects into KFX Section 3, state two control e ect
tinuations internally. Control e ect soundness theorems
soundness theorems Section 4, give precise conditions
guarantee that the e ects computed statically by the
when it is p ossible to mask unobservable control e ects
e ect system are a conservative approximation of the
Section 5, survey related work Section 6, and sum-
dynamic b ehavior of an expression.
marize our results Section 7.
We showhow our e ect system can b e used with the
pro cedure call-with-current-continuation inspired
from [R86], hereafter noted cwcc. This pro cedure allows
rst-class access to the current continuation. Simpler
2 KFX - A Kernel Language for
control structures based on lab els and jumps can be
FX-87
treated in a similar way.
Control e ects are useful to the programmer, the lan-
guage designer and the compiler writer:
For p edagogical purp oses we will study control e ects
in the context of KFX, the kernel language of FX-87.
Control e ects let the programmer sp ecify, in
FX-87 [GJLS87][LG88] is a p olymorphic typ ed language
machine-veri able form, the exp ected run-time
that allows side-e ects and rst-class functions. Its syn-
control b ehavior of a given program, thus increas-
tax and most of its standard op erations are strongly in-
ing do cumentation, mo dularity and maintainabil-
spired byScheme [R86] which will b e used in most of our
ity of programs. Control e ects also provide a pro-
examples. The language KFX has the following Kind,
grammer with a new framework in which to rea-
Description Region, E ect and Typ e and Expression
son ab out languages with rst-class continuations.
domains where I is the domain of identi ers:
Moreover, when unobservable control e ects are
masked, a programmer knows that an expression
K ::= region
will b e well-b ehaved.
effect
Control e ects let the language designer limit the
type
use of continuations to simplify the semantics of
the language. For instance, by saying that top-
R ::= I
level de nitions are not allowed to have comefrom
@I region constant 2
F ::= I
Note that twice is abstracted over the typ e t of the
pure no e ect
argumentof f and its latent e ect e. The typ e of twice
write R write on R
is:
read R read on R
twice : poly t type
alloc R allo cation on R
poly e effect
maxeff F0 F1 combination
subr pure
subr e t t
T ::= I
subr e t t
subr F T T function
poly I K T p olymorphic typ e
The typ e and e ect rules for application, abstraction,
ref T R reference to T in
1
p olymorphic abstraction and pro jection follow. Just as
region R
\:" is used to denote the \typ e of " relation, \!" is used
D ::= R
to denote the \e ect of " relation. T is the typ e and
F
kind assignment function that maps variables to their
T
typ e or kind.
E ::= I
hE0;Ti : subr e t t ! e
1 2 0
lambda I T E lambda
hE1;Ti : t ! e
1 1
abstraction
hE0 E1;Ti : t ! maxeff e
2
E0 E1 application
maxeff e e
0 1
plambda I K E p olymorphic
abstraction
hE;T[t =I]i : t ! e
1 2
proj E D pro jection
hlambda I t E;Ti : subr e t t ! pure
1 1 2
new R T E allo cation
get E dereference
hE;T[k=I]i : t ! pure
set E0 E1 mutation
hplambda I k E;Ti : poly I k t ! pure
hE;Ti : poly I k t ! e
In the e ect domain, a pure expression is referentially
0 0
hproj E k ;Ti : t[k =I] ! e
transparent. Impure expressions can write, read or
allo cate alloc memory in regions which denote sets
where f [y=x]z is y if z is x and fz otherwise the same
of memory lo cations. Combined e ects are intro duced
notation on typ es is used to denote syntactic substitu-
by maxeff.
tion. The remaining e ect and typ e rules of KFX are
In the typ e domain, the subr typ e of a function en-
presented in [LG88].
co des its latent e ect F, the typ e of its argument and its
The standard semantics [S86] of KFX is de ned on
return typ e. Poly represents the typ e of p olymorphic
typ e-erased KFX expressions. A program is typ e-erased
values abstracted over kind K. Ref T R is the typ e of
by reducing plambda and proj expressions to their em-
a reference in the region R to a value of typ e T.
b edded expressions and eliminating all typ e information
In the expression domain, just as lambda abstracts
see [L87] for a formal de nition of typ e erasure.
E over the ordinary variable I of typ e T, plambda ab-
The domain equations for lo cations, basic values, clo-
stracts E over the description variable I of kind K to
sures, values, environment, stores, results and continu-
yield a p olymorphic value. A p olymorphic value is in-
ations in our standard semantics are as follows:
stantiated with the proj construct. New allo cates i.e.,
has an alloc latent e ect a reference in region R to
l : Loc
the value of E of typ e T, get reads and returns the value
b : B asic
stored into the reference E and set writes E1 in the ref-
p : Clo = Val ! C ont ! S tor e ! Answ er
erence E0. As an example, we give b elow the co de of the
v : Val = B asic + Clo + Loc
p olymorphic twice function that applies the function f
u : Env = I ! Val
twice on its argument x:
s : S tor e = Loc ! Val
a : Answ er = Val
plambda t type
k : C ont = Val ! S tor e ! Answ er
plambda e effect
lambda f subr e t t
where + means disjoint sum and ! is right-associative.
lambda x t
1
f f x We supp ose alpha-renaming to avoid name capture. 3
The de nition of Eval, of typ e E ! Env ! C ont ! The cwcc function cannot be treated as pure. The
S tor e ! Answ er , is fairly simple [S86]: comefrom e ect corresp onds to the capture of a con-
tinuation and it is intro duced by providing cwcc with
a comefrom r latent e ect, where r is a region.
E [[ I ]]uk = k u[[ I ]]
The region restricts the domain of in uence of a given
E [[ lambda I E ]]uk = k e:E [[ E ]]u[e=I]
continuation-capturing expression see b elow. The fol-
E [[ E0 E1 ]]uk = E [[ E0 ]]ue :
0
lowing Scheme program shows that a compiler cannot
E [[ E1 ]]ue :
1
common sub expression eliminate calls to cwcc since
e e k
0 1
calling f1 would p erform horrible-effects on the
E [[ new E ]]uk = E [[ E ]]ues:k l s[e=l ]
store; these e ects would not o ccur if f2 were called.
where l = new Loc
Thus the cwcc calls cannot b e pure.
E [[ get E ]]uk = E [[ E ]]ues:k ses
let* f1 cwcc lambda x x
E [[ set E0 E1 ]]uk = E [[ E0 ]]ue :
x horrible-effects
0
E [[ E1 ]]ue s :
f2 cwcc lambda x x
1 1
ke s [e =e ]
...
1 1 1 0
The goto e ect corresp onds to the \escaping" call of a
where new Loc is a function that allo cates fresh mem-
continuation and is intro duced by providing a continua-
ory lo cations. Note that, contrarily to the Scheme set!
tion with a goto r latent e ect, where r is the region
sp ecial form, the function set evaluates its rst argu-
used in the cwcc expression that created the continua-
ment, which is a reference. For clarity,we omitted in-
tion. The following Scheme program shows that any
jection and pro jection op erations in sum domains.
e ect can b e exercised by the evaluation of a continua-
tion:
let x cwcc lambda f
3 Control E ects Describ e Po-
cwcc lambda g f g
tential Behavior
h
f
The material of the previous section is a simpli ed pre-
horrible-effects
sentation of the e ect system that is the basis of the
...
FX-87 programming language [GJLS87]. This static
x 0
system was mainly designed to cleanly deal with mem-
...
ory side-e ects within a typ ed p olymorphic language
In this example, between the evaluations of f g
which allows rst-class functions. We show b elowhow
and h, horrible-effects will be executed; thus
this framework can b e easily extended to deal with the
these expressions cannot be reordered. The e ects of
a-priori unrelated control side-e ects that are caused by
horrible-effects will also b e p erformed after any
rst-class continuations. This shows the p ower and ex-
call to x, since it is b ound to the continuation that binds
ibility of an e ect system.
the result of the call of cwcc to x.
Weintro duce rst-class continuations into KFX with
the Scheme function cwcc. The function cwcc passes a
In summary, the KFX typ e and e ect rules can be
pro cedural representation of its return continuation to
used to compute control e ects by assigning cwcc the
its argument, whichmust b e a one-argument function.
2
following typ e :
In order to add cwcc to KFX we need merely to bind
cwcc to its value in the initial environment u. So, we
cwcc: poly r region
have:
poly t type
poly e effect
0 0 0
u[[ cwcc ]]=ek :ee k :k e k
subr maxeff comefrom r e
subr e
subr goto r
The cwcc function gives the programmer access to
t
continuations. As rst-class values, these continuations
void
can subsequently b e stored into global variables or re-
t
turned, allowing a given expression to b e evaluated more
t
than once to return a result more than once or to es-
2
cap e from its current continuation to discard its current
Without loss of generality,we assume that the anti-aliasing
continuation by applying another one. rule of FX-87 [GJLS87] is eliminated. 4
The function cwcc is abstracted over the region r in lowing a lo ok at the semantic de nition shows that
which the continuation is lo cated, the typ e t of the every equation of E ends by calling the continuation it
eventual result and the latent e ect e of the function is passed, except the application E0 E1. There are
passed as argument. The latent e ect of cwcc denotes twoways we can generate a function in KFX:
the combined e ects of calling its argument and cap-
if e is obtained byevaluating a lamb da expression,
0
turing a continuation in r. The argumentto cwcc is a
then it is continuation following if its b o dy is,
function that exp ects as argument the captured contin-
uation seen as a function with a goto r latent e ect.
otherwise e comes from a continuation created by
0
The typ e void indicates that a call to the continuation
a cwcc expression, and then the expression isn't
will never return to its caller.
continuation following. But, this continuation has
latent e ect goto r where r is the region that
The control e ects of an expression can b e masked if
cwcc has b een pro jected onto. Therefore, the ap-
the e ects cannot b e observed outside of the expression.
plication will have e ect goto r. 2
For instance, if we follow the typing rules of KFX, then
With this theorem, we know that the goto e ect in-
we deduce that the following expression
formation collected by the KFX typ e system is a con-
+ cwcc lambda f f 0 1
servative i.e. safe approximation of the run-time b e-
havior of a program.
has b oth goto r and comefrom r e ects for some
region r. However, it is easy to prove that this expres-
We now prove the soundness of comefrom e ects.
sion is well-b ehaved; it will call its return continuation
This asp ect is a bit more complex, so we need some
it is continuation fol lowing and will not preserve its
preliminary de nitions.
continuation it is continuation discarding. Its control
e ects can thus b e masked as we discuss further in sec-
De nition. A continuation col lecting semantics of a
tion 5; in consequence, the continuation f will not need
programming language L is a non-standard semantics of
to be heap allo cated, thus improving run-time p erfor-
L in whichevery evaluation of an expression E returns an
mance and saving on memory consumption.
ordered pair made of the standard value of E and the set
of intermediate continuations captured while evaluating
E i.e. continuations made available by calls to cwcc.
4 Soundness of comefrom and
goto e ects
De nition. E is the continuation collecting seman-
c
tics of E .
Wenow need to show that the comefrom and goto ef-
fects we compute for an expression are a conservative
The precise de nition of E is an extension of E , where
c
estimate of the expression's dynamic b ehavior. With-
the set of continuations is passed along the computation
out loss of generality,we restrict our soundness pro ofs
and up dated by cwcc. For every function F used in the
to terminating expressions.
semantics of KFX, there is an equivalent one in the con-
tinuation collecting semantics of KFX; we will note it
De nition. An expression E is continuation fol low-
F . Note that the continuations k successively take
c c
ing i , for every environment u and store s, there exist
the value to b e passed, the set of p otentially available
two continuations k and k such that
1 2
continuations and the current store. For instance, here
are the equations de ning E for identi er, lamb da ex-
E [[ E ]]uk s 6= E [[ E ]]uk s
c
1 2
pression and application j denotes the current set of
The de nition of equalitywe use is extensional. The
intermediate continuations:
evaluation of a continuation-following expression ends
E [[ I ]]u jk = k u [[ I ]]j
by calling the continuation it is passed and never escap es
c c c c c
via some continuation available in the environment or
E [[ lambda I E ]]u jk = k ej:
c c c c
store.
E [[ E ]]u [e=I]j j
c c
E [[ E0 E1 ]]u jk = E [[ E0 ]]u j e j :
c c c c c 0 0
Theorem Goto Soundness. If there does not
E [[ E1 ]]u j e j :
c c 0 1 1
exist a region r such that E has e ect goto r, then E
e e j k
0 1 1 c
is continuation fol lowing.
and the de nition for cwcc:
Pro of sketch. By induction on the domain of ex-
0 0 0 0 0
pressions. Every construct of KFX is continuation fol- :k e j fk g[ j k u cwcc = ej k :ee j k
c c c c c c 5
if no free variable uses the region r in its typ e, then De nition. An expression context Ec is an expres-
read and write e ects on r can b e masked, sion with a \hole" in it, identi ed by []. It can b e lled
with E by writing Ec[E].
if r do es not app ear in the typ e of the result of the
De nition. An expression context Ec is wel l-behaved
expression, then allo c e ects on r can b e masked.
i for every environment u , store s and continuation
c c
k , there exist an expression E and a value v such that
c
E ect masking can b e easily extended to deal with con-
trol e ects. Control e ect masking soundness a direct
E [[ Ec[E]]]u fgk s = v; fg
c c c c
extension of FX-87 typ e soundness and e ect masking
where x; y is the ordered pair made of x and y and fg the
soundness [GJLS87]. We use the following lemma:
empty set. A well-b ehaved expression context do esn't
capture any continuation on its own.
Lemma. When evaluating E in the environment u,
continuation k and store s, the value passedtok is de-
De nition. An expression E is continuation discard-
termined by the values of the free variables and the stor-
ing i for every well-b ehaved expression context Ec, en-
age locations accessible via these free variables.
vironment u , store s , continuation k ,value v and list
c c c
of continuations j :
Pro of sketch. By induction on E equations with the
use of the \lo cation invariance" prop erty of [L87] which
E [[ Ec[E]]]u fgk s = v; j = v 62 j
c c c c
expresses that the choice of b ound lo cations do es not
in uence the nal result of the evaluation. 2
The idea b ehind this de nition is that it isn't p ossible,
from the evaluation of E, to recover some continuation
caught in some global variable or returned by E. For
Theorem. A goto r e ect of an expression E can
instance, the following expression :
be maskedifE does not import variables or return values
in the type of which r appears.
begin cwcc lambda f
set x lambda f
Pro of sketch: Wehave to show that if the preceding
h
condition is veri ed, then E is continuation following, i.e.
isn't continuation discarding, since the well-b ehaved
that for every u and s there exist k and k such that
1 2
context begin [] get x returns a caught con-
E [[ E ]]uk s 6= E [[ E ]]uk s. By the previous lemma, the
1 2
tinuation if we replace the hole by the previous expres-
value of E passed to k and k is de ned only by the
1 2
sion.
values of the free variables of E, which app ear in u, and
accessible i.e. in the returned value lo cations, in s. If r
Theorem Comefrom Soundness. If there does
do es not app ear in the typ e of any of these values, then,
not exist a region r such that E has e ect comefrom r,
by application of FX-87 typ e soundness, no goto r
thenEiscontinuation discarding.
e ect can b e p erformed by an external i.e. not de ned
in E continuation. By goto e ect soundness, E is then
Pro of sketch. The pro of is by induction on the equa-
continuation-following, and cho osing k =es:0 and
1
tions of E . The only way the set of caught continuations
c
k =es:1 will suce. 2
2
can b e extended is by a call to cwcc, which implies, fol-
lowing KFX typing rules, that there is a region r such
Theorem. A comefrom r e ect of an expression
that this call has a comefrom r e ect. 2
E can be maskedifE does not import variables or return
values which have r in their type.
With this theorem, we know that the comefrom ef-
fect information collected by the KFX typ e system is a
Pro of sketch. In the same way, we have to show
conservative i.e. safe approximation of the run-time
that if the preceding condition is veri ed, then E is con-
b ehavior of a program.
tinuation discarding. We wanttoprove that for every
Ec, u , s and k , v is not a member of j where v and j
c c c
are as in the de nition of \continuation discarding". As
5 Control E ect Masking
b efore, Ec[E] can only refer to free variables of E or its
FX-87 e ect masking detects and eliminates e ects of returned value. If r do es not app ear in the typ e of any
an expression that are not observable e.g., mutation of of these, then, by application of FX-87 typ e soundness,
lo cally allo cated references under the following condi- no continuation e ectively caughtby cwcc forms inside
tions: of E pro jected on r can b e returned by Ec[E]. 2 6
Not surprisingly these control e ect masking rules are is captured are the ones that are agged with the same
very similar to the ones already in FX-87 for memory comefrom r e ect. These are precisely the control
e ects. One can view a call to cwcc as allo cating some frames that corresp ond to the dynamic extent of the
data in the heap region to store the current stack and a captured continuation up to the p oint where the control
call to a continuation as reading a stored stack and writ- e ect is masked. We know that the control frames that
ing to the current stack. This analogy is not quite right app ear after this p ointwon't b e needed. Note that this
since the goto e ect requires a more stringent rule as optimization is not of utmost imp ortance in a system
seen ab ove. This can b e demonstrated by the following that uses a stack/heap strategy for the implementation
admittedly contrived Scheme program which shows an of continuations [CHO88]. It can however b e useful in
expression with a goto e ect that cannot be masked, an implementation that supp orts parallel evaluation of
even though this expression has no free variables: expressions since it avoids stack sharing b etween di er-
ent pro cesses.
let x cwcc lambda f
let y cons f f
cwcc lambda g
6 Related Work
set-cdr! y g
f y
A related approach to our compile-time analysis of
car y y
programs uses the notion of \abstract interpretation"
cwcc lambda h
[CC77]. Results in the area of functional languages
set-car! x h
[AH87] with higher-order functions and p olymorphism
cdr x x
cannot yet equal the outcome of our typ e and e ect sys-
tem. For instance, the only analysis of continuations we
In this example, by calling f,we rst bind x to the pair
found [H87] do es not address rst-class continuations.
y that contains f and the continuation g that precedes
[F88a] prop osed the -v-CS formal system to deal
the evaluation of car y y. The rst elementofx
with higher-order functions in the presence of imp era-
is mutated with h that corresp onds to the rest of the
tive constructs. Continuations can b e describ ed in such
program and x's binding expression is reentered at the
a framework and prop erties ab out them can b e proved.
p oint where car y y is evaluated; this evaluation
However, these pro ofs are not currently automated and
is not continuation-following. Although there are no
therefore cannot b e used in compiling systems.
free variables in the expression x is b ound to, the goto
Thus, the compile time analysis of programming lan-
e ect of the call car y y cannot b e masked; rst-
guages with rst-class continuations app ears to b e new.
class continuations allow the programmer to temp orar-
Most of the literature dealing with continuations gen-
ily exp ort and mutate lo cal lo cations, thus requiring the
erally fo cuses on their run-time characteristics and ap-
supplementary check on the regions app earing in return
plications to various problems like coroutines [HFW86],
typ e which is also, by construction, the typ e of values
multipro cessing [W80] or backtracking [SM72].
passed to the captured continuation.
The p ossibility of stack-allo cation of continuation
structure, which is a ma jor asset of the control e ect
The ma jor practical b ene t of control e ect mask-
masking we presented ab ove, is often presented, e.g. in
ing is the p ossibility of compile-time detection of exter-
[HF87], but with an \interpreter view" of the problem.
nally well-b ehaved expressions that internally use com-
The detection is done at run time e.g., by checking
plex control structures. This allows a programmer to
that a continuation is not used in away incompatible
formally reason ab out his program as if he were not
with its implementation. Our system p erforms a safe
using rst-class continuations, thus easing program cor-
approximation of this optimization at compile time.
rectness pro ofs. In addition, this allows compile-time
Control e ect masking allows the run-time system to
optimizations such as parallel evaluation of expressions
limit the amountofcontrol information that has to b e
that haveinternal control e ects or recognition of stack-
dump ed in the heap when a continuation is captured;
allo catable continuations, in which case cwcc can b e im-
the \prompts" prop osed in [F88b] or the \shift/reset"
plemented with a more ecient catch-throw mechanism
of [DF89] o er the same kind of facility, but they have
[M74].
to b e intro duced by the programmer.
When an expression has a masked control e ect, it
may use full- edged continuations internally. If con- We showed elsewhere [JG89a, JG89b] how e ect sys-
trol e ects are kept at run time, control e ects can b e tems can b e also used to describ e communication side-
used to dynamically optimize these internal continua- e ects arising when communicating parallel pro cesses
tions as well. The only control frames that havetobe are added to a language that uses and e ect systems.
dump ed into the heap when a continuation in region r Communication e ects describ e transmission op erations 7
References on shared channels between pro cesses. Control and
communication e ects show the p ower of an e ect sys-
[AH87] Abramsky, C., and Hankin, C. T. Eds. Ab-
tem to statically describ e run-time b ehavior of pro-
stract Interpretation for Declarative Languages. J.
grams.
Wiley and Sons, 1987.
[B89] Bawden, A. Submission to the Scheme electronic
7 Conclusion
mailing list [email protected]. March 2,
1989.
We presented a new static system that enables the use
of rst-class continuations in a typ ed p olymorphic lan-
[C73] Clark, R. L. A Linguistic Contribution to
guage. Simpler control structures, for example those
GOTO-Less Programming. Datamation, Decem-
based on gotos and lab els, can be straightforwardly
b er 1973, 62-63. Reprinted in Communications of
treated by the same framework.
the ACM 4, 27 1984, 349-350.
Our new e ect system intro duces two new e ect con-
structors, called goto and comefrom. Every expression
[CC77] Cousot, P., and Cousot, R. Abstract Interpre-
that do es not have a goto comefrom e ect is con-
tation: A Uni ed Lattice Mo del for Static Analy-
tinuation following discarding. Expressions that do
sis of Programs by Construction and Approxima-
not have control e ects are well-b ehaved in the sense
tion of Fixed Points. In Proceedings of the 4th An-
that they observe normal sequential execution seman-
nual ACM Conference on Principles of Program-
tics. We showed how a precise de nition of these no-
ming Languages. ACM, New York, 1977, pp. 238-
tions can b e given with resp ect to the FX-87 standard
252.
and continuation collecting semantics. We also proved
[CHO88] Clinger, W. D., Hartheimer, A. H., and Ost, two e ect soundness theorems.
E. O. Implementation Strategies for Continuations.
The control e ect masking rules enable us to de-
in Proceedings of the 1988 ACM Conference on tect expressions that are externally well-b ehaved even
Lisp and Functional Programming. ACM, New
though they use continuations internally. Such expres-
York, 1988, pp. 124-131.
sions can b e optimized, e.g. continuations can b e stack
allo cated at compile time. Well-b ehaved expressions
[DF89] Danvy, O., and Filinski, A. A Functional Ab-
can b e scheduled to execute in parallel sub ject to their
straction of Typed Contexts. DIKU Rep ort 89/5,
other e ects.
University of Cop enhagen, 1989.
We have also found it relatively easy to implement
our e ect system as an extension to FX-87. As dis-
[F87] Friedman, D. P., and Haynes, C. T. Constrain-
cussed ab ove, we only needed to add three constants to
ing Control. In Proceedings of the 12th Annual
our existing store e ect system in order to handle the
ACM Conference on Principles of Programming
complications intro duced by cwcc.
Languages. ACM, New York, 1985, pp. 245-254.
[F88a] Felleisen, M. -v-CS: An Extended -Calculus
Acknowledgments
for Scheme. In Proceedings of the 1988 ACM
Conference on Lisp and Functional Programming.
Wewant to express our gratitude to Olivier Danvy for
ACM, New York, 1988, pp. 72-85.
his insightful comments on the optimization of continu-
ations and to one of the anonymous referees for encour-
[F88b] Felleisen, M. The Theory and Practice of First-
aging us to lo ok for reference [C73] \if only for hack
Class Prompts. In Proceedings of the 15th An-
value".
nual ACM Conference on Principles of Program-
ming Languages. ACM, New York, 1988, pp. 180-
190.
[GJLS87] Gi ord, D. K., Jouvelot, P., Lucassen, J.
M., and Sheldon, M. A. The FX-87 Reference Man-
ual. MIT/LCS/TR-407, 1987.
[H87] Hughes, J. Strictness Analysis by Abstract In-
terpretation of Continuations. In [AH87].
[HF87] Haynes, C. T., and Friedman, D. P. Emb ed-
ding Continuations in Pro cedural Ob jects. ACM 8
Trans. on Prog. Lang. and Syst. 9, 4 1987, [SM72] Sussman, G. J., and McDermott, D. From
582-598. PLANNER to CONNIVER { A Genetic Approach.
In Proceedings of the Fal l Joint Computer Confer-
[HFW86] Haynes, C. T., Friedman, D. P., and
ence. AFIPS Press, Reston, 1972, pp 1171-1179.
Wand, M. Obtaining Coroutines with Continua-
[W80] Wand, M. Continuation-based Multipro cessing.
tions. Comput. Lang. 11, 3/4 1986, 143-153.
In Proceedings of the 1980 ACM Conference on
Lisp and Functional Programming. ACM, New
[JG88] Jouvelot, P. and Gi ord, D. K. The FX-87 In-
York, 1980, pp. 19-28.
terpreter. In Proceedings of the 2nd IEEE Interna-
tional Conference on Computer Languages. IEEE,
New York, 1988, pp. 65-72.
[JG89a] Jouvelot, P. and Gi ord, D. K. Parallel Func-
tional Programming: The FX-87 Pro ject. To ap-
p ear in Proceedings of the International Workshop
on Paral lel and DistributedAlgorithms. North Hol-
land, Amsterdam, 1989.
[JG89b] Jouvelot, P. and Gi ord, D. K. Commu-
nication E ects for Message-Based Concurrency.
MIT/LCS/TM-386, 1989.
[L87] Lucassen, J. M. Types and E ects: Towards the
Integration of Functional and Imperative Program-
ming. PhD Dissertation, MIT/LCS/TR-408, 1987.
[LG88] Lucassen, J. M., and Gi ord, D. K. Polymor-
phic E ect Systems. In Proceedings of the 15th An-
nual ACM Conference on Principles of Program-
ming Languages. ACM, New York, 1988, pp. 47-
57.
[M74] Mo on, D. MacLISP Reference Manual, Revision
0. MIT Pro ject MAC, 1974.
[M79] McCracken, N. J. An Investigation of a Pro-
gramming Language with a Polymorphic Type
Structure. PhD Dissertation, Syracuse University,
1979.
[MR88] Meyer, A. R., and Riecke, J. Continuations
May Be Unreasonable. In Proceedings of the 1988
ACM Conference on Lisp and Functional Program-
ming. ACM, New York, 1988, pp. 63-71.
3
[R86] Rees, J. A., and Clinger, W. Eds. The Revised
Report on the Algorithmic Language Scheme.
MIT/AI Memo 848a, 1986.
[S78] Steele, G. L., and Sussman, G. J. The Art of
the Interpreter or, The Modularity Complex Parts
Zero, One and Two. MIT/AI Memo 453, 1978.
[S86] Schmidt, D. A. Denotational Semantics: A
Methodology for Language Development. Allyn and
Bacon, 1986. 9