Local states in string diagrams Paul-Andr Mellis CNRS & - - PowerPoint PPT Presentation

local states in string diagrams
SMART_READER_LITE
LIVE PREVIEW

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-1
SLIDE 1

Local states in string diagrams

Paul-André Melliès

CNRS & Université Paris Diderot RTA-TLCA 2014 Vienna Summer Logic 14 −→ 17 July 2014

slide-2
SLIDE 2

Algebraic effects

A seminal idea by Power and Plotkin

slide-3
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
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
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
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
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
SLIDE 8

Creation lookup – update

A × A

lookup

  • A

updatetrue,false

  • id

A

term = lookup

  • update true (term) , update false (term)
slide-9
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
SLIDE 10

Creation lookup-update

root

x

=

x

false

root

true

x

slide-11
SLIDE 11

Interaction update – update

A

updateval1

  • A

updateval2

  • updateval2

A

updateval1 ◦ updateval2 = updateval2

slide-12
SLIDE 12

Interaction update-update

val

root

 val

x

=

root

val

x

slide-13
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
SLIDE 14

Interaction update – lookup

x y

true

root

=

x

true

root x y

false

root

=

y

false

root

slide-15
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
SLIDE 16

A proof based on rewriting

[LICS 2010] A clean bridge between rewriting and semantics

slide-17
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
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
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
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
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
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
SLIDE 23

Canonical form theorem [LICS 2010]

p

wal

root

val

q x x

slide-24
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
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
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
SLIDE 27

The local state monad

A more sophisticated example by Plotkin and Power

slide-28
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
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
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
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
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
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
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
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
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
SLIDE 37

An algebraic presentation of local states

Update, lookup, fresh, permute, discharge

slide-38
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
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
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
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
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
SLIDE 43

An algebraic presentation of the fiber monad

slide-44
SLIDE 44

Creation lookup – update

x

=

t r u e f a l s e

x x

slide-45
SLIDE 45

Interaction update – update

val1 val2

=

val2

slide-46
SLIDE 46

Interaction update – lookup

true

y x

=

true

x

slide-47
SLIDE 47

Commutation update – update

val′ val

=

val val′

slide-48
SLIDE 48

An algebraic presentation of the basis monad

slide-49
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
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
SLIDE 51

Yang-Baxter equation

D D D D D D

=

D D D D D D

slide-52
SLIDE 52

Symmetry

D D D D

=

D D D D

slide-53
SLIDE 53

Interaction dispose – permute

D D D

=

D D D D D D

=

D D D

slide-54
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
SLIDE 55

Garbage collect: fresh – collect

val

=

Identity

slide-56
SLIDE 56

Interaction fresh – permutation

D D D val

=

D D D val

slide-57
SLIDE 57

Commutation fresh – fresh

D val D val

=

D val D val

slide-58
SLIDE 58

An algebraic presentation of the distributivity law

slide-59
SLIDE 59

Interaction fresh – update

D D D D D D val val D

=

D D D D D D val D

slide-60
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
SLIDE 61

Commutation fresh – update

D D D x y val

=

D D D x y val val

slide-62
SLIDE 62

Interaction collect – update

D D D D D D val D

=

D D D D D D val D

slide-63
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
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
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
SLIDE 66

Thank you !

slide-67
SLIDE 67

Effects on the wire

val

η

The update operation in string diagrams

slide-68
SLIDE 68

Effects on the wire

η

left branch

L R η

right branch

L R

The lookup operation in string diagrams

slide-69
SLIDE 69

Effects on the wire

κ+ κ+ ε B A R A B R R L L L