-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphviz_intro.swinb
66 lines (53 loc) · 2.11 KB
/
graphviz_intro.swinb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<div class="notebook">
<div class="nb-cell program" data-background="true">
:- use_rendering(graphviz).
:- include(example(graphviz_swish)).
</div>
<div class="nb-cell markdown">
# 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:
</div>
<div class="nb-cell query">
term([a,b,b,a], _Dot), X = dot(_Dot).
</div>
<div class="nb-cell markdown">
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:
</div>
<div class="nb-cell query">
sld(student_of(S,peter), _Dot), X = dot(_Dot).
</div>
<div class="nb-cell markdown">
Now, try the example queries `?-term2` and `?-sld2` that have been pre-defined in [`graphviz_swish.pl`](graphviz_swish.pl).
</div>
<div class="nb-cell query">
term2(_Dot), X = dot(_Dot).
</div>
<div class="nb-cell query">
sld2(_Dot), X = dot(_Dot).
</div>
</div>