forked from vprover/vampire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Z3Interfacing.hpp
207 lines (172 loc) · 6.01 KB
/
Z3Interfacing.hpp
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
/*
* File Z3Interfacing.hpp.
*
* 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
*
* In summary, you are allowed to use Vampire for non-commercial
* purposes but not allowed to distribute, modify, copy, create derivatives,
* or use in competitions.
* For other uses of Vampire please contact developers for a different
* licence, which we will make an effort to provide.
*/
/**
* @file Z3Interfacing.hpp
* Defines class Z3Interfacing
*/
#ifndef __Z3Interfacing__
#define __Z3Interfacing__
#if VZ3
#include "Lib/DHMap.hpp"
#include "SATSolver.hpp"
#include "SATLiteral.hpp"
#include "SATClause.hpp"
#include "SATInference.hpp"
#include "SAT2FO.hpp"
#include "z3++.h"
#include "z3_api.h"
namespace SAT{
struct UninterpretedForZ3Exception : public ThrowableBase
{
UninterpretedForZ3Exception()
{
CALL("Z3Interfacing::UninterpretedForZ3Exception::UninterpretedForZ3Exception");
}
};
class Z3Interfacing : public PrimitiveProofRecordingSATSolver
{
public:
CLASS_NAME(Z3Interfacing);
USE_ALLOCATOR(Z3Interfacing);
/**
* If @c unsatCoresForAssumptions is set, the solver is configured to use
* the "unsat-core" option (may negatively affect performance) and uses
* this feature to extract a subset of used assumptions when
* called via solveUnderAssumptions.
*/
Z3Interfacing(const Shell::Options& opts, SAT2FO& s2f, bool unsatCoresForAssumptions = false);
void addClause(SATClause* cl, bool withGuard);
void addClause(SATClause* cl) override { addClause(cl,false); }
virtual Status solve(unsigned conflictCountLimit) override;
/**
* If status is @c SATISFIABLE, return assignment of variable @c var
*/
virtual VarAssignment getAssignment(unsigned var) override;
/**
* If status is @c SATISFIABLE, return 0 if the assignment of @c var is
* implied only by unit propagation (i.e. does not depend on any decisions)
*/
virtual bool isZeroImplied(unsigned var) override;
/**
* Collect zero-implied literals.
*
* Can be used in SATISFIABLE and UNKNOWN state.
*
* @see isZeroImplied()
*/
virtual void collectZeroImplied(SATLiteralStack& acc) override;
/**
* Return a valid clause that contains the zero-implied literal
* and possibly the assumptions that implied it. Return 0 if @c var
* was an assumption itself.
* If called on a proof producing solver, the clause will have
* a proper proof history.
*/
virtual SATClause* getZeroImpliedCertificate(unsigned var) override;
// Not required for Z3, but let's keep track of the counter
virtual void ensureVarCount(unsigned newVarCnt) override {
CALL("Z3Interfacing::ensureVarCnt");
_varCnt = max(newVarCnt,_varCnt);
}
virtual unsigned newVar() override {
CALL("Z3Interfacing::newVar");
return ++_varCnt;
}
// Currently not implemented for Z3
virtual void suggestPolarity(unsigned var, unsigned pol) override {}
void addAssumption(SATLiteral lit, bool withGuard);
virtual void addAssumption(SATLiteral lit) override { addAssumption(lit,false); }
virtual void retractAllAssumptions() override { _assumptions.resize(0); }
virtual bool hasAssumptions() const override { return !_assumptions.empty(); }
Status solveUnderAssumptions(const SATLiteralStack& assumps, unsigned,bool,bool);
virtual Status solveUnderAssumptions(const SATLiteralStack& assumps, unsigned c, bool p) override
{ return solveUnderAssumptions(assumps,c,p,false); }
/**
* Record the association between a SATLiteral var and a Literal
* In TWLSolver this is used for computing niceness values
*/
virtual void recordSource(unsigned satlitvar, Literal* lit) override {
// unsupported by Z3; intentionally no-op
};
/**
* The set of inserted clauses may not be propositionally UNSAT
* due to theory reasoning inside Z3.
* We cannot later minimize this set with minisat.
*
* TODO: think of extracting true refutation from Z3 instead.
*/
SATClauseList* getRefutationPremiseList() override{ return 0; }
SATClause* getRefutation() override;
void reset(){
sat2fo.reset();
_solver.reset();
_status = UNKNOWN; // I set it to unknown as I do not reset
}
private:
// just to conform to the interface
unsigned _varCnt;
// Memory belongs to Splitter
SAT2FO& sat2fo;
//DHMap<unsigned,Z3_sort> _sorts;
z3::sort getz3sort(unsigned s);
// Helper funtions for the translation
z3::expr to_int(z3::expr e) {
return z3::expr(e.ctx(), Z3_mk_real2int(e.ctx(), e));
}
z3::expr to_real(z3::expr e) {
return z3::expr(e.ctx(), Z3_mk_int2real(e.ctx(), e));
}
z3::expr ceiling(z3::expr e){
return -to_real(to_int((e)));
}
z3::expr is_even(z3::expr e) {
z3::context& ctx = e.ctx();
z3::expr two = ctx.int_val(2);
z3::expr m = z3::expr(ctx, Z3_mk_mod(ctx, e, two));
return m == 0;
}
z3::expr truncate(z3::expr e) {
return ite(e >= 0, to_int(e), ceiling(e));
}
void addTruncatedOperations(z3::expr_vector, Interpretation qi, Interpretation ti, unsigned srt);
void addFloorOperations(z3::expr_vector, Interpretation qi, Interpretation ti, unsigned srt);
void addIntNonZero(z3::expr);
void addRealNonZero(z3::expr);
public:
// not sure why this one is public
z3::expr getz3expr(Term* trm,bool islit,bool&nameExpression, bool withGuard=false);
Term* evaluateInModel(Term* trm);
private:
z3::expr getRepresentation(SATLiteral lit,bool withGuard);
Status _status;
z3::context _context;
z3::solver _solver;
z3::model _model;
z3::expr_vector _assumptions;
bool _unsatCoreForAssumptions;
bool _showZ3;
bool _unsatCoreForRefutations;
DHSet<unsigned> _namedExpressions;
z3::expr getNameExpr(unsigned var){
vstring name = "v"+Lib::Int::toString(var);
return _context.bool_const(name.c_str());
}
};
}//end SAT namespace
#endif /* if VZ3 */
#endif /*Z3Interfacing*/