SLIDE 1
Local states in string diagrams Paul-Andr Mellis CNRS & - - PowerPoint PPT Presentation
Local states in string diagrams Paul-Andr Mellis CNRS & - - PowerPoint PPT Presentation
Local states in string diagrams Paul-Andr Mellis CNRS & Universit Paris Diderot RTA-TLCA 2014 14 17 July 2014 Vienna Summer Logic Algebraic effects A seminal idea by Power and Plotkin Algebraic presentations of
SLIDE 2
SLIDE 3
Algebraic presentations of effects
We want to reason about programs with effects like states, exceptions... Computational monads: A pimpurep
- B
= A ppurep
- T (B)
Equational theories:
- perations
: A n −→ A and equations
SLIDE 4
The state monad
A program accessing one register with a set of values Val = {true, false} A impure
- B
is interpreted as a function Val × A pure
- Val × B
thus as a function A pure
- Val ⇒ (Val × B)
Hence, the state monad T : A → Val ⇒ (Val × A)
SLIDE 5
Algebras of a monad
A set A equipped with a function alg : TA −→ A making the two diagrams below commute: TTA
T ( alg )
- µ
- TA
alg
- TA
alg
A
TA
alg
- A
ηA
- id
A
SLIDE 6
Algebraic effects
Key idea : Effects are performed by algebraic operations on types The algebraic theory is presented by a family of n-ary operations
- peration
: A × · · · × A
- n
−→ A together with a family of well-chosen equations.
SLIDE 7
States
Similarly, memory calls are performed by a binary operation lookup : A × A −→ A and two unary operations update true : A −→ A update false : A −→ A The three equations of the theory are: term = lookup
- update true (term) , update false (term)
- updateval1 ◦ updateval2
= updateval2 updateval ◦ lookup
- term(true), term( false)
- =
updateval (term(val))
SLIDE 8
Creation lookup – update
A × A
lookup
- A
updatetrue,false
- id
A
term = lookup
- update true (term) , update false (term)
SLIDE 9
Creation lookup – update
Here, the map updatetrue,false : A −→ A × A is the pair
- updatetrue
, update false
- also noted
- val → updateval
SLIDE 10
Creation lookup-update
root
x
=
x
false
root
true
x
SLIDE 11
Interaction update – update
A
updateval1
- A
updateval2
- updateval2
A
updateval1 ◦ updateval2 = updateval2
SLIDE 12
Interaction update-update
val
root
val
x
=
root
val
x
SLIDE 13
Interaction update – lookup
A × A
lookup
- Aval
- A
updateval
- A
updateval
A
updateval ◦ lookup
- term(true) , term(false)
- =
updateval ◦ term(val)
SLIDE 14
Interaction update – lookup
x y
true
root
=
x
true
root x y
false
root
=
y
false
root
SLIDE 15
Key theorem [ Plotkin & Power 2002 ]
the category of mnemoids is equivalent to the category of algebras of the state monad This provides an algebraic presentation of the state monad
SLIDE 16
A proof based on rewriting
[LICS 2010] A clean bridge between rewriting and semantics
SLIDE 17
From algebraic theories to monads
Fact. Every algebraic theory T induces a monad T : Set −→ Set which transports every set A to its free model TA defined as T A =
- terms with variables in A modulo the equations of T
SLIDE 18
Finitary monads
- Definition. A monad
T : Set −→ Set is called finitary when it preserves filtered colimits. Important fact. There is an equivalence of categories the category
- f
algebraic theories
- the category
- f
finitary monads
SLIDE 19
Finitary monads
Alternative definition. A monad T : Set −→ Set is finitary when the diagram Set
id
- FinSet
i
- T◦ i
- Set
T
- exhibits the functor T as a left Kan extension of T ◦ i along i.
SLIDE 20
Finitary monads
Hence, a finitary monad T : Set −→ Set is entirely described by its restriction to finite sets [n] =
- x0 , . . . , xn−1
- .
SLIDE 21
A proof by rewriting in two steps
Step 1. establish that the state monad T : A → Val ⇒ (Val × A) : Set −→ Set is a finitary monad. Step 2. show that it computes the free mnemoid on finite sets [n] =
- x0 , . . . , xn−1
SLIDE 22
Canonical form theorem [LICS 2010]
Every term with n variables is equivalent to a term of the form lookup
- val → update f(val) xg(val)
- which interprets the function
Val −→
n
- Val + · · · + Val
val → injg(val) f(val)
SLIDE 23
Canonical form theorem [LICS 2010]
p
wal
root
val
q x x
SLIDE 24
Proof by rewriting
Step 1. use the creation rule to rewrite term
create
−→ lookup
- val → updateval term
- Step 2. use the interaction rules to rewrite
updateval term
∗
−→ updatewal xp for some value wal and some variable xp. Step 3. conclude...
SLIDE 25
Proof by rewriting
Corollary. The state monad T : Set −→ Set coincides with the free mnemoid monad when restricted to finite sets. Important consequence: One recovers in this way the original theorem by Plotkin & Power.
SLIDE 26
Remark: the equation below is redundant
A × A × A × A
lookup×lookup
- [π11,π22]
- A × A
lookup
- A × A
lookup
A
lookup
- val1 → lookup [ val2 → term (val1, val2) ]
- =
lookup
- val → term (val, val)
SLIDE 27
The local state monad
A more sophisticated example by Plotkin and Power
SLIDE 28
Presheaf models
Key idea: interpret a type A as a family of sets A0 A1 · · · An · · · indexed by natural numbers, where each set An contains the programs of type A which have access to n variables.
SLIDE 29
Presheaf models
This defines a covariant presheaf An : Inj −→ Set
- n the category Inj of natural numbers and injections.
Every injection f : p −→ q induces a function Af : Ap −→ Aq
- btained by renaming every register i ∈ [p] by the register f(i) ∈ [q].
SLIDE 30
Local stores [Plotkin & Power 2002]
The slightly intimidating monad L A : n → S n ⇒ p∈Inj S p × Ap × Inj (n, p)
- n the presheaf category [Inj, Set] where the contravariant presheaf
S p = Val p describes the states available at degree p.
SLIDE 31
Our methodology here
⊲ Show that the monad is finitary in some sense. ⊲ Establish a similar canonical form theorem for local states. Fine, but what is the appropriate notion of finitary monad here ?
SLIDE 32
Graded arities
A graded arity is defined as a finite sum of representables [ p0 , · · · , pn ] = 0 + · · · + 0
- p0 times
+ · · · + n + · · · + n
- pn times
where each representable presheaf n : Inj −→ Set is defined as n = p → Inj(n, p)
SLIDE 33
Graded arities
This defines a full and faithful functor Σ Inj op −→ Inj op which generalizes the full and faithful functor FinSet −→ Set encountered in the case of finitary monads.
SLIDE 34
Graded monads
- Definition. A monad
T : [Inj, Set] −→ [Inj, Set] is graded when the diagram [Inj, Set]
id
- Σ Inj op
i
- T◦ i
- [Inj, Set]
T
- exhibits the functor T as a left Kan extension of T ◦ i along i.
SLIDE 35
Main result of the paper
Theorem. The local state monad L A : n → S n ⇒ p∈Inj S p × Ap × Inj (n, p)
- is a graded monad on the presheaf category [Inj, Set].
SLIDE 36
Main result of the paper
In particular, the local state monad L : [Inj, Set] −→ [Inj, Set] is entirely described by its restriction L ◦ i : Σ Inj op
i
−→ [Inj, Set]
L
−→ [Inj, Set] to the subcategory Σ Inj op of graded arities.
SLIDE 37
An algebraic presentation of local states
Update, lookup, fresh, permute, discharge
SLIDE 38
The Lawvere theory of a graded monad
The Lawvere theory Θ T associated to a graded monad T : [Inj, Set] −→ [Inj, Set] has graded arities as objects [ p0, · · · , pm] and morphisms defined as Θ T ( [ p0, · · · , pm] , [ q0, · · · , qn] ) = [Inj, Set] ( [ p0, · · · , pm] , T [ q0, · · · , qn] )
SLIDE 39
The Lawvere theory of the local state monad
The Lawvere theoryמ associated to the local state monad L : [Inj, Set] −→ [Inj, Set] has graded arities as objects [ p0, · · · , pm] and morphisms defined as
מ([ p0 , · · · , pm ] , [ q0 , · · · , qn ] )
= [Inj, Set] ( [ p0, · · · , pm] , L [ q0, · · · , qn] )
SLIDE 40
Key observation
The local state monad L : [Inj, Set] −→ [Inj, Set] is the composite L = F ◦ B
- f two graded monads
F , B : [Inj, Set] −→ [Inj, Set] related by a distributivity law λ : B ◦ F ⇒ F ◦ B. A phenomenon already noticed by Staton [2010]
SLIDE 41
The two component monads
The first monad F A : n → Sn ⇒ ( Sn × An ) is the global state monad applied on each fiber n while B = ℓ∗ ◦ ∃ℓ : [Inj, Set] −→ [Inj, Set] is the change-of-basis monad associated to a functor ℓ : Inj −→ Res.
SLIDE 42
Local mnemoids
Definition. A local mnemoid is a family of sets A0 A1 · · · An · · · equipped with the following operations lookuploc : An × An −→ An updateloc,val : An −→ An freshloc,val : An+1 −→ An disposeloc : An −→ An+1 permuteloc : An −→ An satisfying a series of elementary equations.
SLIDE 43
An algebraic presentation of the fiber monad
SLIDE 44
Creation lookup – update
x
=
t r u e f a l s e
x x
SLIDE 45
Interaction update – update
val1 val2
=
val2
SLIDE 46
Interaction update – lookup
true
y x
=
true
x
SLIDE 47
Commutation update – update
val′ val
=
val val′
SLIDE 48
An algebraic presentation of the basis monad
SLIDE 49
Modules over the category Inj
An Inj-module is a category A equipped with an action ∗ : Inj × A −→
A
(m, A) m ∗ A satisfying the expected properties: (p + q) ∗ A = p ∗ (q ∗ A) 0 ∗ A = A
SLIDE 50
Modules over the category Inj
A category A equipped with an endofunctor D :
A
−→
A
and two natural transformations permute : D ◦ D −→ D ◦ D dispose : Id −→ D depicted as follows in the language of string diagrams:
D D D D D
SLIDE 51
Yang-Baxter equation
D D D D D D
=
D D D D D D
SLIDE 52
Symmetry
D D D D
=
D D D D
SLIDE 53
Interaction dispose – permute
D D D
=
D D D D D D
=
D D D
SLIDE 54
Allocation
One adds a fresh operator freshval : D −→ Id for each value val ∈ {true, false} depicted in string diagrams as:
val D
SLIDE 55
Garbage collect: fresh – collect
val
=
Identity
SLIDE 56
Interaction fresh – permutation
D D D val
=
D D D val
SLIDE 57
Commutation fresh – fresh
D val D val
=
D val D val
SLIDE 58
An algebraic presentation of the distributivity law
SLIDE 59
Interaction fresh – update
D D D D D D val val D
=
D D D D D D val D
SLIDE 60
Commutation fresh – update
D D D D D D val D val
=
D D D D D D val D val
SLIDE 61
Commutation fresh – update
D D D x y val
=
D D D x y val val
SLIDE 62
Interaction collect – update
D D D D D D val D
=
D D D D D D val D
SLIDE 63
Interaction permute – lookup
D D D D D D D D val
=
D D D D D D D D val
SLIDE 64
Local mnemoids
Definition. A local mnemoid is a family of sets A0 A1 · · · An · · · equipped with the following operations lookuploc : An × An −→ An updateloc,val : An −→ An freshloc,val : An+1 −→ An disposeloc : An −→ An+1 permuteloc : An −→ An satisfying the equations above.
SLIDE 65
Theorem [ revisiting Plotkin & Power 2002 ]
the category of local mnemoids is equivalent to the category of algebras of the local state monad Lookup – Update – Discharge – Allocate – Permute
SLIDE 66
Thank you !
SLIDE 67
Effects on the wire
val
η
The update operation in string diagrams
SLIDE 68
Effects on the wire
η
left branch
L R η
right branch
L R
The lookup operation in string diagrams
SLIDE 69
Effects on the wire
κ+ κ+ ε B A R A B R R L L L