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