[Haskell-cafe] Re: A question about "monad laws"
apfelmus at quantentunnel.de
Mon Feb 11 10:14:34 EST 2008
Deokjae Lee wrote:
> Tutorials about monad mention the "monad axioms" or "monad laws". The
> tutorial "All About Monads" says that "It is up to the programmer to
> ensure that any Monad instance he creates satisfies the monad laws".
> The following is one of the laws.
> (x >>= f) >>= g == x >>= (\v -> f v >>= g)
> However, this seems to me a kind of mathematical identity. If it is
> mathematical identity, a programmer need not care about this law to
> implement a monad. Can anyone give me an example implementation of
> monad that violate this law ?
I will be mean by asking the following counter question:
x + (y + z) = (x + y) + z
is a mathematical identity. If it is a mathematical identity, a
programmer need not care about this law to implement addition + . Can
anyone give me an example implementation of addition that violates this law?
The only difference here is that the associative law for addition is
"obvious" to you, whereas the associative law for monads is not
"obvious" to you (yet). As Neil mentioned, maybe
can help to convince yourself that the associative law monads should be
In short, the reason for its obviousness is the interpretation of >>= in
terms of sequencing actions with side effects. The law is probably best
demonstration with its special case
x >> (y >> z) = (x >> y) >> z
In other words, it signifies that it's only the sequence of x,y,z and
not the nesting that matters.
More information about the Haskell-Cafe