Re: [Haskell-cafe] evaluation semantics of bind

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
but
print $ Just (op 1) >>= \y-> return y
does execute it.
The trace of the first:
[1 of 1] Compiling Main ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> :set stop :list
*Main> :step main
Stopped at test.hs:4:6-43
_result :: IO () = _
3
4 main= print $ Just (op 1) >>= \y-> return 2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5
[test.hs:4:6-43] *Main> :step
Stopped at test.hs:4:14-43
_result :: Maybe Integer = _
3
4 main= print $ Just (op 1) >>= \y-> return 2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5
[test.hs:4:14-43] *Main> :step
Stopped at test.hs:4:14-24
_result :: Maybe Integer = _
3
4 main= print $ Just (op 1) >>= \y-> return 2
^^^^^^^^^^^
5
[test.hs:4:14-24] *Main> :step
Stopped at test.hs:4:35-43
_result :: Maybe Integer = _
3
4 main= print $ Just (op 1) >>= \y-> return 2
^^^^^^^^^
5
[test.hs:4:35-43] *Main> :step
Just 2
But in the second case op is executed:
*Main> :step main
Stopped at test.hs:4:6-43
_result :: IO () = _
3
4 main= print $ Just (op 1) >>= \y-> return y
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5
[test.hs:4:6-43] *Main> :step
Stopped at test.hs:4:14-43
_result :: Maybe Integer = _
3
4 main= print $ Just (op 1) >>= \y-> return y
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5
[test.hs:4:14-43] *Main> :step
Stopped at test.hs:4:14-24
_result :: Maybe Integer = _
3
4 main= print $ Just (op 1) >>= \y-> return y
^^^^^^^^^^^
5
[test.hs:4:14-24] *Main> :step
Stopped at test.hs:4:35-43
_result :: Maybe Integer = _
y :: Integer = _
3
4 main= print $ Just (op 1) >>= \y-> return y
^^^^^^^^^
5
[test.hs:4:35-43] *Main> :step
Just Stopped at test.hs:4:20-23
_result :: Integer = _
3
4 main= print $ Just (op 1) >>= \y-> return y
^^^^
5
[test.hs:4:20-23] *Main> :step
Stopped at test.hs:6:0-8
_result :: Integer = _
5
6 op x= x+x
^^^^^^^^^
7
[test.hs:6:0-8] *Main> :step
Stopped at test.hs:6:6-8
_result :: Integer = _
x :: Integer = _
5
6 op x= x+x
^^^
7
[test.hs:6:6-8] *Main> :step
Just 2
2009/2/5 Gregg Reynolds
On Thu, Feb 5, 2009 at 9:27 AM, Bulat Ziganshin
Hello Gregg,
Thursday, February 5, 2009, 6:20:06 PM, you wrote:
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.
this is prohibited by using pseudo-value of type RealWorld which is passed through entire action stream. actually, order of execution is controlled by dependencies on this values
Thanks. I actually read that a few weeks ago and forgot all about it. So
wrote: the gist is that type IO has special magic semantics. Formal, but hidden. Which means monad semantics are built in to the language, at least for that type. The Haskell Report doesn't seem to say anything about this, which seems odd.
But then for non-IO monads, the optimization would be allowed, no? Of
course; only the IO monad has external world behavior.
Thanks,
gregg
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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

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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (2)
-
Alberto G. Corona
-
wren ng thornton