1 /********************* */
4 ** Original author: Morgan Deters
5 ** Major contributors: Clark Barrett, Dejan Jovanovic, Tim King
6 ** Minor contributors (to current version): Kshitij Bansal, Andrew Reynolds
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2014 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
12 ** \brief Base for theory interface.
14 ** Base for theory interface.
17 #include "theory/theory.h"
21 #include "base/cvc4_assert.h"
22 #include "smt/smt_statistics_registry.h"
23 #include "theory/substitutions.h"
24 #include "theory/quantifiers_engine.h"
32 /** Default value for the uninterpreted sorts is the UF theory */
33 TheoryId
Theory::s_uninterpretedSortOwner
= THEORY_UF
;
35 std::ostream
& operator<<(std::ostream
& os
, Theory::Effort level
){
37 case Theory::EFFORT_STANDARD
:
38 os
<< "EFFORT_STANDARD"; break;
39 case Theory::EFFORT_FULL
:
40 os
<< "EFFORT_FULL"; break;
41 case Theory::EFFORT_COMBINATION
:
42 os
<< "EFFORT_COMBINATION"; break;
43 case Theory::EFFORT_LAST_CALL
:
44 os
<< "EFFORT_LAST_CALL"; break;
49 }/* ostream& operator<<(ostream&, Theory::Effort) */
51 Theory::Theory(TheoryId id
, context::Context
* satContext
, context::UserContext
* userContext
,
52 OutputChannel
& out
, Valuation valuation
, const LogicInfo
& logicInfo
,
53 SmtGlobals
* globals
, std::string name
) throw()
55 , d_instanceName(name
)
56 , d_satContext(satContext
)
57 , d_userContext(userContext
)
58 , d_logicInfo(logicInfo
)
60 , d_factsHead(satContext
, 0)
61 , d_sharedTermsIndex(satContext
, 0)
64 , d_checkTime(getFullInstanceName() + "::checkTime")
65 , d_computeCareGraphTime(getFullInstanceName() + "::computeCareGraphTime")
66 , d_sharedTerms(satContext
)
68 , d_valuation(valuation
)
69 , d_proofsEnabled(false)
72 smtStatisticsRegistry()->registerStat(&d_checkTime
);
73 smtStatisticsRegistry()->registerStat(&d_computeCareGraphTime
);
77 smtStatisticsRegistry()->unregisterStat(&d_checkTime
);
78 smtStatisticsRegistry()->unregisterStat(&d_computeCareGraphTime
);
81 TheoryId
Theory::theoryOf(TheoryOfMode mode
, TNode node
) {
82 TheoryId tid
= THEORY_BUILTIN
;
84 case THEORY_OF_TYPE_BASED
:
85 // Constants, variables, 0-ary constructors
86 if (node
.isVar() || node
.isConst()) {
87 tid
= Theory::theoryOf(node
.getType());
88 } else if (node
.getKind() == kind::EQUAL
) {
89 // Equality is owned by the theory that owns the domain
90 tid
= Theory::theoryOf(node
[0].getType());
92 // Regular nodes are owned by the kind
93 tid
= kindToTheoryId(node
.getKind());
96 case THEORY_OF_TERM_BASED
:
99 if (Theory::theoryOf(node
.getType()) != theory::THEORY_BOOL
) {
100 // We treat the variables as uninterpreted
101 tid
= s_uninterpretedSortOwner
;
103 // Except for the Boolean ones, which we just ignore anyhow
104 tid
= theory::THEORY_BOOL
;
106 } else if (node
.isConst()) {
107 // Constants go to the theory of the type
108 tid
= Theory::theoryOf(node
.getType());
109 } else if (node
.getKind() == kind::EQUAL
) { // Equality
110 // If one of them is an ITE, it's irelevant, since they will get replaced out anyhow
111 if (node
[0].getKind() == kind::ITE
) {
112 tid
= Theory::theoryOf(node
[0].getType());
113 } else if (node
[1].getKind() == kind::ITE
) {
114 tid
= Theory::theoryOf(node
[1].getType());
118 TypeNode ltype
= l
.getType();
119 TypeNode rtype
= r
.getType();
120 if( ltype
!= rtype
){
121 tid
= Theory::theoryOf(l
.getType());
123 // If both sides belong to the same theory the choice is easy
124 TheoryId T1
= Theory::theoryOf(l
);
125 TheoryId T2
= Theory::theoryOf(r
);
129 TheoryId T3
= Theory::theoryOf(ltype
);
131 // * x*y = f(z) -> UF
133 // * f(x) = read(a, y) -> either UF or ARRAY
134 // at least one of the theories has to be parametric, i.e. theory of the type is different
135 // from the theory of the term
138 } else if (T2
== T3
) {
141 // If both are parametric, we take the smaller one (arbitrary)
142 tid
= T1
< T2
? T1
: T2
;
148 // Regular nodes are owned by the kind
149 tid
= kindToTheoryId(node
.getKind());
155 Trace("theory::internal") << "theoryOf(" << mode
<< ", " << node
<< ") -> " << tid
<< std::endl
;
159 void Theory::addSharedTermInternal(TNode n
) {
160 Debug("sharing") << "Theory::addSharedTerm<" << getId() << ">(" << n
<< ")" << endl
;
161 Debug("theory::assertions") << "Theory::addSharedTerm<" << getId() << ">(" << n
<< ")" << endl
;
162 d_sharedTerms
.push_back(n
);
166 void Theory::computeCareGraph() {
167 Debug("sharing") << "Theory::computeCareGraph<" << getId() << ">()" << endl
;
168 for (unsigned i
= 0; i
< d_sharedTerms
.size(); ++ i
) {
169 TNode a
= d_sharedTerms
[i
];
170 TypeNode aType
= a
.getType();
171 for (unsigned j
= i
+ 1; j
< d_sharedTerms
.size(); ++ j
) {
172 TNode b
= d_sharedTerms
[j
];
173 if (b
.getType() != aType
) {
174 // We don't care about the terms of different types
177 switch (d_valuation
.getEqualityStatus(a
, b
)) {
178 case EQUALITY_TRUE_AND_PROPAGATED
:
179 case EQUALITY_FALSE_AND_PROPAGATED
:
180 // If we know about it, we should have propagated it, so we can skip
191 void Theory::printFacts(std::ostream
& os
) const {
192 unsigned i
, n
= d_facts
.size();
193 for(i
= 0; i
< n
; i
++){
194 const Assertion
& a_i
= d_facts
[i
];
195 Node assertion
= a_i
;
196 os
<< d_id
<< '[' << i
<< ']' << " " << assertion
<< endl
;
200 void Theory::debugPrintFacts() const{
201 DebugChannel
.getStream() << "Theory::debugPrintFacts()" << endl
;
202 printFacts(DebugChannel
.getStream());
205 std::hash_set
<TNode
, TNodeHashFunction
> Theory::currentlySharedTerms() const{
206 std::hash_set
<TNode
, TNodeHashFunction
> currentlyShared
;
207 for(shared_terms_iterator i
= shared_terms_begin(), i_end
= shared_terms_end(); i
!= i_end
; ++i
){
208 currentlyShared
.insert (*i
);
210 return currentlyShared
;
214 void Theory::collectTerms(TNode n
, set
<Node
>& termSet
) const
216 if (termSet
.find(n
) != termSet
.end()) {
219 Trace("theory::collectTerms") << "Theory::collectTerms: adding " << n
<< endl
;
221 if (n
.getKind() == kind::NOT
|| n
.getKind() == kind::EQUAL
|| !isLeaf(n
)) {
222 for(TNode::iterator child_it
= n
.begin(); child_it
!= n
.end(); ++child_it
) {
223 collectTerms(*child_it
, termSet
);
229 void Theory::computeRelevantTerms(set
<Node
>& termSet
) const
231 // Collect all terms appearing in assertions
232 context::CDList
<Assertion
>::const_iterator assert_it
= facts_begin(), assert_it_end
= facts_end();
233 for (; assert_it
!= assert_it_end
; ++assert_it
) {
234 collectTerms(*assert_it
, termSet
);
237 // Add terms that are shared terms
238 context::CDList
<TNode
>::const_iterator shared_it
= shared_terms_begin(), shared_it_end
= shared_terms_end();
239 for (; shared_it
!= shared_it_end
; ++shared_it
) {
240 collectTerms(*shared_it
, termSet
);
245 Theory::PPAssertStatus
Theory::ppAssert(TNode in
, SubstitutionMap
& outSubstitutions
)
247 if (in
.getKind() == kind::EQUAL
) {
248 // (and (= x t) phi) can be replaced by phi[x/t] if
249 // 1) x is a variable
250 // 2) x is not in the term t
251 // 3) x : T and t : S, then S <: T
252 if (in
[0].isVar() && !in
[1].hasSubterm(in
[0]) && (in
[1].getType()).isSubtypeOf(in
[0].getType()) ){
253 outSubstitutions
.addSubstitution(in
[0], in
[1]);
254 return PP_ASSERT_STATUS_SOLVED
;
256 if (in
[1].isVar() && !in
[0].hasSubterm(in
[1]) && (in
[0].getType()).isSubtypeOf(in
[1].getType())){
257 outSubstitutions
.addSubstitution(in
[1], in
[0]);
258 return PP_ASSERT_STATUS_SOLVED
;
260 if (in
[0].isConst() && in
[1].isConst()) {
261 if (in
[0] != in
[1]) {
262 return PP_ASSERT_STATUS_CONFLICT
;
267 return PP_ASSERT_STATUS_UNSOLVED
;
270 std::pair
<bool, Node
> Theory::entailmentCheck(TNode lit
,
271 const EntailmentCheckParameters
* params
,
272 EntailmentCheckSideEffects
* out
){
273 return make_pair(false, Node::null());
276 EntailmentCheckParameters::EntailmentCheckParameters(TheoryId tid
)
280 EntailmentCheckParameters::~EntailmentCheckParameters(){}
282 TheoryId
EntailmentCheckParameters::getTheoryId() const {
286 EntailmentCheckSideEffects::EntailmentCheckSideEffects(TheoryId tid
)
290 TheoryId
EntailmentCheckSideEffects::getTheoryId() const {
294 EntailmentCheckSideEffects::~EntailmentCheckSideEffects() {
297 }/* CVC4::theory namespace */
298 }/* CVC4 namespace */