Debug.Trace
trace inside a pure function
Debug.Trace prints a message from inside a pure function and returns the value unchanged. It is implemented with unsafePerformIO, which is the only way a pure function can print, and the type is honest about it:
trace :: String -> a -> a
traceShow :: Show a => a -> b -> b
traceM :: Applicative f => String -> f ()
trace msg value prints the message when the value is forced and gives the value back, so a call can be dropped into an expression without changing its type. traceShow does the same for a value it shows, and traceM is the version for a do block.
A program with three kinds of trace
module Main (main) where
import Debug.Trace (trace, traceM, traceShow)
double :: Int -> Int
double x = trace ("double " ++ show x) (x + x)
main :: IO ()
main = do
print (sum (map double [1, 2, 3]))
print (traceShow (3 :: Int) (4 :: Int))
mapM_ (\n -> traceM ("step " ++ show n)) [1, 2 :: Int]
putStrLn "done"
ghc -Wall -O1 -o trace trace.hs
./trace 2>&1 | cat
double 1
double 2
double 3
3
step 1
step 2
12
4
done
Why the order surprises
The program prints 12 and 4 with print, and those lines appear after the trace lines that logically come later. The reason is buffering and nothing else:
- trace writes to standard error, which is unbuffered, so each message appears the moment it is produced.
- print writes to standard output, which is block buffered when it is a pipe, so 12 and 4 wait in the buffer until the program ends.
- 2>&1 | cat merges the two streams and pipes them, which is exactly the situation that shows the effect.
On a terminal the standard output is line buffered and the same program shows the traces and the values interleaved in evaluation order. Neither order is wrong, and the page shows the piped one because that is the reproducible one: the command on the page is the command that produced the output.
When to use it, when to delete it
A trace is the fastest way to answer "what does this function get" without a GHCi session, and it has three costs:
- It is not pure in the observable sense: the output depends on how much of the value is forced and in what order, so a trace can change behaviour under optimisation.
- It defeats some optimisations, because the compiler cannot move a call that prints.
- It stays in the code if nobody removes it, and a library that prints on every call is unusable.
The rules that follow are simple: use trace while debugging, keep it out of a module that someone else imports, and delete it before the commit. A trace that must stay belongs in a logging layer with an IO type, which the exceptions and files lessons covered. A trace that shows the shape of a value in a test belongs in the test, and the testing section of the course shows that shape with a report of lines.
Exercises
- Add a trace to a recursive function and count how many times it prints for a list of five.
- Compare ./trace on a terminal with ./trace | cat and explain the difference.
- Trace a foldl and a foldl' and describe what each prints when.
- Use traceShow on a list and then on a tuple.
- Explain why a trace can disappear when the optimiser removes the call it was attached to.
- Replace a trace with a GHCi breakpoint and compare the two workflows.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the type of
trace?Answer
String -> a -> atrace prints the message and returns the value, which is why it fits inside a pure expression.
-
Where does
tracewrite its message?Answer
Standard error
The message goes to stderr, which is unbuffered, and that is why the order differs from print.
-
Why do the printed numbers appear after the traces in the output?
Answer
Standard output is block buffered when it is a pipe
The traces on stderr appear at once and the buffered stdout is flushed at exit.
-
What does this program print?
module Main (main) where import Debug.Trace (trace) main :: IO () main = print (trace "here" (1 :: Int))Answer
here and then 1, on separate streams
trace prints to stderr when the value is forced by print, and the value 1 follows.
-
What is
traceimplemented with?Answer
unsafePerformIOThe library uses unsafePerformIO, which is why a pure function can print.
-
What does this program print?
module Main (main) where import Debug.Trace (traceShow) main :: IO () main = print (traceShow [1, 2 :: Int] "x")Answer
"x"
traceShow writes the list to stderr and returns the string, and print shows that string with its quotes on stdout.
-
Which cost does a leftover trace have?
Answer
It can defeat optimisations and prints for every caller
The call cannot be moved or removed freely, and the output stays in the library.
-
What does this program print?
module Main (main) where import Debug.Trace (traceM) main :: IO () main = do traceM "step" putStrLn "done"Answer
step and then done
traceM runs in the do block before the putStrLn, so its message comes first on stderr.
-
Why is a trace not a replacement for a proper log?
Answer
It is unsafe, unordered and cannot be configured or removed at run time
A logging layer has a type, an order and a switch; a trace has none of them.
-
What does
traceShow value resultprint?Answer
The shown value, and it returns result
traceShow shows its first argument to stderr and returns the second unchanged.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev