-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
76 lines (58 loc) · 2.61 KB
/
Copy pathexample.cpp
File metadata and controls
76 lines (58 loc) · 2.61 KB
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
/* Testing boolean expressions for equivalence.
https://github.com/kosarev/eqbool
Copyright (C) 2023-2026 Ivan Kosarev.
mail@ivankosarev.com
Published under the MIT license.
*/
#include "eqbool.h"
int main() {
eqbool::term_set<std::string> terms;
eqbool::eqbool_context eqbools(terms);
eqbool::order_context orders(eqbools);
using eqbool::eqbool;
eqbool eqfalse = eqbools.get_false();
eqbool eqtrue = eqbools.get_true();
// Constants are evaluated and eliminated right away.
assert((eqfalse | ~eqfalse) == eqtrue);
// Expressions get simplified on construction.
eqbool a = eqbools.get(terms.add("a"));
eqbool b = eqbools.get(terms.add("b"));
assert((~b | ~eqbools.ifelse(a, b, ~b)) == (~a | ~b));
// Identical, but differently spelled expressions are uniquified.
eqbool c = eqbools.get(terms.add("c"));
assert(((a | b) | c) == (a | (b | c)));
// Speed is king, so simplifications that require deep traversals,
// restructuring of existing nodes and increasing the diversity of
// SAT clauses are intentionally omitted.
eqbool d = eqbools.get(terms.add("d"));
eqbool e1 = a & ((b | c) | (~a | ((~b | (d | ~c)) & (c | ~b))));
eqbool e2 = a;
assert(!eqbools.is_trivially_equiv(e1, e2));
// The equivalence can still be established using SAT.
assert(eqbools.is_equiv(e1, e2));
// From there on, the expressions are considered identical.
assert(eqbools.is_trivially_equiv(e1, e2));
// They then can be propagated to their simplest known forms.
assert(e1 != e2);
e1.propagate();
e2.propagate();
assert(e1 == e2);
// Order terms are ordinary terms stating that one of two
// given values comes before the other; the opposite order
// is the negation of the same term.
eqbool a_b = eqbools.get(terms.add("a<b"));
eqbool b_c = eqbools.get(terms.add("b<c"));
eqbool a_c = eqbools.get(terms.add("a<c"));
orders.register_order(a_b, terms.add("a"), terms.add("b"));
orders.register_order(b_c, terms.add("b"), terms.add("c"));
orders.register_order(a_c, terms.add("a"), terms.add("c"));
// Orderings whose terms chain into a cycle are impossible.
assert(orders.is_never(a_b & b_c & ~a_c));
assert(orders.is_possible(a_b & b_c));
// Under consistent orders, spelling out the ordering
// implied by transitivity changes nothing...
assert(orders.is_equiv(a_b & b_c, a_b & b_c & a_c));
// ...but as plain propositions the two expressions differ,
// and the order context never confuses the two views.
assert(!eqbools.is_equiv(a_b & b_c, a_b & b_c & a_c));
}