forked from SWI-Prolog/swish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_bdd.swinb
87 lines (62 loc) · 2.27 KB
/
render_bdd.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<div class="notebook">
<div class="nb-cell markdown">
# The BDD renderer
#### Synopsis
:- use_rendering(bdd).
#### Options supported
None
#### Reconised terms
The `bdd` renderer recognises CLP(B) residual goals of `library(clpb)`
and draws them as Binary Decision Diagrams (BDDs). Both kinds of
residual goals that `library(clpb)` can emit are supported by
this renderer:
By default, `library(clpb)` emits `sat/1` residual goals. These are
first translated to BDDs by this renderer.
In cases where the projection to `sat/1` goals is too costly, you can
set the Prolog flag `clpb_residuals` to `bdd` yourself, and use
`copy_term/3` to obtain a BDD representation of the
residual goals. Currently, setting this flag does not affect the
SWISH toplevel, so you also need to use `del_attrs/1`
to avoid the costly projection. See below for an example.
In both cases, the graphviz renderer is used for the final output.
</div>
<div class="nb-cell markdown">
## Examples
The first example simply enables the `bdd` renderer and posts a CLP(B)
query to show the residual goal as a BDD.
</div>
<div class="nb-cell program">
:- use_rendering(bdd).
:- use_module(library(clpb)).
</div>
<div class="nb-cell query">
sat(X+Y).
</div>
<div class="nb-cell markdown">
The second example sets `clpb_residuals` to `bdd` in order to
prevent the costly projection to `sat/1` goals. Note that this also
requires the use of `del_attrs/1` and—since renderers are
not invoked on subterms—introducing a new variable for the
single residual goal that represents the BDD.
</div>
<div class="nb-cell program">
:- use_rendering(bdd).
:- use_module(library(clpb)).
:- set_prolog_flag(clpb_residuals, bdd).
</div>
<div class="nb-cell query">
length(Vs, 20), sat(card([2],Vs)),
copy_term(Vs, Vs, [BDD]), maplist(del_attrs, Vs).
</div>
<div class="nb-cell markdown">
Building on this idea, the third example shows that you can impose an
arbitrary *order* on the variables in the BDD by first stating a
tautology like `+[1,V1,V2,...]`. This orders the variables in the same
way they appear in this list.
</div>
<div class="nb-cell query">
sat(+[1,Z,Y,X]), sat(X+Y+Z),
Vs = [X,Y,Z],
copy_term(Vs, Vs, [BDD]), maplist(del_attrs, Vs).
</div>
</div>