on the expressive power of user defined effects effect
play

On the expressive power of user-defined effects: effect handlers, - PowerPoint PPT Presentation

On the expressive power of user-defined effects: effect handlers, monadic reflection, and delimited control Work in Progress Ohad Kammar <ohad.kammar@cs.ox.ac.uk> joint work with Yannick Forster, Sam Lindley, and Matija Pretnar The 4 th


  1. On the expressive power of user-defined effects: effect handlers, monadic reflection, and delimited control Work in Progress Ohad Kammar <ohad.kammar@cs.ox.ac.uk> joint work with Yannick Forster, Sam Lindley, and Matija Pretnar The 4 th Southern REPLS Imperial College 27 September 2016 N I V E U R E S I H T T Y O H F G R E U D I N B Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  2. Effect oriented programming Native effects ◮ I/O. ◮ Mutable state. ◮ Randomness and non-determinism. User-defined effects ◮ Parsing. ◮ Constraint solving. ◮ Proof-search tactics. ◮ Redefine existing effects? Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  3. A brief history of functional programming and effects A love-hate story Effects are harmful . . . ◮ Disallow useful compiler optimisations. ◮ Break referential transparency. ◮ Depart from the λ -calculus ( βη -equality, confluence). But are useful! A rift and a bridge ML and Scheme vs. Haskell. Monads [Moggi’89, Wadler’91], now in Haskell, ML, Scheme, F ∗ , C++ . . . Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  4. Algebraic effects Monad issues ◮ No interface for effects. ◮ Compositionality and modularity issues. ◮ Steep learning curve. Plotkin-Power-Pretnar-Bauer ◮ Add effect operations to Moggi’s theory [Plotkin-Power’02,’03]. ◮ Add exception handlers, and more generally, effect handlers [Plotkin-Pretnar’09]. ◮ Programming with algebraic effects and handlers [Bauer-Pretnar’15]. Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  5. Delimited control Goto on steroids Effect handlers are a new kind of delimited control effect. But(!): ◮ Clean denotational semantics. [Plotkin-Pretnar’09] ◮ Clean program logic. [Pretnar’s thesis] ◮ Clean meta-theory: strong normalisation and type-and-effect systems [K-Lindley-Oury’13], unrestricted polymorphism [K-Pretnar’16], . . . Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  6. Basic research question Monads, handlers, and delimited control ◮ How do these abstractions compare? ◮ How to compare these abstractions? Macro expressibility [Fellisen’90] ◮ Expressive power of Turing-complete languages. ◮ Computability and complexity reductions are too crude. ◮ Macro translations: ◮ Keep shared fragment identical. ◮ Compositional a.k.a. local a.k.a. homomorphic. Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  7. Contribution: bird’s eye MAM Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  8. Contribution: bird’s eye EFF MAM DEL MON Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  9. Contribution: bird’s eye EFF = = MAM = DEL MON Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  10. Contribution: bird’s eye EFF DEL MAM MON EFF ⊢ DEL ⊢ MAM ⊢ MON ⊢ Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  11. Contribution: bird’s eye EFF DEL MAM MON = = = � ∃ EFF ⊢ DEL ⊢ MAM ⊢ � ∃ � ∃ MON ⊢ Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  12. Contribution: bird’s eye EFF DEL MAM MON = = = � ∃ EFF ⊢ DEL ⊢ MAM ⊢ � ∃ � ∃ MON ⊢ Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  13. Contribution: bird’s eye EFF DEL MAM MON = = EFF ⊢ DEL ⊢ MAM ⊢ � ∃ MON ⊢ Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  14. Contribution: bird’s eye EFF DEL MAM MON EFF + ⊢ DEL + MAM + ⊢ ⊢ MON + ⊢ EFF ⊢ DEL ⊢ MAM ⊢ MON ⊢ Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  15. Contribution: bird’s eye EFF DEL MAM MON EFF + ⊢ = DEL + MAM + = ⊢ ⊢ = MON + ⊢ EFF ⊢ DEL ⊢ MAM ⊢ MON ⊢ Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  16. Talk structure ◮ Short tutorial on algebraic effects, monadic reflection, and delimited control Rationale: Most fiddly task was simplifying and unifying calculi. ◮ Time pertmitting: discuss the negative result. Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  17. The lambda calculus with effects ( mam ) Syntax V , W ::= values x variable | unit value () | ( V 1 , V 2 ) pairing | inj i V variant constructor | { M } thunk M , N ::= computations split ( V , x 1 . x 2 . M ) pattern matching: product | case 0 ( V ) void | case ( V , inj 1 x 1 . M 1 variants , inj 2 x 2 . M 2 ) | V ! force | return V returner | let x ← M in N sequencing (monadic bind) | λ x . M function abstraction | function application M V | � M 1 , M 2 � computation pair | projection prj i M Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  18. The lambda calculus with effects ( mam ) Syntax (CBPV) V , W ::= values variable x | () unit value | ( V 1 , V 2 ) pairing | inj i V variant constructor | { M } thunk M , N ::= computations split ( V , x 1 . x 2 . M ) pattern matching: product | case 0 ( V ) void | case ( V , inj 1 x 1 . M 1 variants , inj 2 x 2 . M 2 ) | V ! force | returner return V | let x ← M in N sequencing (monadic bind) | λ x . M function abstraction | M V function application | � M 1 , M 2 � computation pair | prj i M projection Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  19. The lambda calculus with effects ( mam ) Operational semantics Reduction frames and contexts B ::= let x ← [ ] in N | [ ] V | prj i [ ] basic frames F ::= B computation frames C ::= evaluation context [ ] hole C [ F [ ]] layered frame M � β M ′ Beta reduction ( β. × ) split ( ( V 1 , V 2 ) , x 1 . x 2 . M ) � β M [ V 1 / x 1 , V 2 / x 2 ] ( β. +) case ( inj i V , inj 1 x 1 . M 1 , inj 2 x 2 . M 2 ) � β M i [ V / x i ] { M } ! � β M ( β. U ) ( β. F ) let x ← return V in M � β M [ V / x ] ( β. → ) ( λ x . M ) V � β M [ V / x ] ( β. &) prj i � M 1 , M 2 � � β M i M � β M ′ M � M ′ Reduction C [ M ] � C [ M ′ ] Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  20. The lambda calculus with effects ( mam ) Types E ::= effects ∅ pure effect ::= kinds K | Eff effects values Val | Comp E E -computations | environments Ctxt A , B ::= value types α type variable | 1 unit | A 1 × A 2 value products | 0 empty | A 1 + A 2 variant | U E C thunks C , D ::= computation types FA returners | A → C functions | C 1 & C 2 computation products Θ ::= α 1 , . . . , α n type environments Γ , ∆ ::= x 1 : A 1 , . . . , x n : A n environments Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  21. The lambda calculus with effects ( mam ) Denotational semantics Standard, using sets and functions. Using Hermida’s [’93] lifting: Theorem (adequacy) Denotational equivalence implies contextural equivalence: for all Θ; Γ ⊢ E P , Q : X, if ⟦ P ⟧ = ⟦ Q ⟧ then P ≃ Q. Corollary (soundness and strong normalisation) All well-typed closed ground returners reduce to a normal form: for all ; ⊢ ∅ M : FG there exists some ; ⊢ V : G such that ⟦ return V ⟧ = ⟦ M ⟧ and M � ⋆ return V Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  22. Effect handlers Syntax [K-Lindley-Oury’13] M , N ::= . . . computations | op V operation call | handle M with H handling construct H ::= handlers { return x �→ M } return clause | H ⊎ { op p k �→ N } operation clause (where op does not occur in H ) Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  23. Effect handlers Operational semantics Reduction frames and contexts · · · F ::= . . . computation frame | handle [ ] with H H ::= [ ] | H [ B [ ]] hoisting context Beta reduction · · · ( handle . F ) handle ( return V ) with H � β M [ V / x ] where H return = λ x . M ( handle . op ) handle H [ op V ] with H � β N [ V / p , { λ x . handle H [ return x ] with H } / k ] where H op = λ p k . N and x / ∈ FV ( H , H ) Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

  24. Effect handlers Types E ::= . . . effects | { op : A → B } ⊎ E arity assignment ::= . . . kinds K | Hndlr handlers ::= A E ⇒ E ′ C R handler types · · · Computation typing Θ; Γ ⊢ E M : C (Θ ⊢ k Γ : Ctxt , E : Eff , C : Comp E ) Θ; Γ ⊢ H : A E ⇒ E ′ C ( op : A → B ) ∈ E Θ; Γ ⊢ V : A Θ; Γ ⊢ E M : FA · · · Θ; Γ ⊢ E op V : FB Θ; Γ ⊢ E ′ handle M with H : C Handler typing Θ; Γ ⊢ H : R (Θ ⊢ k Γ : Ctxt , R : Hndlr ) E = { op i : A i → B i } i H = { return x �→ M } ⊎ { op i p k �→ N i } i [Θ; Γ , p : A i , k : U E ′ ( B i → C ) ⊢ E ′ N i : C ] i Θ; Γ , x : A ⊢ E ′ M : C Θ; Γ ⊢ H : A E ⇒ E ′ C Ohad Kammar <ohad.kammar@cs.ox.ac.uk> On the expressive power of user-defined effects

Recommend


More recommend