[Haskell] MonadPlus vs. Monoid

Cale Gibbard cgibbard at gmail.com
Tue Jan 10 15:14:07 EST 2006


On 09/01/06, Twan van Laarhoven <twanvl at gmail.com> wrote:
> I was wondering, in the MonadPlus documentation it says that:
>    * mzero is the identity of mplus (and some extra conditions)
>    * mplus is an associative operation
> While for Monoid we have:
>    * mempty is identity of mappend
>    * mappend is an associative operation
>
> MonadPlus is of course a 'stronger' assertion. But why is not every
> instance of MonadPlus also an instance of Monoid?
>
>  > instance MonadPlus m => Monoid (m a) where
>  >      mempty  = mzero
>  >      mappend = mplus
>
> The only type that is an instance of both is [a]. But I see no reason
> why it there should not be a Monoid instance for other MonadPlus types.
> In particular, an instance for Maybe could be useful with a writer monad
> when you are only interested in the first result.
>

Good idea, I agree. The fact that it's not Haskell 98 doesn't seem to
be too much of a concern, seeing as the major use of Monoid right now
is in conjunction with the MonadWriter class, and that's not
expressible in Haskell 98 either.

The only trouble with this is that there may be a case where you have
a MonadPlus type which is a monoid in a way which is separate from the
way given above, and you want that instance instead. I can't really
think of a good example of that happening though.

 - Cale


More information about the Haskell mailing list