<div dir="auto">Ah, I see. I erroneously assumed binary operators would be defined in similar ways but I realize that's perhaps naive because with addition you're never not going to evaluate both sides, unlike with booleans.<div dir="auto"><br></div><div dir="auto">That's interesting. Thanks for responding.</div></div><br><div class="gmail_quote"><div dir="ltr">On Sun, Dec 23, 2018, 2:26 PM Ian Denhardt <<a href="mailto:ian@zenhack.net">ian@zenhack.net</a> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">There are no guarantees about in what order these things will be<br>
evaluated. The compiler is well within its rights to evaluate<br>
the expressions in any order, or more than once even (though IIRC<br>
ghc never does the latter). The left-to-right ordering holds for &&<br>
because the Haskell report specifically defines[1] it as:<br>
<br>
True && x = x<br>
False && _ = False<br>
<br>
In this case the compiler can't in general evaluate the RHS first,<br>
because if the LHS is False and the RHS is bottom, this would be<br>
incorrect. But this is due to the semantics of &&, and doesn't hold in<br>
general.<br>
<br>
-Ian<br>
<br>
[1]: <a href="https://www.haskell.org/onlinereport/haskell2010/haskellch9.html#x16-1710009" rel="noreferrer noreferrer" target="_blank">https://www.haskell.org/onlinereport/haskell2010/haskellch9.html#x16-1710009</a><br>
<br>
Quoting erik (2018-12-23 15:19:04)<br>
> Tom,<br>
> Why does "b" evaluate before "a" in your example? I would have thought<br>
> left-hand and then right-hand if needed. I'm on my phone but I'd like<br>
> to try the same example with booleans and `&&` instead of `+`.<br>
> Erik<br>
> On Sat, Dec 22, 2018, 11:40 PM Tom Ellis<br>
> <[1]<a href="mailto:tom-lists-haskell-cafe-2017@jaguarpaw.co.uk" target="_blank" rel="noreferrer">tom-lists-haskell-cafe-2017@jaguarpaw.co.uk</a> wrote:<br>
><br>
> On Sat, Dec 22, 2018 at 09:52:18AM +0100, Damien Mattei wrote:<br>
> > i have inserted trace statement that output variable<br>
> > ... i have strange behavior of output:<br>
> Let's take a simpler example.� Do you understand why the trace<br>
> statments<br>
> from this small program appear in the order that they do?� (And for<br>
> what<br>
> it's worth I really think you'll be better off writing programs<br>
> using do<br>
> notation).<br>
> % cat test.hs<br>
> import Debug.Trace<br>
> result =<br>
> � let a = trace "evaluating a" 2<br>
> � � � b = trace "evaluating b" 10<br>
> � � � c = trace "evaluating c" (a + b)<br>
> � in c<br>
> ~% ghci -e result test.hs<br>
> evaluating c<br>
> evaluating b<br>
> evaluating a<br>
> 12<br>
> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> To (un)subscribe, modify options or view archives go to:<br>
> [2]<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
> Only members subscribed via the mailman list are allowed to post.<br>
><br>
> Verweise<br>
><br>
> 1. mailto:<a href="mailto:tom-lists-haskell-cafe-2017@jaguarpaw.co.uk" target="_blank" rel="noreferrer">tom-lists-haskell-cafe-2017@jaguarpaw.co.uk</a><br>
> 2. <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div>