the essence of dataflow programming
play

The essence of dataflow programming Interpreters Comonadic - PowerPoint PPT Presentation

Essence of DFP Varmo Vene Introduction Monadic The essence of dataflow programming Interpreters Comonadic Interpreters Comonads Tarmo Uustalu 1 Varmo Vene 2 Stream Functions Comonadic Interpreters Distributive Interpreters 1 Institute of


  1. Essence of DFP Varmo Vene Introduction Monadic The essence of dataflow programming Interpreters Comonadic Interpreters Comonads Tarmo Uustalu 1 Varmo Vene 2 Stream Functions Comonadic Interpreters Distributive Interpreters 1 Institute of Cybernetics Distributive Laws Tallinn University of Technology Distributive Interpreters Conclusions 2 Department of Computer Science University of Tartu WG2.8 workshop, Kalvi, 1-4 September 2005

  2. Motivation Essence of DFP Varmo Vene Moggi and Wadler showed that effectful computations Introduction can be structured with monads. Monadic Interpreters An effect-producing function from A to B is a map Comonadic A → B in the Kleisli category, i.e., a map A → TB in Interpreters the base category. Comonads Stream Functions Some examples applied in semantics: Comonadic Interpreters Distributive TA = A , the identity monad, Interpreters TA = Maybe A = A + 1, error (partiality) Distributive Laws Distributive Interpreters TA = A + E , exceptions, Conclusions TA = E ⇒ A , environment, TA = List A = µ X . 1 + A × X , non-determinism, TA = S ⇒ A × S , state, TA = ( A ⇒ R ) ⇒ R , continuations.

  3. Motivation Essence of DFP Varmo Vene However, there are several impure features which are Introduction not captured by monads. Monadic Interpreters But what about dataflow languages such as Lucid Comonadic (general stream functions) or Lustre or Lucid Synchrone Interpreters (causal stream functions)? Comonads Stream Functions Comonadic Interpreters Hughes proposed arrow types (also known under the Distributive name of Freyd categories ) as a means to structure Interpreters Distributive Laws stream-based computations. Distributive Interpreters Conclusions But what about comonads? They have not found extensive use (some examples by Brookes and Geva, Kieburtz, but mostly artificial).

  4. This talk Essence of DFP Varmo Vene Comonads provide the right level of abstraction to Introduction organize dataflow computation. Monadic Interpreters In particular, we can define a generic comonadic Comonadic semantics for dataflow(ish) languages, similar to Interpreters Moggi’s generic monadic semantics of languages for Comonads Stream Functions computation with effects. Comonadic Interpreters Distributive Interpreters Distributive Laws Distributive Interpreters Conclusions

  5. Outline Essence of DFP Varmo Vene Introduction Monadic Interpreters 1 Monadic Interpreters Comonadic Interpreters Comonadic Interpreters 2 Comonads Comonads Stream Functions Comonadic Interpreters Comonads for Stream Functions Distributive Interpreters Comonadic Interpreters Distributive Laws Distributive Interpreters Conclusions Distributive Interpreters 3 Distributive Laws Distributive Interpreters Conclusions 4

  6. Monadic Interpreters Essence of DFP Varmo Vene Syntax: Introduction type Var = String Monadic Interpreters data Tm = V Var | L Var Tm | Tm :@ Tm Comonadic Interpreters | N Int | Tm :+ Tm | ... Comonads Stream Functions | TT | FF | Not Tm | ... Comonadic Interpreters | If Tm Tm Tm Distributive | ... Interpreters Distributive Laws -- specific for Maybe Distributive Interpreters | Raise | Tm ‘Handle‘ Tm Conclusions

  7. Monadic Interpreters Essence of DFP Varmo Vene Semantic categories: Introduction data Val t = I Int | B Bool Monadic Interpreters | F (Val t -> t (Val t)) Comonadic Interpreters type Env t = [(Var, Val t)] Comonads Stream Functions Comonadic Interpreters empty :: [(a, b)] Distributive empty = [] Interpreters Distributive Laws Distributive Interpreters update :: a -> b -> [(a, b)] -> [(a, b)] Conclusions update a b abs = (a, b) : abs unsafeLookup :: Eq a => a -> [(a, b)] -> b unsafeLookup a0 ((a, b): abs) | a0 == a = b | otherwise = unsafeLookup a0 abs

  8. Monadic Interpreters Essence of DFP Varmo Vene Evaluation: Introduction class Monad t => MonadEv t where Monadic Interpreters ev :: Tm -> Env t -> t (Val t) Comonadic Interpreters _ev :: MonadEv t => Tm -> Env t -> t (Val t) Comonads Stream Functions _ev (V x) env = return (unsafeLookup x env) Comonadic Interpreters _ev (e :@ e’) env Distributive = ev e env >>= \ (F f) -> Interpreters Distributive Laws ev e’ env >>= \ a -> Distributive Interpreters f a Conclusions _ev (L x e) env = return (F (\ a -> ev e (update x a env))) _ev (N n) env = return (I n) _ev (e0 :+ e1) env = ev e0 env >>= \ (I n0) -> ev e1 env >>= \ (I n1) -> return (I (n0 + n1)) ...

  9. Monadic Interpreters Essence of DFP Varmo Vene Standard evaluator: Introduction instance MonadEv Id where Monadic Interpreters ev e env = _ev e env Comonadic Error handling evaluator: Interpreters Comonads instance MonadEv Maybe where Stream Functions ev Raise env = raise Comonadic Interpreters Distributive ev (e0 ‘Handle‘ e1) env Interpreters = ev e0 env ‘handle‘ ev e1 env Distributive Laws Distributive Interpreters ev (e0 ‘Div‘ e1) env Conclusions = ev e0 env >>= \ (I n0) -> ev e1 env >>= \ (I n1) -> if n1 == 0 then raise else return (I (n0 ‘div‘ n1)) ev (e0 ‘Mod‘ e1) env = ... ev e env = _ev e env

  10. Outline Essence of DFP Varmo Vene Introduction Monadic Interpreters 1 Monadic Interpreters Comonadic Interpreters Comonadic Interpreters 2 Comonads Comonads Stream Functions Comonadic Interpreters Comonads for Stream Functions Distributive Interpreters Comonadic Interpreters Distributive Laws Distributive Interpreters Conclusions Distributive Interpreters 3 Distributive Laws Distributive Interpreters Conclusions 4

  11. Comonads Essence of DFP Varmo Vene Comonads model notions of value in a context; Introduction DA is the type of contextually situated values of A . Monadic Interpreters A context-relying function from A to B is a map A → B in the coKleisli category, Comonadic Interpreters i.e., a map DA → B in the base category. Comonads Stream Functions Some examples: Comonadic Interpreters Distributive DA = A , the identity comonad, Interpreters DA = A × E , the product comonad, environment, Distributive Laws Distributive Interpreters DA = Str A = ν X . A × X , the streams comonad. Conclusions

  12. Comonads in Haskell Essence of DFP Varmo Vene The comonad class: Introduction class Comonad d where Monadic counit :: d a -> a Interpreters cobind :: (d a -> b) -> d a -> d b Comonadic Interpreters Derived operations: Comonads Stream Functions cmap :: Comonad d => (a -> b) -> d a -> d b Comonadic Interpreters cmap f x = cobind (f . counit) x Distributive Interpreters Distributive Laws cdup :: Comonad d => d a -> d (d a) Distributive Interpreters cdup = cobind id Conclusions The identity comonad: instance Comonad Id where counit (Id a) = a cobind k d = Id (k d)

  13. Comonads in Haskell Essence of DFP Varmo Vene The product comonad: Introduction data Prod e a = a :& e Monadic Interpreters instance Comonad (Prod e) where Comonadic Interpreters counit (a :& _) = a Comonads cobind k d@(_ :& e) = k d :& e Stream Functions Comonadic Interpreters Operations specific for product comonad: Distributive Interpreters askP :: Prod e a -> e Distributive Laws askP (_ :& e) = e Distributive Interpreters Conclusions localP :: (e -> e) -> Prod e a -> Prod e a localP g (a :& e) = (a :& g e)

  14. Comonads in Haskell Essence of DFP Varmo Vene The streams comonad: Introduction data Stream a = a :< Stream a -- coinductive Monadic Interpreters instance Comonad Stream where Comonadic Interpreters counit (a :< _) = a Comonads cobind k d@(_ :< as) = k d :< cobind k as Stream Functions Comonadic Interpreters Operations specific for streams comonad: Distributive Interpreters fbyS :: a -> Stream a -> Stream a Distributive Laws fbyS a as = a :< as Distributive Interpreters Conclusions nextS :: Stream a -> Stream a nextS (a :< as) = as

  15. Outline Essence of DFP Varmo Vene Introduction Monadic Interpreters 1 Monadic Interpreters Comonadic Interpreters Comonadic Interpreters 2 Comonads Comonads Stream Functions Comonadic Interpreters Comonads for Stream Functions Distributive Interpreters Comonadic Interpreters Distributive Laws Distributive Interpreters Conclusions Distributive Interpreters 3 Distributive Laws Distributive Interpreters Conclusions 4

  16. Comonads for Stream Functions Essence of DFP Varmo Vene Streams model signals in discrete time. Introduction They are naturally isomorphic to functions from natural Monadic Interpreters numbers: Str A ∼ Comonadic = Nat ⇒ A Interpreters Comonads Haskell implementation of the isomorphism: Stream Functions Comonadic Interpreters str2fun :: Stream a -> Int -> a Distributive Interpreters str2fun (a :< as) 0 = a Distributive Laws str2fun (a :< as) (i + 1) = str2fun as i Distributive Interpreters Conclusions fun2str :: (Int -> a) -> Stream a fun2str f = fun2str’ f 0 where fun2str’ f i = f i :< fun2str’ f (i + 1)

Recommend


More recommend