dataflow computation tree transformations and comonads
play

Dataflow computation, tree transformations and comonads Tarmo - PowerPoint PPT Presentation

Dataflow computation, tree transformations and comonads Tarmo Uustalu, Tallinn Joint work with Varmo Vene, Tartu LogInf 14, Braunschweig, 2324 November 2007 Dataflow computation Dataflow computations = discrete-time signal transformations


  1. Dataflow computation, tree transformations and comonads Tarmo Uustalu, Tallinn Joint work with Varmo Vene, Tartu LogInf 14, Braunschweig, 23–24 November 2007

  2. Dataflow computation Dataflow computations = discrete-time signal transformations = stream functions. The output value at a time instant (stream position) is determined by the input value at the same instant (position) plus further input values. Example dataflow programs pos = 0 fby (pos + 1) sum x = x + (0 fby (sum x )) fact = 1 fby (fact ∗ (pos + 1)) fibo = 0 fby (fibo + (1 fby fibo)) pos 0 1 2 3 4 5 6 . . . sum pos 0 1 3 6 10 15 21 . . . fact 1 1 2 6 24 120 720 . . . fibo 0 1 1 2 3 5 8 . . .

  3. Tree transformations

  4. Context-dependent computation Common to both dataflow computation and tree transformations is computation in a datastructure. The shape of the datastructure is kept, the computation at every node is by the same rule, local, but context-dependent.

  5. This talk Thanks to Moggi’s work of late 1980s, it is now standard to analyse different notions of effectful computation in terms Kleisli categories of strong monads. Brookes, Geva and Stone, early 1990s: coKleisli categories of “computational” comonads for “intensional semantics”. We add: CoKleisli categories of symmetric (semi)monoidal comonads are a setting to analyse notions of context-dependent computation such as dataflow computation and tree transformations.

  6. Outline Minimal recap: monads, strong monads and effectful computation, semantics of effectful languages Comonads, symmetric monoidal comonads and context-dependent computation, semantics of context-dependent languages: “dualizing” the project Examples: dataflow computation, attribute evaluation

  7. � � � � Monads A monad on a category C is given by a functor T : C → C (the underlying functor ), . a natural transformation η : Id C → T (the unit ), . a natural transformation µ : TT → T (the multiplication ) satisfying these conditions: η TA � µ TA � TA TTA TTTA TTA � � � � � � � � � µ A µ A T η A � T µ A � � � � � � � � � TA � TA TTA TTA µ A µ A This definition says that ( T , η, µ ) is a monoid in the endofunctor category [ C , C ].

  8. Kleisli category of a monad A monad T on a category C induces a category Kl ( T ) called the Kleisli category of T defined by an object is an object of C , a map of from A to B is a map of C from A to TB , η A id T A = df A − → TA , if k : A → T B , ℓ : B → T C , then ℓ ◦ T k = df A k ℓ ⋆ − → TB − → TC where ℓ ⋆ = df TB T ℓ µ C − → TTC − → TC . From C there is an identity-on-objects inclusion functor J to Kl ( T ), defined on maps by if f : A → B , then f η B η A Tf Jf = df A − → B − → TB = A − → TA − → TB . J has a right adjoint U : Kl ( T ) → C , defined by UA = df TA , if k : A → T B , then Uk = df TA k ⋆ − → TB . Importantly UJ = T .

  9. Effectful computation Think of C as the category of pure functions and of TA as the type of effectful computations of values of a type A . Kl ( T ) is then the category of effectful functions. η A : A → TA is the identity function on A viewed as trivially effectful. Jf : A → TB is a general pure function f : A → B viewed as trivially effectful. µ A : TTA → TA flattens an effectful computation of an effectful computation. k ⋆ : TA → TB is an effectful function k : A → TB extended into one that can input an effectful computation.

  10. Examples Coproduct monad, for exceptions: TA = df A + E where E is some object (of exceptions), inl η A = df A − → A + E , [id , inr] µ A = df ( A + E ) + E − → A + E . Product monad, for output: TA = df A × E where ( E , e , m ) is some monoid (of output traces), e.g., the type of lists of a fixed element type with nil and append, ur → A × 1 id × e η A = df A − − → A × E , a → A × ( E × E ) id × m µ A = df ( A × E ) × E − − → A × E . Etc.

  11. � � � � � Strong functors A strong functor on a monoidal category ( C , I , ⊗ ) is given by an endofunctor F on C , together with a natural transformation sl A , B : A ⊗ FB → F ( A ⊗ B ) (the strength ) satisfying sl I , A � F ( I ⊗ A ) I ⊗ FA ul FA F ul A FA FA sl A ⊗ B , C ( A ⊗ B ) ⊗ FC F (( A ⊗ B ) ⊗ C ) a A , B , FC F a A , B , C � A ⊗ F ( B ⊗ C ) sl A , B ⊗ C � F ( A ⊗ ( B ⊗ C )) A ⊗ ( B ⊗ FC ) id A ⊗ sl B , C

  12. � � A strong natural transformation between two strong functors ( F , sl), ( G , sl ′ ) is a natural transformation . τ : F → G satisfying sl A , B � F ( A ⊗ B ) A ⊗ FB τ A ⊗ B id A ⊗ τ B � G ( A ⊗ B ) A ⊗ GB sl ′ A , B

  13. � � � � Strong monads A strong monad on a monoidal category ( C , I , ⊗ ) is a monad ( T , η, µ ) together with a strength sl for T for which η and µ are strong, i.e., satisfy A ⊗ B A ⊗ B η A ⊗ B id A ⊗ η B � T ( A ⊗ B ) A ⊗ TB sl A , B sl A , TB � T sl A , B � TT ( A ⊗ B ) A ⊗ TTB T ( A ⊗ TB ) µ A ⊗ B id A ⊗ µ B � T ( A ⊗ B ) A ⊗ TB sl A , B (Id is canonically strong and, if F , G are strong, then GF is canonically strong.) Every monad on Set is (uniquely) strong.

  14. Kleisli categories and Cartesian closed structure Extending simply typed lambda-calculus with effect-constructs, we want the pure constructs not to change their meaning too much. For a (1 , × ) strong monad T on a Cartesian closed category C , we can define a “pre-(Cartesian closed)” structure on Kl ( T ): 1 T = df 1 ! T = df ! A × T B A ⇒ T B = df A × B = df A ⇒ TB π T ev T = df η ◦ π 0 = df ev 0 π T = df η ◦ π 1 1 sr ⋆ ◦ sl ◦ � k 0 , k 1 � � k 0 , k 1 � T Λ T ( k ) = df = df η ◦ Λ( k ) It satisfies the typings required from a Cartesian closed structure, but . . . not all coherence conditions generally.

  15. (1 T , × T ) define a symmetric premonoidal structure on Kl ( T ). In particular, this means that × T is only a functor in each argument separately. More exactly, Kl ( T ) together with J : C → Kl ( T ) is a Freyd category on C . It is not the case either that A ⇒ T − : Kl ( T ) → Kl ( T ) is right adjoint to − × T A : Kl ( T ) → Kl ( T ). However, A ⇒ T ( U − ) : Kl ( T ) → C is right adjoint to J ( − × A ) : C → Kl ( T ). J ( C × A ) → T B C → A ⇒ T ( UB ) If K is a Freyd category on a Cartesian category C and J ( − × A ) has a right adjoint, then K is the Kleisli category of a strong monad on C .

  16. Kleisli semantics We interpret simply typed lambda-calculus into Kl ( T ) in the standard way, using its pre-(Cartesian closed) structure and getting � K � T = df an object of Kl ( T ) = that object of C � 1 � T 1 T = df = 1 � A � T × T � B � T � A × B � T = df = � A � T × � B � T � A � T ⇒ T � B � T � A ⇒ B � T = df = � A � T ⇒ T � B � T � C 0 � T × T . . . × T � C n − 1 � T � C � T = df = � C 0 � T × . . . × � C n − 1 � T

  17. � ( x ) x i � T π T = df i = η ◦ π i � ( x , x ) u � T ◦ T � id T , � ( x ) t � T � T � ( x ) let x ← t in u � T = df = ( � ( x , x ) u � T ) ⋆ ◦ sl ◦ � id , � ( x ) t � T � ! T � ( x ) () � T = df = ! fst T ◦ T � ( x ) t � T � ( x ) fst ( t ) � T = df = T fst ◦ � ( x ) t � T snd T ◦ T � ( x ) t � T � ( x ) snd ( t ) � T = df = T snd ◦ � ( x ) t � T � ( x ) ( t 0 , t 1 ) � T � � ( x ) t 0 � T , � ( x ) t 1 � T � T = df = sl ⋆ ◦ sr ◦ � � ( x ) t 0 � T , � ( x ) t 1 � T � � ( x ) λ xt � T Λ T ( � ( x , x ) t � T ) = df = η ◦ Λ( � ( x , x ) t � T ) ev T ◦ T � � ( x ) t � T , � ( x ) u � T � T � ( x ) t u � T = df = ev ⋆ ◦ sl ⋆ ◦ sr ◦ � � ( x ) t � T , � ( x ) u � T �

  18. Constructs specific to a particular notion of effect are interpreted specifically. We have well-definedness / soundness of typing: x : C ⊢ t : A implies � ( x ) t � T : � C � T → T � A � T . But not all equations of simply-typed lambda calculus are validated by this interpretation. For pure terms � ( x ) t � T = J � ( x ) t � , so the coKleisli semantics is conservative over the pure semantics.

  19. � � � � � � Comonads Comonads are the dual of monads. A comonad is a functor D : C → C (the underlying functor ), . a natural transformation ε : D → Id C (the counit ), . a natural transformation δ : D → DD (the comultiplication ) satisfying these conditions: δ A δ A DA DDA DA DDA � � � � � � � � � δ A � D ε A δ A D δ A � � � � � � � � � DA � DDDA DDA DDA ε DA δ DA In other words, a comonad is a comonoid in [ C , C ] (a monoid in [ C , C ] op ).

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