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"
37 /** Default value for the uninterpreted sorts is the UF theory */
38 TheoryId
Theory::s_uninterpretedSortOwner
= THEORY_UF
;
40 std::ostream
& operator<<(std::ostream
& os
, Theory::Effort level
){
42 case Theory::EFFORT_STANDARD
:
43 os
<< "EFFORT_STANDARD"; break;
44 case Theory::EFFORT_FULL
:
45 os
<< "EFFORT_FULL"; break;
46 case Theory::EFFORT_COMBINATION
:
47 os
<< "EFFORT_COMBINATION"; break;
48 case Theory::EFFORT_LAST_CALL
:
49 os
<< "EFFORT_LAST_CALL"; break;
54 }/* ostream& operator<<(ostream&, Theory::Effort) */
56 Theory::Theory(TheoryId id
,
57 context::Context
* satContext
,
58 context::UserContext
* userContext
,
61 const LogicInfo
& logicInfo
,
65 d_satContext(satContext
),
66 d_userContext(userContext
),
67 d_logicInfo(logicInfo
),
69 d_factsHead(satContext
, 0),
70 d_sharedTermsIndex(satContext
, 0),
73 d_decManager(nullptr),
75 d_checkTime(getStatsPrefix(id
) + name
+ "::checkTime"),
76 d_computeCareGraphTime(getStatsPrefix(id
) + name
77 + "::computeCareGraphTime"),
78 d_sharedTerms(satContext
),
80 d_valuation(valuation
),
81 d_proofsEnabled(false)
83 smtStatisticsRegistry()->registerStat(&d_checkTime
);
84 smtStatisticsRegistry()->registerStat(&d_computeCareGraphTime
);
88 smtStatisticsRegistry()->unregisterStat(&d_checkTime
);
89 smtStatisticsRegistry()->unregisterStat(&d_computeCareGraphTime
);
94 TheoryId
Theory::theoryOf(options::TheoryOfMode mode
, TNode node
)
96 TheoryId tid
= THEORY_BUILTIN
;
98 case options::TheoryOfMode::THEORY_OF_TYPE_BASED
:
99 // Constants, variables, 0-ary constructors
102 if (node
.getKind() == kind::BOOLEAN_TERM_VARIABLE
)
108 tid
= Theory::theoryOf(node
.getType());
111 else if (node
.isConst())
113 tid
= Theory::theoryOf(node
.getType());
115 else if (node
.getKind() == kind::EQUAL
)
117 // Equality is owned by the theory that owns the domain
118 tid
= Theory::theoryOf(node
[0].getType());
122 // Regular nodes are owned by the kind
123 tid
= kindToTheoryId(node
.getKind());
126 case options::TheoryOfMode::THEORY_OF_TERM_BASED
:
130 if (Theory::theoryOf(node
.getType()) != theory::THEORY_BOOL
)
132 // We treat the variables as uninterpreted
133 tid
= s_uninterpretedSortOwner
;
137 if (node
.getKind() == kind::BOOLEAN_TERM_VARIABLE
)
139 // Boolean vars go to UF
144 // Except for the Boolean ones
149 else if (node
.isConst())
151 // Constants go to the theory of the type
152 tid
= Theory::theoryOf(node
.getType());
154 else if (node
.getKind() == kind::EQUAL
)
156 // If one of them is an ITE, it's irelevant, since they will get
157 // replaced out anyhow
158 if (node
[0].getKind() == kind::ITE
)
160 tid
= Theory::theoryOf(node
[0].getType());
162 else if (node
[1].getKind() == kind::ITE
)
164 tid
= Theory::theoryOf(node
[1].getType());
170 TypeNode ltype
= l
.getType();
171 TypeNode rtype
= r
.getType();
174 tid
= Theory::theoryOf(l
.getType());
178 // If both sides belong to the same theory the choice is easy
179 TheoryId T1
= Theory::theoryOf(l
);
180 TheoryId T2
= Theory::theoryOf(r
);
187 TheoryId T3
= Theory::theoryOf(ltype
);
189 // * x*y = f(z) -> UF
191 // * f(x) = read(a, y) -> either UF or ARRAY
192 // at least one of the theories has to be parametric, i.e. theory
193 // of the type is different from the theory of the term
204 // If both are parametric, we take the smaller one (arbitrary)
205 tid
= T1
< T2
? T1
: T2
;
213 // Regular nodes are owned by the kind
214 tid
= kindToTheoryId(node
.getKind());
220 Trace("theory::internal") << "theoryOf(" << mode
<< ", " << node
<< ") -> " << tid
<< std::endl
;
224 void Theory::addSharedTermInternal(TNode n
) {
225 Debug("sharing") << "Theory::addSharedTerm<" << getId() << ">(" << n
<< ")" << endl
;
226 Debug("theory::assertions") << "Theory::addSharedTerm<" << getId() << ">(" << n
<< ")" << endl
;
227 d_sharedTerms
.push_back(n
);
231 void Theory::computeCareGraph() {
232 Debug("sharing") << "Theory::computeCareGraph<" << getId() << ">()" << endl
;
233 for (unsigned i
= 0; i
< d_sharedTerms
.size(); ++ i
) {
234 TNode a
= d_sharedTerms
[i
];
235 TypeNode aType
= a
.getType();
236 for (unsigned j
= i
+ 1; j
< d_sharedTerms
.size(); ++ j
) {
237 TNode b
= d_sharedTerms
[j
];
238 if (b
.getType() != aType
) {
239 // We don't care about the terms of different types
242 switch (d_valuation
.getEqualityStatus(a
, b
)) {
243 case EQUALITY_TRUE_AND_PROPAGATED
:
244 case EQUALITY_FALSE_AND_PROPAGATED
:
245 // If we know about it, we should have propagated it, so we can skip
256 void Theory::printFacts(std::ostream
& os
) const {
257 unsigned i
, n
= d_facts
.size();
258 for(i
= 0; i
< n
; i
++){
259 const Assertion
& a_i
= d_facts
[i
];
260 Node assertion
= a_i
;
261 os
<< d_id
<< '[' << i
<< ']' << " " << assertion
<< endl
;
265 void Theory::debugPrintFacts() const{
266 DebugChannel
.getStream() << "Theory::debugPrintFacts()" << endl
;
267 printFacts(DebugChannel
.getStream());
270 std::unordered_set
<TNode
, TNodeHashFunction
> Theory::currentlySharedTerms() const{
271 std::unordered_set
<TNode
, TNodeHashFunction
> currentlyShared
;
272 for (shared_terms_iterator i
= shared_terms_begin(),
273 i_end
= shared_terms_end(); i
!= i_end
; ++i
) {
274 currentlyShared
.insert (*i
);
276 return currentlyShared
;
279 void Theory::collectTerms(TNode n
,
281 set
<Node
>& termSet
) const
283 if (termSet
.find(n
) != termSet
.end()) {
286 Kind nk
= n
.getKind();
287 if (irrKinds
.find(nk
) == irrKinds
.end())
289 Trace("theory::collectTerms")
290 << "Theory::collectTerms: adding " << n
<< endl
;
293 if (nk
== kind::NOT
|| nk
== kind::EQUAL
|| !isLeaf(n
))
295 for(TNode::iterator child_it
= n
.begin(); child_it
!= n
.end(); ++child_it
) {
296 collectTerms(*child_it
, irrKinds
, termSet
);
302 void Theory::computeRelevantTerms(set
<Node
>& termSet
, bool includeShared
) const
305 computeRelevantTerms(termSet
, irrKinds
, includeShared
);
308 void Theory::computeRelevantTerms(set
<Node
>& termSet
,
310 bool includeShared
) const
312 // Collect all terms appearing in assertions
313 irrKinds
.insert(kind::EQUAL
);
314 irrKinds
.insert(kind::NOT
);
315 context::CDList
<Assertion
>::const_iterator assert_it
= facts_begin(), assert_it_end
= facts_end();
316 for (; assert_it
!= assert_it_end
; ++assert_it
) {
317 collectTerms(*assert_it
, irrKinds
, termSet
);
320 if (!includeShared
) return;
322 // Add terms that are shared terms
324 context::CDList
<TNode
>::const_iterator shared_it
= shared_terms_begin(), shared_it_end
= shared_terms_end();
325 for (; shared_it
!= shared_it_end
; ++shared_it
) {
326 collectTerms(*shared_it
, kempty
, termSet
);
330 Theory::PPAssertStatus
Theory::ppAssert(TNode in
,
331 SubstitutionMap
& outSubstitutions
)
333 if (in
.getKind() == kind::EQUAL
)
335 // (and (= x t) phi) can be replaced by phi[x/t] if
336 // 1) x is a variable
337 // 2) x is not in the term t
338 // 3) x : T and t : S, then S <: T
339 if (in
[0].isVar() && !expr::hasSubterm(in
[1], in
[0])
340 && (in
[1].getType()).isSubtypeOf(in
[0].getType())
341 && in
[0].getKind() != kind::BOOLEAN_TERM_VARIABLE
)
343 outSubstitutions
.addSubstitution(in
[0], in
[1]);
344 return PP_ASSERT_STATUS_SOLVED
;
346 if (in
[1].isVar() && !expr::hasSubterm(in
[0], in
[1])
347 && (in
[0].getType()).isSubtypeOf(in
[1].getType())
348 && in
[1].getKind() != kind::BOOLEAN_TERM_VARIABLE
)
350 outSubstitutions
.addSubstitution(in
[1], in
[0]);
351 return PP_ASSERT_STATUS_SOLVED
;
353 if (in
[0].isConst() && in
[1].isConst())
357 return PP_ASSERT_STATUS_CONFLICT
;
362 return PP_ASSERT_STATUS_UNSOLVED
;
365 std::pair
<bool, Node
> Theory::entailmentCheck(
367 const EntailmentCheckParameters
* params
,
368 EntailmentCheckSideEffects
* out
) {
369 return make_pair(false, Node::null());
372 ExtTheory
* Theory::getExtTheory() {
373 Assert(d_extTheory
!= NULL
);
377 void Theory::addCarePair(TNode t1
, TNode t2
) {
379 d_careGraph
->insert(CarePair(t1
, t2
, d_id
));
383 void Theory::getCareGraph(CareGraph
* careGraph
) {
384 Assert(careGraph
!= NULL
);
386 Trace("sharing") << "Theory<" << getId() << ">::getCareGraph()" << std::endl
;
387 TimerStat::CodeTimer
computeCareGraphTime(d_computeCareGraphTime
);
388 d_careGraph
= careGraph
;
393 void Theory::setQuantifiersEngine(QuantifiersEngine
* qe
) {
394 Assert(d_quantEngine
== NULL
);
399 void Theory::setDecisionManager(DecisionManager
* dm
)
401 Assert(d_decManager
== nullptr);
402 Assert(dm
!= nullptr);
406 void Theory::setupExtTheory() {
407 Assert(d_extTheory
== NULL
);
408 d_extTheory
= new ExtTheory(this);
412 EntailmentCheckParameters::EntailmentCheckParameters(TheoryId tid
)
416 EntailmentCheckParameters::~EntailmentCheckParameters(){}
418 TheoryId
EntailmentCheckParameters::getTheoryId() const {
422 EntailmentCheckSideEffects::EntailmentCheckSideEffects(TheoryId tid
)
426 TheoryId
EntailmentCheckSideEffects::getTheoryId() const {
430 EntailmentCheckSideEffects::~EntailmentCheckSideEffects() {
433 }/* CVC4::theory namespace */
434 }/* CVC4 namespace */