Monad
is a monoid, however it's a monoid in a different way than, say, Integer
. I wonder if there is a way to write Monoid'
and Monad'
such that both Integer
and Monad'
can be expressed as instances of the same Monoid'
typeclass?
So, let's pick a particular way Integer
has a Monoid
instance Monoid Int where
zero = 0
plus = (+)
and now here's a Monad
Monoid
{-# LANGUAGE FlexibleInstances #-}
instance Monad m => Monoid (Kleisli m a a) where
zero = id
plus = (.)
and here's another
instance MonadPlus m => Monoid (m a) where
zero = mzero
plus = mplus
I'm not sure how to express the "Monad is a monoid in the category of endofunctors" formulation in Haskell offhand, however.
I'm turning my comment into an answer at Tikhon's request. This blog post shows how to unify Monad
and Monoid
under the same type class using kind polymorphism. This is slightly different from Tel's answer in that the monad is implemented as a monoid in the category of endofunctors, rather than a monoid in a Kleisli category.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With