-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
53 lines (39 loc) · 1.58 KB
/
Copy pathexample.cpp
File metadata and controls
53 lines (39 loc) · 1.58 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
/* Testing boolean expressions for equivalence.
https://github.com/kosarev/eqbool
Copyright (C) 2023-2025 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);
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);
}