Local states in string diagrams Paul-André Melliès 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 effects We want to reason about programs with effects like states, exceptions... Computational monads: pimpurep ppurep A B = A T ( B ) Equational theories: A n −→ A : operations and equations
� � � The state monad A program accessing one register with a set of values Val = { true , false } impure A B is interpreted as a function pure Val × A Val × B thus as a function pure Val ⇒ ( Val × B ) A Hence, the state monad T : A �→ Val ⇒ ( Val × A )
� � � � � 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 η A alg µ alg alg � A id � A TA A
Algebraic effects Key idea : Effects are performed by algebraic operations on types The algebraic theory is presented by a family of n -ary operations A × · · · × A −→ : A operation � ������� �� ������� � n together with a family of well-chosen equations.
States Similarly, memory calls are performed by a binary operation A × A −→ A : lookup and two unary operations : A −→ A : A −→ A update � true � update � false � The three equations of the theory are: � � term update � true � ( term ) , update � false � ( term ) = lookup update � val 1 � ◦ update � val 2 � = update � val 2 � � � update � val � ◦ lookup term ( true ) , term ( false ) update � val � ( term ( val )) =
� � Creation lookup – update A × A update � true , false � lookup id � A A � � term = update � true � ( term ) , update � false � ( term ) lookup
Creation lookup – update Here, the map : A −→ A × A update � true , false � is the pair � � update � true � , update � false � also noted � � val �→ update � val �
Creation lookup-update x true = root x root false x
� � Interaction update – update A update � val 2 � update � val 1 � update � val 2 � � A A update � val 1 � ◦ update � val 2 � = update � val 2 �
Interaction update-update val val x root = x val root
� � � Interaction update – lookup lookup A × A A update � val � A val update � val � � A A � � update � val � ◦ lookup term ( true ) , term ( false ) update � val � ◦ term ( val ) =
Interaction update – lookup x x true root true root = y x false root false root = y y
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
A proof based on rewriting [LICS 2010] A clean bridge between rewriting and semantics
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 � � terms with variables in A modulo the equations of T T A =
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 the category of of � algebraic theories finitary monads
� � � � 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 .
Finitary monads Hence, a finitary monad T : Set −→ Set is entirely described by its restriction to finite sets � � [ n ] = x 0 , . . . , x n − 1 .
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 ] x 0 , . . . , x n − 1 =
Canonical form theorem [LICS 2010] Every term with n variables is equivalent to a term of the form � � val �→ update � f ( val ) � x g ( val ) lookup which interprets the function n � ������������ �� ������������ � Val −→ Val + · · · + Val �→ val inj � g ( val ) � f ( val )
Canonical form theorem [LICS 2010] x val p root x wal q
Proof by rewriting Step 1. use the creation rule to rewrite � � create term −→ val �→ update � val � term lookup Step 2. use the interaction rules to rewrite ∗ update � val � term −→ update � wal � x p for some value wal and some variable x p . Step 3. conclude...
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.
� � � Remark: the equation below is redundant lookup × lookup A × A × A × A A × A [ π 11 ,π 22 ] lookup lookup � A A × A � � val 1 �→ lookup [ val 2 �→ term ( val 1 , val 2 ) ] lookup � � = val �→ term ( val , val ) lookup
The local state monad A more sophisticated example by Plotkin and Power
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.
Presheaf models This defines a covariant presheaf A n : Inj −→ Set on the category Inj of natural numbers and injections. Every injection f : p −→ q induces a function A f : A p −→ A q obtained by renaming every register i ∈ [ p ] by the register f ( i ) ∈ [ q ] .
Local stores [Plotkin & Power 2002] The slightly intimidating monad � � p ∈ Inj � S p × A p × Inj ( n , p ) S n �→ ⇒ L A : n on the presheaf category [ Inj , Set ] where the contravariant presheaf S p Val p = describes the states available at degree p .
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 ?
Graded arities A graded arity is defined as a finite sum of representables [ p 0 , · · · , p n ] � 0 � + · · · + � 0 � + · · · + � n � + · · · + � n � = � ����������� �� ����������� � � ����������� �� ����������� � p 0 times p n times where each representable presheaf � n � Inj −→ Set : is defined as � n � p �→ Inj ( n , p ) =
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.
� � � � Graded monads Definition. A monad T : [ Inj , Set ] −→ [ Inj , Set ] is graded when the diagram [ Inj , Set ] T ◦ i T id Σ Inj op [ Inj , Set ] i exhibits the functor T as a left Kan extension of T ◦ i along i .
Main result of the paper Theorem. The local state monad � � p ∈ Inj � S p × A p × Inj ( n , p ) S n L A : n �→ ⇒ is a graded monad on the presheaf category [ Inj , Set ] .
Main result of the paper In particular, the local state monad L : [ Inj , Set ] −→ [ Inj , Set ] is entirely described by its restriction i L Σ Inj op L ◦ i −→ −→ : [ Inj , Set ] [ Inj , Set ] to the subcategory Σ Inj op of graded arities.
An algebraic presentation of local states Update, lookup, fresh, permute, discharge
The Lawvere theory of a graded monad The Lawvere theory associated to a graded monad Θ T T : [ Inj , Set ] −→ [ Inj , Set ] has graded arities as objects [ p 0 , · · · , p m ] and morphisms defined as Θ T ( [ p 0 , · · · , p m ] , [ q 0 , · · · , q n ] ) = [ Inj , Set ] ( [ p 0 , · · · , p m ] , T [ q 0 , · · · , q n ] )
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 [ p 0 , · · · , p m ] and morphisms defined as מ ([ p 0 , · · · , p m ] , [ q 0 , · · · , q n ] ) = [ Inj , Set ] ( [ p 0 , · · · , p m ] , L [ q 0 , · · · , q n ] )
Key observation The local state monad −→ L : [ Inj , Set ] [ Inj , Set ] is the composite L F ◦ B = of 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]
The two component monads The first monad S n ⇒ ( S n × A n ) F A n �→ : 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 .
Recommend
More recommend