GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/theory_arith.cpp Lines: 163 181 90.1 %
Date: 2021-11-07 Branches: 208 500 41.6 %

Line Exec Source
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
15273
TheoryArith::TheoryArith(Env& env, OutputChannel& out, Valuation valuation)
39
    : Theory(THEORY_ARITH, env, out, valuation),
40
      d_ppRewriteTimer(
41
30546
          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
15273
      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
61092
      d_rewriter(d_opElim)
52
{
53
  // currently a cyclic dependency to TheoryArithPrivate
54
15273
  d_astate.setParent(d_internal);
55
  // indicate we are using the theory state object and inference manager
56
15273
  d_theoryState = &d_astate;
57
15273
  d_inferManager = &d_im;
58
59
15273
  if (options().arith.arithEqSolver)
60
  {
61
41
    d_eqSolver.reset(new EqualitySolver(env, d_astate, d_im));
62
  }
63
15273
}
64
65
45804
TheoryArith::~TheoryArith(){
66
15268
  delete d_internal;
67
30536
}
68
69
15273
TheoryRewriter* TheoryArith::getTheoryRewriter() { return &d_rewriter; }
70
71
7989
ProofRuleChecker* TheoryArith::getProofChecker()
72
{
73
7989
  return d_internal->getProofChecker();
74
}
75
76
15273
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
15273
  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
15232
  return d_internal->needsEqualityEngine(esi);
87
}
88
15273
void TheoryArith::finishInit()
89
{
90
15273
  const LogicInfo& logic = logicInfo();
91
15273
  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
15273
  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
15273
  if (d_eqSolver != nullptr)
107
  {
108
41
    d_eqSolver->finishInit();
109
  }
110
  // finish initialize in the old linear solver
111
15273
  d_internal->finishInit();
112
15273
}
113
114
908293
void TheoryArith::preRegisterTerm(TNode n)
115
{
116
908293
  if (d_nonlinearExtension != nullptr)
117
  {
118
443808
    d_nonlinearExtension->preRegisterTerm(n);
119
  }
120
908294
  d_internal->preRegisterTerm(n);
121
908292
}
122
123
980512
void TheoryArith::notifySharedTerm(TNode n) { d_internal->notifySharedTerm(n); }
124
125
460707
TrustNode TheoryArith::ppRewrite(TNode atom, std::vector<SkolemLemma>& lems)
126
{
127
921414
  CodeTimer timer(d_ppRewriteTimer, /* allow_reentrant = */ true);
128
460707
  Debug("arith::preprocess") << "arith::preprocess() : " << atom << endl;
129
130
460707
  if (atom.getKind() == kind::EQUAL)
131
  {
132
28838
    return d_ppre.ppRewriteEq(atom);
133
  }
134
431869
  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
431875
  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
250978
void TheoryArith::ppStaticLearn(TNode n, NodeBuilder& learned)
151
{
152
250978
  d_internal->ppStaticLearn(n, learned);
153
250978
}
154
155
2507531
bool TheoryArith::preCheck(Effort level)
156
{
157
2507531
  Trace("arith-check") << "TheoryArith::preCheck " << level << std::endl;
158
2507531
  return d_internal->preCheck(level);
159
}
160
161
2507531
void TheoryArith::postCheck(Effort level)
162
{
163
2507531
  d_im.reset();
164
2507531
  Trace("arith-check") << "TheoryArith::postCheck " << level << std::endl;
165
  // check with the non-linear solver at last call
166
2507531
  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
2503925
  if (d_internal->postCheck(level))
184
  {
185
    // linear solver emitted a conflict or lemma, return
186
74244
    return;
187
  }
188
2429681
  if (d_im.hasSent())
189
  {
190
    return;
191
  }
192
193
2429681
  if (Theory::fullEffort(level))
194
  {
195
76712
    d_arithModelCache.clear();
196
153424
    std::set<Node> termSet;
197
76712
    if (d_nonlinearExtension != nullptr)
198
    {
199
41956
      updateModelCache(termSet);
200
41956
      d_nonlinearExtension->checkFullEffort(d_arithModelCache, termSet);
201
    }
202
34756
    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
76712
    if (d_nonlinearExtension == nullptr)
212
    {
213
34756
      updateModelCache(termSet);
214
    }
215
76712
    sanityCheckIntegerModel();
216
  }
217
}
218
219
7479379
bool TheoryArith::preNotifyFact(
220
    TNode atom, bool pol, TNode fact, bool isPrereg, bool isInternal)
221
{
222
14958758
  Trace("arith-check") << "TheoryArith::preNotifyFact: " << fact
223
7479379
                       << ", isPrereg=" << isPrereg
224
7479379
                       << ", 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
7479379
  bool ret = true;
228
7479379
  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
7479379
  d_internal->preNotifyFact(atom, pol, fact);
236
7479379
  return ret;
237
}
238
239
27491
bool TheoryArith::needsCheckLastEffort() {
240
27491
  if (d_nonlinearExtension != nullptr)
241
  {
242
14231
    return d_nonlinearExtension->hasNlTerms();
243
  }
244
13260
  return false;
245
}
246
247
36525
TrustNode TheoryArith::explain(TNode n)
248
{
249
36525
  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
36525
  return d_internal->explain(n);
259
}
260
261
3936195
void TheoryArith::propagate(Effort e) {
262
3936195
  d_internal->propagate(e);
263
3936195
}
264
265
23043
bool TheoryArith::collectModelInfo(TheoryModel* m,
266
                                   const std::set<Node>& termSet)
267
{
268
  // this overrides behavior to not assert equality engine
269
23043
  return collectModelValues(m, termSet);
270
}
271
272
23043
bool TheoryArith::collectModelValues(TheoryModel* m,
273
                                     const std::set<Node>& termSet)
274
{
275
23043
  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
23043
  updateModelCache(termSet);
285
286
  // We are now ready to assert the model.
287
467074
  for (const std::pair<const Node, Node>& p : d_arithModelCache)
288
  {
289
444031
    if (termSet.find(p.first) == termSet.end())
290
    {
291
4
      continue;
292
    }
293
    // maps to constant of comparable type
294
444027
    Assert(p.first.getType().isComparableTo(p.second.getType()));
295
444027
    if (m->assertEquality(p.first, p.second, true))
296
    {
297
444027
      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
23043
  return true;
318
}
319
320
2025
void TheoryArith::notifyRestart(){
321
2025
  d_internal->notifyRestart();
322
2025
}
323
324
20560
void TheoryArith::presolve(){
325
20560
  d_internal->presolve();
326
20560
}
327
328
979756
EqualityStatus TheoryArith::getEqualityStatus(TNode a, TNode b) {
329
979756
  Debug("arith") << "TheoryArith::getEqualityStatus(" << a << ", " << b << ")" << std::endl;
330
979756
  if (d_arithModelCache.empty())
331
  {
332
    return d_internal->getEqualityStatus(a,b);
333
  }
334
  Node aval =
335
1959512
      rewrite(a.substitute(d_arithModelCache.begin(), d_arithModelCache.end()));
336
  Node bval =
337
1959512
      rewrite(b.substitute(d_arithModelCache.begin(), d_arithModelCache.end()));
338
979756
  if (aval == bval)
339
  {
340
100205
    return EQUALITY_TRUE_IN_MODEL;
341
  }
342
879551
  return EQUALITY_FALSE_IN_MODEL;
343
}
344
345
2895
Node TheoryArith::getModelValue(TNode var) {
346
2895
  return d_internal->getModelValue( var );
347
}
348
349
8478
std::pair<bool, Node> TheoryArith::entailmentCheck(TNode lit)
350
{
351
16956
  ArithEntailmentCheckParameters def;
352
8478
  def.addLookupRowSumAlgorithms();
353
16956
  ArithEntailmentCheckSideEffects ase;
354
8478
  std::pair<bool, Node> res = d_internal->entailmentCheck(lit, def, ase);
355
16956
  return res;
356
}
357
eq::ProofEqEngine* TheoryArith::getProofEqEngine()
358
{
359
  return d_im.getProofEqEngine();
360
}
361
362
76712
void TheoryArith::updateModelCache(std::set<Node>& termSet)
363
{
364
76712
  if (d_arithModelCache.empty())
365
  {
366
76712
    collectAssertedTerms(termSet);
367
76712
    d_internal->collectModelValues(termSet, d_arithModelCache);
368
  }
369
76712
}
370
23043
void TheoryArith::updateModelCache(const std::set<Node>& termSet)
371
{
372
23043
  if (d_arithModelCache.empty())
373
  {
374
5710
    d_internal->collectModelValues(termSet, d_arithModelCache);
375
  }
376
23043
}
377
76712
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
76712
    bool addedLemma = false;
384
76712
    bool badAssignment = false;
385
76712
    Trace("arith-check") << "model:" << std::endl;
386
2332914
    for (const auto& p : d_arithModelCache)
387
    {
388
2256202
      Trace("arith-check") << p.first << " -> " << p.second << std::endl;
389
2256202
      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
76712
    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
76708
    AlwaysAssert(!badAssignment)
413
        << "Bad assignment from TheoryArithPrivate::collectModelValues, and no "
414
          "branching lemma was sent";
415
76708
    return false;
416
}
417
418
}  // namespace arith
419
}  // namespace theory
420
31137
}  // namespace cvc5