Initial infrastructure for ExtTheory, generalize extended term handling in TheoryStri...
[cvc5.git] / src / theory / theory.cpp
1 /********************* */
2 /*! \file theory.cpp
3 ** \verbatim
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
11 **
12 ** \brief Base for theory interface.
13 **
14 ** Base for theory interface.
15 **/
16
17 #include "theory/theory.h"
18
19 #include <vector>
20 #include <sstream>
21 #include <iostream>
22 #include <string>
23
24 #include "base/cvc4_assert.h"
25 #include "smt/smt_statistics_registry.h"
26 #include "theory/substitutions.h"
27 #include "theory/quantifiers_engine.h"
28
29
30 using namespace std;
31
32 namespace CVC4 {
33 namespace theory {
34
35 /** Default value for the uninterpreted sorts is the UF theory */
36 TheoryId Theory::s_uninterpretedSortOwner = THEORY_UF;
37
38 std::ostream& operator<<(std::ostream& os, Theory::Effort level){
39 switch(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;
48 default:
49 Unreachable();
50 }
51 return os;
52 }/* ostream& operator<<(ostream&, Theory::Effort) */
53
54
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()
59 : d_id(id)
60 , d_instanceName(name)
61 , d_satContext(satContext)
62 , d_userContext(userContext)
63 , d_logicInfo(logicInfo)
64 , d_facts(satContext)
65 , d_factsHead(satContext, 0)
66 , d_sharedTermsIndex(satContext, 0)
67 , d_careGraph(NULL)
68 , d_quantEngine(NULL)
69 , d_extt(NULL)
70 , d_checkTime(getFullInstanceName() + "::checkTime")
71 , d_computeCareGraphTime(getFullInstanceName() + "::computeCareGraphTime")
72 , d_sharedTerms(satContext)
73 , d_out(&out)
74 , d_valuation(valuation)
75 , d_proofsEnabled(false)
76 {
77 smtStatisticsRegistry()->registerStat(&d_checkTime);
78 smtStatisticsRegistry()->registerStat(&d_computeCareGraphTime);
79 }
80
81 Theory::~Theory() {
82 smtStatisticsRegistry()->unregisterStat(&d_checkTime);
83 smtStatisticsRegistry()->unregisterStat(&d_computeCareGraphTime);
84 }
85
86 TheoryId Theory::theoryOf(TheoryOfMode mode, TNode node) {
87 TheoryId tid = THEORY_BUILTIN;
88 switch(mode) {
89 case THEORY_OF_TYPE_BASED:
90 // Constants, variables, 0-ary constructors
91 if (node.isVar() || node.isConst()) {
92 tid = Theory::theoryOf(node.getType());
93 } else if (node.getKind() == kind::EQUAL) {
94 // Equality is owned by the theory that owns the domain
95 tid = Theory::theoryOf(node[0].getType());
96 } else {
97 // Regular nodes are owned by the kind
98 tid = kindToTheoryId(node.getKind());
99 }
100 break;
101 case THEORY_OF_TERM_BASED:
102 // Variables
103 if (node.isVar()) {
104 if (Theory::theoryOf(node.getType()) != theory::THEORY_BOOL) {
105 // We treat the variables as uninterpreted
106 tid = s_uninterpretedSortOwner;
107 } else {
108 // Except for the Boolean ones, which we just ignore anyhow
109 tid = theory::THEORY_BOOL;
110 }
111 } else if (node.isConst()) {
112 // Constants go to the theory of the type
113 tid = Theory::theoryOf(node.getType());
114 } else if (node.getKind() == kind::EQUAL) { // Equality
115 // If one of them is an ITE, it's irelevant, since they will get replaced out anyhow
116 if (node[0].getKind() == kind::ITE) {
117 tid = Theory::theoryOf(node[0].getType());
118 } else if (node[1].getKind() == kind::ITE) {
119 tid = Theory::theoryOf(node[1].getType());
120 } else {
121 TNode l = node[0];
122 TNode r = node[1];
123 TypeNode ltype = l.getType();
124 TypeNode rtype = r.getType();
125 if( ltype != rtype ){
126 tid = Theory::theoryOf(l.getType());
127 }else {
128 // If both sides belong to the same theory the choice is easy
129 TheoryId T1 = Theory::theoryOf(l);
130 TheoryId T2 = Theory::theoryOf(r);
131 if (T1 == T2) {
132 tid = T1;
133 } else {
134 TheoryId T3 = Theory::theoryOf(ltype);
135 // This is a case of
136 // * x*y = f(z) -> UF
137 // * x = c -> UF
138 // * f(x) = read(a, y) -> either UF or ARRAY
139 // at least one of the theories has to be parametric, i.e. theory of the type is different
140 // from the theory of the term
141 if (T1 == T3) {
142 tid = T2;
143 } else if (T2 == T3) {
144 tid = T1;
145 } else {
146 // If both are parametric, we take the smaller one (arbitrary)
147 tid = T1 < T2 ? T1 : T2;
148 }
149 }
150 }
151 }
152 } else {
153 // Regular nodes are owned by the kind
154 tid = kindToTheoryId(node.getKind());
155 }
156 break;
157 default:
158 Unreachable();
159 }
160 Trace("theory::internal") << "theoryOf(" << mode << ", " << node << ") -> " << tid << std::endl;
161 return tid;
162 }
163
164 void Theory::addSharedTermInternal(TNode n) {
165 Debug("sharing") << "Theory::addSharedTerm<" << getId() << ">(" << n << ")" << endl;
166 Debug("theory::assertions") << "Theory::addSharedTerm<" << getId() << ">(" << n << ")" << endl;
167 d_sharedTerms.push_back(n);
168 addSharedTerm(n);
169 }
170
171 void Theory::computeCareGraph() {
172 Debug("sharing") << "Theory::computeCareGraph<" << getId() << ">()" << endl;
173 for (unsigned i = 0; i < d_sharedTerms.size(); ++ i) {
174 TNode a = d_sharedTerms[i];
175 TypeNode aType = a.getType();
176 for (unsigned j = i + 1; j < d_sharedTerms.size(); ++ j) {
177 TNode b = d_sharedTerms[j];
178 if (b.getType() != aType) {
179 // We don't care about the terms of different types
180 continue;
181 }
182 switch (d_valuation.getEqualityStatus(a, b)) {
183 case EQUALITY_TRUE_AND_PROPAGATED:
184 case EQUALITY_FALSE_AND_PROPAGATED:
185 // If we know about it, we should have propagated it, so we can skip
186 break;
187 default:
188 // Let's split on it
189 addCarePair(a, b);
190 break;
191 }
192 }
193 }
194 }
195
196 void Theory::printFacts(std::ostream& os) const {
197 unsigned i, n = d_facts.size();
198 for(i = 0; i < n; i++){
199 const Assertion& a_i = d_facts[i];
200 Node assertion = a_i;
201 os << d_id << '[' << i << ']' << " " << assertion << endl;
202 }
203 }
204
205 void Theory::debugPrintFacts() const{
206 DebugChannel.getStream() << "Theory::debugPrintFacts()" << endl;
207 printFacts(DebugChannel.getStream());
208 }
209
210 std::hash_set<TNode, TNodeHashFunction> Theory::currentlySharedTerms() const{
211 std::hash_set<TNode, TNodeHashFunction> currentlyShared;
212 for (shared_terms_iterator i = shared_terms_begin(),
213 i_end = shared_terms_end(); i != i_end; ++i) {
214 currentlyShared.insert (*i);
215 }
216 return currentlyShared;
217 }
218
219
220 void Theory::collectTerms(TNode n, set<Node>& termSet) const
221 {
222 if (termSet.find(n) != termSet.end()) {
223 return;
224 }
225 Trace("theory::collectTerms") << "Theory::collectTerms: adding " << n << endl;
226 termSet.insert(n);
227 if (n.getKind() == kind::NOT || n.getKind() == kind::EQUAL || !isLeaf(n)) {
228 for(TNode::iterator child_it = n.begin(); child_it != n.end(); ++child_it) {
229 collectTerms(*child_it, termSet);
230 }
231 }
232 }
233
234
235 void Theory::computeRelevantTerms(set<Node>& termSet, bool includeShared) const
236 {
237 // Collect all terms appearing in assertions
238 context::CDList<Assertion>::const_iterator assert_it = facts_begin(), assert_it_end = facts_end();
239 for (; assert_it != assert_it_end; ++assert_it) {
240 collectTerms(*assert_it, termSet);
241 }
242
243 if (!includeShared) return;
244
245 // Add terms that are shared terms
246 context::CDList<TNode>::const_iterator shared_it = shared_terms_begin(), shared_it_end = shared_terms_end();
247 for (; shared_it != shared_it_end; ++shared_it) {
248 collectTerms(*shared_it, termSet);
249 }
250 }
251
252
253 Theory::PPAssertStatus Theory::ppAssert(TNode in,
254 SubstitutionMap& outSubstitutions)
255 {
256 if (in.getKind() == kind::EQUAL) {
257 // (and (= x t) phi) can be replaced by phi[x/t] if
258 // 1) x is a variable
259 // 2) x is not in the term t
260 // 3) x : T and t : S, then S <: T
261 if (in[0].isVar() && !in[1].hasSubterm(in[0]) &&
262 (in[1].getType()).isSubtypeOf(in[0].getType()) ){
263 outSubstitutions.addSubstitution(in[0], in[1]);
264 return PP_ASSERT_STATUS_SOLVED;
265 }
266 if (in[1].isVar() && !in[0].hasSubterm(in[1]) &&
267 (in[0].getType()).isSubtypeOf(in[1].getType())){
268 outSubstitutions.addSubstitution(in[1], in[0]);
269 return PP_ASSERT_STATUS_SOLVED;
270 }
271 if (in[0].isConst() && in[1].isConst()) {
272 if (in[0] != in[1]) {
273 return PP_ASSERT_STATUS_CONFLICT;
274 }
275 }
276 }
277
278 return PP_ASSERT_STATUS_UNSOLVED;
279 }
280
281 std::pair<bool, Node> Theory::entailmentCheck(
282 TNode lit,
283 const EntailmentCheckParameters* params,
284 EntailmentCheckSideEffects* out) {
285 return make_pair(false, Node::null());
286 }
287
288 EntailmentCheckParameters::EntailmentCheckParameters(TheoryId tid)
289 : d_tid(tid) {
290 }
291
292 std::string Theory::getFullInstanceName() const {
293 std::stringstream ss;
294 ss << "theory<" << d_id << ">" << d_instanceName;
295 return ss.str();
296 }
297
298 EntailmentCheckParameters::~EntailmentCheckParameters(){}
299
300 TheoryId EntailmentCheckParameters::getTheoryId() const {
301 return d_tid;
302 }
303
304 EntailmentCheckSideEffects::EntailmentCheckSideEffects(TheoryId tid)
305 : d_tid(tid)
306 {}
307
308 TheoryId EntailmentCheckSideEffects::getTheoryId() const {
309 return d_tid;
310 }
311
312 EntailmentCheckSideEffects::~EntailmentCheckSideEffects() {
313 }
314
315
316 ExtTheory::ExtTheory( Theory * p ) : d_parent( p ),
317 d_ext_func_terms( p->getSatContext() ), d_has_extf( p->getSatContext() ){
318
319 }
320
321 void ExtTheory::collectVars( Node n, std::vector< Node >& vars, std::map< Node, bool >& visited ) {
322 if( !n.isConst() ){
323 if( visited.find( n )==visited.end() ){
324 visited[n] = true;
325 if( n.getNumChildren()>0 ){
326 for( unsigned i=0; i<n.getNumChildren(); i++ ){
327 collectVars( n[i], vars, visited );
328 }
329 }else{
330 vars.push_back( n );
331 }
332 }
333 }
334 }
335
336 //do inferences
337 void ExtTheory::getInferences( int effort, std::vector< Node >& terms, std::vector< Node >& sterms, std::vector< std::vector< Node > >& exp ) {
338 //all variables we need to find a substitution for
339 std::vector< Node > vars;
340 std::vector< Node > sub;
341 std::map< Node, std::vector< Node > > expc;
342 Trace("extt-debug") << "Checking " << d_ext_func_terms.size() << " extended functions." << std::endl;
343 for( NodeBoolMap::iterator it = d_ext_func_terms.begin(); it != d_ext_func_terms.end(); ++it ){
344 //if not already reduced
345 if( (*it).second ){
346 Node n = (*it).first;
347 terms.push_back( n );
348 std::map< Node, ExtfInfo >::iterator iti = d_extf_info.find( n );
349 Assert( iti!=d_extf_info.end() );
350 for( unsigned i=0; i<iti->second.d_vars.size(); i++ ){
351 if( std::find( vars.begin(), vars.end(), iti->second.d_vars[i] )==vars.end() ){
352 vars.push_back( iti->second.d_vars[i] );
353 }
354 }
355 }
356 }
357 Trace("extt-debug") << "..." << terms.size() << " unreduced." << std::endl;
358 if( !terms.empty() ){
359 //get the current substitution
360 if( d_parent->getCurrentSubstitution( effort, vars, sub, expc ) ){
361 for( unsigned i=0; i<terms.size(); i++ ){
362 //do substitution, rewrite
363 Node n = terms[i];
364 Node ns = n.substitute( vars.begin(), vars.end(), sub.begin(), sub.end() );
365 std::vector< Node > expn;
366 if( ns!=n ){
367 //build explanation: explanation vars = sub for each vars in FV( n )
368 std::map< Node, ExtfInfo >::iterator iti = d_extf_info.find( n );
369 Assert( iti!=d_extf_info.end() );
370 for( unsigned j=0; j<iti->second.d_vars.size(); j++ ){
371 Node v = iti->second.d_vars[j];
372 std::map< Node, std::vector< Node > >::iterator itx = expc.find( v );
373 if( itx!=expc.end() ){
374 for( unsigned k=0; k<itx->second.size(); k++ ){
375 if( std::find( expn.begin(), expn.end(), itx->second[k] )==expn.end() ){
376 expn.push_back( itx->second[k] );
377 }
378 }
379 }
380 }
381 }
382 Trace("extt-debug") << " have " << n << " == " << ns << ", exp size=" << expn.size() << "." << std::endl;
383 //add to vector
384 sterms.push_back( ns );
385 exp.push_back( expn );
386 }
387 }else{
388 for( unsigned i=0; i<terms.size(); i++ ){
389 sterms.push_back( terms[i] );
390 }
391 }
392 }
393 }
394
395 //register term
396 void ExtTheory::registerTerm( Node n ) {
397 if( d_extf_kind.find( n.getKind() )!=d_extf_kind.end() ){
398 if( d_ext_func_terms.find( n )==d_ext_func_terms.end() ){
399 Trace("extt-debug") << "Found extended function : " << n << " in " << d_parent->getId() << std::endl;
400 d_ext_func_terms[n] = true;
401 d_has_extf = true;
402 std::map< Node, bool > visited;
403 collectVars( n, d_extf_info[n].d_vars, visited );
404 }
405 }
406 }
407
408 //mark reduced
409 void ExtTheory::markReduced( Node n ) {
410 d_ext_func_terms[n] = false;
411 //TODO update has_extf
412 }
413
414 //mark congruent
415 void ExtTheory::markCongruent( Node a, Node b ) {
416 NodeBoolMap::const_iterator it = d_ext_func_terms.find( b );
417 if( it!=d_ext_func_terms.end() ){
418 if( d_ext_func_terms.find( a )==d_ext_func_terms.end() ){
419 d_ext_func_terms[a] = (*it).second;
420 }else{
421 d_ext_func_terms[a] = d_ext_func_terms[a] && (*it).second;
422 }
423 d_ext_func_terms[b] = false;
424 }
425 }
426
427 //is active
428 bool ExtTheory::isActive( Node n ) {
429 NodeBoolMap::const_iterator it = d_ext_func_terms.find( n );
430 if( it!=d_ext_func_terms.end() ){
431 return (*it).second;
432 }else{
433 return false;
434 }
435 }
436 //get active
437 void ExtTheory::getActive( std::vector< Node >& active ) {
438 for( NodeBoolMap::iterator it = d_ext_func_terms.begin(); it != d_ext_func_terms.end(); ++it ){
439 //if not already reduced
440 if( (*it).second ){
441 active.push_back( (*it).first );
442 }
443 }
444 }
445
446 void ExtTheory::getActive( std::vector< Node >& active, Kind k ) {
447 for( NodeBoolMap::iterator it = d_ext_func_terms.begin(); it != d_ext_func_terms.end(); ++it ){
448 //if not already reduced
449 if( (*it).second && (*it).first.getKind()==k ){
450 active.push_back( (*it).first );
451 }
452 }
453 }
454
455 }/* CVC4::theory namespace */
456 }/* CVC4 namespace */