Debugging in GHCi
A debugger inside GHCi
GHCi has a small debugger: you set a breakpoint on a function, run main, and the session stops when the function is entered. From there :print shows the arguments that are bound, :step evaluates one step, :continue runs to the next stop. It is not a source-level debugger of the C kind, because a lazy program has no call stack to walk, but it answers the question a print statement would: which values did this function get.
:break double
:show breaks
:main
:print x
:continue
:step
:quit
The commands above are the whole vocabulary of this lesson. The next section starts GHCi on a file, which is what makes the functions available to :break.
The program under test
module Main (main) where
double :: Int -> Int
double x = x + x
total :: [Int] -> Int
total values = foldr (+) 0 (map double values)
main :: IO ()
main = print (total [1, 2, 3])
The line numbers matter for the debugger: the breakpoint output names dbg.hs:4:12-16, which is the body of double on line 4, and dbg.hs:7:16-46, which is the body of total on line 7. The file is loaded under the name dbg.hs, so that is the name the debugger prints. hlint suggests fusing the foldr and the map into one fold, and the map is kept on purpose: it is the function the session below steps into.
:break and :show breaks
:break double puts a breakpoint on every equation of double and prints where it landed. :show breaks lists the breakpoints with their numbers, which is the list :delete takes:
Breakpoint 0 activated at dbg.hs:4:12-16
[0] Main dbg.hs:4:12-16 enabled
The module is Main because the file is a program, and the range is the part of the line the breakpoint covers. A function that is called from three places stops in all three, which is what the transcript below shows.
A session with three stops
The session was run with ghci -v0 dbg.hs and the commands fed on standard input. GHCi prints its ghci> prompt only when the input is a terminal, so the transcript has the output alone; the commands are the ones in the first block, in order.
:break double
:show breaks
:main
:print x
:continue
:print x
:continue
:print x
:continue
:quit
Breakpoint 0 activated at dbg.hs:4:12-16
[0] Main dbg.hs:4:12-16 enabled
Stopped in Main.double, dbg.hs:4:12-16
_result :: Int = _
x :: Int = 1
x = 1
Stopped in Main.double, dbg.hs:4:12-16
_result :: Int = _
x :: Int = 2
x = 2
Stopped in Main.double, dbg.hs:4:12-16
_result :: Int = _
x :: Int = 3
x = 3
12
Read it as three stops with one variable each: x is 1, then 2, then 3, and after the last :continue the program finished and printed 12. The two lines at a stop are :print x in its two forms: the type and the value with :: is the breakpoint report, and the bare x = 1 is what :print answered. _result is the value the function is about to return, which is still a thunk at the stop.
:step and :print
:step evaluates one step instead of running on, which is how you follow a composition into the functions it calls. A breakpoint on total stops before the fold and shows the argument list; two steps land in the first call of double:
:break total
:main
:print values
:step
:step
:quit
Breakpoint 0 activated at dbg.hs:7:16-46
Stopped in Main.total, dbg.hs:7:16-46
_result :: Int = _
values :: [Int] = [1,2,3]
values = [1,2,3]
Stopped in Main.total, dbg.hs:7:29-45
_result :: [Int] = _
values :: [Int] = [1,2,3]
Stopped in Main.double, dbg.hs:4:12-16
_result :: Int = _
x :: Int = 1
The first stop is the whole body of total, the second is the map double values part on the same line (the column range changed), and the third is inside double with 1. That is the evaluation order of this program made visible: the fold asks for the first element of the mapped list, and the map calls double.
What the debugger does not see
- A thunk is printed as _ until it is forced, so a value you expect to be there can still be a computation.
- Breakpoints work on functions that GHCi interprets, not on the optimised code of a compiled binary; the options of the module can prevent it.
- The debugger follows the source, so a function inlined by the optimiser has no separate stop; the lesson on gdb shows what is left at the binary level.
The next lesson uses Debug.Trace instead, which needs no session and prints from inside a pure function. The two are complements: a breakpoint for a question you can ask interactively, a trace for a question you cannot.
Exercises
- Set a breakpoint on double and print x for each call with a list of four numbers.
- Use :delete to remove a breakpoint and :show breaks to confirm.
- Break on a function with two arguments and print both.
- Step through a case expression and describe the stops.
- Explain what _result = _ means at a stop.
- Compare the stops at -O0 and with the optimiser on, and say which functions still stop.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
:break doubledo?Answer
Sets a breakpoint on the equations of double
The command sets a breakpoint and reports the line it landed on.
-
What does
:continuedo at a stop?Answer
Runs until the next breakpoint or the end of the program
continue resumes and stops again at the next breakpoint.
-
What does
:stepdo?Answer
Evaluates one step of the computation
step moves one evaluation step at a time, which is how the session reached double.
-
What does this program print?
module Main (main) where double :: Int -> Int double x = x + x main :: IO () main = print (double 5)Answer
10
5 doubled is 10 and print shows it without quotes.
-
What is
_resultat a breakpoint?Answer
The value the function will return, often still a thunk
The debugger shows the result variable, and laziness means it may not be evaluated yet.
-
What does this program print?
module Main (main) where total :: [Int] -> Int total values = foldr (+) 0 values main :: IO () main = print (total [1, 2, 3])Answer
6
The fold adds 1, 2 and 3 from a zero start.
-
Why does the transcript have no
ghci>prompt?Answer
The input was piped, and GHCi prints a prompt only on a terminal
With a pipe, GHCi cannot see that a human is typing, so it prints no prompt.
-
What does a value printed as
_mean?Answer
It is a thunk that has not been evaluated yet
An unevaluated value shows as a blank until something forces it.
-
What does this program print?
module Main (main) where main :: IO () main = print (map (+ 1) [1, 2 :: Int])Answer
[2,3]
map applies the increment to both elements.
-
Which functions can the GHCi debugger stop on?
Answer
The ones GHCi interprets, not the inlined code of an optimised binary
The debugger works on the interpreted module, so inlining in a compiled binary removes stops.
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