heapgraph tool
Posted on 21 January 2010
Here is a small program to help draw diagrams of heap graphs.
You feed it (via stdin) a text file written a silly little language, for example:
graph g0
node n0 (closure "double" (closure "(*)" "5" "4"))
graph g1
node n0 (closure "(+)" n1 n1)
node n1 (closure "(*)" "5" "4")
graph g2
node n0 (closure "(+)" n1 n1)
node n1 "20"
graph g3
node n0 "40"
...pipe the results through Graphviz
./heapgraph < example | dot -T pdf -o example.pdf
...and what you get back is a little series of graphs like the following:
I never worked out how to tell graphviz to draw the subgraphs from top to bottom instead of left to right. Help would be appreciated :-)
The context is that I'm in the process of reading Rabhi and Lapalme's
Algorithms: A Functional Programming Approach. One of its introductory chapters has an explanation of graph reduction. It occured to me that I ought to write lots of little graphs and just walk through them. The general idea is that maybe one of the impediments to my understanding Haskell laziness/strictness was sheer impatience, that I was being far too motivated to make my programs go faster. I'm hoping that a slower and more methodical approach will work, for example, starting by making sure I understand basic ideas like a heap first.
Perhaps such a tool will be useful for you if you are in a similar position, or if you happen to be teaching this sort of stuff.