-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMainSolver.h
More file actions
355 lines (280 loc) · 12 KB
/
MainSolver.h
File metadata and controls
355 lines (280 loc) · 12 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
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
/*
* Copyright (c) 2012 - 2022, Antti Hyvarinen <antti.hyvarinen@gmail.com>
* Copyright (c) 2022 - 2024, Martin Blicha <martin.blicha@gmail.com>
*
* SPDX-License-Identifier: MIT
*
*/
#ifndef MAINSOLVER_H
#define MAINSOLVER_H
#include "PartitionManager.h"
#include <cnfizers/Tseitin.h>
#include <common/ScopedVector.h>
#include <common/TermNames.h>
#include <models/Model.h>
#include <proof/InterpolationContext.h>
#include <smtsolvers/SimpSMTSolver.h>
#include <unsatcores/UnsatCore.h>
#include <memory>
namespace opensmt {
class Logic;
class sstat {
public:
explicit sstat(int v) : value(v) {}
bool operator==(sstat s) const { return value == s.value; }
bool operator!=(sstat s) const { return value != s.value; }
sstat() : value(0) {}
sstat(lbool l) {
if (l == l_True)
value = 1;
else if (l == l_False)
value = -1;
else if (l == l_Undef)
value = 0;
else
assert(false);
}
char getValue() const { return value; }
friend sstat toSstat(int v);
private:
char value;
};
inline sstat toSstat(int v) {
return sstat(v);
}
sstat const s_True = toSstat(1);
sstat const s_False = toSstat(-1);
sstat const s_Undef = toSstat(0);
sstat const s_Error = toSstat(2);
class MainSolver {
public:
MainSolver(Logic & logic, SMTConfig & conf, std::string name);
MainSolver(std::unique_ptr<Theory> th, std::unique_ptr<TermMapper> tm, std::unique_ptr<THandler> thd,
std::unique_ptr<SimpSMTSolver> ss, Logic & logic, SMTConfig & conf, std::string name);
virtual ~MainSolver() = default;
MainSolver(MainSolver const &) = delete;
MainSolver & operator=(MainSolver const &) = delete;
MainSolver(MainSolver &&) = default;
MainSolver & operator=(MainSolver &&) = delete;
SMTConfig & getConfig() const { return config; }
Logic & getLogic() const { return logic; }
SimpSMTSolver & getSMTSolver() { return *smt_solver; }
SimpSMTSolver const & getSMTSolver() const { return *smt_solver; }
THandler & getTHandler() { return *thandler; }
THandler const & getTHandler() const { return *thandler; }
// Increases/decreases the level of the assertion stack [1]
// [1]: The SMT-LIB Standard: Version 2.6, section 4.1.4
// https://smt-lib.org/language.shtml
void push();
bool pop();
// The current assertion level; the first assertion level is 0
std::size_t getAssertionLevel() const;
void insertFormula(PTRef fla);
// Alias for `insertFormula`, reserved for future use
void addAssertion(PTRef fla) { return insertFormula(fla); }
std::size_t getInsertedFormulasCount() const { return insertedAssertionsCount; }
// Alias for `getInsertedFormulasCount`, reserved for future use
std::size_t getAssertionsCount() const { return getInsertedFormulasCount(); }
// Uses tryAddTermName and inserts the formula only on success
bool tryAddNamedAssertion(PTRef, std::string const & name);
// Try add a unique name for a term already included in the assertions
bool tryAddTermNameFor(PTRef, std::string const & name);
void initialize();
virtual sstat check(); // A wrapper for solve which simplifies the loaded formulas and initializes the solvers
sstat solve();
// Simplify formulas until all are simplified or the instance is detected unsat
// Skip assertion levels that have already been simplified
sstat simplifyFormulas();
// Alias for `simplifyFormulas`, reserved for future use
sstat preprocess() { return simplifyFormulas(); }
[[nodiscard]] sstat getStatus() const { return status; }
// Returns a copy of all assertions of the currently valid assertion levels of the the assertion stack
// I.e., *excluding* popped assertions
vec<PTRef> getCurrentAssertions() const;
// Returns just a view to the assertions
auto getCurrentAssertionsView() const { return getCurrentAssertionsViewImpl(); }
vec<PTRef> const & getAssertionsAtCurrentLevel() const { return getAssertionsAtLevel(getAssertionLevel()); }
vec<PTRef> const & getAssertionsAtLevel(std::size_t) const;
[[deprecated("Use printCurrentAssertionsAsQuery")]]
void printFramesAsQuery() const {
printCurrentAssertionsAsQuery();
}
void printCurrentAssertionsAsQuery() const;
void printCurrentAssertionsAsQuery(std::ostream &) const;
// Values
lbool getTermValue(PTRef tr) const;
// Returns model of the last query (must be in satisfiable state)
std::unique_ptr<Model> getModel();
std::unique_ptr<UnsatCore> getUnsatCore() const;
// Prints proof of the last query (must be in unsatisfiable state)
void printResolutionProofSMT2() const;
void printResolutionProofSMT2(std::ostream &) const;
// Returns interpolation context for the last query (must be in UNSAT state)
std::unique_ptr<InterpolationContext> getInterpolationContext();
[[deprecated("Use tryAddNamedAssertion or tryAddTermNameFor")]]
TermNames & getTermNames() {
return termNames;
}
TermNames const & getTermNames() const { return termNames; }
// Notify this particular solver to stop the computation
// For stopping at the global scope, refer to GlobalStop.h
void notifyStop() { smt_solver->notifyStop(); }
static std::unique_ptr<Theory> createTheory(Logic & logic, SMTConfig & config);
protected:
friend class UnsatCoreBuilderBase;
using FrameId = uint32_t;
struct PushFrame {
public:
FrameId getId() const { return id; }
int size() const { return formulas.size(); }
void push(PTRef tr) { formulas.push(tr); }
PTRef operator[](int i) const { return formulas[i]; }
vec<PTRef> formulas;
bool unsat{false}; // If true then the stack of frames with this frame at top is UNSAT
PushFrame(PushFrame const &) = delete;
PushFrame & operator=(PushFrame const &) = delete;
PushFrame(PushFrame &&) = default;
PushFrame & operator=(PushFrame &&) = default;
explicit PushFrame(uint32_t id) : id(id) {}
private:
FrameId id;
};
class AssertionStack {
public:
[[nodiscard]] PushFrame const & last() const {
assert(not frames.empty());
return frames.back();
}
[[nodiscard]] PushFrame & last() {
assert(not frames.empty());
return frames.back();
}
[[nodiscard]] std::size_t frameCount() const { return frames.size(); }
PushFrame const & operator[](std::size_t i) const {
assert(i < frames.size());
return frames[i];
}
PushFrame & operator[](std::size_t i) {
assert(i < frames.size());
return frames[i];
}
void push() { frames.emplace_back(frameId++); }
void pop() { frames.pop_back(); }
void add(PTRef fla) {
assert(frameCount() > 0);
last().push(fla);
}
private:
std::vector<PushFrame> frames;
uint32_t frameId = 0;
};
struct SubstitutionResult {
Logic::SubstMap usedSubstitution;
PTRef result{PTRef_Undef};
};
class Preprocessor {
public:
void push();
void pop();
void initialize();
void addPreprocessedFormula(PTRef);
[[nodiscard]] span<PTRef const> getPreprocessedFormulas() const;
[[nodiscard]] Logic::SubstMap getCurrentSubstitutions() const { return substitutions.current(); }
void setSubstitutions(std::size_t level, Logic::SubstMap && subs) { substitutions.set(level, std::move(subs)); }
void prepareForProcessingFrame(std::size_t frameIndex);
private:
class Substitutions {
public:
void push() { perFrameSubst.emplace_back(); }
void pop() { perFrameSubst.pop_back(); }
void set(std::size_t level, Logic::SubstMap && subs) { perFrameSubst.at(level) = std::move(subs); }
[[nodiscard]] Logic::SubstMap current() const {
Logic::SubstMap allSubst;
for (auto const & subs : perFrameSubst) {
for (PTRef key : subs.getKeys()) {
assert(not allSubst.has(key));
allSubst.insert(key, subs[key]);
}
}
return allSubst;
}
private:
std::vector<Logic::SubstMap> perFrameSubst;
};
void pushInternal();
void popInternal();
Substitutions substitutions;
ScopedVector<PTRef> preprocessedFormulas;
std::size_t solverFrameCount{1};
std::size_t internalFrameCount{1};
};
Theory & getTheory() { return *theory; }
Theory const & getTheory() const { return *theory; }
TermMapper & getTermMapper() const { return *term_mapper; }
PartitionManager & getPartitionManager() { return pmanager; }
// TODO: inefficient
vec<PTRef> getCurrentAssertionsViewImpl() const { return getCurrentAssertions(); }
static std::unique_ptr<SimpSMTSolver> createInnerSolver(SMTConfig & config, THandler & thandler);
PTRef newFrameTerm(FrameId frameId) {
assert(frameId != 0);
auto name = std::string(Logic::s_framev_prefix) + std::to_string(frameId);
PTRef frameTerm = logic.mkBoolVar(name.c_str());
Lit l = term_mapper->getOrCreateLit(frameTerm);
term_mapper->setFrozen(var(l));
smt_solver->addAssumptionVar(var(l));
return frameTerm;
}
bool isLastFrameUnsat() const { return frames.last().unsat; }
void rememberLastFrameUnsat() { frames.last().unsat = true; }
void rememberUnsatFrame(std::size_t frameIndex) {
assert(frameIndex < frames.frameCount());
for (; frameIndex < frames.frameCount(); ++frameIndex) {
frames[frameIndex].unsat = true;
}
}
inline bool trackPartitions() const;
virtual bool tryPreprocessFormulasOfFrame(std::size_t);
virtual PTRef preprocessFormulasDefault(vec<PTRef> const & frameFormulas, PreprocessingContext const &);
virtual vec<PTRef> preprocessFormulasPerPartition(vec<PTRef> const & frameFormulas, PreprocessingContext const &);
virtual PTRef preprocessFormula(PTRef, PreprocessingContext const &);
virtual PTRef preprocessFormulaBeforeFinalTheoryPreprocessing(PTRef, PreprocessingContext const &);
virtual void preprocessFormulaDoFinalTheoryPreprocessing(PreprocessingContext const &);
virtual PTRef preprocessFormulaAfterFinalTheoryPreprocessing(PTRef, PreprocessingContext const &);
PTRef rewriteMaxArity(PTRef root);
virtual sstat solve_(vec<FrameId> const & enabledFrames);
sstat giveToSolver(PTRef root, FrameId push_id);
PTRef applyLearntSubstitutions(PTRef fla);
PTRef substitutionPass(PTRef fla, PreprocessingContext const & context);
SubstitutionResult computeSubstitutions(PTRef fla);
AssertionStack frames;
sstat status = s_Undef; // The status of the last solver call
private:
std::unique_ptr<Theory> theory;
std::unique_ptr<TermMapper> term_mapper;
std::unique_ptr<THandler> thandler;
std::unique_ptr<SimpSMTSolver> smt_solver;
TermNames termNames;
Logic & logic;
PartitionManager pmanager;
SMTConfig & config;
Tseitin ts;
Preprocessor preprocessor;
TimeVal query_timer; // How much time we spend solving.
std::string solver_name; // Name for the solver
int check_called = 0; // A counter on how many times check was called.
vec<PTRef> frameTerms;
std::size_t firstNotPreprocessedFrame = 0;
std::size_t insertedAssertionsCount = 0;
std::vector<std::size_t> preprocessedAssertionsCountPerFrame;
};
bool MainSolver::trackPartitions() const {
assert(smt_solver);
if (smt_solver->logsResolutionProof()) { return true; }
if (config.produce_assignments()) { return true; }
// Even if computed independently of resolution proofs, we must track partitions
if (config.produce_unsat_cores()) { return true; }
if (config.produce_inter()) { return true; }
return false;
}
} // namespace opensmt
#endif // MAINSOLVER_H