GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/smt/smt_engine.h Lines: 1 1 100.0 %
Date: 2021-08-03 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
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. Note this formula will
363
   * be included in the unsat core when applicable.
364
   *
365
   * @throw TypeCheckingException, LogicException, UnsafeInterruptException
366
   */
367
  Result assertFormula(const Node& formula);
368
369
  /**
370
   * Reduce an unsatisfiable core to make it minimal.
371
   */
372
  std::vector<Node> reduceUnsatCore(const std::vector<Node>& core);
373
374
  /**
375
   * Check if a given (set of) expression(s) is entailed with respect to the
376
   * current set of assertions. We check this by asserting the negation of
377
   * the (big AND over the) given (set of) expression(s).
378
   * Returns ENTAILED, NOT_ENTAILED, or ENTAILMENT_UNKNOWN result.
379
   *
380
   * @throw Exception
381
   */
382
  Result checkEntailed(const Node& assumption);
383
  Result checkEntailed(const std::vector<Node>& assumptions);
384
385
  /**
386
   * Assert a formula (if provided) to the current context and call
387
   * check().  Returns SAT, UNSAT, or SAT_UNKNOWN result.
388
   *
389
   * @throw Exception
390
   */
391
  Result checkSat();
392
  Result checkSat(const Node& assumption);
393
  Result checkSat(const std::vector<Node>& assumptions);
394
395
  /**
396
   * Returns a set of so-called "failed" assumptions.
397
   *
398
   * The returned set is a subset of the set of assumptions of a previous
399
   * (unsatisfiable) call to checkSatisfiability. Calling checkSatisfiability
400
   * with this set of failed assumptions still produces an unsat answer.
401
   *
402
   * Note that the returned set of failed assumptions is not necessarily
403
   * minimal.
404
   */
405
  std::vector<Node> getUnsatAssumptions(void);
406
407
  /*---------------------------- sygus commands  ---------------------------*/
408
409
  /**
410
   * Add sygus variable declaration.
411
   *
412
   * Declared SyGuS variables may be used in SyGuS constraints, in which they
413
   * are assumed to be universally quantified.
414
   *
415
   * In SyGuS semantics, declared functions are treated in the same manner as
416
   * declared variables, i.e. as universally quantified (function) variables
417
   * which can occur in the SyGuS constraints that compose the conjecture to
418
   * which a function is being synthesized. Thus declared functions should use
419
   * this method as well.
420
   */
421
  void declareSygusVar(Node var);
422
423
  /**
424
   * Add a function-to-synthesize declaration.
425
   *
426
   * The given sygusType may not correspond to the actual function type of func
427
   * but to a datatype encoding the syntax restrictions for the
428
   * function-to-synthesize. In this case this information is stored to be used
429
   * during solving.
430
   *
431
   * vars contains the arguments of the function-to-synthesize. These variables
432
   * are also stored to be used during solving.
433
   *
434
   * isInv determines whether the function-to-synthesize is actually an
435
   * invariant. This information is necessary if we are dumping a command
436
   * corresponding to this declaration, so that it can be properly printed.
437
   */
438
  void declareSynthFun(Node func,
439
                       TypeNode sygusType,
440
                       bool isInv,
441
                       const std::vector<Node>& vars);
442
  /**
443
   * Same as above, without a sygus type.
444
   */
445
  void declareSynthFun(Node func, bool isInv, const std::vector<Node>& vars);
446
447
  /** Add a regular sygus constraint.*/
448
  void assertSygusConstraint(Node constraint);
449
450
  /**
451
   * Add an invariant constraint.
452
   *
453
   * Invariant constraints are not explicitly declared: they are given in terms
454
   * of the invariant-to-synthesize, the pre condition, transition relation and
455
   * post condition. The actual constraint is built based on the inputs of these
456
   * place holder predicates :
457
   *
458
   * PRE(x) -> INV(x)
459
   * INV() ^ TRANS(x, x') -> INV(x')
460
   * INV(x) -> POST(x)
461
   *
462
   * The regular and primed variables are retrieved from the declaration of the
463
   * invariant-to-synthesize.
464
   */
465
  void assertSygusInvConstraint(Node inv, Node pre, Node trans, Node post);
466
  /**
467
   * Assert a synthesis conjecture to the current context and call
468
   * check().  Returns sat, unsat, or unknown result.
469
   *
470
   * The actual synthesis conjecture is built based on the previously
471
   * communicated information to this module (universal variables, defined
472
   * functions, functions-to-synthesize, and which constraints compose it). The
473
   * built conjecture is a higher-order formula of the form
474
   *
475
   * exists f1...fn . forall v1...vm . F
476
   *
477
   * in which f1...fn are the functions-to-synthesize, v1...vm are the declared
478
   * universal variables and F is the set of declared constraints.
479
   *
480
   * @throw Exception
481
   */
482
  Result checkSynth();
483
484
  /*------------------------- end of sygus commands ------------------------*/
485
486
  /**
487
   * Declare pool whose initial value is the terms in initValue. A pool is
488
   * a variable of type (Set T) that is used in quantifier annotations and does
489
   * not occur in constraints.
490
   *
491
   * @param p The pool to declare, which should be a variable of type (Set T)
492
   * for some type T.
493
   * @param initValue The initial value of p, which should be a vector of terms
494
   * of type T.
495
   */
496
  void declarePool(const Node& p, const std::vector<Node>& initValue);
497
  /**
498
   * Simplify a formula without doing "much" work.  Does not involve
499
   * the SAT Engine in the simplification, but uses the current
500
   * definitions, assertions, and the current partial model, if one
501
   * has been constructed.  It also involves theory normalization.
502
   *
503
   * @throw TypeCheckingException, LogicException, UnsafeInterruptException
504
   *
505
   * @todo (design) is this meant to give an equivalent or an
506
   * equisatisfiable formula?
507
   */
508
  Node simplify(const Node& e);
509
510
  /**
511
   * Expand the definitions in a term or formula.
512
   *
513
   * @param n The node to expand
514
   *
515
   * @throw TypeCheckingException, LogicException, UnsafeInterruptException
516
   */
517
  Node expandDefinitions(const Node& n);
518
519
  /**
520
   * Get the assigned value of an expr (only if immediately preceded by a SAT
521
   * or NOT_ENTAILED query).  Only permitted if the SmtEngine is set to operate
522
   * interactively and produce-models is on.
523
   *
524
   * @throw ModalException, TypeCheckingException, LogicException,
525
   *        UnsafeInterruptException
526
   */
527
  Node getValue(const Node& e) const;
528
529
  /**
530
   * Same as getValue but for a vector of expressions
531
   */
532
  std::vector<Node> getValues(const std::vector<Node>& exprs);
533
534
  /** print instantiations
535
   *
536
   * Print all instantiations for all quantified formulas on out,
537
   * returns true if at least one instantiation was printed. The type of output
538
   * (list, num, etc.) is determined by printInstMode.
539
   */
540
  void printInstantiations(std::ostream& out);
541
  /**
542
   * Print the current proof. This method should be called after an UNSAT
543
   * response. It gets the proof of false from the PropEngine and passes
544
   * it to the ProofManager, which post-processes the proof and prints it
545
   * in the proper format.
546
   */
547
  void printProof();
548
549
  /**
550
   * Get synth solution.
551
   *
552
   * This method returns true if we are in a state immediately preceded by
553
   * a successful call to checkSynth.
554
   *
555
   * This method adds entries to solMap that map functions-to-synthesize with
556
   * their solutions, for all active conjectures. This should be called
557
   * immediately after the solver answers unsat for sygus input.
558
   *
559
   * Specifically, given a sygus conjecture of the form
560
   *   exists x1...xn. forall y1...yn. P( x1...xn, y1...yn )
561
   * where x1...xn are second order bound variables, we map each xi to
562
   * lambda term in solMap such that
563
   *    forall y1...yn. P( solMap[x1]...solMap[xn], y1...yn )
564
   * is a valid formula.
565
   */
566
  bool getSynthSolutions(std::map<Node, Node>& solMap);
567
568
  /**
569
   * Do quantifier elimination.
570
   *
571
   * This function takes as input a quantified formula q
572
   * of the form:
573
   *   Q x1...xn. P( x1...xn, y1...yn )
574
   * where P( x1...xn, y1...yn ) is a quantifier-free
575
   * formula in a logic that supports quantifier elimination.
576
   * Currently, the only logics supported by quantifier
577
   * elimination is LRA and LIA.
578
   *
579
   * This function returns a formula ret such that, given
580
   * the current set of formulas A asserted to this SmtEngine :
581
   *
582
   * If doFull = true, then
583
   *   - ( A ^ q ) and ( A ^ ret ) are equivalent
584
   *   - ret is quantifier-free formula containing
585
   *     only free variables in y1...yn.
586
   *
587
   * If doFull = false, then
588
   *   - (A ^ q) => (A ^ ret) if Q is forall or
589
   *     (A ^ ret) => (A ^ q) if Q is exists,
590
   *   - ret is quantifier-free formula containing
591
   *     only free variables in y1...yn,
592
   *   - If Q is exists, let A^Q_n be the formula
593
   *       A ^ ~ret^Q_1 ^ ... ^ ~ret^Q_n
594
   *     where for each i=1,...n, formula ret^Q_i
595
   *     is the result of calling doQuantifierElimination
596
   *     for q with the set of assertions A^Q_{i-1}.
597
   *     Similarly, if Q is forall, then let A^Q_n be
598
   *       A ^ ret^Q_1 ^ ... ^ ret^Q_n
599
   *     where ret^Q_i is the same as above.
600
   *     In either case, we have that ret^Q_j will
601
   *     eventually be true or false, for some finite j.
602
   *
603
   * The former feature is quantifier elimination, and
604
   * is run on invocations of the smt2 extended command get-qe.
605
   * The latter feature is referred to as partial quantifier
606
   * elimination, and is run on invocations of the smt2
607
   * extended command get-qe-disjunct, which can be used
608
   * for incrementally computing the result of a
609
   * quantifier elimination.
610
   *
611
   * The argument strict is whether to output
612
   * warnings, such as when an unexpected logic is used.
613
   *
614
   * throw@ Exception
615
   */
616
  Node getQuantifierElimination(Node q, bool doFull, bool strict = true);
617
618
  /**
619
   * This method asks this SMT engine to find an interpolant with respect to
620
   * the current assertion stack (call it A) and the conjecture (call it B). If
621
   * this method returns true, then interpolant is set to a formula I such that
622
   * A ^ ~I and I ^ ~B are both unsatisfiable.
623
   *
624
   * The argument grammarType is a sygus datatype type that encodes the syntax
625
   * restrictions on the shapes of possible solutions.
626
   *
627
   * This method invokes a separate copy of the SMT engine for solving the
628
   * corresponding sygus problem for generating such a solution.
629
   */
630
  bool getInterpol(const Node& conj,
631
                   const TypeNode& grammarType,
632
                   Node& interpol);
633
634
  /** Same as above, but without user-provided grammar restrictions */
635
  bool getInterpol(const Node& conj, Node& interpol);
636
637
  /**
638
   * This method asks this SMT engine to find an abduct with respect to the
639
   * current assertion stack (call it A) and the conjecture (call it B).
640
   * If this method returns true, then abd is set to a formula C such that
641
   * A ^ C is satisfiable, and A ^ ~B ^ C is unsatisfiable.
642
   *
643
   * The argument grammarType is a sygus datatype type that encodes the syntax
644
   * restrictions on the shape of possible solutions.
645
   *
646
   * This method invokes a separate copy of the SMT engine for solving the
647
   * corresponding sygus problem for generating such a solution.
648
   */
649
  bool getAbduct(const Node& conj, const TypeNode& grammarType, Node& abd);
650
651
  /** Same as above, but without user-provided grammar restrictions */
652
  bool getAbduct(const Node& conj, Node& abd);
653
654
  /**
655
   * Get list of quantified formulas that were instantiated on the last call
656
   * to check-sat.
657
   */
658
  void getInstantiatedQuantifiedFormulas(std::vector<Node>& qs);
659
660
  /**
661
   * Get instantiation term vectors for quantified formula q.
662
   *
663
   * This method is similar to above, but in the example above, we return the
664
   * (vectors of) terms t1, ..., tn instead.
665
   *
666
   * Notice that these are not guaranteed to come in the same order as the
667
   * instantiation lemmas above.
668
   */
669
  void getInstantiationTermVectors(Node q,
670
                                   std::vector<std::vector<Node>>& tvecs);
671
  /**
672
   * As above but only the instantiations that were relevant for the
673
   * refutation.
674
   */
675
  void getRelevantInstantiationTermVectors(
676
      std::map<Node, std::vector<std::vector<Node>>>& insts);
677
  /**
678
   * Get instantiation term vectors, which maps each instantiated quantified
679
   * formula to the list of instantiations for that quantified formula. This
680
   * list is minimized if proofs are enabled, and this call is immediately
681
   * preceded by an UNSAT or ENTAILED query
682
   */
683
  void getInstantiationTermVectors(
684
      std::map<Node, std::vector<std::vector<Node>>>& insts);
685
686
  /**
687
   * Get an unsatisfiable core (only if immediately preceded by an UNSAT or
688
   * ENTAILED query).  Only permitted if cvc5 was built with unsat-core support
689
   * and produce-unsat-cores is on.
690
   */
691
  UnsatCore getUnsatCore();
692
693
  /**
694
   * Get a refutation proof (only if immediately preceded by an UNSAT or
695
   * ENTAILED query). Only permitted if cvc5 was built with proof support and
696
   * the proof option is on. */
697
  std::string getProof();
698
699
  /**
700
   * Get the current set of assertions.  Only permitted if the
701
   * SmtEngine is set to operate interactively.
702
   */
703
  std::vector<Node> getAssertions();
704
705
  /**
706
   * Push a user-level context.
707
   * throw@ ModalException, LogicException, UnsafeInterruptException
708
   */
709
  void push();
710
711
  /**
712
   * Pop a user-level context.  Throws an exception if nothing to pop.
713
   * @throw ModalException
714
   */
715
  void pop();
716
717
  /** Reset all assertions, global declarations, etc.  */
718
  void resetAssertions();
719
720
  /**
721
   * Interrupt a running query.  This can be called from another thread
722
   * or from a signal handler.  Throws a ModalException if the SmtEngine
723
   * isn't currently in a query.
724
   *
725
   * @throw ModalException
726
   */
727
  void interrupt();
728
729
  /**
730
   * Set a resource limit for SmtEngine operations.  This is like a time
731
   * limit, but it's deterministic so that reproducible results can be
732
   * obtained.  Currently, it's based on the number of conflicts.
733
   * However, please note that the definition may change between different
734
   * versions of cvc5 (as may the number of conflicts required, anyway),
735
   * and it might even be different between instances of the same version
736
   * of cvc5 on different platforms.
737
   *
738
   * A cumulative and non-cumulative (per-call) resource limit can be
739
   * set at the same time.  A call to setResourceLimit() with
740
   * cumulative==true replaces any cumulative resource limit currently
741
   * in effect; a call with cumulative==false replaces any per-call
742
   * resource limit currently in effect.  Time limits can be set in
743
   * addition to resource limits; the SmtEngine obeys both.  That means
744
   * that up to four independent limits can control the SmtEngine
745
   * at the same time.
746
   *
747
   * When an SmtEngine is first created, it has no time or resource
748
   * limits.
749
   *
750
   * Currently, these limits only cause the SmtEngine to stop what its
751
   * doing when the limit expires (or very shortly thereafter); no
752
   * heuristics are altered by the limits or the threat of them expiring.
753
   * We reserve the right to change this in the future.
754
   *
755
   * @param units the resource limit, or 0 for no limit
756
   * @param cumulative whether this resource limit is to be a cumulative
757
   * resource limit for all remaining calls into the SmtEngine (true), or
758
   * whether it's a per-call resource limit (false); the default is false
759
   */
760
  void setResourceLimit(uint64_t units, bool cumulative = false);
761
762
  /**
763
   * Set a per-call time limit for SmtEngine operations.
764
   *
765
   * A per-call time limit can be set at the same time and replaces
766
   * any per-call time limit currently in effect.
767
   * Resource limits (either per-call or cumulative) can be set in
768
   * addition to a time limit; the SmtEngine obeys all three of them.
769
   *
770
   * Note that the per-call timer only ticks away when one of the
771
   * SmtEngine's workhorse functions (things like assertFormula(),
772
   * checkEntailed(), checkSat(), and simplify()) are running.
773
   * Between calls, the timer is still.
774
   *
775
   * When an SmtEngine is first created, it has no time or resource
776
   * limits.
777
   *
778
   * Currently, these limits only cause the SmtEngine to stop what its
779
   * doing when the limit expires (or very shortly thereafter); no
780
   * heuristics are altered by the limits or the threat of them expiring.
781
   * We reserve the right to change this in the future.
782
   *
783
   * @param millis the time limit in milliseconds, or 0 for no limit
784
   */
785
  void setTimeLimit(uint64_t millis);
786
787
  /**
788
   * Get the current resource usage count for this SmtEngine.  This
789
   * function can be used to ascertain reasonable values to pass as
790
   * resource limits to setResourceLimit().
791
   */
792
  unsigned long getResourceUsage() const;
793
794
  /** Get the current millisecond count for this SmtEngine.  */
795
  unsigned long getTimeUsage() const;
796
797
  /**
798
   * Get the remaining resources that can be consumed by this SmtEngine
799
   * according to the currently-set cumulative resource limit.  If there
800
   * is not a cumulative resource limit set, this function throws a
801
   * ModalException.
802
   *
803
   * @throw ModalException
804
   */
805
  unsigned long getResourceRemaining() const;
806
807
  /** Permit access to the underlying NodeManager. */
808
  NodeManager* getNodeManager() const;
809
810
  /**
811
   * Print statistics from the statistics registry in the env object owned by
812
   * this SmtEngine. Safe to use in a signal handler.
813
   */
814
  void printStatisticsSafe(int fd) const;
815
816
  /**
817
   * Print the changes to the statistics from the statistics registry in the
818
   * env object owned by this SmtEngine since this method was called the last
819
   * time. Internally prints the diff and then stores a snapshot for the next
820
   * call.
821
   */
822
  void printStatisticsDiff() const;
823
824
  /**
825
   * Set user attribute.
826
   * This function is called when an attribute is set by a user.
827
   * In SMT-LIBv2 this is done via the syntax (! expr :attr)
828
   */
829
  void setUserAttribute(const std::string& attr,
830
                        Node expr,
831
                        const std::vector<Node>& expr_values,
832
                        const std::string& str_value);
833
834
  /** Get the options object (const and non-const versions) */
835
  Options& getOptions();
836
  const Options& getOptions() const;
837
838
  /** Get a pointer to the UserContext owned by this SmtEngine. */
839
  context::UserContext* getUserContext();
840
841
  /** Get a pointer to the Context owned by this SmtEngine. */
842
  context::Context* getContext();
843
844
  /** Get a pointer to the TheoryEngine owned by this SmtEngine. */
845
  TheoryEngine* getTheoryEngine();
846
847
  /** Get a pointer to the PropEngine owned by this SmtEngine. */
848
  prop::PropEngine* getPropEngine();
849
850
  /** Get the resource manager of this SMT engine */
851
  ResourceManager* getResourceManager() const;
852
853
  /** Permit access to the underlying dump manager. */
854
  smt::DumpManager* getDumpManager();
855
856
  /** Get the printer used by this SMT engine */
857
  const Printer& getPrinter() const;
858
859
  /** Get the output manager for this SMT engine */
860
  OutputManager& getOutputManager();
861
862
  /** Get a pointer to the Rewriter owned by this SmtEngine. */
863
  theory::Rewriter* getRewriter();
864
  /**
865
   * Get expanded assertions.
866
   *
867
   * Return the set of assertions, after expanding definitions.
868
   */
869
  std::vector<Node> getExpandedAssertions();
870
871
  /**
872
   * !!!!! temporary, until the environment is passsed to all classes that
873
   * require it.
874
   */
875
  Env& getEnv();
876
  /* .......................................................................  */
877
 private:
878
  /* .......................................................................  */
879
880
  // disallow copy/assignment
881
  SmtEngine(const SmtEngine&) = delete;
882
  SmtEngine& operator=(const SmtEngine&) = delete;
883
884
  /** Set solver instance that owns this SmtEngine. */
885
6864
  void setSolver(api::Solver* solver) { d_solver = solver; }
886
887
  /** Get a pointer to the (new) PfManager owned by this SmtEngine. */
888
  smt::PfManager* getPfManager() { return d_pfManager.get(); };
889
890
  /** Get a pointer to the StatisticsRegistry owned by this SmtEngine. */
891
  StatisticsRegistry& getStatisticsRegistry();
892
893
  /**
894
   * Internal method to get an unsatisfiable core (only if immediately preceded
895
   * by an UNSAT or ENTAILED query). Only permitted if cvc5 was built with
896
   * unsat-core support and produce-unsat-cores is on. Does not dump the
897
   * command.
898
   */
899
  UnsatCore getUnsatCoreInternal();
900
901
  /**
902
   * Check that a generated proof checks. This method is the same as printProof,
903
   * but does not print the proof. Like that method, it should be called
904
   * after an UNSAT response. It ensures that a well-formed proof of false
905
   * can be constructed by the combination of the PropEngine and ProofManager.
906
   */
907
  void checkProof();
908
909
  /**
910
   * Check that an unsatisfiable core is indeed unsatisfiable.
911
   */
912
  void checkUnsatCore();
913
914
  /**
915
   * Check that a generated Model (via getModel()) actually satisfies
916
   * all user assertions.
917
   */
918
  void checkModel(bool hardFailure = true);
919
920
  /**
921
   * Check that a solution to an interpolation problem is indeed a solution.
922
   *
923
   * The check is made by determining that the assertions imply the solution of
924
   * the interpolation problem (interpol), and the solution implies the goal
925
   * (conj). If these criteria are not met, an internal error is thrown.
926
   */
927
  void checkInterpol(Node interpol,
928
                     const std::vector<Node>& easserts,
929
                     const Node& conj);
930
931
  /**
932
   * This is called by the destructor, just before destroying the
933
   * PropEngine, TheoryEngine, and DecisionEngine (in that order).  It
934
   * is important because there are destruction ordering issues
935
   * between PropEngine and Theory.
936
   */
937
  void shutdown();
938
939
  /**
940
   * Quick check of consistency in current context: calls
941
   * processAssertionList() then look for inconsistency (based only on
942
   * that).
943
   */
944
  Result quickCheck();
945
946
  /**
947
   * Get the (SMT-level) model pointer, if we are in SAT mode. Otherwise,
948
   * return nullptr.
949
   *
950
   * This ensures that the underlying theory model of the SmtSolver maintained
951
   * by this class is currently available, which means that cvc5 is producing
952
   * models, and is in "SAT mode", otherwise a recoverable exception is thrown.
953
   *
954
   * @param c used for giving an error message to indicate the context
955
   * this method was called.
956
   */
957
  smt::Model* getAvailableModel(const char* c) const;
958
  /**
959
   * Get available quantifiers engine, which throws a modal exception if it
960
   * does not exist. This can happen if a quantifiers-specific call (e.g.
961
   * getInstantiatedQuantifiedFormulas) is called in a non-quantified logic.
962
   *
963
   * @param c used for giving an error message to indicate the context
964
   * this method was called.
965
   */
966
  theory::QuantifiersEngine* getAvailableQuantifiersEngine(const char* c) const;
967
968
  // --------------------------------------- callbacks from the state
969
  /**
970
   * Notify push pre, which is called just before the user context of the state
971
   * pushes. This processes all pending assertions.
972
   */
973
  void notifyPushPre();
974
  /**
975
   * Notify push post, which is called just after the user context of the state
976
   * pushes. This performs a push on the underlying prop engine.
977
   */
978
  void notifyPushPost();
979
  /**
980
   * Notify pop pre, which is called just before the user context of the state
981
   * pops. This performs a pop on the underlying prop engine.
982
   */
983
  void notifyPopPre();
984
  /**
985
   * Notify post solve pre, which is called once per check-sat query. It
986
   * is triggered when the first d_state.doPendingPops() is issued after the
987
   * check-sat. This method is called before the contexts pop in the method
988
   * doPendingPops.
989
   */
990
  void notifyPostSolvePre();
991
  /**
992
   * Same as above, but after contexts are popped. This calls the postsolve
993
   * method of the underlying TheoryEngine.
994
   */
995
  void notifyPostSolvePost();
996
  // --------------------------------------- end callbacks from the state
997
998
  /**
999
   * Internally handle the setting of a logic.  This function should always
1000
   * be called when d_logic is updated.
1001
   */
1002
  void setLogicInternal();
1003
1004
  /*
1005
   * Check satisfiability (used to check satisfiability and entailment).
1006
   */
1007
  Result checkSatInternal(const std::vector<Node>& assumptions,
1008
                          bool isEntailmentCheck);
1009
1010
  /**
1011
   * Check that all Expr in formals are of BOUND_VARIABLE kind, where func is
1012
   * the function that the formal argument list is for. This method is used
1013
   * as a helper function when defining (recursive) functions.
1014
   */
1015
  void debugCheckFormals(const std::vector<Node>& formals, Node func);
1016
1017
  /**
1018
   * Checks whether formula is a valid function body for func whose formal
1019
   * argument list is stored in formals. This method is
1020
   * used as a helper function when defining (recursive) functions.
1021
   */
1022
  void debugCheckFunctionBody(Node formula,
1023
                              const std::vector<Node>& formals,
1024
                              Node func);
1025
1026
  /**
1027
   * Helper method to obtain both the heap and nil from the solver. Returns a
1028
   * std::pair where the first element is the heap expression and the second
1029
   * element is the nil expression.
1030
   */
1031
  std::pair<Node, Node> getSepHeapAndNilExpr();
1032
1033
  /* Members -------------------------------------------------------------- */
1034
1035
  /** Solver instance that owns this SmtEngine instance. */
1036
  api::Solver* d_solver = nullptr;
1037
1038
  /**
1039
   * The environment object, which contains all utilities that are globally
1040
   * available to internal code.
1041
   */
1042
  std::unique_ptr<Env> d_env;
1043
  /**
1044
   * The state of this SmtEngine, which is responsible for maintaining which
1045
   * SMT mode we are in, the contexts, the last result, etc.
1046
   */
1047
  std::unique_ptr<smt::SmtEngineState> d_state;
1048
1049
  /** Abstract values */
1050
  std::unique_ptr<smt::AbstractValues> d_absValues;
1051
  /** Assertions manager */
1052
  std::unique_ptr<smt::Assertions> d_asserts;
1053
  /** Resource out listener */
1054
  std::unique_ptr<smt::ResourceOutListener> d_routListener;
1055
  /** Node manager listener */
1056
  std::unique_ptr<smt::SmtNodeManagerListener> d_snmListener;
1057
1058
  /** The SMT solver */
1059
  std::unique_ptr<smt::SmtSolver> d_smtSolver;
1060
1061
  /**
1062
   * The SMT-level model object, which contains information about how to
1063
   * print the model, as well as a pointer to the underlying TheoryModel
1064
   * implementation maintained by the SmtSolver.
1065
   */
1066
  std::unique_ptr<smt::Model> d_model;
1067
1068
  /**
1069
   * The utility used for checking models
1070
   */
1071
  std::unique_ptr<smt::CheckModels> d_checkModels;
1072
1073
  /**
1074
   * The proof manager, which manages all things related to checking,
1075
   * processing, and printing proofs.
1076
   */
1077
  std::unique_ptr<smt::PfManager> d_pfManager;
1078
1079
  /**
1080
   * The unsat core manager, which produces unsat cores and related information
1081
   * from refutations. */
1082
  std::unique_ptr<smt::UnsatCoreManager> d_ucManager;
1083
1084
  /** The solver for sygus queries */
1085
  std::unique_ptr<smt::SygusSolver> d_sygusSolver;
1086
1087
  /** The solver for abduction queries */
1088
  std::unique_ptr<smt::AbductionSolver> d_abductSolver;
1089
  /** The solver for interpolation queries */
1090
  std::unique_ptr<smt::InterpolationSolver> d_interpolSolver;
1091
  /** The solver for quantifier elimination queries */
1092
  std::unique_ptr<smt::QuantElimSolver> d_quantElimSolver;
1093
1094
  /**
1095
   * The logic set by the user. The actual logic, which may extend the user's
1096
   * logic, lives in the Env class.
1097
   */
1098
  LogicInfo d_userLogic;
1099
1100
  /** Whether this is an internal subsolver. */
1101
  bool d_isInternalSubsolver;
1102
1103
  /**
1104
   * Verbosity of various commands.
1105
   */
1106
  std::map<std::string, int> d_commandVerbosity;
1107
1108
  /** The statistics class */
1109
  std::unique_ptr<smt::SmtEngineStatistics> d_stats;
1110
1111
  /** the output manager for commands */
1112
  mutable OutputManager d_outMgr;
1113
  /**
1114
   * The options manager, which is responsible for implementing core options
1115
   * such as those related to time outs and printing. It is also responsible
1116
   * for set default options based on the logic.
1117
   */
1118
  std::unique_ptr<smt::OptionsManager> d_optm;
1119
  /**
1120
   * The preprocessor.
1121
   */
1122
  std::unique_ptr<smt::Preprocessor> d_pp;
1123
  /**
1124
   * The global scope object. Upon creation of this SmtEngine, it becomes the
1125
   * SmtEngine in scope. It says the SmtEngine in scope until it is destructed,
1126
   * or another SmtEngine is created.
1127
   */
1128
  std::unique_ptr<smt::SmtScope> d_scope;
1129
}; /* class SmtEngine */
1130
1131
/* -------------------------------------------------------------------------- */
1132
1133
}  // namespace cvc5
1134
1135
#endif /* CVC5__SMT_ENGINE_H */