Delimited Control with Multiple Prompts in Theory and Practice Paul - - PowerPoint PPT Presentation

delimited control with multiple prompts in theory and
SMART_READER_LITE
LIVE PREVIEW

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 )


slide-1
SLIDE 1

1/27

Delimited Control with Multiple Prompts in Theory and Practice Paul Downen Zena M. Ariola

University of Oregon

HOPE’14 — August 31, 2014

slide-2
SLIDE 2

2/27

Crash course on control

slide-3
SLIDE 3

3/27

Separating a redex from its evaluation context 1 + 2 + (3 × 4)

slide-4
SLIDE 4

3/27

Separating a redex from its evaluation context 1 + 2 + (3 × 4) 1 + 2 + 3 × 4

slide-5
SLIDE 5

3/27

Separating a redex from its evaluation context 1 + 2 + (3 × 4) 1 + 2 + 3 × 4 ⇓ 1 + 2 + 12 1 + 2 + 12

slide-6
SLIDE 6

4/27

Classical control: Abortive continuations

1 + 2 + call/cc(λk.3 × (k 4))

slide-7
SLIDE 7

4/27

Classical control: Abortive continuations

1 + 2 + call/cc(λk.3 × (k 4)) 1 + 2 + call/cc(λk.3 × (k 4))

slide-8
SLIDE 8

4/27

Classical control: Abortive continuations

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)

slide-9
SLIDE 9

4/27

Classical control: Abortive continuations

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

slide-10
SLIDE 10

4/27

Classical control: Abortive continuations

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

slide-11
SLIDE 11

5/27

Delimited control: Composable continuations

1 + #( 2 + F(λk.3 × (k 4)) )

slide-12
SLIDE 12

5/27

Delimited control: Composable continuations

1 + #( 2 + F(λk.3 × (k 4)) ) 1 + # 2 + F(λk.3 × (k 4))

slide-13
SLIDE 13

5/27

Delimited control: Composable continuations

1 + #( 2 + F(λk.3 × (k 4)) ) 1 + # k : 2 + F(λk.3 × (k 4)) ⇓ 1 + #

  • 3 × (k 4)

1 + #( 3 × (k 4) )

slide-14
SLIDE 14

5/27

Delimited control: Composable continuations

1 + #( 2 + F(λk.3 × (k 4)) ) 1 + # k : 2 + F(λk.3 × (k 4)) ⇓ 1 + #

  • 3 × (k 4)

1 + #( 3 × (k 4) ) 1 + # 3 × k 4

slide-15
SLIDE 15

5/27

Delimited control: Composable continuations

1 + #( 2 + F(λk.3 × (k 4)) ) 1 + # k : 2 + F(λk.3 × (k 4)) ⇓ 1 + #

  • 3 × (k 4)

1 + #( 3 × (k 4) ) 1 + # 3 × k 4 ⇓ 1 + # 3 × 2 + 4 1 + #( 3 × (2 + 4) )

slide-16
SLIDE 16

6/27

A zoo of delimited control

  • perators
slide-17
SLIDE 17

7/27

Design decisions E[#(E ′[F V ])]

◮ Does F remove # surrounding E ′ ? ◮ Does continuation guard its call-site with a #?

slide-18
SLIDE 18

8/27

A family of operators: ∗F∗

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 ]

slide-19
SLIDE 19

8/27

A family of operators: +F∗ vs −F∗

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 ]

slide-20
SLIDE 20

8/27

A family of operators: ∗F+ vs ∗F−

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 ]

slide-21
SLIDE 21

9/27

A family of operators

◮ +F+: shift (S) and reset ( ) of Danvy and

Filinski

◮ +F−: control (F) and prompt (#) of Felleisen ◮ −F+: shift0 (S0) and reset0 ( 0) ◮ −F−: control0 (F0) and prompt0 (#0)

slide-22
SLIDE 22

10/27

shift = control?

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?

slide-23
SLIDE 23

10/27

shift = control

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

slide-24
SLIDE 24

11/27

shift = shift0?

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?

slide-25
SLIDE 25

11/27

shift = shift0

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

slide-26
SLIDE 26

12/27

Theory vs. Practice Theory

◮ 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”

slide-27
SLIDE 27

12/27

Theory vs. Practice Practice

◮ 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

slide-28
SLIDE 28

12/27

Theory vs. Practice Practice

◮ 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

slide-29
SLIDE 29

13/27

Giving prompts a name

slide-30
SLIDE 30

14/27

Multiple prompts

◮ Multiple prompts, referred to by name ◮ Similar to exception handling

◮ F

α: go to nearest prompt (handler) for

α

◮ #

α: delimit (handle) control effects for

α

slide-31
SLIDE 31

15/27

Multiple prompts: Dynamic continuations

#

γ(1 + # β( 2 + # α(3 + F β (λk.4 × (k 5)) ) ))

slide-32
SLIDE 32

15/27

Multiple prompts: Dynamic continuations

#

γ(1 + # β( 2 + # α(3 + F β (λk.4 × (k 5)) ) ))

#

γ(1 + # β )

2 + #

α(3 + )

F

β (λk.4 × (k 5))

slide-33
SLIDE 33

15/27

Multiple prompts: Dynamic continuations

#

γ(1 + # β( 2 + # α(3 + F β (λk.4 × (k 5)) ) ))

#

γ(1 + # β )

k : 2 + #

α(3 + )

F

β (λk.4 × (k 5))

⇓ #

γ(1 + # β )

  • 4 × (k 5)

#

γ(1 + #

β( 4 × (k 5) ))

slide-34
SLIDE 34

15/27

Multiple prompts: Dynamic continuations

#

γ(1 + # β( 2 + # α(3 + F β (λk.4 × (k 5)) ) ))

#

γ(1 + # β )

k : 2 + #

α(3 + )

F

β (λk.4 × (k 5))

⇓ #

γ(1 + # β )

  • 4 × (k 5)

#

γ(1 + #

β( 4 × (k 5) )) #

γ(1 + # β )

4 × k 5

slide-35
SLIDE 35

15/27

Multiple prompts: Dynamic continuations

#

γ(1 + # β( 2 + # α(3 + F β (λk.4 × (k 5)) ) ))

#

γ(1 + # β )

k : 2 + #

α(3 + )

F

β (λk.4 × (k 5))

⇓ #

γ(1 + # β )

  • 4 × (k 5)

#

γ(1 + #

β( 4 × (k 5) )) #

γ(1 + # β )

4 × k 5 ⇓ #

γ(1 + # β )

4 × 2 + #

α(3 + 5)

#

γ(1 + #

  • β(4 × (2 + #

α(3 + 5))))

slide-36
SLIDE 36

16/27

Putting practice to theory

slide-37
SLIDE 37

17/27

Multiple prompts via marked stacks

◮ Monadic Framework for Delimited Continuations

(Dybvig et al., 2007)

◮ control0 and prompt0 style control with multiple

prompts

◮ Use hybrid abstract/concrete continuation monad

◮ Stack of ordinary continuations ◮ Special markers representing prompts

slide-38
SLIDE 38

18/27

Multiple prompts via dynamic binding

◮ Systematic Approach to Delimited Control with

Multiple Prompts (Downen and Ariola, 2012)

◮ shift0 and reset0 style control with multiple

prompts

◮ λ

µ0: Conservative extension of CBV Parigot’s λµ (i.e., λ-calculus with call/cc)

◮ Dynamic continuation variables ◮ Splitting/joining dynamic environment of continuations

slide-39
SLIDE 39

19/27

Comparing the two frameworks

◮ Biggest mismatch comes down to representation

  • f meta-contexts

◮ Monadic framework: marked stack

MetaCont = [Ident + Cont] [k3, α3, α2, k2, k1, α1]

◮ λ

µ0: dynamic environment MetaCont = [Ident ∗ Cont] [ α3 → k3, α2 → k2, α1 → k1]

slide-40
SLIDE 40

20/27

Dynamic environment to marked stack

◮ Embed λ

µ0 into Monadic Framework

◮ Flatten [Ident ∗ Cont] to [Ident + Cont]? ◮ Easy!

[ α3 → k3, α2 → k2, α1 → k1] = [k3, α3, k2, α2, k1, α1]

slide-41
SLIDE 41

21/27

Marked stack to dynamic environment

◮ Embed Monadic Framework into λ

µ

◮ Restore [Iden + Cont] to [Ident ∗ Cont] ◮ Not so easy. . .

slide-42
SLIDE 42

22/27

Distinguished return

◮ Reserve one dynamic continuation variable (

tp)

◮ “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 → k2,

tp → k1, α1 → k

tp]

slide-43
SLIDE 43

23/27

From practice to theory

◮ Embedding from Monadic Framework to λ

µ0

◮ With multiple prompts, shift0 and reset0 style

control implements control0 and prompt0

◮ Corollary: shift0 and reset0 style control with 2

prompts implements control0 and prompt0

slide-44
SLIDE 44

24/27

Simulation of ∗F− style operators # ≡ Prompt handler for F0, F ≡ Traces of “naked” context composition E ::= | E M | V E | M E[#(E ′[F0V ])] → E[V k] where k = λx.E[x] E[#(E ′[FV ])] → E[#(V k)] where k = λx.E[x]

slide-45
SLIDE 45

25/27

Dynamic traversal

#( 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]

slide-46
SLIDE 46

25/27

Dynamic traversal

#( 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]

slide-47
SLIDE 47

25/27

Dynamic traversal

#( 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]

slide-48
SLIDE 48

25/27

Dynamic traversal

#( 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]

slide-49
SLIDE 49

26/27

Questions?

slide-50
SLIDE 50

27/27

References

  • D. Biernacki, O. Danvy, and K. Millikin. A dynamic

continuation-passing style for dynamic delimited

  • continuations. BRICS, Department of Computer

Science, Univ., 2005.

  • P. Downen and Z. M. Ariola. A systematic approach

to delimited control with multiple prompts. In Procedings of the 21st European Symposium on Programming, pages 234–253. Springer, 2012.

  • R. K. Dybvig, S. P. Jones, and A. Sabry. A monadic

framework for delimited continuations. Journal of Functional Programming, 17(06):687–730, 2007.