Merge branch '1.4.x'
[cvc5.git] / src / theory / quantifiers / theory_quantifiers.cpp
1 /********************* */
2 /*! \file theory_quantifiers.cpp
3 ** \verbatim
4 ** Original author: Morgan Deters
5 ** Major contributors: Andrew Reynolds
6 ** Minor contributors (to current version): Dejan Jovanovic
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
11 **
12 ** \brief Implementation of the theory of quantifiers
13 **
14 ** Implementation of the theory of quantifiers.
15 **/
16
17
18 #include "theory/quantifiers/theory_quantifiers.h"
19 #include "theory/valuation.h"
20 #include "theory/quantifiers_engine.h"
21 #include "theory/quantifiers/instantiation_engine.h"
22 #include "theory/quantifiers/model_engine.h"
23 #include "expr/kind.h"
24 #include "util/cvc4_assert.h"
25 #include "theory/quantifiers/options.h"
26 #include "theory/quantifiers/term_database.h"
27 #include "theory/quantifiers/quantifiers_attributes.h"
28
29 using namespace std;
30 using namespace CVC4;
31 using namespace CVC4::kind;
32 using namespace CVC4::context;
33 using namespace CVC4::theory;
34 using namespace CVC4::theory::quantifiers;
35
36 TheoryQuantifiers::TheoryQuantifiers(Context* c, context::UserContext* u, OutputChannel& out, Valuation valuation, const LogicInfo& logicInfo) :
37 Theory(THEORY_QUANTIFIERS, c, u, out, valuation, logicInfo),
38 d_numRestarts(0),
39 d_masterEqualityEngine(0)
40 {
41 d_numInstantiations = 0;
42 d_baseDecLevel = -1;
43 out.handleUserAttribute( "axiom", this );
44 out.handleUserAttribute( "conjecture", this );
45 out.handleUserAttribute( "inst-level", this );
46 out.handleUserAttribute( "rr-priority", this );
47 }
48
49 TheoryQuantifiers::~TheoryQuantifiers() {
50 }
51
52 void TheoryQuantifiers::setMasterEqualityEngine(eq::EqualityEngine* eq) {
53 d_masterEqualityEngine = eq;
54 }
55
56 void TheoryQuantifiers::addSharedTerm(TNode t) {
57 Debug("quantifiers-other") << "TheoryQuantifiers::addSharedTerm(): "
58 << t << endl;
59 }
60
61
62 void TheoryQuantifiers::notifyEq(TNode lhs, TNode rhs) {
63 Debug("quantifiers-other") << "TheoryQuantifiers::notifyEq(): "
64 << lhs << " = " << rhs << endl;
65
66 }
67
68 void TheoryQuantifiers::preRegisterTerm(TNode n) {
69 Debug("quantifiers-prereg") << "TheoryQuantifiers::preRegisterTerm() " << n << endl;
70 if( n.getKind()==FORALL && !TermDb::hasInstConstAttr(n) ){
71 getQuantifiersEngine()->registerQuantifier( n );
72 }
73 }
74
75
76 void TheoryQuantifiers::presolve() {
77 Debug("quantifiers-presolve") << "TheoryQuantifiers::presolve()" << endl;
78 }
79
80 Node TheoryQuantifiers::getValue(TNode n) {
81 //NodeManager* nodeManager = NodeManager::currentNM();
82 switch(n.getKind()) {
83 case FORALL:
84 case EXISTS:
85 bool value;
86 if( d_valuation.hasSatValue( n, value ) ){
87 return NodeManager::currentNM()->mkConst(value);
88 }else{
89 return NodeManager::currentNM()->mkConst(false); //FIX_THIS?
90 }
91 break;
92 default:
93 Unhandled(n.getKind());
94 }
95 }
96
97 void TheoryQuantifiers::collectModelInfo(TheoryModel* m, bool fullModel) {
98 if(fullModel) {
99 for(assertions_iterator i = facts_begin(); i != facts_end(); ++i) {
100 if((*i).assertion.getKind() == kind::NOT) {
101 Debug("quantifiers::collectModelInfo") << "got quant FALSE: " << (*i).assertion[0] << endl;
102 m->assertPredicate((*i).assertion[0], false);
103 } else {
104 Debug("quantifiers::collectModelInfo") << "got quant TRUE : " << *i << endl;
105 m->assertPredicate(*i, true);
106 }
107 }
108 }
109 }
110
111 void TheoryQuantifiers::check(Effort e) {
112 if (done() && !fullEffort(e)) {
113 return;
114 }
115
116 CodeTimer codeTimer(d_theoryTime);
117
118 Trace("quantifiers-check") << "quantifiers::check(" << e << ")" << std::endl;
119 while(!done()) {
120 Node assertion = get();
121 Trace("quantifiers-assert") << "quantifiers::assert(): " << assertion << std::endl;
122 switch(assertion.getKind()) {
123 case kind::FORALL:
124 assertUniversal( assertion );
125 break;
126 case kind::NOT:
127 {
128 switch( assertion[0].getKind()) {
129 case kind::FORALL:
130 assertExistential( assertion );
131 break;
132 default:
133 Unhandled(assertion[0].getKind());
134 break;
135 }
136 }
137 break;
138 default:
139 Unhandled(assertion.getKind());
140 break;
141 }
142 }
143 // call the quantifiers engine to check
144 getQuantifiersEngine()->check( e );
145 }
146
147 void TheoryQuantifiers::propagate(Effort level){
148 //CodeTimer codeTimer(d_theoryTime);
149 //getQuantifiersEngine()->propagate( level );
150 }
151
152 Node TheoryQuantifiers::getNextDecisionRequest(){
153 return getQuantifiersEngine()->getNextDecisionRequest();
154 }
155
156 void TheoryQuantifiers::assertUniversal( Node n ){
157 Assert( n.getKind()==FORALL );
158 if( options::recurseCbqi() || !TermDb::hasInstConstAttr(n) ){
159 getQuantifiersEngine()->assertQuantifier( n, true );
160 }
161 }
162
163 void TheoryQuantifiers::assertExistential( Node n ){
164 Assert( n.getKind()== NOT && n[0].getKind()==FORALL );
165 if( !options::cbqi() || options::recurseCbqi() || !TermDb::hasInstConstAttr(n[0]) ){
166 getQuantifiersEngine()->assertQuantifier( n[0], false );
167 }
168 }
169
170 bool TheoryQuantifiers::flipDecision(){
171 //Debug("quantifiers-flip") << "No instantiation given, flip decision, level = " << d_valuation.getDecisionLevel() << std::endl;
172 //for( int i=1; i<=(int)d_valuation.getDecisionLevel(); i++ ){
173 // Debug("quantifiers-flip") << " " << d_valuation.getDecision( i ) << std::endl;
174 //}
175 //if( d_valuation.getDecisionLevel()>0 ){
176 // double r = double(rand())/double(RAND_MAX);
177 // unsigned decisionLevel = (unsigned)(r*d_valuation.getDecisionLevel());
178 // d_out->flipDecision( decisionLevel );
179 // return true;
180 //}else{
181 // return false;
182 //}
183
184 if( !d_out->flipDecision() ){
185 return restart();
186 }
187 return true;
188 }
189
190 bool TheoryQuantifiers::restart(){
191 static const int restartLimit = 0;
192 if( d_numRestarts==restartLimit ){
193 Debug("quantifiers-flip") << "No more restarts." << std::endl;
194 return false;
195 }else{
196 d_numRestarts++;
197 Debug("quantifiers-flip") << "Do restart." << std::endl;
198 return true;
199 }
200 }
201
202 void TheoryQuantifiers::setUserAttribute(const std::string& attr, Node n, std::vector<Node> node_values, std::string str_value){
203 QuantifiersAttributes::setUserAttribute( attr, n, node_values, str_value );
204 }