:- use_rendering(graphviz). :- include(example(graphviz_swish)).
# Visualising terms and SLD trees with Graphviz [Graphviz](http://www.graphviz.org/) is a collection of programs for visualising graphs. Here, we conveniently use it for visualising Prolog terms and SLD trees. The code in the box above defines the two main predicates `term/2` and `sld/2`. The query `?-term(+Term, -Dot)`, where `Term` is bound to a Prolog term, will return a variable `Dot`, which defines a tree in Graphviz format. For example, the query `?-term([a,b,b,a], Dot)` produces the following Graphviz input: Dot = ' digraph { node [shape=plaintext, fontname=Courier, fontsize=12] 0 [label="[|]"]; 1 [label="a"]; 0 -> 1; 2 [label="[|]"]; 0 -> 2; 3 [label="b"]; 2 -> 3; 4 [label="[|]"]; 2 -> 4; 5 [label="b"]; 4 -> 5; 6 [label="[|]"]; 4 -> 6; 7 [label="a"]; 6 -> 7; 8 [label="[]"]; 6 -> 8; } ' Adding `X = dot(Dot)` to the query then invokes the Graphviz renderer:
term([a,b,b,a], _Dot), X = dot(_Dot).
See [here](http://www.graphviz.org/pub/scm/graphviz2/doc/info/command.html) for more information about how to use Graphviz programs from the command line. Similarly, the query `?-sld(Goal,Dot)`, where Goal is bound to a Prolog goal, will produce an atom, which defines a tree in Graphviz format. For example, the query `?-sld(student_of(S,peter),Dot)` produces the following tree when rendered with Graphviz:
sld(student_of(S,peter), _Dot), X = dot(_Dot).
Now, try the example queries `?-term2` and `?-sld2` that have been pre-defined in [`graphviz_swish.pl`](graphviz_swish.pl).
term2(_Dot), X = dot(_Dot).
sld2(_Dot), X = dot(_Dot).