1 /********************* */
4 ** Top contributors (to current version):
5 ** Tim King, Dejan Jovanovic, Clark Barrett
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2016 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/cvc4_assert.h"
25 #include "smt/smt_statistics_registry.h"
26 #include "theory/substitutions.h"
27 #include "theory/quantifiers_engine.h"
35 /** Default value for the uninterpreted sorts is the UF theory */
36 TheoryId
Theory::s_uninterpretedSortOwner
= THEORY_UF
;
38 std::ostream
& operator<<(std::ostream
& os
, Theory::Effort level
){
40 case Theory::EFFORT_STANDARD
:
41 os
<< "EFFORT_STANDARD"; break;
42 case Theory::EFFORT_FULL
:
43 os
<< "EFFORT_FULL"; break;
44 case Theory::EFFORT_COMBINATION
:
45 os
<< "EFFORT_COMBINATION"; break;
46 case Theory::EFFORT_LAST_CALL
:
47 os
<< "EFFORT_LAST_CALL"; break;
52 }/* ostream& operator<<(ostream&, Theory::Effort) */
55 Theory::Theory(TheoryId id
, context::Context
* satContext
,
56 context::UserContext
* userContext
, OutputChannel
& out
,
57 Valuation valuation
, const LogicInfo
& logicInfo
,
58 std::string name
) throw()
60 , d_instanceName(name
)
61 , d_satContext(satContext
)
62 , d_userContext(userContext
)
63 , d_logicInfo(logicInfo
)
65 , d_factsHead(satContext
, 0)
66 , d_sharedTermsIndex(satContext
, 0)
70 , d_checkTime(getFullInstanceName() + "::checkTime")
71 , d_computeCareGraphTime(getFullInstanceName() + "::computeCareGraphTime")
72 , d_sharedTerms(satContext
)
74 , d_valuation(valuation
)
75 , d_proofsEnabled(false)
77 smtStatisticsRegistry()->registerStat(&d_checkTime
);
78 smtStatisticsRegistry()->registerStat(&d_computeCareGraphTime
);
82 smtStatisticsRegistry()->unregisterStat(&d_checkTime
);
83 smtStatisticsRegistry()->unregisterStat(&d_computeCareGraphTime
);
88 TheoryId
Theory::theoryOf(TheoryOfMode mode
, TNode node
) {
89 TheoryId tid
= THEORY_BUILTIN
;
91 case THEORY_OF_TYPE_BASED
:
92 // Constants, variables, 0-ary constructors
94 if( node
.getKind() == kind::BOOLEAN_TERM_VARIABLE
){
97 tid
= Theory::theoryOf(node
.getType());
99 }else if (node
.isConst()) {
100 tid
= Theory::theoryOf(node
.getType());
101 } else if (node
.getKind() == kind::EQUAL
) {
102 // Equality is owned by the theory that owns the domain
103 tid
= Theory::theoryOf(node
[0].getType());
105 // Regular nodes are owned by the kind
106 tid
= kindToTheoryId(node
.getKind());
109 case THEORY_OF_TERM_BASED
:
112 if (Theory::theoryOf(node
.getType()) != theory::THEORY_BOOL
) {
113 // We treat the variables as uninterpreted
114 tid
= s_uninterpretedSortOwner
;
116 if( node
.getKind() == kind::BOOLEAN_TERM_VARIABLE
){
117 //Boolean vars go to UF
120 // Except for the Boolean ones
124 } else if (node
.isConst()) {
125 // Constants go to the theory of the type
126 tid
= Theory::theoryOf(node
.getType());
127 } else if (node
.getKind() == kind::EQUAL
) { // Equality
128 // If one of them is an ITE, it's irelevant, since they will get replaced out anyhow
129 if (node
[0].getKind() == kind::ITE
) {
130 tid
= Theory::theoryOf(node
[0].getType());
131 } else if (node
[1].getKind() == kind::ITE
) {
132 tid
= Theory::theoryOf(node
[1].getType());
136 TypeNode ltype
= l
.getType();
137 TypeNode rtype
= r
.getType();
138 if( ltype
!= rtype
){
139 tid
= Theory::theoryOf(l
.getType());
141 // If both sides belong to the same theory the choice is easy
142 TheoryId T1
= Theory::theoryOf(l
);
143 TheoryId T2
= Theory::theoryOf(r
);
147 TheoryId T3
= Theory::theoryOf(ltype
);
149 // * x*y = f(z) -> UF
151 // * f(x) = read(a, y) -> either UF or ARRAY
152 // at least one of the theories has to be parametric, i.e. theory of the type is different
153 // from the theory of the term
156 } else if (T2
== T3
) {
159 // If both are parametric, we take the smaller one (arbitrary)
160 tid
= T1
< T2
? T1
: T2
;
166 // Regular nodes are owned by the kind
167 tid
= kindToTheoryId(node
.getKind());
173 Trace("theory::internal") << "theoryOf(" << mode
<< ", " << node
<< ") -> " << tid
<< std::endl
;
177 void Theory::addSharedTermInternal(TNode n
) {
178 Debug("sharing") << "Theory::addSharedTerm<" << getId() << ">(" << n
<< ")" << endl
;
179 Debug("theory::assertions") << "Theory::addSharedTerm<" << getId() << ">(" << n
<< ")" << endl
;
180 d_sharedTerms
.push_back(n
);
184 void Theory::computeCareGraph() {
185 Debug("sharing") << "Theory::computeCareGraph<" << getId() << ">()" << endl
;
186 for (unsigned i
= 0; i
< d_sharedTerms
.size(); ++ i
) {
187 TNode a
= d_sharedTerms
[i
];
188 TypeNode aType
= a
.getType();
189 for (unsigned j
= i
+ 1; j
< d_sharedTerms
.size(); ++ j
) {
190 TNode b
= d_sharedTerms
[j
];
191 if (b
.getType() != aType
) {
192 // We don't care about the terms of different types
195 switch (d_valuation
.getEqualityStatus(a
, b
)) {
196 case EQUALITY_TRUE_AND_PROPAGATED
:
197 case EQUALITY_FALSE_AND_PROPAGATED
:
198 // If we know about it, we should have propagated it, so we can skip
209 void Theory::printFacts(std::ostream
& os
) const {
210 unsigned i
, n
= d_facts
.size();
211 for(i
= 0; i
< n
; i
++){
212 const Assertion
& a_i
= d_facts
[i
];
213 Node assertion
= a_i
;
214 os
<< d_id
<< '[' << i
<< ']' << " " << assertion
<< endl
;
218 void Theory::debugPrintFacts() const{
219 DebugChannel
.getStream() << "Theory::debugPrintFacts()" << endl
;
220 printFacts(DebugChannel
.getStream());
223 std::hash_set
<TNode
, TNodeHashFunction
> Theory::currentlySharedTerms() const{
224 std::hash_set
<TNode
, TNodeHashFunction
> currentlyShared
;
225 for (shared_terms_iterator i
= shared_terms_begin(),
226 i_end
= shared_terms_end(); i
!= i_end
; ++i
) {
227 currentlyShared
.insert (*i
);
229 return currentlyShared
;
233 void Theory::collectTerms(TNode n
, set
<Node
>& termSet
) const
235 if (termSet
.find(n
) != termSet
.end()) {
238 Trace("theory::collectTerms") << "Theory::collectTerms: adding " << n
<< endl
;
240 if (n
.getKind() == kind::NOT
|| n
.getKind() == kind::EQUAL
|| !isLeaf(n
)) {
241 for(TNode::iterator child_it
= n
.begin(); child_it
!= n
.end(); ++child_it
) {
242 collectTerms(*child_it
, termSet
);
248 void Theory::computeRelevantTerms(set
<Node
>& termSet
, bool includeShared
) const
250 // Collect all terms appearing in assertions
251 context::CDList
<Assertion
>::const_iterator assert_it
= facts_begin(), assert_it_end
= facts_end();
252 for (; assert_it
!= assert_it_end
; ++assert_it
) {
253 collectTerms(*assert_it
, termSet
);
256 if (!includeShared
) return;
258 // Add terms that are shared terms
259 context::CDList
<TNode
>::const_iterator shared_it
= shared_terms_begin(), shared_it_end
= shared_terms_end();
260 for (; shared_it
!= shared_it_end
; ++shared_it
) {
261 collectTerms(*shared_it
, termSet
);
266 Theory::PPAssertStatus
Theory::ppAssert(TNode in
,
267 SubstitutionMap
& outSubstitutions
)
269 if (in
.getKind() == kind::EQUAL
) {
270 // (and (= x t) phi) can be replaced by phi[x/t] if
271 // 1) x is a variable
272 // 2) x is not in the term t
273 // 3) x : T and t : S, then S <: T
274 if (in
[0].isVar() && !in
[1].hasSubterm(in
[0]) &&
275 (in
[1].getType()).isSubtypeOf(in
[0].getType()) ){
276 outSubstitutions
.addSubstitution(in
[0], in
[1]);
277 return PP_ASSERT_STATUS_SOLVED
;
279 if (in
[1].isVar() && !in
[0].hasSubterm(in
[1]) &&
280 (in
[0].getType()).isSubtypeOf(in
[1].getType())){
281 outSubstitutions
.addSubstitution(in
[1], in
[0]);
282 return PP_ASSERT_STATUS_SOLVED
;
284 if (in
[0].isConst() && in
[1].isConst()) {
285 if (in
[0] != in
[1]) {
286 return PP_ASSERT_STATUS_CONFLICT
;
291 return PP_ASSERT_STATUS_UNSOLVED
;
294 std::pair
<bool, Node
> Theory::entailmentCheck(
296 const EntailmentCheckParameters
* params
,
297 EntailmentCheckSideEffects
* out
) {
298 return make_pair(false, Node::null());
301 ExtTheory
* Theory::getExtTheory() {
302 Assert(d_extTheory
!= NULL
);
306 void Theory::addCarePair(TNode t1
, TNode t2
) {
308 d_careGraph
->insert(CarePair(t1
, t2
, d_id
));
312 void Theory::getCareGraph(CareGraph
* careGraph
) {
313 Assert(careGraph
!= NULL
);
315 Trace("sharing") << "Theory<" << getId() << ">::getCareGraph()" << std::endl
;
316 TimerStat::CodeTimer
computeCareGraphTime(d_computeCareGraphTime
);
317 d_careGraph
= careGraph
;
322 void Theory::setQuantifiersEngine(QuantifiersEngine
* qe
) {
323 Assert(d_quantEngine
== NULL
);
328 void Theory::setupExtTheory() {
329 Assert(d_extTheory
== NULL
);
330 d_extTheory
= new ExtTheory(this);
334 EntailmentCheckParameters::EntailmentCheckParameters(TheoryId tid
)
338 std::string
Theory::getFullInstanceName() const {
339 std::stringstream ss
;
340 ss
<< "theory<" << d_id
<< ">" << d_instanceName
;
344 EntailmentCheckParameters::~EntailmentCheckParameters(){}
346 TheoryId
EntailmentCheckParameters::getTheoryId() const {
350 EntailmentCheckSideEffects::EntailmentCheckSideEffects(TheoryId tid
)
354 TheoryId
EntailmentCheckSideEffects::getTheoryId() const {
358 EntailmentCheckSideEffects::~EntailmentCheckSideEffects() {
362 ExtTheory::ExtTheory( Theory
* p
) : d_parent( p
),
363 d_ext_func_terms( p
->getSatContext() ), d_ci_inactive( p
->getUserContext() ),
364 d_lemmas( p
->getUserContext() ), d_pp_lemmas( p
->getUserContext() ), d_has_extf( p
->getSatContext() ){
365 d_true
= NodeManager::currentNM()->mkConst( true );
368 //gets all leaf terms in n
369 void ExtTheory::collectVars( Node n
, std::vector
< Node
>& vars
, std::map
< Node
, bool >& visited
) {
371 if( visited
.find( n
)==visited
.end() ){
373 //treat terms not belonging to this theory as leaf
374 // AJR TODO : should include terms not belonging to this theory (commented below)
375 if( n
.getNumChildren()>0 ){//&& Theory::theoryOf(n)==d_parent->getId() ){
376 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
377 collectVars( n
[i
], vars
, visited
);
387 void ExtTheory::getSubstitutedTerms( int effort
, std::vector
< Node
>& terms
, std::vector
< Node
>& sterms
, std::vector
< std::vector
< Node
> >& exp
) {
388 Trace("extt-debug") << "Currently " << d_ext_func_terms
.size() << " extended functions." << std::endl
;
389 Trace("extt-debug") << "..." << terms
.size() << " to reduce." << std::endl
;
390 if( !terms
.empty() ){
391 //all variables we need to find a substitution for
392 std::vector
< Node
> vars
;
393 std::vector
< Node
> sub
;
394 std::map
< Node
, std::vector
< Node
> > expc
;
395 for( unsigned i
=0; i
<terms
.size(); i
++ ){
396 //do substitution, rewrite
398 std::map
< Node
, ExtfInfo
>::iterator iti
= d_extf_info
.find( n
);
399 Trace("extt-debug") << "Check extf : " << n
<< std::endl
;
400 Assert( iti
!=d_extf_info
.end() );
401 for( unsigned i
=0; i
<iti
->second
.d_vars
.size(); i
++ ){
402 if( std::find( vars
.begin(), vars
.end(), iti
->second
.d_vars
[i
] )==vars
.end() ){
403 vars
.push_back( iti
->second
.d_vars
[i
] );
407 //get the current substitution for all variables
408 if( d_parent
->getCurrentSubstitution( effort
, vars
, sub
, expc
) ){
409 Assert( vars
.size()==sub
.size() );
410 for( unsigned i
=0; i
<terms
.size(); i
++ ){
413 Node ns
= n
.substitute( vars
.begin(), vars
.end(), sub
.begin(), sub
.end() );
414 std::vector
< Node
> expn
;
416 //build explanation: explanation vars = sub for each vars in FV( n )
417 std::map
< Node
, ExtfInfo
>::iterator iti
= d_extf_info
.find( n
);
418 Assert( iti
!=d_extf_info
.end() );
419 for( unsigned j
=0; j
<iti
->second
.d_vars
.size(); j
++ ){
420 Node v
= iti
->second
.d_vars
[j
];
421 std::map
< Node
, std::vector
< Node
> >::iterator itx
= expc
.find( v
);
422 if( itx
!=expc
.end() ){
423 for( unsigned k
=0; k
<itx
->second
.size(); k
++ ){
424 if( std::find( expn
.begin(), expn
.end(), itx
->second
[k
] )==expn
.end() ){
425 expn
.push_back( itx
->second
[k
] );
431 Trace("extt-debug") << " have " << n
<< " == " << ns
<< ", exp size=" << expn
.size() << "." << std::endl
;
433 sterms
.push_back( ns
);
434 exp
.push_back( expn
);
437 for( unsigned i
=0; i
<terms
.size(); i
++ ){
438 sterms
.push_back( terms
[i
] );
444 bool ExtTheory::doInferencesInternal( int effort
, std::vector
< Node
>& terms
, std::vector
< Node
>& nred
, bool batch
, bool isRed
) {
446 bool addedLemma
= false;
448 for( unsigned i
=0; i
<terms
.size(); i
++ ){
451 //TODO: reduction with substitution?
452 int ret
= d_parent
->getReduction( effort
, n
, nr
);
456 if( !nr
.isNull() && n
!=nr
){
457 Node lem
= NodeManager::currentNM()->mkNode( kind::EQUAL
, n
, nr
);
458 if( sendLemma( lem
, true ) ){
459 Trace("extt-lemma") << "ExtTheory : Reduction lemma : " << lem
<< std::endl
;
463 markReduced( terms
[i
], ret
<0 );
467 std::vector
< Node
> sterms
;
468 std::vector
< std::vector
< Node
> > exp
;
469 getSubstitutedTerms( effort
, terms
, sterms
, exp
);
470 for( unsigned i
=0; i
<terms
.size(); i
++ ){
471 bool processed
= false;
472 if( sterms
[i
]!=terms
[i
] ){
473 Node sr
= Rewriter::rewrite( sterms
[i
] );
476 markReduced( terms
[i
] );
477 Node eq
= terms
[i
].eqNode( sr
);
478 Node expn
= exp
[i
].size()>1 ? NodeManager::currentNM()->mkNode( kind::AND
, exp
[i
] ) : ( exp
[i
].size()==1 ? exp
[i
][0] : d_true
);
479 Trace("extt-debug") << "ExtTheory::doInferences : infer : " << eq
<< " by " << expn
<< std::endl
;
480 Node lem
= NodeManager::currentNM()->mkNode( kind::IMPLIES
, expn
, eq
);
481 Trace("extt-debug") << "...send lemma " << lem
<< std::endl
;
482 if( sendLemma( lem
) ){
483 Trace("extt-lemma") << "ExtTheory : Constant rewrite lemma : " << lem
<< std::endl
;
489 nred
.push_back( terms
[i
] );
495 std::vector
< Node
> nnred
;
497 for( NodeBoolMap::iterator it
= d_ext_func_terms
.begin(); it
!= d_ext_func_terms
.end(); ++it
){
498 if( (*it
).second
&& !isContextIndependentInactive( (*it
).first
) ){
499 std::vector
< Node
> nterms
;
500 nterms
.push_back( (*it
).first
);
501 if( doInferencesInternal( effort
, nterms
, nnred
, true, isRed
) ){
507 for( unsigned i
=0; i
<terms
.size(); i
++ ){
508 std::vector
< Node
> nterms
;
509 nterms
.push_back( terms
[i
] );
510 if( doInferencesInternal( effort
, nterms
, nnred
, true, isRed
) ){
519 bool ExtTheory::sendLemma( Node lem
, bool preprocess
) {
521 if( d_pp_lemmas
.find( lem
)==d_pp_lemmas
.end() ){
522 d_pp_lemmas
.insert( lem
);
523 d_parent
->getOutputChannel().lemma( lem
, false, true );
527 if( d_lemmas
.find( lem
)==d_lemmas
.end() ){
528 d_lemmas
.insert( lem
);
529 d_parent
->getOutputChannel().lemma( lem
);
536 bool ExtTheory::doInferences( int effort
, std::vector
< Node
>& terms
, std::vector
< Node
>& nred
, bool batch
) {
537 if( !terms
.empty() ){
538 return doInferencesInternal( effort
, terms
, nred
, batch
, false );
544 bool ExtTheory::doInferences( int effort
, std::vector
< Node
>& nred
, bool batch
) {
545 std::vector
< Node
> terms
;
547 return doInferencesInternal( effort
, terms
, nred
, batch
, false );
550 bool ExtTheory::doReductions( int effort
, std::vector
< Node
>& terms
, std::vector
< Node
>& nred
, bool batch
) {
551 if( !terms
.empty() ){
552 return doInferencesInternal( effort
, terms
, nred
, batch
, true );
558 bool ExtTheory::doReductions( int effort
, std::vector
< Node
>& nred
, bool batch
) {
559 std::vector
< Node
> terms
;
561 return doInferencesInternal( effort
, terms
, nred
, batch
, true );
566 void ExtTheory::registerTerm( Node n
) {
567 if( d_extf_kind
.find( n
.getKind() )!=d_extf_kind
.end() ){
568 if( d_ext_func_terms
.find( n
)==d_ext_func_terms
.end() ){
569 Trace("extt-debug") << "Found extended function : " << n
<< " in " << d_parent
->getId() << std::endl
;
570 d_ext_func_terms
[n
] = true;
572 std::map
< Node
, bool > visited
;
573 collectVars( n
, d_extf_info
[n
].d_vars
, visited
);
578 void ExtTheory::registerTermRec( Node n
) {
579 std::map
< Node
, bool > visited
;
580 registerTermRec( n
, visited
);
583 void ExtTheory::registerTermRec( Node n
, std::map
< Node
, bool >& visited
) {
584 if( visited
.find( n
)==visited
.end() ){
587 for( unsigned i
=0; i
<n
.getNumChildren(); i
++ ){
588 registerTermRec( n
[i
], visited
);
594 void ExtTheory::markReduced( Node n
, bool contextDepend
) {
596 Assert( d_ext_func_terms
.find( n
)!=d_ext_func_terms
.end() );
597 d_ext_func_terms
[n
] = false;
598 if( !contextDepend
){
599 d_ci_inactive
.insert( n
);
603 if( d_has_extf
.get()==n
){
604 for( NodeBoolMap::iterator it
= d_ext_func_terms
.begin(); it
!= d_ext_func_terms
.end(); ++it
){
605 //if not already reduced
606 if( (*it
).second
&& !isContextIndependentInactive( (*it
).first
) ){
607 d_has_extf
= (*it
).first
;
615 void ExtTheory::markCongruent( Node a
, Node b
) {
616 Trace("extt-debug") << "Mark congruent : " << a
<< " " << b
<< std::endl
;
619 NodeBoolMap::const_iterator it
= d_ext_func_terms
.find( b
);
620 if( it
!=d_ext_func_terms
.end() ){
621 if( d_ext_func_terms
.find( a
)!=d_ext_func_terms
.end() ){
622 d_ext_func_terms
[a
] = d_ext_func_terms
[a
] && (*it
).second
;
626 d_ext_func_terms
[b
] = false;
632 bool ExtTheory::isContextIndependentInactive( Node n
) {
633 return d_ci_inactive
.find( n
)!=d_ci_inactive
.end();
636 bool ExtTheory::hasActiveTerm() {
637 return !d_has_extf
.get().isNull();
641 bool ExtTheory::isActive( Node n
) {
642 NodeBoolMap::const_iterator it
= d_ext_func_terms
.find( n
);
643 if( it
!=d_ext_func_terms
.end() ){
644 return (*it
).second
&& !isContextIndependentInactive( n
);
650 void ExtTheory::getActive( std::vector
< Node
>& active
) {
651 for( NodeBoolMap::iterator it
= d_ext_func_terms
.begin(); it
!= d_ext_func_terms
.end(); ++it
){
652 //if not already reduced
653 if( (*it
).second
&& !isContextIndependentInactive( (*it
).first
) ){
654 active
.push_back( (*it
).first
);
659 void ExtTheory::getActive( std::vector
< Node
>& active
, Kind k
) {
660 for( NodeBoolMap::iterator it
= d_ext_func_terms
.begin(); it
!= d_ext_func_terms
.end(); ++it
){
661 //if not already reduced
662 if( (*it
).first
.getKind()==k
&& (*it
).second
&& !isContextIndependentInactive( (*it
).first
) ){
663 active
.push_back( (*it
).first
);
668 }/* CVC4::theory namespace */
669 }/* CVC4 namespace */