-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsat_solver.cpp
528 lines (461 loc) · 23.9 KB
/
sat_solver.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#include "sat_solver.h"
const token_t* const and_token = new const token_t("AND"); // "And" operator.
const token_t* const or_token = new const token_t("OR"); // "Or" operator.
const token_t* const not_token = new const token_t("NOT"); // "Not" operator.
const token_t* const impl_token = new const token_t("IMPLIES"); // "Implies" operator.
const token_t* const equiv_token = new const token_t("EQUIVALENCE"); // "Equivalence" operator.
/*
sat_solver_exit
Delete all pre-defined tokens.
*/
void sat_solver_exit()
{
delete and_token;
delete or_token;
delete not_token;
delete impl_token;
delete equiv_token;
}
/*
to_cnf
Convert a sentence to conjunctive normal form.
Returns a newly-allocated sentence if not returning the sentence.
*/
const sentence_entity_t* const to_cnf(const sentence_entity_t* const sentence)
{
const sentence_entity_t* const equiv_elimination_sentence = equiv_elimination(sentence);
const sentence_entity_t* const impl_elimination_sentence = impl_elimination(equiv_elimination_sentence);
const sentence_entity_t* const not_elimination_sentence = not_elimination(impl_elimination_sentence);
const sentence_entity_t* const distribute_or_sentence = distribute_or(not_elimination_sentence);
const sentence_entity_t* const o = distribute_and(distribute_or_sentence); // Only runs each once due to problem with expansion (described in report).
if (!equals(impl_elimination_sentence, equiv_elimination_sentence)) // Based on whether impl_elimination returns a newly-allocated sentence.
{
delete equiv_elimination_sentence;
}
if (!equals(not_elimination_sentence, impl_elimination_sentence)) // Based on whether not_elimination returns a newly-allocated sentence.
{
delete impl_elimination_sentence;
}
if (!equals(distribute_or_sentence, not_elimination_sentence)) // Based on whether distribute_or returns a newly-allocated sentence.
{
delete not_elimination_sentence;
}
if (!equals(o, distribute_or_sentence)) // Based on whether distribute_and returns a newly-allocated sentence.
{
delete distribute_or_sentence;
}
return o;
}
/*
resolve
Generate new sentences based on existing sentences.
*/
llist<sentence_entity_t>* resolve(const sentence_entity_t* const sentence_a, const sentence_entity_t* const sentence_b)
{
return nullptr; // To be implemented at a later date.
}
/*
are_complements
Return whether the sentences are negations of each other.
*/
bool are_complements(const sentence_entity_t* const sentence_a, const sentence_entity_t* const sentence_b)
{
if (sentence_a->data->detail // If the detail tokenstream exists in sentence_a...
&& sentence_a->data->detail->data == NOT_TOKEN // and the first token in sentence_a's tokenstream is the "not" token...
&& sentence_a->data->implicants && !sentence_a->data->implicants->rest // and there is only one implicant in sentence_a...
&& equals(sentence_b, sentence_a->data->implicants->data)) { // and that implicant equals sentence_b...
return true;
}
if (sentence_b->data->detail // If the detail tokenstream exists in sentence_b...
&& sentence_b->data->detail->data == NOT_TOKEN // and the first token in sentence_b's tokenstream is the "not" token...
&& sentence_b->data->implicants && !sentence_b->data->implicants->rest // and there is only one implicant in sentence_b...
&& equals(sentence_a, sentence_b->data->implicants->data)) { // and that implicant equals sentence_a...
return true;
}
return false;
}
/*
equiv_elimination
Apply the biconditional elimination rule to every instance in a sentence.
Returns a newly-allocated sentence if not returning the sentence.
*/
const sentence_entity_t* const equiv_elimination(const sentence_entity_t* const sentence)
{
const sentence_entity_t* const first = (const sentence_entity_t* const)(sentence->find_exact(EQUIV_TOKEN)); // sentence_entity_t::find_exact only returns sentence_entity_t objects.
if (first)
{
const sentence_entity_t* const new_first = new sentence_entity_t(
new tokenstream_t(AND_TOKEN),
new llist<entity_t>(new sentence_entity_t(
(const tokenstream_t* const)((first->data->detail->rest)->cons(IMPL_TOKEN)), // Cut out the "equivalence" operator and insert the "implies" operator.
first->data->implicants,
(const sentence_entity_t* const)(first->rest)
)),
new sentence_entity_t(
nullptr,
new llist<entity_t>(new sentence_entity_t(
(const tokenstream_t* const)((first->rest->data->detail)->cons(IMPL_TOKEN)),
first->rest->data->implicants,
new sentence_entity_t(
first->data->detail->rest, // Cut out the "equivalence" operator.
first->data->implicants
)
))
)
); // P <-> Q => (P -> Q) & (Q -> P)
const sentence_entity_t* const new_sentence = (const sentence_entity_t* const)(sentence->replace(first, new_first)); // sentence_entity_t::replace only returns sentence_entity_t objects.
if (!equals(sentence, new_sentence) && !equals(sentence, first)) // If new_first is a newly-allocated sentence...
{
delete new_first;
}
delete first;
return equiv_elimination(new_sentence); // Run until all biconditionals are eliminated.
}
else
{
return sentence;
}
}
/*
impl_elimination
Apply the conditional elimination rule to every instance in a sentence.
Returns a newly-allocated sentence if not returning the sentence.
*/
const sentence_entity_t* const impl_elimination(const sentence_entity_t* const sentence)
{
const sentence_entity_t* const first = (const sentence_entity_t* const)(sentence->find_exact(IMPL_TOKEN)); // sentence_entity_t::find_exact only returns sentence_entity_t objects.
if (first)
{
const sentence_entity_t* const new_first = new sentence_entity_t(
(const tokenstream_t* const)((first->data->detail->rest)->cons(NOT_TOKEN)->cons(OR_TOKEN)), // Cut out the "implies" operator and insert the "or" and "not" operators.
first->data->implicants,
(const sentence_entity_t* const)(first->rest)
); // P -> Q => ~P v Q
const sentence_entity_t* const new_sentence = (const sentence_entity_t* const)(sentence->replace(first, new_first)); // sentence_entity_t::replace only returns sentence_entity_t objects.
if (!equals(sentence, new_sentence) && !equals(sentence, first)) // If new_first is a newly-allocated sentence...
{
delete new_first;
}
delete first;
return impl_elimination(new_sentence); // Run until all conditionals are eliminated.
}
else
{
return sentence;
}
}
/*
not_elimination
Apply the double-negation elimination and De Morgan's rules to every instance in a sentence.
Returns a newly-allocated sentence if not returning the sentence.
*/
const sentence_entity_t* const not_elimination(const sentence_entity_t* const sentence)
{
const llist<token_t>* const dn_list = (new llist<token_t>(NOT_TOKEN))->cons(NOT_TOKEN);
const sentence_entity_t* const first_dn = (const sentence_entity_t* const)(sentence->find_exact(dn_list));
if (first_dn)
{
const sentence_entity_t* const new_first = new sentence_entity_t(
first_dn->data->detail->rest->rest, // Cut out both "not" operators.
first_dn->data->implicants,
(const sentence_entity_t* const)(first_dn->rest)
); // ~~P => P
const sentence_entity_t* const new_sentence = (const sentence_entity_t* const)(sentence->replace(first_dn, new_first)); // sentence_entity_t::replace only returns sentence_entity_t objects.
if (!equals(sentence, new_sentence) && !equals(sentence, first_dn)) // If new_first is a newly-allocated sentence...
{
delete new_first;
}
delete first_dn;
delete dn_list;
return not_elimination(new_sentence); // Run until all instances are eliminated.
}
else
{
delete dn_list;
}
const llist<token_t>* const dm1_list = (new llist<token_t>(AND_TOKEN))->cons(NOT_TOKEN);
const sentence_entity_t* const first_dm1 = (const sentence_entity_t* const)(sentence->find_exact(dm1_list));
if (first_dm1)
{
const sentence_entity_t* const new_first = new sentence_entity_t(
(const tokenstream_t* const)((first_dm1->data->detail->rest->rest)->cons(NOT_TOKEN)->cons(OR_TOKEN)), // Cut out the "not" and "and" operators and insert the "or" and "not" operators.
first_dm1->data->implicants,
new sentence_entity_t(
(first_dm1->rest->data->detail // first_dm1->rest should exist if there is an "and" operator at first_dm1->data->detail.
? (const tokenstream_t* const)(first_dm1->rest->data->detail->cons(NOT_TOKEN))
: new tokenstream_t(NOT_TOKEN)),
first_dm1->rest->data->implicants,
(const sentence_entity_t* const)(first_dm1->rest->rest)
)
); // ~(P & Q) => ~P v ~Q
const sentence_entity_t* const new_sentence = (const sentence_entity_t* const)(sentence->replace(first_dm1, new_first)); // sentence_entity_t::replace only returns sentence_entity_t objects.
if (!equals(sentence, new_sentence) && !equals(sentence, first_dm1)) // If new_first is a newly-allocated sentence...
{
delete new_first;
}
delete first_dm1;
delete dm1_list;
return not_elimination(new_sentence); // Run until all instances are eliminated.
}
else
{
delete dm1_list;
}
const llist<token_t>* const dm2_list = (new llist<token_t>(OR_TOKEN))->cons(NOT_TOKEN);
const sentence_entity_t* const first_dm2 = (const sentence_entity_t* const)(sentence->find_exact(dm2_list));
if (first_dm2)
{
const sentence_entity_t* const new_first = new sentence_entity_t(
(const tokenstream_t* const)((first_dm1->data->detail->rest->rest)->cons(NOT_TOKEN)->cons(AND_TOKEN)), // Cut out the "not" and "or" operators and insert the "or" and "and" operators.
first_dm1->data->implicants,
new sentence_entity_t(
(first_dm1->rest->data->detail // first_dm1->rest should exist if there is an "or" operator at first_dm1->data->detail.
? (const tokenstream_t* const)(first_dm1->rest->data->detail->cons(NOT_TOKEN))
: new tokenstream_t(NOT_TOKEN)),
first_dm1->rest->data->implicants,
(const sentence_entity_t* const)(first_dm1->rest->rest)
)
); // ~(P v Q) => ~P & ~Q
const sentence_entity_t* const new_sentence = (const sentence_entity_t* const)(sentence->replace(first_dm2, new_first)); // sentence_entity_t::replace only returns sentence_entity_t objects.
if (!equals(sentence, new_sentence) && !equals(sentence, first_dm2)) // If new_first is a newly-allocated sentence...
{
delete new_first;
}
delete first_dm2;
delete dm2_list;
return not_elimination(new_sentence); // Run until all instances are eliminated.
}
else
{
delete dm2_list;
}
return sentence;
}
/*
distribute_or
Apply the principle of distributivity to every "or" clause in a sentence.
Returns a newly-allocated sentence if not returning the sentence.
*/
const sentence_entity_t* const distribute_or(const sentence_entity_t* const sentence)
{
const sentence_entity_t* const first = (const sentence_entity_t* const)(sentence->find_exact(OR_TOKEN));
if (first && first->data->implicants)
{
const sentence_entity_t* const lhs_inner_rest = first->data->implicants
? (const sentence_entity_t* const)(first->data->implicants->data->get_rest()) // sentence_entity_t::get_rest only returns sentence_entity_t objects.
: nullptr;
const sentence_entity_t* const rhs_inner_rest = first->rest->data->implicants
? (const sentence_entity_t* const)(first->rest->data->implicants->data->get_rest()) // sentence_entity_t::get_rest only returns sentence_entity_t objects.
: nullptr;
if (lhs_inner_rest // I think if lhs_inner_rest then rhs_inner_rest, but I don't want to change it in case it breaks something this close to submission.
&& ((const sentence_entity_t* const)(first->data->implicants->data))->data->detail
&& ((const sentence_entity_t* const)(first->data->implicants->data))->data->implicants
&& ((const sentence_entity_t* const)(first->data->implicants->data))->rest
&& ((const sentence_entity_t* const)(first->data->implicants->data))->data->detail->data == AND_TOKEN // If the next token is the "and" operator...
&& !first->data->detail->rest // Requires no tokens (specifically, operators) to exist directly above the "and" clause.
&& first->rest && first->rest->data->implicants) // If "R" exists in "(P & Q) v R."
{
delete lhs_inner_rest;
if (rhs_inner_rest)
{
delete rhs_inner_rest;
}
const sentence_entity_t* const new_first = new sentence_entity_t(
new tokenstream_t(AND_TOKEN),
new llist<entity_t>(new sentence_entity_t(
(const tokenstream_t* const)((((const sentence_entity_t* const)(first->data->implicants->data))->data->detail->rest)->cons(OR_TOKEN)), // Cut out the "and" operator and insert the "or" operator.
((const sentence_entity_t* const)(first->data->implicants->data))->data->implicants, // "P" in the expression below.
(const sentence_entity_t* const)(first->rest) // The sentence containing "R" in the expression below.
)),
new sentence_entity_t(
nullptr,
new llist<entity_t>(new sentence_entity_t(
(const tokenstream_t* const)((const sentence_entity_t* const)(((const sentence_entity_t* const)(first->data->implicants->data))->rest->data->detail)->cons(OR_TOKEN)), // Append the "or" operator.
((const sentence_entity_t* const)(first->data->implicants->data))->rest->data->implicants, // "Q" in the expression below.
(const sentence_entity_t* const)(first->rest) // The sentence containing "R" in the expression below.
))
)
); // (P & Q) v R => (P v R) & (Q v R)
const sentence_entity_t* const new_sentence = (const sentence_entity_t* const)(sentence->replace(first, new_first)); // sentence_entity_t::replace only returns sentence_entity_t objects.
if (!equals(sentence, new_sentence) && !equals(sentence, first)) // If new_first is a newly-allocated sentence...
{
delete new_first;
}
delete first;
return distribute_and(new_sentence); // Run until all instances are distributed.
}
else if (rhs_inner_rest
&& ((const sentence_entity_t* const)(first->rest->data->implicants->data))->data->detail
&& ((const sentence_entity_t* const)(first->rest->data->implicants->data))->data->implicants
&& ((const sentence_entity_t* const)(first->rest->data->implicants->data))->rest
&& ((const sentence_entity_t* const)(first->rest->data->implicants->data))->data->detail->data == AND_TOKEN // If the "other" next token is the "and" operator...
&& !first->rest->data->detail // Requires no tokens (specifically, operators) to exist directly above the "and" clause.
&& first->data->implicants) // If "R" exists in "R v (P & Q)."
{
if (lhs_inner_rest)
{
delete lhs_inner_rest;
}
delete rhs_inner_rest;
const sentence_entity_t* const new_first = new sentence_entity_t(
new tokenstream_t(AND_TOKEN),
new llist<entity_t>(new sentence_entity_t(
(const tokenstream_t* const)((((const sentence_entity_t* const)(first->rest->data->implicants->data))->data->detail->rest)->cons(OR_TOKEN)), // Cut out the "and" operator and insert the "or" operator.
((const sentence_entity_t* const)(first->rest->data->implicants->data))->data->implicants, // "P" in the expression below.
new sentence_entity_t(
first->data->detail->rest, // Cut out the "or" operator.
first->data->implicants
) // The sentence containing "R" in the expression below.
)),
new sentence_entity_t(
nullptr,
new llist<entity_t>(new sentence_entity_t(
(const tokenstream_t* const)((const sentence_entity_t* const)(((const sentence_entity_t* const)(first->rest->data->implicants->data))->rest->data->detail)->cons(OR_TOKEN)), // Append the "or" operator.
((const sentence_entity_t* const)(first->rest->data->implicants->data))->rest->data->implicants, // "Q" in the expression below.
new sentence_entity_t(
first->data->detail->rest, // Cut out the "or" operator.
first->data->implicants
) // The sentence containing "R" in the expression below.
))
)
); // R v (P & Q) => (P v R) & (Q v R)
const sentence_entity_t* const new_sentence = (const sentence_entity_t* const)(sentence->replace(first, new_first)); // sentence_entity_t::replace only returns sentence_entity_t objects.
if (!equals(sentence, new_sentence) && !equals(sentence, first)) // If new_first is a newly-allocated sentence...
{
delete new_first;
}
delete first;
return distribute_and(new_sentence); // Run until all instances are distributed.
}
else
{
if (lhs_inner_rest)
{
delete lhs_inner_rest;
}
if (rhs_inner_rest)
{
delete rhs_inner_rest;
}
return sentence;
}
}
else
{
return sentence;
}
}
/*
distribute_and
Apply the principle of distributivity to every "and" clause in a sentence.
Returns a newly-allocated sentence if not returning the sentence.
*/
const sentence_entity_t* const distribute_and(const sentence_entity_t* const sentence)
{
const sentence_entity_t* const first = (const sentence_entity_t* const)(sentence->find_exact(AND_TOKEN));
if (first && first->data->implicants)
{
const sentence_entity_t* const lhs_inner_rest = first->data->implicants
? (const sentence_entity_t* const)(first->data->implicants->data->get_rest()) // sentence_entity_t::get_rest only returns sentence_entity_t objects.
: nullptr;
const sentence_entity_t* const rhs_inner_rest = first->rest->data->implicants
? (const sentence_entity_t* const)(first->rest->data->implicants->data->get_rest()) // sentence_entity_t::get_rest only returns sentence_entity_t objects.
: nullptr;
if (lhs_inner_rest // I think if lhs_inner_rest then rhs_inner_rest, but I don't want to change it in case it breaks something this close to submission.
&& ((const sentence_entity_t* const)(first->data->implicants->data))->data->detail
&& ((const sentence_entity_t* const)(first->data->implicants->data))->data->implicants
&& ((const sentence_entity_t* const)(first->data->implicants->data))->rest
&& ((const sentence_entity_t* const)(first->data->implicants->data))->data->detail->data == OR_TOKEN // If the next token is the "or" operator...
&& !first->data->detail->rest // Requires no tokens (specifically, operators) to exist directly above the "or" clause.
&& first->rest && first->rest->data->implicants) // If "R" exists in "(P v Q) & R."
{
delete lhs_inner_rest;
if (rhs_inner_rest)
{
delete rhs_inner_rest;
}
const sentence_entity_t* const new_first = new sentence_entity_t(
new tokenstream_t(OR_TOKEN),
new llist<entity_t>(new sentence_entity_t(
(const tokenstream_t* const)((((const sentence_entity_t* const)(first->data->implicants->data))->data->detail->rest)->cons(AND_TOKEN)), // Cut out the "or" operator and insert the "and" operator.
((const sentence_entity_t* const)(first->data->implicants->data))->data->implicants, // "P" in the expression below.
(const sentence_entity_t* const)(first->rest) // The sentence containing "R" in the expression below.
)),
new sentence_entity_t(
nullptr,
new llist<entity_t>(new sentence_entity_t(
(const tokenstream_t* const)((const sentence_entity_t* const)(((const sentence_entity_t* const)(first->data->implicants->data))->rest->data->detail)->cons(AND_TOKEN)), // Append the "and" operator.
((const sentence_entity_t* const)(first->data->implicants->data))->rest->data->implicants, // "Q" in the expression below.
(const sentence_entity_t* const)(first->rest) // The sentence containing "R" in the expression below.
))
)
); // (P v Q) & R => (P & R) v (Q & R)
const sentence_entity_t* const new_sentence = (const sentence_entity_t* const)(sentence->replace(first, new_first)); // sentence_entity_t::replace only returns sentence_entity_t objects.
if (!equals(sentence, new_sentence) && !equals(sentence, first)) // If new_first is a newly-allocated sentence...
{
delete new_first;
}
delete first;
return distribute_and(new_sentence); // Run until all instances are distributed.
}
else if (rhs_inner_rest
&& ((const sentence_entity_t* const)(first->rest->data->implicants->data))->data->detail
&& ((const sentence_entity_t* const)(first->rest->data->implicants->data))->data->implicants
&& ((const sentence_entity_t* const)(first->rest->data->implicants->data))->rest
&& ((const sentence_entity_t* const)(first->rest->data->implicants->data))->data->detail->data == OR_TOKEN // If the "other" next token is the "or" operator...
&& !first->rest->data->detail // Requires no tokens (specifically, operators) to exist directly above the "or" clause.
&& first->data->implicants) // If "R" exists in "R & (P v Q)."
{
if (lhs_inner_rest)
{
delete lhs_inner_rest;
}
delete rhs_inner_rest;
const sentence_entity_t* const new_first = new sentence_entity_t(
new tokenstream_t(OR_TOKEN),
new llist<entity_t>(new sentence_entity_t(
(const tokenstream_t* const)((((const sentence_entity_t* const)(first->rest->data->implicants->data))->data->detail->rest)->cons(AND_TOKEN)), // Cut out the "or" operator and insert the "and" operator.
((const sentence_entity_t* const)(first->rest->data->implicants->data))->data->implicants, // "P" in the expression below.
new sentence_entity_t(
first->data->detail->rest, // Cut out the "and" operator.
first->data->implicants
) // The sentence containing "R" in the expression below.
)),
new sentence_entity_t(
nullptr,
new llist<entity_t>(new sentence_entity_t(
(const tokenstream_t* const)((const sentence_entity_t* const)(((const sentence_entity_t* const)(first->rest->data->implicants->data))->rest->data->detail)->cons(AND_TOKEN)), // Append the "and" operator.
((const sentence_entity_t* const)(first->rest->data->implicants->data))->rest->data->implicants, // "Q" in the expression below.
new sentence_entity_t(
first->data->detail->rest, // Cut out the "and" operator.
first->data->implicants
) // The sentence containing "R" in the expression below.
))
)
); // R & (P v Q) => (P & R) v (Q & R)
const sentence_entity_t* const new_sentence = (const sentence_entity_t* const)(sentence->replace(first, new_first)); // sentence_entity_t::replace only returns sentence_entity_t objects.
if (!equals(sentence, new_sentence) && !equals(sentence, first)) // If new_first is a newly-allocated sentence...
{
delete new_first;
}
delete first;
return distribute_and(new_sentence); // Run until all instances are distributed.
}
else
{
if (lhs_inner_rest)
{
delete lhs_inner_rest;
}
if (rhs_inner_rest)
{
delete rhs_inner_rest;
}
return sentence;
}
}
else
{
return sentence;
}
}