[ Jocelyn Ireson-Paine's Home Page | Publications | Dobbs Code Talk Index | Dobbs Blog Version ]

Christoph Lehner's Tree Drawing Program for Prolog

I was looking for a tree-drawing utility, to depict the syntax trees generated by a compiler I wrote in Prolog in order to teach a friend how compilers work. I have just found Christoph Lehner's Prolog Tree Drawing Program.

His program can be downloaded from the link above, which also shows some examples. It displays trees as ASCII. Here are some examples which I tried, passing various terms to the main predicate drucke_baum:

3 ?- drucke_baum( 1 ).
 1


true.

4 ?- drucke_baum( a ).
 a


true.

5 ?- drucke_baum( 1+a ).
  +
  |
 / \
 1  a


true 
.

6 ?- drucke_baum( sum( [1,2,3] ) ).
  sum
   |
   |
   .
  _|_
 /   \
 1    .
     _|_
    /   \
    2    .
        _|
       /  \
       3  []


true 
.

7 ?- drucke_baum( root( a, b(1), c(3,4), d(e(5),f(g(6))), h ) ).
        root
  ________|_______
 /  |   |     |   \
 a  b   c     d    h
    |   |     |
    |  / \   / \
    1  3  4  e  f
             |  |
             |  |
             5  g
                |
                |
                6


true 
.

Notice that the program displays lists in terms of their underlying representation: the list

  [1,2,3]
is
  .(1, .(2, .(3, [])))

I tried the program under SWI-Prolog for XP, version 5.6.64. Consulting it gave me lots of singleton-variable warnings, which can be ignored. It also gave three errors, which I fixed (correctly, I hope) as follows: