forked from ballerina-platform/nballerina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbdd.bal
233 lines (217 loc) · 6.98 KB
/
bdd.bal
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Binary Decision Diagrams
// These are BDDs as in CDuce, which are a bit different from e.g. Knuth.
// A Bdd represents a disjunction of conjunctions of atoms, where each
// atom may be positive (not negated) or negative (negated). There is
// a total order defined on the atoms.
// Each leaf of the tree is `true` or `false`. Each path from the
// root to a `true` leaf represents a conjunction. When the path goes
// left after passing through a node, then the node's atom is included
// positively in the conjunction; when it goes right, the node's atom
// is included negatively; when it goes through the middle, the node's
// atom is not included. BDDs are constructed so that the atoms on
// every path are in strictly increasing order.
public type Bdd BddNode|boolean;
public type BddNode readonly & record {|
Atom atom;
Bdd left;
Bdd middle;
Bdd right;
|};
isolated function bddAtom(Atom atom) returns BddNode {
return { atom, left: true, middle: false, right: false };
}
isolated function bddUnion(Bdd b1, Bdd b2) returns Bdd {
if b1 === b2 {
return b1;
}
else if b1 is boolean {
return b1 == true ? true : b2;
}
else if b2 is boolean {
return b2 == true ? true : b1;
}
else {
int cmp = atomCmp(b1.atom, b2.atom);
if cmp < 0 {
return bddCreate(b1.atom,
b1.left,
bddUnion(b1.middle, b2),
b1.right);
}
else if cmp > 0 {
return bddCreate(b2.atom,
b2.left,
bddUnion(b1, b2.middle),
b2.right);
}
else {
return bddCreate(b1.atom,
bddUnion(b1.left, b2.left),
bddUnion(b1.middle, b2.middle),
bddUnion(b1.right, b2.right));
}
}
}
isolated function bddIntersect(Bdd b1, Bdd b2) returns Bdd {
if b1 === b2 {
return b1;
}
else if b1 is boolean {
return b1 == true ? b2 : false;
}
else if b2 is boolean {
return b2 == true ? b1 : false;
}
else {
int cmp = atomCmp(b1.atom, b2.atom);
if cmp < 0 {
return bddCreate(b1.atom,
bddIntersect(b1.left, b2),
bddIntersect(b1.middle, b2),
bddIntersect(b1.right, b2));
}
else if cmp > 0 {
return bddCreate(b2.atom,
bddIntersect(b1, b2.left),
bddIntersect(b1, b2.middle),
bddIntersect(b1, b2.right));
}
else {
return bddCreate(b1.atom,
bddIntersect(bddUnion(b1.left, b1.middle), bddUnion(b2.left, b2.middle)),
false,
bddIntersect(bddUnion(b1.right, b1.middle), bddUnion(b2.right, b2.middle)));
}
}
}
isolated function bddDiff(Bdd b1, Bdd b2) returns Bdd {
if b1 === b2 {
return false;
}
else if b2 is boolean {
return b2 == true ? false : b1;
}
else if b1 is boolean {
return b1 == true ? bddComplement(b2) : false;
}
else {
int cmp = atomCmp(b1.atom, b2.atom);
if cmp < 0 {
return bddCreate(b1.atom,
bddDiff(bddUnion(b1.left, b1.middle), b2),
false,
bddDiff(bddUnion(b1.right, b1.middle), b2));
}
else if cmp > 0 {
return bddCreate(b2.atom,
bddDiff(b1, bddUnion(b2.left, b2.middle)),
false,
bddDiff(b1, bddUnion(b2.right, b2.middle)));
}
else {
// There is an error in the Castagna paper for this formula.
// The union needs to be materialized here.
// The original formula does not work in a case like (a0|a1) - a0.
// Castagna confirms that the following formula is the correct one.
return bddCreate(b1.atom,
bddDiff(bddUnion(b1.left, b1.middle), bddUnion(b2.left, b2.middle)),
false,
bddDiff(bddUnion(b1.right, b1.middle), bddUnion(b2.right, b2.middle)));
}
}
}
isolated function bddComplement(Bdd b) returns Bdd {
if b is boolean {
return !b;
}
else {
return bddNodeComplement(b);
}
}
isolated function bddNodeComplement(BddNode b) returns Bdd {
if b.right === false {
return bddCreate(b.atom,
false,
bddComplement(bddUnion(b.left, b.middle)),
bddComplement(b.middle));
}
else if b.left === false {
return bddCreate(b.atom,
bddComplement(b.middle),
bddComplement(bddUnion(b.right, b.middle)),
false);
}
else if b.middle === false {
return bddCreate(b.atom,
bddComplement(b.left),
bddComplement(bddUnion(b.left, b.right)),
bddComplement(b.right));
}
else {
// There is a typo in the Frisch PhD thesis for this formula.
// (It has left and right swapped.)
// Castagna (the PhD supervisor) confirms that this is the correct formula.
return bddCreate(b.atom,
bddComplement(bddUnion(b.left, b.middle)),
false,
bddComplement(bddUnion(b.right, b.middle)));
}
}
// this is just for observing
isolated int bddCount = 0;
isolated function bddCreate(Atom atom, Bdd left, Bdd middle, Bdd right) returns Bdd {
if middle == true {
return true;
}
if left == right {
return bddUnion(left, middle);
}
lock {
bddCount += 1;
}
return { atom, left, middle, right };
}
public isolated function bddGetCount() returns int {
lock {
return bddCount;
}
}
// order RecAtom < TypeAtom
isolated function atomCmp(Atom a1, Atom a2) returns int {
if a1 is RecAtom {
if a2 is RecAtom {
return a1 - a2;
}
else {
return -1;
}
}
else if a2 is RecAtom {
return 1;
}
else {
return a1.index - a2.index;
}
}
// This is for debugging purposes.
// It uses the Frisch/Castagna notation.
isolated function bddToString(Bdd b, boolean inner = false) returns string {
if b is boolean {
return b ? "1" : "0";
}
else {
string str;
Atom a = b.atom;
if a is RecAtom {
str = "r" + a.toString();
}
else {
str = "a" + a.index.toString();
}
str += "?" + bddToString(b.left, true) + ":" + bddToString(b.middle, true) + ":" + bddToString(b.right, true);
if inner {
str = "(" + str + ")";
}
return str;
}
}