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