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

Holger Siegel holgersiegel74 at yahoo.de
Sun Dec 9 00:27:38 CET 2012


Am 08.12.2012 um 23:18 schrieb Edward Z. Yang:

> Excerpts from Roman Cheplyaka's message of Sat Dec 08 14:00:52 -0800 2012:
>> * Edward Z. Yang <ezyang at MIT.EDU> [2012-12-08 11:19:01-0800]
>>> The monoid instance is necessary to ensure adherence to the monad laws.
>> 
>> This doesn't make any sense to me. Are you sure you're talking about the
>> MonadWriter class and not about the Writer monad?

(...)

> Now, it's possible what GP is actually asking about is more a question of
> encapsulation.  Well, one answer is, "Well, just give the user specialized
> functions which do the appropriate wrapping/unwrapping"; another answer is,
> "if you let the user run a writer action and extract the resulting written
> value, then he can always reverse engineer the monoid instance out of it".

For deriving a monoid instance of w from monad  (Writer w), you will need
function execWriter:: Writer w a -> w, but in case of a general instance of
(MonadWriter w m) you would have to use function listen :: m a -> m (a, w)
that will only provide you a value of type (m w), but not of type w. Therefore,
I'm not yet convinced that every instance of (MonadWriter w m) gives rise
to a monoid instance of w.

Regards,

Holger




More information about the Haskell-Cafe mailing list