GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/theory.h Lines: 65 85 76.5 %
Date: 2021-09-07 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
79366
  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
9926
  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
56537451
  static inline TheoryId theoryOf(TypeNode typeNode)
345
  {
346
56537451
    Trace("theory::internal") << "theoryOf(" << typeNode << ")" << std::endl;
347
    TheoryId id;
348
56537451
    if (typeNode.getKind() == kind::TYPE_CONSTANT)
349
    {
350
41871446
      id = typeConstantToTheoryId(typeNode.getConst<TypeConstant>());
351
    }
352
    else
353
    {
354
14666005
      id = kindToTheoryId(typeNode.getKind());
355
    }
356
56537451
    if (id == THEORY_BUILTIN)
357
    {
358
5170828
      Trace("theory::internal")
359
2585414
          << "theoryOf(" << typeNode << ") == " << s_uninterpretedSortOwner
360
2585414
          << std::endl;
361
2585414
      return s_uninterpretedSortOwner;
362
    }
363
53952037
    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
191540894
  static inline TheoryId theoryOf(TNode node)
375
  {
376
191540894
    return theoryOf(options::theoryOfMode(), node);
377
  }
378
379
  /**
380
   * Set the owner of the uninterpreted sort.
381
   */
382
9926
  static void setUninterpretedSortOwner(TheoryId theory)
383
  {
384
9926
    s_uninterpretedSortOwner = theory;
385
9926
  }
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
391057
  inline bool isLeaf(TNode node) const
399
  {
400
391057
    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
228115729
  inline static bool isLeafOf(TNode node, TheoryId theoryId)
407
  {
408
228115729
    return node.getNumChildren() == 0 || theoryOf(node) != theoryId;
409
  }
410
411
  /** Returns true if the assertFact queue is empty*/
412
51523623
  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
35447602
  static inline bool fullEffort(Effort e) CVC5_CONST_FUNCTION
452
  {
453
35447602
    return e == EFFORT_FULL;
454
  }
455
456
  /**
457
   * Get the id for this Theory.
458
   */
459
19345273
  TheoryId getId() const { return d_id; }
460
461
  /**
462
   * Get a reference to the environment.
463
   */
464
54350
  Env& getEnv() const { return d_env; }
465
466
  /**
467
   * Shorthand to access the options object.
468
   */
469
54350
  const Options& options() const { return getEnv().getOptions(); }
470
471
  /**
472
   * Get the SAT context associated to this Theory.
473
   */
474
20133707
  context::Context* getSatContext() const { return d_env.getContext(); }
475
476
  /**
477
   * Get the context associated to this Theory.
478
   */
479
302990
  context::UserContext* getUserContext() const
480
  {
481
302990
    return d_env.getUserContext();
482
  }
483
484
  /**
485
   * Get the output channel associated to this theory.
486
   */
487
124808
  OutputChannel& getOutputChannel() { return *d_out; }
488
489
  /**
490
   * Get the valuation associated to this theory.
491
   */
492
38868
  Valuation& getValuation() { return d_valuation; }
493
494
  /** Get the equality engine being used by this theory. */
495
  eq::EqualityEngine* getEqualityEngine();
496
497
  /**
498
   * Get the quantifiers engine associated to this theory.
499
   */
500
272810
  QuantifiersEngine* getQuantifiersEngine() { return d_quantEngine; }
501
502
  /**
503
   * @return The theory state associated with this theory.
504
   */
505
  TheoryState* getTheoryState() { return d_theoryState; }
506
507
  /**
508
   * @return The theory inference manager associated with this theory.
509
   */
510
9926
  TheoryInferenceManager* getInferenceManager() { return d_inferManager; }
511
512
  /**
513
   * Pre-register a term.  Done one time for a Node per SAT context level.
514
   */
515
  virtual void preRegisterTerm(TNode);
516
517
  /**
518
   * Assert a fact in the current context.
519
   */
520
15296920
  void assertFact(TNode assertion, bool isPreregistered)
521
  {
522
30593840
    Trace("theory") << "Theory<" << getId() << ">::assertFact["
523
30593840
                    << getSatContext()->getLevel() << "](" << assertion << ", "
524
15296920
                    << (isPreregistered ? "true" : "false") << ")" << std::endl;
525
15296920
    d_facts.push_back(Assertion(assertion, isPreregistered));
526
15296920
  }
527
528
  /** Add shared term to the theory. */
529
  void addSharedTerm(TNode node);
530
531
  /**
532
   * Return the current theory care graph. Theories should overload
533
   * computeCareGraph to do the actual computation, and use addCarePair to add
534
   * pairs to the care graph.
535
   */
536
  void getCareGraph(CareGraph* careGraph);
537
538
  /**
539
   * Return the status of two terms in the current context. Should be
540
   * implemented in sub-theories to enable more efficient theory-combination.
541
   */
542
  virtual EqualityStatus getEqualityStatus(TNode a, TNode b);
543
544
  /**
545
   * Return the model value of the give shared term (or null if not
546
   * available).
547
   *
548
   * TODO (project #39): this method is likely to become deprecated.
549
   */
550
1638
  virtual Node getModelValue(TNode var) { return Node::null(); }
551
552
  /** T-propagate new literal assignments in the current context. */
553
  virtual void propagate(Effort level = EFFORT_FULL) {}
554
555
  /**
556
   * Return an explanation for the literal represented by parameter n
557
   * (which was previously propagated by this theory).
558
   */
559
  virtual TrustNode explain(TNode n)
560
  {
561
    Unimplemented() << "Theory " << identify()
562
                    << " propagated a node but doesn't implement the "
563
                       "Theory::explain() interface!";
564
    return TrustNode::null();
565
  }
566
567
  //--------------------------------- check
568
  /**
569
   * Does this theory wish to be called to check at last call effort? This is
570
   * the case for any theory that wishes to run when a model is available.
571
   */
572
61169
  virtual bool needsCheckLastEffort() { return false; }
573
  /**
574
   * Check the current assignment's consistency.
575
   *
576
   * An implementation of check() is required to either:
577
   * - return a conflict on the output channel,
578
   * - be interrupted,
579
   * - throw an exception
580
   * - or call get() until done() is true.
581
   *
582
   * The standard method for check consists of a loop that processes the
583
   * entire fact queue when preCheck returns false. It makes four
584
   * theory-specific callbacks, (preCheck, postCheck, preNotifyFact,
585
   * notifyFact) as described below. It asserts each fact to the official
586
   * equality engine when preNotifyFact returns false.
587
   *
588
   * Theories that use this check method must use an official theory
589
   * state object (d_theoryState).
590
   */
591
  void check(Effort level = EFFORT_FULL);
592
  /**
593
   * Pre-check, called before the fact queue of the theory is processed.
594
   * If this method returns false, then the theory will process its fact
595
   * queue. If this method returns true, then the theory has indicated
596
   * its check method should finish immediately.
597
   */
598
  virtual bool preCheck(Effort level = EFFORT_FULL);
599
  /**
600
   * Post-check, called after the fact queue of the theory is processed.
601
   */
602
  virtual void postCheck(Effort level = EFFORT_FULL);
603
  /**
604
   * Pre-notify fact, return true if the theory processed it. If this
605
   * method returns false, then the atom will be added to the equality engine
606
   * of the theory and notifyFact will be called with isInternal=false.
607
   *
608
   * Theories that implement check but do not use official equality
609
   * engines should always return true for this method.
610
   *
611
   * @param atom The atom
612
   * @param polarity Its polarity
613
   * @param fact The original literal that was asserted
614
   * @param isPrereg Whether the assertion is preregistered
615
   * @param isInternal Whether the origin of the fact was internal. If this
616
   * is false, the fact was asserted via the fact queue of the theory.
617
   * @return true if the theory completely processed this fact, i.e. it does
618
   * not need to assert the fact to its equality engine.
619
   */
620
  virtual bool preNotifyFact(
621
      TNode atom, bool pol, TNode fact, bool isPrereg, bool isInternal);
622
  /**
623
   * Notify fact, called immediately after the fact was pushed into the
624
   * equality engine.
625
   *
626
   * @param atom The atom
627
   * @param polarity Its polarity
628
   * @param fact The original literal that was asserted.
629
   * @param isInternal Whether the origin of the fact was internal. If this
630
   * is false, the fact was asserted via the fact queue of the theory.
631
   */
632
  virtual void notifyFact(TNode atom, bool pol, TNode fact, bool isInternal);
633
  //--------------------------------- end check
634
635
  //--------------------------------- collect model info
636
  /**
637
   * Get all relevant information in this theory regarding the current
638
   * model.  This should be called after a call to check( FULL_EFFORT )
639
   * for all theories with no conflicts and no lemmas added.
640
   *
641
   * This method returns true if and only if the equality engine of m is
642
   * consistent as a result of this call.
643
   *
644
   * The standard method for collectModelInfo computes the relevant terms,
645
   * asserts the theory's equality engine to the model (if necessary) and
646
   * then calls computeModelValues.
647
   *
648
   * TODO (project #39): this method should be non-virtual, once all theories
649
   * conform to the new standard, delete, move to model manager distributed.
650
   */
651
  virtual bool collectModelInfo(TheoryModel* m, const std::set<Node>& termSet);
652
  /**
653
   * Compute terms that are not necessarily part of the assertions or
654
   * shared terms that should be considered relevant, add them to termSet.
655
   */
656
  virtual void computeRelevantTerms(std::set<Node>& termSet);
657
  /**
658
   * Collect asserted terms for this theory and add them to  termSet.
659
   *
660
   * @param termSet The set to add terms to
661
   * @param includeShared Whether to include the shared terms of the theory
662
   */
663
  void collectAssertedTerms(std::set<Node>& termSet,
664
                            bool includeShared = true) const;
665
  /**
666
   * Helper function for collectAssertedTerms, adds all subterms
667
   * belonging to this theory to termSet.
668
   */
669
  void collectTerms(TNode n, std::set<Node>& termSet) const;
670
  /**
671
   * Collect model values, after equality information is added to the model.
672
   * The argument termSet is the set of relevant terms returned by
673
   * computeRelevantTerms.
674
   */
675
  virtual bool collectModelValues(TheoryModel* m,
676
                                  const std::set<Node>& termSet);
677
  /** if theories want to do something with model after building, do it here
678
   */
679
7524
  virtual void postProcessModel(TheoryModel* m) {}
680
  //--------------------------------- end collect model info
681
682
  //--------------------------------- preprocessing
683
  /**
684
   * Statically learn from assertion "in," which has been asserted
685
   * true at the top level.  The theory should only add (via
686
   * ::operator<< or ::append()) to the "learned" builder---it should
687
   * *never* clear it.  It is a conjunction to add to the formula at
688
   * the top-level and may contain other theories' contributions.
689
   */
690
  virtual void ppStaticLearn(TNode in, NodeBuilder& learned) {}
691
692
  enum PPAssertStatus
693
  {
694
    /** Atom has been solved  */
695
    PP_ASSERT_STATUS_SOLVED,
696
    /** Atom has not been solved */
697
    PP_ASSERT_STATUS_UNSOLVED,
698
    /** Atom is inconsistent */
699
    PP_ASSERT_STATUS_CONFLICT
700
  };
701
702
  /**
703
   * Given a literal and its proof generator (encapsulated by trust node tin),
704
   * add the solved substitutions to the map, if any. The method should return
705
   * true if the literal can be safely removed from the input problem.
706
   *
707
   * Note that tin has trust node kind LEMMA. Its proof generator should be
708
   * taken into account when adding a substitution to outSubstitutions when
709
   * proofs are enabled.
710
   */
711
  virtual PPAssertStatus ppAssert(TrustNode tin,
712
                                  TrustSubstitutionMap& outSubstitutions);
713
714
  /**
715
   * Given a term of the theory coming from the input formula or
716
   * from a lemma generated during solving, this method can be overridden in a
717
   * theory implementation to rewrite the term into an equivalent form.
718
   *
719
   * This method returns a TrustNode of kind TrustNodeKind::REWRITE, which
720
   * carries information about the proof generator for the rewrite, which can
721
   * be the null TrustNode if n is unchanged.
722
   *
723
   * Notice this method is used both in the "theory rewrite equalities"
724
   * preprocessing pass, where n is an equality from the input formula,
725
   * and in theory preprocessing, where n is a (non-equality) term occurring
726
   * in the input or generated in a lemma.
727
   *
728
   * @param n the node to preprocess-rewrite.
729
   * @param lems a set of lemmas that should be added as a consequence of
730
   * preprocessing n. These are in the form of "skolem lemmas". For example,
731
   * calling this method on (div x n), we return a trust node proving:
732
   *   (= (div x n) k_div)
733
   * for fresh skolem k, and add the skolem lemma for k that indicates that
734
   * it is the division of x and n.
735
   *
736
   * Note that ppRewrite should not return WITNESS terms, since the internal
737
   * calculus works in "original forms" and not "witness forms".
738
   */
739
118095
  virtual TrustNode ppRewrite(TNode n, std::vector<SkolemLemma>& lems)
740
  {
741
118095
    return TrustNode::null();
742
  }
743
744
  /**
745
   * Notify preprocessed assertions. Called on new assertions after
746
   * preprocessing before they are asserted to theory engine.
747
   */
748
151624
  virtual void ppNotifyAssertions(const std::vector<Node>& assertions) {}
749
  //--------------------------------- end preprocessing
750
751
  /**
752
   * A Theory is called with presolve exactly one time per user
753
   * check-sat.  presolve() is called after preregistration,
754
   * rewriting, and Boolean propagation, (other theories'
755
   * propagation?), but the notified Theory has not yet had its
756
   * check() or propagate() method called.  A Theory may empty its
757
   * assertFact() queue using get().  A Theory can raise conflicts,
758
   * add lemmas, and propagate literals during presolve().
759
   *
760
   * NOTE: The presolve property must be added to the kinds file for
761
   * the theory.
762
   */
763
  virtual void presolve() {}
764
765
  /**
766
   * A Theory is called with postsolve exactly one time per user
767
   * check-sat.  postsolve() is called after the query has completed
768
   * (regardless of whether sat, unsat, or unknown), and after any
769
   * model-querying related to the query has been performed.
770
   * After this call, the theory will not get another check() or
771
   * propagate() call until presolve() is called again.  A Theory
772
   * cannot raise conflicts, add lemmas, or propagate literals during
773
   * postsolve().
774
   */
775
  virtual void postsolve() {}
776
777
  /**
778
   * Notification sent to the theory wheneven the search restarts.
779
   * Serves as a good time to do some clean-up work, and you can
780
   * assume you're at DL 0 for the purposes of Contexts.  This function
781
   * should not use the output channel.
782
   */
783
  virtual void notifyRestart() {}
784
785
  /**
786
   * Identify this theory (for debugging, dynamic configuration,
787
   * etc..)
788
   */
789
  virtual std::string identify() const = 0;
790
791
  typedef context::CDList<Assertion>::const_iterator assertions_iterator;
792
793
  /**
794
   * Provides access to the facts queue, primarily intended for theory
795
   * debugging purposes.
796
   *
797
   * @return the iterator to the beginning of the fact queue
798
   */
799
167379
  assertions_iterator facts_begin() const { return d_facts.begin(); }
800
801
  /**
802
   * Provides access to the facts queue, primarily intended for theory
803
   * debugging purposes.
804
   *
805
   * @return the iterator to the end of the fact queue
806
   */
807
1165532
  assertions_iterator facts_end() const { return d_facts.end(); }
808
  /**
809
   * Whether facts have been asserted to this theory.
810
   *
811
   * @return true iff facts have been asserted to this theory.
812
   */
813
5936
  bool hasFacts() { return !d_facts.empty(); }
814
815
  /** Return total number of facts asserted to this theory */
816
4368
  size_t numAssertions() { return d_facts.size(); }
817
818
  typedef context::CDList<TNode>::const_iterator shared_terms_iterator;
819
820
  /**
821
   * Provides access to the shared terms, primarily intended for theory
822
   * debugging purposes.
823
   *
824
   * @return the iterator to the beginning of the shared terms list
825
   */
826
200296
  shared_terms_iterator shared_terms_begin() const
827
  {
828
200296
    return d_sharedTerms.begin();
829
  }
830
831
  /**
832
   * Provides access to the facts queue, primarily intended for theory
833
   * debugging purposes.
834
   *
835
   * @return the iterator to the end of the shared terms list
836
   */
837
238975
  shared_terms_iterator shared_terms_end() const { return d_sharedTerms.end(); }
838
839
  /**
840
   * This is a utility function for constructing a copy of the currently
841
   * shared terms in a queriable form.  As this is
842
   */
843
  std::unordered_set<TNode> currentlySharedTerms() const;
844
845
  /**
846
   * This allows the theory to be queried for whether a literal, lit, is
847
   * entailed by the theory.  This returns a pair of a Boolean and a node E.
848
   *
849
   * If the Boolean is true, then E is a formula that entails lit and E is
850
   * propositionally entailed by the assertions to the theory.
851
   *
852
   * If the Boolean is false, it is "unknown" if lit is entailed and E may be
853
   * any node.
854
   *
855
   * The literal lit is either an atom a or (not a), which must belong to the
856
   * theory: There is some TheoryOfMode m s.t. Theory::theoryOf(m, a) ==
857
   * this->getId().
858
   *
859
   * There are NO assumptions that a or the subterms of a have been
860
   * preprocessed in any form.  This includes ppRewrite, rewriting,
861
   * preregistering, registering, definition expansion or ITE removal!
862
   *
863
   * Theories are free to limit the amount of effort they use and so may
864
   * always opt to return "unknown".  Both "unknown" and "not entailed",
865
   * may return for E a non-boolean Node (e.g. Node::null()).  (There is no
866
   * explicit output for the negation of lit is entailed.)
867
   *
868
   * If lit is theory valid, the return result may be the Boolean constant
869
   * true for E.
870
   *
871
   * If lit is entailed by multiple assertions on the theory's getFact()
872
   * queue, a_1, a_2, ... and a_k, this may return E=(and a_1 a_2 ... a_k) or
873
   * another theory entailed explanation E=(and (and a_1 a_2) (and a3 a_4) ...
874
   * a_k)
875
   *
876
   * If lit is entailed by a single assertion on the theory's getFact()
877
   * queue, say a, this may return E=a.
878
   *
879
   * The theory may always return false!
880
   *
881
   * Theories may not touch their output stream during an entailment check.
882
   *
883
   * @param  lit     a literal belonging to the theory.
884
   * @return         a pair <b,E> s.t. if b is true, then a formula E such
885
   * that E |= lit in the theory.
886
   */
887
  virtual std::pair<bool, Node> entailmentCheck(TNode lit);
888
889
  /** Return true if this theory uses central equality engine */
890
  bool usesCentralEqualityEngine() const;
891
  /** uses central equality engine (static) */
892
  static bool usesCentralEqualityEngine(TheoryId id);
893
  /** Explains/propagates via central equality engine only */
894
  static bool expUsingCentralEqualityEngine(TheoryId id);
895
}; /* class Theory */
896
897
std::ostream& operator<<(std::ostream& os, theory::Theory::Effort level);
898
899
900
14944568
inline theory::Assertion Theory::get() {
901
14944568
  Assert(!done()) << "Theory::get() called with assertion queue empty!";
902
903
  // Get the assertion
904
14944568
  Assertion fact = d_facts[d_factsHead];
905
14944568
  d_factsHead = d_factsHead + 1;
906
907
14944568
  Trace("theory") << "Theory::get() => " << fact << " (" << d_facts.size() - d_factsHead << " left)" << std::endl;
908
909
14944568
  return fact;
910
}
911
912
inline std::ostream& operator<<(std::ostream& out,
913
                                const cvc5::theory::Theory& theory)
914
{
915
  return out << theory.identify();
916
}
917
918
inline std::ostream& operator << (std::ostream& out, theory::Theory::PPAssertStatus status) {
919
  switch (status) {
920
  case theory::Theory::PP_ASSERT_STATUS_SOLVED:
921
    out << "SOLVE_STATUS_SOLVED"; break;
922
  case theory::Theory::PP_ASSERT_STATUS_UNSOLVED:
923
    out << "SOLVE_STATUS_UNSOLVED"; break;
924
  case theory::Theory::PP_ASSERT_STATUS_CONFLICT:
925
    out << "SOLVE_STATUS_CONFLICT"; break;
926
  default:
927
    Unhandled();
928
  }
929
  return out;
930
}
931
932
}  // namespace theory
933
}  // namespace cvc5
934
935
#endif /* CVC5__THEORY__THEORY_H */