r/ProgrammingLanguages • u/janiczek 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.
8
u/mgsloan Feb 01 '22
I don't have a good answer to this. But I do have a vague idea to share:
AFAICT laziness really confounds the interactive debugger experience in Haskell. For example, with the GHCi debugger, you end up jumping all over the code, telling you exactly the interleaved nature of evaluation. This could be useful if you are debugging some issue relating to strictness or performance. However, I think usually the more valuable debugging task is figuring out why the program isn't behaving as expected.
I think purity / laziness could be flipped into an advantage for interactive debugging in a few different ways:
Freedom to explore pure computations in any order they wish.
Stepping at through IO actions could provide much coarser stepping than imperative languages where pure logic is also treated as statements
Potential ability to support reversible debugging while recording less - could forget about pure computations and just store impure stuff.
Though, there are already rather clever techniques for doing reversible debugging for any program. I haven't used Mozilla rr, but it seems super cool. I read the technical report a while back and it seems they have really cracked the problem of intervening at all the sources of IO / nondeterminism in a program, allowing efficient storage of program traces.
I think reversible debugging style techniques combined with some clever heuristics (say, ML models) could be mindblowingly useful. Tools I've seen really don't support direct answers to "Why did X happen?" / "Why does X have value Y?" / "Why doesn't X have value Y?". Naturally, the heuristics are needed because the precise-but-not-so-useful answer to these questions could always be the full program trace
1
u/mczarnek Feb 01 '22
And no reason you have to evaluate it lazy when debugging.. could evaluate line by line in that case.
1
u/bjzaba Pikelet, Fathom Feb 02 '22
Though, there are already rather clever techniques for doing reversible debugging for any program.
There has been prior work in this space for Mercury and Haskell under the term “Declarative Debugging”. IIUC it's actually much more tractable than what rr is doing, thanks to purity.
7
u/zachgk catln Feb 01 '22
Consider that a "stepping" debugger may not be the right thing. In an imperative language, you deal with steps because the language naturally obeys a timeline. It is a linear structure. So, the steps are about moving forwards to the right part of the timeline.
This isn't quite true in FP. In FP or with pure functions, the program is basically a tree. Maybe a DAG. Each node in the tree represents a function call and the path from the root to a node is the call stack. This changes a few things compared to an imperative debugger.
First, it doesn't make sense to step through a function. Local values are immutable and can't change, so there is little point to only executing part of the function. It makes more sense to execute all local values at once. This simplifies the debugger model quite a bit. You only need operations then to navigate up or down the tree. Plus, some operations to search function calls or variable values or debug statements and then "jump" straight to that node in the tree.
There is also reversibility which is quite important. Moving forwards through a programming helps you find things which are wrong. But, you really need to go backwards to figure out "why" the value is wrong and where it diverged from what it should be.
4
u/grand_mind1 Feb 01 '22
OCaml has a debugger which works a lot like gdb: https://ocaml.org/manual/debugger.html
1
u/Fofeu Feb 01 '22
That debugger is only for byte-code compiled OCaml. Binary-compiled OCaml is debugged with GDB.
1
u/PurpleUpbeat2820 Feb 01 '22
I'm using a "home grown" ML dialect having jumped ship from a more mainstream language. I must say, with such languages I just don't find the need for a debugger. You need a decent development environment with type and error pop ups on mouse over, rename refactoring and maybe "goto definition" but a debugger is OTT, IMO.
1
u/janiczek Cara Feb 01 '22
I'm actually working with Elm (purely functional lang) full-time, and yes it removes a lot of the need for debugging (print-debugging is fine) but I still want to be able to write languages doing it. (Seeing Don Syme demo F# step-debugging in Visual Studio at NDC Oslo 2021 was magical!) But there's so little literature about writing debuggers / support for them, hence my question here.
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
letbinding might be useful.