GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/smt/smt_engine.h Lines: 1 1 100.0 %
Date: 2021-05-22 Branches: 0 0 0.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Morgan Deters, Aina Niemetz
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
 * SmtEngine: the main public entry point of libcvc5.
14
 */
15
16
#include "cvc5_public.h"
17
18
#ifndef CVC5__SMT_ENGINE_H
19
#define CVC5__SMT_ENGINE_H
20
21
#include <map>
22
#include <memory>
23
#include <string>
24
#include <vector>
25
26
#include "context/cdhashmap_forward.h"
27
#include "cvc5_export.h"
28
#include "options/options.h"
29
#include "smt/output_manager.h"
30
#include "smt/smt_mode.h"
31
#include "theory/logic_info.h"
32
#include "util/result.h"
33
34
namespace cvc5 {
35
36
template <bool ref_count> class NodeTemplate;
37
typedef NodeTemplate<true> Node;
38
typedef NodeTemplate<false> TNode;
39
class TypeNode;
40
41
class Env;
42
class NodeManager;
43
class TheoryEngine;
44
class UnsatCore;
45
class LogicRequest;
46
class StatisticsRegistry;
47
class Printer;
48
class ResourceManager;
49
50
/* -------------------------------------------------------------------------- */
51
52
namespace api {
53
class Solver;
54
}  // namespace api
55
56
/* -------------------------------------------------------------------------- */
57
58
namespace context {
59
  class Context;
60
  class UserContext;
61
  }  // namespace context
62
63
/* -------------------------------------------------------------------------- */
64
65
namespace preprocessing {
66
class PreprocessingPassContext;
67
}
68
69
/* -------------------------------------------------------------------------- */
70
71
namespace prop {
72
  class PropEngine;
73
  }  // namespace prop
74
75
/* -------------------------------------------------------------------------- */
76
77
namespace smt {
78
/** Utilities */
79
class Model;
80
class SmtEngineState;
81
class AbstractValues;
82
class Assertions;
83
class DumpManager;
84
class ResourceOutListener;
85
class SmtNodeManagerListener;
86
class OptionsManager;
87
class Preprocessor;
88
class CheckModels;
89
/** Subsolvers */
90
class SmtSolver;
91
class SygusSolver;
92
class AbductionSolver;
93
class InterpolationSolver;
94
class QuantElimSolver;
95
96
struct SmtEngineStatistics;
97
class SmtScope;
98
class PfManager;
99
class UnsatCoreManager;
100
101
}  // namespace smt
102
103
/* -------------------------------------------------------------------------- */
104
105
namespace theory {
106
  class Rewriter;
107
  class QuantifiersEngine;
108
  }  // namespace theory
109
110
/* -------------------------------------------------------------------------- */
111
112
class CVC5_EXPORT SmtEngine
113
{
114
  friend class ::cvc5::api::Solver;
115
  friend class ::cvc5::smt::SmtEngineState;
116
  friend class ::cvc5::smt::SmtScope;
117
  friend class ::cvc5::LogicRequest;
118
119
  /* .......................................................................  */
120
 public:
121
  /* .......................................................................  */
122
123
  /**
124
   * Construct an SmtEngine with the given expression manager.
125
   * If provided, optr is a pointer to a set of options that should initialize the values
126
   * of the options object owned by this class.
127
   */
128
  SmtEngine(NodeManager* nm, Options* optr = nullptr);
129
  /** Destruct the SMT engine.  */
130
  ~SmtEngine();
131
132
  //--------------------------------------------- concerning the state
133
134
  /**
135
   * This is the main initialization procedure of the SmtEngine.
136
   *
137
   * Should be called whenever the final options and logic for the problem are
138
   * set (at least, those options that are not permitted to change after
139
   * assertions and queries are made).
140
   *
141
   * Internally, this creates the theory engine, prop engine, decision engine,
142
   * and other utilities whose initialization depends on the final set of
143
   * options being set.
144
   *
145
   * This post-construction initialization is automatically triggered by the
146
   * use of the SmtEngine; e.g. when the first formula is asserted, a call
147
   * to simplify() is issued, a scope is pushed, etc.
148
   */
149
  void finishInit();
150
  /**
151
   * Return true if this SmtEngine is fully initialized (post-construction)
152
   * by the above call.
153
   */
154
  bool isFullyInited() const;
155
  /**
156
   * Return true if a checkEntailed() or checkSatisfiability() has been made.
157
   */
158
  bool isQueryMade() const;
159
  /** Return the user context level.  */
160
  size_t getNumUserLevels() const;
161
  /** Return the current mode of the solver. */
162
  SmtMode getSmtMode() const;
163
  /**
164
   * Whether the SmtMode allows for get-value, get-model, get-assignment, etc.
165
   * This is equivalent to:
166
   * getSmtMode()==SmtMode::SAT || getSmtMode()==SmtMode::SAT_UNKNOWN
167
   */
168
  bool isSmtModeSat() const;
169
  /**
170
   * Returns the most recent result of checkSat/checkEntailed or
171
   * (set-info :status).
172
   */
173
  Result getStatusOfLastCommand() const;
174
  //--------------------------------------------- end concerning the state
175
176
  /**
177
   * Set the logic of the script.
178
   * @throw ModalException, LogicException
179
   */
180
  void setLogic(const std::string& logic);
181
182
  /**
183
   * Set the logic of the script.
184
   * @throw ModalException, LogicException
185
   */
186
  void setLogic(const char* logic);
187
188
  /**
189
   * Set the logic of the script.
190
   * @throw ModalException
191
   */
192
  void setLogic(const LogicInfo& logic);
193
194
  /** Get the logic information currently set. */
195
  const LogicInfo& getLogicInfo() const;
196
197
  /** Get the logic information set by the user. */
198
  LogicInfo getUserLogicInfo() const;
199
200
  /**
201
   * Set information about the script executing.
202
   */
203
  void setInfo(const std::string& key, const std::string& value);
204
205
  /** Return true if given keyword is a valid SMT-LIB v2 get-info flag. */
206
  bool isValidGetInfoFlag(const std::string& key) const;
207
208
  /** Query information about the SMT environment.  */
209
  std::string getInfo(const std::string& key) const;
210
211
  /**
212
   * Set an aspect of the current SMT execution environment.
213
   * @throw OptionException, ModalException
214
   */
215
  void setOption(const std::string& key, const std::string& value);
216
217
  /** Set is internal subsolver.
218
   *
219
   * This function is called on SmtEngine objects that are created internally.
220
   * It is used to mark that this SmtEngine should not perform preprocessing
221
   * passes that rephrase the input, such as --sygus-rr-synth-input or
222
   * --sygus-abduct.
223
   */
224
  void setIsInternalSubsolver();
225
  /** Is this an internal subsolver? */
226
  bool isInternalSubsolver() const;
227
228
  /**
229
   * Notify that we are now parsing the input with the given filename.
230
   * This call sets the filename maintained by this SmtEngine for bookkeeping
231
   * and also makes a copy of the current options of this SmtEngine. This
232
   * is required so that the SMT-LIB command (reset) returns the SmtEngine
233
   * to a state where its options were prior to parsing but after e.g.
234
   * reading command line options.
235
   */
236
  void notifyStartParsing(const std::string& filename) CVC5_EXPORT;
237
  /** return the input name (if any) */
238
  const std::string& getFilename() const;
239
240
  /**
241
   * Helper method for the API to put the last check result into the statistics.
242
   */
243
  void setResultStatistic(const std::string& result) CVC5_EXPORT;
244
  /**
245
   * Helper method for the API to put the total runtime into the statistics.
246
   */
247
  void setTotalTimeStatistic(double seconds) CVC5_EXPORT;
248
249
  /**
250
   * Get the model (only if immediately preceded by a SAT or NOT_ENTAILED
251
   * query).  Only permitted if produce-models is on.
252
   */
253
  smt::Model* getModel();
254
255
  /**
256
   * Block the current model. Can be called only if immediately preceded by
257
   * a SAT or INVALID query. Only permitted if produce-models is on, and the
258
   * block-models option is set to a mode other than "none".
259
   *
260
   * This adds an assertion to the assertion stack that blocks the current
261
   * model based on the current options configured by cvc5.
262
   *
263
   * The return value has the same meaning as that of assertFormula.
264
   */
265
  Result blockModel();
266
267
  /**
268
   * Block the current model values of (at least) the values in exprs.
269
   * Can be called only if immediately preceded by a SAT or NOT_ENTAILED query.
270
   * Only permitted if produce-models is on, and the block-models option is set
271
   * to a mode other than "none".
272
   *
273
   * This adds an assertion to the assertion stack of the form:
274
   *  (or (not (= exprs[0] M0)) ... (not (= exprs[n] Mn)))
275
   * where M0 ... Mn are the current model values of exprs[0] ... exprs[n].
276
   *
277
   * The return value has the same meaning as that of assertFormula.
278
   */
279
  Result blockModelValues(const std::vector<Node>& exprs);
280
281
  /**
282
   * Declare heap. For smt2 inputs, this is called when the command
283
   * (declare-heap (locT datat)) is invoked by the user. This sets locT as the
284
   * location type and dataT is the data type for the heap. This command should
285
   * be executed only once, and must be invoked before solving separation logic
286
   * inputs.
287
   */
288
  void declareSepHeap(TypeNode locT, TypeNode dataT);
289
290
  /**
291
   * Get the separation heap types, which extracts which types were passed to
292
   * the method above.
293
   *
294
   * @return true if the separation logic heap types have been declared.
295
   */
296
  bool getSepHeapTypes(TypeNode& locT, TypeNode& dataT);
297
298
  /** When using separation logic, obtain the expression for the heap.  */
299
  Node getSepHeapExpr();
300
301
  /** When using separation logic, obtain the expression for nil.  */
302
  Node getSepNilExpr();
303
304
  /**
305
   * Get an aspect of the current SMT execution environment.
306
   * @throw OptionException
307
   */
308
  std::string getOption(const std::string& key) const;
309
310
  /**
311
   * Define function func in the current context to be:
312
   *   (lambda (formals) formula)
313
   * This adds func to the list of defined functions, which indicates that
314
   * all occurrences of func should be expanded during expandDefinitions.
315
   *
316
   * @param func a variable of function type that expects the arguments in
317
   *             formal
318
   * @param formals a list of BOUND_VARIABLE expressions
319
   * @param formula The body of the function, must not contain func
320
   * @param global True if this definition is global (i.e. should persist when
321
   *               popping the user context)
322
   */
323
  void defineFunction(Node func,
324
                      const std::vector<Node>& formals,
325
                      Node formula,
326
                      bool global = false);
327
328
  /**
329
   * Define functions recursive
330
   *
331
   * For each i, this constrains funcs[i] in the current context to be:
332
   *   (lambda (formals[i]) formulas[i])
333
   * where formulas[i] may contain variables from funcs. Unlike defineFunction
334
   * above, we do not add funcs[i] to the set of defined functions. Instead,
335
   * we consider funcs[i] to be a free uninterpreted function, and add:
336
   *   forall formals[i]. f(formals[i]) = formulas[i]
337
   * to the set of assertions in the current context.
338
   * This method expects input such that for each i:
339
   * - func[i] : a variable of function type that expects the arguments in
340
   *             formals[i], and
341
   * - formals[i] : a list of BOUND_VARIABLE expressions.
342
   *
343
   * @param global True if this definition is global (i.e. should persist when
344
   *               popping the user context)
345
   */
346
  void defineFunctionsRec(const std::vector<Node>& funcs,
347
                          const std::vector<std::vector<Node>>& formals,
348
                          const std::vector<Node>& formulas,
349
                          bool global = false);
350
  /**
351
   * Define function recursive
352
   * Same as above, but for a single function.
353
   */
354
  void defineFunctionRec(Node func,
355
                         const std::vector<Node>& formals,
356
                         Node formula,
357
                         bool global = false);
358
  /**
359
   * Add a formula to the current context: preprocess, do per-theory
360
   * setup, use processAssertionList(), asserting to T-solver for
361
   * literals and conjunction of literals.  Returns false if
362
   * immediately determined to be inconsistent.  This version
363
   * takes a Boolean flag to determine whether to include this asserted
364
   * formula in an unsat core (if one is later requested).
365
   *
366
   * @throw TypeCheckingException, LogicException, UnsafeInterruptException
367
   */
368
  Result assertFormula(const Node& formula, bool inUnsatCore = true);
369
370
  /**
371
   * Check if a given (set of) expression(s) is entailed with respect to the
372
   * current set of assertions. We check this by asserting the negation of
373
   * the (big AND over the) given (set of) expression(s).
374
   * Returns ENTAILED, NOT_ENTAILED, or ENTAILMENT_UNKNOWN result.
375
   *
376
   * @throw Exception
377
   */
378
  Result checkEntailed(const Node& assumption, bool inUnsatCore = true);
379
  Result checkEntailed(const std::vector<Node>& assumptions,
380
                       bool inUnsatCore = true);
381
382
  /**
383
   * Assert a formula (if provided) to the current context and call
384
   * check().  Returns SAT, UNSAT, or SAT_UNKNOWN result.
385
   *
386
   * @throw Exception
387
   */
388
  Result checkSat();
389
  Result checkSat(const Node& assumption, bool inUnsatCore = true);
390
  Result checkSat(const std::vector<Node>& assumptions,
391
                  bool inUnsatCore = true);
392
393
  /**
394
   * Returns a set of so-called "failed" assumptions.
395
   *
396
   * The returned set is a subset of the set of assumptions of a previous
397
   * (unsatisfiable) call to checkSatisfiability. Calling checkSatisfiability
398
   * with this set of failed assumptions still produces an unsat answer.
399
   *
400
   * Note that the returned set of failed assumptions is not necessarily
401
   * minimal.
402
   */
403
  std::vector<Node> getUnsatAssumptions(void);
404
405
  /*---------------------------- sygus commands  ---------------------------*/
406
407
  /**
408
   * Add sygus variable declaration.
409
   *
410
   * Declared SyGuS variables may be used in SyGuS constraints, in which they
411
   * are assumed to be universally quantified.
412
   *
413
   * In SyGuS semantics, declared functions are treated in the same manner as
414
   * declared variables, i.e. as universally quantified (function) variables
415
   * which can occur in the SyGuS constraints that compose the conjecture to
416
   * which a function is being synthesized. Thus declared functions should use
417
   * this method as well.
418
   */
419
  void declareSygusVar(Node var);
420
421
  /**
422
   * Add a function-to-synthesize declaration.
423
   *
424
   * The given sygusType may not correspond to the actual function type of func
425
   * but to a datatype encoding the syntax restrictions for the
426
   * function-to-synthesize. In this case this information is stored to be used
427
   * during solving.
428
   *
429
   * vars contains the arguments of the function-to-synthesize. These variables
430
   * are also stored to be used during solving.
431
   *
432
   * isInv determines whether the function-to-synthesize is actually an
433
   * invariant. This information is necessary if we are dumping a command
434
   * corresponding to this declaration, so that it can be properly printed.
435
   */
436
  void declareSynthFun(Node func,
437
                       TypeNode sygusType,
438
                       bool isInv,
439
                       const std::vector<Node>& vars);
440
  /**
441
   * Same as above, without a sygus type.
442
   */
443
  void declareSynthFun(Node func, bool isInv, const std::vector<Node>& vars);
444
445
  /** Add a regular sygus constraint.*/
446
  void assertSygusConstraint(Node constraint);
447
448
  /**
449
   * Add an invariant constraint.
450
   *
451
   * Invariant constraints are not explicitly declared: they are given in terms
452
   * of the invariant-to-synthesize, the pre condition, transition relation and
453
   * post condition. The actual constraint is built based on the inputs of these
454
   * place holder predicates :
455
   *
456
   * PRE(x) -> INV(x)
457
   * INV() ^ TRANS(x, x') -> INV(x')
458
   * INV(x) -> POST(x)
459
   *
460
   * The regular and primed variables are retrieved from the declaration of the
461
   * invariant-to-synthesize.
462
   */
463
  void assertSygusInvConstraint(Node inv, Node pre, Node trans, Node post);
464
  /**
465
   * Assert a synthesis conjecture to the current context and call
466
   * check().  Returns sat, unsat, or unknown result.
467
   *
468
   * The actual synthesis conjecture is built based on the previously
469
   * communicated information to this module (universal variables, defined
470
   * functions, functions-to-synthesize, and which constraints compose it). The
471
   * built conjecture is a higher-order formula of the form
472
   *
473
   * exists f1...fn . forall v1...vm . F
474
   *
475
   * in which f1...fn are the functions-to-synthesize, v1...vm are the declared
476
   * universal variables and F is the set of declared constraints.
477
   *
478
   * @throw Exception
479
   */
480
  Result checkSynth();
481
482
  /*------------------------- end of sygus commands ------------------------*/
483
484
  /**
485
   * Declare pool whose initial value is the terms in initValue. A pool is
486
   * a variable of type (Set T) that is used in quantifier annotations and does
487
   * not occur in constraints.
488
   *
489
   * @param p The pool to declare, which should be a variable of type (Set T)
490
   * for some type T.
491
   * @param initValue The initial value of p, which should be a vector of terms
492
   * of type T.
493
   */
494
  void declarePool(const Node& p, const std::vector<Node>& initValue);
495
  /**
496
   * Simplify a formula without doing "much" work.  Does not involve
497
   * the SAT Engine in the simplification, but uses the current
498
   * definitions, assertions, and the current partial model, if one
499
   * has been constructed.  It also involves theory normalization.
500
   *
501
   * @throw TypeCheckingException, LogicException, UnsafeInterruptException
502
   *
503
   * @todo (design) is this meant to give an equivalent or an
504
   * equisatisfiable formula?
505
   */
506
  Node simplify(const Node& e);
507
508
  /**
509
   * Expand the definitions in a term or formula.
510
   *
511
   * @param n The node to expand
512
   *
513
   * @throw TypeCheckingException, LogicException, UnsafeInterruptException
514
   */
515
  Node expandDefinitions(const Node& n);
516
517
  /**
518
   * Get the assigned value of an expr (only if immediately preceded by a SAT
519
   * or NOT_ENTAILED query).  Only permitted if the SmtEngine is set to operate
520
   * interactively and produce-models is on.
521
   *
522
   * @throw ModalException, TypeCheckingException, LogicException,
523
   *        UnsafeInterruptException
524
   */
525
  Node getValue(const Node& e) const;
526
527
  /**
528
   * Same as getValue but for a vector of expressions
529
   */
530
  std::vector<Node> getValues(const std::vector<Node>& exprs);
531
532
  /** print instantiations
533
   *
534
   * Print all instantiations for all quantified formulas on out,
535
   * returns true if at least one instantiation was printed. The type of output
536
   * (list, num, etc.) is determined by printInstMode.
537
   */
538
  void printInstantiations(std::ostream& out);
539
  /**
540
   * Print the current proof. This method should be called after an UNSAT
541
   * response. It gets the proof of false from the PropEngine and passes
542
   * it to the ProofManager, which post-processes the proof and prints it
543
   * in the proper format.
544
   */
545
  void printProof();
546
547
  /**
548
   * Get synth solution.
549
   *
550
   * This method returns true if we are in a state immediately preceded by
551
   * a successful call to checkSynth.
552
   *
553
   * This method adds entries to solMap that map functions-to-synthesize with
554
   * their solutions, for all active conjectures. This should be called
555
   * immediately after the solver answers unsat for sygus input.
556
   *
557
   * Specifically, given a sygus conjecture of the form
558
   *   exists x1...xn. forall y1...yn. P( x1...xn, y1...yn )
559
   * where x1...xn are second order bound variables, we map each xi to
560
   * lambda term in solMap such that
561
   *    forall y1...yn. P( solMap[x1]...solMap[xn], y1...yn )
562
   * is a valid formula.
563
   */
564
  bool getSynthSolutions(std::map<Node, Node>& solMap);
565
566
  /**
567
   * Do quantifier elimination.
568
   *
569
   * This function takes as input a quantified formula q
570
   * of the form:
571
   *   Q x1...xn. P( x1...xn, y1...yn )
572
   * where P( x1...xn, y1...yn ) is a quantifier-free
573
   * formula in a logic that supports quantifier elimination.
574
   * Currently, the only logics supported by quantifier
575
   * elimination is LRA and LIA.
576
   *
577
   * This function returns a formula ret such that, given
578
   * the current set of formulas A asserted to this SmtEngine :
579
   *
580
   * If doFull = true, then
581
   *   - ( A ^ q ) and ( A ^ ret ) are equivalent
582
   *   - ret is quantifier-free formula containing
583
   *     only free variables in y1...yn.
584
   *
585
   * If doFull = false, then
586
   *   - (A ^ q) => (A ^ ret) if Q is forall or
587
   *     (A ^ ret) => (A ^ q) if Q is exists,
588
   *   - ret is quantifier-free formula containing
589
   *     only free variables in y1...yn,
590
   *   - If Q is exists, let A^Q_n be the formula
591
   *       A ^ ~ret^Q_1 ^ ... ^ ~ret^Q_n
592
   *     where for each i=1,...n, formula ret^Q_i
593
   *     is the result of calling doQuantifierElimination
594
   *     for q with the set of assertions A^Q_{i-1}.
595
   *     Similarly, if Q is forall, then let A^Q_n be
596
   *       A ^ ret^Q_1 ^ ... ^ ret^Q_n
597
   *     where ret^Q_i is the same as above.
598
   *     In either case, we have that ret^Q_j will
599
   *     eventually be true or false, for some finite j.
600
   *
601
   * The former feature is quantifier elimination, and
602
   * is run on invocations of the smt2 extended command get-qe.
603
   * The latter feature is referred to as partial quantifier
604
   * elimination, and is run on invocations of the smt2
605
   * extended command get-qe-disjunct, which can be used
606
   * for incrementally computing the result of a
607
   * quantifier elimination.
608
   *
609
   * The argument strict is whether to output
610
   * warnings, such as when an unexpected logic is used.
611
   *
612
   * throw@ Exception
613
   */
614
  Node getQuantifierElimination(Node q, bool doFull, bool strict = true);
615
616
  /**
617
   * This method asks this SMT engine to find an interpolant with respect to
618
   * the current assertion stack (call it A) and the conjecture (call it B). If
619
   * this method returns true, then interpolant is set to a formula I such that
620
   * A ^ ~I and I ^ ~B are both unsatisfiable.
621
   *
622
   * The argument grammarType is a sygus datatype type that encodes the syntax
623
   * restrictions on the shapes of possible solutions.
624
   *
625
   * This method invokes a separate copy of the SMT engine for solving the
626
   * corresponding sygus problem for generating such a solution.
627
   */
628
  bool getInterpol(const Node& conj,
629
                   const TypeNode& grammarType,
630
                   Node& interpol);
631
632
  /** Same as above, but without user-provided grammar restrictions */
633
  bool getInterpol(const Node& conj, Node& interpol);
634
635
  /**
636
   * This method asks this SMT engine to find an abduct with respect to the
637
   * current assertion stack (call it A) and the conjecture (call it B).
638
   * If this method returns true, then abd is set to a formula C such that
639
   * A ^ C is satisfiable, and A ^ ~B ^ C is unsatisfiable.
640
   *
641
   * The argument grammarType is a sygus datatype type that encodes the syntax
642
   * restrictions on the shape of possible solutions.
643
   *
644
   * This method invokes a separate copy of the SMT engine for solving the
645
   * corresponding sygus problem for generating such a solution.
646
   */
647
  bool getAbduct(const Node& conj, const TypeNode& grammarType, Node& abd);
648
649
  /** Same as above, but without user-provided grammar restrictions */
650
  bool getAbduct(const Node& conj, Node& abd);
651
652
  /**
653
   * Get list of quantified formulas that were instantiated on the last call
654
   * to check-sat.
655
   */
656
  void getInstantiatedQuantifiedFormulas(std::vector<Node>& qs);
657
658
  /**
659
   * Get instantiation term vectors for quantified formula q.
660
   *
661
   * This method is similar to above, but in the example above, we return the
662
   * (vectors of) terms t1, ..., tn instead.
663
   *
664
   * Notice that these are not guaranteed to come in the same order as the
665
   * instantiation lemmas above.
666
   */
667
  void getInstantiationTermVectors(Node q,
668
                                   std::vector<std::vector<Node>>& tvecs);
669
  /**
670
   * As above but only the instantiations that were relevant for the
671
   * refutation.
672
   */
673
  void getRelevantInstantiationTermVectors(
674
      std::map<Node, std::vector<std::vector<Node>>>& insts);
675
  /**
676
   * Get instantiation term vectors, which maps each instantiated quantified
677
   * formula to the list of instantiations for that quantified formula. This
678
   * list is minimized if proofs are enabled, and this call is immediately
679
   * preceded by an UNSAT or ENTAILED query
680
   */
681
  void getInstantiationTermVectors(
682
      std::map<Node, std::vector<std::vector<Node>>>& insts);
683
684
  /**
685
   * Get an unsatisfiable core (only if immediately preceded by an UNSAT or
686
   * ENTAILED query).  Only permitted if cvc5 was built with unsat-core support
687
   * and produce-unsat-cores is on.
688
   */
689
  UnsatCore getUnsatCore();
690
691
  /**
692
   * Get a refutation proof (only if immediately preceded by an UNSAT or
693
   * ENTAILED query). Only permitted if cvc5 was built with proof support and
694
   * the proof option is on. */
695
  std::string getProof();
696
697
  /**
698
   * Get the current set of assertions.  Only permitted if the
699
   * SmtEngine is set to operate interactively.
700
   */
701
  std::vector<Node> getAssertions();
702
703
  /**
704
   * Push a user-level context.
705
   * throw@ ModalException, LogicException, UnsafeInterruptException
706
   */
707
  void push();
708
709
  /**
710
   * Pop a user-level context.  Throws an exception if nothing to pop.
711
   * @throw ModalException
712
   */
713
  void pop();
714
715
  /** Reset all assertions, global declarations, etc.  */
716
  void resetAssertions();
717
718
  /**
719
   * Interrupt a running query.  This can be called from another thread
720
   * or from a signal handler.  Throws a ModalException if the SmtEngine
721
   * isn't currently in a query.
722
   *
723
   * @throw ModalException
724
   */
725
  void interrupt();
726
727
  /**
728
   * Set a resource limit for SmtEngine operations.  This is like a time
729
   * limit, but it's deterministic so that reproducible results can be
730
   * obtained.  Currently, it's based on the number of conflicts.
731
   * However, please note that the definition may change between different
732
   * versions of cvc5 (as may the number of conflicts required, anyway),
733
   * and it might even be different between instances of the same version
734
   * of cvc5 on different platforms.
735
   *
736
   * A cumulative and non-cumulative (per-call) resource limit can be
737
   * set at the same time.  A call to setResourceLimit() with
738
   * cumulative==true replaces any cumulative resource limit currently
739
   * in effect; a call with cumulative==false replaces any per-call
740
   * resource limit currently in effect.  Time limits can be set in
741
   * addition to resource limits; the SmtEngine obeys both.  That means
742
   * that up to four independent limits can control the SmtEngine
743
   * at the same time.
744
   *
745
   * When an SmtEngine is first created, it has no time or resource
746
   * limits.
747
   *
748
   * Currently, these limits only cause the SmtEngine to stop what its
749
   * doing when the limit expires (or very shortly thereafter); no
750
   * heuristics are altered by the limits or the threat of them expiring.
751
   * We reserve the right to change this in the future.
752
   *
753
   * @param units the resource limit, or 0 for no limit
754
   * @param cumulative whether this resource limit is to be a cumulative
755
   * resource limit for all remaining calls into the SmtEngine (true), or
756
   * whether it's a per-call resource limit (false); the default is false
757
   */
758
  void setResourceLimit(uint64_t units, bool cumulative = false);
759
760
  /**
761
   * Set a per-call time limit for SmtEngine operations.
762
   *
763
   * A per-call time limit can be set at the same time and replaces
764
   * any per-call time limit currently in effect.
765
   * Resource limits (either per-call or cumulative) can be set in
766
   * addition to a time limit; the SmtEngine obeys all three of them.
767
   *
768
   * Note that the per-call timer only ticks away when one of the
769
   * SmtEngine's workhorse functions (things like assertFormula(),
770
   * checkEntailed(), checkSat(), and simplify()) are running.
771
   * Between calls, the timer is still.
772
   *
773
   * When an SmtEngine is first created, it has no time or resource
774
   * limits.
775
   *
776
   * Currently, these limits only cause the SmtEngine to stop what its
777
   * doing when the limit expires (or very shortly thereafter); no
778
   * heuristics are altered by the limits or the threat of them expiring.
779
   * We reserve the right to change this in the future.
780
   *
781
   * @param millis the time limit in milliseconds, or 0 for no limit
782
   */
783
  void setTimeLimit(uint64_t millis);
784
785
  /**
786
   * Get the current resource usage count for this SmtEngine.  This
787
   * function can be used to ascertain reasonable values to pass as
788
   * resource limits to setResourceLimit().
789
   */
790
  unsigned long getResourceUsage() const;
791
792
  /** Get the current millisecond count for this SmtEngine.  */
793
  unsigned long getTimeUsage() const;
794
795
  /**
796
   * Get the remaining resources that can be consumed by this SmtEngine
797
   * according to the currently-set cumulative resource limit.  If there
798
   * is not a cumulative resource limit set, this function throws a
799
   * ModalException.
800
   *
801
   * @throw ModalException
802
   */
803
  unsigned long getResourceRemaining() const;
804
805
  /** Permit access to the underlying NodeManager. */
806
  NodeManager* getNodeManager() const;
807
808
  /**
809
   * Print statistics from the statistics registry in the env object owned by
810
   * this SmtEngine.
811
   */
812
  void printStatistics(std::ostream& out) const;
813
814
  /**
815
   * Print statistics from the statistics registry in the env object owned by
816
   * this SmtEngine. Safe to use in a signal handler.
817
   */
818
  void printStatisticsSafe(int fd) const;
819
820
  /**
821
   * Print the changes to the statistics from the statistics registry in the
822
   * env object owned by this SmtEngine since this method was called the last
823
   * time. Internally prints the diff and then stores a snapshot for the next
824
   * call.
825
   */
826
  void printStatisticsDiff(std::ostream&) const;
827
828
  /**
829
   * Set user attribute.
830
   * This function is called when an attribute is set by a user.
831
   * In SMT-LIBv2 this is done via the syntax (! expr :attr)
832
   */
833
  void setUserAttribute(const std::string& attr,
834
                        Node expr,
835
                        const std::vector<Node>& expr_values,
836
                        const std::string& str_value);
837
838
  /** Get the options object (const and non-const versions) */
839
  Options& getOptions();
840
  const Options& getOptions() const;
841
842
  /** Get a pointer to the UserContext owned by this SmtEngine. */
843
  context::UserContext* getUserContext();
844
845
  /** Get a pointer to the Context owned by this SmtEngine. */
846
  context::Context* getContext();
847
848
  /** Get a pointer to the TheoryEngine owned by this SmtEngine. */
849
  TheoryEngine* getTheoryEngine();
850
851
  /** Get a pointer to the PropEngine owned by this SmtEngine. */
852
  prop::PropEngine* getPropEngine();
853
854
  /** Get the resource manager of this SMT engine */
855
  ResourceManager* getResourceManager() const;
856
857
  /** Permit access to the underlying dump manager. */
858
  smt::DumpManager* getDumpManager();
859
860
  /** Get the printer used by this SMT engine */
861
  const Printer& getPrinter() const;
862
863
  /** Get the output manager for this SMT engine */
864
  OutputManager& getOutputManager();
865
866
  /** Get a pointer to the Rewriter owned by this SmtEngine. */
867
  theory::Rewriter* getRewriter();
868
  /**
869
   * Get expanded assertions.
870
   *
871
   * Return the set of assertions, after expanding definitions.
872
   */
873
  std::vector<Node> getExpandedAssertions();
874
875
  /**
876
   * !!!!! temporary, until the environment is passsed to all classes that
877
   * require it.
878
   */
879
  Env& getEnv();
880
  /* .......................................................................  */
881
 private:
882
  /* .......................................................................  */
883
884
  // disallow copy/assignment
885
  SmtEngine(const SmtEngine&) = delete;
886
  SmtEngine& operator=(const SmtEngine&) = delete;
887
888
  /** Set solver instance that owns this SmtEngine. */
889
6615
  void setSolver(api::Solver* solver) { d_solver = solver; }
890
891
  /** Get a pointer to the (new) PfManager owned by this SmtEngine. */
892
  smt::PfManager* getPfManager() { return d_pfManager.get(); };
893
894
  /** Get a pointer to the StatisticsRegistry owned by this SmtEngine. */
895
  StatisticsRegistry& getStatisticsRegistry();
896
897
  /**
898
   * Internal method to get an unsatisfiable core (only if immediately preceded
899
   * by an UNSAT or ENTAILED query). Only permitted if cvc5 was built with
900
   * unsat-core support and produce-unsat-cores is on. Does not dump the
901
   * command.
902
   */
903
  UnsatCore getUnsatCoreInternal();
904
905
  /**
906
   * Check that a generated proof checks. This method is the same as printProof,
907
   * but does not print the proof. Like that method, it should be called
908
   * after an UNSAT response. It ensures that a well-formed proof of false
909
   * can be constructed by the combination of the PropEngine and ProofManager.
910
   */
911
  void checkProof();
912
913
  /**
914
   * Check that an unsatisfiable core is indeed unsatisfiable.
915
   */
916
  void checkUnsatCore();
917
918
  /**
919
   * Check that a generated Model (via getModel()) actually satisfies
920
   * all user assertions.
921
   */
922
  void checkModel(bool hardFailure = true);
923
924
  /**
925
   * Check that a solution to an interpolation problem is indeed a solution.
926
   *
927
   * The check is made by determining that the assertions imply the solution of
928
   * the interpolation problem (interpol), and the solution implies the goal
929
   * (conj). If these criteria are not met, an internal error is thrown.
930
   */
931
  void checkInterpol(Node interpol,
932
                     const std::vector<Node>& easserts,
933
                     const Node& conj);
934
935
  /**
936
   * This is called by the destructor, just before destroying the
937
   * PropEngine, TheoryEngine, and DecisionEngine (in that order).  It
938
   * is important because there are destruction ordering issues
939
   * between PropEngine and Theory.
940
   */
941
  void shutdown();
942
943
  /**
944
   * Quick check of consistency in current context: calls
945
   * processAssertionList() then look for inconsistency (based only on
946
   * that).
947
   */
948
  Result quickCheck();
949
950
  /**
951
   * Get the (SMT-level) model pointer, if we are in SAT mode. Otherwise,
952
   * return nullptr.
953
   *
954
   * This ensures that the underlying theory model of the SmtSolver maintained
955
   * by this class is currently available, which means that cvc5 is producing
956
   * models, and is in "SAT mode", otherwise a recoverable exception is thrown.
957
   *
958
   * @param c used for giving an error message to indicate the context
959
   * this method was called.
960
   */
961
  smt::Model* getAvailableModel(const char* c) const;
962
  /**
963
   * Get available quantifiers engine, which throws a modal exception if it
964
   * does not exist. This can happen if a quantifiers-specific call (e.g.
965
   * getInstantiatedQuantifiedFormulas) is called in a non-quantified logic.
966
   *
967
   * @param c used for giving an error message to indicate the context
968
   * this method was called.
969
   */
970
  theory::QuantifiersEngine* getAvailableQuantifiersEngine(const char* c) const;
971
972
  // --------------------------------------- callbacks from the state
973
  /**
974
   * Notify push pre, which is called just before the user context of the state
975
   * pushes. This processes all pending assertions.
976
   */
977
  void notifyPushPre();
978
  /**
979
   * Notify push post, which is called just after the user context of the state
980
   * pushes. This performs a push on the underlying prop engine.
981
   */
982
  void notifyPushPost();
983
  /**
984
   * Notify pop pre, which is called just before the user context of the state
985
   * pops. This performs a pop on the underlying prop engine.
986
   */
987
  void notifyPopPre();
988
  /**
989
   * Notify post solve pre, which is called once per check-sat query. It
990
   * is triggered when the first d_state.doPendingPops() is issued after the
991
   * check-sat. This method is called before the contexts pop in the method
992
   * doPendingPops.
993
   */
994
  void notifyPostSolvePre();
995
  /**
996
   * Same as above, but after contexts are popped. This calls the postsolve
997
   * method of the underlying TheoryEngine.
998
   */
999
  void notifyPostSolvePost();
1000
  // --------------------------------------- end callbacks from the state
1001
1002
  /**
1003
   * Internally handle the setting of a logic.  This function should always
1004
   * be called when d_logic is updated.
1005
   */
1006
  void setLogicInternal();
1007
1008
  /*
1009
   * Check satisfiability (used to check satisfiability and entailment).
1010
   */
1011
  Result checkSatInternal(const std::vector<Node>& assumptions,
1012
                          bool inUnsatCore,
1013
                          bool isEntailmentCheck);
1014
1015
  /**
1016
   * Check that all Expr in formals are of BOUND_VARIABLE kind, where func is
1017
   * the function that the formal argument list is for. This method is used
1018
   * as a helper function when defining (recursive) functions.
1019
   */
1020
  void debugCheckFormals(const std::vector<Node>& formals, Node func);
1021
1022
  /**
1023
   * Checks whether formula is a valid function body for func whose formal
1024
   * argument list is stored in formals. This method is
1025
   * used as a helper function when defining (recursive) functions.
1026
   */
1027
  void debugCheckFunctionBody(Node formula,
1028
                              const std::vector<Node>& formals,
1029
                              Node func);
1030
1031
  /**
1032
   * Helper method to obtain both the heap and nil from the solver. Returns a
1033
   * std::pair where the first element is the heap expression and the second
1034
   * element is the nil expression.
1035
   */
1036
  std::pair<Node, Node> getSepHeapAndNilExpr();
1037
1038
  /* Members -------------------------------------------------------------- */
1039
1040
  /** Solver instance that owns this SmtEngine instance. */
1041
  api::Solver* d_solver = nullptr;
1042
1043
  /**
1044
   * The environment object, which contains all utilities that are globally
1045
   * available to internal code.
1046
   */
1047
  std::unique_ptr<Env> d_env;
1048
  /**
1049
   * The state of this SmtEngine, which is responsible for maintaining which
1050
   * SMT mode we are in, the contexts, the last result, etc.
1051
   */
1052
  std::unique_ptr<smt::SmtEngineState> d_state;
1053
1054
  /** Abstract values */
1055
  std::unique_ptr<smt::AbstractValues> d_absValues;
1056
  /** Assertions manager */
1057
  std::unique_ptr<smt::Assertions> d_asserts;
1058
  /** Resource out listener */
1059
  std::unique_ptr<smt::ResourceOutListener> d_routListener;
1060
  /** Node manager listener */
1061
  std::unique_ptr<smt::SmtNodeManagerListener> d_snmListener;
1062
1063
  /** The SMT solver */
1064
  std::unique_ptr<smt::SmtSolver> d_smtSolver;
1065
1066
  /**
1067
   * The SMT-level model object, which contains information about how to
1068
   * print the model, as well as a pointer to the underlying TheoryModel
1069
   * implementation maintained by the SmtSolver.
1070
   */
1071
  std::unique_ptr<smt::Model> d_model;
1072
1073
  /**
1074
   * The utility used for checking models
1075
   */
1076
  std::unique_ptr<smt::CheckModels> d_checkModels;
1077
1078
  /**
1079
   * The proof manager, which manages all things related to checking,
1080
   * processing, and printing proofs.
1081
   */
1082
  std::unique_ptr<smt::PfManager> d_pfManager;
1083
1084
  /**
1085
   * The unsat core manager, which produces unsat cores and related information
1086
   * from refutations. */
1087
  std::unique_ptr<smt::UnsatCoreManager> d_ucManager;
1088
1089
  /** The solver for sygus queries */
1090
  std::unique_ptr<smt::SygusSolver> d_sygusSolver;
1091
1092
  /** The solver for abduction queries */
1093
  std::unique_ptr<smt::AbductionSolver> d_abductSolver;
1094
  /** The solver for interpolation queries */
1095
  std::unique_ptr<smt::InterpolationSolver> d_interpolSolver;
1096
  /** The solver for quantifier elimination queries */
1097
  std::unique_ptr<smt::QuantElimSolver> d_quantElimSolver;
1098
1099
  /**
1100
   * The logic set by the user. The actual logic, which may extend the user's
1101
   * logic, lives in the Env class.
1102
   */
1103
  LogicInfo d_userLogic;
1104
1105
  /** Whether this is an internal subsolver. */
1106
  bool d_isInternalSubsolver;
1107
1108
  /**
1109
   * Verbosity of various commands.
1110
   */
1111
  std::map<std::string, int> d_commandVerbosity;
1112
1113
  /** The statistics class */
1114
  std::unique_ptr<smt::SmtEngineStatistics> d_stats;
1115
1116
  /** the output manager for commands */
1117
  mutable OutputManager d_outMgr;
1118
  /**
1119
   * The options manager, which is responsible for implementing core options
1120
   * such as those related to time outs and printing. It is also responsible
1121
   * for set default options based on the logic.
1122
   */
1123
  std::unique_ptr<smt::OptionsManager> d_optm;
1124
  /**
1125
   * The preprocessor.
1126
   */
1127
  std::unique_ptr<smt::Preprocessor> d_pp;
1128
  /**
1129
   * The global scope object. Upon creation of this SmtEngine, it becomes the
1130
   * SmtEngine in scope. It says the SmtEngine in scope until it is destructed,
1131
   * or another SmtEngine is created.
1132
   */
1133
  std::unique_ptr<smt::SmtScope> d_scope;
1134
}; /* class SmtEngine */
1135
1136
/* -------------------------------------------------------------------------- */
1137
1138
}  // namespace cvc5
1139
1140
#endif /* CVC5__SMT_ENGINE_H */