1 /********************* */
4 ** Top contributors (to current version):
5 ** Tim King, Mathias Preiner, Dejan Jovanovic
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2020 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.\endverbatim
12 ** \brief Base for theory interface.
14 ** Base for theory interface.
17 #include "theory/theory.h"
24 #include "base/check.h"
25 #include "expr/node_algorithm.h"
26 #include "options/theory_options.h"
27 #include "smt/smt_statistics_registry.h"
28 #include "theory/ext_theory.h"
29 #include "theory/quantifiers_engine.h"
30 #include "theory/substitutions.h"
31 #include "theory/theory_rewriter.h"
38 /** Default value for the uninterpreted sorts is the UF theory */
39 TheoryId
Theory::s_uninterpretedSortOwner
= THEORY_UF
;
41 std::ostream
& operator<<(std::ostream
& os
, Theory::Effort level
){
43 case Theory::EFFORT_STANDARD
:
44 os
<< "EFFORT_STANDARD"; break;
45 case Theory::EFFORT_FULL
:
46 os
<< "EFFORT_FULL"; break;
47 case Theory::EFFORT_COMBINATION
:
48 os
<< "EFFORT_COMBINATION"; break;
49 case Theory::EFFORT_LAST_CALL
:
50 os
<< "EFFORT_LAST_CALL"; break;
55 }/* ostream& operator<<(ostream&, Theory::Effort) */
57 Theory::Theory(TheoryId id
,
58 context::Context
* satContext
,
59 context::UserContext
* userContext
,
62 const LogicInfo
& logicInfo
,
63 ProofNodeManager
* pnm
,
66 d_satContext(satContext
),
67 d_userContext(userContext
),
68 d_logicInfo(logicInfo
),
71 d_factsHead(satContext
, 0),
72 d_sharedTermsIndex(satContext
, 0),
75 d_decManager(nullptr),
77 d_checkTime(getStatsPrefix(id
) + name
+ "::checkTime"),
78 d_computeCareGraphTime(getStatsPrefix(id
) + name
79 + "::computeCareGraphTime"),
80 d_sharedTerms(satContext
),
82 d_valuation(valuation
),
83 d_equalityEngine(nullptr),
84 d_allocEqualityEngine(nullptr),
85 d_proofsEnabled(false)
87 smtStatisticsRegistry()->registerStat(&d_checkTime
);
88 smtStatisticsRegistry()->registerStat(&d_computeCareGraphTime
);
92 smtStatisticsRegistry()->unregisterStat(&d_checkTime
);
93 smtStatisticsRegistry()->unregisterStat(&d_computeCareGraphTime
);
96 bool Theory::needsEqualityEngine(EeSetupInfo
& esi
)
98 // by default, this theory does not use an (official) equality engine
102 void Theory::setEqualityEngine(eq::EqualityEngine
* ee
)
104 // set the equality engine pointer
105 d_equalityEngine
= ee
;
107 void Theory::setQuantifiersEngine(QuantifiersEngine
* qe
)
109 Assert(d_quantEngine
== nullptr);
113 void Theory::setDecisionManager(DecisionManager
* dm
)
115 Assert(d_decManager
== nullptr);
116 Assert(dm
!= nullptr);
120 void Theory::finishInitStandalone()
123 if (needsEqualityEngine(esi
))
125 // always associated with the same SAT context as the theory (d_satContext)
126 d_allocEqualityEngine
.reset(new eq::EqualityEngine(
127 *esi
.d_notify
, d_satContext
, esi
.d_name
, esi
.d_constantsAreTriggers
));
128 // use it as the official equality engine
129 d_equalityEngine
= d_allocEqualityEngine
.get();
134 TheoryId
Theory::theoryOf(options::TheoryOfMode mode
, TNode node
)
136 TheoryId tid
= THEORY_BUILTIN
;
138 case options::TheoryOfMode::THEORY_OF_TYPE_BASED
:
139 // Constants, variables, 0-ary constructors
142 if (node
.getKind() == kind::BOOLEAN_TERM_VARIABLE
)
148 tid
= Theory::theoryOf(node
.getType());
151 else if (node
.isConst())
153 tid
= Theory::theoryOf(node
.getType());
155 else if (node
.getKind() == kind::EQUAL
)
157 // Equality is owned by the theory that owns the domain
158 tid
= Theory::theoryOf(node
[0].getType());
162 // Regular nodes are owned by the kind
163 tid
= kindToTheoryId(node
.getKind());
166 case options::TheoryOfMode::THEORY_OF_TERM_BASED
:
170 if (Theory::theoryOf(node
.getType()) != theory::THEORY_BOOL
)
172 // We treat the variables as uninterpreted
173 tid
= s_uninterpretedSortOwner
;
177 if (node
.getKind() == kind::BOOLEAN_TERM_VARIABLE
)
179 // Boolean vars go to UF
184 // Except for the Boolean ones
189 else if (node
.isConst())
191 // Constants go to the theory of the type
192 tid
= Theory::theoryOf(node
.getType());
194 else if (node
.getKind() == kind::EQUAL
)
196 // If one of them is an ITE, it's irelevant, since they will get
197 // replaced out anyhow
198 if (node
[0].getKind() == kind::ITE
)
200 tid
= Theory::theoryOf(node
[0].getType());
202 else if (node
[1].getKind() == kind::ITE
)
204 tid
= Theory::theoryOf(node
[1].getType());
210 TypeNode ltype
= l
.getType();
211 TypeNode rtype
= r
.getType();
214 tid
= Theory::theoryOf(l
.getType());
218 // If both sides belong to the same theory the choice is easy
219 TheoryId T1
= Theory::theoryOf(l
);
220 TheoryId T2
= Theory::theoryOf(r
);
227 TheoryId T3
= Theory::theoryOf(ltype
);
229 // * x*y = f(z) -> UF
231 // * f(x) = read(a, y) -> either UF or ARRAY
232 // at least one of the theories has to be parametric, i.e. theory
233 // of the type is different from the theory of the term
244 // If both are parametric, we take the smaller one (arbitrary)
245 tid
= T1
< T2
? T1
: T2
;
253 // Regular nodes are owned by the kind
254 tid
= kindToTheoryId(node
.getKind());
260 Trace("theory::internal") << "theoryOf(" << mode
<< ", " << node
<< ") -> " << tid
<< std::endl
;
264 void Theory::addSharedTermInternal(TNode n
) {
265 Debug("sharing") << "Theory::addSharedTerm<" << getId() << ">(" << n
<< ")" << endl
;
266 Debug("theory::assertions") << "Theory::addSharedTerm<" << getId() << ">(" << n
<< ")" << endl
;
267 d_sharedTerms
.push_back(n
);
271 void Theory::computeCareGraph() {
272 Debug("sharing") << "Theory::computeCareGraph<" << getId() << ">()" << endl
;
273 for (unsigned i
= 0; i
< d_sharedTerms
.size(); ++ i
) {
274 TNode a
= d_sharedTerms
[i
];
275 TypeNode aType
= a
.getType();
276 for (unsigned j
= i
+ 1; j
< d_sharedTerms
.size(); ++ j
) {
277 TNode b
= d_sharedTerms
[j
];
278 if (b
.getType() != aType
) {
279 // We don't care about the terms of different types
282 switch (d_valuation
.getEqualityStatus(a
, b
)) {
283 case EQUALITY_TRUE_AND_PROPAGATED
:
284 case EQUALITY_FALSE_AND_PROPAGATED
:
285 // If we know about it, we should have propagated it, so we can skip
296 void Theory::printFacts(std::ostream
& os
) const {
297 unsigned i
, n
= d_facts
.size();
298 for(i
= 0; i
< n
; i
++){
299 const Assertion
& a_i
= d_facts
[i
];
300 Node assertion
= a_i
;
301 os
<< d_id
<< '[' << i
<< ']' << " " << assertion
<< endl
;
305 void Theory::debugPrintFacts() const{
306 DebugChannel
.getStream() << "Theory::debugPrintFacts()" << endl
;
307 printFacts(DebugChannel
.getStream());
310 bool Theory::isLegalElimination(TNode x
, TNode val
)
313 if (x
.getKind() == kind::BOOLEAN_TERM_VARIABLE
314 || val
.getKind() == kind::BOOLEAN_TERM_VARIABLE
)
318 if (expr::hasSubterm(val
, x
))
322 if (!val
.getType().isSubtypeOf(x
.getType()))
326 if (!options::produceModels())
328 // don't care about the model, we are fine
331 // if there is a model object
332 TheoryModel
* tm
= d_valuation
.getModel();
333 Assert(tm
!= nullptr);
334 return tm
->isLegalElimination(x
, val
);
337 std::unordered_set
<TNode
, TNodeHashFunction
> Theory::currentlySharedTerms() const{
338 std::unordered_set
<TNode
, TNodeHashFunction
> currentlyShared
;
339 for (shared_terms_iterator i
= shared_terms_begin(),
340 i_end
= shared_terms_end(); i
!= i_end
; ++i
) {
341 currentlyShared
.insert (*i
);
343 return currentlyShared
;
346 void Theory::collectTerms(TNode n
,
348 set
<Node
>& termSet
) const
350 if (termSet
.find(n
) != termSet
.end()) {
353 Kind nk
= n
.getKind();
354 if (irrKinds
.find(nk
) == irrKinds
.end())
356 Trace("theory::collectTerms")
357 << "Theory::collectTerms: adding " << n
<< endl
;
360 if (nk
== kind::NOT
|| nk
== kind::EQUAL
|| !isLeaf(n
))
362 for(TNode::iterator child_it
= n
.begin(); child_it
!= n
.end(); ++child_it
) {
363 collectTerms(*child_it
, irrKinds
, termSet
);
369 void Theory::computeRelevantTerms(set
<Node
>& termSet
, bool includeShared
) const
372 computeRelevantTerms(termSet
, irrKinds
, includeShared
);
375 void Theory::computeRelevantTerms(set
<Node
>& termSet
,
377 bool includeShared
) const
379 // Collect all terms appearing in assertions
380 irrKinds
.insert(kind::EQUAL
);
381 irrKinds
.insert(kind::NOT
);
382 context::CDList
<Assertion
>::const_iterator assert_it
= facts_begin(), assert_it_end
= facts_end();
383 for (; assert_it
!= assert_it_end
; ++assert_it
) {
384 collectTerms(*assert_it
, irrKinds
, termSet
);
387 if (!includeShared
) return;
389 // Add terms that are shared terms
391 context::CDList
<TNode
>::const_iterator shared_it
= shared_terms_begin(), shared_it_end
= shared_terms_end();
392 for (; shared_it
!= shared_it_end
; ++shared_it
) {
393 collectTerms(*shared_it
, kempty
, termSet
);
397 Theory::PPAssertStatus
Theory::ppAssert(TNode in
,
398 SubstitutionMap
& outSubstitutions
)
400 if (in
.getKind() == kind::EQUAL
)
402 // (and (= x t) phi) can be replaced by phi[x/t] if
403 // 1) x is a variable
404 // 2) x is not in the term t
405 // 3) x : T and t : S, then S <: T
406 if (in
[0].isVar() && isLegalElimination(in
[0], in
[1])
407 && in
[0].getKind() != kind::BOOLEAN_TERM_VARIABLE
)
409 outSubstitutions
.addSubstitution(in
[0], in
[1]);
410 return PP_ASSERT_STATUS_SOLVED
;
412 if (in
[1].isVar() && isLegalElimination(in
[1], in
[0])
413 && in
[1].getKind() != kind::BOOLEAN_TERM_VARIABLE
)
415 outSubstitutions
.addSubstitution(in
[1], in
[0]);
416 return PP_ASSERT_STATUS_SOLVED
;
418 if (in
[0].isConst() && in
[1].isConst())
422 return PP_ASSERT_STATUS_CONFLICT
;
427 return PP_ASSERT_STATUS_UNSOLVED
;
430 std::pair
<bool, Node
> Theory::entailmentCheck(TNode lit
)
432 return make_pair(false, Node::null());
435 void Theory::addCarePair(TNode t1
, TNode t2
) {
437 d_careGraph
->insert(CarePair(t1
, t2
, d_id
));
441 void Theory::getCareGraph(CareGraph
* careGraph
) {
442 Assert(careGraph
!= NULL
);
444 Trace("sharing") << "Theory<" << getId() << ">::getCareGraph()" << std::endl
;
445 TimerStat::CodeTimer
computeCareGraphTime(d_computeCareGraphTime
);
446 d_careGraph
= careGraph
;
451 eq::EqualityEngine
* Theory::getEqualityEngine()
453 // get the assigned equality engine, which is a pointer stored in this class
454 return d_equalityEngine
;
457 }/* CVC4::theory namespace */
458 }/* CVC4 namespace */