21

I describe this question by using an example from a book.

In Simon Thompson's book "HASKELL the craft of functional programming" on page 82 (see images below) are shown the evaluation steps for fac 4.

QUESTION:

Is it possible to use some tool or some "Haskell debugger" that would write out the evaluation steps that GHCi uses when it would evaluate the value of fac 4 ?

Preferably in a human readable format, for educational and learning purposes.

It would be also good to have some automatic explanation for each evaluation step, for example which equation was used in the rewriting step.

My main purpose is to gain a deeper understanding of how the rewriting steps are carried out when I run simple educational Haskell example programs (like fac).

Is there a way to do this ? If yes, how ?

enter image description here

enter image description here

jhegedus
  • 20,244
  • 16
  • 99
  • 167
  • [simple-reflect](https://hackage.haskell.org/package/simple-reflect) can do some of this. But while it's useful for simple stuff like lists and maps, it doesn't work for everything. – genisage Oct 06 '14 at 19:51

3 Answers3

11

There is a tool called Lambda bubble pop where you can click on the expression to see how the expression is getting reduced. Note that the tool only supports Integers and Lists as of now, but nevertheless is a good educational tool.

Snapshot of the tool in action:

enter image description here

Sibi
  • 47,472
  • 16
  • 95
  • 163
11

Yes and No. I haven't seen a tool yet that does this line-by-line evaluation that is depicted in your textbook - mostly because a Haskell programm does no "rewriting" of expressions.

However, there is a tool that does visualize Haskell's actual evaluation strategy, step by step: ghc-vis. Instead of just evaluating the result and displaying it on the console like ghci does, it displays a graphical representation of the unevaluated result - and you can force the evaluation of it thunk by thunk, until you arrive at the primitive values and structures.

As an example of what it can do, here's the evaluation until the third list member of the infinite fibonacci sequence:

0, 1 and 1 are evaluated, the rest of the list is a thunk referring back to parts of the list itself

Source: examples section of the project website. You should have a look at all of them!

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Can it deal with full Haskell? For example with type classes and Monads? I'd love to see the visualisation how state monads work in Brian Beckmans video lectures. – jhegedus Oct 10 '14 at 07:32
  • This might be even more educational than seeing the rewriting steps! – jhegedus Oct 10 '14 at 07:34
  • More specificallys, Brian Beckman's video lectures are here : http://channel9.msdn.com/Shows/Going+Deep/Brian-Beckman-The-Zen-of-Expressing-State-The-State-Monad). – jhegedus Oct 10 '14 at 07:39
  • Yes, it can deal with arbitrary haskell structures. I don't think that they would be display differently just because a particular data structure is instance of a specific type class such as a monad, though - but I haven't tried myself yet. – Bergi Oct 10 '14 at 08:17
3

This is a much-requested and highly useful feature — which, as best as I know, is not available anywhere. :-(

MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220