effect systems revisited
play

Effect systems revisited control-flow algebra and semantics (slides) - PowerPoint PPT Presentation

Effect systems revisited control-flow algebra and semantics (slides) Nielson-Nielson Festschrift 2016 Alan Mycroft 1 Dominic Orchard 1 , 2 Tomas Petricek 1 1 University of Cambridge 2 Imperial College London 1 / 20 Simple set-based effects `


  1. from “Type and effect systems” (Nielson, Nielson, 1999) “Now we turn to explaining the individual steps in the overall methodology of designing and using type and effect systems: – devise a semantics for the programming language, – develop a program analysis in the form of a type and effect system ... – prove the semantic correctness of the analysis, – develop an efficient inference algorithm, ...” ◮ An effect-directed semantics unifies the first three steps 9 / 20

  2. from “Type and effect systems” (Nielson, Nielson, 1999) “Now we turn to explaining the individual steps in the overall methodology of designing and using type and effect systems: – devise a semantics for the programming language, – develop a program analysis in the form of a type and effect system ... – prove the semantic correctness of the analysis, – develop an efficient inference algorithm, ...” ◮ An effect-directed semantics unifies the first three steps ◮ We develop an effect-directed semantics for rich Nielson-Nielson-style effects 9 / 20

  3. Type-directed semantics 10 / 20

  4. Type-directed semantics untyped model: � e � : D D ∼ = Z + ( D → D ) + { wrong } 10 / 20

  5. Type-directed semantics untyped model: � e � : D D ∼ = Z + ( D → D ) + { wrong } simply typed model : � Γ ⊢ e : τ � : D τ D int = Z D σ → τ = D σ → D τ 10 / 20

  6. Type-directed semantics untyped model: � e � : D D ∼ = Z + ( D → D ) + { wrong } simply typed model : � Γ ⊢ e : τ � : D τ D int = Z D σ → τ = D σ → D τ effect-directed model : � Γ ⊢ e : τ, F � : D F τ 10 / 20

  7. Type-directed semantics untyped model: � e � : D D ∼ = Z + ( D → D ) + { wrong } simply typed model : � Γ ⊢ e : τ � : D τ D int = Z D σ → τ = D σ → D τ effect-directed model : � Γ ⊢ e : τ, F � : D F τ Core idea: algebra-semantics homomorphism Algebraic structure of F determines structure on family D F τ . 10 / 20

  8. � � � � � Effect analysis and semantics Lattice effects Richer structure on F (Gifford, Lucassen ’86) (Nielson, Nielson ’94, ’99) Γ ⊢ e : τ, F “Marriage of effects and monads” (Wadler, Thiemann ’03) � Γ ⊢ e : M F τ � : � Γ � → T � τ � Effect-directed semantics This paper (Katsumata ’14) Richer effect-directed � Γ ⊢ e : M F τ � : � Γ � → T F � τ � semantics (graded joinads) (graded monads) 11 / 20

  9. � � � � � Effect analysis and semantics Lattice effects Richer structure on F (Gifford, Lucassen ’86) (Nielson, Nielson ’94, ’99) Γ ⊢ e : τ, F “Marriage of effects and monads” (Wadler, Thiemann ’03) � Γ ⊢ e : M F τ � : � Γ � → T � τ � Effect-directed semantics This paper (Katsumata ’14) Richer effect-directed � Γ ⊢ e : M F τ � : � Γ � → T F � τ � semantics (graded joinads) (graded monads) Operations on T F homomorphic to operations on F 11 / 20

  10. Modelling effects with graded monads (Katsumata 2014) 12 / 20

  11. Modelling effects with graded monads (Katsumata 2014) � Γ ⊢ e : τ, F � : � Γ � → T F � τ � 12 / 20

  12. Modelling effects with graded monads (Katsumata 2014) � Γ ⊢ e : τ, F � : � Γ � → T F � τ � e.g. for state T A = S → ( A × S ). 12 / 20

  13. Modelling effects with graded monads (Katsumata 2014) � Γ ⊢ e : τ, F � : � Γ � → T F � τ � e.g. for state T F A = ( reads ( F )) → ( A × writes ( F )). 12 / 20

  14. Modelling effects with graded monads (Katsumata 2014) � Γ ⊢ e : τ, F � : � Γ � → T F � τ � e.g. for state T F A = ( reads ( F )) → ( A × writes ( F )). e.g. for partiality T A = ⊥ + A 12 / 20

  15. Modelling effects with graded monads (Katsumata 2014) � Γ ⊢ e : τ, F � : � Γ � → T F � τ � e.g. for state T F A = ( reads ( F )) → ( A × writes ( F )). e.g. for partiality T ⊥ A = ⊥ , T ⊤ A = A , 12 / 20

  16. Modelling effects with graded monads (Katsumata 2014) � Γ ⊢ e : τ, F � : � Γ � → T F � τ � e.g. for state T F A = ( reads ( F )) → ( A × writes ( F )). e.g. for partiality T ⊥ A = ⊥ , T ⊤ A = A , T ? A = A + 1

  17. Modelling effects with graded monads (Katsumata 2014) � Γ ⊢ e : τ, F � : � Γ � → T F � τ � e.g. for state T F A = ( reads ( F )) → ( A × writes ( F )). e.g. for partiality T ⊥ A = ⊥ , T ⊤ A = A , T ? A = A + 1 Graded monads provide sequential composition

  18. Modelling effects with graded monads (Katsumata 2014) � Γ ⊢ e : τ, F � : � Γ � → T F � τ � e.g. for state T F A = ( reads ( F )) → ( A × writes ( F )). e.g. for partiality T ⊥ A = ⊥ , T ⊤ A = A , T ? A = A + 1 Graded monads provide sequential composition Let ( F , • , 0) be an effect monoid.

  19. Modelling effects with graded monads (Katsumata 2014) � Γ ⊢ e : τ, F � : � Γ � → T F � τ � e.g. for state T F A = ( reads ( F )) → ( A × writes ( F )). e.g. for partiality T ⊥ A = ⊥ , T ⊤ A = A , T ? A = A + 1 Graded monads provide sequential composition Let ( F , • , 0) be an effect monoid. Given d 1 : A → T F B and d 2 : B → T G C

  20. Modelling effects with graded monads (Katsumata 2014) � Γ ⊢ e : τ, F � : � Γ � → T F � τ � e.g. for state T F A = ( reads ( F )) → ( A × writes ( F )). e.g. for partiality T ⊥ A = ⊥ , T ⊤ A = A , T ? A = A + 1 Graded monads provide sequential composition Let ( F , • , 0) be an effect monoid. Given d 1 : A → T F B and d 2 : B → T G C then d 2 ˆ ◦ d 1 : A → T F • G C

  21. Modelling effects with graded monads (Katsumata 2014) � Γ ⊢ e : τ, F � : � Γ � → T F � τ � e.g. for state T F A = ( reads ( F )) → ( A × writes ( F )). e.g. for partiality T ⊥ A = ⊥ , T ⊤ A = A , T ? A = A + 1 Graded monads provide sequential composition Let ( F , • , 0) be an effect monoid. Given d 1 : A → T F B and d 2 : B → T G C ◦ d 1 : A → T F • G C with ˆ then d 2 ˆ id A : A → T 0 A . 12 / 20

  22. Modelling effects with graded monads (Katsumata 2014) � Γ ⊢ e : τ, F � : � Γ � → T F � τ � e.g. for state T F A = ( reads ( F )) → ( A × writes ( F )). e.g. for partiality T ⊥ A = ⊥ , T ⊤ A = A , T ? A = A + 1 Graded monads provide sequential composition Let ( F , • , 0) be an effect monoid. Given d 1 : A → T F B and d 2 : B → T G C ◦ d 1 : A → T F • G C with ˆ then d 2 ˆ id A : A → T 0 A . With ordering Given partially ordered ( F , • , 0 , ⊑ ) then for all F ⊑ G then coercion: ι F , G , A : T F A → T G A 12 / 20

  23. Semantics of branching: derived vs. parameterised 13 / 20

  24. Semantics of branching: derived vs. parameterised � if e 0 then e 1 else e 2 � = COND( � e 0 � , � e 1 � , � e 2 � ) 13 / 20

  25. Semantics of branching: derived vs. parameterised � if e 0 then e 1 else e 2 � = COND( � e 0 � , � e 1 � , � e 2 � ) ◮ COND definable via cond : B × A × A → A 13 / 20

  26. Semantics of branching: derived vs. parameterised � if e 0 then e 1 else e 2 � = COND( � e 0 � , � e 1 � , � e 2 � ) ◮ COND definable via cond : B × A × A → A cond(true , x , y ) = x cond(false , x , y ) = y 13 / 20

  27. Semantics of branching: derived vs. parameterised � if e 0 then e 1 else e 2 � = COND( � e 0 � , � e 1 � , � e 2 � ) ◮ COND definable via cond : B × A × A → A cond(true , x , y ) = x cond(false , x , y ) = y ◮ Restricts effect branching behaviour: 13 / 20

  28. Semantics of branching: derived vs. parameterised � if e 0 then e 1 else e 2 � = COND( � e 0 � , � e 1 � , � e 2 � ) ◮ COND definable via cond : B × A × A → A cond(true , x , y ) = x cond(false , x , y ) = y ◮ Restricts effect branching behaviour: (if) Γ ⊢ e 0 : bool , F Γ ⊢ e 1 : τ, G Γ ⊢ e 2 : τ, H Γ ⊢ if e 0 then e 1 else e 2 : τ, F • ( G ⊔ H ) 13 / 20

  29. Semantics of branching: derived vs. parameterised � if e 0 then e 1 else e 2 � = COND( � e 0 � , � e 1 � , � e 2 � ) ◮ COND definable via cond : B × A × A → A cond(true , x , y ) = x cond(false , x , y ) = y ◮ Restricts effect branching behaviour: (if) Γ ⊢ e 0 : bool , F Γ ⊢ e 1 : τ, G Γ ⊢ e 2 : τ, H Γ ⊢ if e 0 then e 1 else e 2 : τ, F • ( G ⊔ H ) cond : B × T G ⊔ H A × T G ⊔ H A → T G ⊔ H A 13 / 20

  30. Semantics of branching: derived vs. parameterised � if e 0 then e 1 else e 2 � = COND( � e 0 � , � e 1 � , � e 2 � ) ◮ COND definable via cond : B × A × A → A cond(true , x , y ) = x cond(false , x , y ) = y ◮ Restricts effect branching behaviour: (if) Γ ⊢ e 0 : bool , F Γ ⊢ e 1 : τ, G Γ ⊢ e 2 : τ, H Γ ⊢ if e 0 then e 1 else e 2 : τ, F • ( G ⊔ H ) cond : B × T G ⊔ H A × T G ⊔ H A → T G ⊔ H A ◮ Towards Nielson-Nielson richer effects, but + may not be ⊔ . 13 / 20

  31. Semantics of branching: derived vs. parameterised � if e 0 then e 1 else e 2 � = COND( � e 0 � , � e 1 � , � e 2 � ) ◮ COND definable via cond : B × A × A → A cond(true , x , y ) = x cond(false , x , y ) = y ◮ Restricts effect branching behaviour: (if) Γ ⊢ e 0 : bool , F Γ ⊢ e 1 : τ, G Γ ⊢ e 2 : τ, H Γ ⊢ if e 0 then e 1 else e 2 : τ, F • ( G ⊔ H ) cond : B × T G ⊔ H A × T G ⊔ H A → T G ⊔ H A ◮ Towards Nielson-Nielson richer effects, but + may not be ⊔ . ◮ Instead: parameterise semantics on COND : T F B × T G A × T H A → T ? +( F , G , H ) A 13 / 20

  32. Definition: jonoid For a set of effects F then ( F , • , I , & , ? + , ⊑ ) is a joinoid control-flow algebra :

  33. Definition: jonoid For a set of effects F then ( F , • , I , & , ? + , ⊑ ) is a joinoid control-flow algebra : ◮ ( F , • , I ) is a monoid, representing sequential composition and purity;

  34. Definition: jonoid For a set of effects F then ( F , • , I , & , ? + , ⊑ ) is a joinoid control-flow algebra : ◮ ( F , • , I ) is a monoid, representing sequential composition and purity; ◮ ( F , & , I ) is a commutative monoid, representing parallel composition;

  35. Definition: jonoid For a set of effects F then ( F , • , I , & , ? + , ⊑ ) is a joinoid control-flow algebra : ◮ ( F , • , I ) is a monoid, representing sequential composition and purity; ◮ ( F , & , I ) is a commutative monoid, representing parallel composition; ◮ letting F + G = ? +( I , F , G ) (pure guard)

  36. Definition: jonoid For a set of effects F then ( F , • , I , & , ? + , ⊑ ) is a joinoid control-flow algebra : ◮ ( F , • , I ) is a monoid, representing sequential composition and purity; ◮ ( F , & , I ) is a commutative monoid, representing parallel composition; ◮ letting F + G = ? +( I , F , G ) (pure guard) ( F , +) is a semigroup, representing choice between branches

  37. Definition: jonoid For a set of effects F then ( F , • , I , & , ? + , ⊑ ) is a joinoid control-flow algebra : ◮ ( F , • , I ) is a monoid, representing sequential composition and purity; ◮ ( F , & , I ) is a commutative monoid, representing parallel composition; ◮ letting F + G = ? +( I , F , G ) (pure guard) ( F , +) is a semigroup, representing choice between branches ◮ with right-distributivity axioms:

  38. Definition: jonoid For a set of effects F then ( F , • , I , & , ? + , ⊑ ) is a joinoid control-flow algebra : ◮ ( F , • , I ) is a monoid, representing sequential composition and purity; ◮ ( F , & , I ) is a commutative monoid, representing parallel composition; ◮ letting F + G = ? +( I , F , G ) (pure guard) ( F , +) is a semigroup, representing choice between branches ◮ with right-distributivity axioms: ( F + G ) • H = ( F • H ) + ( G • H ) ( F + G ) & H = ( F & H ) + ( G & H )

  39. Definition: jonoid For a set of effects F then ( F , • , I , & , ? + , ⊑ ) is a joinoid control-flow algebra : ◮ ( F , • , I ) is a monoid, representing sequential composition and purity; ◮ ( F , & , I ) is a commutative monoid, representing parallel composition; ◮ letting F + G = ? +( I , F , G ) (pure guard) ( F , +) is a semigroup, representing choice between branches ◮ with right-distributivity axioms: ( F + G ) • H = ( F • H ) + ( G • H ) ( F + G ) & H = ( F & H ) + ( G & H ) ◮ all operations are monotonic with respect to ⊑ . 14 / 20

  40. Graded generalised joinad Definition: graded conditional joinad Given a jonoid ( F , • , I , & , ? + , ⊑ ):

  41. Graded generalised joinad Definition: graded conditional joinad Given a jonoid ( F , • , I , & , ? + , ⊑ ): ◮ graded monad for the pre-ordered monoid ( F , • , I , ⊑ )

  42. Graded generalised joinad Definition: graded conditional joinad Given a jonoid ( F , • , I , & , ? + , ⊑ ): ◮ graded monad for the pre-ordered monoid ( F , • , I , ⊑ ) ◮ additional operations:

  43. Graded generalised joinad Definition: graded conditional joinad Given a jonoid ( F , • , I , & , ? + , ⊑ ): ◮ graded monad for the pre-ordered monoid ( F , • , I , ⊑ ) ◮ additional operations: cond F , G , H , A :T F B × T G A × T H A → T ? +( F , G , H ) A par F , G , A :T F A × T G A → T F & G A

  44. Graded generalised joinad Definition: graded conditional joinad Given a jonoid ( F , • , I , & , ? + , ⊑ ): ◮ graded monad for the pre-ordered monoid ( F , • , I , ⊑ ) ◮ additional operations: cond F , G , H , A :T F B × T G A × T H A → T ? +( F , G , H ) A par F , G , A :T F A × T G A → T F & G A ◮ Satisfy jonoid axioms (modulo lifting to functors) 15 / 20

  45. Graded generalised joinad Definition: graded conditional joinad Given a jonoid ( F , • , I , & , ? + , ⊑ ): ◮ graded monad for the pre-ordered monoid ( F , • , I , ⊑ ) ◮ additional operations: cond F , G , H , A :T F B × T G A × T H A → T ? +( F , G , H ) A par F , G , A :T F A × T G A → T F & G A ◮ Satisfy jonoid axioms (modulo lifting to functors) where ? + = cond and & = par Monoids are to (graded) monads as jonoids are to (graded) joinads 15 / 20

  46. Theorem: soundness Given a graded joinadic semantics for the simply-effect-and- typed λ -calculus with if and par then, for all e , e ′ , Γ , τ, F :

  47. Theorem: soundness Given a graded joinadic semantics for the simply-effect-and- typed λ -calculus with if and par then, for all e , e ′ , Γ , τ, F : Γ ⊢ e ≡ e ′ : τ, F � Γ ⊢ e : τ, F � = � Γ ⊢ e ′ : τ, F � ⇒

  48. Theorem: soundness Given a graded joinadic semantics for the simply-effect-and- typed λ -calculus with if and par then, for all e , e ′ , Γ , τ, F : Γ ⊢ e ≡ e ′ : τ, F � Γ ⊢ e : τ, F � = � Γ ⊢ e ′ : τ, F � ⇒ wrt. CBV β - ≡ with additional equations:

  49. Theorem: soundness Given a graded joinadic semantics for the simply-effect-and- typed λ -calculus with if and par then, for all e , e ′ , Γ , τ, F : Γ ⊢ e ≡ e ′ : τ, F � Γ ⊢ e : τ, F � = � Γ ⊢ e ′ : τ, F � ⇒ wrt. CBV β - ≡ with additional equations: if true then e else x ≡ e (if β 1’) if false then x else e ′ ≡ e ′ (if β 2’) (if b then e else e ′ ) par e ′′ (if-dist-par) ≡ if b then ( e par e ′′ ) else ( e ′ par e ′′ ) let x = (if e then e ′ else e ′′ ) in e ′′′ (if-dist-seq) ≡ if e then (let x = e ′ in e ′′′ ) else (let x = e ′′ in e ′′′ ) x par e ≡ ( x , e ) (par-pure) e par e ′ ≡ swap ( e ′ par e ) (par-sym) e par ( e ′ par e ′′ ) ≡ assoc (( e par e ′ ) par e ′′ ) (par-assoc) 16 / 20

  50. Conclusions 17 / 20

  51. Conclusions ◮ unify Hanne and Flemming’s rich effects with semantics 17 / 20

  52. Conclusions ◮ unify Hanne and Flemming’s rich effects with semantics indexing non-seq. control monads − − − − − → graded monads − − − − − − − − − → graded joinads 17 / 20

  53. Conclusions ◮ unify Hanne and Flemming’s rich effects with semantics indexing non-seq. control monads − − − − − → graded monads − − − − − − − − − → graded joinads ◮ Semantics for various kinds of parallel, concurrent (e.g. music) and speculative behaviour (e.g. prefetching) 17 / 20

  54. Conclusions ◮ unify Hanne and Flemming’s rich effects with semantics indexing non-seq. control monads − − − − − → graded monads − − − − − − − − − → graded joinads ◮ Semantics for various kinds of parallel, concurrent (e.g. music) and speculative behaviour (e.g. prefetching) ◮ Considerable simplification to proofs 17 / 20

  55. Conclusions ◮ unify Hanne and Flemming’s rich effects with semantics indexing non-seq. control monads − − − − − → graded monads − − − − − − − − − → graded joinads ◮ Semantics for various kinds of parallel, concurrent (e.g. music) and speculative behaviour (e.g. prefetching) ◮ Considerable simplification to proofs ◮ Represent effect-dependent optimisations more easily 17 / 20

  56. Conclusions ◮ unify Hanne and Flemming’s rich effects with semantics indexing non-seq. control monads − − − − − → graded monads − − − − − − − − − → graded joinads ◮ Semantics for various kinds of parallel, concurrent (e.g. music) and speculative behaviour (e.g. prefetching) ◮ Considerable simplification to proofs ◮ Represent effect-dependent optimisations more easily ◮ Effect-directed semantics provides co-design approach 17 / 20

  57. Conclusions ◮ unify Hanne and Flemming’s rich effects with semantics indexing non-seq. control monads − − − − − → graded monads − − − − − − − − − → graded joinads ◮ Semantics for various kinds of parallel, concurrent (e.g. music) and speculative behaviour (e.g. prefetching) ◮ Considerable simplification to proofs ◮ Represent effect-dependent optimisations more easily ◮ Effect-directed semantics provides co-design approach ◮ Equations of analysis carry over to semantics, and vice versa 17 / 20

  58. Conclusions ◮ unify Hanne and Flemming’s rich effects with semantics indexing non-seq. control monads − − − − − → graded monads − − − − − − − − − → graded joinads ◮ Semantics for various kinds of parallel, concurrent (e.g. music) and speculative behaviour (e.g. prefetching) ◮ Considerable simplification to proofs ◮ Represent effect-dependent optimisations more easily ◮ Effect-directed semantics provides co-design approach ◮ Equations of analysis carry over to semantics, and vice versa ◮ Exposes which structure is needed in each direction 17 / 20

  59. Thanks Hanne and Flemming for the inspiration. Happy Birthday! Thanks to Sam Aaron (Cambridge) for the Sonic Pi language used for the intro program 18 / 20

  60. Backup slides 19 / 20

  61. Modeling effects with monads Model effectful computations via some data type T 20 / 20

  62. Modeling effects with monads Model effectful computations via some data type T � Γ ⊢ e : τ � : � Γ � → T � τ � 20 / 20

  63. Modeling effects with monads Model effectful computations via some data type T � Γ ⊢ e : τ � : � Γ � → T � τ � e.g. for state T A = S → ( A × S ). 20 / 20

  64. Modeling effects with monads Model effectful computations via some data type T � Γ ⊢ e : τ � : � Γ � → T � τ � e.g. for state T A = S → ( A × S ). e.g. for partiality T A = ⊥ + A 20 / 20

  65. Modeling effects with monads Model effectful computations via some data type T � Γ ⊢ e : τ � : � Γ � → T � τ � e.g. for state T A = S → ( A × S ). e.g. for partiality T A = ⊥ + A Monads provide sequential composition Given f : A → T B and g : B → T C then

Recommend


More recommend