[Haskell-cafe] Re: class default method proposal
apfelmus
apfelmus at quantentunnel.de
Thu Dec 13 04:30:37 EST 2007
Luke Palmer wrote:
> Isn't a type which is both a Monad and a Comonad just Identity?
>
> (I'm actually not sure, I'm just conjecting)
Good idea, but it's not the case.
data L a = One a | Cons a (L a) -- non-empty list
-- standard list monad
instance Monad L where
return = One
-- join concatenates all lists in the list
join (One y) = y
join (Cons (One x) ys) = Cons x (join ys)
join (Cons (Cons x xs) ys) = Cons x (join (Cons xs ys))
class Comonad c where
counit :: c a -> a
cobind :: c a -> (c a -> b) -> c b
instance Comonad L where
counit (One x) = x -- that's why we insist on non-emptiness
counit (Cons x _) = x
-- f has access to the whole past
cobind ys@(One x) f = One (f ys)
cobind ys@(Cons x xs) f = Cons (f ys) (cobind f xs)
Checking the laws is left as an exercise for the reader :)
Regards,
apfelmus
More information about the Haskell-Cafe
mailing list