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

Roman Cheplyaka roma at ro-che.info
Sat Dec 8 23:41:51 CET 2012


* Edward Z. Yang <ezyang at MIT.EDU> [2012-12-08 14:18:38-0800]
> 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?
> 
> Well, I assume the rules for Writer generalize for MonadWriter, no?
> 
> Here's an example.  Haskell monads have the associativity law:
> 
>     (f >=> g) >=> h === f >=> (g >=> h)
> 
> From this, we can see that
> 
>     (m1 >> m2) >> m3 === m1 >> (m2 >> m3)
> 
> Now, consider tell. We'd expect it to obey a law like this:
> 
>     tell w1 >> tell w2 === tell (w1 <> w2)

First of all, I don't see why two tells should be equivalent to one
tell. Imagine a MonadWriter that additionally records the number of
times 'tell' has been called. (You might argue that your last equation
should be a MonadWriter class law, but that's a different story — we're
talking about the Monad laws here.)

Second, even *if* the above holds (two tells are equivalent to one
tell), then there is *some* function f such that

    tell w1 >> tell w2 == tell (f w1 w2)

It isn't necessary that f coincides with mappend, or even that the type
w is declared as a Monoid at all. The only thing we can tell from the
Monad laws is that that function f should be associative.

Roman



More information about the Haskell-Cafe mailing list