[Haskell-cafe] evaluation semantics of bind

Alberto G. Corona agocorona at gmail.com
Wed Feb 11 05:31:16 EST 2009

Yes,  Just must be executed because by the very definition of bind for the
Maybe mondad,

         (>>=) Nothing f = Nothing
         (>>=) (Just x) f = f x

He need to know if the value injected is Just or   Nothing, but anyway, my
point is : it is just plain lazy functional code!. No magic inside.
everything depend on the definition of bind.

2009/2/11 wren ng thornton <wren at freegeek.org>
> 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
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090211/d8ae0eaf/attachment.htm

More information about the Haskell-Cafe mailing list