GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/arith/theory_arith.cpp Lines: 153 179 85.5 %
Date: 2021-09-10 Branches: 194 516 37.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
9913
TheoryArith::TheoryArith(Env& env, OutputChannel& out, Valuation valuation)
39
    : Theory(THEORY_ARITH, env, out, valuation),
40
9913
      d_ppRewriteTimer(smtStatisticsRegistry().registerTimer(
41
19826
          "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
9913
      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
39652
      d_rewriter(d_opElim)
52
{
53
  // currently a cyclic dependency to TheoryArithPrivate
54
9913
  d_astate.setParent(d_internal);
55
  // indicate we are using the theory state object and inference manager
56
9913
  d_theoryState = &d_astate;
57
9913
  d_inferManager = &d_im;
58
59
9913
  if (options().arith.arithEqSolver)
60
  {
61
39
    d_eqSolver.reset(new EqualitySolver(env, d_astate, d_im));
62
  }
63
9913
}
64
65
29730
TheoryArith::~TheoryArith(){
66
9910
  delete d_internal;
67
19820
}
68
69
9913
TheoryRewriter* TheoryArith::getTheoryRewriter() { return &d_rewriter; }
70
71
3781
ProofRuleChecker* TheoryArith::getProofChecker()
72
{
73
3781
  return d_internal->getProofChecker();
74
}
75
76
9913
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
9913
  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
9874
  return d_internal->needsEqualityEngine(esi);
87
}
88
9913
void TheoryArith::finishInit()
89
{
90
9913
  const LogicInfo& logic = logicInfo();
91
9913
  if (logic.isTheoryEnabled(THEORY_ARITH) && logic.areTranscendentalsUsed())
92
  {
93
    // witness is used to eliminate square root
94
4236
    d_valuation.setUnevaluatedKind(kind::WITNESS);
95
    // we only need to add the operators that are not syntax sugar
96
4236
    d_valuation.setUnevaluatedKind(kind::EXPONENTIAL);
97
4236
    d_valuation.setUnevaluatedKind(kind::SINE);
98
4236
    d_valuation.setUnevaluatedKind(kind::PI);
99
  }
100
  // only need to create nonlinear extension if non-linear logic
101
9913
  if (logic.isTheoryEnabled(THEORY_ARITH) && !logic.isLinear())
102
  {
103
10386
    d_nonlinearExtension.reset(
104
5193
        new nl::NonlinearExtension(d_env, *this, d_astate));
105
  }
106
9913
  if (d_eqSolver != nullptr)
107
  {
108
39
    d_eqSolver->finishInit();
109
  }
110
  // finish initialize in the old linear solver
111
9913
  d_internal->finishInit();
112
9913
}
113
114
818203
void TheoryArith::preRegisterTerm(TNode n)
115
{
116
818203
  if (d_nonlinearExtension != nullptr)
117
  {
118
410361
    d_nonlinearExtension->preRegisterTerm(n);
119
  }
120
818204
  d_internal->preRegisterTerm(n);
121
818202
}
122
123
642897
void TheoryArith::notifySharedTerm(TNode n) { d_internal->notifySharedTerm(n); }
124
125
776801
TrustNode TheoryArith::ppRewrite(TNode atom, std::vector<SkolemLemma>& lems)
126
{
127
1553602
  CodeTimer timer(d_ppRewriteTimer, /* allow_reentrant = */ true);
128
776801
  Debug("arith::preprocess") << "arith::preprocess() : " << atom << endl;
129
130
776801
  if (atom.getKind() == kind::EQUAL)
131
  {
132
27109
    return d_ppre.ppRewriteEq(atom);
133
  }
134
749692
  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
749698
  return d_arithPreproc.eliminate(atom, lems, false);
142
}
143
144
11724
Theory::PPAssertStatus TheoryArith::ppAssert(
145
    TrustNode tin, TrustSubstitutionMap& outSubstitutions)
146
{
147
11724
  return d_internal->ppAssert(tin, outSubstitutions);
148
}
149
150
105422
void TheoryArith::ppStaticLearn(TNode n, NodeBuilder& learned)
151
{
152
105422
  d_internal->ppStaticLearn(n, learned);
153
105422
}
154
155
1804806
bool TheoryArith::preCheck(Effort level)
156
{
157
1804806
  Trace("arith-check") << "TheoryArith::preCheck " << level << std::endl;
158
1804806
  return d_internal->preCheck(level);
159
}
160
161
1804806
void TheoryArith::postCheck(Effort level)
162
{
163
1804806
  d_im.reset();
164
1804806
  Trace("arith-check") << "TheoryArith::postCheck " << level << std::endl;
165
  // check with the non-linear solver at last call
166
1804806
  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
1801509
  if (d_internal->postCheck(level))
176
  {
177
    // linear solver emitted a conflict or lemma, return
178
57994
    return;
179
  }
180
1743515
  if (d_im.hasSent())
181
  {
182
    return;
183
  }
184
185
1743515
  if (Theory::fullEffort(level))
186
  {
187
62092
    d_arithModelCache.clear();
188
62092
    if (d_nonlinearExtension != nullptr)
189
    {
190
68864
      std::set<Node> termSet;
191
34432
      updateModelCache(termSet);
192
34432
      d_nonlinearExtension->check(level);
193
34432
      d_nonlinearExtension->interceptModel(d_arithModelCache, termSet);
194
    }
195
27660
    else if (d_internal->foundNonlinear())
196
    {
197
      // set incomplete
198
      d_im.setIncomplete(IncompleteId::ARITH_NL_DISABLED);
199
    }
200
  }
201
}
202
203
5893188
bool TheoryArith::preNotifyFact(
204
    TNode atom, bool pol, TNode fact, bool isPrereg, bool isInternal)
205
{
206
11786376
  Trace("arith-check") << "TheoryArith::preNotifyFact: " << fact
207
5893188
                       << ", isPrereg=" << isPrereg
208
5893188
                       << ", 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
5893188
  bool ret = true;
212
5893188
  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
5893188
  d_internal->preNotifyFact(atom, pol, fact);
220
5893188
  return ret;
221
}
222
223
18680
bool TheoryArith::needsCheckLastEffort() {
224
18680
  if (d_nonlinearExtension != nullptr)
225
  {
226
10056
    return d_nonlinearExtension->needsCheckLastEffort();
227
  }
228
8624
  return false;
229
}
230
231
25921
TrustNode TheoryArith::explain(TNode n)
232
{
233
25921
  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
25921
  return d_internal->explain(n);
243
}
244
245
2744391
void TheoryArith::propagate(Effort e) {
246
2744391
  d_internal->propagate(e);
247
2744391
}
248
249
14441
bool TheoryArith::collectModelInfo(TheoryModel* m,
250
                                   const std::set<Node>& termSet)
251
{
252
  // this overrides behavior to not assert equality engine
253
14441
  return collectModelValues(m, termSet);
254
}
255
256
14441
bool TheoryArith::collectModelValues(TheoryModel* m,
257
                                     const std::set<Node>& termSet)
258
{
259
14441
  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
14441
  updateModelCache(termSet);
269
270
14441
  if (sanityCheckIntegerModel())
271
  {
272
    // We added a lemma
273
    return false;
274
  }
275
276
  // We are now ready to assert the model.
277
265136
  for (const std::pair<const Node, Node>& p : d_arithModelCache)
278
  {
279
250695
    if (termSet.find(p.first) == termSet.end())
280
    {
281
2
      continue;
282
    }
283
    // maps to constant of comparable type
284
250693
    Assert(p.first.getType().isComparableTo(p.second.getType()));
285
250693
    if (m->assertEquality(p.first, p.second, true))
286
    {
287
250693
      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
14441
  return true;
308
}
309
310
1689
void TheoryArith::notifyRestart(){
311
1689
  d_internal->notifyRestart();
312
1689
}
313
314
15237
void TheoryArith::presolve(){
315
15237
  d_internal->presolve();
316
15237
  if (d_nonlinearExtension != nullptr)
317
  {
318
6425
    d_nonlinearExtension->presolve();
319
  }
320
15237
}
321
322
780587
EqualityStatus TheoryArith::getEqualityStatus(TNode a, TNode b) {
323
780587
  Debug("arith") << "TheoryArith::getEqualityStatus(" << a << ", " << b << ")" << std::endl;
324
780587
  if (d_arithModelCache.empty())
325
  {
326
747572
    return d_internal->getEqualityStatus(a,b);
327
  }
328
66030
  Node aval = Rewriter::rewrite(a.substitute(d_arithModelCache.begin(), d_arithModelCache.end()));
329
66030
  Node bval = Rewriter::rewrite(b.substitute(d_arithModelCache.begin(), d_arithModelCache.end()));
330
33015
  if (aval == bval)
331
  {
332
10752
    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
34432
void TheoryArith::updateModelCache(std::set<Node>& termSet)
355
{
356
34432
  if (d_arithModelCache.empty())
357
  {
358
34432
    collectAssertedTerms(termSet);
359
34432
    d_internal->collectModelValues(termSet, d_arithModelCache);
360
  }
361
34432
}
362
14441
void TheoryArith::updateModelCache(const std::set<Node>& termSet)
363
{
364
14441
  if (d_arithModelCache.empty())
365
  {
366
8097
    d_internal->collectModelValues(termSet, d_arithModelCache);
367
  }
368
14441
}
369
14441
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
14441
    bool addedLemma = false;
376
14441
    bool badAssignment = false;
377
14441
    Trace("arith-check") << "model:" << std::endl;
378
265136
    for (const auto& p : d_arithModelCache)
379
    {
380
250695
      Trace("arith-check") << p.first << " -> " << p.second << std::endl;
381
250695
      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
14441
    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
14441
    AlwaysAssert(!badAssignment)
405
        << "Bad assignment from TheoryArithPrivate::collectModelValues, and no "
406
          "branching lemma was sent";
407
14441
    return false;
408
}
409
410
}  // namespace arith
411
}  // namespace theory
412
29502
}  // namespace cvc5