GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/theory.h Lines: 60 80 75.0 %
Date: 2021-09-09 Branches: 70 158 44.3 %

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
 private:
103
  // Disallow default construction, copy, assignment.
104
  Theory() = delete;
105
  Theory(const Theory&) = delete;
106
  Theory& operator=(const Theory&) = delete;
107
108
  /** An integer identifying the type of the theory. */
109
  TheoryId d_id;
110
111
  /**
112
   * The assertFact() queue.
113
   *
114
   * These can not be TNodes as some atoms (such as equalities) are sent
115
   * across theories without being stored in a global map.
116
   */
117
  context::CDList<Assertion> d_facts;
118
119
  /** Index into the head of the facts list */
120
  context::CDO<unsigned> d_factsHead;
121
122
  /** Indices for splitting on the shared terms. */
123
  context::CDO<unsigned> d_sharedTermsIndex;
124
125
  /** The care graph the theory will use during combination. */
126
  CareGraph* d_careGraph;
127
128
  /** Pointer to the decision manager. */
129
  DecisionManager* d_decManager;
130
131
 protected:
132
  /** Name of this theory instance. Along with the TheoryId this should
133
   * provide an unique string identifier for each instance of a Theory class.
134
   * We need this to ensure unique statistics names over multiple theory
135
   * 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
79382
  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
204
   * engine. Notice the theory is responsible for memory management of this
205
   * class.
206
   */
207
  TheoryState* d_theoryState;
208
  /**
209
   * The theory inference manager. This is a wrapper around the equality
210
   * engine and the output channel. It ensures that the output channel and
211
   * the equality engine are used properly.
212
   */
213
  TheoryInferenceManager* d_inferManager;
214
215
  /**
216
   * Pointer to the quantifiers engine (or NULL, if quantifiers are not
217
   * supported or not enabled). Not owned by the theory.
218
   */
219
  QuantifiersEngine* d_quantEngine;
220
221
  /** Pointer to proof node manager */
222
  ProofNodeManager* d_pnm;
223
  /**
224
   * Are proofs enabled?
225
   *
226
   * They are considered enabled if the ProofNodeManager is non-null.
227
   */
228
  bool proofsEnabled() const;
229
230
  /**
231
   * Returns the next assertion in the assertFact() queue.
232
   *
233
   * @return the next assertion in the assertFact() queue
234
   */
235
  inline Assertion get();
236
237
  /**
238
   * Set separation logic heap. This is called when the location and data
239
   * types for separation logic are determined. This should be called at
240
   * most once, before solving.
241
   *
242
   * This currently should be overridden by the separation logic theory only.
243
   */
244
1444
  virtual void declareSepHeap(TypeNode locT, TypeNode dataT) {}
245
246
  /**
247
   * The theory that owns the uninterpreted sort.
248
   */
249
  static TheoryId s_uninterpretedSortOwner;
250
251
  void printFacts(std::ostream& os) const;
252
  void debugPrintFacts() const;
253
254
  /** is legal elimination
255
   *
256
   * Returns true if x -> val is a legal elimination of variable x. This is
257
   * useful for ppAssert, when x = val is an entailed equality. This function
258
   * determines whether indeed x can be eliminated from the problem via the
259
   * substituion x -> val.
260
   *
261
   * The following criteria imply that x -> val is *not* a legal elimination:
262
   * (1) If x is contained in val,
263
   * (2) If the type of val is not a subtype of the type of x,
264
   * (3) If val contains an operator that cannot be evaluated, and
265
   * produceModels is true. For example, x -> sqrt(2) is not a legal
266
   * elimination if we are producing models. This is because we care about the
267
   * value of x, and its value must be computed (approximated) by the
268
   * 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
9928
  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
302
   * TheoryEngine by any theory (not necessarily this one). This signals that
303
   * the theory should suspend what it is currently doing and wait for
304
   * backtracking.
305
   */
306
  virtual void notifyInConflict();
307
308
 public:
309
  //--------------------------------- initialization
310
  /**
311
   * @return The theory rewriter associated with this theory.
312
   */
313
  virtual TheoryRewriter* getTheoryRewriter() = 0;
314
  /**
315
   * @return The proof checker associated with this theory.
316
   */
317
  virtual ProofRuleChecker* getProofChecker() = 0;
318
  /**
319
   * Returns true if this theory needs an equality engine for checking
320
   * satisfiability.
321
   *
322
   * If this method returns true, then the equality engine manager will
323
   * initialize its equality engine field via setEqualityEngine above during
324
   * TheoryEngine::finishInit, prior to calling finishInit for this theory.
325
   *
326
   * Additionally, if this method returns true, then this method is required
327
   * to update the argument esi with instructions for initializing and setting
328
   * up notifications from its equality engine, which is commonly done with a
329
   * notifications class (eq::EqualityEngineNotify).
330
   */
331
  virtual bool needsEqualityEngine(EeSetupInfo& esi);
332
  /**
333
   * Finish theory initialization, standalone version. This is used to
334
   * initialize this class if it is not associated with a theory engine.
335
   * This allocates the official equality engine of this Theory and then
336
   * calls the finishInit method above.
337
   */
338
  void finishInitStandalone();
339
  //--------------------------------- end initialization
340
341
  /**
342
   * Return the ID of the theory responsible for the given type.
343
   */
344
55585514
  static inline TheoryId theoryOf(TypeNode typeNode)
345
  {
346
55585514
    Trace("theory::internal") << "theoryOf(" << typeNode << ")" << std::endl;
347
    TheoryId id;
348
55585514
    if (typeNode.getKind() == kind::TYPE_CONSTANT)
349
    {
350
40855083
      id = typeConstantToTheoryId(typeNode.getConst<TypeConstant>());
351
    }
352
    else
353
    {
354
14730431
      id = kindToTheoryId(typeNode.getKind());
355
    }
356
55585514
    if (id == THEORY_BUILTIN)
357
    {
358
5168190
      Trace("theory::internal")
359
2584095
          << "theoryOf(" << typeNode << ") == " << s_uninterpretedSortOwner
360
2584095
          << std::endl;
361
2584095
      return s_uninterpretedSortOwner;
362
    }
363
53001419
    return id;
364
  }
365
366
  /**
367
   * Returns the ID of the theory responsible for the given node.
368
   */
369
  static TheoryId theoryOf(options::TheoryOfMode mode, TNode node);
370
371
  /**
372
   * Returns the ID of the theory responsible for the given node.
373
   */
374
186350081
  static inline TheoryId theoryOf(TNode node)
375
  {
376
186350081
    return theoryOf(options::theoryOfMode(), node);
377
  }
378
379
  /**
380
   * Set the owner of the uninterpreted sort.
381
   */
382
9928
  static void setUninterpretedSortOwner(TheoryId theory)
383
  {
384
9928
    s_uninterpretedSortOwner = theory;
385
9928
  }
386
387
  /**
388
   * Get the owner of the uninterpreted sort.
389
   */
390
  static TheoryId getUninterpretedSortOwner()
391
  {
392
    return s_uninterpretedSortOwner;
393
  }
394
395
  /**
396
   * Checks if the node is a leaf node of this theory
397
   */
398
392107
  inline bool isLeaf(TNode node) const
399
  {
400
392107
    return node.getNumChildren() == 0 || theoryOf(node) != d_id;
401
  }
402
403
  /**
404
   * Checks if the node is a leaf node of a theory.
405
   */
406
219341627
  inline static bool isLeafOf(TNode node, TheoryId theoryId)
407
  {
408
219341627
    return node.getNumChildren() == 0 || theoryOf(node) != theoryId;
409
  }
410
411
  /** Returns true if the assertFact queue is empty*/
412
51709847
  bool done() const { return d_factsHead == d_facts.size(); }
413
  /**
414
   * Destructs a Theory.
415
   */
416
  virtual ~Theory();
417
418
  /**
419
   * Subclasses of Theory may add additional efforts.  DO NOT CHECK
420
   * equality with one of these values (e.g. if STANDARD xxx) but
421
   * rather use range checks (or use the helper functions below).
422
   * Normally we call QUICK_CHECK or STANDARD; at the leaves we call
423
   * with FULL_EFFORT.
424
   */
425
  enum Effort
426
  {
427
    /**
428
     * Standard effort where theory need not do anything
429
     */
430
    EFFORT_STANDARD = 50,
431
    /**
432
     * Full effort requires the theory make sure its assertions are
433
     * satisfiable or not
434
     */
435
    EFFORT_FULL = 100,
436
    /**
437
     * Last call effort, called after theory combination has completed with
438
     * no lemmas and a model is available.
439
     */
440
    EFFORT_LAST_CALL = 200
441
  }; /* enum Effort */
442
443
4
  static inline bool standardEffortOrMore(Effort e) CVC5_CONST_FUNCTION
444
  {
445
4
    return e >= EFFORT_STANDARD;
446
  }
447
4
  static inline bool standardEffortOnly(Effort e) CVC5_CONST_FUNCTION
448
  {
449
4
    return e >= EFFORT_STANDARD && e < EFFORT_FULL;
450
  }
451
35406536
  static inline bool fullEffort(Effort e) CVC5_CONST_FUNCTION
452
  {
453
35406536
    return e == EFFORT_FULL;
454
  }
455
456
  /**
457
   * Get the id for this Theory.
458
   */
459
19052126
  TheoryId getId() const { return d_id; }
460
461
  /**
462
   * Get the output channel associated to this theory.
463
   */
464
124832
  OutputChannel& getOutputChannel() { return *d_out; }
465
466
  /**
467
   * Get the valuation associated to this theory.
468
   */
469
39003
  Valuation& getValuation() { return d_valuation; }
470
471
  /** Get the equality engine being used by this theory. */
472
  eq::EqualityEngine* getEqualityEngine();
473
474
  /**
475
   * Get the quantifiers engine associated to this theory.
476
   */
477
272979
  QuantifiersEngine* getQuantifiersEngine() { return d_quantEngine; }
478
479
  /**
480
   * @return The theory state associated with this theory.
481
   */
482
  TheoryState* getTheoryState() { return d_theoryState; }
483
484
  /**
485
   * @return The theory inference manager associated with this theory.
486
   */
487
9928
  TheoryInferenceManager* getInferenceManager() { return d_inferManager; }
488
489
  /**
490
   * Pre-register a term.  Done one time for a Node per SAT context level.
491
   */
492
  virtual void preRegisterTerm(TNode);
493
494
  /**
495
   * Assert a fact in the current context.
496
   */
497
15201351
  void assertFact(TNode assertion, bool isPreregistered)
498
  {
499
30402702
    Trace("theory") << "Theory<" << getId() << ">::assertFact["
500
30402702
                    << context()->getLevel() << "](" << assertion << ", "
501
15201351
                    << (isPreregistered ? "true" : "false") << ")" << std::endl;
502
15201351
    d_facts.push_back(Assertion(assertion, isPreregistered));
503
15201351
  }
504
505
  /** Add shared term to the theory. */
506
  void addSharedTerm(TNode node);
507
508
  /**
509
   * Return the current theory care graph. Theories should overload
510
   * computeCareGraph to do the actual computation, and use addCarePair to add
511
   * pairs to the care graph.
512
   */
513
  void getCareGraph(CareGraph* careGraph);
514
515
  /**
516
   * Return the status of two terms in the current context. Should be
517
   * implemented in sub-theories to enable more efficient theory-combination.
518
   */
519
  virtual EqualityStatus getEqualityStatus(TNode a, TNode b);
520
521
  /**
522
   * Return the model value of the give shared term (or null if not
523
   * available).
524
   *
525
   * TODO (project #39): this method is likely to become deprecated.
526
   */
527
1638
  virtual Node getModelValue(TNode var) { return Node::null(); }
528
529
  /** T-propagate new literal assignments in the current context. */
530
  virtual void propagate(Effort level = EFFORT_FULL) {}
531
532
  /**
533
   * Return an explanation for the literal represented by parameter n
534
   * (which was previously propagated by this theory).
535
   */
536
  virtual TrustNode explain(TNode n)
537
  {
538
    Unimplemented() << "Theory " << identify()
539
                    << " propagated a node but doesn't implement the "
540
                       "Theory::explain() interface!";
541
    return TrustNode::null();
542
  }
543
544
  //--------------------------------- check
545
  /**
546
   * Does this theory wish to be called to check at last call effort? This is
547
   * the case for any theory that wishes to run when a model is available.
548
   */
549
61035
  virtual bool needsCheckLastEffort() { return false; }
550
  /**
551
   * Check the current assignment's consistency.
552
   *
553
   * An implementation of check() is required to either:
554
   * - return a conflict on the output channel,
555
   * - be interrupted,
556
   * - throw an exception
557
   * - or call get() until done() is true.
558
   *
559
   * The standard method for check consists of a loop that processes the
560
   * entire fact queue when preCheck returns false. It makes four
561
   * theory-specific callbacks, (preCheck, postCheck, preNotifyFact,
562
   * notifyFact) as described below. It asserts each fact to the official
563
   * equality engine when preNotifyFact returns false.
564
   *
565
   * Theories that use this check method must use an official theory
566
   * state object (d_theoryState).
567
   */
568
  void check(Effort level = EFFORT_FULL);
569
  /**
570
   * Pre-check, called before the fact queue of the theory is processed.
571
   * If this method returns false, then the theory will process its fact
572
   * queue. If this method returns true, then the theory has indicated
573
   * its check method should finish immediately.
574
   */
575
  virtual bool preCheck(Effort level = EFFORT_FULL);
576
  /**
577
   * Post-check, called after the fact queue of the theory is processed.
578
   */
579
  virtual void postCheck(Effort level = EFFORT_FULL);
580
  /**
581
   * Pre-notify fact, return true if the theory processed it. If this
582
   * method returns false, then the atom will be added to the equality engine
583
   * of the theory and notifyFact will be called with isInternal=false.
584
   *
585
   * Theories that implement check but do not use official equality
586
   * engines should always return true for this method.
587
   *
588
   * @param atom The atom
589
   * @param polarity Its polarity
590
   * @param fact The original literal that was asserted
591
   * @param isPrereg Whether the assertion is preregistered
592
   * @param isInternal Whether the origin of the fact was internal. If this
593
   * is false, the fact was asserted via the fact queue of the theory.
594
   * @return true if the theory completely processed this fact, i.e. it does
595
   * not need to assert the fact to its equality engine.
596
   */
597
  virtual bool preNotifyFact(
598
      TNode atom, bool pol, TNode fact, bool isPrereg, bool isInternal);
599
  /**
600
   * Notify fact, called immediately after the fact was pushed into the
601
   * equality engine.
602
   *
603
   * @param atom The atom
604
   * @param polarity Its polarity
605
   * @param fact The original literal that was asserted.
606
   * @param isInternal Whether the origin of the fact was internal. If this
607
   * is false, the fact was asserted via the fact queue of the theory.
608
   */
609
  virtual void notifyFact(TNode atom, bool pol, TNode fact, bool isInternal);
610
  //--------------------------------- end check
611
612
  //--------------------------------- collect model info
613
  /**
614
   * Get all relevant information in this theory regarding the current
615
   * model.  This should be called after a call to check( FULL_EFFORT )
616
   * for all theories with no conflicts and no lemmas added.
617
   *
618
   * This method returns true if and only if the equality engine of m is
619
   * consistent as a result of this call.
620
   *
621
   * The standard method for collectModelInfo computes the relevant terms,
622
   * asserts the theory's equality engine to the model (if necessary) and
623
   * then calls computeModelValues.
624
   *
625
   * TODO (project #39): this method should be non-virtual, once all theories
626
   * conform to the new standard, delete, move to model manager distributed.
627
   */
628
  virtual bool collectModelInfo(TheoryModel* m, const std::set<Node>& termSet);
629
  /**
630
   * Compute terms that are not necessarily part of the assertions or
631
   * shared terms that should be considered relevant, add them to termSet.
632
   */
633
  virtual void computeRelevantTerms(std::set<Node>& termSet);
634
  /**
635
   * Collect asserted terms for this theory and add them to  termSet.
636
   *
637
   * @param termSet The set to add terms to
638
   * @param includeShared Whether to include the shared terms of the theory
639
   */
640
  void collectAssertedTerms(std::set<Node>& termSet,
641
                            bool includeShared = true) const;
642
  /**
643
   * Helper function for collectAssertedTerms, adds all subterms
644
   * belonging to this theory to termSet.
645
   */
646
  void collectTerms(TNode n, std::set<Node>& termSet) const;
647
  /**
648
   * Collect model values, after equality information is added to the model.
649
   * The argument termSet is the set of relevant terms returned by
650
   * computeRelevantTerms.
651
   */
652
  virtual bool collectModelValues(TheoryModel* m,
653
                                  const std::set<Node>& termSet);
654
  /** if theories want to do something with model after building, do it here
655
   */
656
7536
  virtual void postProcessModel(TheoryModel* m) {}
657
  //--------------------------------- end collect model info
658
659
  //--------------------------------- preprocessing
660
  /**
661
   * Statically learn from assertion "in," which has been asserted
662
   * true at the top level.  The theory should only add (via
663
   * ::operator<< or ::append()) to the "learned" builder---it should
664
   * *never* clear it.  It is a conjunction to add to the formula at
665
   * the top-level and may contain other theories' contributions.
666
   */
667
  virtual void ppStaticLearn(TNode in, NodeBuilder& learned) {}
668
669
  enum PPAssertStatus
670
  {
671
    /** Atom has been solved  */
672
    PP_ASSERT_STATUS_SOLVED,
673
    /** Atom has not been solved */
674
    PP_ASSERT_STATUS_UNSOLVED,
675
    /** Atom is inconsistent */
676
    PP_ASSERT_STATUS_CONFLICT
677
  };
678
679
  /**
680
   * Given a literal and its proof generator (encapsulated by trust node tin),
681
   * add the solved substitutions to the map, if any. The method should return
682
   * true if the literal can be safely removed from the input problem.
683
   *
684
   * Note that tin has trust node kind LEMMA. Its proof generator should be
685
   * taken into account when adding a substitution to outSubstitutions when
686
   * proofs are enabled.
687
   */
688
  virtual PPAssertStatus ppAssert(TrustNode tin,
689
                                  TrustSubstitutionMap& outSubstitutions);
690
691
  /**
692
   * Given a term of the theory coming from the input formula or
693
   * from a lemma generated during solving, this method can be overridden in a
694
   * theory implementation to rewrite the term into an equivalent form.
695
   *
696
   * This method returns a TrustNode of kind TrustNodeKind::REWRITE, which
697
   * carries information about the proof generator for the rewrite, which can
698
   * be the null TrustNode if n is unchanged.
699
   *
700
   * Notice this method is used both in the "theory rewrite equalities"
701
   * preprocessing pass, where n is an equality from the input formula,
702
   * and in theory preprocessing, where n is a (non-equality) term occurring
703
   * in the input or generated in a lemma.
704
   *
705
   * @param n the node to preprocess-rewrite.
706
   * @param lems a set of lemmas that should be added as a consequence of
707
   * preprocessing n. These are in the form of "skolem lemmas". For example,
708
   * calling this method on (div x n), we return a trust node proving:
709
   *   (= (div x n) k_div)
710
   * for fresh skolem k, and add the skolem lemma for k that indicates that
711
   * it is the division of x and n.
712
   *
713
   * Note that ppRewrite should not return WITNESS terms, since the internal
714
   * calculus works in "original forms" and not "witness forms".
715
   */
716
116107
  virtual TrustNode ppRewrite(TNode n, std::vector<SkolemLemma>& lems)
717
  {
718
116107
    return TrustNode::null();
719
  }
720
721
  /**
722
   * Notify preprocessed assertions. Called on new assertions after
723
   * preprocessing before they are asserted to theory engine.
724
   */
725
151646
  virtual void ppNotifyAssertions(const std::vector<Node>& assertions) {}
726
  //--------------------------------- end preprocessing
727
728
  /**
729
   * A Theory is called with presolve exactly one time per user
730
   * check-sat.  presolve() is called after preregistration,
731
   * rewriting, and Boolean propagation, (other theories'
732
   * propagation?), but the notified Theory has not yet had its
733
   * check() or propagate() method called.  A Theory may empty its
734
   * assertFact() queue using get().  A Theory can raise conflicts,
735
   * add lemmas, and propagate literals during presolve().
736
   *
737
   * NOTE: The presolve property must be added to the kinds file for
738
   * the theory.
739
   */
740
  virtual void presolve() {}
741
742
  /**
743
   * A Theory is called with postsolve exactly one time per user
744
   * check-sat.  postsolve() is called after the query has completed
745
   * (regardless of whether sat, unsat, or unknown), and after any
746
   * model-querying related to the query has been performed.
747
   * After this call, the theory will not get another check() or
748
   * propagate() call until presolve() is called again.  A Theory
749
   * cannot raise conflicts, add lemmas, or propagate literals during
750
   * postsolve().
751
   */
752
  virtual void postsolve() {}
753
754
  /**
755
   * Notification sent to the theory wheneven the search restarts.
756
   * Serves as a good time to do some clean-up work, and you can
757
   * assume you're at DL 0 for the purposes of Contexts.  This function
758
   * should not use the output channel.
759
   */
760
  virtual void notifyRestart() {}
761
762
  /**
763
   * Identify this theory (for debugging, dynamic configuration,
764
   * etc..)
765
   */
766
  virtual std::string identify() const = 0;
767
768
  typedef context::CDList<Assertion>::const_iterator assertions_iterator;
769
770
  /**
771
   * Provides access to the facts queue, primarily intended for theory
772
   * debugging purposes.
773
   *
774
   * @return the iterator to the beginning of the fact queue
775
   */
776
167396
  assertions_iterator facts_begin() const { return d_facts.begin(); }
777
778
  /**
779
   * Provides access to the facts queue, primarily intended for theory
780
   * debugging purposes.
781
   *
782
   * @return the iterator to the end of the fact queue
783
   */
784
1251156
  assertions_iterator facts_end() const { return d_facts.end(); }
785
  /**
786
   * Whether facts have been asserted to this theory.
787
   *
788
   * @return true iff facts have been asserted to this theory.
789
   */
790
5950
  bool hasFacts() { return !d_facts.empty(); }
791
792
  /** Return total number of facts asserted to this theory */
793
4433
  size_t numAssertions() { return d_facts.size(); }
794
795
  typedef context::CDList<TNode>::const_iterator shared_terms_iterator;
796
797
  /**
798
   * Provides access to the shared terms, primarily intended for theory
799
   * debugging purposes.
800
   *
801
   * @return the iterator to the beginning of the shared terms list
802
   */
803
200279
  shared_terms_iterator shared_terms_begin() const
804
  {
805
200279
    return d_sharedTerms.begin();
806
  }
807
808
  /**
809
   * Provides access to the facts queue, primarily intended for theory
810
   * debugging purposes.
811
   *
812
   * @return the iterator to the end of the shared terms list
813
   */
814
249465
  shared_terms_iterator shared_terms_end() const { return d_sharedTerms.end(); }
815
816
  /**
817
   * This is a utility function for constructing a copy of the currently
818
   * shared terms in a queriable form.  As this is
819
   */
820
  std::unordered_set<TNode> currentlySharedTerms() const;
821
822
  /**
823
   * This allows the theory to be queried for whether a literal, lit, is
824
   * entailed by the theory.  This returns a pair of a Boolean and a node E.
825
   *
826
   * If the Boolean is true, then E is a formula that entails lit and E is
827
   * propositionally entailed by the assertions to the theory.
828
   *
829
   * If the Boolean is false, it is "unknown" if lit is entailed and E may be
830
   * any node.
831
   *
832
   * The literal lit is either an atom a or (not a), which must belong to the
833
   * theory: There is some TheoryOfMode m s.t. Theory::theoryOf(m, a) ==
834
   * this->getId().
835
   *
836
   * There are NO assumptions that a or the subterms of a have been
837
   * preprocessed in any form.  This includes ppRewrite, rewriting,
838
   * preregistering, registering, definition expansion or ITE removal!
839
   *
840
   * Theories are free to limit the amount of effort they use and so may
841
   * always opt to return "unknown".  Both "unknown" and "not entailed",
842
   * may return for E a non-boolean Node (e.g. Node::null()).  (There is no
843
   * explicit output for the negation of lit is entailed.)
844
   *
845
   * If lit is theory valid, the return result may be the Boolean constant
846
   * true for E.
847
   *
848
   * If lit is entailed by multiple assertions on the theory's getFact()
849
   * queue, a_1, a_2, ... and a_k, this may return E=(and a_1 a_2 ... a_k) or
850
   * another theory entailed explanation E=(and (and a_1 a_2) (and a3 a_4) ...
851
   * a_k)
852
   *
853
   * If lit is entailed by a single assertion on the theory's getFact()
854
   * queue, say a, this may return E=a.
855
   *
856
   * The theory may always return false!
857
   *
858
   * Theories may not touch their output stream during an entailment check.
859
   *
860
   * @param  lit     a literal belonging to the theory.
861
   * @return         a pair <b,E> s.t. if b is true, then a formula E such
862
   * that E |= lit in the theory.
863
   */
864
  virtual std::pair<bool, Node> entailmentCheck(TNode lit);
865
866
  /** Return true if this theory uses central equality engine */
867
  bool usesCentralEqualityEngine() const;
868
  /** uses central equality engine (static) */
869
  static bool usesCentralEqualityEngine(TheoryId id);
870
  /** Explains/propagates via central equality engine only */
871
  static bool expUsingCentralEqualityEngine(TheoryId id);
872
}; /* class Theory */
873
874
std::ostream& operator<<(std::ostream& os, theory::Theory::Effort level);
875
876
877
14830055
inline theory::Assertion Theory::get() {
878
14830055
  Assert(!done()) << "Theory::get() called with assertion queue empty!";
879
880
  // Get the assertion
881
14830055
  Assertion fact = d_facts[d_factsHead];
882
14830055
  d_factsHead = d_factsHead + 1;
883
884
14830055
  Trace("theory") << "Theory::get() => " << fact << " (" << d_facts.size() - d_factsHead << " left)" << std::endl;
885
886
14830055
  return fact;
887
}
888
889
inline std::ostream& operator<<(std::ostream& out,
890
                                const cvc5::theory::Theory& theory)
891
{
892
  return out << theory.identify();
893
}
894
895
inline std::ostream& operator << (std::ostream& out, theory::Theory::PPAssertStatus status) {
896
  switch (status) {
897
  case theory::Theory::PP_ASSERT_STATUS_SOLVED:
898
    out << "SOLVE_STATUS_SOLVED"; break;
899
  case theory::Theory::PP_ASSERT_STATUS_UNSOLVED:
900
    out << "SOLVE_STATUS_UNSOLVED"; break;
901
  case theory::Theory::PP_ASSERT_STATUS_CONFLICT:
902
    out << "SOLVE_STATUS_CONFLICT"; break;
903
  default:
904
    Unhandled();
905
  }
906
  return out;
907
}
908
909
}  // namespace theory
910
}  // namespace cvc5
911
912
#endif /* CVC5__THEORY__THEORY_H */