Programming Languages in String Diagrams [ four ] Local stores Paul-André Melliès Oregon Summer School in Programming Languages June 2011
Finitary monads A monadic account of algebraic theories
� � Algebraic presentations of effects We want to reason about programs with effects like states, excep- tions... Computational monads: pimpurep ppurep T ( B ) = A B A Algebraic theories: A n −→ A operations : and equations
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 the category of of � algebraic theories finitary monads 4
� � � Finitary monads Alternative definition. A monad T : Set −→ Set is finitary when the diagram Set � �������������������� � � � � � � � � T ◦ i T � � � � � id � � � � � � � � FinSet Set i exhibits the functor T as a left Kan extension of T ◦ i along i . 5
Finitary monad A monad T on the category Set is finitary when every map [1] −→ TA factors as T f e [1] −→ T [ p ] −→ TA for a pair of maps [ p ] −→ A . [1] −→ T [ p ] 6
� � � � � Finitary monad Moreover, the factorization should be unique up to zig-zag: T [ p ] � � � � � � � � � � � � � e 1 T f 1 � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � [1] TA Tu � � � � � � � � � � � � � � � � � � � � � � � � e 2 � � � T f 2 � � � � � � � � � � � � � � � � � � T [ q ] 7
The Kleisli category of a monad The Kleisli category associated to a monad Set T 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
The Lawvere theory of a monad The Lawvere theory associated to a monad Θ T T : Set −→ Set is the full subcategory of with finite ordinals Set T [ p ] { 0 , 1 , . . . , p − 1 } = as objects. The category Θ T has finite sums. Remark. 9
� � � � � Algebras of a monad A set A equipped with a function alg : TA −→ A making the two diagrams below commute: T ( alg ) TTA TA TA � � � � � � � � � � � � � � � � � alg � � η A � � alg � � µ � � � � � � � � � � � � � � � � � � � � � � alg � � � A � id � A TA A 10
Key theorem [ originating from Lawvere ] The category of T -algebras is equivalent to the category of finite product preserving functors Θ op −→ Set T This property holds for every finitary monad T . 11
Finitary monads as saturated theories The finitary monad describes the equational classes of the theory : terms with n variables modulo equations Tn = where n x 0 , x 1 , · · · , x n − 1 = The monad often reveals a canonical form for the equational theory.
Mnemoids An algebraic presentation of the state monad
� � � The state monad A program accessing one register with a finite set V of values impure A B is interpreted as a function pure V × B V × A thus as a function pure V ⇒ ( V × B ) A Hence, the state monad T : A �→ V ⇒ ( V × A ) 14
What is an algebra of the state monad ? An elegant answer by Plotkin and Power [2002]
Mnemoids A mnemoid is a set A equipped with a V -ary operation A V : −→ A lookup and a unary operation : A −→ A update � val � for each value val ∈ V . 16
Mnemoids Typically, the lookup operation on the boolean space V = { f alse , t rue } is binary : : A × A −→ A lookup The intuition is that � when the register is false u lookup ( u , v ) = when the register is true v 17
� � Annihilation lookup – update A V � � � � � � � � � � � update � V � � � lookup � � � � � � � � � � � � � � � � � � � � � � � � � � � � id � A � A term val �→ update � val � term = lookup 18
Annihilation lookup – update Here, the map A V : A −→ update � V � is the V -ary vector · · · update � 0 � update � V − 1 � , , also noted val �→ update � val � 19
Annihilation lookup – update x true = x x false 20
� � Interaction update – update A � � � � � � � � � � � � � � � update � val 2 � update � val 1 � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � update � val 2 � � � � � � � A A update � val 1 � ◦ update � val 2 � = update � val 2 � 21
Interaction update – update = val 2 val 2 val 1 22
� � � Interaction update – lookup lookup A V A update � val � A val update � val � � A A update � val � ◦ lookup x �→ term ( x ) update � val � ◦ term ( val ) = 23
Interaction update – lookup x = x true true y 24
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
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
Store with several locations Algebraic presentation of the state monad
The global state monad The state monad is generally defined as T : A �→ S ⇒ ( S × A ) for a set of states V L S = induced by a set L of locations and a finite set V of values. 28
Global store [Plotkin & Power ] A global store is a family of compatible mnemoidal structures A V : −→ A lookup � loc � : A −→ A update � loc , val � one for each location loc ∈ L . A tensor product of algebraic theories 29
� � Annihilation lookup – update A V � � � � � � � � � � � � � update � loc , V � lookup � loc � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � id � A � A val �→ update � loc , val � term = term lookup � loc � 30
Annihilation lookup – update t r x u e = x e s a l f x 31
� � Interaction update – update A � � � � � � � � � � � � � � � update � loc , val ′� update � loc , val � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � A A update � loc , val ′� update � loc , val 1 � ◦ update � loc , val 2 � = update � loc , val 2 � 32
Interaction update – update val 1 val 2 val 2 = 33
� � � Interaction update – lookup lookup � loc � A V A update � loc , val � A val � A A update � loc , val � update � loc , val � ◦ lookup � loc � x �→ term ( x ) update � loc , val � ◦ term ( val ) = 34
true true Interaction update – lookup x = x y 35
false false Interaction update – lookup x = y y 36
� � � Commutation update – update update � loc , val � A A update � loc ′ , val ′� update � loc ′ , val ′� � A A update � loc , val � update � loc , v � update � loc ′ , v ′ � x = update � loc ′ , v ′ � update � loc , v � x loc � loc ′ when 37
Commutation update – update = val val val ′ val ′ 38
Corollary [ Plotkin & Power ] the category of objects with a global store is equivalent to the category of algebras of the state monad 39
Graded monads A stratified version of finitary monads
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
Recommend
More recommend