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

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