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 |
15272 |
TheoryArith::TheoryArith(Env& env, OutputChannel& out, Valuation valuation) |
39 |
|
: Theory(THEORY_ARITH, env, out, valuation), |
40 |
|
d_ppRewriteTimer( |
41 |
30544 |
statisticsRegistry().registerTimer("theory::arith::ppRewriteTimer")), |
42 |
|
d_astate(env, valuation), |
43 |
|
d_im(env, *this, d_astate), |
44 |
|
d_ppre(d_env), |
45 |
|
d_bab(env, d_astate, d_im, d_ppre, d_pnm), |
46 |
|
d_eqSolver(nullptr), |
47 |
15272 |
d_internal(new TheoryArithPrivate(*this, env, d_bab)), |
48 |
|
d_nonlinearExtension(nullptr), |
49 |
|
d_opElim(d_env), |
50 |
|
d_arithPreproc(env, d_astate, d_im, d_pnm, d_opElim), |
51 |
61088 |
d_rewriter(d_opElim) |
52 |
|
{ |
53 |
|
// currently a cyclic dependency to TheoryArithPrivate |
54 |
15272 |
d_astate.setParent(d_internal); |
55 |
|
// indicate we are using the theory state object and inference manager |
56 |
15272 |
d_theoryState = &d_astate; |
57 |
15272 |
d_inferManager = &d_im; |
58 |
|
|
59 |
15272 |
if (options().arith.arithEqSolver) |
60 |
|
{ |
61 |
41 |
d_eqSolver.reset(new EqualitySolver(env, d_astate, d_im)); |
62 |
|
} |
63 |
15272 |
} |
64 |
|
|
65 |
45801 |
TheoryArith::~TheoryArith(){ |
66 |
15267 |
delete d_internal; |
67 |
30534 |
} |
68 |
|
|
69 |
15272 |
TheoryRewriter* TheoryArith::getTheoryRewriter() { return &d_rewriter; } |
70 |
|
|
71 |
7987 |
ProofRuleChecker* TheoryArith::getProofChecker() |
72 |
|
{ |
73 |
7987 |
return d_internal->getProofChecker(); |
74 |
|
} |
75 |
|
|
76 |
15272 |
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 |
15272 |
if (d_eqSolver != nullptr) |
81 |
|
{ |
82 |
41 |
return d_eqSolver->needsEqualityEngine(esi); |
83 |
|
} |
84 |
|
// otherwise, the linear arithmetic solver is responsible for setting up |
85 |
|
// the equality engine |
86 |
15231 |
return d_internal->needsEqualityEngine(esi); |
87 |
|
} |
88 |
15272 |
void TheoryArith::finishInit() |
89 |
|
{ |
90 |
15272 |
const LogicInfo& logic = logicInfo(); |
91 |
15272 |
if (logic.isTheoryEnabled(THEORY_ARITH) && logic.areTranscendentalsUsed()) |
92 |
|
{ |
93 |
|
// witness is used to eliminate square root |
94 |
8545 |
d_valuation.setUnevaluatedKind(kind::WITNESS); |
95 |
|
// we only need to add the operators that are not syntax sugar |
96 |
8545 |
d_valuation.setUnevaluatedKind(kind::EXPONENTIAL); |
97 |
8545 |
d_valuation.setUnevaluatedKind(kind::SINE); |
98 |
8545 |
d_valuation.setUnevaluatedKind(kind::PI); |
99 |
|
} |
100 |
|
// only need to create nonlinear extension if non-linear logic |
101 |
15272 |
if (logic.isTheoryEnabled(THEORY_ARITH) && !logic.isLinear()) |
102 |
|
{ |
103 |
19392 |
d_nonlinearExtension.reset( |
104 |
9696 |
new nl::NonlinearExtension(d_env, *this, d_astate)); |
105 |
|
} |
106 |
15272 |
if (d_eqSolver != nullptr) |
107 |
|
{ |
108 |
41 |
d_eqSolver->finishInit(); |
109 |
|
} |
110 |
|
// finish initialize in the old linear solver |
111 |
15272 |
d_internal->finishInit(); |
112 |
15272 |
} |
113 |
|
|
114 |
911899 |
void TheoryArith::preRegisterTerm(TNode n) |
115 |
|
{ |
116 |
911899 |
if (d_nonlinearExtension != nullptr) |
117 |
|
{ |
118 |
443808 |
d_nonlinearExtension->preRegisterTerm(n); |
119 |
|
} |
120 |
911900 |
d_internal->preRegisterTerm(n); |
121 |
911898 |
} |
122 |
|
|
123 |
876681 |
void TheoryArith::notifySharedTerm(TNode n) { d_internal->notifySharedTerm(n); } |
124 |
|
|
125 |
461532 |
TrustNode TheoryArith::ppRewrite(TNode atom, std::vector<SkolemLemma>& lems) |
126 |
|
{ |
127 |
923064 |
CodeTimer timer(d_ppRewriteTimer, /* allow_reentrant = */ true); |
128 |
461532 |
Debug("arith::preprocess") << "arith::preprocess() : " << atom << endl; |
129 |
|
|
130 |
461532 |
if (atom.getKind() == kind::EQUAL) |
131 |
|
{ |
132 |
28838 |
return d_ppre.ppRewriteEq(atom); |
133 |
|
} |
134 |
432694 |
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 |
432700 |
return d_arithPreproc.eliminate(atom, lems, false); |
142 |
|
} |
143 |
|
|
144 |
14925 |
Theory::PPAssertStatus TheoryArith::ppAssert( |
145 |
|
TrustNode tin, TrustSubstitutionMap& outSubstitutions) |
146 |
|
{ |
147 |
14925 |
return d_internal->ppAssert(tin, outSubstitutions); |
148 |
|
} |
149 |
|
|
150 |
250899 |
void TheoryArith::ppStaticLearn(TNode n, NodeBuilder& learned) |
151 |
|
{ |
152 |
250899 |
d_internal->ppStaticLearn(n, learned); |
153 |
250899 |
} |
154 |
|
|
155 |
2476754 |
bool TheoryArith::preCheck(Effort level) |
156 |
|
{ |
157 |
2476754 |
Trace("arith-check") << "TheoryArith::preCheck " << level << std::endl; |
158 |
2476754 |
return d_internal->preCheck(level); |
159 |
|
} |
160 |
|
|
161 |
2476754 |
void TheoryArith::postCheck(Effort level) |
162 |
|
{ |
163 |
2476754 |
d_im.reset(); |
164 |
2476754 |
Trace("arith-check") << "TheoryArith::postCheck " << level << std::endl; |
165 |
|
// check with the non-linear solver at last call |
166 |
2476754 |
if (level == Theory::EFFORT_LAST_CALL) |
167 |
|
{ |
168 |
3606 |
if (d_nonlinearExtension != nullptr) |
169 |
|
{ |
170 |
|
// If we computed lemmas in the last FULL_EFFORT check, send them now. |
171 |
3606 |
if (d_im.hasPendingLemma()) |
172 |
|
{ |
173 |
3266 |
d_im.doPendingFacts(); |
174 |
3266 |
d_im.doPendingLemmas(); |
175 |
3266 |
d_im.doPendingPhaseRequirements(); |
176 |
3266 |
return; |
177 |
|
} |
178 |
340 |
d_nonlinearExtension->finalizeModel(getValuation().getModel()); |
179 |
|
} |
180 |
340 |
return; |
181 |
|
} |
182 |
|
// otherwise, check with the linear solver |
183 |
2473148 |
if (d_internal->postCheck(level)) |
184 |
|
{ |
185 |
|
// linear solver emitted a conflict or lemma, return |
186 |
71025 |
return; |
187 |
|
} |
188 |
2402123 |
if (d_im.hasSent()) |
189 |
|
{ |
190 |
|
return; |
191 |
|
} |
192 |
|
|
193 |
2402123 |
if (Theory::fullEffort(level)) |
194 |
|
{ |
195 |
75035 |
d_arithModelCache.clear(); |
196 |
150070 |
std::set<Node> termSet; |
197 |
75035 |
if (d_nonlinearExtension != nullptr) |
198 |
|
{ |
199 |
41956 |
updateModelCache(termSet); |
200 |
41956 |
d_nonlinearExtension->checkFullEffort(d_arithModelCache, termSet); |
201 |
|
} |
202 |
33079 |
else if (d_internal->foundNonlinear()) |
203 |
|
{ |
204 |
|
// set incomplete |
205 |
|
d_im.setIncomplete(IncompleteId::ARITH_NL_DISABLED); |
206 |
|
} |
207 |
|
// If we won't be doing a last call effort check (which implies that |
208 |
|
// models will be computed), we must sanity check the integer model |
209 |
|
// from the linear solver now. We also must update the model cache |
210 |
|
// if we did not do so above. |
211 |
75035 |
if (d_nonlinearExtension == nullptr) |
212 |
|
{ |
213 |
33079 |
updateModelCache(termSet); |
214 |
|
} |
215 |
75035 |
sanityCheckIntegerModel(); |
216 |
|
} |
217 |
|
} |
218 |
|
|
219 |
7414195 |
bool TheoryArith::preNotifyFact( |
220 |
|
TNode atom, bool pol, TNode fact, bool isPrereg, bool isInternal) |
221 |
|
{ |
222 |
14828390 |
Trace("arith-check") << "TheoryArith::preNotifyFact: " << fact |
223 |
7414195 |
<< ", isPrereg=" << isPrereg |
224 |
7414195 |
<< ", isInternal=" << isInternal << std::endl; |
225 |
|
// We do not assert to the equality engine of arithmetic in the standard way, |
226 |
|
// hence we return "true" to indicate we are finished with this fact. |
227 |
7414195 |
bool ret = true; |
228 |
7414195 |
if (d_eqSolver != nullptr) |
229 |
|
{ |
230 |
|
// the equality solver may indicate ret = false, after which the assertion |
231 |
|
// will be asserted to the equality engine in the default way. |
232 |
3321 |
ret = d_eqSolver->preNotifyFact(atom, pol, fact, isPrereg, isInternal); |
233 |
|
} |
234 |
|
// we also always also notify the internal solver |
235 |
7414195 |
d_internal->preNotifyFact(atom, pol, fact); |
236 |
7414195 |
return ret; |
237 |
|
} |
238 |
|
|
239 |
27498 |
bool TheoryArith::needsCheckLastEffort() { |
240 |
27498 |
if (d_nonlinearExtension != nullptr) |
241 |
|
{ |
242 |
14231 |
return d_nonlinearExtension->hasNlTerms(); |
243 |
|
} |
244 |
13267 |
return false; |
245 |
|
} |
246 |
|
|
247 |
32632 |
TrustNode TheoryArith::explain(TNode n) |
248 |
|
{ |
249 |
32632 |
if (d_eqSolver != nullptr) |
250 |
|
{ |
251 |
|
// if the equality solver has an explanation for it, use it |
252 |
54 |
TrustNode texp = d_eqSolver->explain(n); |
253 |
27 |
if (!texp.isNull()) |
254 |
|
{ |
255 |
|
return texp; |
256 |
|
} |
257 |
|
} |
258 |
32632 |
return d_internal->explain(n); |
259 |
|
} |
260 |
|
|
261 |
3843203 |
void TheoryArith::propagate(Effort e) { |
262 |
3843203 |
d_internal->propagate(e); |
263 |
3843203 |
} |
264 |
|
|
265 |
23049 |
bool TheoryArith::collectModelInfo(TheoryModel* m, |
266 |
|
const std::set<Node>& termSet) |
267 |
|
{ |
268 |
|
// this overrides behavior to not assert equality engine |
269 |
23049 |
return collectModelValues(m, termSet); |
270 |
|
} |
271 |
|
|
272 |
23049 |
bool TheoryArith::collectModelValues(TheoryModel* m, |
273 |
|
const std::set<Node>& termSet) |
274 |
|
{ |
275 |
23049 |
if (Trace.isOn("arith::model")) |
276 |
|
{ |
277 |
|
Trace("arith::model") << "arithmetic model after pruning" << std::endl; |
278 |
|
for (const auto& p : d_arithModelCache) |
279 |
|
{ |
280 |
|
Trace("arith::model") << "\t" << p.first << " -> " << p.second << std::endl; |
281 |
|
} |
282 |
|
} |
283 |
|
|
284 |
23049 |
updateModelCache(termSet); |
285 |
|
|
286 |
|
// We are now ready to assert the model. |
287 |
467759 |
for (const std::pair<const Node, Node>& p : d_arithModelCache) |
288 |
|
{ |
289 |
444710 |
if (termSet.find(p.first) == termSet.end()) |
290 |
|
{ |
291 |
4 |
continue; |
292 |
|
} |
293 |
|
// maps to constant of comparable type |
294 |
444706 |
Assert(p.first.getType().isComparableTo(p.second.getType())); |
295 |
444706 |
if (m->assertEquality(p.first, p.second, true)) |
296 |
|
{ |
297 |
444706 |
continue; |
298 |
|
} |
299 |
|
Assert(false) << "A model equality could not be asserted: " << p.first |
300 |
|
<< " == " << p.second << std::endl; |
301 |
|
// If we failed to assert an equality, it is likely due to theory |
302 |
|
// combination, namely the repaired model for non-linear changed |
303 |
|
// an equality status that was agreed upon by both (linear) arithmetic |
304 |
|
// and another theory. In this case, we must add a lemma, or otherwise |
305 |
|
// we would terminate with an invalid model. Thus, we add a splitting |
306 |
|
// lemma of the form ( x = v V x != v ) where v is the model value |
307 |
|
// assigned by the non-linear solver to x. |
308 |
|
if (d_nonlinearExtension != nullptr) |
309 |
|
{ |
310 |
|
Node eq = p.first.eqNode(p.second); |
311 |
|
Node lem = NodeManager::currentNM()->mkNode(kind::OR, eq, eq.negate()); |
312 |
|
bool added = d_im.lemma(lem, InferenceId::ARITH_SPLIT_FOR_NL_MODEL); |
313 |
|
AlwaysAssert(added) << "The lemma was already in cache. Probably there is something wrong with theory combination..."; |
314 |
|
} |
315 |
|
return false; |
316 |
|
} |
317 |
23049 |
return true; |
318 |
|
} |
319 |
|
|
320 |
2004 |
void TheoryArith::notifyRestart(){ |
321 |
2004 |
d_internal->notifyRestart(); |
322 |
2004 |
} |
323 |
|
|
324 |
20562 |
void TheoryArith::presolve(){ |
325 |
20562 |
d_internal->presolve(); |
326 |
20562 |
} |
327 |
|
|
328 |
816153 |
EqualityStatus TheoryArith::getEqualityStatus(TNode a, TNode b) { |
329 |
816153 |
Debug("arith") << "TheoryArith::getEqualityStatus(" << a << ", " << b << ")" << std::endl; |
330 |
816153 |
if (d_arithModelCache.empty()) |
331 |
|
{ |
332 |
|
return d_internal->getEqualityStatus(a,b); |
333 |
|
} |
334 |
|
Node aval = |
335 |
1632306 |
rewrite(a.substitute(d_arithModelCache.begin(), d_arithModelCache.end())); |
336 |
|
Node bval = |
337 |
1632306 |
rewrite(b.substitute(d_arithModelCache.begin(), d_arithModelCache.end())); |
338 |
816153 |
if (aval == bval) |
339 |
|
{ |
340 |
76368 |
return EQUALITY_TRUE_IN_MODEL; |
341 |
|
} |
342 |
739785 |
return EQUALITY_FALSE_IN_MODEL; |
343 |
|
} |
344 |
|
|
345 |
2944 |
Node TheoryArith::getModelValue(TNode var) { |
346 |
2944 |
return d_internal->getModelValue( var ); |
347 |
|
} |
348 |
|
|
349 |
10399 |
std::pair<bool, Node> TheoryArith::entailmentCheck(TNode lit) |
350 |
|
{ |
351 |
20798 |
ArithEntailmentCheckParameters def; |
352 |
10399 |
def.addLookupRowSumAlgorithms(); |
353 |
20798 |
ArithEntailmentCheckSideEffects ase; |
354 |
10399 |
std::pair<bool, Node> res = d_internal->entailmentCheck(lit, def, ase); |
355 |
20798 |
return res; |
356 |
|
} |
357 |
|
eq::ProofEqEngine* TheoryArith::getProofEqEngine() |
358 |
|
{ |
359 |
|
return d_im.getProofEqEngine(); |
360 |
|
} |
361 |
|
|
362 |
75035 |
void TheoryArith::updateModelCache(std::set<Node>& termSet) |
363 |
|
{ |
364 |
75035 |
if (d_arithModelCache.empty()) |
365 |
|
{ |
366 |
75035 |
collectAssertedTerms(termSet); |
367 |
75035 |
d_internal->collectModelValues(termSet, d_arithModelCache); |
368 |
|
} |
369 |
75035 |
} |
370 |
23049 |
void TheoryArith::updateModelCache(const std::set<Node>& termSet) |
371 |
|
{ |
372 |
23049 |
if (d_arithModelCache.empty()) |
373 |
|
{ |
374 |
5710 |
d_internal->collectModelValues(termSet, d_arithModelCache); |
375 |
|
} |
376 |
23049 |
} |
377 |
75035 |
bool TheoryArith::sanityCheckIntegerModel() |
378 |
|
{ |
379 |
|
|
380 |
|
// Double check that the model from the linear solver respects integer types, |
381 |
|
// if it does not, add a branch and bound lemma. This typically should never |
382 |
|
// be necessary, but is needed in rare cases. |
383 |
75035 |
bool addedLemma = false; |
384 |
75035 |
bool badAssignment = false; |
385 |
75035 |
Trace("arith-check") << "model:" << std::endl; |
386 |
2366054 |
for (const auto& p : d_arithModelCache) |
387 |
|
{ |
388 |
2291019 |
Trace("arith-check") << p.first << " -> " << p.second << std::endl; |
389 |
2291019 |
if (p.first.getType().isInteger() && !p.second.getType().isInteger()) |
390 |
|
{ |
391 |
4 |
warning() << "TheoryArithPrivate generated a bad model value for " |
392 |
4 |
"integer variable " |
393 |
4 |
<< p.first << " : " << p.second << std::endl; |
394 |
|
// must branch and bound |
395 |
|
TrustNode lem = |
396 |
8 |
d_bab.branchIntegerVariable(p.first, p.second.getConst<Rational>()); |
397 |
4 |
if (d_im.trustedLemma(lem, InferenceId::ARITH_BB_LEMMA)) |
398 |
|
{ |
399 |
4 |
addedLemma = true; |
400 |
|
} |
401 |
4 |
badAssignment = true; |
402 |
|
} |
403 |
|
} |
404 |
75035 |
if (addedLemma) |
405 |
|
{ |
406 |
|
// we had to add a branch and bound lemma since the linear solver assigned |
407 |
|
// a non-integer value to an integer variable. |
408 |
4 |
return true; |
409 |
|
} |
410 |
|
// this would imply that linear arithmetic's model failed to satisfy a branch |
411 |
|
// and bound lemma |
412 |
75031 |
AlwaysAssert(!badAssignment) |
413 |
|
<< "Bad assignment from TheoryArithPrivate::collectModelValues, and no " |
414 |
|
"branching lemma was sent"; |
415 |
75031 |
return false; |
416 |
|
} |
417 |
|
|
418 |
|
} // namespace arith |
419 |
|
} // namespace theory |
420 |
31137 |
} // namespace cvc5 |