1 /********************* */
4 ** Top contributors (to current version):
5 ** Tim King, Dejan Jovanovic, Andrew Reynolds
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2019 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
,
66 d_satContext(satContext
),
67 d_userContext(userContext
),
68 d_logicInfo(logicInfo
),
70 d_factsHead(satContext
, 0),
71 d_sharedTermsIndex(satContext
, 0),
74 d_decManager(nullptr),
76 d_checkTime(getStatsPrefix(id
) + name
+ "::checkTime"),
77 d_computeCareGraphTime(getStatsPrefix(id
) + name
78 + "::computeCareGraphTime"),
79 d_sharedTerms(satContext
),
81 d_valuation(valuation
),
82 d_proofsEnabled(false)
84 smtStatisticsRegistry()->registerStat(&d_checkTime
);
85 smtStatisticsRegistry()->registerStat(&d_computeCareGraphTime
);
89 smtStatisticsRegistry()->unregisterStat(&d_checkTime
);
90 smtStatisticsRegistry()->unregisterStat(&d_computeCareGraphTime
);
95 TheoryId
Theory::theoryOf(options::TheoryOfMode mode
, TNode node
)
97 TheoryId tid
= THEORY_BUILTIN
;
99 case options::TheoryOfMode::THEORY_OF_TYPE_BASED
:
100 // Constants, variables, 0-ary constructors
103 if (node
.getKind() == kind::BOOLEAN_TERM_VARIABLE
)
109 tid
= Theory::theoryOf(node
.getType());
112 else if (node
.isConst())
114 tid
= Theory::theoryOf(node
.getType());
116 else if (node
.getKind() == kind::EQUAL
)
118 // Equality is owned by the theory that owns the domain
119 tid
= Theory::theoryOf(node
[0].getType());
123 // Regular nodes are owned by the kind
124 tid
= kindToTheoryId(node
.getKind());
127 case options::TheoryOfMode::THEORY_OF_TERM_BASED
:
131 if (Theory::theoryOf(node
.getType()) != theory::THEORY_BOOL
)
133 // We treat the variables as uninterpreted
134 tid
= s_uninterpretedSortOwner
;
138 if (node
.getKind() == kind::BOOLEAN_TERM_VARIABLE
)
140 // Boolean vars go to UF
145 // Except for the Boolean ones
150 else if (node
.isConst())
152 // Constants go to the theory of the type
153 tid
= Theory::theoryOf(node
.getType());
155 else if (node
.getKind() == kind::EQUAL
)
157 // If one of them is an ITE, it's irelevant, since they will get
158 // replaced out anyhow
159 if (node
[0].getKind() == kind::ITE
)
161 tid
= Theory::theoryOf(node
[0].getType());
163 else if (node
[1].getKind() == kind::ITE
)
165 tid
= Theory::theoryOf(node
[1].getType());
171 TypeNode ltype
= l
.getType();
172 TypeNode rtype
= r
.getType();
175 tid
= Theory::theoryOf(l
.getType());
179 // If both sides belong to the same theory the choice is easy
180 TheoryId T1
= Theory::theoryOf(l
);
181 TheoryId T2
= Theory::theoryOf(r
);
188 TheoryId T3
= Theory::theoryOf(ltype
);
190 // * x*y = f(z) -> UF
192 // * f(x) = read(a, y) -> either UF or ARRAY
193 // at least one of the theories has to be parametric, i.e. theory
194 // of the type is different from the theory of the term
205 // If both are parametric, we take the smaller one (arbitrary)
206 tid
= T1
< T2
? T1
: T2
;
214 // Regular nodes are owned by the kind
215 tid
= kindToTheoryId(node
.getKind());
221 Trace("theory::internal") << "theoryOf(" << mode
<< ", " << node
<< ") -> " << tid
<< std::endl
;
225 void Theory::addSharedTermInternal(TNode n
) {
226 Debug("sharing") << "Theory::addSharedTerm<" << getId() << ">(" << n
<< ")" << endl
;
227 Debug("theory::assertions") << "Theory::addSharedTerm<" << getId() << ">(" << n
<< ")" << endl
;
228 d_sharedTerms
.push_back(n
);
232 void Theory::computeCareGraph() {
233 Debug("sharing") << "Theory::computeCareGraph<" << getId() << ">()" << endl
;
234 for (unsigned i
= 0; i
< d_sharedTerms
.size(); ++ i
) {
235 TNode a
= d_sharedTerms
[i
];
236 TypeNode aType
= a
.getType();
237 for (unsigned j
= i
+ 1; j
< d_sharedTerms
.size(); ++ j
) {
238 TNode b
= d_sharedTerms
[j
];
239 if (b
.getType() != aType
) {
240 // We don't care about the terms of different types
243 switch (d_valuation
.getEqualityStatus(a
, b
)) {
244 case EQUALITY_TRUE_AND_PROPAGATED
:
245 case EQUALITY_FALSE_AND_PROPAGATED
:
246 // If we know about it, we should have propagated it, so we can skip
257 void Theory::printFacts(std::ostream
& os
) const {
258 unsigned i
, n
= d_facts
.size();
259 for(i
= 0; i
< n
; i
++){
260 const Assertion
& a_i
= d_facts
[i
];
261 Node assertion
= a_i
;
262 os
<< d_id
<< '[' << i
<< ']' << " " << assertion
<< endl
;
266 void Theory::debugPrintFacts() const{
267 DebugChannel
.getStream() << "Theory::debugPrintFacts()" << endl
;
268 printFacts(DebugChannel
.getStream());
271 bool Theory::isLegalElimination(TNode x
, TNode val
)
274 if (expr::hasSubterm(val
, x
))
278 if (!val
.getType().isSubtypeOf(x
.getType()))
282 if (!options::produceModels())
284 // don't care about the model, we are fine
287 // if there is a model object
288 TheoryModel
* tm
= d_valuation
.getModel();
289 Assert(tm
!= nullptr);
290 return tm
->isLegalElimination(x
, val
);
293 std::unordered_set
<TNode
, TNodeHashFunction
> Theory::currentlySharedTerms() const{
294 std::unordered_set
<TNode
, TNodeHashFunction
> currentlyShared
;
295 for (shared_terms_iterator i
= shared_terms_begin(),
296 i_end
= shared_terms_end(); i
!= i_end
; ++i
) {
297 currentlyShared
.insert (*i
);
299 return currentlyShared
;
302 void Theory::collectTerms(TNode n
,
304 set
<Node
>& termSet
) const
306 if (termSet
.find(n
) != termSet
.end()) {
309 Kind nk
= n
.getKind();
310 if (irrKinds
.find(nk
) == irrKinds
.end())
312 Trace("theory::collectTerms")
313 << "Theory::collectTerms: adding " << n
<< endl
;
316 if (nk
== kind::NOT
|| nk
== kind::EQUAL
|| !isLeaf(n
))
318 for(TNode::iterator child_it
= n
.begin(); child_it
!= n
.end(); ++child_it
) {
319 collectTerms(*child_it
, irrKinds
, termSet
);
325 void Theory::computeRelevantTerms(set
<Node
>& termSet
, bool includeShared
) const
328 computeRelevantTerms(termSet
, irrKinds
, includeShared
);
331 void Theory::computeRelevantTerms(set
<Node
>& termSet
,
333 bool includeShared
) const
335 // Collect all terms appearing in assertions
336 irrKinds
.insert(kind::EQUAL
);
337 irrKinds
.insert(kind::NOT
);
338 context::CDList
<Assertion
>::const_iterator assert_it
= facts_begin(), assert_it_end
= facts_end();
339 for (; assert_it
!= assert_it_end
; ++assert_it
) {
340 collectTerms(*assert_it
, irrKinds
, termSet
);
343 if (!includeShared
) return;
345 // Add terms that are shared terms
347 context::CDList
<TNode
>::const_iterator shared_it
= shared_terms_begin(), shared_it_end
= shared_terms_end();
348 for (; shared_it
!= shared_it_end
; ++shared_it
) {
349 collectTerms(*shared_it
, kempty
, termSet
);
353 Theory::PPAssertStatus
Theory::ppAssert(TNode in
,
354 SubstitutionMap
& outSubstitutions
)
356 if (in
.getKind() == kind::EQUAL
)
358 // (and (= x t) phi) can be replaced by phi[x/t] if
359 // 1) x is a variable
360 // 2) x is not in the term t
361 // 3) x : T and t : S, then S <: T
362 if (in
[0].isVar() && isLegalElimination(in
[0], in
[1])
363 && in
[0].getKind() != kind::BOOLEAN_TERM_VARIABLE
)
365 outSubstitutions
.addSubstitution(in
[0], in
[1]);
366 return PP_ASSERT_STATUS_SOLVED
;
368 if (in
[1].isVar() && isLegalElimination(in
[1], in
[0])
369 && in
[1].getKind() != kind::BOOLEAN_TERM_VARIABLE
)
371 outSubstitutions
.addSubstitution(in
[1], in
[0]);
372 return PP_ASSERT_STATUS_SOLVED
;
374 if (in
[0].isConst() && in
[1].isConst())
378 return PP_ASSERT_STATUS_CONFLICT
;
383 return PP_ASSERT_STATUS_UNSOLVED
;
386 std::pair
<bool, Node
> Theory::entailmentCheck(
388 const EntailmentCheckParameters
* params
,
389 EntailmentCheckSideEffects
* out
) {
390 return make_pair(false, Node::null());
393 ExtTheory
* Theory::getExtTheory() {
394 Assert(d_extTheory
!= NULL
);
398 void Theory::addCarePair(TNode t1
, TNode t2
) {
400 d_careGraph
->insert(CarePair(t1
, t2
, d_id
));
404 void Theory::getCareGraph(CareGraph
* careGraph
) {
405 Assert(careGraph
!= NULL
);
407 Trace("sharing") << "Theory<" << getId() << ">::getCareGraph()" << std::endl
;
408 TimerStat::CodeTimer
computeCareGraphTime(d_computeCareGraphTime
);
409 d_careGraph
= careGraph
;
414 void Theory::setQuantifiersEngine(QuantifiersEngine
* qe
) {
415 Assert(d_quantEngine
== NULL
);
420 void Theory::setDecisionManager(DecisionManager
* dm
)
422 Assert(d_decManager
== nullptr);
423 Assert(dm
!= nullptr);
427 void Theory::setupExtTheory() {
428 Assert(d_extTheory
== NULL
);
429 d_extTheory
= new ExtTheory(this);
433 EntailmentCheckParameters::EntailmentCheckParameters(TheoryId tid
)
437 EntailmentCheckParameters::~EntailmentCheckParameters(){}
439 TheoryId
EntailmentCheckParameters::getTheoryId() const {
443 EntailmentCheckSideEffects::EntailmentCheckSideEffects(TheoryId tid
)
447 TheoryId
EntailmentCheckSideEffects::getTheoryId() const {
451 EntailmentCheckSideEffects::~EntailmentCheckSideEffects() {
454 }/* CVC4::theory namespace */
455 }/* CVC4 namespace */