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?
Thanks,
gregg