GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/theory.h Lines: 49 69 71.0 %
Date: 2021-11-07 Branches: 54 114 47.4 %

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