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
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
Type-directed semantics 10 / 20
Type-directed semantics untyped model: � e � : D D ∼ = Z + ( D → D ) + { wrong } 10 / 20
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
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
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
� � � � � 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
� � � � � 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
Modelling effects with graded monads (Katsumata 2014) 12 / 20
Modelling effects with graded monads (Katsumata 2014) � Γ ⊢ e : τ, F � : � Γ � → T F � τ � 12 / 20
Modelling effects with graded monads (Katsumata 2014) � Γ ⊢ e : τ, F � : � Γ � → T F � τ � e.g. for state T A = S → ( A × S ). 12 / 20
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
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
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
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
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
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.
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
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
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
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
Semantics of branching: derived vs. parameterised 13 / 20
Semantics of branching: derived vs. parameterised � if e 0 then e 1 else e 2 � = COND( � e 0 � , � e 1 � , � e 2 � ) 13 / 20
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
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
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
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
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
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
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
Definition: jonoid For a set of effects F then ( F , • , I , & , ? + , ⊑ ) is a joinoid control-flow algebra :
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;
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;
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)
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
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:
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 )
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
Graded generalised joinad Definition: graded conditional joinad Given a jonoid ( F , • , I , & , ? + , ⊑ ):
Graded generalised joinad Definition: graded conditional joinad Given a jonoid ( F , • , I , & , ? + , ⊑ ): ◮ graded monad for the pre-ordered monoid ( F , • , I , ⊑ )
Graded generalised joinad Definition: graded conditional joinad Given a jonoid ( F , • , I , & , ? + , ⊑ ): ◮ graded monad for the pre-ordered monoid ( F , • , I , ⊑ ) ◮ additional operations:
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
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
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
Theorem: soundness Given a graded joinadic semantics for the simply-effect-and- typed λ -calculus with if and par then, for all e , e ′ , Γ , τ, F :
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 � ⇒
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:
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
Conclusions 17 / 20
Conclusions ◮ unify Hanne and Flemming’s rich effects with semantics 17 / 20
Conclusions ◮ unify Hanne and Flemming’s rich effects with semantics indexing non-seq. control monads − − − − − → graded monads − − − − − − − − − → graded joinads 17 / 20
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
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
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
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
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
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
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
Backup slides 19 / 20
Modeling effects with monads Model effectful computations via some data type T 20 / 20
Modeling effects with monads Model effectful computations via some data type T � Γ ⊢ e : τ � : � Γ � → T � τ � 20 / 20
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
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
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