<span class="Apple-style-span" style="border-collapse: collapse; "><div>forwarded:</div><div><br></div>Yes! &nbsp;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:<div>
<br>op x= x+x</div><div><br>print $ Just (op 1) &gt;&gt;= \y-&gt; return (Just 2)<br><br>does not evaluate &nbsp;op 1</div><div><br>but</div><div><br>print $ Just (op 1) &gt;&gt;= \y-&gt; return y<br><br><span style="font-style: italic; ">does&nbsp;</span>execute it.<br>
<br><br><br>The trace of the first:<br><br>[1 of 1] Compiling Main &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ( test.hs, interpreted )<br>Ok, modules loaded: Main.<br>*Main&gt; :set stop :list<br>*Main&gt; :step main<br>Stopped at test.hs:4:6-43<br>_result :: IO () = _<br>
3<br>4 &nbsp;main= print $ Just (op 1) &gt;&gt;= \y-&gt; return &nbsp;2<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>5<br>[test.hs:4:6-43] *Main&gt; :step<br>Stopped at test.hs:4:14-43<br>_result :: Maybe Integer = _<br>
3<br>4 &nbsp;main= print $ Just (op 1) &gt;&gt;= \y-&gt; return &nbsp;2<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>5<br>[test.hs:4:14-43] *Main&gt; :step<br>Stopped at test.hs:4:14-24<br>_result :: Maybe Integer = _<br>
3<br>4 &nbsp;main= print $ Just (op 1) &gt;&gt;= \y-&gt; return &nbsp;2<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^^^^^^^^<br>5<br>[test.hs:4:14-24] *Main&gt; :step<br>Stopped at test.hs:4:35-43<br>_result :: Maybe Integer = _<br>3<br>4 &nbsp;main= print $ Just (op 1) &gt;&gt;= \y-&gt; return &nbsp;2<br>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^^^^^<br>5<br>[test.hs:4:35-43] *Main&gt; :step<br>Just 2<br><br><br><span style="font-style: italic; ">But in the second case op is executed:<br></span><br><br>*Main&gt; :step main<br>
Stopped at test.hs:4:6-43<br>_result :: IO () = _<br>3<br>4 &nbsp;main= print $ Just (op 1) &gt;&gt;= \y-&gt; return &nbsp;y<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>5<br>[test.hs:4:6-43] *Main&gt; :step<br>Stopped at test.hs:4:14-43<br>
_result :: Maybe Integer = _<br>3<br>4 &nbsp;main= print $ Just (op 1) &gt;&gt;= \y-&gt; return &nbsp;y<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>5<br>[test.hs:4:14-43] *Main&gt; :step<br>Stopped at test.hs:4:14-24<br>
_result :: Maybe Integer = _<br>3<br>4 &nbsp;main= print $ Just (op 1) &gt;&gt;= \y-&gt; return &nbsp;y<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^^^^^^^^<br>5<br>[test.hs:4:14-24] *Main&gt; :step<br>Stopped at test.hs:4:35-43<br>_result :: Maybe Integer = _<br>
y :: Integer = _<br>3<br>4 &nbsp;main= print $ Just (op 1) &gt;&gt;= \y-&gt; return &nbsp;y<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^^^^^<br>5<br>[test.hs:4:35-43] *Main&gt; :step<br>Just Stopped at test.hs:4:20-23<br>
_result :: Integer = _<br>3<br>4 &nbsp;main= print $ Just (op 1) &gt;&gt;= \y-&gt; return &nbsp;y<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^^^^<br>5<br>[test.hs:4:20-23] *Main&gt; :step<br>Stopped at test.hs:6:0-8<br>
_result :: Integer = _<br>5<br>6&nbsp;<span style="font-weight: bold; ">&nbsp;op x= x+x</span><br>&nbsp; &nbsp; ^^^^^^^^^<br>7<br>[test.hs:6:0-8] *Main&gt; :step<br>Stopped at test.hs:6:6-8<br>_result :: Integer = _<br>x :: Integer = _<br>5<br>
6 &nbsp;<span style="font-weight: bold; ">o</span><span style="font-weight: bold; ">p x= x+x</span><br>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^<br>7<br>[test.hs:6:6-8] *Main&gt; :step<br>Just 2<br><br><br><br><br>2009/2/5 Gregg Reynolds &lt;<a href="mailto:dev@mobileink.com" target="_blank" style="color: rgb(42, 93, 176); ">dev@mobileink.com</a>&gt;<div>
<div><span id="q_11f60636b3e3ee54_1" class="WQ9l9c" style="cursor: pointer; color: rgb(80, 0, 80); font-size: 9px; ">- Ocultar texto citado -</span></div><div class="Ih2E3d" style="color: rgb(80, 0, 80); "><br>&gt;<br>&gt; On Thu, Feb 5, 2009 at 9:27 AM, Bulat Ziganshin &lt;<a href="mailto:bulat.ziganshin@gmail.com" target="_blank" style="color: rgb(42, 93, 176); ">bulat.ziganshin@gmail.com</a>&gt; wrote:<br>
&gt;&gt;<br>&gt;&gt; Hello Gregg,<br>&gt;&gt;<br>&gt;&gt; Thursday, February 5, 2009, 6:20:06 PM, you wrote:<br>&gt;&gt;<br>&gt;&gt; &gt; An optimizer can see that the result of the first getChar is<br>&gt;&gt; &gt; discarded and replace the entire expression with one getChar without<br>
&gt;&gt; &gt; changing the formal semantics.&nbsp;<br>&gt;&gt;<br>&gt;&gt; this is prohibited by using pseudo-value of type RealWorld which is<br>&gt;&gt; passed through entire action stream. actually, order of execution is<br>
&gt;&gt; controlled by dependencies on this values<br>&gt;&gt;<br>&gt;&gt;&nbsp;<a href="http://haskell.org/haskellwiki/IO_inside" target="_blank" style="color: rgb(42, 93, 176); ">http://haskell.org/haskellwiki/IO_inside</a><br>
&gt;&gt;<br>&gt; Thanks.&nbsp; I actually read that a few weeks ago and forgot all about it.&nbsp; So the gist is that type IO has special magic semantics.&nbsp; Formal, but hidden.&nbsp; Which means monad semantics are built in to the language, at least for that type.&nbsp; The Haskell Report doesn&#39;t seem to say anything about this, which seems odd.&nbsp;<br>
&gt;<br>&gt; But then for non-IO monads, the optimization would be allowed, no?&nbsp; Of course; only the IO monad has external world behavior.<br>&gt;<br>&gt; Thanks,<br>&gt;<br>&gt; gregg<br>&gt;<br></div></div><div class="Ih2E3d" style="color: rgb(80, 0, 80); ">
&gt; _______________________________________________<br>&gt; Haskell-Cafe mailing list<br>&gt;&nbsp;<a href="mailto:Haskell-Cafe@haskell.org" target="_blank" style="color: rgb(42, 93, 176); ">Haskell-Cafe@haskell.org</a><br>&gt;&nbsp;<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank" style="color: rgb(42, 93, 176); ">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
&gt;<br></div></div></span>