Lecture 1: Algebraic Effects I Gordon Plotkin Laboratory for the - - PowerPoint PPT Presentation

lecture 1 algebraic effects i
SMART_READER_LITE
LIVE PREVIEW

Lecture 1: Algebraic Effects I Gordon Plotkin Laboratory for the - - PowerPoint PPT Presentation

Moggis Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Lecture 1: Algebraic Effects I Gordon Plotkin Laboratory for the Foundations of Computer Science, School of Informatics, University of Edinburgh 20th


slide-1
SLIDE 1

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Lecture 1: Algebraic Effects I

Gordon Plotkin

Laboratory for the Foundations of Computer Science, School of Informatics, University of Edinburgh

20th Estonian Winter School in Computer Science

Plotkin Lecture 1: Algebraic Effects I

slide-2
SLIDE 2

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Outline

1

Moggi’s Monads As Notions of Computation

2

Algebraic Effects Introduction Equational theories

Finitary equational theories Algebraic operations and generic effects

3

Prospectus and Exercises

Plotkin Lecture 1: Algebraic Effects I

slide-3
SLIDE 3

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Outline

1

Moggi’s Monads As Notions of Computation

2

Algebraic Effects Introduction Equational theories

Finitary equational theories Algebraic operations and generic effects

3

Prospectus and Exercises

Plotkin Lecture 1: Algebraic Effects I

slide-4
SLIDE 4

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

The typed λ-calculus: syntax

Raw Syntax Types σ ::= b | σ → τ Terms M ::= c | x | λx : σ. M | MN Typing Environments Γ ::= x1 : σ1, . . . , xn : σn Judgements Γ ⊢ M : σ

Plotkin Lecture 1: Algebraic Effects I

slide-5
SLIDE 5

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

The typed λ-calculus: typing rules

Variables x1 : σ1, . . . , xn : σn ⊢ xi : σi (1 ≤ i ≤ n) Constants Γ ⊢ c : σ (as given) Abstractions Γ, x : σ ⊢ M : τ Γ ⊢ λx : σ. M : σ → τ Applications Γ ⊢ M : σ → τ Γ ⊢ N : σ Γ ⊢ MN : τ

Plotkin Lecture 1: Algebraic Effects I

slide-6
SLIDE 6

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

The typed λ-calculus: semantics in Set

Types [ [σ] ] ∈ Set Basic Types [ [b] ] = (as given) Function spaces [ [σ → τ] ] = [ [σ] ] ⇒ [ [τ] ] Environments [ [x1 : σ1, . . . , xn : σn] ] = [ [σ1] ] × . . . × [ [σn] ]

Plotkin Lecture 1: Algebraic Effects I

slide-7
SLIDE 7

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Semantics: very explicit

Terms Γ ⊢ M : σ [ [M] ] : [ [Γ] ] → [ [σ] ] Variables [ [xi] ](a1, . . . , an) = ai Constants [ [c] ](a1, . . . , an) = (as given) Abstractions [ [λx : σ. M] ](a1, . . . , an) = a ∈ [ [σ] ] → [ [M] ](a1, . . . , an, a) Applications [ [MN] ](a1, . . . , an) = [ [M] ](a1, . . . , an)([ [N] ](a1, . . . , an))

Plotkin Lecture 1: Algebraic Effects I

slide-8
SLIDE 8

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Semantics of Exceptions

Suppose programs can raise exceptions e ∈ E. Then we want: Γ ⊢ M : σ [ [M] ] : [ [Γ] ] → ([ [σ] ]+E) (Remember: X + Y = ({0} × X) ∪ ({1} × Y)) Function spaces [ [σ → τ] ] = [ [σ] ] ⇒ ([ [τ] ]+E) Variables [ [xi] ](a1, . . . , an) = inl(ai) = η(ai) Constants [ [c] ](a1, . . . , an) = (as given)

Plotkin Lecture 1: Algebraic Effects I

slide-9
SLIDE 9

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Semantics of Exceptions (cntnd.)

Abstractions [ [λx : σ. M] ](a1, . . . , an) = η(a ∈ [ [σ] ] → [ [M] ](a1, . . . , an, a)) Applications For M : σ → τ, N : σ [ [MN] ](γ) = E-ap([ [M] ](γ), [ [N] ](γ)) where E-ap : ([ [σ → τ] ] + E) × ([ [σ] ] + E) − → ([ [τ] ] + E) E-ap(a, b) =    inr(e) (if a = inr(e)) inr(e′) (if a = inl(f) and b = inr(e′) f(c) (if a = inl(f) and b = inr(c)

Plotkin Lecture 1: Algebraic Effects I

slide-10
SLIDE 10

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Moggi’s insight: a categorical view of - + E

Functorial action f : X → Y f + E : X + E → Y + E where: (f + E)(a) = inl(f(b)) (if a = inl(b)) inr(e)) (if a = inr(e)) Monadic structure Unit η : X → X + E Multiplication µ : (X + E) + E → X + E µ(a) =    inl(e) (if a = inr(e)) inr(e′) (if a = inl(inr(e))) b (if a = inl(inl(b))

Plotkin Lecture 1: Algebraic Effects I

slide-11
SLIDE 11

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

A categorical view of - + E (cntnd.)

Strength Left strength lst : X × (Y + E) − → (X × Y) + E Right strength rst : (X + E) × Y − → (X × Y) + E lst(a, b) = inr(e) (if b = inr(e)) inl(a, c) (if b = inl(c))

Plotkin Lecture 1: Algebraic Effects I

slide-12
SLIDE 12

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Putting these together

((X ⇒ (Y + E)) + E) × (X + E)

rst

− → ((X ⇒ (Y + E)) × (X + E)) + E

lst+E

− − − → ((X ⇒ (Y + E)) × X) + E) + E

µ

− → ((X ⇒ (Y + E)) × X) + E

ap+E

− − − → (Y + E) + E

µ

− → Y + E

Plotkin Lecture 1: Algebraic Effects I

slide-13
SLIDE 13

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Strong monads

Summarising, one needs a operator T(X) on Set, the category

  • f sets, equipped with:

A functorial action Set(X, Y)

T( · )

− − − − → Set(T(X), T(Y)) This makes T a functor A unit X

ηX

− − − → T(X) A multiplication T(T(X))

µX

− − − → T(X) These make T a monad A (left) strength X × T(Y)

stX,Y

− − − − → T(X × Y) This makes T a strong monad Note, can derive the right strength: T(X) × Y

twist

− − − → Y × T(X)

stY,X

− − − → T(Y × X)

T(twist)

− − − − → T(X × Y)

Plotkin Lecture 1: Algebraic Effects I

slide-14
SLIDE 14

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Moggi’s insight (cntnd.)

Other computational effects can also be modelled by strong monads T, e.g. in Set: State S × X − → S × Y can be rewritten as X − → (S × Y)S and Tstate(X) = (S × X)S is a strong monad. Finite Nondeterminism TSL(X) = F+(X) the collection of non-empty finite subsets of X. Continuations Tcont(X) = RRX . Functions X → Tcont(Y) can be rewritten as RY − → RX. and there are many other examples, including combinations, such as this for state plus exceptions: T(X) = (S × (X + E))S In Cpo one has similar examples, generally including lifting to accommodate recursion, so that T(P) is a cppo, e.g., for state plus nontermination: T(P) = ((S × P)⊥)S

Plotkin Lecture 1: Algebraic Effects I

slide-15
SLIDE 15

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Semantics of application for any monad

T(X ⇒ T(Y)) × T(X)

rst

− → T((X ⇒ T(Y)) × T(X))

T(lst)

− − − → T(T((X ⇒ T(Y)) × X))

µ

− → T((X ⇒ T(Y)) × X)

T(ap)

− − − → T(T(Y))

µ

− → T(Y)

Plotkin Lecture 1: Algebraic Effects I

slide-16
SLIDE 16

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Outline

1

Moggi’s Monads As Notions of Computation

2

Algebraic Effects Introduction Equational theories

Finitary equational theories Algebraic operations and generic effects

3

Prospectus and Exercises

Plotkin Lecture 1: Algebraic Effects I

slide-17
SLIDE 17

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Outline

1

Moggi’s Monads As Notions of Computation

2

Algebraic Effects Introduction Equational theories

Finitary equational theories Algebraic operations and generic effects

3

Prospectus and Exercises

Plotkin Lecture 1: Algebraic Effects I

slide-18
SLIDE 18

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Two questions

So we have a denotational semantics. How about an

  • perational one?

How do effects arise, i.e., how do we “construct” them in a programming language?

  • Answering the first question immediately leads to the

second.

  • Answering that leads to understanding where Moggi’s

monads come from.

Plotkin Lecture 1: Algebraic Effects I

slide-19
SLIDE 19

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

An example: finite nondeterminism

Take TSL(X) = F+(X) the collection of non-empty finite subsets of X. To create the effects we add an effect constructor to the language: M : σ N : σ M + N : σ with semantics [ [M + N] ](γ) = [ [M] ](γ) ∪ [ [N] ](γ) So TSL(X) is an algebra (when equipped) with the binary

  • peration ∪ : TSL(X) × TSL(X) → TSL(X). But which algebra is

it?

Plotkin Lecture 1: Algebraic Effects I

slide-20
SLIDE 20

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Nondeterminism as an algebraic effect

There is a natural equational theory, with signature + : 2, and set of axioms SL (for semilattices) given by: Associativity (x + y) + z = x + (y + z) Commutativity x + y = y + x Absorption x + x = x The above algebra on F+(X) satisfies these equations, interpreting + as ∪. Further: F+ is the free algebra monad.

Plotkin Lecture 1: Algebraic Effects I

slide-21
SLIDE 21

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Is this the right set of axioms?

An equational theory is equationally inconsistent if it proves x = y. An equational theory is Hilbert-Post complete if adding an unprovable equation makes it equationally inconsistent. Theorem SL is Hilbert-Post complete. Proof. Let t = u be an unprovable equation, and assume it. Then there is a variable x in one of t or u, but not in the other. Equating all the other variables to y one obtains one of the following two equations: x = y or x + y = y. One obtains x = y from either of these.

Plotkin Lecture 1: Algebraic Effects I

slide-22
SLIDE 22

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Other effects

Similar results hold in Set for, eg, exceptions, (global) state; I/O; (probabilistic) nondeterminism; and combinations thereof (but may not get HP completeness). May need infinitary algebra and parameterised operations. Further Works similarly for Cpo but also need inequations t ≤ u. For locality, e.g., new variables and fresh names, one uses categories of presheaves.

Plotkin Lecture 1: Algebraic Effects I

slide-23
SLIDE 23

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Outline

1

Moggi’s Monads As Notions of Computation

2

Algebraic Effects Introduction Equational theories

Finitary equational theories Algebraic operations and generic effects

3

Prospectus and Exercises

Plotkin Lecture 1: Algebraic Effects I

slide-24
SLIDE 24

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Finitary equational theories: syntax

Signature Σe = (Op, ar : Op → N). We write op : n for arities. Terms t ::= x |

  • p(t1, . . . , tn) (op : n). We leave open

what the set Var of variables is; this will prove useful. Equations t = u Axiomatisations Sets Ax of equations Deduction Ax ⊢ t = u Theories Sets of equations Th closed under equational deduction

Plotkin Lecture 1: Algebraic Effects I

slide-25
SLIDE 25

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Finitary equational theories: equational deduction rules

Axiom Ax ⊢ t = u (if t = u ∈ Ax) Equivalence Ax ⊢ t = t Ax ⊢ t = u Ax ⊢ u = v Ax ⊢ u = v Ax ⊢ t = u Ax ⊢ u = t Congruence Ax ⊢ ti = ui (i = 1, n) Ax ⊢ f(t1, . . . , tn) = f(u1, . . . , un) (for f : n) Substitution Ax ⊢ t = u Ax ⊢ t[v/x] = u[v/x]

Plotkin Lecture 1: Algebraic Effects I

slide-26
SLIDE 26

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Addition to λ-calculus syntax

Γ ⊢ M1 : σ, . . . , Γ ⊢ Mn : σ Γ ⊢ op(M1, . . . , Mn) : σ (op : n)

Plotkin Lecture 1: Algebraic Effects I

slide-27
SLIDE 27

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Finitary equational theories: semantics

Algebras A = (A, opA : An − → A (op : n)) Homomorphisms h : A → B are functions h : A → B such that, for all op : n, and a1, . . . , an ∈ A: h(opA(a1, . . . , an)) = opB(h(a1), . . . , h(an)) Denotation A[ [t] ](ρ), where ρ : Var → A. A[ [x] ](ρ) = ρ(x) A[ [op(t1, . . . , tn)] ](ρ) = opA(A[ [t1] ](ρ), . . . , A[ [tn] ](ρ)) Validity A | = t = u Models A is a model of Ax if A | = t = u, for all t = u in Ax.

Plotkin Lecture 1: Algebraic Effects I

slide-28
SLIDE 28

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

The free algebra monad TAx of an axiomatic theory Ax

The free model FAx(X) of Ax over a set X is the algebra with carrier: TAx(X) =def {[t]Ax | t is a term with variables in X} where [t]Ax =def {u | Ax ⊢ t = u}. Its operations are given by:

  • pFAx(X)([t1], . . . , [tn]) = [op(t1, . . . , tn)]

(op : n)

Plotkin Lecture 1: Algebraic Effects I

slide-29
SLIDE 29

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Freeness

For any model A of Ax, and any function f : X → A there is a unique homomorphism f † : FAx(X) → A such that the following diagram commutes: X f TAx(X) η

f †

✲ A ✲

where η =def x → [x]Ax

1

f †([t]) = A[ [t] ](f)

2

TAx(X) is a strong monad with unit η, multiplication (idTAx(X))†.

3

Above only characterises FAx up to (algebraic) isomorphism (e.g., consider SL).

Plotkin Lecture 1: Algebraic Effects I

slide-30
SLIDE 30

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Another example: exceptions

Given a (possibly infinite) set E of exceptions, the signature has nullary operation symbols: raisee (e ∈ E) The set of axioms Exc is empty, and one obtains the usual exceptions monad TExc(X) = X + E There is then a puzzle: how do exception handlers fit into the algebraic theory of effects - more later!

Plotkin Lecture 1: Algebraic Effects I

slide-31
SLIDE 31

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Yet another example: one boolean location

Signature: writetrue, writefalse : 1; read : 2. Read writeb(x) as “write b, and continue with x” Read read(x, y) as “read the boolean variable, continuing with x, if true, and with y otherwise”. Axioms The set of axioms BoolState read(x, x) = x read(read(w, x), read(y, z)) = read(w, z) writeb(writeb′(x)) = writeb′(x) read(writetrue(x), writefalse(y)) = read(x, y) writetrue(read(x, y)) = writetrue(x) writefalse(read(x, y)) = writefalse(y)

Plotkin Lecture 1: Algebraic Effects I

slide-32
SLIDE 32

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

One boolean location (cntnd)

The monad is: Tbool(X) = (T × X)T The unit is η(x)(b) = (b, x)

Plotkin Lecture 1: Algebraic Effects I

slide-33
SLIDE 33

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Yet another example: probabilistic computation

Signature: binary operations +p for p ∈ [0, 1]. Read +p as ‘do x with probability p and y with probability 1 − p’ Axioms: the barycentric algebra axioms: One x +1 y = x ID x +r x = x SC x +r y = y +1−r x SA (x +p y) +r z = x +pr (y + r(1−p)

1−pr z)

(r < 1, p < 1) A consequence: COM (x +r u) +s (v +r y) = (x +s v) +r (u +s y)

Plotkin Lecture 1: Algebraic Effects I

slide-34
SLIDE 34

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Yet another example: probabilistic computation

The monad is the set of finite probability distributions over X Tprob(X) = Dω(X) =def {

n

  • i=1

λixi | n ≥ 0, λi ≥ 0,

  • i

λi = 1} and the unit is η(x) = δx the Dirac probability distribution on x.

Plotkin Lecture 1: Algebraic Effects I

slide-35
SLIDE 35

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Remarks

Prob is not HP complete, but its only proper equational extension is (equivalent to) SL, the theory of semilattices (this is a non-trivial result). These have an associative, commutative binary operator. Prob can be alternatively axiomatised using n-ary

  • perations

n,p1,...,pn for all n ≥ 0 and n-tuples of

nonnegative reals p1, . . . , pn summing to 1. The axioms are:

1

  • i=1,m δi

jxi = xj

2

  • i=1,m pi
  • j=1,n qijxj =

j=1,n( i=1,m piqij)xj

where Kronecker’s δi

j is 1 if i = j, and 0 otherwise.

Plotkin Lecture 1: Algebraic Effects I

slide-36
SLIDE 36

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Algebraic semantics

Form of semantics Γ ⊢ M : σ!α [ [Γ] ]

[ [M] ]

− − → TAxα([ [σ] ]) Abstraction: Typing Γ, x : σ ⊢ M : τ Γ ⊢ λx : σ. M : σ − → τ Abstraction: Semantics [ [λx : σ. M] ](γ) = [a ∈ [ [σ] ] → [ [M] ](γ, a)]Ax

Plotkin Lecture 1: Algebraic Effects I

slide-37
SLIDE 37

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Algebraic semantics (cntnd.)

Application: Typing Γ ⊢ M : σ − → τ Γ ⊢ N : σ Γ ⊢ MN : τ Application: Semantics Suppose [ [M] ](γ) = [t(f1, . . . , fm)]Ax [ [N] ](γ) = [u(a1, . . . , an)]Ax fi(xj) = [vij]Ax Then [ [MN] ](γ) = [t(u(v11, . . . , v1n), . . . , u(vm1, . . . , vmn))]Ax

Plotkin Lecture 1: Algebraic Effects I

slide-38
SLIDE 38

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Algebraic operations

Fix a finitary equational axiomatic theory Ax. Then for any set X and operation symbol op : n we have the function: TAx(X)n

  • pFAx(X)

− − − − − − → TAx(X) Further for any function f : X → TAx(Y), f † is a homomorphism: TAx(X)n opFAx(X)

✲ TAx(X)

= TAx(Y)n (f †)n

  • pFAx(Y)

✲ TAx(Y)

f †

We call such a famiy of functions TAx(X)n ϕX − − → TAx(X) algebraic.

Plotkin Lecture 1: Algebraic Effects I

slide-39
SLIDE 39

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Programming counterpart of being algebraic

Evaluation contexts are given by: E ::= [ · ] | EN | (λx : σ. M)E For any operation symbol op : n we have: | = E[op(M1, . . . , Mn)] = op(E[M1], . . . , E[Mn]) For example, with E alternatively of the forms [ · ]N or (λx : σ. N)[ · ] | = (raisee())(N) = raisee() | = (λx : σ. M)raisee() = raisee() | = (M +p M′)N = (MN) +p (M′N) | = (λx : σ. M)(N +p N′) = (λx : σ. M)N +p (λx : σ. M)N′

Plotkin Lecture 1: Algebraic Effects I

slide-40
SLIDE 40

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Generic effects

Given an algebraic family TAx(X)n ϕX − − → TAx(X), regarding n as {0, . . . , n − 1}, we obtain the generic effect: e ∈ TAx(n) = ϕn(ηn) Given e ∈ TAx(n) we obtain such an algebraic family by setting: ϕX =def TAx(X)n ( · )† − − → TAx(X)TAx(n) f → f(e) − − − − − → TAx(X) This correspondence is a bijection between algebraic families and generic effects. Noting that TAx(X)n is the collection of (equivalence classes) of terms with n free variables, we see (following the above definition) that the algebraic families are exactly the definable ones.

Plotkin Lecture 1: Algebraic Effects I

slide-41
SLIDE 41

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises Introduction Equational theories

Examples

Nondeterminism Corresponding to + we have: arb ∈ TSL({0, 1}) = {0, 1} which can be thought of as the (equivalence class of) the term x + y. Probabilistic nondeterminism Corresponding to + we have: coinp ∈ TSL({0, 1}) = pδ0 + (1 − p)δ1 which can be thought of as the (equivalence class of) the term x +p y. Exceptions Roughly raisee : 0 is its own generic effect. Precisely, to the family (raisee) · +E : ✶ = (X + E)0 → X + E corresponds inr(e) ∈ ∅ + E, which we can identify as e.

Plotkin Lecture 1: Algebraic Effects I

slide-42
SLIDE 42

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Outline

1

Moggi’s Monads As Notions of Computation

2

Algebraic Effects Introduction Equational theories

Finitary equational theories Algebraic operations and generic effects

3

Prospectus and Exercises

Plotkin Lecture 1: Algebraic Effects I

slide-43
SLIDE 43

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Some things that have been done so far

Calculi with effects, such as λc and CBPV. (Moggi; Levy; Egger, Mogelberg & Simpson) (Moderately) general operational semantics. May not get expected op. sems., eg, state. (P . & Power; Kammar et al ) Work on general notions of observation and full abstraction. (Johann, Simpson & Voigtl¨ ander) Theory, and application, of effect deconstructors, such as exception handlers via not necessarily free algebras. (P . & Pretnar; Bauer & Pretnar; Kammar, Oury & Lindley) Combining monads in terms of combining theories, primarily sum and

  • tensor. (Hyland,P

. & Power) Work on combining algebraic effects with continuations, which are not algebraic and require special treatment. (Hyland, Levy, Power & P .) First thoughts on a general logic of effects; connects with modal logic. Does not give Hoare logic. (P . & Pretnar) Type and effect systems. (Kammar & P .; Katsumata; Bauer & Pretnar) Work on locality and effects (Staton; Melli` es; P . & Power; Power). Algebraic accounts of control (Fiore & Staton)

Plotkin Lecture 1: Algebraic Effects I

slide-44
SLIDE 44

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Exercise 1: An extension of the typed λ-calculus

Consider the following extension of the λ-calculus. Raw Syntax Types σ ::= bool | unit | σ × τ | σ → τ Terms M ::= true | false | if A then M else N | x | ∗ | (M, N) | fst(M) | snd(M) | λx : σ. M | MN |

  • p(M1, . . . , Mn)

Informal understanding of the language. bool has two values true and false. The conditional uses them. unit has one value ∗. σ × τ consists of pairs of elements of σ and τ, given by (M, N) and accessed by fst and snd

Plotkin Lecture 1: Algebraic Effects I

slide-45
SLIDE 45

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

The Exercise

Write down typing rules for this language Give it a monadic semantics Give it an algebraic semantics Define evaluation contexts for this language. Prove that evaluation contexts commute with operations. (That is, show the equality given above for the relation between evaluation contexts and operations holds in the semantics.)

Plotkin Lecture 1: Algebraic Effects I

slide-46
SLIDE 46

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Exercise 2: Looking at monads

Recall the claimed example monads covered in the lecture: State Tstate(X) = (S × X)S Tbool(X) = (T × X)T Finite Nondeterminism TSL(X) = F+(X) Continuations Tcont(X) = RRX State plus exceptions T(X) = (S × (X + E))S Finite Probabilistic Nondeterminism Tprob(X) = Dω(X)

Plotkin Lecture 1: Algebraic Effects I

slide-47
SLIDE 47

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Exercise

  • 1. Choose a monad or two (I recommend state) and find

their functorial actions their unit and multiplication, their (left) strength the algebraic structure of T(X) (for example + : TSL(X)2 → TSL(X) is union).

  • 2. (More advanced)

Show that every monad on Set has a unique (left or right) strength. Show that Tcont is not given by any algebraic theory.

Plotkin Lecture 1: Algebraic Effects I

slide-48
SLIDE 48

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Exercise 3: Free Algebra Monads

Show that TAx is indeed a strong monad, i.e.: Find its functorial action. Find its unit (well that was given) and its multiplication Find its strength (Hint: if A is an algebra then AX becomes

  • ne too).

Plotkin Lecture 1: Algebraic Effects I

slide-49
SLIDE 49

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Exercise 4: Identifying monads

It is claimed above that some monads are the free algebra monads associated to certain equational theories. Prove this! Method We know the monads are given, up to isomorphism, by the equational logic as free algebra monads. So: Associate to every term t a normal form |t| so that ⊢Ax t = u iff |t| = |u|. Inspect the normal forms to see how they correspond to elements of T(Var). For example, for semilattices, every term can be proved equal to one of the form x1 + .... + xn (ignoring brackets) with no xi repeated, and the xi taken in order according to some fixed enumeration of the variables. (If you are trying probability, use the alternative theory.)

Plotkin Lecture 1: Algebraic Effects I

slide-50
SLIDE 50

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Exercise 5: HP completeness

Show that BoolState is Hilbert Post complete. (Hint: see what happens if you assume two different normal forms equal). Is the axiomatisation BoolState redundant. That is can you derive any of the axioms from the others?

Plotkin Lecture 1: Algebraic Effects I

slide-51
SLIDE 51

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Exercise 6: Probability

Show the two axiomatisations are equivalent. That is, one can define each in terms of the other. More precisely:

In one direction, define x +p y to be px + (1 − p)y then, using the alternative axiomatisation, show that all the barycentric axioms are derivable. In the other direction define the operations

n,p1,...,pn in

terms of the barycentric operations, and show all the axioms of the alternative theory are derivable. Then show the compositions of the two translations are provably equal in the relevant theories to the identity translation. (The definition of translations between equational theories is in the next lecture.)

Plotkin Lecture 1: Algebraic Effects I

slide-52
SLIDE 52

Moggi’s Monads As Notions of Computation Algebraic Effects Prospectus and Exercises

Probability (cntnd)

Prove Neumann’s Lemma, which is that if you add one equation of the form x +p y = x +q y with 0 < p < q < 1 to Prob then one can derive all such equations (this is not so easy). Show that the only nontrivial extension of Prob is SL the theory of semilattices. Method: show that if you add one equation between two different normal forms then the resulting theory is either equationally inconsistent or else it is intertranslatable with SL; you will need Neumann’s lemma.

Plotkin Lecture 1: Algebraic Effects I