SLIDE 1 Effect systems revisited – control-flow algebra and semantics (slides)
Nielson-Nielson Festschrift 2016 Alan Mycroft1 Dominic Orchard1,2 Tomas Petricek1
1University of Cambridge 2Imperial College London 1 / 20
SLIDE 2 Simple set-based effects `
a la (Gifford, Lucassen 1986)
Γ ⊢ e : τ, Φ
2 / 20
SLIDE 3 Simple set-based effects `
a la (Gifford, Lucassen 1986)
Γ ⊢ e : τ, Φ
(play) Γ ⊢ play(N, L) : void, {N}
2 / 20
SLIDE 4 Simple set-based effects `
a la (Gifford, Lucassen 1986)
Γ ⊢ e : τ, Φ
(play) Γ ⊢ play(N, L) : void, {N} (if) Γ ⊢ e0 : bool, Φ0
Γ ⊢ e1 : τ, Φ1 Γ ⊢ e2 : τ, Φ2 Γ ⊢ if e0 then e1 else e2 : τ, Φ0 ∪ Φ1 ∪ Φ2
2 / 20
SLIDE 5 Simple set-based effects `
a la (Gifford, Lucassen 1986)
Γ ⊢ e : τ, Φ
(play) Γ ⊢ play(N, L) : void, {N} (if) Γ ⊢ e0 : bool, Φ0
Γ ⊢ e1 : τ, Φ1 Γ ⊢ e2 : τ, Φ2 Γ ⊢ if e0 then e1 else e2 : τ, Φ0 ∪ Φ1 ∪ Φ2
(seq) Γ ⊢ e1 : τ1, Φ1
Γ ⊢ e2 : τ2, Φ2 Γ ⊢ e1; e2 : τ2, Φ1 ∪ Φ2
2 / 20
SLIDE 6 Simple set-based effects `
a la (Gifford, Lucassen 1986)
Γ ⊢ e : τ, Φ
(play) Γ ⊢ play(N, L) : void, {N} (if) Γ ⊢ e0 : bool, Φ0
Γ ⊢ e1 : τ, Φ1 Γ ⊢ e2 : τ, Φ2 Γ ⊢ if e0 then e1 else e2 : τ, Φ0 ∪ Φ1 ∪ Φ2
(seq) Γ ⊢ e1 : τ1, Φ1
Γ ⊢ e2 : τ2, Φ2 Γ ⊢ e1; e2 : τ2, Φ1 ∪ Φ2
(for)
Γ ⊢ e : void, Φ Γ ⊢ for i = n1 to n2 do e : void, Φ
2 / 20
SLIDE 7 Simple set-based effects `
a la (Gifford, Lucassen 1986)
Γ ⊢ e : τ, Φ
(play) Γ ⊢ play(N, L) : void, {N} (if) Γ ⊢ e0 : bool, Φ0
Γ ⊢ e1 : τ, Φ1 Γ ⊢ e2 : τ, Φ2 Γ ⊢ if e0 then e1 else e2 : τ, Φ0 ∪ Φ1 ∪ Φ2
(seq) Γ ⊢ e1 : τ1, Φ1
Γ ⊢ e2 : τ2, Φ2 Γ ⊢ e1; e2 : τ2, Φ1 ∪ Φ2
(for)
Γ ⊢ e : void, Φ Γ ⊢ for i = n1 to n2 do e : void, Φ for our example we get {G3, F3, A3, B3, C4, D4, E4, G4}
2 / 20
SLIDE 8 ...in POPL’94, richer effect systems for concurrency
3 / 20
SLIDE 9 ...in POPL’94, richer effect systems for concurrency
4 / 20
SLIDE 10 ...in POPL’94, richer effect systems for concurrency
◮ Causality of operations (from sets to lists of effects)
4 / 20
SLIDE 11 ...in POPL’94, richer effect systems for concurrency
◮ Causality of operations (from sets to lists of effects)
(seq) Γ ⊢ e1 : τ1, Φ1
Γ ⊢ e2 : τ2, Φ2 Γ ⊢ e1; e2 : τ2, Φ1; Φ2
4 / 20
SLIDE 12 ...in POPL’94, richer effect systems for concurrency
◮ Causality of operations (from sets to lists of effects)
(seq) Γ ⊢ e1 : τ1, Φ1
Γ ⊢ e2 : τ2, Φ2 Γ ⊢ e1; e2 : τ2, Φ1; Φ2
◮ Branching control-flow (from lists to trees)
4 / 20
SLIDE 13 ...in POPL’94, richer effect systems for concurrency
◮ Causality of operations (from sets to lists of effects)
(seq) Γ ⊢ e1 : τ1, Φ1
Γ ⊢ e2 : τ2, Φ2 Γ ⊢ e1; e2 : τ2, Φ1; Φ2
◮ Branching control-flow (from lists to trees)
(if) Γ ⊢ e0 : bool, Φ0
Γ ⊢ e1 : τ, Φ1 Γ ⊢ e2 : τ, Φ2 Γ ⊢ if e0 then e1 else e2 : τ, Φ0; (Φ1 + Φ2)
4 / 20
SLIDE 14 ...in POPL’94, richer effect systems for concurrency
◮ Causality of operations (from sets to lists of effects)
(seq) Γ ⊢ e1 : τ1, Φ1
Γ ⊢ e2 : τ2, Φ2 Γ ⊢ e1; e2 : τ2, Φ1; Φ2
◮ Branching control-flow (from lists to trees)
(if) Γ ⊢ e0 : bool, Φ0
Γ ⊢ e1 : τ, Φ1 Γ ⊢ e2 : τ, Φ2 Γ ⊢ if e0 then e1 else e2 : τ, Φ0; (Φ1 + Φ2)
◮ Parallel control-flow
4 / 20
SLIDE 15 ...in POPL’94, richer effect systems for concurrency
◮ Causality of operations (from sets to lists of effects)
(seq) Γ ⊢ e1 : τ1, Φ1
Γ ⊢ e2 : τ2, Φ2 Γ ⊢ e1; e2 : τ2, Φ1; Φ2
◮ Branching control-flow (from lists to trees)
(if) Γ ⊢ e0 : bool, Φ0
Γ ⊢ e1 : τ, Φ1 Γ ⊢ e2 : τ, Φ2 Γ ⊢ if e0 then e1 else e2 : τ, Φ0; (Φ1 + Φ2)
◮ Parallel control-flow
(fork)
Γ ⊢ e : unit
Φ0
− → τ, ǫ Γ ⊢ fork e : τ, FORK Φ0
4 / 20
SLIDE 16 ...in 1999, rich general effect-system structure
5 / 20
SLIDE 17 ...in 1999, rich general effect-system structure
6 / 20
SLIDE 18 ...in 1999, rich general effect-system structure
7 / 20
SLIDE 19 ...in 1999, rich general effect-system structure
(Ordered) Semiring of effects (F, •, 1, +, ⊑)
7 / 20
SLIDE 20 ...in 1999, rich general effect-system structure
(Ordered) Semiring of effects (F, •, 1, +, ⊑)
◮ (F, •, 1) for sequential composition (monoid)
7 / 20
SLIDE 21 ...in 1999, rich general effect-system structure
(Ordered) Semiring of effects (F, •, 1, +, ⊑)
◮ (F, •, 1) for sequential composition (monoid) ◮ (F, +) for alternation (semigroup)
7 / 20
SLIDE 22 ...in 1999, rich general effect-system structure
(Ordered) Semiring of effects (F, •, 1, +, ⊑)
◮ (F, •, 1) for sequential composition (monoid) ◮ (F, +) for alternation (semigroup) ◮ ⊑ for subeffecting
7 / 20
SLIDE 23 ...in 1999, rich general effect-system structure
(Ordered) Semiring of effects (F, •, 1, +, ⊑)
◮ (F, •, 1) for sequential composition (monoid) ◮ (F, +) for alternation (semigroup) ◮ ⊑ for subeffecting ◮ ... with fixed-point recβ.Φ
7 / 20
SLIDE 24 ...in 1999, rich general effect-system structure
(Ordered) Semiring of effects (F, •, 1, +, ⊑)
◮ (F, •, 1) for sequential composition (monoid) ◮ (F, +) for alternation (semigroup) ◮ ⊑ for subeffecting ◮ ... with fixed-point recβ.Φ ◮ ... subeffecting axioms provide semiring equations, e.g.
(Φ1 + Φ2); Φ3 ⊑ (Φ1; Φ3) + (Φ1; Φ2) (Φ1; Φ3) + (Φ1; Φ2) ⊑ (Φ1 + Φ2); Φ3
7 / 20
SLIDE 25 For our musical example...
Two example instances:
8 / 20
SLIDE 26 For our musical example...
Two example instances:
◮ Song structure via term-algebra:
Φ = C, D, E, . . . , rest | Φ1 + Φ2 | Φ1 • Φ2 | Φ∗
8 / 20
SLIDE 27 For our musical example...
Two example instances:
◮ Song structure via term-algebra:
Φ = C, D, E, . . . , rest | Φ1 + Φ2 | Φ1 • Φ2 | Φ∗ with Γ ⊢ play(N, L) : void, N
8 / 20
SLIDE 28 For our musical example...
Two example instances:
◮ Song structure via term-algebra:
Φ = C, D, E, . . . , rest | Φ1 + Φ2 | Φ1 • Φ2 | Φ∗ with Γ ⊢ play(N, L) : void, N gives (G3•G3•(G4•E4•C4•B3•A3+A3•G3•(D4•C4+C4•B3)))∗
8 / 20
SLIDE 29 For our musical example...
Two example instances:
◮ Song structure via term-algebra:
Φ = C, D, E, . . . , rest | Φ1 + Φ2 | Φ1 • Φ2 | Φ∗ with Γ ⊢ play(N, L) : void, N gives (G3•G3•(G4•E4•C4•B3•A3+A3•G3•(D4•C4+C4•B3)))∗
◮ or, timing via (R+, +, 0, max, ≤)
8 / 20
SLIDE 30 For our musical example...
Two example instances:
◮ Song structure via term-algebra:
Φ = C, D, E, . . . , rest | Φ1 + Φ2 | Φ1 • Φ2 | Φ∗ with Γ ⊢ play(N, L) : void, N gives (G3•G3•(G4•E4•C4•B3•A3+A3•G3•(D4•C4+C4•B3)))∗
◮ or, timing via (R+, +, 0, max, ≤)
with Γ ⊢ play(N, L) : void, L
8 / 20
SLIDE 31 For our musical example...
Two example instances:
◮ Song structure via term-algebra:
Φ = C, D, E, . . . , rest | Φ1 + Φ2 | Φ1 • Φ2 | Φ∗ with Γ ⊢ play(N, L) : void, N gives (G3•G3•(G4•E4•C4•B3•A3+A3•G3•(D4•C4+C4•B3)))∗
◮ or, timing via (R+, +, 0, max, ≤)
with Γ ⊢ play(N, L) : void, L gives 13 seconds
8 / 20
SLIDE 32 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, ...”
9 / 20
SLIDE 33 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, ...”
9 / 20
SLIDE 34 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, ...”
9 / 20
SLIDE 35 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, ...”
9 / 20
SLIDE 36 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
SLIDE 37 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
SLIDE 38 Type-directed semantics
10 / 20
SLIDE 39 Type-directed semantics
untyped model: e : D D ∼ = Z + (D → D) + {wrong}
10 / 20
SLIDE 40 Type-directed semantics
untyped model: e : D D ∼ = Z + (D → D) + {wrong} simply typed model: Γ ⊢ e : τ : Dτ Dint = Z Dσ→τ = Dσ → Dτ
10 / 20
SLIDE 41 Type-directed semantics
untyped model: e : D D ∼ = Z + (D → D) + {wrong} simply typed model: Γ ⊢ e : τ : Dτ Dint = Z Dσ→τ = Dσ → Dτ effect-directed model: Γ ⊢ e : τ, F : DF
τ
10 / 20
SLIDE 42 Type-directed semantics
untyped model: e : D D ∼ = Z + (D → D) + {wrong} simply typed model: Γ ⊢ e : τ : Dτ Dint = Z Dσ→τ = Dσ → Dτ effect-directed model: Γ ⊢ e : τ, F : DF
τ
Core idea: algebra-semantics homomorphism Algebraic structure of F determines structure on family DF
τ .
10 / 20
SLIDE 43 Effect analysis and semantics
Lattice effects (Gifford, Lucassen ’86) Γ ⊢ e : τ, F
(Nielson, Nielson ’94, ’99)
- “Marriage of effects and monads”
(Wadler, Thiemann ’03) Γ ⊢ e : MFτ : Γ → Tτ
- Effect-directed semantics
(Katsumata ’14) Γ ⊢ e : MFτ : Γ → TFτ (graded monads)
Richer effect-directed semantics (graded joinads)
11 / 20
SLIDE 44 Effect analysis and semantics
Lattice effects (Gifford, Lucassen ’86) Γ ⊢ e : τ, F
(Nielson, Nielson ’94, ’99)
- “Marriage of effects and monads”
(Wadler, Thiemann ’03) Γ ⊢ e : MFτ : Γ → Tτ
- Effect-directed semantics
(Katsumata ’14) Γ ⊢ e : MFτ : Γ → TFτ (graded monads)
Richer effect-directed semantics (graded joinads) Operations on TF homomorphic to operations on F
11 / 20
SLIDE 45 Modelling effects with graded monads (Katsumata 2014)
12 / 20
SLIDE 46 Modelling effects with graded monads (Katsumata 2014)
Γ ⊢ e : τ, F : Γ → TFτ
12 / 20
SLIDE 47 Modelling effects with graded monads (Katsumata 2014)
Γ ⊢ e : τ, F : Γ → TFτ e.g. for state TA = S → (A × S).
12 / 20
SLIDE 48 Modelling effects with graded monads (Katsumata 2014)
Γ ⊢ e : τ, F : Γ → TFτ e.g. for state TFA = (reads(F)) → (A × writes(F)).
12 / 20
SLIDE 49 Modelling effects with graded monads (Katsumata 2014)
Γ ⊢ e : τ, F : Γ → TFτ e.g. for state TFA = (reads(F)) → (A × writes(F)). e.g. for partiality TA = ⊥ + A
12 / 20
SLIDE 50 Modelling effects with graded monads (Katsumata 2014)
Γ ⊢ e : τ, F : Γ → TFτ e.g. for state TFA = (reads(F)) → (A × writes(F)). e.g. for partiality T⊥A = ⊥, T⊤A = A,
12 / 20
SLIDE 51
Modelling effects with graded monads (Katsumata 2014)
Γ ⊢ e : τ, F : Γ → TFτ e.g. for state TFA = (reads(F)) → (A × writes(F)). e.g. for partiality T⊥A = ⊥, T⊤A = A, T?A = A + 1
SLIDE 52
Modelling effects with graded monads (Katsumata 2014)
Γ ⊢ e : τ, F : Γ → TFτ e.g. for state TFA = (reads(F)) → (A × writes(F)). e.g. for partiality T⊥A = ⊥, T⊤A = A, T?A = A + 1 Graded monads provide sequential composition
SLIDE 53
Modelling effects with graded monads (Katsumata 2014)
Γ ⊢ e : τ, F : Γ → TFτ e.g. for state TFA = (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.
SLIDE 54
Modelling effects with graded monads (Katsumata 2014)
Γ ⊢ e : τ, F : Γ → TFτ e.g. for state TFA = (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 d1 : A → TFB and d2 : B → TGC
SLIDE 55 Modelling effects with graded monads (Katsumata 2014)
Γ ⊢ e : τ, F : Γ → TFτ e.g. for state TFA = (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 d1 : A → TFB and d2 : B → TGC then d2 ˆ
SLIDE 56 Modelling effects with graded monads (Katsumata 2014)
Γ ⊢ e : τ, F : Γ → TFτ e.g. for state TFA = (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 d1 : A → TFB and d2 : B → TGC then d2 ˆ
idA : A → T0A.
12 / 20
SLIDE 57 Modelling effects with graded monads (Katsumata 2014)
Γ ⊢ e : τ, F : Γ → TFτ e.g. for state TFA = (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 d1 : A → TFB and d2 : B → TGC then d2 ˆ
idA : A → T0A. With ordering Given partially ordered (F, •, 0, ⊑) then for all F ⊑ G then coercion: ιF,G,A : TFA → TGA
12 / 20
SLIDE 58 Semantics of branching: derived vs. parameterised
13 / 20
SLIDE 59 Semantics of branching: derived vs. parameterised
if e0 then e1 else e2 = COND(e0, e1, e2)
13 / 20
SLIDE 60 Semantics of branching: derived vs. parameterised
if e0 then e1 else e2 = COND(e0, e1, e2)
◮ COND definable via cond : B × A × A → A
13 / 20
SLIDE 61 Semantics of branching: derived vs. parameterised
if e0 then e1 else e2 = COND(e0, e1, e2)
◮ COND definable via cond : B × A × A → A
cond(true, x, y) = x cond(false, x, y) = y
13 / 20
SLIDE 62 Semantics of branching: derived vs. parameterised
if e0 then e1 else e2 = COND(e0, e1, e2)
◮ COND definable via cond : B × A × A → A
cond(true, x, y) = x cond(false, x, y) = y
◮ Restricts effect branching behaviour:
13 / 20
SLIDE 63 Semantics of branching: derived vs. parameterised
if e0 then e1 else e2 = COND(e0, e1, e2)
◮ COND definable via cond : B × A × A → A
cond(true, x, y) = x cond(false, x, y) = y
◮ Restricts effect branching behaviour:
(if) Γ ⊢ e0 : bool, F
Γ ⊢ e1 : τ, G Γ ⊢ e2 : τ, H Γ ⊢ if e0 then e1 else e2 : τ, F • (G ⊔ H)
13 / 20
SLIDE 64 Semantics of branching: derived vs. parameterised
if e0 then e1 else e2 = COND(e0, e1, e2)
◮ COND definable via cond : B × A × A → A
cond(true, x, y) = x cond(false, x, y) = y
◮ Restricts effect branching behaviour:
(if) Γ ⊢ e0 : bool, F
Γ ⊢ e1 : τ, G Γ ⊢ e2 : τ, H Γ ⊢ if e0 then e1 else e2 : τ, F • (G ⊔ H) cond : B × TG⊔HA × TG⊔HA → TG⊔HA
13 / 20
SLIDE 65 Semantics of branching: derived vs. parameterised
if e0 then e1 else e2 = COND(e0, e1, e2)
◮ COND definable via cond : B × A × A → A
cond(true, x, y) = x cond(false, x, y) = y
◮ Restricts effect branching behaviour:
(if) Γ ⊢ e0 : bool, F
Γ ⊢ e1 : τ, G Γ ⊢ e2 : τ, H Γ ⊢ if e0 then e1 else e2 : τ, F • (G ⊔ H) cond : B × TG⊔HA × TG⊔HA → TG⊔HA
◮ Towards Nielson-Nielson richer effects, but + may not be ⊔.
13 / 20
SLIDE 66 Semantics of branching: derived vs. parameterised
if e0 then e1 else e2 = COND(e0, e1, e2)
◮ COND definable via cond : B × A × A → A
cond(true, x, y) = x cond(false, x, y) = y
◮ Restricts effect branching behaviour:
(if) Γ ⊢ e0 : bool, F
Γ ⊢ e1 : τ, G Γ ⊢ e2 : τ, H Γ ⊢ if e0 then e1 else e2 : τ, F • (G ⊔ H) cond : B × TG⊔HA × TG⊔HA → TG⊔HA
◮ Towards Nielson-Nielson richer effects, but + may not be ⊔. ◮ Instead: parameterise semantics on
COND : TFB × TGA × THA → T?
+(F,G,H)A
13 / 20
SLIDE 67
Definition: jonoid For a set of effects F then (F, •, I, &, ? +, ⊑) is a joinoid control-flow algebra:
SLIDE 68
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;
SLIDE 69
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;
SLIDE 70
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)
SLIDE 71
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
SLIDE 72
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:
SLIDE 73
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)
SLIDE 74 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
SLIDE 75
Graded generalised joinad
Definition: graded conditional joinad Given a jonoid (F, •, I, &, ? +, ⊑):
SLIDE 76
Graded generalised joinad
Definition: graded conditional joinad Given a jonoid (F, •, I, &, ? +, ⊑):
◮ graded monad for the pre-ordered monoid (F, •, I, ⊑)
SLIDE 77
Graded generalised joinad
Definition: graded conditional joinad Given a jonoid (F, •, I, &, ? +, ⊑):
◮ graded monad for the pre-ordered monoid (F, •, I, ⊑) ◮ additional operations:
SLIDE 78
Graded generalised joinad
Definition: graded conditional joinad Given a jonoid (F, •, I, &, ? +, ⊑):
◮ graded monad for the pre-ordered monoid (F, •, I, ⊑) ◮ additional operations:
condF,G,H,A :TFB × TGA × THA → T?
+(F,G,H)A
parF,G,A :TFA × TGA → TF&GA
SLIDE 79 Graded generalised joinad
Definition: graded conditional joinad Given a jonoid (F, •, I, &, ? +, ⊑):
◮ graded monad for the pre-ordered monoid (F, •, I, ⊑) ◮ additional operations:
condF,G,H,A :TFB × TGA × THA → T?
+(F,G,H)A
parF,G,A :TFA × TGA → TF&GA
◮ Satisfy jonoid axioms (modulo lifting to functors)
15 / 20
SLIDE 80 Graded generalised joinad
Definition: graded conditional joinad Given a jonoid (F, •, I, &, ? +, ⊑):
◮ graded monad for the pre-ordered monoid (F, •, I, ⊑) ◮ additional operations:
condF,G,H,A :TFB × TGA × THA → T?
+(F,G,H)A
parF,G,A :TFA × TGA → TF&GA
◮ Satisfy jonoid axioms (modulo lifting to functors)
where ? + = cond and & = par Monoids are to (graded) monads as jonoids are to (graded) joinads
15 / 20
SLIDE 81
Theorem: soundness Given a graded joinadic semantics for the simply-effect-and- typed λ-calculus with if and par then, for all e, e′, Γ, τ, F:
SLIDE 82
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
SLIDE 83 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:
SLIDE 84 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β1’)
if true then e else x ≡ e
(ifβ2’)
if false then x else e′ ≡ e′
(if-dist-par)
(if b then e else e′) par e′′ ≡ if b then (e par e′′) else (e′ par e′′)
(if-dist-seq)
let x = (if e then e′ else e′′) in e′′′ ≡ if e then (let x = e′ in e′′′) else (let x = e′′ in e′′′)
(par-pure)
x par e ≡ (x, e)
(par-sym)
e par e′ ≡ swap (e′ par e)
(par-assoc)
e par (e′ par e′′) ≡ assoc ((e par e′) par e′′)
16 / 20
SLIDE 85 Conclusions
17 / 20
SLIDE 86 Conclusions
◮ unify Hanne and Flemming’s rich effects with semantics
17 / 20
SLIDE 87 Conclusions
◮ unify Hanne and Flemming’s rich effects with semantics
monads
indexing
− − − − − → graded monads
non-seq. control
− − − − − − − − − → graded joinads
17 / 20
SLIDE 88 Conclusions
◮ unify Hanne and Flemming’s rich effects with semantics
monads
indexing
− − − − − → graded monads
non-seq. control
− − − − − − − − − → graded joinads
◮ Semantics for various kinds of parallel, concurrent (e.g. music)
and speculative behaviour (e.g. prefetching)
17 / 20
SLIDE 89 Conclusions
◮ unify Hanne and Flemming’s rich effects with semantics
monads
indexing
− − − − − → graded monads
non-seq. control
− − − − − − − − − → graded joinads
◮ Semantics for various kinds of parallel, concurrent (e.g. music)
and speculative behaviour (e.g. prefetching)
◮ Considerable simplification to proofs
17 / 20
SLIDE 90 Conclusions
◮ unify Hanne and Flemming’s rich effects with semantics
monads
indexing
− − − − − → graded monads
non-seq. control
− − − − − − − − − → 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
SLIDE 91 Conclusions
◮ unify Hanne and Flemming’s rich effects with semantics
monads
indexing
− − − − − → graded monads
non-seq. control
− − − − − − − − − → 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
SLIDE 92 Conclusions
◮ unify Hanne and Flemming’s rich effects with semantics
monads
indexing
− − − − − → graded monads
non-seq. control
− − − − − − − − − → 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
SLIDE 93 Conclusions
◮ unify Hanne and Flemming’s rich effects with semantics
monads
indexing
− − − − − → graded monads
non-seq. control
− − − − − − − − − → 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
SLIDE 94 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
SLIDE 95 Backup slides
19 / 20
SLIDE 96 Modeling effects with monads
Model effectful computations via some data type T
20 / 20
SLIDE 97 Modeling effects with monads
Model effectful computations via some data type T Γ ⊢ e : τ : Γ → Tτ
20 / 20
SLIDE 98 Modeling effects with monads
Model effectful computations via some data type T Γ ⊢ e : τ : Γ → Tτ e.g. for state TA = S → (A × S).
20 / 20
SLIDE 99 Modeling effects with monads
Model effectful computations via some data type T Γ ⊢ e : τ : Γ → Tτ e.g. for state TA = S → (A × S). e.g. for partiality TA = ⊥ + A
20 / 20
SLIDE 100
Modeling effects with monads
Model effectful computations via some data type T Γ ⊢ e : τ : Γ → Tτ e.g. for state TA = S → (A × S). e.g. for partiality TA = ⊥ + A Monads provide sequential composition Given f : A → TB and g : B → TC then
SLIDE 101 Modeling effects with monads
Model effectful computations via some data type T Γ ⊢ e : τ : Γ → Tτ e.g. for state TA = S → (A × S). e.g. for partiality TA = ⊥ + A Monads provide sequential composition Given f : A → TB and g : B → TC then g ˆ
SLIDE 102 Modeling effects with monads
Model effectful computations via some data type T Γ ⊢ e : τ : Γ → Tτ e.g. for state TA = S → (A × S). e.g. for partiality TA = ⊥ + A Monads provide sequential composition Given f : A → TB and g : B → TC then g ˆ
with ˆ idA : A → TA.
20 / 20