-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathndf.cpp
358 lines (300 loc) · 10.5 KB
/
ndf.cpp
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include <string_view>
#include <iostream>
#include <memory>
#include <vector>
#include <functional>
#include <algorithm>
// Normal Disjunction Form builder
using String = std::string;
class ASTFunction;
using ASTPtr = std::shared_ptr<ASTFunction>;
using ASTs = std::vector<ASTPtr>;
struct ASTFunction
{
String name;
String id;
ASTPtr arguments;
ASTs children;
template <typename STREAM>
void dumpTree(STREAM & ostr, size_t indent = 0) const;
void normTree();
String getID() const {return name + ':' + id;}
bool operator==(const ASTFunction& rhs) {return (void*)this == (void*)&rhs;}
bool isOr() const {return name == "or";}
bool isAnd() const {return name == "and";}
ASTFunction(std::string_view name_, std::string_view id_ = "")
:name(name_), id(id_)
{}
};
template <typename STREAM>
void ASTFunction::dumpTree(STREAM & ostr, size_t indent) const
{
String indent_str(indent, '-');
ostr << indent_str << getID() << ", " << (void*)this << std::endl;
for (const auto & child : children)
{
child->dumpTree(ostr, indent + 1);
}
}
// Distribute Or over And
// https://proofwiki.org/wiki/Rule_of_Distribution/Conjunction_Distributes_over_Disjunction/Left_Distributive/Formulation_2
ASTPtr distribute(ASTPtr node)
{
ASTPtr or_child;
if (node->isAnd())
{
{
ASTs lst;
for (auto & arg : node->children)
{
lst.push_back(distribute(arg));
}
node->children = lst;
}
auto or_child = std::find_if(node->children.begin(), node->children.end(), [](auto & arg)
{
return arg->isOr();
});
if (or_child == node->children.end())
{
return node;
}
ASTs rest_lst;
for (auto & arg : node->children)
{
if (*arg == **or_child)
{
continue;
}
rest_lst.push_back(arg);
}
ASTs lst;
for (auto & arg : (*or_child)->children)
{
auto and_node = std::make_shared<ASTFunction>("and");
and_node->children.push_back(arg);
and_node->children.insert(and_node->children.end(), std::begin(rest_lst), std::end(rest_lst));
lst.push_back(distribute(and_node));
}
if (lst.size() == 1)
{
return lst[0];
}
else
{
auto ret = std::make_shared<ASTFunction>("or");
ret->children = lst;
return ret;
}
}
else if (node->isOr())
{
ASTs lst;
for (auto & arg : node->children)
{
lst.push_back(distribute(arg));
}
node->children = lst;
return node;
}
else
{
return node;
}
}
void ASTFunction::normTree()
{
for (bool touched = true; touched; )
{
touched = false;
ASTs new_children;
for (const auto & child : children)
{
if (child->isOr() && isOr() || child->isAnd() && isAnd())
{
std::copy(child->children.begin(), child->children.end(), std::back_inserter(new_children));
touched = true;
}
else
{
new_children.push_back(child);
}
}
children = new_children;
}
for (const auto & child : children)
{
child->normTree();
}
}
ASTPtr f1()
{
auto node1 = std::make_shared<ASTFunction>("or");
auto node2 = std::make_shared<ASTFunction>("and");
auto node21 = std::make_shared<ASTFunction>("literal", "two_1");
auto node22 = std::make_shared<ASTFunction>("literal", "two_2");
node2->children.push_back(node21);
node2->children.push_back(node22);
auto node3 = std::make_shared<ASTFunction>("and");
auto node31 = std::make_shared<ASTFunction>("literal", "three_1");
auto node32 = std::make_shared<ASTFunction>("literal", "three_2");
node3->children.push_back(node31);
node3->children.push_back(node32);
node1->children.push_back(node2);
node1->children.push_back(node3);
return node1;
}
ASTPtr f2()
{
auto node1 = std::make_shared<ASTFunction>("and", "one");
auto node2 = std::make_shared<ASTFunction>("or", "two");
auto node21 = std::make_shared<ASTFunction>("literal", "two_1");
auto node22 = std::make_shared<ASTFunction>("literal", "two_2");
node2->children.push_back(node21);
node2->children.push_back(node22);
auto node3 = std::make_shared<ASTFunction>("and", "three");
auto node31 = std::make_shared<ASTFunction>("literal", "three_1");
auto node32 = std::make_shared<ASTFunction>("literal", "three_2");
node3->children.push_back(node31);
node3->children.push_back(node32);
node1->children.push_back(node2);
node1->children.push_back(node3);
return node1;
}
ASTPtr f3()
{
auto node1 = std::make_shared<ASTFunction>("and");
auto node2 = std::make_shared<ASTFunction>("or");
auto node21 = std::make_shared<ASTFunction>("literal", "two_1");
auto node22 = std::make_shared<ASTFunction>("literal", "two_2");
node2->children.push_back(node21);
node2->children.push_back(node22);
auto node3 = std::make_shared<ASTFunction>("literal", "three");
node1->children.push_back(node2);
node1->children.push_back(node3);
return node1;
}
ASTPtr f4()
{
auto node1 = std::make_shared<ASTFunction>("and");
auto node2 = std::make_shared<ASTFunction>("or");
auto node21 = std::make_shared<ASTFunction>("literal", "two_1");
auto node22 = std::make_shared<ASTFunction>("literal", "two_2");
node2->children.push_back(node21);
node2->children.push_back(node22);
auto node31 = std::make_shared<ASTFunction>("literal", "three_1");
auto node32 = std::make_shared<ASTFunction>("literal", "three_2");
auto node33 = std::make_shared<ASTFunction>("literal", "three_3");
node1->children.push_back(node2);
node1->children.push_back(node31);
node1->children.push_back(node32);
node1->children.push_back(node33);
return node1;
}
ASTPtr f5()
{
auto node1 = std::make_shared<ASTFunction>("and");
auto node2 = std::make_shared<ASTFunction>("or");
auto node21 = std::make_shared<ASTFunction>("literal", "two_1");
auto node22 = std::make_shared<ASTFunction>("literal", "two_2");
node2->children.push_back(node21);
node2->children.push_back(node22);
auto node3 = std::make_shared<ASTFunction>("or");
auto node31 = std::make_shared<ASTFunction>("literal", "three_1");
auto node32 = std::make_shared<ASTFunction>("literal", "three_2");
node3->children.push_back(node31);
node3->children.push_back(node32);
node1->children.push_back(node2);
node1->children.push_back(node3);
return node1;
}
ASTPtr f6()
{
auto node1 = std::make_shared<ASTFunction>("and");
auto node11 = std::make_shared<ASTFunction>("literal", "one_1");
auto node12 = std::make_shared<ASTFunction>("literal", "one_2");
auto node13 = std::make_shared<ASTFunction>("and");
node1->children.push_back(node11);
node1->children.push_back(node12);
node1->children.push_back(node13);
auto node131 = std::make_shared<ASTFunction>("or");
auto node1311 = std::make_shared<ASTFunction>("literal", "three_1");
auto node1312 = std::make_shared<ASTFunction>("literal", "three_2");
node131->children.push_back(node1311);
node131->children.push_back(node1312);
auto node132 = std::make_shared<ASTFunction>("literal", "two_1");
node13->children.push_back(node131);
node13->children.push_back(node132);
return node1;
}
ASTPtr f7()
{
auto node1 = std::make_shared<ASTFunction>("and");
auto node2 = std::make_shared<ASTFunction>("or");
auto node21 = std::make_shared<ASTFunction>("literal", "two_1");
auto node22 = std::make_shared<ASTFunction>("literal", "two_2");
node2->children.push_back(node21);
node2->children.push_back(node22);
auto node3 = std::make_shared<ASTFunction>("or");
auto node31 = std::make_shared<ASTFunction>("literal", "three_1");
auto node32 = std::make_shared<ASTFunction>("literal", "three_2");
node3->children.push_back(node31);
node3->children.push_back(node32);
node1->children.push_back(node2);
node1->children.push_back(node3);
auto node0 = std::make_shared<ASTFunction>("or");
auto node01 = std::make_shared<ASTFunction>("literal", "zero_1");
node0->children.push_back(node01);
node0->children.push_back(node1);
return node0;
}
ASTPtr f8()
{
auto node1 = std::make_shared<ASTFunction>("or");
auto node11 = std::make_shared<ASTFunction>("or");
auto node12 = std::make_shared<ASTFunction>("literal", "two_1");
node1->children.push_back(node11);
node1->children.push_back(node12);
auto node111 = std::make_shared<ASTFunction>("literal", "three_1");
auto node112 = std::make_shared<ASTFunction>("literal", "three_2");
node11->children.push_back(node111);
node11->children.push_back(node112);
return node1;
}
ASTPtr f9()
{
auto node1 = std::make_shared<ASTFunction>("and");
auto node11 = std::make_shared<ASTFunction>("literal", "literal_equal");
auto node12 = std::make_shared<ASTFunction>("or");
auto node13 = std::make_shared<ASTFunction>("or");
node1->children.push_back(node11);
node1->children.push_back(node12);
node1->children.push_back(node13);
auto node121 = std::make_shared<ASTFunction>("literal", "literal_more");
auto node122 = std::make_shared<ASTFunction>("literal", "literal_less");
node12->children.push_back(node121);
node12->children.push_back(node122);
auto node131 = std::make_shared<ASTFunction>("literal", "literal_more_or_equal");
auto node132 = std::make_shared<ASTFunction>("literal", "literal_less_or_equal");
node13->children.push_back(node131);
node13->children.push_back(node132);
return node1;
}
int main(int argc, char** argv)
{
for (auto f : {f1, f2, f3, f4, f5, f6, f7, f8, f9})
{
auto root = f();
std::cout << std::endl << "Original ===========================" << std::endl;
root->dumpTree(std::cout);
root->normTree();
std::cout << "Normalized ===========================" << std::endl;
root->dumpTree(std::cout);
auto distributed = distribute(root);
std::cout << "Distributed ===========================" << std::endl;
distributed->dumpTree(std::cout);
distributed->normTree();
std::cout << "Normalized ===========================" << std::endl;
distributed->dumpTree(std::cout);
}
}