GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/smt/smt_solver.cpp Lines: 87 98 88.8 %
Date: 2021-09-15 Branches: 139 286 48.6 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Aina Niemetz, Morgan Deters
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
 * The solver for SMT queries in an SmtEngine.
14
 */
15
16
#include "smt/smt_solver.h"
17
18
#include "options/main_options.h"
19
#include "options/smt_options.h"
20
#include "prop/prop_engine.h"
21
#include "smt/assertions.h"
22
#include "smt/env.h"
23
#include "smt/preprocessor.h"
24
#include "smt/smt_engine.h"
25
#include "smt/smt_engine_state.h"
26
#include "smt/smt_engine_stats.h"
27
#include "theory/logic_info.h"
28
#include "theory/theory_engine.h"
29
#include "theory/theory_traits.h"
30
31
using namespace std;
32
33
namespace cvc5 {
34
namespace smt {
35
36
13209
SmtSolver::SmtSolver(Env& env,
37
                     SmtEngineState& state,
38
                     Preprocessor& pp,
39
13209
                     SmtEngineStatistics& stats)
40
    : d_env(env),
41
      d_state(state),
42
      d_pp(pp),
43
      d_stats(stats),
44
      d_theoryEngine(nullptr),
45
13209
      d_propEngine(nullptr)
46
{
47
13209
}
48
49
10562
SmtSolver::~SmtSolver() {}
50
51
9942
void SmtSolver::finishInit(const LogicInfo& logicInfo)
52
{
53
  // We have mutual dependency here, so we add the prop engine to the theory
54
  // engine later (it is non-essential there)
55
9942
  d_theoryEngine.reset(new TheoryEngine(d_env));
56
57
  // Add the theories
58
139188
  for (theory::TheoryId id = theory::THEORY_FIRST; id < theory::THEORY_LAST;
59
       ++id)
60
  {
61
129246
    theory::TheoryConstructor::addTheory(d_theoryEngine.get(), id);
62
  }
63
  // Add the proof checkers for each theory
64
9942
  ProofNodeManager* pnm = d_env.getProofNodeManager();
65
9942
  if (pnm)
66
  {
67
3796
    d_theoryEngine->initializeProofChecker(pnm->getChecker());
68
  }
69
9942
  Trace("smt-debug") << "Making prop engine..." << std::endl;
70
  /* force destruction of referenced PropEngine to enforce that statistics
71
   * are unregistered by the obsolete PropEngine object before registered
72
   * again by the new PropEngine object */
73
9942
  d_propEngine.reset(nullptr);
74
9942
  d_propEngine.reset(new prop::PropEngine(d_theoryEngine.get(), d_env));
75
76
9942
  Trace("smt-debug") << "Setting up theory engine..." << std::endl;
77
9942
  d_theoryEngine->setPropEngine(getPropEngine());
78
9942
  Trace("smt-debug") << "Finishing init for theory engine..." << std::endl;
79
9942
  d_theoryEngine->finishInit();
80
9942
  d_propEngine->finishInit();
81
9942
}
82
83
67
void SmtSolver::resetAssertions()
84
{
85
  /* Create new PropEngine.
86
   * First force destruction of referenced PropEngine to enforce that
87
   * statistics are unregistered by the obsolete PropEngine object before
88
   * registered again by the new PropEngine object */
89
67
  d_propEngine.reset(nullptr);
90
67
  d_propEngine.reset(new prop::PropEngine(d_theoryEngine.get(), d_env));
91
67
  d_theoryEngine->setPropEngine(getPropEngine());
92
  // Notice that we do not reset TheoryEngine, nor does it require calling
93
  // finishInit again. In particular, TheoryEngine::finishInit does not
94
  // depend on knowing the associated PropEngine.
95
67
  d_propEngine->finishInit();
96
67
}
97
98
void SmtSolver::interrupt()
99
{
100
  if (d_propEngine != nullptr)
101
  {
102
    d_propEngine->interrupt();
103
  }
104
  if (d_theoryEngine != nullptr)
105
  {
106
    d_theoryEngine->interrupt();
107
  }
108
}
109
110
10562
void SmtSolver::shutdown()
111
{
112
10562
  if (d_propEngine != nullptr)
113
  {
114
9939
    d_propEngine->shutdown();
115
  }
116
10562
  if (d_theoryEngine != nullptr)
117
  {
118
9939
    d_theoryEngine->shutdown();
119
  }
120
10562
}
121
122
15278
Result SmtSolver::checkSatisfiability(Assertions& as,
123
                                      const std::vector<Node>& assumptions,
124
                                      bool isEntailmentCheck)
125
{
126
  // update the state to indicate we are about to run a check-sat
127
15278
  bool hasAssumptions = !assumptions.empty();
128
15278
  d_state.notifyCheckSat(hasAssumptions);
129
130
  // then, initialize the assertions
131
15278
  as.initializeCheckSat(assumptions, isEntailmentCheck);
132
133
  // make the check, where notice smt engine should be fully inited by now
134
135
15278
  Trace("smt") << "SmtSolver::check()" << endl;
136
137
15278
  const std::string& filename = d_env.getOptions().driver.filename;
138
15278
  ResourceManager* rm = d_env.getResourceManager();
139
15278
  if (rm->out())
140
  {
141
    Result::UnknownExplanation why =
142
        rm->outOfResources() ? Result::RESOURCEOUT : Result::TIMEOUT;
143
    return Result(Result::ENTAILMENT_UNKNOWN, why, filename);
144
  }
145
15278
  rm->beginCall();
146
147
  // Make sure the prop layer has all of the assertions
148
15278
  Trace("smt") << "SmtSolver::check(): processing assertions" << endl;
149
15278
  processAssertions(as);
150
15264
  Trace("smt") << "SmtSolver::check(): done processing assertions" << endl;
151
152
30528
  TimerStat::CodeTimer solveTimer(d_stats.d_solveTime);
153
154
15264
  Chat() << "solving..." << endl;
155
15264
  Trace("smt") << "SmtSolver::check(): running check" << endl;
156
30512
  Result result = d_propEngine->checkSat();
157
158
15248
  rm->endCall();
159
30496
  Trace("limit") << "SmtSolver::check(): cumulative millis "
160
30496
                 << rm->getTimeUsage() << ", resources "
161
15248
                 << rm->getResourceUsage() << endl;
162
163
45731
  if ((options::solveRealAsInt() || options::solveIntAsBV() > 0)
164
30525
      && result.asSatisfiabilityResult().isSat() == Result::UNSAT)
165
  {
166
6
    result = Result(Result::SAT_UNKNOWN, Result::UNKNOWN_REASON);
167
  }
168
  // flipped if we did a global negation
169
15248
  if (as.isGlobalNegated())
170
  {
171
6
    Trace("smt") << "SmtSolver::process global negate " << result << std::endl;
172
6
    if (result.asSatisfiabilityResult().isSat() == Result::UNSAT)
173
    {
174
4
      result = Result(Result::SAT);
175
    }
176
2
    else if (result.asSatisfiabilityResult().isSat() == Result::SAT)
177
    {
178
      // Only can answer unsat if the theory is satisfaction complete. This
179
      // includes linear arithmetic and bitvectors, which are the primary
180
      // targets for the global negate option. Other logics are possible here
181
      // but not considered.
182
4
      LogicInfo logic = d_env.getLogicInfo();
183
2
      if ((logic.isPure(theory::THEORY_ARITH) && logic.isLinear()) ||
184
          logic.isPure(theory::THEORY_BV))
185
      {
186
2
        result = Result(Result::UNSAT);
187
      }
188
      else
189
      {
190
        result = Result(Result::SAT_UNKNOWN, Result::UNKNOWN_REASON);
191
      }
192
    }
193
6
    Trace("smt") << "SmtSolver::global negate returned " << result << std::endl;
194
  }
195
196
  // set the filename on the result
197
30496
  Result r = Result(result, filename);
198
199
  // notify our state of the check-sat result
200
15248
  d_state.notifyCheckSatResult(hasAssumptions, r);
201
202
15248
  return r;
203
}
204
205
24251
void SmtSolver::processAssertions(Assertions& as)
206
{
207
38053
  TimerStat::CodeTimer paTimer(d_stats.d_processAssertionsTime);
208
24251
  d_env.getResourceManager()->spendResource(Resource::PreprocessStep);
209
24251
  Assert(d_state.isFullyReady());
210
211
24251
  preprocessing::AssertionPipeline& ap = as.getAssertionPipeline();
212
213
24251
  if (ap.size() == 0)
214
  {
215
    // nothing to do
216
10449
    return;
217
  }
218
219
  // process the assertions with the preprocessor
220
13802
  d_pp.process(as);
221
222
  // end: INVARIANT to maintain: no reordering of assertions or
223
  // introducing new ones
224
225
  // Push the formula to SAT
226
  {
227
13792
    Chat() << "converting to CNF..." << endl;
228
13792
    const std::vector<Node>& assertions = ap.ref();
229
    // It is important to distinguish the input assertions from the skolem
230
    // definitions, as the decision justification heuristic treates the latter
231
    // specially.
232
13792
    preprocessing::IteSkolemMap& ism = ap.getIteSkolemMap();
233
13792
    d_propEngine->assertInputFormulas(assertions, ism);
234
  }
235
236
  // clear the current assertions
237
13788
  as.clearCurrent();
238
}
239
240
358819
TheoryEngine* SmtSolver::getTheoryEngine() { return d_theoryEngine.get(); }
241
242
66426
prop::PropEngine* SmtSolver::getPropEngine() { return d_propEngine.get(); }
243
244
378
theory::QuantifiersEngine* SmtSolver::getQuantifiersEngine()
245
{
246
378
  Assert(d_theoryEngine != nullptr);
247
378
  return d_theoryEngine->getQuantifiersEngine();
248
}
249
250
Preprocessor* SmtSolver::getPreprocessor() { return &d_pp; }
251
252
}  // namespace smt
253
29577
}  // namespace cvc5