Delimited Control with Multiple Prompts in Theory and Practice Paul Downen Zena M. Ariola University of Oregon HOPE’14 — August 31, 2014 1/27
Crash course on control 2/27
Separating a redex from its evaluation context 1 + 2 + ( 3 × 4 ) 3/27
Separating a redex from its evaluation context 1 + 2 + ( 3 × 4 ) 1 + 2 + � 3 × 4 3/27
Separating a redex from its evaluation context 1 + 2 + ( 3 × 4 ) 1 + 2 + � 3 × 4 ⇓ 1 + 2 + � 12 1 + 2 + 12 3/27
Classical control: Abortive continuations 1 + 2 + call / cc ( λ k . 3 × ( k 4 )) 4/27
Classical control: Abortive continuations 1 + 2 + call / cc ( λ k . 3 × ( k 4 )) 1 + 2 + � call / cc ( λ k . 3 × ( k 4 )) 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 ) 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 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 4/27
Delimited control: Composable continuations 1 + #( 2 + F ( λ k . 3 × ( k 4 )) ) 5/27
Delimited control: Composable continuations 1 + #( 2 + F ( λ k . 3 × ( k 4 )) ) 1 + # � 2 + � F ( λ k . 3 × ( k 4 )) 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 ) ) 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 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 ) ) 5/27
A zoo of delimited control operators 6/27
Design decisions E [#( E ′ [ F V ])] ◮ Does F remove # surrounding E ′ ? ◮ Does continuation guard its call-site with a # ? 7/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 ] 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 ] 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 ] 8/27
A family of operators ◮ + F + : shift ( S ) and reset ( � � ) of Danvy and Filinski ◮ + F− : control ( F ) and prompt ( # ) of Felleisen ◮ −F + : shift 0 ( S 0 ) and reset 0 ( � � 0 ) ◮ −F− : control 0 ( F 0 ) and prompt 0 ( # 0 ) 9/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? 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 10/27
shift = shift 0 ? Continuation swap two ways swap x = S ( λ k 1 . S ( λ k 2 . k 1 ( k 2 x ))) swap 0 x = S 0 ( λ k 1 . S 0 ( λ k 2 . k 1 ( k 2 x ))) What’s the difference? 11/27
shift � = shift 0 Continuation swap two ways swap x = S ( λ k 1 . S ( λ k 2 . k 1 ( k 2 x ))) swap 0 x = S 0 ( λ k 1 . S 0 ( λ k 2 . k 1 ( k 2 x ))) What’s the difference? � 10 + � 2 × ( swap 1 ) �� �→ ∗ � 10 + � k 1 ( k 2 1 ) �� �→ ∗ 12 where k 1 x = � 2 × x � k 2 x = � x � identity function � 10 + � 2 × ( swap 0 1 ) � 0 � 0 �→ ∗ k 1 ( k 2 1 ) �→ ∗ 22 where k 1 x = � 2 × x � 0 k 2 x = � 10 + x � 0 Context switch 11/27
Theory vs. Practice Theory ◮ Focus on ∗F + operators ◮ shift and reset are heavily studied ◮ shift 0 and reset 0 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
Theory vs. Practice Practice ◮ Focus on ∗F− operators ◮ Major implementations of delimited control ◮ Racket: control and prompt ◮ Haskell library CC-delcont: control 0 and prompt 0 ◮ OCaml library delcontcc: control 0 and prompt 0 ◮ Practical extensions of delimited control ◮ Integrated into languages with other effects ◮ Multiple prompts 12/27
Theory vs. Practice Practice ◮ Focus on ∗F− operators ◮ Major implementations of delimited control ◮ Racket: control and prompt ◮ Haskell library CC-delcont: control 0 and prompt 0 ◮ OCaml library delcontcc: control 0 and prompt 0 ◮ Practical extensions of delimited control ◮ Integrated into languages with other effects ◮ Multiple prompts 12/27
Giving prompts a name 13/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 � α 14/27
Multiple prompts: Dynamic continuations γ ( 1 + # � α ( 3 + F � β ( λ k . 4 × ( k 5 )) ) )) β ( 2 + # � # � 15/27
Multiple prompts: Dynamic continuations γ ( 1 + # � α ( 3 + F � β ( λ k . 4 × ( k 5 )) ) )) β ( 2 + # � # � β ( λ k . 4 × ( k 5 )) γ ( 1 + # � β � ) F � # � 2 + # � α ( 3 + � ) 15/27
Multiple prompts: Dynamic continuations γ ( 1 + # � α ( 3 + F � β ( λ k . 4 × ( k 5 )) ) )) β ( 2 + # � # � β ( λ k . 4 × ( k 5 )) γ ( 1 + # � β � ) F � # � k : 2 + # � α ( 3 + � ) ⇓ γ ( 1 + # � β � ) # � 4 × ( k 5 ) � γ ( 1 + # � # � β ( 4 × ( k 5 ) )) 15/27
Multiple prompts: Dynamic continuations γ ( 1 + # � α ( 3 + F � β ( λ k . 4 × ( k 5 )) ) )) β ( 2 + # � # � β ( λ k . 4 × ( k 5 )) γ ( 1 + # � β � ) F � # � k : 2 + # � α ( 3 + � ) ⇓ γ ( 1 + # � β � ) # � 4 × ( k 5 ) � γ ( 1 + # � # � β ( 4 × ( k 5 ) )) γ ( 1 + # � β � ) # � 4 × � k 5 15/27
Multiple prompts: Dynamic continuations γ ( 1 + # � α ( 3 + F � β ( λ k . 4 × ( k 5 )) ) )) β ( 2 + # � # � β ( λ k . 4 × ( k 5 )) γ ( 1 + # � β � ) F � # � k : 2 + # � α ( 3 + � ) ⇓ γ ( 1 + # � β � ) # � 4 × ( k 5 ) � γ ( 1 + # � # � β ( 4 × ( k 5 ) )) γ ( 1 + # � β � ) # � 4 × � k 5 ⇓ γ ( 1 + # � β � ) # � 2 + # � α ( 3 + 5 ) 4 × � � # � γ ( 1 + # β ( 4 × ( 2 + # � α ( 3 + 5 )))) 15/27
Putting practice to theory 16/27
Multiple prompts via marked stacks ◮ Monadic Framework for Delimited Continuations (Dybvig et al., 2007) ◮ control 0 and prompt 0 style control with multiple prompts ◮ Use hybrid abstract/concrete continuation monad ◮ Stack of ordinary continuations ◮ Special markers representing prompts 17/27
Multiple prompts via dynamic binding ◮ Systematic Approach to Delimited Control with Multiple Prompts (Downen and Ariola, 2012) ◮ shift 0 and reset 0 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 18/27
Comparing the two frameworks ◮ Biggest mismatch comes down to representation of meta-contexts ◮ Monadic framework: marked stack MetaCont = [ Ident + Cont ] [ k 3 , � α 3 , � α 2 , k 2 , k 1 , � α 1 ] ◮ λ � µ 0 : dynamic environment MetaCont = [ Ident ∗ Cont ] [ � α 3 �→ k 3 , � α 2 �→ k 2 , � α 1 �→ k 1 ] 19/27
Dynamic environment to marked stack ◮ Embed λ � µ 0 into Monadic Framework ◮ Flatten [ Ident ∗ Cont ] to [ Ident + Cont ] ? ◮ Easy! [ � α 3 �→ k 3 , � α 2 �→ k 2 , � α 1 �→ k 1 ] = [ k 3 , � α 3 , k 2 , � α 2 , k 1 , � α 1 ] 20/27
Recommend
More recommend