Any ideas on what this should generate and not generate...?

We had a strange phenomenon while debugging a large compiler. This is the smallest example, but we have seen this behavior in our own monad. module TracEx where import Debug.Trace runit :: IO () runit = traceShow "Bla" $ do traceShow "Bloe” runit and this generates Bla Bloe and then it hangs. Nothing else is generated. We are guessing this has to do with optimisations of some kind. And what about runit :: IO () runit = traceShow "Bla" $ do n <- traceShow "Num" print 2 traceShow "Bloe” runit Now we get Bla Num 2 Bloe 2 Bloe 2 … No more Bla and no more Num. Any ideas whether this is a bug or a feature? Jur and Tibor

Hi,
In the first example the traceShows are only called as a side effect of
evaluating the IO expression (traceShow "Bla" $ do traceShow "Bloe" runit)
which happens only once (runit is a top-level binding). I assume that for
the second example GHC lifts (traceShow "Num" print 2) out of the do block
and similarly evaluates it only once.
The traceShow "Bloe" is not lifted out of there because it's under a
lambda. E.g. if you do this instead:
runit :: IO ()
runit = traceShow "Bla" $ do
traceShow "Num" (print 2)
traceShow "Bloe" runit
You get
"Bla"
"Num"
2
"Bloe"
2
2
2
2
...
Jacco
2017-06-28 16:16 GMT+01:00 Jurriaan Hage
We had a strange phenomenon while debugging a large compiler. This is the smallest example, but we have seen this behavior in our own monad.
module TracEx where
import Debug.Trace
runit :: IO () runit = traceShow "Bla" $ do traceShow "Bloe” runit
and this generates Bla Bloe
and then it hangs. Nothing else is generated. We are guessing this has to do with optimisations of some kind.
And what about
runit :: IO () runit = traceShow "Bla" $ do n <- traceShow "Num" print 2 traceShow "Bloe” runit
Now we get Bla Num 2 Bloe 2 Bloe 2 … No more Bla and no more Num.
Any ideas whether this is a bug or a feature?
Jur and Tibor
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

http://hackage.haskell.org/package/base-4.9.1.0/docs/Debug-Trace.html#v:trac...
My sense is that you should probably be using traceIO rather than the
pure trace functions. The pure functions do not sequence with respect
to IO and once the expression you are tracing has been fully
evaluated, they will never take effect again.
On Wed, Jun 28, 2017 at 11:16 AM, Jurriaan Hage
We had a strange phenomenon while debugging a large compiler. This is the smallest example, but we have seen this behavior in our own monad.
module TracEx where
import Debug.Trace
runit :: IO () runit = traceShow "Bla" $ do traceShow "Bloe” runit
and this generates Bla Bloe
and then it hangs. Nothing else is generated. We are guessing this has to do with optimisations of some kind.
And what about
runit :: IO () runit = traceShow "Bla" $ do n <- traceShow "Num" print 2 traceShow "Bloe” runit
Now we get Bla Num 2 Bloe 2 Bloe 2 … No more Bla and no more Num.
Any ideas whether this is a bug or a feature?
Jur and Tibor
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

On Wed, Jun 28, 2017 at 2:48 PM, David McBride
http://hackage.haskell.org/package/base-4.9.1.0/docs/ Debug-Trace.html#v:traceM
My sense is that you should probably be using traceIO rather than the pure trace functions. The pure functions do not sequence with respect to IO and once the expression you are tracing has been fully evaluated, they will never take effect again.
This, and remember that IO is a bit of a trick: the expressions you build are in fact pure, they are *descriptions* of impure computations. Once a pure description produces a description of a low-level, impure I/O action, the *evaluation* is done. *Execution* is separate and not affected (or for that matter effected). (As if we had: data IO a = GetChar | PutChar c | ... except typing that would get 'interesting'.) So things like 'traceShow' are done once in the process of evaluating your IO down to a sequence of low-level actions, and actually performing those actions is distinct from this and done later. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
participants (4)
-
Brandon Allbery
-
David McBride
-
Jacco Krijnen
-
Jurriaan Hage