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

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