GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/smt/smt_solver.cpp Lines: 95 106 89.6 %
Date: 2021-08-03 Branches: 148 316 46.8 %

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