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:

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:

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

  1. Add a trace to a recursive function and count how many times it prints for a list of five.
  2. Compare ./trace on a terminal with ./trace | cat and explain the difference.
  3. Trace a foldl and a foldl' and describe what each prints when.
  4. Use traceShow on a list and then on a tuple.
  5. Explain why a trace can disappear when the optimiser removes the call it was attached to.
  6. 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.

  1. What is the type of trace?

    Answer

    String -> a -> a

    trace prints the message and returns the value, which is why it fits inside a pure expression.

  2. Where does trace write its message?

    Answer

    Standard error

    The message goes to stderr, which is unbuffered, and that is why the order differs from print.

  3. 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.

  4. 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.

  5. What is trace implemented with?

    Answer

    unsafePerformIO

    The library uses unsafePerformIO, which is why a pure function can print.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. What does traceShow value result print?

    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