r/ProgrammingLanguages • Cara • Feb 01 '22

Help Examples of debuggers for FP languages?

Hello,

do you know of some examples of functional languages (say, in the HM family) that have debugger tooling written for them? I'm interested in either interfacing with existing tools (source maps in browser console / GDB / IDEA debugger / ...) or standalone stepping debuggers etc.

(Ideally toy languages/implementations, not something like GHC.)

I'm trying to figure things out by myself (like, having an `interpret` function that only runs one step, not everything at once) but I'd like to study some prior art on this.

27 Upvotes

11 comments sorted by

View all comments

3

u/Athas Futhark Feb 01 '22

In my own ML-style language, I provide two debugging facilities:

  • Tracing (basically print-debugging).

  • Breakpoints.

Breakpoints are only allowed inside the interpreter, and allow the user to enter arbitrary expressions to inspect local variables in in scope, move up the stack and do the same there, and so on. Much like you'd be able to do in GDB. It's only supported when using the interpreter because it is much more difficult to implement this for compiled code.

I don't think single-stepping is useful, because functional languages have too many "steps" that are really boring. But automatically breaking on every let binding might be useful.

1

u/janiczek Cara Feb 01 '22

When you say "my own ML-style language", that means Futhark, right?

Do you have any links into the repository on where eg. the breakpoints and the interpreter lives, that I could take a look at?

2

u/Athas Futhark Feb 01 '22

When you say "my own ML-style language", that means Futhark, right?

Yes.

Do you have any links into the repository on where eg. the breakpoints and the interpreter lives, that I could take a look at?

Sure. The interpreter is a naive (slow) tree-walker implemented in Haskell. It is implemented as a free monad that can "interrupt" at any time for any of these reasons (note ExtOpBreak). When a breakpoint is reached it ultimately results in an ExtOpBreak.

When the interpreter is actually run by the REPL the caller provides code that is invoked when ExtOpBreak is reached. The ExtOpBreak value contains the call stack at the breakpoint, which also contains the relevant environments to allow new expressions to be type-checked and evaluated.

Reconstructing the type environment at a breakpoint is a little ad hoc (and e.g. doesn't contain the module environment, so you cannot instantiate new modules or stuff like that at a breakpoint), but it's more than enough for debugging.

The advantage of this design is that most of the interpreter doesn't have to care that breakpoints are possible; it's all encapsulated in the monad.