Created
October 17, 2012 23:12
-
-
Save tonymorris/3908937 to your computer and use it in GitHub Desktop.
Tree Comonad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Functor f => Extend f where | |
-- CoSelectMany | |
extend :: | |
(f a -> b) | |
-> f a | |
-> f b | |
-- CoFlatten? | |
duplicate :: | |
f a | |
-> f (f a) | |
duplicate = | |
extend id | |
class Extend f => Comonad f where | |
counit :: | |
f a | |
-> a | |
data Tree a = | |
Tree a [Tree a] | |
instance Functor Tree where | |
fmap f (Tree x y) = | |
Tree (f x) (map (fmap f) y) | |
instance Extend Tree where | |
extend f t@(Tree _ y) = | |
Tree (f t) (map (extend f) y) | |
instance Comonad Tree where | |
counit (Tree x _) = | |
x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment