
Alberto G. Corona wrote:
forwarded:
Yes! if no state is passed, the optimization makes sense and the term is not executed, like any lazy evaluation. For example, I used the debugger (that is, without optimizations) to verify it with the Maybe monad: op x= x+x
print $ Just (op 1) >>= \y-> return (Just 2)
does not evaluate op 1
Presumably you mean?: print $ Just (op 1) >>= \y-> return 2
but
print $ Just (op 1) >>= \y-> return y
does execute it.
Dashing off towards the White Knight, we should be careful what is said here. If we take only the expression "Just (op 1) >>= \y-> return y" then evaluating it yields "Just (op 1)". That is, it only evaluates to WHNF and does not evaluate what's inside. It is only once this value is subsequently handed off to print or some other function, that it may become evaluated. Similarly with the first example as originally written. It so happens that bind is non-strict for the field in Just, so we can discard the "op 1". However, according to the semantics we do not evaluate "Just 2" either; we only need to evaluate the return which will produce Just and pass the operand down. (Regardless of the fact that the value yielded by applying Just to 2 is Just 2. Expressions and their denotations are different.) -- Live well, ~wren