GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/theory.h Lines: 70 93 75.3 %
Date: 2021-08-17 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
78782
  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
222078
  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
9850
  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
52666879
  static inline TheoryId theoryOf(TypeNode typeNode) {
344
52666879
    Trace("theory::internal") << "theoryOf(" << typeNode << ")" << std::endl;
345
    TheoryId id;
346
52666879
    if (typeNode.getKind() == kind::TYPE_CONSTANT) {
347
37927523
      id = typeConstantToTheoryId(typeNode.getConst<TypeConstant>());
348
    } else {
349
14739356
      id = kindToTheoryId(typeNode.getKind());
350
    }
351
52666879
    if (id == THEORY_BUILTIN) {
352
2575014
      Trace("theory::internal") << "theoryOf(" << typeNode << ") == " << s_uninterpretedSortOwner << std::endl;
353
2575014
      return s_uninterpretedSortOwner;
354
    }
355
50091865
    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
170669392
  static inline TheoryId theoryOf(TNode node) {
367
170669392
    return theoryOf(options::theoryOfMode(), node);
368
  }
369
370
  /**
371
   * Set the owner of the uninterpreted sort.
372
   */
373
9850
  static void setUninterpretedSortOwner(TheoryId theory) {
374
9850
    s_uninterpretedSortOwner = theory;
375
9850
  }
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
382331
  inline bool isLeaf(TNode node) const {
388
382331
    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
204447551
  inline static bool isLeafOf(TNode node, TheoryId theoryId) {
395
204447551
    return node.getNumChildren() == 0 || theoryOf(node) != theoryId;
396
  }
397
398
  /** Returns true if the assertFact queue is empty*/
399
49074076
  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
32673232
  static inline bool fullEffort(Effort e) CVC5_CONST_FUNCTION
437
  {
438
32673232
    return e == EFFORT_FULL; }
439
440
  /**
441
   * Get the id for this Theory.
442
   */
443
18067112
  TheoryId getId() const {
444
18067112
    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
18941636
  context::Context* getSatContext() const { return d_env.getContext(); }
456
457
  /**
458
   * Get the context associated to this Theory.
459
   */
460
330640
  context::UserContext* getUserContext() const {
461
330640
    return d_env.getUserContext();
462
  }
463
464
  /**
465
   * Get the output channel associated to this theory.
466
   */
467
123905
  OutputChannel& getOutputChannel() {
468
123905
    return *d_out;
469
  }
470
471
  /**
472
   * Get the valuation associated to this theory.
473
   */
474
36026
  Valuation& getValuation() {
475
36026
    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
269677
  QuantifiersEngine* getQuantifiersEngine() {
485
269677
    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
9850
  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
14488947
  void assertFact(TNode assertion, bool isPreregistered) {
507
28977894
    Trace("theory") << "Theory<" << getId() << ">::assertFact["
508
28977894
                    << getSatContext()->getLevel() << "](" << assertion << ", "
509
14488947
                    << (isPreregistered ? "true" : "false") << ")" << std::endl;
510
14488947
    d_facts.push_back(Assertion(assertion, isPreregistered));
511
14488947
  }
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
60873
  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 model values, after equality information is added to the model.
643
   * The argument termSet is the set of relevant terms returned by
644
   * computeRelevantTerms.
645
   */
646
  virtual bool collectModelValues(TheoryModel* m,
647
                                  const std::set<Node>& termSet);
648
  /** if theories want to do something with model after building, do it here */
649
7320
  virtual void postProcessModel( TheoryModel* m ){ }
650
  //--------------------------------- end collect model info
651
652
  //--------------------------------- preprocessing
653
  /**
654
   * Statically learn from assertion "in," which has been asserted
655
   * true at the top level.  The theory should only add (via
656
   * ::operator<< or ::append()) to the "learned" builder---it should
657
   * *never* clear it.  It is a conjunction to add to the formula at
658
   * the top-level and may contain other theories' contributions.
659
   */
660
  virtual void ppStaticLearn(TNode in, NodeBuilder& learned) {}
661
662
  enum PPAssertStatus {
663
    /** Atom has been solved  */
664
    PP_ASSERT_STATUS_SOLVED,
665
    /** Atom has not been solved */
666
    PP_ASSERT_STATUS_UNSOLVED,
667
    /** Atom is inconsistent */
668
    PP_ASSERT_STATUS_CONFLICT
669
  };
670
671
  /**
672
   * Given a literal and its proof generator (encapsulated by trust node tin),
673
   * add the solved substitutions to the map, if any. The method should return
674
   * true if the literal can be safely removed from the input problem.
675
   *
676
   * Note that tin has trust node kind LEMMA. Its proof generator should be
677
   * taken into account when adding a substitution to outSubstitutions when
678
   * proofs are enabled.
679
   */
680
  virtual PPAssertStatus ppAssert(TrustNode tin,
681
                                  TrustSubstitutionMap& outSubstitutions);
682
683
  /**
684
   * Given a term of the theory coming from the input formula or
685
   * from a lemma generated during solving, this method can be overridden in a
686
   * theory implementation to rewrite the term into an equivalent form.
687
   *
688
   * This method returns a TrustNode of kind TrustNodeKind::REWRITE, which
689
   * carries information about the proof generator for the rewrite, which can
690
   * be the null TrustNode if n is unchanged.
691
   *
692
   * Notice this method is used both in the "theory rewrite equalities"
693
   * preprocessing pass, where n is an equality from the input formula,
694
   * and in theory preprocessing, where n is a (non-equality) term occurring
695
   * in the input or generated in a lemma.
696
   *
697
   * @param n the node to preprocess-rewrite.
698
   * @param lems a set of lemmas that should be added as a consequence of
699
   * preprocessing n. These are in the form of "skolem lemmas". For example,
700
   * calling this method on (div x n), we return a trust node proving:
701
   *   (= (div x n) k_div)
702
   * for fresh skolem k, and add the skolem lemma for k that indicates that
703
   * it is the division of x and n.
704
   *
705
   * Note that ppRewrite should not return WITNESS terms, since the internal
706
   * calculus works in "original forms" and not "witness forms".
707
   */
708
118210
  virtual TrustNode ppRewrite(TNode n, std::vector<SkolemLemma>& lems)
709
  {
710
118210
    return TrustNode::null();
711
  }
712
713
  /**
714
   * Notify preprocessed assertions. Called on new assertions after
715
   * preprocessing before they are asserted to theory engine.
716
   */
717
151107
  virtual void ppNotifyAssertions(const std::vector<Node>& assertions) {}
718
  //--------------------------------- end preprocessing
719
720
  /**
721
   * A Theory is called with presolve exactly one time per user
722
   * check-sat.  presolve() is called after preregistration,
723
   * rewriting, and Boolean propagation, (other theories'
724
   * propagation?), but the notified Theory has not yet had its
725
   * check() or propagate() method called.  A Theory may empty its
726
   * assertFact() queue using get().  A Theory can raise conflicts,
727
   * add lemmas, and propagate literals during presolve().
728
   *
729
   * NOTE: The presolve property must be added to the kinds file for
730
   * the theory.
731
   */
732
  virtual void presolve() { }
733
734
  /**
735
   * A Theory is called with postsolve exactly one time per user
736
   * check-sat.  postsolve() is called after the query has completed
737
   * (regardless of whether sat, unsat, or unknown), and after any
738
   * model-querying related to the query has been performed.
739
   * After this call, the theory will not get another check() or
740
   * propagate() call until presolve() is called again.  A Theory
741
   * cannot raise conflicts, add lemmas, or propagate literals during
742
   * postsolve().
743
   */
744
  virtual void postsolve() { }
745
746
  /**
747
   * Notification sent to the theory wheneven the search restarts.
748
   * Serves as a good time to do some clean-up work, and you can
749
   * assume you're at DL 0 for the purposes of Contexts.  This function
750
   * should not use the output channel.
751
   */
752
  virtual void notifyRestart() { }
753
754
  /**
755
   * Identify this theory (for debugging, dynamic configuration,
756
   * etc..)
757
   */
758
  virtual std::string identify() const = 0;
759
760
  /** Set user attribute
761
    * This function is called when an attribute is set by a user.  In SMT-LIBv2 this is done
762
    *  via the syntax (! n :attr)
763
    */
764
  virtual void setUserAttribute(const std::string& attr, Node n, std::vector<Node> node_values, std::string str_value) {
765
    Unimplemented() << "Theory " << identify()
766
                    << " doesn't support Theory::setUserAttribute interface";
767
  }
768
769
  typedef context::CDList<Assertion>::const_iterator assertions_iterator;
770
771
  /**
772
   * Provides access to the facts queue, primarily intended for theory
773
   * debugging purposes.
774
   *
775
   * @return the iterator to the beginning of the fact queue
776
   */
777
162357
  assertions_iterator facts_begin() const {
778
162357
    return d_facts.begin();
779
  }
780
781
  /**
782
   * Provides access to the facts queue, primarily intended for theory
783
   * debugging purposes.
784
   *
785
   * @return the iterator to the end of the fact queue
786
   */
787
781031
  assertions_iterator facts_end() const {
788
781031
    return d_facts.end();
789
  }
790
  /**
791
   * Whether facts have been asserted to this theory.
792
   *
793
   * @return true iff facts have been asserted to this theory.
794
   */
795
5939
  bool hasFacts() { return !d_facts.empty(); }
796
797
  /** Return total number of facts asserted to this theory */
798
3015
  size_t numAssertions() {
799
3015
    return d_facts.size();
800
  }
801
802
  typedef context::CDList<TNode>::const_iterator shared_terms_iterator;
803
804
  /**
805
   * Provides access to the shared terms, primarily intended for theory
806
   * debugging purposes.
807
   *
808
   * @return the iterator to the beginning of the shared terms list
809
   */
810
152833
  shared_terms_iterator shared_terms_begin() const {
811
152833
    return d_sharedTerms.begin();
812
  }
813
814
  /**
815
   * Provides access to the facts queue, primarily intended for theory
816
   * debugging purposes.
817
   *
818
   * @return the iterator to the end of the shared terms list
819
   */
820
165532
  shared_terms_iterator shared_terms_end() const {
821
165532
    return d_sharedTerms.end();
822
  }
823
824
825
  /**
826
   * This is a utility function for constructing a copy of the currently shared terms
827
   * in a queriable form.  As this is
828
   */
829
  std::unordered_set<TNode> currentlySharedTerms() const;
830
831
  /**
832
   * This allows the theory to be queried for whether a literal, lit, is
833
   * entailed by the theory.  This returns a pair of a Boolean and a node E.
834
   *
835
   * If the Boolean is true, then E is a formula that entails lit and E is propositionally
836
   * entailed by the assertions to the theory.
837
   *
838
   * If the Boolean is false, it is "unknown" if lit is entailed and E may be
839
   * any node.
840
   *
841
   * The literal lit is either an atom a or (not a), which must belong to the theory:
842
   *   There is some TheoryOfMode m s.t. Theory::theoryOf(m, a) == this->getId().
843
   *
844
   * There are NO assumptions that a or the subterms of a have been
845
   * preprocessed in any form.  This includes ppRewrite, rewriting,
846
   * preregistering, registering, definition expansion or ITE removal!
847
   *
848
   * Theories are free to limit the amount of effort they use and so may
849
   * always opt to return "unknown".  Both "unknown" and "not entailed",
850
   * may return for E a non-boolean Node (e.g. Node::null()).  (There is no explicit output
851
   * for the negation of lit is entailed.)
852
   *
853
   * If lit is theory valid, the return result may be the Boolean constant
854
   * true for E.
855
   *
856
   * If lit is entailed by multiple assertions on the theory's getFact()
857
   * queue, a_1, a_2, ... and a_k, this may return E=(and a_1 a_2 ... a_k) or
858
   * another theory entailed explanation E=(and (and a_1 a_2) (and a3 a_4) ... a_k)
859
   *
860
   * If lit is entailed by a single assertion on the theory's getFact()
861
   * queue, say a, this may return E=a.
862
   *
863
   * The theory may always return false!
864
   *
865
   * Theories may not touch their output stream during an entailment check.
866
   *
867
   * @param  lit     a literal belonging to the theory.
868
   * @return         a pair <b,E> s.t. if b is true, then a formula E such that
869
   * E |= lit in the theory.
870
   */
871
  virtual std::pair<bool, Node> entailmentCheck(TNode lit);
872
873
  /** Return true if this theory uses central equality engine */
874
  bool usesCentralEqualityEngine() const;
875
  /** uses central equality engine (static) */
876
  static bool usesCentralEqualityEngine(TheoryId id);
877
  /** Explains/propagates via central equality engine only */
878
  static bool expUsingCentralEqualityEngine(TheoryId id);
879
};/* class Theory */
880
881
std::ostream& operator<<(std::ostream& os, theory::Theory::Effort level);
882
883
884
14147761
inline theory::Assertion Theory::get() {
885
14147761
  Assert(!done()) << "Theory::get() called with assertion queue empty!";
886
887
  // Get the assertion
888
14147761
  Assertion fact = d_facts[d_factsHead];
889
14147761
  d_factsHead = d_factsHead + 1;
890
891
14147761
  Trace("theory") << "Theory::get() => " << fact << " (" << d_facts.size() - d_factsHead << " left)" << std::endl;
892
893
14147761
  return fact;
894
}
895
896
inline std::ostream& operator<<(std::ostream& out,
897
                                const cvc5::theory::Theory& theory)
898
{
899
  return out << theory.identify();
900
}
901
902
inline std::ostream& operator << (std::ostream& out, theory::Theory::PPAssertStatus status) {
903
  switch (status) {
904
  case theory::Theory::PP_ASSERT_STATUS_SOLVED:
905
    out << "SOLVE_STATUS_SOLVED"; break;
906
  case theory::Theory::PP_ASSERT_STATUS_UNSOLVED:
907
    out << "SOLVE_STATUS_UNSOLVED"; break;
908
  case theory::Theory::PP_ASSERT_STATUS_CONFLICT:
909
    out << "SOLVE_STATUS_CONFLICT"; break;
910
  default:
911
    Unhandled();
912
  }
913
  return out;
914
}
915
916
}  // namespace theory
917
}  // namespace cvc5
918
919
#endif /* CVC5__THEORY__THEORY_H */