RE: How to best add logging/debugging code?
Manuel M T Chakravarty wrote:
There is `Debug.Trace.trace' for this:
http://haskell.org/ghc/docs/latest/html/base/Debug.Trace.html
However, if you want to log as opposed to debug, you may want to have the output go to somewhere else but stdout. Hence, it might be useful to have a variant of the `trace' that takes an explicit file handle in the library.
Is there a list of problems anywhere with using trace? For example does it affect evaluation order?
You can think of the meaning of trace like this: trace e1 e2 <=> foldr seq () e1 `seq` e2 in other words, it deepSeqs the first argument and then behaves as the second argument. If you insert a trace with a literal string into your program, eg. trace "calling f" (f x) then it has no effect on the meaning of the program. Trace will only have an effect on the semantics of the program when you print out a value in the program: trace ("calling f with " ++ show x) (f x) in which case it will cause the value (x in this case) to be evaluated earlier than it would otherwise. If x turned out to be _|_, then the program might fail when it otherwise might not have. Cheers, Simon
Is there a list of problems anywhere with using trace? For example does it affect evaluation order?
Apart from changing the evaluation order of expressions, trace has other drawbacks, noted I think by Lennart(?) but I can't remember exactly where. One issue is this: Consider an expression where the printable argument to 'trace' causes the evaluation of another expression which itself is defined using 'trace'. The nested 'trace' outputs will appear interspersed with each other. In recursive definitions this can become quite painful to disentangle. Regards, Malcolm
On Thu, 27 Nov 2003 14:19:53 +0000 Malcolm Wallace <Malcolm.Wallace@cs.york.ac.uk> wrote:
Is there a list of problems anywhere with using trace? For example does it affect evaluation order?
Apart from changing the evaluation order of expressions, trace has other drawbacks, noted I think by Lennart(?) but I can't remember exactly where. One issue is this:
Consider an expression where the printable argument to 'trace' causes the evaluation of another expression which itself is defined using 'trace'. The nested 'trace' outputs will appear interspersed with each other. In recursive definitions this can become quite painful to disentangle.
trace is handy for quick debugging, but if you reach this point, HOOD would probably be a better choice (or some of the other debugging technologies, e.g. Buddha, Hat, HsDebug).
participants (3)
-
Derek Elkins -
Malcolm Wallace -
Simon Marlow