Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
datatypesa La Carte  08 25 2011               Yun-Yan Chi
outline• modular syntax •   signature functor •   recursion •   functor combination• modular semantics •   fold for type F...
outline• modular syntax •   signature functor •   recursion •   functor combination• modular semantics •   fold for type F...
flatten a recursive      datatype:data Term = Val Int | Add Term Term           | Throw | Catch Term Term
flatten a recursive      datatype:data Term = Val Int | Add Term Term           | Throw | Catch Term Term           | If Bo...
flatten a recursive       datatype:data Term = Val Int | Add Term Term           | Throw | Catch Term Termdata Arith e = Va...
flatten a recursive       datatype:data Term = Val Int | Add Term Term           | Throw | Catch Term Term           | If B...
signature functor:data Arith e = Val Int | Add e edata Except e = Throw | Catch e einstance Functor Except where   fmap f ...
the “recursive”:    (Fix F = F(Fix F          )         )  data Fix f = In (f (Fix f))
the “recursive”:              (Fix F = F(Fix F                    )         )            data Fix f = In (f (Fix f))exampl...
the “recursive”:              (Fix F = F(Fix F                    )         )            data Fix f = In (f (Fix f))exampl...
the “recursive”:              (Fix F = F(Fix F                    )         )            data Fix f = In (f (Fix f))exampl...
the “recursive”:                    (Fix F = F(Fix F                          )         )                data Fix f = In (...
combining functor by             coproduct:           C     f               g         data (f :+: g) e = Inl (f e)        ...
combining functor by     coproduct:• In $ Val 2 :: Fix Arith• In $ Inl $ Val 2 :: Fix (Arith :+: g)• In $ Add (In $ Throw)...
combining functor by     coproduct:• In $ Val 2 :: Fix Arith• In $ Inl $ Val 2 :: Fix (Arith :+: g)• In $ Add (In $ Throw)...
combining functor by     coproduct:• In $ Val 2 :: Fix Arith• In $ Inl $ Val 2 :: Fix (Arith :+: g)• In $ Add (In $ Throw)...
outline• modular syntax •   signature functor •   recursion •   functor combination• modular semantics •   fold for type F...
why fold• A recursive datatype, (Fix f), represents a  expression language.• the semantics of such expression language  ha...
fold for type Fix              a          T        FT([ f ])                 F([ f ])          A        FA              f
fold for type Fix             a (Fix F)           F(Fix F)([ f ])                F([ f ])m Value            F(m Value)    ...
fold for type Fix           (pattern match) (Fix F)                     F(Fix F)fold f                          fmap (fold...
fold for type Fix               (pattern match) (Fix F)                         F(Fix F)fold f                            ...
fold for type Fix             (pattern match) (Fix F)                       F(Fix F)fold f                            fmap...
fold for type Fix             (pattern match) (Fix F)                         F(Fix F)fold f                              ...
fold for type Fix             (pattern match) (Fix F)                         F(Fix F)fold f                              ...
evaluation-algebra• fold :: Functor f => (f a → a) → Fix f → a• fold f = (In t) → f (fmap (fold f) t)• we need a (f a → a)...
evaluation-algebra                  eval-alge   m Value                       F(m Value)class (Monad m, Functor f) => Eval...
evaluation-algebrainstance Monad m => EvalAlge Arith m where   evalAlge (Val n) = return n   evalAlge (Add x y) = x >>= m ...
evaluation-algebrainstance ExceptMonad m =>   EvalAlge Except m where      evalAlge Throw = throw      evalAlge (Catch x h...
evaluation-algebra                         m Value              evalAlge             evalAlgeArith (m Value)              ...
evaluation-algebra                         m Value              evalAlge                                      evalAlge    ...
evaluation-algebrainstance (EvalAlge f m, EvalAlge g m) =>   EvalAlge (f :+: g) m where      evalAlge (Inl x) = evalAlge x...
evaluation• fold :: Functor f => (f a → a) → Fix f → a• evalAlge :: f (m Value) →(m Value)• eval :: (Functor f, Monad m) =...
Question?
Upcoming SlideShare
Loading in …5
×

Data type a la carte

769 views

Published on

  • Be the first to comment

  • Be the first to like this

Data type a la carte

  1. 1. datatypesa La Carte 08 25 2011 Yun-Yan Chi
  2. 2. outline• modular syntax • signature functor • recursion • functor combination• modular semantics • fold for type Fix • evaluation-algebra
  3. 3. outline• modular syntax • signature functor • recursion • functor combination• modular semantics • fold for type Fix • evaluation-algebra
  4. 4. flatten a recursive datatype:data Term = Val Int | Add Term Term | Throw | Catch Term Term
  5. 5. flatten a recursive datatype:data Term = Val Int | Add Term Term | Throw | Catch Term Term | If Bool Term Term
  6. 6. flatten a recursive datatype:data Term = Val Int | Add Term Term | Throw | Catch Term Termdata Arith e = Val Int | Add e edata Except e = Throw | Catch e e the Menu
  7. 7. flatten a recursive datatype:data Term = Val Int | Add Term Term | Throw | Catch Term Term | If Bool Term Termdata Arith e = Val Int | Add e edata Except e = Throw | Catch e e the Menudata Branch e = If Bool e e
  8. 8. signature functor:data Arith e = Val Int | Add e edata Except e = Throw | Catch e einstance Functor Except where fmap f Throw = Throw fmap f (Catch x g) = Catch (f x) (f g)instance Functor Arith where fmap f (Val n) = Val n fmap f (Add x y) = Add (f x) (f y)
  9. 9. the “recursive”: (Fix F = F(Fix F ) ) data Fix f = In (f (Fix f))
  10. 10. the “recursive”: (Fix F = F(Fix F ) ) data Fix f = In (f (Fix f))example : (In $ Val 2) :: Fix Arith
  11. 11. the “recursive”: (Fix F = F(Fix F ) ) data Fix f = In (f (Fix f))example : In $ (Add (In $ Val 1) (In $ Val 2)) :: Fix Arith
  12. 12. the “recursive”: (Fix F = F(Fix F ) ) data Fix f = In (f (Fix f))example : In $ Add (In $ Throw) (In $ Val 2) :: ???
  13. 13. the “recursive”: (Fix F = F(Fix F ) ) data Fix f = In (f (Fix f))example : In $ Add (In $ Throw) (In $ Val 2) :: ??? Couldnt match expected type `Arith with actual type `Except
  14. 14. combining functor by coproduct: C f g data (f :+: g) e = Inl (f e) | Inr (f e)A [f, g] B instance (Functor f, Functor g) => Functor (f :+: g) where inl inr fmap h (Inl x) = Inl (fmap h x) fmap h (Inr y) = Inr (fmap h y) A+B
  15. 15. combining functor by coproduct:• In $ Val 2 :: Fix Arith• In $ Inl $ Val 2 :: Fix (Arith :+: g)• In $ Add (In $ Throw) (In $ Val 2) :: ???• In $ Inl $ Add (In $ Inr $ Throw) (In $ Inl $ Val 2) :: Fix (Arith :+: Except)
  16. 16. combining functor by coproduct:• In $ Val 2 :: Fix Arith• In $ Inl $ Val 2 :: Fix (Arith :+: g)• In $ Add (In $ Throw) (In $ Val 2) :: ???• In $ Inl $ Add (In $ Inr $ Throw) (In $ Inl $ Val 2) :: Fix (Arith :+: Except)
  17. 17. combining functor by coproduct:• In $ Val 2 :: Fix Arith• In $ Inl $ Val 2 :: Fix (Arith :+: g)• In $ Add (In $ Throw) (In $ Val 2) :: ???• In $ Inl $ Add (In $ Inr $ Throw) (In $ Inl $ Val 2) :: Fix (Arith :+: Except)
  18. 18. outline• modular syntax • signature functor • recursion • functor combination• modular semantics • fold for type Fix • evaluation-algebra
  19. 19. why fold• A recursive datatype, (Fix f), represents a expression language.• the semantics of such expression language has type: Fix f → m Value• we need fold for type Fix
  20. 20. fold for type Fix a T FT([ f ]) F([ f ]) A FA f
  21. 21. fold for type Fix a (Fix F) F(Fix F)([ f ]) F([ f ])m Value F(m Value) f
  22. 22. fold for type Fix (pattern match) (Fix F) F(Fix F)fold f fmap (fold f )m Value F(m Value) f
  23. 23. fold for type Fix (pattern match) (Fix F) F(Fix F)fold f fmap (fold f )m Value F(m Value) ffold f = ...
  24. 24. fold for type Fix (pattern match) (Fix F) F(Fix F)fold f fmap (fold f )m Value F(m Value) ffold f = (In t) → t
  25. 25. fold for type Fix (pattern match) (Fix F) F(Fix F)fold f fmap (fold f )m Value F(m Value) ffold f = (In t) → fmap (fold f) $ t
  26. 26. fold for type Fix (pattern match) (Fix F) F(Fix F)fold f fmap (fold f )m Value F(m Value) ffold f = (In t) → f $ fmap (fold f) $ t
  27. 27. evaluation-algebra• fold :: Functor f => (f a → a) → Fix f → a• fold f = (In t) → f (fmap (fold f) t)• we need a (f a → a) as a parameter • f-algebra • evaluation-algebra
  28. 28. evaluation-algebra eval-alge m Value F(m Value)class (Monad m, Functor f) => EvalAlge f m where evalAlge :: f (m Value) →(m Value)
  29. 29. evaluation-algebrainstance Monad m => EvalAlge Arith m where evalAlge (Val n) = return n evalAlge (Add x y) = x >>= m -> y >>= n -> return (m+n)
  30. 30. evaluation-algebrainstance ExceptMonad m => EvalAlge Except m where evalAlge Throw = throw evalAlge (Catch x h) = x `catch` h
  31. 31. evaluation-algebra m Value evalAlge evalAlgeArith (m Value) Except (m Value)
  32. 32. evaluation-algebra m Value evalAlge evalAlge [evalAlge, evalAlge] evalAlgeArith (m Value) Except (m Value) inl inr Arith :+: Except
  33. 33. evaluation-algebrainstance (EvalAlge f m, EvalAlge g m) => EvalAlge (f :+: g) m where evalAlge (Inl x) = evalAlge x evalAlge (Inr y) = evalAlge y
  34. 34. evaluation• fold :: Functor f => (f a → a) → Fix f → a• evalAlge :: f (m Value) →(m Value)• eval :: (Functor f, Monad m) => Fix f → m Value• eval = fold evalAlg• here, f can be Arith, Except or (Arith :+: Except)
  35. 35. Question?

×