Skip to content

Instantly share code, notes, and snippets.

@tonymorris
Created October 17, 2012 23:12
Show Gist options
  • Save tonymorris/3908937 to your computer and use it in GitHub Desktop.
Save tonymorris/3908937 to your computer and use it in GitHub Desktop.
Tree Comonad
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