delimited control with multiple prompts in theory and
play

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. Delimited Control with Multiple Prompts in Theory and Practice Paul Downen Zena M. Ariola University of Oregon HOPE’14 — August 31, 2014 1/27

  2. Crash course on control 2/27

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

  4. Separating a redex from its evaluation context 1 + 2 + ( 3 × 4 ) 1 + 2 + � 3 × 4 3/27

  5. Separating a redex from its evaluation context 1 + 2 + ( 3 × 4 ) 1 + 2 + � 3 × 4 ⇓ 1 + 2 + � 12 1 + 2 + 12 3/27

  6. Classical control: Abortive continuations 1 + 2 + call / cc ( λ k . 3 × ( k 4 )) 4/27

  7. Classical control: Abortive continuations 1 + 2 + call / cc ( λ k . 3 × ( k 4 )) 1 + 2 + � call / cc ( λ k . 3 × ( k 4 )) 4/27

  8. 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

  9. 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

  10. 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

  11. Delimited control: Composable continuations 1 + #( 2 + F ( λ k . 3 × ( k 4 )) ) 5/27

  12. Delimited control: Composable continuations 1 + #( 2 + F ( λ k . 3 × ( k 4 )) ) 1 + # � 2 + � F ( λ k . 3 × ( k 4 )) 5/27

  13. 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

  14. 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

  15. 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

  16. A zoo of delimited control operators 6/27

  17. Design decisions E [#( E ′ [ F V ])] ◮ Does F remove # surrounding E ′ ? ◮ Does continuation guard its call-site with a # ? 7/27

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  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

  28. 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

  29. Giving prompts a name 13/27

  30. 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

  31. Multiple prompts: Dynamic continuations γ ( 1 + # � α ( 3 + F � β ( λ k . 4 × ( k 5 )) ) )) β ( 2 + # � # � 15/27

  32. Multiple prompts: Dynamic continuations γ ( 1 + # � α ( 3 + F � β ( λ k . 4 × ( k 5 )) ) )) β ( 2 + # � # � β ( λ k . 4 × ( k 5 )) γ ( 1 + # � β � ) F � # � 2 + # � α ( 3 + � ) 15/27

  33. 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

  34. 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

  35. 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

  36. Putting practice to theory 16/27

  37. 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

  38. 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

  39. 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

  40. 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