Excerpts from Roman Cheplyaka's message of Sat Dec 08 14:00:52 -0800 2012:
> * Edward Z. Yang <ezy...@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)

Combine this with the monad associativity law:

    (tell w1 >> tell w2) >> tell w3 === tell w1 >> (tell w2 >> tell w3)

And it's easy to see that '<>' must be associative in order for this law
to be upheld.  Additionally, the existence of identities in monads means
that there must be a corresponding identity for the monoid.

So anything that is "writer-like" and also satisfies the monad laws...
is going to be a monoid.

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".

Edward

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to