comonadic notions of computation
play

Comonadic notions of computation Tarmo Uustalu 1 Varmo Vene 2 1 - PowerPoint PPT Presentation

Comonadic notions of computation Tarmo Uustalu 1 Varmo Vene 2 1 Institute of Cybernetics, Tallinn 2 University of Tartu FMCS 2006, Kananaskis, 8 June 2006 Motivation Moggi and Wadler showed that effectful computations can be structured with


  1. Comonadic notions of computation Tarmo Uustalu 1 Varmo Vene 2 1 Institute of Cybernetics, Tallinn 2 University of Tartu FMCS 2006, Kananaskis, 8 June 2006

  2. Motivation Moggi and Wadler showed that effectful computations can be structured with monads. An effect-producing function from A to B is a map A → B in the Kleisli category, i.e., a map A → TB in the base category. Some examples applied in semantics: TA = A + 1 partiality TA = A + E exceptions A E = environment TA A ∗ = µ X . 1 + A × X TA = non-determinism ( A × S ) S = state TA Are all impure features captured by monads? What about comonads?

  3. � � � � Comonads Definition A comonad on category C is given by a functor D : C → C a natural transformation ε A : DA → A counit of the comonad a natural transformation δ A : DA → D 2 A comultiplication of the comonad s.t. following diagrams commute δ A � D 2 A DA DA � � �������� � δ A � δ A � � δ A D δ A � � � � DA � D 3 A D 2 A D 2 A D 2 A ε DA D ε A δ DA

  4. Comonads Comonads model notions of value in a context; DA is the type of contextually situated values of A . A context-relying function from A to B is a map A → B in the coKleisli category, i.e., a map DA → B in the base category. Product (environment) comonad Functor: DA = A × E Counit: ε A : A × E → A ( a , e ) �→ a Comultiplication: δ A : A × E → ( A × E ) × E ( a , e ) �→ (( a , e ) , e )

  5. Comonads Streams comonad DA = A N = ν X . A × X Functor: Counit: A N → A : ε A α �→ α ( 0 ) Comultiplication: A N → ( A N ) N δ A : �→ λ n . ( λ m .α ( n + m )) α [ a 0 , a 1 , a 2 , . . . ] �→ [[ a 0 , a 1 , a 2 , . . . ] , [ a 1 , a 2 , a 3 . . . ] , . . . ]

  6. Comonads for stream functions Dataflow computation = discrete-time signal transformations = stream functions. Example: simple dataflow programs = 0 fby ( pos + 1 ) pos sum x = x + ( 0 fby ( sum x )) fact = 1 fby ( fact ∗ ( pos + 1 )) fibo = 0 fby ( fibo + ( 1 fby fibo )) 0 1 2 3 4 5 6 . . . pos 0 1 3 6 10 15 21 . . . sum pos 1 1 2 6 24 120 720 . . . fact 0 1 1 2 3 5 8 fibo . . . Stream functions A N → B N are naturally isomorfic to A N × N → B

  7. Comonads for stream functions General stream functions DA = A N × N Functor: Input streams with past/present/future: a 0 , a 1 , . . . , a n − 1 , a n , a n + 1 , a n + 2 , . . . Counit: A N × N → A ε A : ( α, n ) �→ α ( n ) Comultiplication: A N × N → ( A N × N ) N × N δ A : ( α, n ) �→ ( λ m . ( α, m ) , n )

  8. Comonads for stream functions Causal stream functions ( ∼ DA = A + = A ∗ × A ) Functor: Input streams with past and present but no future Counit: A + → A : ε A [ a 0 , . . . , a n ] �→ a n Comultiplication: A + → ( A + ) + δ A : [ a 0 , . . . , a n ] �→ [[ a 0 ] , [ a 0 , a 1 ] , . . . , [ a 0 , . . . , a n ]] Anticausal stream functions Input streams with present and future but no past ( ∼ DA = A N = A × A N ) Functor:

  9. Comonads for attribute grammars An attribute grammar is a CF grammar augmented with attributes and semantic equations. Example: preorder numbering of the nodes S ℓ − → E S b S b L S b − → R S b S b . numin + 1 L . numin = S b S b = L . numout + 1 R . numin S ℓ . numout S ℓ . numin = S b . numout S b = R . numout Tree functions where the output at a position depends on the input at that position and around it (synthesized, inherited attributes).

  10. Comonads for attribute grammars Purely synthesized AG-s Functor: DA = Tree A = µ X . A × ( 1 + X × X ) Counit: ε A : Tree A → A ( a , s ) �→ a Comultiplication: : Tree A → Tree ( Tree A ) δ A � ( t , inl ( ∗ )) , if t = ( a , inl ( ∗ )) δ A ( t ) = ( t , inr ( δ A ( t 1 ) , δ A ( t 2 )) , if t = ( a , inr ( t 1 , t 2 ))

  11. Comonads for attribute grammars General AG-s DA = ( 2 × Tree A ) ∗ × Tree A Functor: Path structure from the root to the focus and the local tree below the focus

  12. Pre-[Cartesian closed] co-Kleisli categories Extending a pure language (the lambda calculus) with coeffect-constructs, we want the old constructs to remain and not to change their meaning too much. If D is a comonad on a Cartesian closed category C , how much of that structure carries over to CoKl ( D ) ? Products A × D B = df A × B π D = df π 0 ◦ ε 0 π D = df π 1 ◦ ε 1 � k 0 , k 1 � D = df � k 0 , k 1 �

  13. � � � � Pre-[Cartesian closed] co-Kleisli categories For (pre-)exponents we need some extra structure on a comonad: � D π 0 , D π 1 � D (( DA ⇒ B ) × A ) D (( DA ⇒ B )) × DA ev D ( ε × id ) ev ( DA ⇒ B ) × DA B k � C ? k � D ( A × B ) � C D ( A × B ) DA × DB Λ D ( k ) � DB ⇒ C Λ( k ◦ ?) � DB ⇒ C DA DA

  14. Pre-[Cartesian closed] co-Kleisli categories Definition A comonad D on a [symmetric] [semi]monoidal cat. C is said to be {lax/strong} [symmetric] [semi]monoidal, if it comes with a nat. {transf./iso.} m : DA ⊗ DB → D ( A ⊗ B ) [and a nat. {transf./iso.} e : I → DI ] behaving well wrt. α, [ l , r , ] [ γ, ] ε, δ . Pre-exponents Let D be a comonad on a Cartesian closed cat. C . Assuming that D that is a {lax/strong} [symmetric] [semi]monoidal wrt. the ( 1 , × ) symmetric monoidal structure on C , define this structure on A ⇒ D B CoKl ( D ) : = df DA ⇒ B ev D = df ev ◦ � ε ◦ D π 0 , D π 1 � Λ D ( k ) = df Λ( k ◦ m )

  15. Pre-[Cartesian closed] co-Kleisli categories If D is strong monoidal, then C ⇒ D − is right adjoint to − × D C and hence ⇒ D is an exponent functor: D ( A × C ) → B DA × DC → B DA → DC ⇒ B However, this seems rare in computational applications, DA = A N being an atypical example. Strong symmetric monoidal structure on streams A N × B N → ( A × B ) N : m ( α, β ) �→ λ n . ( α ( n ) , β ( n ))

  16. Pre-[Cartesian closed] co-Kleisli categories More common is that a comonad is lax symmetric semimonoidal, eg DA = A + , DA = A N × N . Lax symmetric semimonoidal structure on − N × N ( A N × N ) × ( B N × N ) → ( A × B ) N × N : m (( α, k 1 ) , ( β, k 2 ) �→ ( λ n . ( α ( n ) , β ( n )) , k 1 ) Then it suffices to have m satisfying m ◦ ∆ = D ∆ , where ∆ = � id , id � : A → A × A is the semicomonoid structure on the objects of C , to get that ⇒ D is a weak exponent functor.

  17. Comonadic semantics Comonadic semantics is obtained by interpreting the lambda-calculus into CoKl ( D ) in the standard way. Comonadic semantics � A � D × D � B � D � A � D × � B � D � A × B � D = df = � A � D ⇒ D � B � D D � A � D ⇒ � B � D � A ⇒ B � D = df = � ( x ) x i � D π D = df = π i ◦ ε i 0 ◦ D � ( x ) t � D � ( x ) fst ( t ) � D π D π 0 ◦ � ( x ) t � D = df = 1 ◦ D � ( x ) t � D � ( x ) snd ( t ) � D π D π 1 ◦ � ( x ) t � D = df = � ( x )( t 0 , t 1 ) � D � � ( x ) t 0 � D , � ( x ) t 1 � D � D = df � � ( x ) t 0 � D , � ( x ) t 1 � D � = ev D ◦ D � � ( x ) t � D , � ( x ) u � D � D � ( x ) t u � D = df ev ◦ � � ( x ) t � D , ( � ( x ) u � D ) † � = Λ( � ( x , x ) t � D ◦ m ) � ( x ) λ xt � D Λ D ( � ( x , x ) t � D ) = df = Coeffect-specific constructs are interpreted specifically.

  18. Comonadic semantics x : C ⊢ t : A implies � ( x ) t � D : � C � D → D � A � D , but not all equations of the lambda-calculus are validated. Closed terms: Type soundness for ⊢ t : A says that � t � D : 1 → D � A � D , i.e., D 1 → � A � D , so closed terms are evaluated relative to a coeffect over 1. In case of general or causal stream functions, this is a list over 1, the time from the start. If D is properly (symmetric) monoidal (e.g., ( − ) N ), we have a canonical choice e : 1 → D 1. Comonadic dataflow language semantics: The first-order language agrees perfectly with Lucid and Lustre by its semantics. The meaning of higher-order dataflow computation has been unclear. We get a neat semantics from mathematical considerations (cf. Colaço, Pouzet’s design with two flavors of function spaces).

  19. � � � � � � � � � � � Distributive laws Definition A distributive law of a monad ( T , η, µ ) over a comonad ( D , ε, δ ) is a natural transformation λ A : DTA → TDA st. λ λ DTA TDA DTA TDA � � � � � T ε � δ T δ � ε � � D λ � DTDA λ � TD 2 A TA D 2 TA λ T λ � T 2 DA DT 2 A DA TDTA � � � η � � D η µ D µ � � � � λ � TDA λ � TDA DTA DTA

  20. Distributive laws Clocked dataflow computation (partial-stream functions) TA = 1 + A A + = DA ( 1 + A ) + → 1 + A + λ : � inl ( ∗ ) if last ( as ) = inl ( ∗ ) as �→ inr ([ a i | inr ( a i ) ← as ]) otherwise

  21. Distributive laws BiKleisli category Given a monad T and comonad D with a distributive law λ : DTA → TDA , the biKleisli category BiKl ( T , D ) is defined as: | BiKl ( T , D ) | = df |C| BiKl ( T , D )( A , B ) = df C ( DA , TB ) id D , T = df η ◦ ε ℓ ⋆ ◦ λ ◦ k † ℓ ◦ D , T k = df

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend