Your SlideShare is downloading. ×
0
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Data type a la carte
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Data type a la carte

256

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
256
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. datatypesa La Carte 08 25 2011 Yun-Yan Chi
  • 2. outline• modular syntax • signature functor • recursion • functor combination• modular semantics • fold for type Fix • evaluation-algebra
  • 3. outline• modular syntax • signature functor • recursion • functor combination• modular semantics • fold for type Fix • evaluation-algebra
  • 4. flatten a recursive datatype:data Term = Val Int | Add Term Term | Throw | Catch Term Term
  • 5. flatten a recursive datatype:data Term = Val Int | Add Term Term | Throw | Catch Term Term | If Bool Term Term
  • 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. 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. 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. the “recursive”: (Fix F = F(Fix F ) ) data Fix f = In (f (Fix f))
  • 10. the “recursive”: (Fix F = F(Fix F ) ) data Fix f = In (f (Fix f))example : (In $ Val 2) :: Fix Arith
  • 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. the “recursive”: (Fix F = F(Fix F ) ) data Fix f = In (f (Fix f))example : In $ Add (In $ Throw) (In $ Val 2) :: ???
  • 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. 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. 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. 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. 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. outline• modular syntax • signature functor • recursion • functor combination• modular semantics • fold for type Fix • evaluation-algebra
  • 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. fold for type Fix a T FT([ f ]) F([ f ]) A FA f
  • 21. fold for type Fix a (Fix F) F(Fix F)([ f ]) F([ f ])m Value F(m Value) f
  • 22. fold for type Fix (pattern match) (Fix F) F(Fix F)fold f fmap (fold f )m Value F(m Value) f
  • 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. 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. 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. 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. 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. 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. 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. evaluation-algebrainstance ExceptMonad m => EvalAlge Except m where evalAlge Throw = throw evalAlge (Catch x h) = x `catch` h
  • 31. evaluation-algebra m Value evalAlge evalAlgeArith (m Value) Except (m Value)
  • 32. evaluation-algebra m Value evalAlge evalAlge [evalAlge, evalAlge] evalAlgeArith (m Value) Except (m Value) inl inr Arith :+: Except
  • 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. 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. Question?

×