This document discusses axiomatizing subtyped delimited continuations using the shift0/$ operators. It begins with introductions to continuations, evaluation contexts, and delimited control operators like shift/reset and shift0/$. It then presents the axioms for shift0/reset0 and shift0/$, and discusses Sabry's proof method for showing the axioms are complete with respect to the CPS semantics through an intermediate language. The document focuses on providing formal definitions and semantics to give an axiomatic account of shift0/$.
2. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Structure of the talk
Short introduction to continuations
Introduction to shift0/$ operators
The axioms
The proof method
The typed version
Conclusion
This is a continuation of our previous work (ICFP’11,
APLAS’12).
3. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
What are continuations?
Continuations are representations of the rest of the
program execution.
Control operators are a means of altering a program’s
control flow.
They can be thought of as capturing and restoring the
program’s control stack, making continuations first
class.
The operator call/cc, which captures ,,full”
continuations, is well known and implemented in e.g.
Scheme and SML/NJ.
5. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Partial evaluation contexts
One can consider partial contexts:
if sq(2) = 4 then 1 else 0
Partial context is a prefix of the full context.
cyan part – evaluation context
yellow part – partial evaluation context
6. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Delimited control operators
They allow to reify partial contexts as functions, just as
call/cc reifies entire contexts.
Examples are Felleisen’s control/prompt and Danvy and
Filinski’s shift/reset.
Delimited control has lots of applications, including
asynchronous I/O, representing monads, Web
programming, mobile code, linguistics, and so on.
7. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Delimited control operators
Delimited control operators usually come in pairs:
the delimiter, which marks where a context begins,
capture operator, which reifies the context up to the
dynamically nearest delimiter.
Example:
1 + 2 + Sf.f(f 3)
8. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Delimited control operators shift/reset
The most known and well explored delimited control
operators.
The shift operator captures the context up to (and
including) the nearest delimiter and resumes execution
in an empty context.
1 + 2 ∗ Sf. 3 + Sg.f(g 4)
The term above evaluates to 15: f gets the yellow context,
g gets the cyan one. Notice the “implicit” delimiter created
by a shift.
9. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Delimited control operators shift0/reset0
A variant of shift/reset operators (also by Danvy and
Filinski).
When shift0 executes, the execution resumes in the
surrounding context.
This allows the shift0 operator to “reach” beyond the
nearest surrounding delimiter.
1 + 2 + 3 + S0f.S0g.f (g (g 4))
The term above evaluates to 12. (f gets the
yellow context , g gets the cyan one .)
10. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Delimited control operators shift0/$
A variant of shift0/reset0 operators, inspired by Kiselyov
and Shan’s work (TLCA’07).
The $ operator is a delimiter with a “chain link” to a
function which is executed in place (not inside!) of the
delimiter when the delimited term evaluates completely.
(λx.x ∗ 2) $ (λx.x + 1) $ 1 + S0f.S0g.f (g 2)
Evaluates to 6. (f gets the yellow context , g gets the
cyan one .)
Reading tip: the $ operator is right-associative, binds weaker
than every other binary operator, but stronger than λ.
12. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Bigger example (shift0/$)
(λx.x + 2) $ (λy.S0f.f (f y)) $ 1 + S0g.2 ∗ g 1
→(λx.x + 2) $ 2 ∗ ((λy.S0f.f (f y)) $ 1 + 1)
The term above evaluates to 14:
g gets the yellow context , which gets applied to 1,
13. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Bigger example (shift0/$)
(λx.x + 2) $ (λy.S0f.f (f y)) $ 1 + S0g.2 ∗ g 1
→(λx.x + 2) $ 2 ∗ ((λy.S0f.f (f y)) $ 1 + 1)
→(λx.x + 2) $ 2 ∗ (S0f.f (f 2))
The term above evaluates to 14:
g gets the yellow context , which gets applied to 1,
y gets the value 2,
14. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Bigger example (shift0/$)
(λx.x + 2) $ (λy.S0f.f (f y)) $ 1 + S0g.2 ∗ g 1
→(λx.x + 2) $ 2 ∗ ((λy.S0f.f (f y)) $ 1 + 1)
→(λx.x + 2) $ 2 ∗ (S0f.f (f 2))
→(λy.(λx.x + 2) $ 2 ∗ y) ((λy.(λx.x + 2) $ 2 ∗ y) 2)
The term above evaluates to 14:
g gets the yellow context , which gets applied to 1,
y gets the value 2,
f gets the cyan context joined with 2 ∗ ,
15. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Bigger example (shift0/$)
(λx.x + 2) $ (λy.S0f.f (f y)) $ 1 + S0g.2 ∗ g 1
→(λx.x + 2) $ 2 ∗ ((λy.S0f.f (f y)) $ 1 + 1)
→(λx.x + 2) $ 2 ∗ (S0f.f (f 2))
→(λy.(λx.x + 2) $ 2 ∗ y) ((λy.(λx.x + 2) $ 2 ∗ y) 2)
→(λy.(λx.x + 2) $ 2 ∗ y) 6 → 14
The term above evaluates to 14:
g gets the yellow context , which gets applied to 1,
y gets the value 2,
f gets the cyan context joined with 2 ∗ ,
f (f y) gets evaluated.
16. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
CPS semantics
The control operators can be interpreted in the lambda
calculus. This makes the continuations explicit.
x = λk.k x
λx.e = λk.k (λx. e )
e1 e2 = λk. e1 (λv1. e2 (λv2.v1 v2 k))
S0x.e = λx. e
e = e (λx.λk.k x)
e1 $ e2 = λk. e1 (λv1. e2 v1 k)
This interpretation is consistent with the operational view
presented in previous slides: e1 → e2 implies e1 =βη e2 .
17. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
The goal
The operational view is not complete with respect to the
CPS semantics. For example:
λf.(λx.x) $ 2 + f 1
=βη λk.k (λf.f 1 (λx.λk.k (2 + x)))
=βη λf.(λx.2 + x) $ f 1
But there is no way to equalize the two terms using only
operational rules.
18. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
The goal
The operational view is not complete with respect to the
CPS semantics. For example:
λf.(λx.x) $ 2 + f 1
=βη λk.k (λf.f 1 (λx.λk.k (2 + x)))
=βη λf.(λx.2 + x) $ f 1
But there is no way to equalize the two terms using only
operational rules.
The goal: find a finite set of equational axioms defined on
the terms of shift0/reset0 (or shift0/$) such that
e1 =ax e2 iff e1 =βη e2
19. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
The axioms for shift0/reset0
(λx.e) v = e{v/x}
λx.v x = v x ∈ FV(v)
(λx.E[x]) e = E[e] x ∈ FV(E)
E[S0x.e] = e{λx. E[x] /x} x ∈ FV(E)
v = v
S0k. (λx.S0z.k x) e = e k ∈ FV(e)
(λx.S0k. e1 ) e2 = (λx.e1) e2 k ∈ FV(e1)
21. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Proving completeness
Proving soundness is easy. What about completeness?
Proof for shift/reset: uses a restricted grammar for the
target terms with six syntactic categories and an inverse
translation (Kameyama and Hasegawa, ICFP’03).
Sabry introduced a technique for proving completeness
for various control operators, which involves an
intermediate language. However, the technique was not
successfully applied for shift/reset.
But it worked very well for shift0/$!
22. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Taking care of shift0/reset0
The shift0/reset0 operators are of equal expressive
power as shift0/$ (APLAS’12):
e ≈ (λx.x) $ e
e1 $ e2 ≈ (λw. (λv.S0k.w v) e2 ) e1
(I will be using e as a shorthand for (λx.x) $ e)
It can be proved that the axioms for shift0/reset0 are
sound and complete if and only if the axioms for
shift0/$ are sound and complete.
23. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Opaque CPS
Opaque CPS translation is an abstract version of the
CPS translation:
x o = get k.k x
λx.e o = get k.k (λx. e o)
e1 e2 o = get k.send (λv1.
send (λv2.send k (v1 v2)) e2 o) e1 o
Uses abstract control operators (get and send), which
have semantics consistent with β and η-conversions, for
continuation passing:
send v get x.e =op e{v/x}
get x.send x e =op e x ∈ FV(e)
24. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
CGS translation
The opaque CPS translation with the get and send
operators implemented using some control operators is
called a continuation-grabbing style (CGS) translation.
The fact that CGS and CPS are both instances of
opaque CPS can be used for proving completeness.
Can we find a CGS translation for shift0/$?
25. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
CGS translation
The opaque CPS translation with the get and send
operators implemented using some control operators is
called a continuation-grabbing style (CGS) translation.
The fact that CGS and CPS are both instances of
opaque CPS can be used for proving completeness.
Can we find a CGS translation for shift0/$?
get x.e =def S0x.e
send e1 e2 =def e1 $ e2
It’s that simple!
26. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
CGS translation for shift0/$
We get the following translation:
x g = S0k.k x
λx.e g = S0k.k (λx. e g)
e1 e2 g = S0k.(λv1.(λv2.k $ v1 v2) $ e2 g) $ e1 g
S0x.e g = S0x. e g
e1 $ e2 g = S0k.(λv1.k $ v1 $ e2 g) $ e1 g
e1 =g e2 implies e1 =ax e2;
e g =ax e;
therefore, if e1 g =g e2 g, then e1 =ax e2.
Completeness follows easily, with a minor hurdle.
27. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Type system for shift0
There are expressive and elegant type systems for both
shift0/reset0 (ICFP’11) and shift0/$ (APLAS’12).
The type systems track how the terms manipulate their
contexts using effects. In particular, it distinguishes
effect-free terms from effectful ones.
An important part of the type systems, which gives
them their expresiveness, is subtyping. It allows to use
effect-free terms in contexts permitting effects. (This is
a simplification.)
28. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Proofs in the typed setting
The proofs can be adapted for the typed setting.
In the adapted proofs, the subtyping is eliminated at the
CGS stage: the CGS terms are fully explicit.
The typed axioms are more permissive than the untyped
ones: value restriction is replaced by purity restriction.
29. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
K&H axioms in the typed setting
The shift/reset operators can be embedded in
shift0/reset0:
Sx.e =def S0x. e
Using this embedding, the axioms of Kameyama and
Hasegawa are not validated in the untyped setting.
The type system for shift/reset by Danvy and Filinski
can be embedded into the type system for shift0/reset0.
In the image of this embedding, the axioms of
Kameyama and Hasegawa are valid.
30. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
K&H axioms in the untyped setting
There is a different embedding of shift/reset in shift0/$:
Sx.e =def S0x.e{λy.S0f.S0g.(λz.g $ f z) $ x y/x}
e =def S0f.S0g.(λx.g $ f x) $ e
The Kameyama and Hasegawa’s axioms are valid in the
untyped setting when using this embedding.
31. Axiomatizing
Subtyped
Delimited
Continuations
Marek Materzok
Introduction
Evaluation contexts
Delimited control
Shift0/$
Axioms
Proof
Sabry’s proof method
CGS translation
Typed version
Relationship with
shift/reset
Conclusions
Conclusions
The Sabry’s elegant proof method can be applied for
shift0/$.
The axioms for shift0/$ are simple and elegant.
The axioms for shift0/reset0 can be proved complete
using the axioms for shift0/$.
The proofs can be adapted to the typed version of the
languages considered.
The Kameyama and Hasegawa’s axioms for shift/reset
are validated only in the typed setting (with the folklore
interpretation of shift/reset).