summaryrefslogtreecommitdiff
path: root/src/smt/smt_engine_state.cpp
blob: b33f1e13c72e97857b1c28347c7141ea43180fa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*********************                                                        */
/*! \file smt_engine_state.cpp
 ** \verbatim
 ** Top contributors (to current version):
 **   Andrew Reynolds, Morgan Deters, Ying Sheng
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
 ** in the top-level source directory and their institutional affiliations.
 ** All rights reserved.  See the file COPYING in the top-level source
 ** directory for licensing information.\endverbatim
 **
 ** \brief Utility for maintaining the state of the SMT engine.
 **/

#include "smt/smt_engine_state.h"

#include "options/smt_options.h"
#include "smt/smt_engine.h"

namespace cvc5 {
namespace smt {

SmtEngineState::SmtEngineState(context::Context* c,
                               context::UserContext* u,
                               SmtEngine& smt)
    : d_smt(smt),
      d_context(c),
      d_userContext(u),
      d_pendingPops(0),
      d_fullyInited(false),
      d_queryMade(false),
      d_needPostsolve(false),
      d_status(),
      d_expectedStatus(),
      d_smtMode(SmtMode::START)
{
}

void SmtEngineState::notifyExpectedStatus(const std::string& status)
{
  Assert(status == "sat" || status == "unsat" || status == "unknown")
      << "SmtEngineState::notifyExpectedStatus: unexpected status string "
      << status;
  d_expectedStatus = Result(status, d_filename);
}

void SmtEngineState::notifyResetAssertions()
{
  doPendingPops();
  while (!d_userLevels.empty())
  {
    userPop();
  }
  // Remember the global push/pop around everything when beyond Start mode
  // (see solver execution modes in the SMT-LIB standard)
  Assert(d_userLevels.size() == 0 && d_userContext->getLevel() == 1);
  popto(0);
}

void SmtEngineState::notifyCheckSat(bool hasAssumptions)
{
  // process the pending pops
  doPendingPops();
  if (d_queryMade && !options::incrementalSolving())
  {
    throw ModalException(
        "Cannot make multiple queries unless "
        "incremental solving is enabled "
        "(try --incremental)");
  }

  // Note that a query has been made and we are in assert mode
  d_queryMade = true;
  d_smtMode = SmtMode::ASSERT;

  // push if there are assumptions
  if (hasAssumptions)
  {
    internalPush();
  }
}

void SmtEngineState::notifyCheckSatResult(bool hasAssumptions, Result r)
{
  d_needPostsolve = true;

  // Pop the context
  if (hasAssumptions)
  {
    internalPop();
  }

  // Remember the status
  d_status = r;
  // Check against expected status
  if (!d_expectedStatus.isUnknown() && !d_status.isUnknown()
      && d_status != d_expectedStatus)
  {
    CVC4_FATAL() << "Expected result " << d_expectedStatus << " but got "
                 << d_status;
  }
  // clear expected status
  d_expectedStatus = Result();
  // Update the SMT mode
  switch (d_status.asSatisfiabilityResult().isSat())
  {
    case Result::UNSAT: d_smtMode = SmtMode::UNSAT; break;
    case Result::SAT: d_smtMode = SmtMode::SAT; break;
    default: d_smtMode = SmtMode::SAT_UNKNOWN;
  }
}

void SmtEngineState::notifyGetAbduct(bool success)
{
  if (success)
  {
    // successfully generated an abduct, update to abduct state
    d_smtMode = SmtMode::ABDUCT;
  }
  else
  {
    // failed, we revert to the assert state
    d_smtMode = SmtMode::ASSERT;
  }
}

void SmtEngineState::notifyGetInterpol(bool success)
{
  if (success)
  {
    // successfully generated an interpolant, update to interpol state
    d_smtMode = SmtMode::INTERPOL;
  }
  else
  {
    // failed, we revert to the assert state
    d_smtMode = SmtMode::ASSERT;
  }
}

void SmtEngineState::setup()
{
  // push a context
  push();
}

void SmtEngineState::finishInit()
{
  // set the flag to remember that we are fully initialized
  d_fullyInited = true;
}

void SmtEngineState::shutdown()
{
  doPendingPops();

  while (options::incrementalSolving() && d_userContext->getLevel() > 1)
  {
    internalPop(true);
  }
}

void SmtEngineState::cleanup()
{
  // pop to level zero
  popto(0);
}

void SmtEngineState::setFilename(const std::string& filename)
{
  d_filename = filename;
}

void SmtEngineState::userPush()
{
  if (!options::incrementalSolving())
  {
    throw ModalException(
        "Cannot push when not solving incrementally (use --incremental)");
  }
  // The problem isn't really "extended" yet, but this disallows
  // get-model after a push, simplifying our lives somewhat and
  // staying symmetric with pop.
  d_smtMode = SmtMode::ASSERT;

  d_userLevels.push_back(d_userContext->getLevel());
  internalPush();
  Trace("userpushpop") << "SmtEngineState: pushed to level "
                       << d_userContext->getLevel() << std::endl;
}

void SmtEngineState::userPop()
{
  if (!options::incrementalSolving())
  {
    throw ModalException(
        "Cannot pop when not solving incrementally (use --incremental)");
  }
  if (d_userLevels.size() == 0)
  {
    throw ModalException("Cannot pop beyond the first user frame");
  }
  // The problem isn't really "extended" yet, but this disallows
  // get-model after a pop, simplifying our lives somewhat.  It might
  // not be strictly necessary to do so, since the pops occur lazily,
  // but also it would be weird to have a legally-executed (get-model)
  // that only returns a subset of the assignment (because the rest
  // is no longer in scope!).
  d_smtMode = SmtMode::ASSERT;

  AlwaysAssert(d_userContext->getLevel() > 0);
  AlwaysAssert(d_userLevels.back() < d_userContext->getLevel());
  while (d_userLevels.back() < d_userContext->getLevel())
  {
    internalPop(true);
  }
  d_userLevels.pop_back();
}

void SmtEngineState::push()
{
  d_userContext->push();
  d_context->push();
}

void SmtEngineState::pop()
{
  d_userContext->pop();
  d_context->pop();
}

void SmtEngineState::popto(int toLevel)
{
  d_context->popto(toLevel);
  d_userContext->popto(toLevel);
}

context::UserContext* SmtEngineState::getUserContext() { return d_userContext; }

context::Context* SmtEngineState::getContext() { return d_context; }

Result SmtEngineState::getStatus() const { return d_status; }

bool SmtEngineState::isFullyInited() const { return d_fullyInited; }
bool SmtEngineState::isFullyReady() const
{
  return d_fullyInited && d_pendingPops == 0;
}
bool SmtEngineState::isQueryMade() const { return d_queryMade; }
size_t SmtEngineState::getNumUserLevels() const { return d_userLevels.size(); }

SmtMode SmtEngineState::getMode() const { return d_smtMode; }

const std::string& SmtEngineState::getFilename() const { return d_filename; }

void SmtEngineState::internalPush()
{
  Assert(d_fullyInited);
  Trace("smt") << "SmtEngineState::internalPush()" << std::endl;
  doPendingPops();
  if (options::incrementalSolving())
  {
    // notifies the SmtEngine to process the assertions immediately
    d_smt.notifyPushPre();
    d_userContext->push();
    // the context push is done inside of the SAT solver
    d_smt.notifyPushPost();
  }
}

void SmtEngineState::internalPop(bool immediate)
{
  Assert(d_fullyInited);
  Trace("smt") << "SmtEngineState::internalPop()" << std::endl;
  if (options::incrementalSolving())
  {
    ++d_pendingPops;
  }
  if (immediate)
  {
    doPendingPops();
  }
}

void SmtEngineState::doPendingPops()
{
  Trace("smt") << "SmtEngineState::doPendingPops()" << std::endl;
  Assert(d_pendingPops == 0 || options::incrementalSolving());
  // check to see if a postsolve() is pending
  if (d_needPostsolve)
  {
    d_smt.notifyPostSolvePre();
  }
  while (d_pendingPops > 0)
  {
    // the context pop is done inside of the SAT solver
    d_smt.notifyPopPre();
    // pop the context
    d_userContext->pop();
    --d_pendingPops;
    // no need for pop post (for now)
  }
  if (d_needPostsolve)
  {
    d_smt.notifyPostSolvePost();
    d_needPostsolve = false;
  }
}

}  // namespace smt
}  // namespace cvc5
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback