Local stores Paul-Andr Mellis Oregon Summer School in Programming - - PowerPoint PPT Presentation

local stores
SMART_READER_LITE
LIVE PREVIEW

Local stores Paul-Andr Mellis Oregon Summer School in Programming - - PowerPoint PPT Presentation

Programming Languages in String Diagrams [ four ] Local stores Paul-Andr Mellis Oregon Summer School in Programming Languages June 2011 Finitary monads A monadic account of algebraic theories Algebraic presentations of


slide-1
SLIDE 1

Programming Languages in String Diagrams [ four ]

Local stores

Paul-André Melliès Oregon Summer School in Programming Languages June 2011

slide-2
SLIDE 2

Finitary monads

A monadic account of algebraic theories

slide-3
SLIDE 3

Algebraic presentations of effects

We want to reason about programs with effects like states, excep- tions... Computational monads: A

pimpurep

  • B

= A

ppurep

  • T (B)

Algebraic theories:

  • perations

: A n −→ A and equations

slide-4
SLIDE 4

A useful correspondence

Recall that a monad T : Set −→ Set is finitary when it preserves the filtered colimit. Important fact. There is an equivalence of categories the category

  • f

algebraic theories

  • the category
  • f

finitary monads

4

slide-5
SLIDE 5

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.

5

slide-6
SLIDE 6

Finitary monad

A monad T on the category Set is finitary when every map [1] −→ TA factors as [1]

e

−→ T [p]

T f

−→ TA for a pair of maps [1] −→ T [p] [p] −→ A.

6

slide-7
SLIDE 7

Finitary monad

Moreover, the factorization should be unique up to zig-zag: T [p]

Tu

  • T f1
  • [1]

e1

  • e2
  • TA

T [q]

T f2

  • 7
slide-8
SLIDE 8

The Kleisli category of a monad

The Kleisli category Set T associated to a monad T : Set −→ Set has sets as objects, and functions f : A −→ T(B) as morphisms from A to B. An alternative formulation of the category of free algebras.

8

slide-9
SLIDE 9

The Lawvere theory of a monad

The Lawvere theory Θ T associated to a monad T : Set −→ Set is the full subcategory of Set T with finite ordinals [p] = { 0 , 1 , . . . , p − 1 } as objects. Remark. The category Θ T has finite sums.

9

slide-10
SLIDE 10

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

10

slide-11
SLIDE 11

Key theorem [ originating from Lawvere ]

The category of T-algebras is equivalent to the category of finite product preserving functors Θ op

T

−→ Set This property holds for every finitary monad T.

11

slide-12
SLIDE 12

Finitary monads as saturated theories

The finitary monad describes the equational classes of the theory : Tn =      terms with n variables modulo equations      where n =      x0 , x1 , · · · , xn−1      The monad often reveals a canonical form for the equational theory.

slide-13
SLIDE 13

Mnemoids

An algebraic presentation of the state monad

slide-14
SLIDE 14

The state monad

A program accessing one register with a finite set V of values A

impure

  • B

is interpreted as a function V × A

pure

  • V × B

thus as a function A

pure

  • V ⇒ (V × B)

Hence, the state monad T : A → V ⇒ (V × A)

14

slide-15
SLIDE 15

What is an algebra of the state monad ?

An elegant answer by Plotkin and Power [2002]

slide-16
SLIDE 16

Mnemoids

A mnemoid is a set A equipped with a V-ary operation lookup : A V −→ A and a unary operation updateval : A −→ A for each value val ∈ V .

16

slide-17
SLIDE 17

Mnemoids

Typically, the lookup operation on the boolean space V = { false , true } is binary : lookup : A × A −→ A The intuition is that lookup (u, v) =

  • u

when the register is false v when the register is true

17

slide-18
SLIDE 18

Annihilation lookup – update

A V

lookup

  • A

updateV

  • id

A

term = lookup       val → updateval term      

18

slide-19
SLIDE 19

Annihilation lookup – update

Here, the map updateV : A −→ A V is the V-ary vector       update0 , · · · , updateV−1       also noted       val → updateval      

19

slide-20
SLIDE 20

Annihilation lookup – update

x

=

x x true false

20

slide-21
SLIDE 21

Interaction update – update

A

updateval1

  • A

updateval2

  • updateval2

A

updateval1 ◦ updateval2 = updateval2

21

slide-22
SLIDE 22

Interaction update – update

val2 val1

=

val2

22

slide-23
SLIDE 23

Interaction update – lookup

AV

lookup

  • Aval
  • A

updateval

  • A

updateval

A

updateval ◦ lookup       x → term(x)       = updateval ◦ term(val)

23

slide-24
SLIDE 24

Interaction update – lookup

x y true

=

true x

24

slide-25
SLIDE 25

Key theorem [ Plotkin & Power ]

the category of mnemoids is equivalent to the category of algebras of the state monad Provides an algebraic presentation of the state monad

25

slide-26
SLIDE 26

A proof in three steps

1. establish that the state monad T is finitary [easy] 2. construct a functor [easy] ι : Θ M −→ Θ T starting from the Lawvere theory Θ M of mnemoids by interpreting the update and lookup operations and by checking that the equations are valid in Θ T 3. show that the functor ι is full and faithful [rewriting]

26

slide-27
SLIDE 27

Store with several locations

Algebraic presentation of the state monad

slide-28
SLIDE 28

The global state monad

The state monad is generally defined as T : A → S ⇒ (S × A) for a set of states S = V L induced by a set L of locations and a finite set V of values.

28

slide-29
SLIDE 29

Global store [Plotkin & Power ]

A global store is a family of compatible mnemoidal structures lookuploc : AV −→ A updateloc,val : A −→ A

  • ne for each location loc ∈ L.

A tensor product of algebraic theories

29

slide-30
SLIDE 30

Annihilation lookup – update

AV

lookuploc

  • A

updateloc,V

  • id

A

lookuploc       val → updateloc,val term       = term

30

slide-31
SLIDE 31

Annihilation lookup – update

x

=

t r u e f a l s e

x x

31

slide-32
SLIDE 32

Interaction update – update

A

updateloc,val

  • A

updateloc,val′

  • updateloc,val′

A

updateloc,val1 ◦ updateloc,val2 = updateloc,val2

32

slide-33
SLIDE 33

Interaction update – update

val1 val2

=

val2

33

slide-34
SLIDE 34

Interaction update – lookup

AV

lookuploc

  • Aval
  • A

updateloc,val

  • A

updateloc,val

A

updateloc,val ◦ lookuploc       x → term(x)       = updateloc,val ◦ term(val)

34

slide-35
SLIDE 35

Interaction update – lookup

true

y x

=

true

x 35

slide-36
SLIDE 36

Interaction update – lookup

false

y x

=

false

y 36

slide-37
SLIDE 37

Commutation update – update

A

updateloc,val

  • updateloc′,val′
  • A

updateloc′,val′

  • A

updateloc,val

A

updateloc,v updateloc′,v′ x = updateloc′,v′ updateloc,v x when loc loc′

37

slide-38
SLIDE 38

Commutation update – update

val′ val

=

val val′

38

slide-39
SLIDE 39

Corollary [ Plotkin & Power ]

the category of objects with a global store is equivalent to the category of algebras of the state monad

39

slide-40
SLIDE 40

Graded monads

A stratified version of finitary monads

slide-41
SLIDE 41

Presheaf models

Key idea: interpret a type A as a family of sets A[0] A[1] · · · A[n] · · · indexed by natural numbers, where each set A[n] contains the programs of type A which have access to n variables.

41

slide-42
SLIDE 42

Presheaf models

This defines a covariant presheaf A[n] : Inj −→ Set

  • n the category Inj of natural numbers and injections.

The action of the injections on A are induced by the operations collectloc : A[n] −→ A[n+1] defined for 0 ≤ loc ≤ n.

42

slide-43
SLIDE 43

Local stores [Plotkin – Power]

The slightly intimidating monad TA : n → S[n] ⇒       p∈Inj S[p] × A[p] × Inj(n, p)      

  • n the presheaf category [Inj, Set] where the contravariant presheaf

S[p] = V p describes the states available at degree p.

43

slide-44
SLIDE 44

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)

44

slide-45
SLIDE 45

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.

45

slide-46
SLIDE 46

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.

46

slide-47
SLIDE 47

Graded monad

A monad T on the category [Inj, Set] is graded when every map n −→ TA factors as n

e

−→ T [p0, . . . , pk]

T f

−→ TA for a pair of maps n −→ T [p0, . . . , pk] [p0, . . . , pk] −→ A.

47

slide-48
SLIDE 48

Graded monad

Moreover, the factorization should be unique up to zig-zag: T [p0, · · · , pi]

Tu

  • T f1
  • n

e1

  • e2
  • TA

T [q0, · · · , qj]

T f2

  • 48
slide-49
SLIDE 49

Motivating example

  • Theorem. The local state monad

TA : n → S[n] ⇒       p∈Inj S[p] × A[p] × Inj(n, p)       is a graded monad on the presheaf category [Inj, Set].

49

slide-50
SLIDE 50

Graded theories

A stratified version of algebraic theories

slide-51
SLIDE 51

The Lawvere theory of a graded monad

The Lawvere theory Θ T associated to a graded monad T : [Inj, Set] −→ [Inj, Set] is the full subcategory of [Inj, Set] T with the graded arities [ p0 , · · · , pn ] as objects. Remark. The category Θ T has finite sums.

51

slide-52
SLIDE 52

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

52

slide-53
SLIDE 53

Modules over the category Inj

A category A equipped with an endofunctor D : A −→ A and two natural transformations permute : D ◦ D −→ D ◦ D collect : Id −→ D depicted as follows in the language of string diagrams:

D D D D D

53

slide-54
SLIDE 54

Yang-Baxter equation

D D D D D D

=

D D D D D D

54

slide-55
SLIDE 55

Symmetry

D D D D

=

D D D D

55

slide-56
SLIDE 56

Interaction dispose – permute

D D D

=

D D D D D D

=

D D D

56

slide-57
SLIDE 57

Graded theories

A graded theory is a category with finite products with objects [ p0 , · · · , pn ] = 0 × · · · × 0

  • p0 times

× · · · × n × · · · × n

  • pn times

equipped with an action of the category Inj satisfying D(A × B)

  • DA × DB

D(1)

  • 1

In particular D [ p0 , · · · , pn ] = [ 0 , p0 , · · · , pn ]

57

slide-58
SLIDE 58

The trivial graded theory

Σ Inj op is the free Inj-module with finite sums such that D(A + B)

  • DA + DB

D(0)

  • Π Inj is the free Inj-module with finite products such that

D(A × B)

  • DA × DB

D(1)

  • 1

58

slide-59
SLIDE 59

The graded theory of local stores

Letמ‌ be the graded theory associated to the monad of local stores. Fact.

מ‌ is the free Inj-module with finite sums such that

D(A + B)

  • DA + DB

D(0)

  • such that D is a dynamic mnemoid.

59

slide-60
SLIDE 60

Dynamic mnemoids

A dynamic mnemoid is a pair of sets A[0] A[1] equipped with the following operations lookup : AV

[1]

−→ A[1] updateval : A[1] −→ A[1] freshval : A[1] −→ A[0] collect : A[0] −→ A[1] satisfying a series of basic equations.

60

slide-61
SLIDE 61

Garbage collect : fresh – dispose

val

=

id

61

slide-62
SLIDE 62

Interaction fresh – update

val2 val1

=

val2

62

slide-63
SLIDE 63

Interaction fresh – lookup

x y true

=

true x x y false

=

false y

63

slide-64
SLIDE 64

Model of a graded theory

A model of a graded theoryמ‌ is a functor M :מ‌−→[Inj, Set] which preserves the finite products and the action of D. A stratified version of the usual algebraic theories

64

slide-65
SLIDE 65

The graded theory of local mnemoids

Main theorem [after Plotkin and Power] The category of models of the theoryמ‌ of local mnemoids is equivalent to the category of algebras of the local state monad

65

slide-66
SLIDE 66

Local stores in string diagrams

A graphical account of memory calls

slide-67
SLIDE 67

Annihilation lookup – update

x

=

t r u e f a l s e

x x

The equation D(A × B) DA × DB means that space commutes with time ramification !!!

slide-68
SLIDE 68

Interaction update – update

val1 val2

=

val2

68

slide-69
SLIDE 69

Interaction update – lookup

true

y x

=

true

x 69

slide-70
SLIDE 70

Interaction update – lookup

false

y x

=

false

y 70

slide-71
SLIDE 71

Interaction fresh – dispose

val

D D D D D D

=

D D D D D D

71

slide-72
SLIDE 72

Interaction fresh – update

D D D D D D D val1 val2

=

D D D D D D D val2

72

slide-73
SLIDE 73

Interaction fresh – lookup

true

D D D y x

=

true

D D D y

73

slide-74
SLIDE 74

Interaction fresh – lookup

false

D D D y x

=

false

D D D y

74

slide-75
SLIDE 75

Interaction fresh – permutation

val D D D D D D D

=

val D D D D D D D

75

slide-76
SLIDE 76

The Segal condition

A basic tool in higher dimensional algebra

76

slide-77
SLIDE 77

In higher dimensional algebra

Exactly the same categorical combinatorics !!!

77

slide-78
SLIDE 78

In higher dimensional algebra

A globular set is defined as a contravariant presheaf over the cate- gory

s

  • t
  • 1

s

  • t
  • 2

s

  • t
  • · · ·

defined by the equations s ◦ s = s ◦ t t ◦ s = t ◦ t An ω-category is then defined as an algebra of a particular monad T. Is the notion of ω-category equational ?

78

slide-79
SLIDE 79

Level 1 : the free category monad

A category C is a graph where every path A0

f1

A1

f2

A2

f3

· · ·

fn

An

defines an edge A0

  • n (f1, ··· ,fn)
  • An

where the operations ◦n are required to be associative. the arities of the theory are finite paths rather than finite sets

slide-80
SLIDE 80

Category with arities

A fully faithful and dense functor i0 : Θ0 −→

A

where Θ0 is a small category. Typical example: the full subcategory Θ0 of linear graphs [n] =

1 · · · n

in the category Graph.

80

slide-81
SLIDE 81

The nerve functor

This induces a fully faithful functor nerve :

A

−→

  • Θ0

which transports every object A of the category A into the presheaf nerve : Θ0 −→ Set p →

A( i0p , A )

81

slide-82
SLIDE 82

The nerve functor

slide-83
SLIDE 83

Definition of a monad T with arity

  • the functor T is the left kan extension of T ◦ i0 along the functor i0

A

id

  • Θ0

i0

  • T◦i0
  • A

T

  • This enables to reconstruct T from its restriction to the arities

83

slide-84
SLIDE 84

Monads with arities

  • this left kan extension is preserved by the nerve functor:
  • Θ0

A

nerve

  • id
  • Θ0

i0

  • T◦i0
  • A

T

  • 84
slide-85
SLIDE 85

A purely combinatorial formulation

For every object A, the canonical morphism p∈Θ0

A(i0n, Ti0p) × A(i0p, A)

−→

A(i0n, TA)

is an isomorphism.

85

slide-86
SLIDE 86

Unique factorization up to zig-zag

Every map n −→ TA in the category A decomposes as n

e

−→ Tp

T f

−→ TA A finite number of words encounters a finite number of letters A finite path in the free category TA encounters a finite path in A

86

slide-87
SLIDE 87

Unique factorization up to zig-zag

The factorization should be unique up to zig-zag of Ti0p

Ti0u

  • T f1
  • i0n

e1

  • e2
  • TA

Ti0q

T f2

  • 87
slide-88
SLIDE 88

Model of an algebraic theory

A model A of a Lawvere theory L is a presheaf A : Θop

L

−→ Set such that the induced presheaf Θop

i0

Θop

L A

Set

is representable along i0.

88

slide-89
SLIDE 89

Key theorem

The category of T-modules is equivalent to the category of models of the theory ΘT This property holds for every monad T with arities Θ0.

89

slide-90
SLIDE 90

Algebraic theories with arities

A 2-dimensional approach to Lawvere theories

90

slide-91
SLIDE 91

Algebraic theory with arities

An algebraic theory L with arities i0 : Θ0 −→

A

is an identity-on-object functor L : Θ0 −→ ΘL such that...

91

slide-92
SLIDE 92

Algebraic theories with arities

... there exists a functor F :

A

−→

A

making the diagram

  • Θ0

∃L

  • ΘL

L∗

  • Θ0

nerve

  • nerve
  • A

F

  • A

commute up natural isomorphism.

92

slide-93
SLIDE 93

Algebraic theories with arities

Set

  • Θop

L

  • nerve(A)
  • Θop

L left Kan extension

  • This simply means that the free construction works...

93

slide-94
SLIDE 94

Theorem

the category of algebraic theories with arity (A, i0) is equivalent to the category of monads with arity (A, i0) This generalizes the traditional correspondence algebraic theories ∼ finitary monads Also extends the result by Power on enriched Lawvere theories

94