Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Every monad is monoid?

Since every Monad is a Monoid on the sequencing operation. Why doesn't Monad inherit Monoid in haskell?

like image 782
vinothkr Avatar asked May 14 '13 16:05

vinothkr


1 Answers

It doesn't have to be a Monad even, this works for every Applicative. So yes, you could define:

class (Functor f, Monoid (f ())) => Applicative f where

But this means that you would have to provide the Monoid instance every time you write an Applicative instance. That can be quite annoying, certainly since this Monoid instance would not be used very often.

A better solution is to create a newtype wrapper around f (), and then you can provide a Monoid instance for all applicative functors once and for all. There's one readily available in the reducers package.

like image 154
Sjoerd Visscher Avatar answered Oct 15 '22 00:10

Sjoerd Visscher