
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
Excerpts from Roman Cheplyaka's message of Sat Dec 08 14:00:52 -0800 2012:
* Edward Z. Yang
[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
* Edward Z. Yang
[2012-12-08 14:18:38-0800] 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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe