
data Free a = Leaf a | Succ (Free a) deriving Show
instance Functor Free where
fmap f (Leaf x) = Leaf (f x)
fmap f (Succ y) = Succ (fmap f y)
instance Monad Free where
return a = Leaf a
Leaf x >>= f = f x
Succ y >>= f = Succ (y >>= f)
En generalement
data Free f a = Leaf a | Succ (f (Free f a)) instance (Functor f) => Monad (Free f) where return a = Leaf a Leaf x >>= g = g x Succ y >>= g = Succ $ fmap (>>= g) y
------- Free version of sequence seqFree :: (Monad m) => Free (m a) -> m (Free a) seqFree (Leaf x) = do y <- x return (Leaf y) seqFree (Succ x) = do y <- seqFree x return y
class NonAssMonoid a where
mappend :: a -> a -> a
data Tree a = Leaf a | Node (Tree a) (Tree a) deriving Show
instance NonAssMonoid (Tree a) where
x `mappend` y = Node x y