[Haskell-cafe] evaluation semantics of bind

Gregg Reynolds dev at mobileink.com
Thu Feb 5 10:20:06 EST 2009

I think I've just about got monads figured out, but there's one detail that
still escapes me.  As I understand it, a monad is a kind of programming
trick the uses data dependency to force evaluation order.  x >>= f means
apply f to x; since the value of f x depends on the value of x, the
evaluator must evaluate x before f x. However, consider:

    getChar >>= \x -> getChar

An optimizer can see that the result of the first getChar is discarded and
replace the entire expression with one getChar without changing the formal
semantics.  But that would change the behavior, so to get the desired
behavior, there must be some principle that prevents this from happening,
ensuring that x >>= f always evaluates f x.

I can see that the monad laws ensure this But I haven't found anything that
states this.  Am I missing something?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090205/f57d25a6/attachment.htm

More information about the Haskell-Cafe mailing list