Haskell Language Using GHCi Breakpoints with GHCi


Example

GHCi supports imperative-style breakpoints out of the box with interpreted code (code that's been :loaded).

With the following program:

-- mySum.hs
doSum n = do
  putStrLn ("Counting to " ++ (show n))
  let v = sum [1..n]
  putStrLn ("sum to " ++ (show n) ++ " = " ++ (show v))

loaded into GHCi:

Prelude> :load mySum.hs 
[1 of 1] Compiling Main             ( mySum.hs, interpreted )
Ok, modules loaded: Main.
*Main> 

We can now set breakpoints using line numbers:

*Main> :break 2
Breakpoint 0 activated at mySum.hs:2:3-39

and GHCi will stop at the relevant line when we run the function:

*Main> doSum 12
Stopped at mySum.hs:2:3-39
_result :: IO () = _
n :: Integer = 12
[mySum.hs:2:3-39] *Main> 

It might be confusing where we are in the program, so we can use :list to clarify:

[mySum.hs:2:3-39] *Main> :list
1  doSum n = do
2    putStrLn ("Counting to " ++ (show n))   -- GHCi will emphasise this line, as that's where we've stopped
3    let v = sum [1..n]

We can print variables, and continue execution too:

[mySum.hs:2:3-39] *Main> n
12
:continue 
Counting to 12
sum to 12 = 78
*Main>