1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andrew Reynolds, Tim King, Alex Ozdemir |
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 |
|
* Arithmetic theory. |
14 |
|
*/ |
15 |
|
|
16 |
|
#include "theory/arith/theory_arith.h" |
17 |
|
|
18 |
|
#include "options/smt_options.h" |
19 |
|
#include "proof/proof_checker.h" |
20 |
|
#include "proof/proof_rule.h" |
21 |
|
#include "smt/smt_statistics_registry.h" |
22 |
|
#include "theory/arith/arith_rewriter.h" |
23 |
|
#include "theory/arith/equality_solver.h" |
24 |
|
#include "theory/arith/infer_bounds.h" |
25 |
|
#include "theory/arith/nl/nonlinear_extension.h" |
26 |
|
#include "theory/arith/theory_arith_private.h" |
27 |
|
#include "theory/ext_theory.h" |
28 |
|
#include "theory/rewriter.h" |
29 |
|
#include "theory/theory_model.h" |
30 |
|
|
31 |
|
using namespace std; |
32 |
|
using namespace cvc5::kind; |
33 |
|
|
34 |
|
namespace cvc5 { |
35 |
|
namespace theory { |
36 |
|
namespace arith { |
37 |
|
|
38 |
6271 |
TheoryArith::TheoryArith(Env& env, OutputChannel& out, Valuation valuation) |
39 |
|
: Theory(THEORY_ARITH, env, out, valuation), |
40 |
|
d_ppRewriteTimer( |
41 |
12542 |
statisticsRegistry().registerTimer("theory::arith::ppRewriteTimer")), |
42 |
|
d_astate(env, valuation), |
43 |
|
d_im(env, *this, d_astate, d_pnm), |
44 |
|
d_ppre(context(), d_pnm), |
45 |
|
d_bab(env, d_astate, d_im, d_ppre, d_pnm), |
46 |
|
d_eqSolver(nullptr), |
47 |
6271 |
d_internal(new TheoryArithPrivate(*this, env, d_bab)), |
48 |
|
d_nonlinearExtension(nullptr), |
49 |
|
d_opElim(d_pnm, logicInfo()), |
50 |
|
d_arithPreproc(env, d_astate, d_im, d_pnm, d_opElim), |
51 |
25084 |
d_rewriter(d_opElim) |
52 |
|
{ |
53 |
|
// currently a cyclic dependency to TheoryArithPrivate |
54 |
6271 |
d_astate.setParent(d_internal); |
55 |
|
// indicate we are using the theory state object and inference manager |
56 |
6271 |
d_theoryState = &d_astate; |
57 |
6271 |
d_inferManager = &d_im; |
58 |
|
|
59 |
6271 |
if (options().arith.arithEqSolver) |
60 |
|
{ |
61 |
15 |
d_eqSolver.reset(new EqualitySolver(env, d_astate, d_im)); |
62 |
|
} |
63 |
6271 |
} |
64 |
|
|
65 |
18804 |
TheoryArith::~TheoryArith(){ |
66 |
6268 |
delete d_internal; |
67 |
12536 |
} |
68 |
|
|
69 |
6271 |
TheoryRewriter* TheoryArith::getTheoryRewriter() { return &d_rewriter; } |
70 |
|
|
71 |
143 |
ProofRuleChecker* TheoryArith::getProofChecker() |
72 |
|
{ |
73 |
143 |
return d_internal->getProofChecker(); |
74 |
|
} |
75 |
|
|
76 |
6271 |
bool TheoryArith::needsEqualityEngine(EeSetupInfo& esi) |
77 |
|
{ |
78 |
|
// if the equality solver is enabled, then it is responsible for setting |
79 |
|
// up the equality engine |
80 |
6271 |
if (d_eqSolver != nullptr) |
81 |
|
{ |
82 |
15 |
return d_eqSolver->needsEqualityEngine(esi); |
83 |
|
} |
84 |
|
// otherwise, the linear arithmetic solver is responsible for setting up |
85 |
|
// the equality engine |
86 |
6256 |
return d_internal->needsEqualityEngine(esi); |
87 |
|
} |
88 |
6271 |
void TheoryArith::finishInit() |
89 |
|
{ |
90 |
6271 |
const LogicInfo& logic = logicInfo(); |
91 |
6271 |
if (logic.isTheoryEnabled(THEORY_ARITH) && logic.areTranscendentalsUsed()) |
92 |
|
{ |
93 |
|
// witness is used to eliminate square root |
94 |
2758 |
d_valuation.setUnevaluatedKind(kind::WITNESS); |
95 |
|
// we only need to add the operators that are not syntax sugar |
96 |
2758 |
d_valuation.setUnevaluatedKind(kind::EXPONENTIAL); |
97 |
2758 |
d_valuation.setUnevaluatedKind(kind::SINE); |
98 |
2758 |
d_valuation.setUnevaluatedKind(kind::PI); |
99 |
|
} |
100 |
|
// only need to create nonlinear extension if non-linear logic |
101 |
6271 |
if (logic.isTheoryEnabled(THEORY_ARITH) && !logic.isLinear()) |
102 |
|
{ |
103 |
6858 |
d_nonlinearExtension.reset( |
104 |
3429 |
new nl::NonlinearExtension(d_env, *this, d_astate)); |
105 |
|
} |
106 |
6271 |
if (d_eqSolver != nullptr) |
107 |
|
{ |
108 |
15 |
d_eqSolver->finishInit(); |
109 |
|
} |
110 |
|
// finish initialize in the old linear solver |
111 |
6271 |
d_internal->finishInit(); |
112 |
6271 |
} |
113 |
|
|
114 |
433390 |
void TheoryArith::preRegisterTerm(TNode n) |
115 |
|
{ |
116 |
433390 |
if (d_nonlinearExtension != nullptr) |
117 |
|
{ |
118 |
212306 |
d_nonlinearExtension->preRegisterTerm(n); |
119 |
|
} |
120 |
433391 |
d_internal->preRegisterTerm(n); |
121 |
433389 |
} |
122 |
|
|
123 |
414355 |
void TheoryArith::notifySharedTerm(TNode n) { d_internal->notifySharedTerm(n); } |
124 |
|
|
125 |
410713 |
TrustNode TheoryArith::ppRewrite(TNode atom, std::vector<SkolemLemma>& lems) |
126 |
|
{ |
127 |
821426 |
CodeTimer timer(d_ppRewriteTimer, /* allow_reentrant = */ true); |
128 |
410713 |
Debug("arith::preprocess") << "arith::preprocess() : " << atom << endl; |
129 |
|
|
130 |
410713 |
if (atom.getKind() == kind::EQUAL) |
131 |
|
{ |
132 |
12502 |
return d_ppre.ppRewriteEq(atom); |
133 |
|
} |
134 |
398211 |
Assert(Theory::theoryOf(atom) == THEORY_ARITH); |
135 |
|
// Eliminate operators. Notice we must do this here since other |
136 |
|
// theories may generate lemmas that involve non-standard operators. For |
137 |
|
// example, quantifier instantiation may use TO_INTEGER terms; SyGuS may |
138 |
|
// introduce non-standard arithmetic terms appearing in grammars. |
139 |
|
// call eliminate operators. In contrast to expandDefinitions, we eliminate |
140 |
|
// *all* extended arithmetic operators here, including total ones. |
141 |
398217 |
return d_arithPreproc.eliminate(atom, lems, false); |
142 |
|
} |
143 |
|
|
144 |
8799 |
Theory::PPAssertStatus TheoryArith::ppAssert( |
145 |
|
TrustNode tin, TrustSubstitutionMap& outSubstitutions) |
146 |
|
{ |
147 |
8799 |
return d_internal->ppAssert(tin, outSubstitutions); |
148 |
|
} |
149 |
|
|
150 |
63519 |
void TheoryArith::ppStaticLearn(TNode n, NodeBuilder& learned) |
151 |
|
{ |
152 |
63519 |
d_internal->ppStaticLearn(n, learned); |
153 |
63519 |
} |
154 |
|
|
155 |
1175392 |
bool TheoryArith::preCheck(Effort level) |
156 |
|
{ |
157 |
1175392 |
Trace("arith-check") << "TheoryArith::preCheck " << level << std::endl; |
158 |
1175392 |
return d_internal->preCheck(level); |
159 |
|
} |
160 |
|
|
161 |
1175392 |
void TheoryArith::postCheck(Effort level) |
162 |
|
{ |
163 |
1175392 |
d_im.reset(); |
164 |
1175392 |
Trace("arith-check") << "TheoryArith::postCheck " << level << std::endl; |
165 |
|
// check with the non-linear solver at last call |
166 |
1175392 |
if (level == Theory::EFFORT_LAST_CALL) |
167 |
|
{ |
168 |
1542 |
if (d_nonlinearExtension != nullptr) |
169 |
|
{ |
170 |
|
// If we computed lemmas in the last FULL_EFFORT check, send them now. |
171 |
1542 |
if (d_im.hasPendingLemma()) |
172 |
|
{ |
173 |
1328 |
d_im.doPendingFacts(); |
174 |
1328 |
d_im.doPendingLemmas(); |
175 |
1328 |
d_im.doPendingPhaseRequirements(); |
176 |
1328 |
return; |
177 |
|
} |
178 |
214 |
d_nonlinearExtension->finalizeModel(getValuation().getModel()); |
179 |
|
} |
180 |
214 |
return; |
181 |
|
} |
182 |
|
// otherwise, check with the linear solver |
183 |
1173850 |
if (d_internal->postCheck(level)) |
184 |
|
{ |
185 |
|
// linear solver emitted a conflict or lemma, return |
186 |
35401 |
return; |
187 |
|
} |
188 |
1138449 |
if (d_im.hasSent()) |
189 |
|
{ |
190 |
|
return; |
191 |
|
} |
192 |
|
|
193 |
1138449 |
if (Theory::fullEffort(level)) |
194 |
|
{ |
195 |
40724 |
d_arithModelCache.clear(); |
196 |
40724 |
if (d_nonlinearExtension != nullptr) |
197 |
|
{ |
198 |
46978 |
std::set<Node> termSet; |
199 |
23489 |
updateModelCache(termSet); |
200 |
23489 |
d_nonlinearExtension->checkFullEffort(d_arithModelCache, termSet); |
201 |
|
} |
202 |
17235 |
else if (d_internal->foundNonlinear()) |
203 |
|
{ |
204 |
|
// set incomplete |
205 |
|
d_im.setIncomplete(IncompleteId::ARITH_NL_DISABLED); |
206 |
|
} |
207 |
|
} |
208 |
|
} |
209 |
|
|
210 |
3463557 |
bool TheoryArith::preNotifyFact( |
211 |
|
TNode atom, bool pol, TNode fact, bool isPrereg, bool isInternal) |
212 |
|
{ |
213 |
6927114 |
Trace("arith-check") << "TheoryArith::preNotifyFact: " << fact |
214 |
3463557 |
<< ", isPrereg=" << isPrereg |
215 |
3463557 |
<< ", isInternal=" << isInternal << std::endl; |
216 |
|
// We do not assert to the equality engine of arithmetic in the standard way, |
217 |
|
// hence we return "true" to indicate we are finished with this fact. |
218 |
3463557 |
bool ret = true; |
219 |
3463557 |
if (d_eqSolver != nullptr) |
220 |
|
{ |
221 |
|
// the equality solver may indicate ret = false, after which the assertion |
222 |
|
// will be asserted to the equality engine in the default way. |
223 |
583 |
ret = d_eqSolver->preNotifyFact(atom, pol, fact, isPrereg, isInternal); |
224 |
|
} |
225 |
|
// we also always also notify the internal solver |
226 |
3463557 |
d_internal->preNotifyFact(atom, pol, fact); |
227 |
3463557 |
return ret; |
228 |
|
} |
229 |
|
|
230 |
14006 |
bool TheoryArith::needsCheckLastEffort() { |
231 |
14006 |
if (d_nonlinearExtension != nullptr) |
232 |
|
{ |
233 |
7511 |
return d_nonlinearExtension->hasNlTerms(); |
234 |
|
} |
235 |
6495 |
return false; |
236 |
|
} |
237 |
|
|
238 |
13682 |
TrustNode TheoryArith::explain(TNode n) |
239 |
|
{ |
240 |
13682 |
if (d_eqSolver != nullptr) |
241 |
|
{ |
242 |
|
// if the equality solver has an explanation for it, use it |
243 |
12 |
TrustNode texp = d_eqSolver->explain(n); |
244 |
6 |
if (!texp.isNull()) |
245 |
|
{ |
246 |
|
return texp; |
247 |
|
} |
248 |
|
} |
249 |
13682 |
return d_internal->explain(n); |
250 |
|
} |
251 |
|
|
252 |
1864142 |
void TheoryArith::propagate(Effort e) { |
253 |
1864142 |
d_internal->propagate(e); |
254 |
1864142 |
} |
255 |
|
|
256 |
11948 |
bool TheoryArith::collectModelInfo(TheoryModel* m, |
257 |
|
const std::set<Node>& termSet) |
258 |
|
{ |
259 |
|
// this overrides behavior to not assert equality engine |
260 |
11948 |
return collectModelValues(m, termSet); |
261 |
|
} |
262 |
|
|
263 |
11948 |
bool TheoryArith::collectModelValues(TheoryModel* m, |
264 |
|
const std::set<Node>& termSet) |
265 |
|
{ |
266 |
11948 |
if (Trace.isOn("arith::model")) |
267 |
|
{ |
268 |
|
Trace("arith::model") << "arithmetic model after pruning" << std::endl; |
269 |
|
for (const auto& p : d_arithModelCache) |
270 |
|
{ |
271 |
|
Trace("arith::model") << "\t" << p.first << " -> " << p.second << std::endl; |
272 |
|
} |
273 |
|
} |
274 |
|
|
275 |
11948 |
updateModelCache(termSet); |
276 |
|
|
277 |
11948 |
if (sanityCheckIntegerModel()) |
278 |
|
{ |
279 |
|
// We added a lemma |
280 |
|
return false; |
281 |
|
} |
282 |
|
|
283 |
|
// We are now ready to assert the model. |
284 |
213747 |
for (const std::pair<const Node, Node>& p : d_arithModelCache) |
285 |
|
{ |
286 |
201799 |
if (termSet.find(p.first) == termSet.end()) |
287 |
|
{ |
288 |
2 |
continue; |
289 |
|
} |
290 |
|
// maps to constant of comparable type |
291 |
201797 |
Assert(p.first.getType().isComparableTo(p.second.getType())); |
292 |
201797 |
if (m->assertEquality(p.first, p.second, true)) |
293 |
|
{ |
294 |
201797 |
continue; |
295 |
|
} |
296 |
|
Assert(false) << "A model equality could not be asserted: " << p.first |
297 |
|
<< " == " << p.second << std::endl; |
298 |
|
// If we failed to assert an equality, it is likely due to theory |
299 |
|
// combination, namely the repaired model for non-linear changed |
300 |
|
// an equality status that was agreed upon by both (linear) arithmetic |
301 |
|
// and another theory. In this case, we must add a lemma, or otherwise |
302 |
|
// we would terminate with an invalid model. Thus, we add a splitting |
303 |
|
// lemma of the form ( x = v V x != v ) where v is the model value |
304 |
|
// assigned by the non-linear solver to x. |
305 |
|
if (d_nonlinearExtension != nullptr) |
306 |
|
{ |
307 |
|
Node eq = p.first.eqNode(p.second); |
308 |
|
Node lem = NodeManager::currentNM()->mkNode(kind::OR, eq, eq.negate()); |
309 |
|
bool added = d_im.lemma(lem, InferenceId::ARITH_SPLIT_FOR_NL_MODEL); |
310 |
|
AlwaysAssert(added) << "The lemma was already in cache. Probably there is something wrong with theory combination..."; |
311 |
|
} |
312 |
|
return false; |
313 |
|
} |
314 |
11948 |
return true; |
315 |
|
} |
316 |
|
|
317 |
878 |
void TheoryArith::notifyRestart(){ |
318 |
878 |
d_internal->notifyRestart(); |
319 |
878 |
} |
320 |
|
|
321 |
9354 |
void TheoryArith::presolve(){ |
322 |
9354 |
d_internal->presolve(); |
323 |
9354 |
} |
324 |
|
|
325 |
386189 |
EqualityStatus TheoryArith::getEqualityStatus(TNode a, TNode b) { |
326 |
386189 |
Debug("arith") << "TheoryArith::getEqualityStatus(" << a << ", " << b << ")" << std::endl; |
327 |
386189 |
if (d_arithModelCache.empty()) |
328 |
|
{ |
329 |
369739 |
return d_internal->getEqualityStatus(a,b); |
330 |
|
} |
331 |
32900 |
Node aval = Rewriter::rewrite(a.substitute(d_arithModelCache.begin(), d_arithModelCache.end())); |
332 |
32900 |
Node bval = Rewriter::rewrite(b.substitute(d_arithModelCache.begin(), d_arithModelCache.end())); |
333 |
16450 |
if (aval == bval) |
334 |
|
{ |
335 |
5315 |
return EQUALITY_TRUE_IN_MODEL; |
336 |
|
} |
337 |
11135 |
return EQUALITY_FALSE_IN_MODEL; |
338 |
|
} |
339 |
|
|
340 |
2288 |
Node TheoryArith::getModelValue(TNode var) { |
341 |
2288 |
return d_internal->getModelValue( var ); |
342 |
|
} |
343 |
|
|
344 |
6721 |
std::pair<bool, Node> TheoryArith::entailmentCheck(TNode lit) |
345 |
|
{ |
346 |
13442 |
ArithEntailmentCheckParameters def; |
347 |
6721 |
def.addLookupRowSumAlgorithms(); |
348 |
13442 |
ArithEntailmentCheckSideEffects ase; |
349 |
6721 |
std::pair<bool, Node> res = d_internal->entailmentCheck(lit, def, ase); |
350 |
13442 |
return res; |
351 |
|
} |
352 |
|
eq::ProofEqEngine* TheoryArith::getProofEqEngine() |
353 |
|
{ |
354 |
|
return d_im.getProofEqEngine(); |
355 |
|
} |
356 |
|
|
357 |
23489 |
void TheoryArith::updateModelCache(std::set<Node>& termSet) |
358 |
|
{ |
359 |
23489 |
if (d_arithModelCache.empty()) |
360 |
|
{ |
361 |
23489 |
collectAssertedTerms(termSet); |
362 |
23489 |
d_internal->collectModelValues(termSet, d_arithModelCache); |
363 |
|
} |
364 |
23489 |
} |
365 |
11948 |
void TheoryArith::updateModelCache(const std::set<Node>& termSet) |
366 |
|
{ |
367 |
11948 |
if (d_arithModelCache.empty()) |
368 |
|
{ |
369 |
7532 |
d_internal->collectModelValues(termSet, d_arithModelCache); |
370 |
|
} |
371 |
11948 |
} |
372 |
11948 |
bool TheoryArith::sanityCheckIntegerModel() |
373 |
|
{ |
374 |
|
|
375 |
|
// Double check that the model from the linear solver respects integer types, |
376 |
|
// if it does not, add a branch and bound lemma. This typically should never |
377 |
|
// be necessary, but is needed in rare cases. |
378 |
11948 |
bool addedLemma = false; |
379 |
11948 |
bool badAssignment = false; |
380 |
11948 |
Trace("arith-check") << "model:" << std::endl; |
381 |
213747 |
for (const auto& p : d_arithModelCache) |
382 |
|
{ |
383 |
201799 |
Trace("arith-check") << p.first << " -> " << p.second << std::endl; |
384 |
201799 |
if (p.first.getType().isInteger() && !p.second.getType().isInteger()) |
385 |
|
{ |
386 |
|
Assert(false) << "TheoryArithPrivate generated a bad model value for " |
387 |
|
"integer variable " |
388 |
|
<< p.first << " : " << p.second; |
389 |
|
// must branch and bound |
390 |
|
TrustNode lem = |
391 |
|
d_bab.branchIntegerVariable(p.first, p.second.getConst<Rational>()); |
392 |
|
if (d_im.trustedLemma(lem, InferenceId::ARITH_BB_LEMMA)) |
393 |
|
{ |
394 |
|
addedLemma = true; |
395 |
|
} |
396 |
|
badAssignment = true; |
397 |
|
} |
398 |
|
} |
399 |
11948 |
if (addedLemma) |
400 |
|
{ |
401 |
|
// we had to add a branch and bound lemma since the linear solver assigned |
402 |
|
// a non-integer value to an integer variable. |
403 |
|
return true; |
404 |
|
} |
405 |
|
// this would imply that linear arithmetic's model failed to satisfy a branch |
406 |
|
// and bound lemma |
407 |
11948 |
AlwaysAssert(!badAssignment) |
408 |
|
<< "Bad assignment from TheoryArithPrivate::collectModelValues, and no " |
409 |
|
"branching lemma was sent"; |
410 |
11948 |
return false; |
411 |
|
} |
412 |
|
|
413 |
|
} // namespace arith |
414 |
|
} // namespace theory |
415 |
22746 |
} // namespace cvc5 |