1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Aina Niemetz, Morgan Deters |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* The solver for SMT queries in an SmtEngine. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "smt/smt_solver.h" |
17 |
|
|
18 |
|
#include "options/main_options.h" |
19 |
|
#include "options/smt_options.h" |
20 |
|
#include "prop/prop_engine.h" |
21 |
|
#include "smt/assertions.h" |
22 |
|
#include "smt/env.h" |
23 |
|
#include "smt/preprocessor.h" |
24 |
|
#include "smt/smt_engine.h" |
25 |
|
#include "smt/smt_engine_state.h" |
26 |
|
#include "smt/smt_engine_stats.h" |
27 |
|
#include "theory/logic_info.h" |
28 |
|
#include "theory/theory_engine.h" |
29 |
|
#include "theory/theory_traits.h" |
30 |
|
|
31 |
|
using namespace std; |
32 |
|
|
33 |
|
namespace cvc5 { |
34 |
|
namespace smt { |
35 |
|
|
36 |
9522 |
SmtSolver::SmtSolver(Env& env, |
37 |
|
SmtEngineState& state, |
38 |
|
Preprocessor& pp, |
39 |
9522 |
SmtEngineStatistics& stats) |
40 |
|
: d_env(env), |
41 |
|
d_state(state), |
42 |
|
d_pp(pp), |
43 |
|
d_stats(stats), |
44 |
|
d_theoryEngine(nullptr), |
45 |
9522 |
d_propEngine(nullptr) |
46 |
|
{ |
47 |
9522 |
} |
48 |
|
|
49 |
6875 |
SmtSolver::~SmtSolver() {} |
50 |
|
|
51 |
6278 |
void SmtSolver::finishInit(const LogicInfo& logicInfo) |
52 |
|
{ |
53 |
|
// We have mutual dependency here, so we add the prop engine to the theory |
54 |
|
// engine later (it is non-essential there) |
55 |
6278 |
d_theoryEngine.reset(new TheoryEngine(d_env)); |
56 |
|
|
57 |
|
// Add the theories |
58 |
87892 |
for (theory::TheoryId id = theory::THEORY_FIRST; id < theory::THEORY_LAST; |
59 |
|
++id) |
60 |
|
{ |
61 |
81614 |
theory::TheoryConstructor::addTheory(d_theoryEngine.get(), id); |
62 |
|
} |
63 |
|
// Add the proof checkers for each theory |
64 |
6278 |
ProofNodeManager* pnm = d_env.getProofNodeManager(); |
65 |
6278 |
if (pnm) |
66 |
|
{ |
67 |
143 |
d_theoryEngine->initializeProofChecker(pnm->getChecker()); |
68 |
|
} |
69 |
6278 |
Trace("smt-debug") << "Making prop engine..." << std::endl; |
70 |
|
/* force destruction of referenced PropEngine to enforce that statistics |
71 |
|
* are unregistered by the obsolete PropEngine object before registered |
72 |
|
* again by the new PropEngine object */ |
73 |
6278 |
d_propEngine.reset(nullptr); |
74 |
6278 |
d_propEngine.reset(new prop::PropEngine(d_theoryEngine.get(), d_env)); |
75 |
|
|
76 |
6278 |
Trace("smt-debug") << "Setting up theory engine..." << std::endl; |
77 |
6278 |
d_theoryEngine->setPropEngine(getPropEngine()); |
78 |
6278 |
Trace("smt-debug") << "Finishing init for theory engine..." << std::endl; |
79 |
6278 |
d_theoryEngine->finishInit(); |
80 |
6278 |
d_propEngine->finishInit(); |
81 |
6278 |
} |
82 |
|
|
83 |
57 |
void SmtSolver::resetAssertions() |
84 |
|
{ |
85 |
|
/* Create new PropEngine. |
86 |
|
* First force destruction of referenced PropEngine to enforce that |
87 |
|
* statistics are unregistered by the obsolete PropEngine object before |
88 |
|
* registered again by the new PropEngine object */ |
89 |
57 |
d_propEngine.reset(nullptr); |
90 |
57 |
d_propEngine.reset(new prop::PropEngine(d_theoryEngine.get(), d_env)); |
91 |
57 |
d_theoryEngine->setPropEngine(getPropEngine()); |
92 |
|
// Notice that we do not reset TheoryEngine, nor does it require calling |
93 |
|
// finishInit again. In particular, TheoryEngine::finishInit does not |
94 |
|
// depend on knowing the associated PropEngine. |
95 |
57 |
d_propEngine->finishInit(); |
96 |
57 |
} |
97 |
|
|
98 |
|
void SmtSolver::interrupt() |
99 |
|
{ |
100 |
|
if (d_propEngine != nullptr) |
101 |
|
{ |
102 |
|
d_propEngine->interrupt(); |
103 |
|
} |
104 |
|
if (d_theoryEngine != nullptr) |
105 |
|
{ |
106 |
|
d_theoryEngine->interrupt(); |
107 |
|
} |
108 |
|
} |
109 |
|
|
110 |
6875 |
void SmtSolver::shutdown() |
111 |
|
{ |
112 |
6875 |
if (d_propEngine != nullptr) |
113 |
|
{ |
114 |
6275 |
d_propEngine->shutdown(); |
115 |
|
} |
116 |
6875 |
if (d_theoryEngine != nullptr) |
117 |
|
{ |
118 |
6275 |
d_theoryEngine->shutdown(); |
119 |
|
} |
120 |
6875 |
} |
121 |
|
|
122 |
9377 |
Result SmtSolver::checkSatisfiability(Assertions& as, |
123 |
|
const std::vector<Node>& assumptions, |
124 |
|
bool isEntailmentCheck) |
125 |
|
{ |
126 |
|
// update the state to indicate we are about to run a check-sat |
127 |
9377 |
bool hasAssumptions = !assumptions.empty(); |
128 |
9377 |
d_state.notifyCheckSat(hasAssumptions); |
129 |
|
|
130 |
|
// then, initialize the assertions |
131 |
9377 |
as.initializeCheckSat(assumptions, isEntailmentCheck); |
132 |
|
|
133 |
|
// make the check, where notice smt engine should be fully inited by now |
134 |
|
|
135 |
9377 |
Trace("smt") << "SmtSolver::check()" << endl; |
136 |
|
|
137 |
9377 |
const std::string& filename = d_env.getOptions().driver.filename; |
138 |
9377 |
ResourceManager* rm = d_env.getResourceManager(); |
139 |
9377 |
if (rm->out()) |
140 |
|
{ |
141 |
|
Result::UnknownExplanation why = |
142 |
|
rm->outOfResources() ? Result::RESOURCEOUT : Result::TIMEOUT; |
143 |
|
return Result(Result::ENTAILMENT_UNKNOWN, why, filename); |
144 |
|
} |
145 |
9377 |
rm->beginCall(); |
146 |
|
|
147 |
|
// Make sure the prop layer has all of the assertions |
148 |
9377 |
Trace("smt") << "SmtSolver::check(): processing assertions" << endl; |
149 |
9377 |
processAssertions(as); |
150 |
9363 |
Trace("smt") << "SmtSolver::check(): done processing assertions" << endl; |
151 |
|
|
152 |
18726 |
TimerStat::CodeTimer solveTimer(d_stats.d_solveTime); |
153 |
|
|
154 |
9363 |
Chat() << "solving..." << endl; |
155 |
9363 |
Trace("smt") << "SmtSolver::check(): running check" << endl; |
156 |
18710 |
Result result = d_propEngine->checkSat(); |
157 |
|
|
158 |
9347 |
rm->endCall(); |
159 |
18694 |
Trace("limit") << "SmtSolver::check(): cumulative millis " |
160 |
18694 |
<< rm->getTimeUsage() << ", resources " |
161 |
9347 |
<< rm->getResourceUsage() << endl; |
162 |
|
|
163 |
28028 |
if ((options::solveRealAsInt() || options::solveIntAsBV() > 0) |
164 |
18723 |
&& result.asSatisfiabilityResult().isSat() == Result::UNSAT) |
165 |
|
{ |
166 |
6 |
result = Result(Result::SAT_UNKNOWN, Result::UNKNOWN_REASON); |
167 |
|
} |
168 |
|
// flipped if we did a global negation |
169 |
9347 |
if (as.isGlobalNegated()) |
170 |
|
{ |
171 |
5 |
Trace("smt") << "SmtSolver::process global negate " << result << std::endl; |
172 |
5 |
if (result.asSatisfiabilityResult().isSat() == Result::UNSAT) |
173 |
|
{ |
174 |
4 |
result = Result(Result::SAT); |
175 |
|
} |
176 |
1 |
else if (result.asSatisfiabilityResult().isSat() == Result::SAT) |
177 |
|
{ |
178 |
|
// Only can answer unsat if the theory is satisfaction complete. This |
179 |
|
// includes linear arithmetic and bitvectors, which are the primary |
180 |
|
// targets for the global negate option. Other logics are possible here |
181 |
|
// but not considered. |
182 |
2 |
LogicInfo logic = d_env.getLogicInfo(); |
183 |
1 |
if ((logic.isPure(theory::THEORY_ARITH) && logic.isLinear()) || |
184 |
|
logic.isPure(theory::THEORY_BV)) |
185 |
|
{ |
186 |
1 |
result = Result(Result::UNSAT); |
187 |
|
} |
188 |
|
else |
189 |
|
{ |
190 |
|
result = Result(Result::SAT_UNKNOWN, Result::UNKNOWN_REASON); |
191 |
|
} |
192 |
|
} |
193 |
5 |
Trace("smt") << "SmtSolver::global negate returned " << result << std::endl; |
194 |
|
} |
195 |
|
|
196 |
|
// set the filename on the result |
197 |
18694 |
Result r = Result(result, filename); |
198 |
|
|
199 |
|
// notify our state of the check-sat result |
200 |
9347 |
d_state.notifyCheckSatResult(hasAssumptions, r); |
201 |
|
|
202 |
9347 |
return r; |
203 |
|
} |
204 |
|
|
205 |
15264 |
void SmtSolver::processAssertions(Assertions& as) |
206 |
|
{ |
207 |
23815 |
TimerStat::CodeTimer paTimer(d_stats.d_processAssertionsTime); |
208 |
15264 |
d_env.getResourceManager()->spendResource(Resource::PreprocessStep); |
209 |
15264 |
Assert(d_state.isFullyReady()); |
210 |
|
|
211 |
15264 |
preprocessing::AssertionPipeline& ap = as.getAssertionPipeline(); |
212 |
|
|
213 |
15264 |
if (ap.size() == 0) |
214 |
|
{ |
215 |
|
// nothing to do |
216 |
6713 |
return; |
217 |
|
} |
218 |
|
|
219 |
|
// process the assertions with the preprocessor |
220 |
8551 |
d_pp.process(as); |
221 |
|
|
222 |
|
// end: INVARIANT to maintain: no reordering of assertions or |
223 |
|
// introducing new ones |
224 |
|
|
225 |
|
// Push the formula to SAT |
226 |
|
{ |
227 |
8541 |
Chat() << "converting to CNF..." << endl; |
228 |
8541 |
const std::vector<Node>& assertions = ap.ref(); |
229 |
|
// It is important to distinguish the input assertions from the skolem |
230 |
|
// definitions, as the decision justification heuristic treates the latter |
231 |
|
// specially. |
232 |
8541 |
preprocessing::IteSkolemMap& ism = ap.getIteSkolemMap(); |
233 |
8541 |
d_propEngine->assertInputFormulas(assertions, ism); |
234 |
|
} |
235 |
|
|
236 |
|
// clear the current assertions |
237 |
8537 |
as.clearCurrent(); |
238 |
|
} |
239 |
|
|
240 |
236857 |
TheoryEngine* SmtSolver::getTheoryEngine() { return d_theoryEngine.get(); } |
241 |
|
|
242 |
40325 |
prop::PropEngine* SmtSolver::getPropEngine() { return d_propEngine.get(); } |
243 |
|
|
244 |
363 |
theory::QuantifiersEngine* SmtSolver::getQuantifiersEngine() |
245 |
|
{ |
246 |
363 |
Assert(d_theoryEngine != nullptr); |
247 |
363 |
return d_theoryEngine->getQuantifiersEngine(); |
248 |
|
} |
249 |
|
|
250 |
|
Preprocessor* SmtSolver::getPreprocessor() { return &d_pp; } |
251 |
|
|
252 |
|
} // namespace smt |
253 |
22755 |
} // namespace cvc5 |