[Haskell-cafe] mtl: Why there is "Monoid w" constraint in the definition of class MonadWriter?

Petr P petr.mvd at gmail.com
Sat Dec 8 19:59:25 CET 2012


The class is defined as

> class (Monoid w, Monad m) => MonadWriter w m | m -> w where
>   ...

What is the reason for the Monoid constrait? It seems superfluous to me. I
recompiled the whole package without it, with no problems.


Of course, the Monoid constraint is necessary for most _instances_, like in

> instance (Monoid w, Monad m) => MonadWriter w (Lazy.WriterT w m) where
> ...

but this is a different thing - it depends on how the particular instance
is implemented.

I encountered the problem when I needed to define an instance where the
monoidal structure is fixed (Last) and I didn't want to expose it to the
user. I wanted to spare the user of of having to write Last/getLast
everywhere. (I have an instance of MonadWriter independent of WriterT, its
'tell' saves values to a MVar. Functions 'listen' and 'pass' create a new
temporary MVar. I can post the detail, if anybody is interested.)

Would anything break by removing the constraint? I think the type class
would get a bit more general this way.

  Thanks for help,
  Petr Pudlak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121208/2a7ec242/attachment.htm>


More information about the Haskell-Cafe mailing list