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

Kim-Ee Yeoh ky3 at atamo.com
Sun Dec 9 00:16:48 CET 2012


> The only thing we can tell from the Monad laws is that that function f
should be associative.

That f is associative is a very small step away from f forming a monoid.
What about listen :: m a -> m (w, a)? What laws should it hold that are
compatible with those of the monad and those of tell? Reasoning about
listen is enough to force the existence of a monoid identity.

-- Kim-Ee



On Sun, Dec 9, 2012 at 5:41 AM, Roman Cheplyaka <roma at ro-che.info> wrote:

> * 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
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121209/1ce820da/attachment.htm>


More information about the Haskell-Cafe mailing list