[Haskell-cafe] evaluation semantics of bind
Alberto G. Corona
agocorona at gmail.com
Tue Feb 10 08:16:08 EST 2009
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 <dev at mobileink.com>- Ocultar texto citado -
>
> On Thu, Feb 5, 2009 at 9:27 AM, Bulat Ziganshin <bulat.ziganshin at gmail.com>
wrote:
>>
>> 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
>>
>> http://haskell.org/haskellwiki/IO_inside
>>
> Thanks. I actually read that a few weeks ago and forgot all about it. So
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 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/20090210/7b4cbb8f/attachment.htm
More information about the Haskell-Cafe
mailing list