A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
A Dynamic Interpretationof the CPS Hierarchy
Marek Materzok, Dariusz BiernackiInstitute of Computer Science, University of Wroc law
APLAS 2012Dec 12, 2012
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Structure of the talk
I Short introduction to continuations
I Introducing shift0/$ operators
I The CPS Hierarchy
I Interpreting the CPS Hierarchy with shift0/$
I Conclusion
This is a continuation of our previous work (ICFP’11).
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
What are continuations?
I Continuations are representations of the rest of theprogram execution.
I Control operators are a means of altering a program’scontrol flow.
I They can be thought of as capturing and restoring theprogram’s control stack, making continuations firstclass.
I The operator call/cc, which captures ,,full”continuations, is well known and implemented in e.g.Scheme and SML/NJ.
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Evaluation contexts
Evaluation context is a ,,term with a hole”:
if sq(2) = 4 then 1 else 0
I cyan part – evaluation context
It is a formal representation of the continuation.
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
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.
I cyan part – evaluation context
I yellow part – partial evaluation context
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Delimited control operators
I They allow to reify partial contexts as functions, just ascall/cc reifies entire contexts.
I Examples are Felleisen’s control/prompt and Danvy andFilinski’s shift/reset.
I Delimited control has lots of applications, includingasynchronous I/O, representing monads, Webprogramming, mobile code, linguistics, and so on.
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Delimited control operators
Delimited control operators usually come in pairs:
I the delimiter, which marks where a context begins,
I capture operator, which reifies the context up to thedynamically nearest delimiter.
Example:
1 + 〈2 + Sf.f(f 3) 〉
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Delimited control operators shift/reset
I The most known and well explored delimited controloperators.
I The shift operator captures the context up to (andincluding) the nearest delimiter and resumes executionin 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 createdby a shift.
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
The abstract machine for shift/reset
T denotes a stack of evaluation contexts.
〈λx.e, E · T 〉e ⇒ 〈λx.e, E · T 〉a〈E′, E · T 〉e ⇒ 〈E′, E · T 〉a〈e1 e2, E · T 〉e ⇒ 〈e1, E e2 · T 〉e
(1) 〈Sf.e, E · T 〉e ⇒ 〈e{E/f}, • · T 〉e(2) 〈〈e〉, T 〉e ⇒ 〈e, • · T 〉e
〈v,E e2 · T 〉a ⇒ 〈e2, v E · T 〉e〈v, (λx.e) E · T 〉a ⇒ 〈e{v/x}, E · T 〉e
(3) 〈v,E′ E · T 〉a ⇒ 〈v,E′ · E · T 〉a(4) 〈v, • · T 〉a ⇒ 〈v, T 〉a
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
The abstract machine for shift/reset
〈Sf.e, E · T 〉e ⇒ 〈e{E/f}, • · T 〉e
Sf.eE[�]. . .
⇒e{E/f}�. . .
〈〈e〉, T 〉e ⇒ 〈e, • · T 〉e
〈e〉. . .
⇒e
�. . .
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
The abstract machine for shift/reset
〈v,E′ E · T 〉a ⇒ 〈v,E′ · E · T 〉a
v
E[E′ �]. . .
⇒
v
E′[�]E[�]. . .
〈v, • · T 〉a ⇒ 〈v, T 〉a
v
�. . .
⇒ v. . .
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Delimited control operators shift0/reset0
I A variant of shift/reset operators (also by Danvy andFilinski).
I When shift0 executes, the execution resumes in thesurrounding context.
I This allows the shift0 operator to “reach” beyond thenearest surrounding delimiter.
1 + 〈2 + 〈3 + S0f.S0g.f (g (g 4)) 〉 〉
The term above evaluates to 12. (f gets theyellow context , g gets the cyan one .)
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
The abstract machine for shift0/reset0
Only single change from shift/reset:
〈Sf.e, E · T 〉e ⇒ 〈e{E/f}, • · T 〉e
Sf.eE[�]. . .
⇒e{E/f}�. . .
is changed to:
〈S0f.e, E · T 〉e ⇒ 〈e{E/f}, T 〉e
S0f.eE[�]. . .
⇒ e{E/f}. . .
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Delimited control operators shift0/$
I A variant of shift0/reset0 operators, inspired by Kiselyovand Shan’s work (TLCA’07).
I The $ operator is a delimiter with a “chain link” to afunction which is executed in place (not inside!) of thedelimiter when the delimited context evaluatescompletely.
(λx.x+ 2) $ (λy.S0f.f (f y)) $ 1 + S0g.2 ∗ g 1
The term above evaluates to 14:
I g gets the yellow context , which gets applied to 1,
I y gets the value 2,
I f gets the cyan context joined with 2 ∗ ,
I f (f y) gets evaluated.
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
The abstract machine for shift0/reset0
Has to take care of the chain links!
〈λx.e, w$E · T 〉e ⇒ 〈λx.e, w$E · T 〉a〈v$E′, w$E · T 〉e ⇒ 〈v$E′, w$E · T 〉a〈e1 e2, w$E · T 〉e ⇒ 〈e1, w$(E e2) · T 〉e〈Sf.e, w$E · T 〉e ⇒ 〈e{w$E/f}, • · T 〉e
(1) 〈e1 $ e2, w$E · T 〉e ⇒ 〈e1, w$(E $ e2) · T 〉e〈v, w$(E e2) · T 〉a ⇒ 〈e2, w$(v E) · T 〉e
〈v, w$((λx.e) E) · T 〉a ⇒ 〈e{v/x}, w$E · T 〉e〈v, w$((w′$E′) E) · T 〉a ⇒ 〈v, w′$E′ · w$E · T 〉a
(2) 〈v, w$(E $ e2) · T 〉a ⇒ 〈e2, v$ • ·w$E · T 〉e(3) 〈v, (λx.e)$ • ·T 〉a ⇒ 〈e{v/x}, T 〉a(4) 〈v, (w$E)$ • ·T 〉a ⇒ 〈v, w$E · T 〉a
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
The abstract machine for shift0/$
〈e1 $ e2, w$E · T 〉e ⇒ 〈e1, w$(E $ e2) · T 〉e
e1 $ e2w E[�]
. . .⇒
e1w E[� $ e2]
. . .
〈v, w$(E $ e2) · T 〉a ⇒ 〈e2, v$ • ·w$E · T 〉e
v
w E[� $ e2]. . .
⇒
e2v �w E[�]
. . .
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
The abstract machine for shift0/$
〈v, (λx.e)$ • ·T 〉a ⇒ 〈e{v/x}, T 〉av
λx.e �. . .
⇒ e{v/x}. . .
〈v, (w$E)$ • ·T 〉a ⇒ 〈v, w$E · T 〉a
v
w E[�] �. . .
⇒v
w E[�]. . .
Notice that λx.w $E[x] behaves like (w$E)!
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
More about shift0/$
The operators are equally expressible as shift0/reset0, even inthe typed setting!
〈e〉 ≈ (λx.x) $ ee1 $ e2 ≈ (λw.〈(λv.S0k.w v) e2〉) e1
(I will be using 〈e〉 as a shorthand for (λx.x) $ e)But the $ is very convenient...
S0k.k $ e ≈ e(λx.v $E[x]) $ e ≈ v $E[e]
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
The CPS Hierarchy
I Introduced by Danvy and Filinski, enables expressingnested control effects.
I The n-th level of the CPS Hierarchy has n pairs ofdelimited control operators: Sik. and 〈 〉i.
I They give access to the layers of continuationsconstructed by iterated CPS translation.
I The Sik. operator captures the context up to thenearest 〈 〉j (where j ≥ i), which is not removed (theexecution resumes inside it). The captured context isdelimited by 〈 〉i.
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
The abstract machine for the CPS Hierarchy
I Has n+ 1 layers of continuations.
I The first level has one evaluation context.
I Every next level has stacks of continuations of theprevious level.
〈Sik.e, E, T1, . . . , Ti−1, Ti, . . . , Tn〉e⇒ 〈e{(E, T1, . . . , Ti−1)/k}, •, •1, . . . , •i−1, Ti, . . . , Tn〉e〈〈e〉i, E, T1, . . . , Ti−1, Ti, . . . , Tn〉e⇒ 〈e, •, •1, . . . , •i−1, ((E · T1) . . . · Ti−1) · Ti, Ti+1, . . . , Tn〉e〈v, •, •1, . . . , •i−1, ((E · T1) . . . · Ti−1) · Ti, Ti+1, . . . , Tn〉a⇒ 〈v,E, T1, . . . , Ti−1, Ti, . . . , Tn〉a〈v, (E′, T ′1, . . . , T ′i−1) E, T1, . . . , Ti−1, Ti, . . . , Tn〉a⇒ 〈v,E′, T ′1, . . . , T ′i−1, ((E · T1) . . . · Ti−1) · Ti, Ti+1, . . . , Tn〉a
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
The abstract machine for the CPS Hierarchy
〈S2k.e, E, T1, T2〉e ⇒ 〈e{(E, T1)/k}, •, •1, T2〉e
S2k.eE[�]T1T2
⇒
e{(E, T1)/k}�•1T2
〈〈e〉2, E, T1, T2〉e ⇒ 〈e, •, •1, (E · T1) · T2〉e
〈e〉2E[�]T1T2
⇒
e
�•1
T2 T1 – E
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
The abstract machine for the CPS Hierarchy
〈v, •, •1, (E · T1) · T2〉a ⇒ 〈v,E, T1, T2〉av
�•1
T2 T1 – E
⇒
v
E[�]T1T2
〈v, (E′, T ′1) E, T1, T2〉a ⇒ 〈v,E′, T ′1, (E · T1) · T2〉a
vE[(E′, T ′
1) �]
T1T2
⇒
v
E′[�]T ′1
T2 T1 – E
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Embedding the CPS Hierarchy in shift0/$
Structure seemingly not similar, but... Idea:
I Layers – stack depth
I Continuation stacks – $ chain links
Sik.e = S0k.S0k1. . . .S0ki−1.〈 i. . . 〈e{λx.S0k′.S0k′1. . . .S0k′i.(λy.k′i $ . . . $ k1 $ k x) $ ki−1 $ . . . $ k
′1 $ k
′ x/k}〉 . . .〉
〈e〉i = S0k.S0k1. . . .S0ki.(λx.ki $ . . . $ k1 $ k x) $ 〈 i. . . 〈e〉 . . .〉
Let’s see how this works!
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Embedding the CPS Hierarchy in shift0/$
〈〈e〉2, E, T1, T2〉e ⇒ 〈e, •, •1, (E · T1) · T2〉e
〈e〉2E[�]T1T2
⇒
e
�•1
T2 T1 – E
〈S0k.S0k1.S0k2.(λx.k2 $ k1 $ k x) $ 〈〈e〉〉,((λx.x)$E) · (w1$•) · (w2$•) · ((λx.x)$•)〉e
λx.x E[�]w1 �w2 �λx.x �
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Embedding the CPS Hierarchy in shift0/$
〈〈e〉2, E, T1, T2〉e ⇒ 〈e, •, •1, (E · T1) · T2〉e
〈e〉2E[�]T1T2
⇒
e
�•1
T2 T1 – E
〈(λx.(w2$•) $ (w1$•) $ ((λx.x)$E) x) $ 〈〈e〉〉,((λx.x)$•)〉e
λx.x �
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Embedding the CPS Hierarchy in shift0/$
〈〈e〉2, E, T1, T2〉e ⇒ 〈e, •, •1, (E · T1) · T2〉e
〈e〉2E[�]T1T2
⇒
e
�•1
T2 T1 – E
〈〈〈e〉〉,((λx.(w2$•) $ (w1$•) $ ((λx.x)$E) x)$•) · ((λx.x)$•)〉e
w2 � (w1$•) $ (id$E) � �λx.x �
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Embedding the CPS Hierarchy in shift0/$
〈〈e〉2, E, T1, T2〉e ⇒ 〈e, •, •1, (E · T1) · T2〉e
〈e〉2E[�]T1T2
⇒
e
�•1
T2 T1 – E
〈e, ((λx.x)$•) · ((λx.x)$•)·((λx.(w2$•) $ (w1$•) $ ((λx.x)$E) x)$•) · ((λx.x)$•)〉e
λx.x �λx.x �
w2 � (w1$•) $ (id$E) � �λx.x �
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Embedding the CPS Hierarchy in shift0/$
〈v, •, •1, (E · T1) · T2〉a ⇒ 〈v,E, T1, T2〉av
�•1
T2 T1 – E
⇒
v
E[�]T1T2
〈v, ((λx.x)$•) · ((λx.x)$•)·((λx.(w2$•) $ (w1$•) $ ((λx.x)$E) x)$•) · ((λx.x)$•)〉a
λx.x �λx.x �
w2 � (w1$•) $ (id$E) � �λx.x �
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Embedding the CPS Hierarchy in shift0/$
〈v, •, •1, (E · T1) · T2〉a ⇒ 〈v,E, T1, T2〉av
�•1
T2 T1 – E
⇒
v
E[�]T1T2
〈v,((λx.(w2$•) $ (w1$•) $ ((λx.x)$E) x)$•) · ((λx.x)$•)〉a
w2 � (w1$•) $ (id$E) � �λx.x �
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Embedding the CPS Hierarchy in shift0/$
〈v, •, •1, (E · T1) · T2〉a ⇒ 〈v,E, T1, T2〉av
�•1
T2 T1 – E
⇒
v
E[�]T1T2
〈(w2$•) $ (w1$•) $ ((λx.x)$E) v,((λx.x)$•)〉e
λx.x �
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Embedding the CPS Hierarchy in shift0/$
〈v, •, •1, (E · T1) · T2〉a ⇒ 〈v,E, T1, T2〉av
�•1
T2 T1 – E
⇒
v
E[�]T1T2
〈(((λx.x)$E) v,((w1$•)$•) · ((w2$•)$•) · ((λx.x)$•)〉e
w1 � �w2 � �
λx.x �
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Embedding the CPS Hierarchy in shift0/$
〈v, •, •1, (E · T1) · T2〉a ⇒ 〈v,E, T1, T2〉av
�•1
T2 T1 – E
⇒
v
E[�]T1T2
〈v,((λx.x)$E) · ((w1$•)$•) · ((w2$•)$•) · ((λx.x)$•)〉a
λx.x E[�]w1 � �w2 � �
λx.x �
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Shift0 is more powerful
csort l =let insert a =
let cinsert k = S0f.case f [] of[] → 〈〈a :: k ()〉〉[b] →
if a < bthen cinsert (λx.f $ k x)else f $ 〈a :: k ()〉
in S0k. cinsert kin 〈〈 foldr (λx.λl′. insert x; l′ ) [] l 〉〉
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Summary of the results
We have shown the correspondence to hold for:
I Abstract machines (as shown)
I CPS translations (βη-equality)
I Reduction semantics
I Type systems (with the Biernacka et al. type system –PPDP’11)
We have also shown a shift0/$ program which cannotcorrespond to any CPS Hierarchy program.
A DynamicInterpretation
of the CPSHierarchy
Marek Materzok,Dariusz Biernacki
Introduction
Evaluation contexts
Delimited control
Shift0/$
CPS Hierarchy
Embedding
Conclusions
Conclusions
What does this result give us?
I The shift0/reset0 operators have more expressive powerthan the CPS Hierarchy.
I We can use the much simpler shift0/reset0 toimplement the CPS Hierarchy.
I The result can lead to a level-polymorphic type systemfor the Hierarchy.
Thank you for your attention!
Top Related