What is the best way to test and interact with inner functions defined inside a toplevel function?

Solution 1:

When you are stopped at a breakpoint in GHCi, you can access anything that's in scope. Let's say you have a function like this:

foo :: Int -> Int
foo x = g (x + 2)
  where g y = x^y 

You can set a breakpoint on foo and try calling it:

> :break foo
Breakpoint 1 activated at /tmp/Foo.hs:(2,1)-(3,17)
> foo 42
Stopped at /tmp/Foo.hs:(2,1)-(3,17)
_result :: Int = _

g is not in scope yet at this point, so we have to step once:

[/tmp/Foo.hs:(2,1)-(3,17)] > :step
Stopped at /tmp/Foo.hs:2:9-17
_result :: Int = _
g :: Integral b => b -> Int = _
x :: Int = 42

Now that g is in scope, we can use it like any top-level function:

[/tmp/Foo.hs:2:9-17] > g 2
1764
[/tmp/Foo.hs:2:9-17] > g 3
74088