Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing out Haskell's evaluation (rewriting) steps for educational/learning purposes. Is it possible?

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

like image 447
jhegedus Avatar asked Oct 06 '14 10:10

jhegedus


3 Answers

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!

like image 80
Bergi Avatar answered Oct 27 '22 21:10

Bergi


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

like image 29
Sibi Avatar answered Oct 27 '22 21:10

Sibi


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

like image 33
MathematicalOrchid Avatar answered Oct 27 '22 21:10

MathematicalOrchid