GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/theory.h Lines: 70 93 75.3 %
Date: 2021-08-19 Branches: 70 170 41.2 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Morgan Deters, Dejan Jovanovic
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
 * Base of the theory interface.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__THEORY__THEORY_H
19
#define CVC5__THEORY__THEORY_H
20
21
#include <iosfwd>
22
#include <set>
23
#include <string>
24
#include <unordered_set>
25
26
#include "context/cdlist.h"
27
#include "context/cdo.h"
28
#include "context/context.h"
29
#include "expr/node.h"
30
#include "options/theory_options.h"
31
#include "proof/trust_node.h"
32
#include "smt/env.h"
33
#include "theory/assertion.h"
34
#include "theory/care_graph.h"
35
#include "theory/logic_info.h"
36
#include "theory/skolem_lemma.h"
37
#include "theory/theory_id.h"
38
#include "theory/valuation.h"
39
#include "util/statistics_stats.h"
40
41
namespace cvc5 {
42
43
class ProofNodeManager;
44
class TheoryEngine;
45
class ProofRuleChecker;
46
47
namespace theory {
48
49
class DecisionManager;
50
struct EeSetupInfo;
51
class OutputChannel;
52
class QuantifiersEngine;
53
class TheoryInferenceManager;
54
class TheoryModel;
55
class TheoryRewriter;
56
class TheoryState;
57
class TrustSubstitutionMap;
58
59
namespace eq {
60
  class EqualityEngine;
61
  }  // namespace eq
62
63
/**
64
 * Base class for T-solvers.  Abstract DPLL(T).
65
 *
66
 * This is essentially an interface class.  The TheoryEngine has
67
 * pointers to Theory.  Note that only one specific Theory type (e.g.,
68
 * TheoryUF) can exist per NodeManager, because of how the
69
 * RegisteredAttr works.  (If you need multiple instances of the same
70
 * theory, you'll have to write a multiplexed theory that dispatches
71
 * all calls to them.)
72
 *
73
 * NOTE: A Theory has a special way of being initialized. The owner of a Theory
74
 * is either:
75
 *
76
 * (A) Using Theory as a standalone object, not associated with a TheoryEngine.
77
 * In this case, simply call the public initialization method
78
 * (Theory::finishInitStandalone).
79
 *
80
 * (B) TheoryEngine, which determines how the Theory acts in accordance with
81
 * its theory combination policy. We require the following steps in order:
82
 * (B.1) Get information about whether the theory wishes to use an equality
83
 * eninge, and more specifically which equality engine notifications the Theory
84
 * would like to be notified of (Theory::needsEqualityEngine).
85
 * (B.2) Set the equality engine of the theory (Theory::setEqualityEngine),
86
 * which we refer to as the "official equality engine" of this Theory. The
87
 * equality engine passed to the theory must respect the contract(s) specified
88
 * by the equality engine setup information (EeSetupInfo) returned in the
89
 * previous step.
90
 * (B.3) Set the other required utilities including setQuantifiersEngine and
91
 * setDecisionManager.
92
 * (B.4) Call the private initialization method (Theory::finishInit).
93
 *
94
 * Initialization of the second form happens during TheoryEngine::finishInit,
95
 * after the quantifiers engine and model objects have been set up.
96
 */
97
class Theory {
98
  friend class ::cvc5::TheoryEngine;
99
100
 private:
101
  // Disallow default construction, copy, assignment.
102
  Theory() = delete;
103
  Theory(const Theory&) = delete;
104
  Theory& operator=(const Theory&) = delete;
105
106
  /** An integer identifying the type of the theory. */
107
  TheoryId d_id;
108
109
  /** The environment class */
110
  Env& d_env;
111
112
  /**
113
   * The assertFact() queue.
114
   *
115
   * These can not be TNodes as some atoms (such as equalities) are sent
116
   * across theories without being stored in a global map.
117
   */
118
  context::CDList<Assertion> d_facts;
119
120
  /** Index into the head of the facts list */
121
  context::CDO<unsigned> d_factsHead;
122
123
  /** Indices for splitting on the shared terms. */
124
  context::CDO<unsigned> d_sharedTermsIndex;
125
126
  /** The care graph the theory will use during combination. */
127
  CareGraph* d_careGraph;
128
129
  /** Pointer to the decision manager. */
130
  DecisionManager* d_decManager;
131
132
 protected:
133
  /** Name of this theory instance. Along with the TheoryId this should provide
134
   * an unique string identifier for each instance of a Theory class. We need
135
   * this to ensure unique statistics names over multiple theory instances. */
136
  std::string d_instanceName;
137
138
  // === STATISTICS ===
139
  /** time spent in check calls */
140
  TimerStat d_checkTime;
141
  /** time spent in theory combination */
142
  TimerStat d_computeCareGraphTime;
143
144
  /**
145
   * The only method to add suff to the care graph.
146
   */
147
  void addCarePair(TNode t1, TNode t2);
148
149
  /**
150
   * The function should compute the care graph over the shared terms.
151
   * The default function returns all the pairs among the shared variables.
152
   */
153
  virtual void computeCareGraph();
154
155
  /**
156
   * A list of shared terms that the theory has.
157
   */
158
  context::CDList<TNode> d_sharedTerms;
159
160
  /**
161
   * Construct a Theory.
162
   *
163
   * The pair <id, instance> is assumed to uniquely identify this Theory
164
   * w.r.t. the SmtEngine.
165
   */
166
  Theory(TheoryId id,
167
         Env& env,
168
         OutputChannel& out,
169
         Valuation valuation,
170
         std::string instance = "");  // taking : No default.
171
172
  /**
173
   * This is called at shutdown time by the TheoryEngine, just before
174
   * destruction.  It is important because there are destruction
175
   * ordering issues between PropEngine and Theory (based on what
176
   * hard-links to Nodes are outstanding).  As the fact queue might be
177
   * nonempty, we ensure here that it's clear.  If you overload this,
178
   * you must make an explicit call here to this->Theory::shutdown()
179
   * too.
180
   */
181
78814
  virtual void shutdown() { }
182
183
  /**
184
   * The output channel for the Theory.
185
   */
186
  OutputChannel* d_out;
187
188
  /**
189
   * The valuation proxy for the Theory to communicate back with the
190
   * theory engine (and other theories).
191
   */
192
  Valuation d_valuation;
193
  /**
194
   * Pointer to the official equality engine of this theory, which is owned by
195
   * the equality engine manager of TheoryEngine.
196
   */
197
  eq::EqualityEngine* d_equalityEngine;
198
  /**
199
   * The official equality engine, if we allocated it.
200
   */
201
  std::unique_ptr<eq::EqualityEngine> d_allocEqualityEngine;
202
  /**
203
   * The theory state, which contains contexts, valuation, and equality engine.
204
   * Notice the theory is responsible for memory management of this class.
205
   */
206
  TheoryState* d_theoryState;
207
  /**
208
   * The theory inference manager. This is a wrapper around the equality
209
   * engine and the output channel. It ensures that the output channel and
210
   * the equality engine are used properly.
211
   */
212
  TheoryInferenceManager* d_inferManager;
213
214
  /**
215
   * Pointer to the quantifiers engine (or NULL, if quantifiers are not
216
   * supported or not enabled). Not owned by the theory.
217
   */
218
  QuantifiersEngine* d_quantEngine;
219
220
  /** Pointer to proof node manager */
221
  ProofNodeManager* d_pnm;
222
  /**
223
   * Are proofs enabled?
224
   *
225
   * They are considered enabled if the ProofNodeManager is non-null.
226
   */
227
  bool proofsEnabled() const;
228
229
  /**
230
   * Returns the next assertion in the assertFact() queue.
231
   *
232
   * @return the next assertion in the assertFact() queue
233
   */
234
  inline Assertion get();
235
236
221076
  const LogicInfo& getLogicInfo() const { return d_env.getLogicInfo(); }
237
238
  /**
239
   * Set separation logic heap. This is called when the location and data
240
   * types for separation logic are determined. This should be called at
241
   * most once, before solving.
242
   *
243
   * This currently should be overridden by the separation logic theory only.
244
   */
245
1444
  virtual void declareSepHeap(TypeNode locT, TypeNode dataT) {}
246
247
  /**
248
   * The theory that owns the uninterpreted sort.
249
   */
250
  static TheoryId s_uninterpretedSortOwner;
251
252
  void printFacts(std::ostream& os) const;
253
  void debugPrintFacts() const;
254
255
  /** is legal elimination
256
   *
257
   * Returns true if x -> val is a legal elimination of variable x. This is
258
   * useful for ppAssert, when x = val is an entailed equality. This function
259
   * determines whether indeed x can be eliminated from the problem via the
260
   * substituion x -> val.
261
   *
262
   * The following criteria imply that x -> val is *not* a legal elimination:
263
   * (1) If x is contained in val,
264
   * (2) If the type of val is not a subtype of the type of x,
265
   * (3) If val contains an operator that cannot be evaluated, and produceModels
266
   * is true. For example, x -> sqrt(2) is not a legal elimination if we
267
   * are producing models. This is because we care about the value of x, and
268
   * its value must be computed (approximated) by the non-linear solver.
269
   */
270
  bool isLegalElimination(TNode x, TNode val);
271
  //--------------------------------- private initialization
272
  /**
273
   * Called to set the official equality engine. This should be done by
274
   * TheoryEngine only.
275
   */
276
  void setEqualityEngine(eq::EqualityEngine* ee);
277
  /** Called to set the quantifiers engine. */
278
  void setQuantifiersEngine(QuantifiersEngine* qe);
279
  /** Called to set the decision manager. */
280
  void setDecisionManager(DecisionManager* dm);
281
  /**
282
   * Finish theory initialization.  At this point, options and the logic
283
   * setting are final, the master equality engine and quantifiers
284
   * engine (if any) are initialized, and the official equality engine of this
285
   * theory has been assigned.  This base class implementation
286
   * does nothing. This should be called by TheoryEngine only.
287
   */
288
9854
  virtual void finishInit() {}
289
  //--------------------------------- end private initialization
290
291
  /**
292
   * This method is called to notify a theory that the node n should
293
   * be considered a "shared term" by this theory. This does anything
294
   * theory-specific concerning the fact that n is now marked as a shared
295
   * term, which is done in addition to explicitly storing n as a shared
296
   * term and adding it as a trigger term in the equality engine of this
297
   * class (see addSharedTerm).
298
   */
299
  virtual void notifySharedTerm(TNode n);
300
  /**
301
   * Notify in conflict, called when a conflict clause is added to TheoryEngine
302
   * by any theory (not necessarily this one). This signals that the theory
303
   * should suspend what it is currently doing and wait for backtracking.
304
   */
305
  virtual void notifyInConflict();
306
307
 public:
308
  //--------------------------------- initialization
309
  /**
310
   * @return The theory rewriter associated with this theory.
311
   */
312
  virtual TheoryRewriter* getTheoryRewriter() = 0;
313
  /**
314
   * @return The proof checker associated with this theory.
315
   */
316
  virtual ProofRuleChecker* getProofChecker() = 0;
317
  /**
318
   * Returns true if this theory needs an equality engine for checking
319
   * satisfiability.
320
   *
321
   * If this method returns true, then the equality engine manager will
322
   * initialize its equality engine field via setEqualityEngine above during
323
   * TheoryEngine::finishInit, prior to calling finishInit for this theory.
324
   *
325
   * Additionally, if this method returns true, then this method is required to
326
   * update the argument esi with instructions for initializing and setting up
327
   * notifications from its equality engine, which is commonly done with
328
   * a notifications class (eq::EqualityEngineNotify).
329
   */
330
  virtual bool needsEqualityEngine(EeSetupInfo& esi);
331
  /**
332
   * Finish theory initialization, standalone version. This is used to
333
   * initialize this class if it is not associated with a theory engine.
334
   * This allocates the official equality engine of this Theory and then
335
   * calls the finishInit method above.
336
   */
337
  void finishInitStandalone();
338
  //--------------------------------- end initialization
339
340
  /**
341
   * Return the ID of the theory responsible for the given type.
342
   */
343
52464056
  static inline TheoryId theoryOf(TypeNode typeNode) {
344
52464056
    Trace("theory::internal") << "theoryOf(" << typeNode << ")" << std::endl;
345
    TheoryId id;
346
52464056
    if (typeNode.getKind() == kind::TYPE_CONSTANT) {
347
37807745
      id = typeConstantToTheoryId(typeNode.getConst<TypeConstant>());
348
    } else {
349
14656311
      id = kindToTheoryId(typeNode.getKind());
350
    }
351
52464056
    if (id == THEORY_BUILTIN) {
352
2562910
      Trace("theory::internal") << "theoryOf(" << typeNode << ") == " << s_uninterpretedSortOwner << std::endl;
353
2562910
      return s_uninterpretedSortOwner;
354
    }
355
49901146
    return id;
356
  }
357
358
  /**
359
   * Returns the ID of the theory responsible for the given node.
360
   */
361
  static TheoryId theoryOf(options::TheoryOfMode mode, TNode node);
362
363
  /**
364
   * Returns the ID of the theory responsible for the given node.
365
   */
366
168702684
  static inline TheoryId theoryOf(TNode node) {
367
168702684
    return theoryOf(options::theoryOfMode(), node);
368
  }
369
370
  /**
371
   * Set the owner of the uninterpreted sort.
372
   */
373
9854
  static void setUninterpretedSortOwner(TheoryId theory) {
374
9854
    s_uninterpretedSortOwner = theory;
375
9854
  }
376
377
  /**
378
   * Get the owner of the uninterpreted sort.
379
   */
380
  static TheoryId getUninterpretedSortOwner() {
381
    return s_uninterpretedSortOwner;
382
  }
383
384
  /**
385
   * Checks if the node is a leaf node of this theory
386
   */
387
379487
  inline bool isLeaf(TNode node) const {
388
379487
    return node.getNumChildren() == 0 || theoryOf(node) != d_id;
389
  }
390
391
  /**
392
   * Checks if the node is a leaf node of a theory.
393
   */
394
200601819
  inline static bool isLeafOf(TNode node, TheoryId theoryId) {
395
200601819
    return node.getNumChildren() == 0 || theoryOf(node) != theoryId;
396
  }
397
398
  /** Returns true if the assertFact queue is empty*/
399
48858409
  bool done() const { return d_factsHead == d_facts.size(); }
400
  /**
401
   * Destructs a Theory.
402
   */
403
  virtual ~Theory();
404
405
  /**
406
   * Subclasses of Theory may add additional efforts.  DO NOT CHECK
407
   * equality with one of these values (e.g. if STANDARD xxx) but
408
   * rather use range checks (or use the helper functions below).
409
   * Normally we call QUICK_CHECK or STANDARD; at the leaves we call
410
   * with FULL_EFFORT.
411
   */
412
  enum Effort
413
  {
414
    /**
415
     * Standard effort where theory need not do anything
416
     */
417
    EFFORT_STANDARD = 50,
418
    /**
419
     * Full effort requires the theory make sure its assertions are satisfiable
420
     * or not
421
     */
422
    EFFORT_FULL = 100,
423
    /**
424
     * Last call effort, called after theory combination has completed with
425
     * no lemmas and a model is available.
426
     */
427
    EFFORT_LAST_CALL = 200
428
  }; /* enum Effort */
429
430
4
  static inline bool standardEffortOrMore(Effort e) CVC5_CONST_FUNCTION
431
  {
432
4
    return e >= EFFORT_STANDARD; }
433
4
  static inline bool standardEffortOnly(Effort e) CVC5_CONST_FUNCTION
434
  {
435
4
    return e >= EFFORT_STANDARD && e < EFFORT_FULL; }
436
32575909
  static inline bool fullEffort(Effort e) CVC5_CONST_FUNCTION
437
  {
438
32575909
    return e == EFFORT_FULL; }
439
440
  /**
441
   * Get the id for this Theory.
442
   */
443
17992721
  TheoryId getId() const {
444
17992721
    return d_id;
445
  }
446
447
  /**
448
   * Get a reference to the environment.
449
   */
450
  Env& getEnv() const { return d_env; }
451
452
  /**
453
   * Get the SAT context associated to this Theory.
454
   */
455
18866878
  context::Context* getSatContext() const { return d_env.getContext(); }
456
457
  /**
458
   * Get the context associated to this Theory.
459
   */
460
330780
  context::UserContext* getUserContext() const {
461
330780
    return d_env.getUserContext();
462
  }
463
464
  /**
465
   * Get the output channel associated to this theory.
466
   */
467
123944
  OutputChannel& getOutputChannel() {
468
123944
    return *d_out;
469
  }
470
471
  /**
472
   * Get the valuation associated to this theory.
473
   */
474
35978
  Valuation& getValuation() {
475
35978
    return d_valuation;
476
  }
477
478
  /** Get the equality engine being used by this theory. */
479
  eq::EqualityEngine* getEqualityEngine();
480
481
  /**
482
   * Get the quantifiers engine associated to this theory.
483
   */
484
269197
  QuantifiersEngine* getQuantifiersEngine() {
485
269197
    return d_quantEngine;
486
  }
487
488
  /**
489
   * @return The theory state associated with this theory.
490
   */
491
  TheoryState* getTheoryState() { return d_theoryState; }
492
493
  /**
494
   * @return The theory inference manager associated with this theory.
495
   */
496
9854
  TheoryInferenceManager* getInferenceManager() { return d_inferManager; }
497
498
  /**
499
   * Pre-register a term.  Done one time for a Node per SAT context level.
500
   */
501
  virtual void preRegisterTerm(TNode);
502
503
  /**
504
   * Assert a fact in the current context.
505
   */
506
14443924
  void assertFact(TNode assertion, bool isPreregistered) {
507
28887848
    Trace("theory") << "Theory<" << getId() << ">::assertFact["
508
28887848
                    << getSatContext()->getLevel() << "](" << assertion << ", "
509
14443924
                    << (isPreregistered ? "true" : "false") << ")" << std::endl;
510
14443924
    d_facts.push_back(Assertion(assertion, isPreregistered));
511
14443924
  }
512
513
  /** Add shared term to the theory. */
514
  void addSharedTerm(TNode node);
515
516
  /**
517
   * Return the current theory care graph. Theories should overload
518
   * computeCareGraph to do the actual computation, and use addCarePair to add
519
   * pairs to the care graph.
520
   */
521
  void getCareGraph(CareGraph* careGraph);
522
523
  /**
524
   * Return the status of two terms in the current context. Should be
525
   * implemented in sub-theories to enable more efficient theory-combination.
526
   */
527
  virtual EqualityStatus getEqualityStatus(TNode a, TNode b);
528
529
  /**
530
   * Return the model value of the give shared term (or null if not available).
531
   *
532
   * TODO (project #39): this method is likely to become deprecated.
533
   */
534
1638
  virtual Node getModelValue(TNode var) { return Node::null(); }
535
536
  /** T-propagate new literal assignments in the current context. */
537
  virtual void propagate(Effort level = EFFORT_FULL) {}
538
539
  /**
540
   * Return an explanation for the literal represented by parameter n
541
   * (which was previously propagated by this theory).
542
   */
543
  virtual TrustNode explain(TNode n)
544
  {
545
    Unimplemented() << "Theory " << identify()
546
                    << " propagated a node but doesn't implement the "
547
                       "Theory::explain() interface!";
548
    return TrustNode::null();
549
  }
550
551
  //--------------------------------- check
552
  /**
553
   * Does this theory wish to be called to check at last call effort? This is
554
   * the case for any theory that wishes to run when a model is available.
555
   */
556
60744
  virtual bool needsCheckLastEffort() { return false; }
557
  /**
558
   * Check the current assignment's consistency.
559
   *
560
   * An implementation of check() is required to either:
561
   * - return a conflict on the output channel,
562
   * - be interrupted,
563
   * - throw an exception
564
   * - or call get() until done() is true.
565
   *
566
   * The standard method for check consists of a loop that processes the entire
567
   * fact queue when preCheck returns false. It makes four theory-specific
568
   * callbacks, (preCheck, postCheck, preNotifyFact, notifyFact) as described
569
   * below. It asserts each fact to the official equality engine when
570
   * preNotifyFact returns false.
571
   *
572
   * Theories that use this check method must use an official theory
573
   * state object (d_theoryState).
574
   */
575
  void check(Effort level = EFFORT_FULL);
576
  /**
577
   * Pre-check, called before the fact queue of the theory is processed.
578
   * If this method returns false, then the theory will process its fact
579
   * queue. If this method returns true, then the theory has indicated
580
   * its check method should finish immediately.
581
   */
582
  virtual bool preCheck(Effort level = EFFORT_FULL);
583
  /**
584
   * Post-check, called after the fact queue of the theory is processed.
585
   */
586
  virtual void postCheck(Effort level = EFFORT_FULL);
587
  /**
588
   * Pre-notify fact, return true if the theory processed it. If this
589
   * method returns false, then the atom will be added to the equality engine
590
   * of the theory and notifyFact will be called with isInternal=false.
591
   *
592
   * Theories that implement check but do not use official equality
593
   * engines should always return true for this method.
594
   *
595
   * @param atom The atom
596
   * @param polarity Its polarity
597
   * @param fact The original literal that was asserted
598
   * @param isPrereg Whether the assertion is preregistered
599
   * @param isInternal Whether the origin of the fact was internal. If this
600
   * is false, the fact was asserted via the fact queue of the theory.
601
   * @return true if the theory completely processed this fact, i.e. it does
602
   * not need to assert the fact to its equality engine.
603
   */
604
  virtual bool preNotifyFact(
605
      TNode atom, bool pol, TNode fact, bool isPrereg, bool isInternal);
606
  /**
607
   * Notify fact, called immediately after the fact was pushed into the
608
   * equality engine.
609
   *
610
   * @param atom The atom
611
   * @param polarity Its polarity
612
   * @param fact The original literal that was asserted.
613
   * @param isInternal Whether the origin of the fact was internal. If this
614
   * is false, the fact was asserted via the fact queue of the theory.
615
   */
616
  virtual void notifyFact(TNode atom, bool pol, TNode fact, bool isInternal);
617
  //--------------------------------- end check
618
619
  //--------------------------------- collect model info
620
  /**
621
   * Get all relevant information in this theory regarding the current
622
   * model.  This should be called after a call to check( FULL_EFFORT )
623
   * for all theories with no conflicts and no lemmas added.
624
   *
625
   * This method returns true if and only if the equality engine of m is
626
   * consistent as a result of this call.
627
   *
628
   * The standard method for collectModelInfo computes the relevant terms,
629
   * asserts the theory's equality engine to the model (if necessary) and
630
   * then calls computeModelValues.
631
   *
632
   * TODO (project #39): this method should be non-virtual, once all theories
633
   * conform to the new standard, delete, move to model manager distributed.
634
   */
635
  virtual bool collectModelInfo(TheoryModel* m, const std::set<Node>& termSet);
636
  /**
637
   * Compute terms that are not necessarily part of the assertions or
638
   * shared terms that should be considered relevant, add them to termSet.
639
   */
640
  virtual void computeRelevantTerms(std::set<Node>& termSet);
641
  /**
642
   * Collect asserted terms for this theory and add them to  termSet.
643
   *
644
   * @param termSet The set to add terms to
645
   * @param includeShared Whether to include the shared terms of the theory
646
   */
647
  void collectAssertedTerms(std::set<Node>& termSet,
648
                            bool includeShared = true) const;
649
  /**
650
   * Helper function for collectAssertedTerms, adds all subterms
651
   * belonging to this theory to termSet.
652
   */
653
  void collectTerms(TNode n, std::set<Node>& termSet) const;
654
  /**
655
   * Collect model values, after equality information is added to the model.
656
   * The argument termSet is the set of relevant terms returned by
657
   * computeRelevantTerms.
658
   */
659
  virtual bool collectModelValues(TheoryModel* m,
660
                                  const std::set<Node>& termSet);
661
  /** if theories want to do something with model after building, do it here */
662
7320
  virtual void postProcessModel( TheoryModel* m ){ }
663
  //--------------------------------- end collect model info
664
665
  //--------------------------------- preprocessing
666
  /**
667
   * Statically learn from assertion "in," which has been asserted
668
   * true at the top level.  The theory should only add (via
669
   * ::operator<< or ::append()) to the "learned" builder---it should
670
   * *never* clear it.  It is a conjunction to add to the formula at
671
   * the top-level and may contain other theories' contributions.
672
   */
673
  virtual void ppStaticLearn(TNode in, NodeBuilder& learned) {}
674
675
  enum PPAssertStatus {
676
    /** Atom has been solved  */
677
    PP_ASSERT_STATUS_SOLVED,
678
    /** Atom has not been solved */
679
    PP_ASSERT_STATUS_UNSOLVED,
680
    /** Atom is inconsistent */
681
    PP_ASSERT_STATUS_CONFLICT
682
  };
683
684
  /**
685
   * Given a literal and its proof generator (encapsulated by trust node tin),
686
   * add the solved substitutions to the map, if any. The method should return
687
   * true if the literal can be safely removed from the input problem.
688
   *
689
   * Note that tin has trust node kind LEMMA. Its proof generator should be
690
   * taken into account when adding a substitution to outSubstitutions when
691
   * proofs are enabled.
692
   */
693
  virtual PPAssertStatus ppAssert(TrustNode tin,
694
                                  TrustSubstitutionMap& outSubstitutions);
695
696
  /**
697
   * Given a term of the theory coming from the input formula or
698
   * from a lemma generated during solving, this method can be overridden in a
699
   * theory implementation to rewrite the term into an equivalent form.
700
   *
701
   * This method returns a TrustNode of kind TrustNodeKind::REWRITE, which
702
   * carries information about the proof generator for the rewrite, which can
703
   * be the null TrustNode if n is unchanged.
704
   *
705
   * Notice this method is used both in the "theory rewrite equalities"
706
   * preprocessing pass, where n is an equality from the input formula,
707
   * and in theory preprocessing, where n is a (non-equality) term occurring
708
   * in the input or generated in a lemma.
709
   *
710
   * @param n the node to preprocess-rewrite.
711
   * @param lems a set of lemmas that should be added as a consequence of
712
   * preprocessing n. These are in the form of "skolem lemmas". For example,
713
   * calling this method on (div x n), we return a trust node proving:
714
   *   (= (div x n) k_div)
715
   * for fresh skolem k, and add the skolem lemma for k that indicates that
716
   * it is the division of x and n.
717
   *
718
   * Note that ppRewrite should not return WITNESS terms, since the internal
719
   * calculus works in "original forms" and not "witness forms".
720
   */
721
118052
  virtual TrustNode ppRewrite(TNode n, std::vector<SkolemLemma>& lems)
722
  {
723
118052
    return TrustNode::null();
724
  }
725
726
  /**
727
   * Notify preprocessed assertions. Called on new assertions after
728
   * preprocessing before they are asserted to theory engine.
729
   */
730
151151
  virtual void ppNotifyAssertions(const std::vector<Node>& assertions) {}
731
  //--------------------------------- end preprocessing
732
733
  /**
734
   * A Theory is called with presolve exactly one time per user
735
   * check-sat.  presolve() is called after preregistration,
736
   * rewriting, and Boolean propagation, (other theories'
737
   * propagation?), but the notified Theory has not yet had its
738
   * check() or propagate() method called.  A Theory may empty its
739
   * assertFact() queue using get().  A Theory can raise conflicts,
740
   * add lemmas, and propagate literals during presolve().
741
   *
742
   * NOTE: The presolve property must be added to the kinds file for
743
   * the theory.
744
   */
745
  virtual void presolve() { }
746
747
  /**
748
   * A Theory is called with postsolve exactly one time per user
749
   * check-sat.  postsolve() is called after the query has completed
750
   * (regardless of whether sat, unsat, or unknown), and after any
751
   * model-querying related to the query has been performed.
752
   * After this call, the theory will not get another check() or
753
   * propagate() call until presolve() is called again.  A Theory
754
   * cannot raise conflicts, add lemmas, or propagate literals during
755
   * postsolve().
756
   */
757
  virtual void postsolve() { }
758
759
  /**
760
   * Notification sent to the theory wheneven the search restarts.
761
   * Serves as a good time to do some clean-up work, and you can
762
   * assume you're at DL 0 for the purposes of Contexts.  This function
763
   * should not use the output channel.
764
   */
765
  virtual void notifyRestart() { }
766
767
  /**
768
   * Identify this theory (for debugging, dynamic configuration,
769
   * etc..)
770
   */
771
  virtual std::string identify() const = 0;
772
773
  /** Set user attribute
774
    * This function is called when an attribute is set by a user.  In SMT-LIBv2 this is done
775
    *  via the syntax (! n :attr)
776
    */
777
  virtual void setUserAttribute(const std::string& attr, Node n, std::vector<Node> node_values, std::string str_value) {
778
    Unimplemented() << "Theory " << identify()
779
                    << " doesn't support Theory::setUserAttribute interface";
780
  }
781
782
  typedef context::CDList<Assertion>::const_iterator assertions_iterator;
783
784
  /**
785
   * Provides access to the facts queue, primarily intended for theory
786
   * debugging purposes.
787
   *
788
   * @return the iterator to the beginning of the fact queue
789
   */
790
129059
  assertions_iterator facts_begin() const {
791
129059
    return d_facts.begin();
792
  }
793
794
  /**
795
   * Provides access to the facts queue, primarily intended for theory
796
   * debugging purposes.
797
   *
798
   * @return the iterator to the end of the fact queue
799
   */
800
721107
  assertions_iterator facts_end() const {
801
721107
    return d_facts.end();
802
  }
803
  /**
804
   * Whether facts have been asserted to this theory.
805
   *
806
   * @return true iff facts have been asserted to this theory.
807
   */
808
5930
  bool hasFacts() { return !d_facts.empty(); }
809
810
  /** Return total number of facts asserted to this theory */
811
2987
  size_t numAssertions() {
812
2987
    return d_facts.size();
813
  }
814
815
  typedef context::CDList<TNode>::const_iterator shared_terms_iterator;
816
817
  /**
818
   * Provides access to the shared terms, primarily intended for theory
819
   * debugging purposes.
820
   *
821
   * @return the iterator to the beginning of the shared terms list
822
   */
823
119520
  shared_terms_iterator shared_terms_begin() const {
824
119520
    return d_sharedTerms.begin();
825
  }
826
827
  /**
828
   * Provides access to the facts queue, primarily intended for theory
829
   * debugging purposes.
830
   *
831
   * @return the iterator to the end of the shared terms list
832
   */
833
132115
  shared_terms_iterator shared_terms_end() const {
834
132115
    return d_sharedTerms.end();
835
  }
836
837
838
  /**
839
   * This is a utility function for constructing a copy of the currently shared terms
840
   * in a queriable form.  As this is
841
   */
842
  std::unordered_set<TNode> currentlySharedTerms() const;
843
844
  /**
845
   * This allows the theory to be queried for whether a literal, lit, is
846
   * entailed by the theory.  This returns a pair of a Boolean and a node E.
847
   *
848
   * If the Boolean is true, then E is a formula that entails lit and E is propositionally
849
   * entailed by the assertions to the theory.
850
   *
851
   * If the Boolean is false, it is "unknown" if lit is entailed and E may be
852
   * any node.
853
   *
854
   * The literal lit is either an atom a or (not a), which must belong to the theory:
855
   *   There is some TheoryOfMode m s.t. Theory::theoryOf(m, a) == this->getId().
856
   *
857
   * There are NO assumptions that a or the subterms of a have been
858
   * preprocessed in any form.  This includes ppRewrite, rewriting,
859
   * preregistering, registering, definition expansion or ITE removal!
860
   *
861
   * Theories are free to limit the amount of effort they use and so may
862
   * always opt to return "unknown".  Both "unknown" and "not entailed",
863
   * may return for E a non-boolean Node (e.g. Node::null()).  (There is no explicit output
864
   * for the negation of lit is entailed.)
865
   *
866
   * If lit is theory valid, the return result may be the Boolean constant
867
   * true for E.
868
   *
869
   * If lit is entailed by multiple assertions on the theory's getFact()
870
   * queue, a_1, a_2, ... and a_k, this may return E=(and a_1 a_2 ... a_k) or
871
   * another theory entailed explanation E=(and (and a_1 a_2) (and a3 a_4) ... a_k)
872
   *
873
   * If lit is entailed by a single assertion on the theory's getFact()
874
   * queue, say a, this may return E=a.
875
   *
876
   * The theory may always return false!
877
   *
878
   * Theories may not touch their output stream during an entailment check.
879
   *
880
   * @param  lit     a literal belonging to the theory.
881
   * @return         a pair <b,E> s.t. if b is true, then a formula E such that
882
   * E |= lit in the theory.
883
   */
884
  virtual std::pair<bool, Node> entailmentCheck(TNode lit);
885
886
  /** Return true if this theory uses central equality engine */
887
  bool usesCentralEqualityEngine() const;
888
  /** uses central equality engine (static) */
889
  static bool usesCentralEqualityEngine(TheoryId id);
890
  /** Explains/propagates via central equality engine only */
891
  static bool expUsingCentralEqualityEngine(TheoryId id);
892
};/* class Theory */
893
894
std::ostream& operator<<(std::ostream& os, theory::Theory::Effort level);
895
896
897
14103521
inline theory::Assertion Theory::get() {
898
14103521
  Assert(!done()) << "Theory::get() called with assertion queue empty!";
899
900
  // Get the assertion
901
14103521
  Assertion fact = d_facts[d_factsHead];
902
14103521
  d_factsHead = d_factsHead + 1;
903
904
14103521
  Trace("theory") << "Theory::get() => " << fact << " (" << d_facts.size() - d_factsHead << " left)" << std::endl;
905
906
14103521
  return fact;
907
}
908
909
inline std::ostream& operator<<(std::ostream& out,
910
                                const cvc5::theory::Theory& theory)
911
{
912
  return out << theory.identify();
913
}
914
915
inline std::ostream& operator << (std::ostream& out, theory::Theory::PPAssertStatus status) {
916
  switch (status) {
917
  case theory::Theory::PP_ASSERT_STATUS_SOLVED:
918
    out << "SOLVE_STATUS_SOLVED"; break;
919
  case theory::Theory::PP_ASSERT_STATUS_UNSOLVED:
920
    out << "SOLVE_STATUS_UNSOLVED"; break;
921
  case theory::Theory::PP_ASSERT_STATUS_CONFLICT:
922
    out << "SOLVE_STATUS_CONFLICT"; break;
923
  default:
924
    Unhandled();
925
  }
926
  return out;
927
}
928
929
}  // namespace theory
930
}  // namespace cvc5
931
932
#endif /* CVC5__THEORY__THEORY_H */