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