local stores
play

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


  1. Programming Languages in String Diagrams [ four ] Local stores Paul-André Melliès Oregon Summer School in Programming Languages June 2011

  2. Finitary monads A monadic account of algebraic theories

  3. � � 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

  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 the category of of � algebraic theories finitary monads 4

  5. � � � 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

  6. 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

  7. � � � � � 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

  8. 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

  9. 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

  10. � � � � � 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

  11. 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

  12. 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.

  13. Mnemoids An algebraic presentation of the state monad

  14. � � � 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

  15. What is an algebra of the state monad ? An elegant answer by Plotkin and Power [2002]

  16. 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

  17. 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

  18. � � Annihilation lookup – update A V � � � � � � � � � � � update � V � � � lookup � � � � � � � � � � � � � � � � � � � � � � � � � � � � id � A � A           term  val �→ update � val � term = lookup  18

  19. 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

  20. Annihilation lookup – update x true = x x false 20

  21. � � 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

  22. Interaction update – update = val 2 val 2 val 1 22

  23. � � � 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

  24. Interaction update – lookup x = x true true y 24

  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

  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

  27. Store with several locations Algebraic presentation of the state monad

  28. 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

  29. 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

  30. � � Annihilation lookup – update A V � � � � � � � � � � � � � update � loc , V � lookup � loc � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � id � A � A            val �→ update � loc , val � term = term lookup � loc �  30

  31. Annihilation lookup – update t r x u e = x e s a l f x 31

  32. � � 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

  33. Interaction update – update val 1 val 2 val 2 = 33

  34. � � � 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

  35. true true Interaction update – lookup x = x y 35

  36. false false Interaction update – lookup x = y y 36

  37. � � � 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

  38. Commutation update – update = val val val ′ val ′ 38

  39. Corollary [ Plotkin & Power ] the category of objects with a global store is equivalent to the category of algebras of the state monad 39

  40. Graded monads A stratified version of finitary monads

  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

Recommend


More recommend