instance Monoid Monad

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.

Tags:

Haskell

Monads