[Haskell-cafe] Monads vs. monoids

Olaf Klinke olf at aatal-apotheke.de
Mon Jul 16 19:48:05 UTC 2018


Joachim Durchholz wrote:
>Nope, monoid is a special case of monad (the case where all input and
>output types are the same).
>(BTW monoid is associativity + neutral element. Not 100% sure whether
>monad's "return" qualifies as a neutral element, and my
>monoid-equals-monotyped-monad claim above may fall down if it is not.

For every monad M, the type M () is a monoid, with
mempty = return ()
x <> y = x >> y
Does every monoid arise this way? Yes: Since base-4.9 there is the monad 
instance
Monoid a => Monad ((,) a)
and of course a and (a,()) are isomorphic (disregarding bottoms).

Also, there is the famous tongue-in-cheek saying "monads are just monoids 
in the category of endofunctors" which is explained in numerous blog posts 
and Youtube videos.

The monoid M () above has funny properties. Specialize M = [] and obtain 
the monoid of natural numbers under multiplication. Since (>>) has the 
more general type
Monad m => m a -> m b -> m b
We have a monoid action of M () on any type M b. In case of lists, it 
multiplies elements: [(),()] >> xs doubles the number of every element of 
the list xs.

Olaf



More information about the Haskell-Cafe mailing list