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
/*++
Copyright (c) 2022 Microsoft Corporation
Module Name:
tseitin_theory_checker.cpp
Abstract:
Plugin for checking quantifier instantiations
Author:
Nikolaj Bjorner (nbjorner) 2022-10-07
TODOs:
- handle distinct
- handle other internalization from euf_internalize
- equiv should be modulo commutativity (the E-graph indexes expressions modulo commutativity of top-level operator)
- should we log rules for root clauses too? Root clauses should follow from input.
They may be simplified using Tseitin transformation. For example, (and a b) is clausified into
two clauses a, b.
- Tesitin checking could also be performed by depth-bounded SAT (e.g., using BDDs)
--*/
#include "ast/ast_pp.h"
#include "sat/smt/tseitin_theory_checker.h"
namespace tseitin {
expr_ref_vector theory_checker::clause(app* jst) {
expr_ref_vector result(m);
result.append(jst->get_num_args(), jst->get_args());
return result;
}
bool theory_checker::check(app* jst) {
expr* main_expr = nullptr;
unsigned max_depth = 0;
expr* a, * x, * y, * z, * u, * v;
for (expr* arg : *jst) {
unsigned arg_depth = get_depth(arg);
if (arg_depth > max_depth) {
main_expr = arg;
max_depth = arg_depth;
}
if (arg_depth == max_depth && m.is_not(main_expr)) {
if (m.is_not(arg, x) && m.is_not(main_expr, y) &&
is_app(x) && is_app(y) &&
to_app(x)->get_num_args() < to_app(y)->get_num_args())
continue;
main_expr = arg;
}
}
if (!main_expr)
return false;
// (or (and a b) (not a) (not b))
// (or (and (not a) b) a (not b))
if (m.is_and(main_expr)) {
scoped_mark sm(*this);
for (expr* arg : *jst)
complement_mark(arg);
for (expr* arg : *to_app(main_expr))
if (!is_complement(arg) && !m.is_true(arg))
return false;
return true;
}
// (or (or a b) (not a))
if (m.is_or(main_expr)) {
scoped_mark sm(*this);
for (expr* arg : *jst)
complement_mark(arg);
for (expr* arg : *to_app(main_expr))
if (is_complement(arg))
return true;
return false;
}
// (or (= a b) a b)
// (or (= a b) (not a) (not b))
// (or (= (not a) b) a (not b))
if (m.is_eq(main_expr, x, y) && m.is_bool(x)) {
scoped_mark sm(*this);
for (expr* arg : *jst)
complement_mark(arg);
if (is_marked(x) && is_marked(y))
return true;
if (is_complement(x) && is_complement(y))
return true;
}
if (m.is_eq(main_expr, x, y) && m.is_ite(x, z, u, v)) {
scoped_mark sm(*this);
for (expr* arg : *jst)
complement_mark(arg);
if (is_marked(z) && equiv(y, v))
return true;
if (is_complement(z) && equiv(y, u))
return true;
}
// (or (if a b c) (not b) (not c))
// (or (if a b c) a (not c))
// (or (if a b c) (not a) (not b))
if (m.is_ite(main_expr, x, y, z) && m.is_bool(z)) {
scoped_mark sm(*this);
for (expr* arg : *jst)
complement_mark(arg);
if (is_marked(x) && is_complement(z))
return true;
if (is_complement(x) && is_complement(y))
return true;
if (is_complement(y) && is_complement(z))
return true;
IF_VERBOSE(0, verbose_stream() << mk_pp(main_expr, m) << "\n");
}
// (or (=> a b) a)
// (or (=> a b) (not b))
if (m.is_implies(main_expr, x, y)) {
scoped_mark sm(*this);
for (expr* arg : *jst)
complement_mark(arg);
if (is_marked(x))
return true;
if (is_complement(y))
return true;
}
// (or (xor a b c d) a b c (not d))
if (m.is_xor(main_expr)) {
scoped_mark sm(*this);
for (expr* arg : *jst)
complement_mark(arg);
return xor_holds(to_app(main_expr), true);
}
if (m.is_not(main_expr, a)) {
// (or (not a) a')
for (expr* arg : *jst)
if (equiv(a, arg))
return true;
// (or (not (and a b)) a)
if (m.is_and(a)) {
scoped_mark sm(*this);
for (expr* arg : *jst)
mark(arg);
for (expr* arg : *to_app(a))
if (is_marked(arg))
return true;
}
// (or (not (or a b) a b))
if (m.is_or(a)) {
scoped_mark sm(*this);
for (expr* arg : *jst)
mark(arg);
for (expr* arg : *to_app(a))
if (!is_marked(arg) && !m.is_false(arg))
return false;
return true;
}
// (or (not (= a b) (not a) b)
if (m.is_eq(a, x, y) && m.is_bool(x)) {
scoped_mark sm(*this);
for (expr* arg : *jst)
complement_mark(arg);
if (is_marked(x) && is_complement(y))
return true;
if (is_marked(y) && is_complement(x))
return true;
}
// (or (not (if a b c)) (not a) b)
// (or (not (if a b c)) a c)
if (m.is_ite(a, x, y, z) && m.is_bool(z)) {
scoped_mark sm(*this);
for (expr* arg : *jst)
complement_mark(arg);
if (is_complement(x) && is_marked(y))
return true;
if (is_marked(x) && is_marked(z))
return true;
if (is_marked(y) && is_marked(z))
return true;
}
// (or (not (=> a b)) b (not a))
if (m.is_implies(a, x, y)) {
scoped_mark sm(*this);
for (expr* arg : *jst)
complement_mark(arg);
if (is_complement(x) && is_marked(y))
return true;
}
// (or (not (xor a b c d)) a b (not c) (not d))
if (m.is_xor(a)) {
scoped_mark sm(*this);
for (expr* arg : *jst)
complement_mark(arg);
if (xor_holds(to_app(a), false))
return true;
}
IF_VERBOSE(0, verbose_stream() << "miss " << mk_pp(main_expr, m) << "\n");
}
return false;
}
// The clause is a tautology for the gate x when x evaluates to expected
// under the assignment that falsifies every other clause literal. That
// assignment is only determined for the arguments of x that occur in
// the clause (or are constants): a clause literal l makes argument l
// false and argument (not l) true. x is true iff an odd number of its
// arguments are true.
bool theory_checker::xor_holds(app* x, bool expected) {
bool value = false;
for (expr* arg : *x) {
if (m.is_true(arg) || is_complement(arg))
value = !value;
else if (!m.is_false(arg) && !is_marked(arg))
return false;
}
return value == expected;
}
bool theory_checker::equiv(expr* a, expr* b) {
if (a == b)
return true;
if (!is_app(a) || !is_app(b))
return false;
if (to_app(a)->get_decl() != to_app(b)->get_decl())
return false;
if (!to_app(a)->get_decl()->is_commutative())
return false;
if (to_app(a)->get_num_args() != 2)
return false;
return to_app(a)->get_arg(0) == to_app(b)->get_arg(1) &&
to_app(a)->get_arg(1) == to_app(b)->get_arg(0);
}
}