1/27
Delimited Control with Multiple Prompts in Theory and Practice Paul Downen Zena M. Ariola
University of Oregon
Delimited Control with Multiple Prompts in Theory and Practice Paul - - PowerPoint PPT Presentation
Delimited Control with Multiple Prompts in Theory and Practice Paul Downen Zena M. Ariola University of Oregon HOPE14 August 31, 2014 1/27 Crash course on control 2/27 Separating a redex from its evaluation context 1 + 2 + ( 3 4 )
1/27
University of Oregon
2/27
3/27
3/27
3/27
4/27
1 + 2 + call/cc(λk.3 × (k 4))
4/27
1 + 2 + call/cc(λk.3 × (k 4)) 1 + 2 + call/cc(λk.3 × (k 4))
4/27
1 + 2 + call/cc(λk.3 × (k 4)) 1 + 2 + call/cc(λk.3 × (k 4)) ⇓ k : 1 + 2 + 3 × (k 4) 1 + 2 + 3 × (k 4)
4/27
1 + 2 + call/cc(λk.3 × (k 4)) 1 + 2 + call/cc(λk.3 × (k 4)) ⇓ k : 1 + 2 + 3 × (k 4) 1 + 2 + 3 × (k 4) 1 + 2 + 3 × k 4
4/27
1 + 2 + call/cc(λk.3 × (k 4)) 1 + 2 + call/cc(λk.3 × (k 4)) ⇓ k : 1 + 2 + 3 × (k 4) 1 + 2 + 3 × (k 4) 1 + 2 + 3 × k 4 ⇓ 1 + 2 + 4 1 + 2 + 4
5/27
1 + #( 2 + F(λk.3 × (k 4)) )
5/27
1 + #( 2 + F(λk.3 × (k 4)) ) 1 + # 2 + F(λk.3 × (k 4))
5/27
1 + #( 2 + F(λk.3 × (k 4)) ) 1 + # k : 2 + F(λk.3 × (k 4)) ⇓ 1 + #
1 + #( 3 × (k 4) )
5/27
1 + #( 2 + F(λk.3 × (k 4)) ) 1 + # k : 2 + F(λk.3 × (k 4)) ⇓ 1 + #
1 + #( 3 × (k 4) ) 1 + # 3 × k 4
5/27
1 + #( 2 + F(λk.3 × (k 4)) ) 1 + # k : 2 + F(λk.3 × (k 4)) ⇓ 1 + #
1 + #( 3 × (k 4) ) 1 + # 3 × k 4 ⇓ 1 + # 3 × 2 + 4 1 + #( 3 × (2 + 4) )
6/27
7/27
◮ Does F remove # surrounding E ′ ? ◮ Does continuation guard its call-site with a #?
8/27
E[#( E ′[ +F + V ] )] → E[#( V k )] where k x = #( E ′[ x ] ) E[#( E ′[ +F − V ] )] → E[#( V k )] where k x = E ′[ x ] E[#( E ′[ −F + V ] )] → E[ V k ] where k = #( E ′[ x ] ) E[#( E ′[ −F − V ] )] → E[ V k ] where k x = E ′[ x ]
8/27
E[#( E ′[ +F + V ] )] → E[#( V k )] where k x = #( E ′[ x ] ) E[#( E ′[ +F − V ] )] → E[#( V k )] where k x = E ′[ x ] E[#( E ′[ −F + V ] )] → E[ V k ] where k = #( E ′[ x ] ) E[#( E ′[ −F − V ] )] → E[ V k ] where k x = E ′[ x ]
8/27
E[#( E ′[ +F + V ] )] → E[#( V k )] where k x = #( E ′[ x ] ) E[#( E ′[ +F − V ] )] → E[#( V k )] where k x = E ′[ x ] E[#( E ′[ −F + V ] )] → E[ V k ] where k = #( E ′[ x ] ) E[#( E ′[ −F − V ] )] → E[ V k ] where k x = E ′[ x ]
9/27
◮ +F+: shift (S) and reset ( ) of Danvy and
◮ +F−: control (F) and prompt (#) of Felleisen ◮ −F+: shift0 (S0) and reset0 ( 0) ◮ −F−: control0 (F0) and prompt0 (#0)
10/27
List traversal two ways (Biernacki et al., 2005) Straverse xs = visit xs where visit [] = [] visit (x :: xs) = visit (S(λk. x :: ( k xs ) )) Ftraverse xs = #(visit xs) where visit [] = [] visit (x :: xs) = visit (F(λk. x :: ( k xs ) )) What’s the difference?
10/27
List traversal two ways (Biernacki et al., 2005) Straverse xs = visit xs where visit [] = [] visit (x :: xs) = visit (S(λk. x :: ( k xs ) )) Ftraverse xs = #(visit xs) where visit [] = [] visit (x :: xs) = visit (F(λk. x :: ( k xs ) )) What’s the difference? Straverse [1, 2, 3] →∗ [1, 2, 3] list copy Ftraverse [1, 2, 3] →∗ [3, 2, 1] list reverse
11/27
Continuation swap two ways swap x = S(λk1.S(λk2.k1 (k2 x))) swap0 x = S0(λk1.S0(λk2.k1 (k2 x))) What’s the difference?
11/27
Continuation swap two ways swap x = S(λk1.S(λk2.k1 (k2 x))) swap0 x = S0(λk1.S0(λk2.k1 (k2 x))) What’s the difference? 10 + 2 × (swap 1) →∗ 10 + k1 (k2 1) →∗ 12 where k1 x = 2 × x k2 x = x identity function 10 + 2 × (swap0 1)00 →∗ k1 (k2 1) →∗ 22 where k1 x = 2 × x0 k2 x = 10 + x0 Context switch
12/27
◮ Focus on ∗F+ operators ◮ shift and reset are heavily studied ◮ shift0 and reset0 recently gaining interest ◮ Both have theories with desirable properties
◮ Simple continuation-passing style semantics ◮ Sound and complete axiomatizations ◮ Error-free type and effect systems ◮ “Observational purity”
12/27
◮ Focus on ∗F− operators ◮ Major implementations of delimited control
◮ Racket: control and prompt ◮ Haskell library CC-delcont: control0 and prompt0 ◮ OCaml library delcontcc: control0 and prompt0
◮ Practical extensions of delimited control
◮ Integrated into languages with other effects ◮ Multiple prompts
12/27
◮ Focus on ∗F− operators ◮ Major implementations of delimited control
◮ Racket: control and prompt ◮ Haskell library CC-delcont: control0 and prompt0 ◮ OCaml library delcontcc: control0 and prompt0
◮ Practical extensions of delimited control
◮ Integrated into languages with other effects ◮ Multiple prompts
13/27
14/27
◮ Multiple prompts, referred to by name ◮ Similar to exception handling
◮ F
α: go to nearest prompt (handler) for
α
◮ #
α: delimit (handle) control effects for
α
15/27
#
γ(1 + # β( 2 + # α(3 + F β (λk.4 × (k 5)) ) ))
15/27
#
γ(1 + # β( 2 + # α(3 + F β (λk.4 × (k 5)) ) ))
#
γ(1 + # β )
2 + #
α(3 + )
F
β (λk.4 × (k 5))
15/27
#
γ(1 + # β( 2 + # α(3 + F β (λk.4 × (k 5)) ) ))
#
γ(1 + # β )
k : 2 + #
α(3 + )
F
β (λk.4 × (k 5))
⇓ #
γ(1 + # β )
#
γ(1 + #
β( 4 × (k 5) ))
15/27
#
γ(1 + # β( 2 + # α(3 + F β (λk.4 × (k 5)) ) ))
#
γ(1 + # β )
k : 2 + #
α(3 + )
F
β (λk.4 × (k 5))
⇓ #
γ(1 + # β )
#
γ(1 + #
β( 4 × (k 5) )) #
γ(1 + # β )
4 × k 5
15/27
#
γ(1 + # β( 2 + # α(3 + F β (λk.4 × (k 5)) ) ))
#
γ(1 + # β )
k : 2 + #
α(3 + )
F
β (λk.4 × (k 5))
⇓ #
γ(1 + # β )
#
γ(1 + #
β( 4 × (k 5) )) #
γ(1 + # β )
4 × k 5 ⇓ #
γ(1 + # β )
4 × 2 + #
α(3 + 5)
#
γ(1 + #
α(3 + 5))))
16/27
17/27
◮ Monadic Framework for Delimited Continuations
◮ control0 and prompt0 style control with multiple
◮ Use hybrid abstract/concrete continuation monad
◮ Stack of ordinary continuations ◮ Special markers representing prompts
18/27
◮ Systematic Approach to Delimited Control with
◮ shift0 and reset0 style control with multiple
◮ λ
◮ Dynamic continuation variables ◮ Splitting/joining dynamic environment of continuations
19/27
◮ Biggest mismatch comes down to representation
◮ Monadic framework: marked stack
◮ λ
20/27
◮ Embed λ
◮ Flatten [Ident ∗ Cont] to [Ident + Cont]? ◮ Easy!
21/27
◮ Embed Monadic Framework into λ
◮ Restore [Iden + Cont] to [Ident ∗ Cont] ◮ Not so easy. . .
22/27
◮ Reserve one dynamic continuation variable (
◮ “Return” value by sending it to
tp
◮ “Empty” continuation k
tp “returns” input to
tp
◮ Continuations associated with
tp (next return point)
◮ Prompts associated with empty continuation k
tp (skip other
prompts, go to next return point)
◮ Embedding back into environment!
[k3, α3, α2, k2, k1, α1] = [ tp → k3, α3 → k
tp,
α2 → k
tp,
tp → k1, α1 → k
tp]
23/27
◮ Embedding from Monadic Framework to λ
◮ With multiple prompts, shift0 and reset0 style
◮ Corollary: shift0 and reset0 style control with 2
24/27
25/27
#( E[ visit(x :: xs) ] ) →∗ #(x :: E[visit xs]) Ftraverse [1, 2, 3] → #( visit [1, 2, 3] ) →∗ #( 1 :: visit [2, 3] ) →∗ #( 2 :: 1 :: visit [3] ) →∗ #( 3 :: 2 :: 1 :: visit [] ) → #(3 :: 2 :: 1 :: []) →∗ [3, 2, 1]
25/27
#( E[ visit(x :: xs) ] ) →∗ #(x :: E[visit xs]) Ftraverse [1, 2, 3] → #( visit [1, 2, 3] ) →∗ #( 1 :: visit [2, 3] ) →∗ #( 2 :: 1 :: visit [3] ) →∗ #( 3 :: 2 :: 1 :: visit [] ) → #(3 :: 2 :: 1 :: []) →∗ [3, 2, 1]
25/27
#( E[ visit(x :: xs) ] ) →∗ #(x :: E[visit xs]) Ftraverse [1, 2, 3] → #( visit [1, 2, 3] ) →∗ #( 1 :: visit [2, 3] ) →∗ #( 2 :: 1 :: visit [3] ) →∗ #( 3 :: 2 :: 1 :: visit [] ) → #(3 :: 2 :: 1 :: []) →∗ [3, 2, 1]
25/27
#( E[ visit(x :: xs) ] ) →∗ #(x :: E[visit xs]) Ftraverse [1, 2, 3] → #( visit [1, 2, 3] ) →∗ #( 1 :: visit [2, 3] ) →∗ #( 2 :: 1 :: visit [3] ) →∗ #( 3 :: 2 :: 1 :: visit [] ) → #(3 :: 2 :: 1 :: []) →∗ [3, 2, 1]
26/27
27/27