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
/*
* This file is part of the source code of the software program
* Vampire. It is protected by applicable
* copyright laws.
*
* This source code is distributed under the licence found here
* https://vprover.github.io/license.html
* and in the source directory
*/
#include "Test/UnitTesting.hpp"
#include "Test/SyntaxSugar.hpp"
#include "Test/SyntaxSugar.hpp"
#include "SAT/Z3Interfacing.hpp"
#if VZ3
#define DBG_ON 0
#if 0
#define EXPORT_FILE "exported.bla"
#else
#define EXPORT_FILE ""
#endif
#define EXPORT_SYNTAX Shell::Options::ProblemExportSyntax::API_CALLS
// #if DBG_ON
// #define DEBUG(...)
// #else
// #define DEBUG(...) DBG(__VA_ARGS__)
// #endif
using namespace std;
using namespace Shell;
using namespace SAT;
using Sort = unsigned;
using FuncId = unsigned;
/////////////////////////////////////////////////////////////////////////////////////////
// 1) TEST SOLVING
/////////////////////////////////////////////////////////////////////////////////////////
/** runs z3 on a bunch of vampire literals as assumptions, and checks the status afterwards */
void checkStatus(SAT::Z3Interfacing& z3, SAT2FO& s2f, Status expected, Stack<Literal*> assumptions)
{
SATLiteralStack assms;
for (auto a : assumptions) {
// Stack<SATLiteral> clause{s2f.toSAT(a)};
// z3.addClause(SATClause::fromStack(clause));
assms.push(s2f.toSAT(a));
}
auto status = z3.solveUnderAssumptions(assms);
if(status != expected) {
cout << "[ input ] " << endl;
for (auto a : assumptions) {
cout << "\t" << *a << endl;
}
cout << "[ expected ] " << expected << endl;
cout << "[ is ] " << status << endl;
if (status == Status::SATISFIABLE) {
cout << "[ model ] " << z3.getModel() << endl;
}
exit(-1);
}
}
void checkStatus(Status expected, Stack<Literal*> assumptions)
{
SAT2FO s2f;
SAT::Z3Interfacing z3(s2f, /* show z3 */ DBG_ON == 1, /* unsat core */ false, EXPORT_FILE, EXPORT_SYNTAX);
checkStatus(z3, s2f, expected, assumptions);
}
////////////////////////////////////
// Real Tests
/////////////////
TEST_FUN(solve__real__simple_01) {
NUMBER_SUGAR(Real)
checkStatus(
Status::UNSATISFIABLE,
{ num(3) == num(0) });
}
TEST_FUN(solve__real__simple_02) {
NUMBER_SUGAR(Real)
DECL_CONST(a, Real)
checkStatus(
Status::SATISFIABLE,
{ num(3) == a });
}
TEST_FUN(solve__rat__simple_03) {
NUMBER_SUGAR(Real)
checkStatus(
Status::UNSATISFIABLE,
{ num(3) == num(3) + 2 * num(7) });
}
TEST_FUN(solve__rat__simple_04) {
NUMBER_SUGAR(Real)
checkStatus(
Status::UNSATISFIABLE,
{ num(17) != num(3) + 2 * num(7) });
}
////////////////////////////////////
// FOOL Tests
/////////////////
TEST_FUN(solve__fool__simple_01) {
DECL_VAR(x, 0);
checkStatus(
Status::SATISFIABLE,
{ fool(false) == x });
}
TEST_FUN(solve__fool__simple_02) {
DECL_VAR(x, 0);
checkStatus(
Status::SATISFIABLE,
{ fool(true) == x });
}
TEST_FUN(solve__fool__simple_03) {
checkStatus(
Status::UNSATISFIABLE,
{ fool(true) == fool(false) });
}
////////////////////////////////////
// term algebra tests
/////////////////
#define DECL_LIST(sort) \
DECL_SORT(list) \
\
DECL_CONST(nil, list) \
DECL_FUNC(cons, { sort, list }, list) \
\
DECL_TERM_ALGEBRA(list, {nil, cons}) \
__ALLOW_UNUSED( \
auto head = cons.dtor(0); \
auto tail = cons.dtor(1); \
) \
TEST_FUN(solve__dty__01) {
DECL_SORT(alpha)
DECL_LIST(alpha)
DECL_CONST(a0, alpha)
DECL_CONST(a1, alpha)
checkStatus(Status::UNSATISFIABLE, { cons(a0, nil) == nil });
checkStatus(Status::UNSATISFIABLE, { cons(a0, nil) == cons(a1, nil), a0 != a1 });
}
// data Even = Zero | SuccEven Odd
// data Odd = SuccOdd Even
#define DECL_EVEN_ODD \
DECL_SORT(even) \
DECL_SORT(odd) \
\
DECL_CONST(zero, even) \
DECL_FUNC(succEven, { odd }, even) \
\
DECL_FUNC(succOdd, { even }, odd) \
\
DECL_TERM_ALGEBRA(even, {zero, succEven}) \
DECL_TERM_ALGEBRA(odd , { succOdd }) \
TEST_FUN(solve__dty__02) {
DECL_EVEN_ODD
checkStatus(Status::UNSATISFIABLE, { succEven(succOdd(zero)) == zero });
}
TEST_FUN(solve__dty__03_01) {
// we have a non-mutually recursive datatype that depends on a non-mutual but recursive datatype that
DECL_EVEN_ODD
DECL_LIST(even)
// request non-mutual first
checkStatus(Status::UNSATISFIABLE, { cons(succEven(succOdd(zero)), nil) == cons(zero, nil) });
}
TEST_FUN(solve__dty__03_02) {
// we have a non-mutually recursive datatype that depends on a non-mutual but recursive datatype that
DECL_EVEN_ODD
DECL_LIST(even)
// request mutual only
checkStatus(Status::UNSATISFIABLE, { succEven(succOdd(zero)) == zero });
}
TEST_FUN(solve__dty__03_03) {
// we have a non-mutually recursive datatype that depends on a non-mutual but recursive datatype that
DECL_EVEN_ODD
DECL_LIST(even)
// request mutual first
checkStatus(Status::UNSATISFIABLE, { succEven(succOdd(zero)) == zero });
checkStatus(Status::UNSATISFIABLE, { cons(succEven(succOdd(zero)), nil) == cons(zero, nil) });
}
/////////////////////////////////////////////////////////////////////////////////////////
// 2) TEST INSTANTIATION
/////////////////////////////////////////////////////////////////////////////////////////
void checkInstantiation(SAT::Z3Interfacing& z3, SAT2FO& s2f, Stack<Literal*> assumptions, TermList toInstantiate, TermList expected)
{
SATLiteralStack assms;
for (auto a : assumptions) {
assms.push(s2f.toSAT(a));
}
auto status = z3.solveUnderAssumptions(assms);
ASS_EQ(status, Status::SATISFIABLE);
auto result = z3.evaluateInModel(toInstantiate.term());
if (result != expected.term()) {
cout << "[ input ] " << endl;
for (auto a : assumptions) {
cout << "\t" << *a << endl;
}
cout << "[ toInstantiate ] " << toInstantiate << endl;
cout << "[ expected ] " << expected << endl;
cout << "[ is ] " << ( result == nullptr ? "null" : result->toString() ) << endl;
cout << "[ model ] " << z3.getModel() << endl;
exit(-1);
}
}
/**
* Runs z3 on a bunch of vampire literals as assumptions, that need to be satisfyable.
* Then the term toInstantiate will be instantiated with the model. The instantiated
* term will be checked to be equal to the term expected.
*/
void checkInstantiation(Stack<Literal*> assumptions, TermList toInstantiate, TermList expected)
{
SAT2FO s2f;
SAT::Z3Interfacing z3(s2f, /* show z3 */ DBG_ON == 1, /* unsat core */ false, EXPORT_FILE, EXPORT_SYNTAX);
return checkInstantiation(z3, s2f, assumptions, toInstantiate, expected);
}
////////////////////////////////////
// Real Tests
/////////////////
TEST_FUN(instantiate__rat__simple_01) {
NUMBER_SUGAR(Real)
DECL_CONST(c, Real)
checkInstantiation(
{ c * 3 == 9 },
c, num(3)
);
}
TEST_FUN(instantiate__rat__simple_02) {
NUMBER_SUGAR(Real)
DECL_CONST(c, Real)
checkInstantiation(
{ c * c == 9, c < 0 },
c, num(-3)
);
}
////////////////////////////////////
// term algebra tests
/////////////////
TEST_FUN(instantiate__list_01) {
NUMBER_SUGAR(Real)
DECL_LIST(Real)
DECL_CONST(c, Real)
checkInstantiation(
{ cons(c, nil) == cons(num(3), nil) },
c, num(3)
);
}
TEST_FUN(instantiate__list_02) {
NUMBER_SUGAR(Real)
DECL_LIST(Real)
DECL_CONST(l, list)
DECL_CONST(h, Real)
DECL_CONST(t, list)
checkInstantiation(
{ tail(l) == cons(num(2), nil)
, head(l) == 1
, cons(h,t) == l // <- necessary since selectors are partial
},
l, cons(1,cons(2,nil))
);
}
TEST_FUN(segfault01) {
Z3_config config = Z3_mk_config();
Z3_context context = Z3_mk_context(config);
Z3_symbol consName = Z3_mk_string_symbol(context, "e00");
Z3_symbol sortNames = Z3_mk_string_symbol(context, "Enum");
Z3_func_decl enumCtor;
Z3_func_decl enumDiscr;
Z3_sort sorts = Z3_mk_enumeration_sort(context,
sortNames, 1,
&consName, &enumCtor, &enumDiscr);
Z3_symbol c1_sym = Z3_mk_string_symbol(context, "c1");
Z3_symbol c2_sym = Z3_mk_string_symbol(context, "c1");
Z3_ast c1 = Z3_mk_const(context, c1_sym, sorts);
Z3_ast c2 = Z3_mk_const(context, c2_sym, sorts);
Z3_solver solver = Z3_mk_solver(context);
Z3_solver_push(context, solver);
Z3_ast expr = Z3_mk_eq(context, c1, c2);
Z3_solver_assert(context, solver, expr);
Z3_solver_check(context, solver);
Z3_solver_check(context, solver);
Z3_solver_pop(context, solver, 1);
// std::cout << "segfault occurs between here ..." << std::endl;
Z3_solver_check(context, solver);
// std::cout << "... and here" << std::endl;
}
TEST_FUN(segfault02) {
DECL_SORT(Enum)
DECL_CONST(e00, Enum)
DECL_TERM_ALGEBRA(Enum, { e00 })
DECL_CONST(inst159, Enum)
DECL_CONST(inst160, Enum)
SAT2FO s2f;
SAT::Z3Interfacing z3(s2f, /* show z3 */ DBG_ON == 1, /* unsat core */ false, EXPORT_FILE, EXPORT_SYNTAX);
checkStatus(z3, s2f, Status::SATISFIABLE, { inst159 == inst160 });
z3.solve();
}
#endif // VZ3