Skip to content

Debug.Trace.trace is too strict

Consider the following toy example:

import Debug.Trace

f n
  = let res = g n
    in (trace $ unlines ["in: " ++ show n, "out: " ++ show res])
       res
  where
    g n = if n <= 1000 then n+1 else g n

main
  = print $ [f 500, f 302, f 2000, f 22]

When run it outputs:

in: 500
out: 501

in: 302
out: 303

^C

In a real example, for a program that hangs, where one only suspects that f may be the culprit, and where f is being called from various places with different values, this output is not very useful (and in fact, it is misleading).

My mental model of the trace function is something along these lines:

myTrace :: String -> a -> a
myTrace s a
  = unsafePerformIO $ do
      putStrLn s
      return a

and in fact, replacing trace by myTrace in the example above one gets the more useful:

in: 500
out: 501

in: 302
out: 303

in: 2000
^C
Trac metadata
Trac field Value
Version 7.8.3
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries/base
Test case
Differential revisions
BlockedBy
Related
Blocking
CC ekmett, hvr
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information