Merge branch '1.3.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-2013 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 }
46
47 TheoryQuantifiers::~TheoryQuantifiers() {
48 }
49
50 void TheoryQuantifiers::setMasterEqualityEngine(eq::EqualityEngine* eq) {
51 d_masterEqualityEngine = eq;
52 }
53
54 void TheoryQuantifiers::addSharedTerm(TNode t) {
55 Debug("quantifiers-other") << "TheoryQuantifiers::addSharedTerm(): "
56 << t << endl;
57 }
58
59
60 void TheoryQuantifiers::notifyEq(TNode lhs, TNode rhs) {
61 Debug("quantifiers-other") << "TheoryQuantifiers::notifyEq(): "
62 << lhs << " = " << rhs << endl;
63
64 }
65
66 void TheoryQuantifiers::preRegisterTerm(TNode n) {
67 Debug("quantifiers-prereg") << "TheoryQuantifiers::preRegisterTerm() " << n << endl;
68 if( n.getKind()==FORALL && !TermDb::hasInstConstAttr(n) ){
69 getQuantifiersEngine()->registerQuantifier( n );
70 }
71 }
72
73
74 void TheoryQuantifiers::presolve() {
75 Debug("quantifiers-presolve") << "TheoryQuantifiers::presolve()" << endl;
76 }
77
78 Node TheoryQuantifiers::getValue(TNode n) {
79 //NodeManager* nodeManager = NodeManager::currentNM();
80 switch(n.getKind()) {
81 case FORALL:
82 case EXISTS:
83 bool value;
84 if( d_valuation.hasSatValue( n, value ) ){
85 return NodeManager::currentNM()->mkConst(value);
86 }else{
87 return NodeManager::currentNM()->mkConst(false); //FIX_THIS?
88 }
89 break;
90 default:
91 Unhandled(n.getKind());
92 }
93 }
94
95 void TheoryQuantifiers::collectModelInfo(TheoryModel* m, bool fullModel) {
96 if(fullModel) {
97 for(assertions_iterator i = facts_begin(); i != facts_end(); ++i) {
98 if((*i).assertion.getKind() == kind::NOT) {
99 Debug("quantifiers::collectModelInfo") << "got quant FALSE: " << (*i).assertion[0] << endl;
100 m->assertPredicate((*i).assertion[0], false);
101 } else {
102 Debug("quantifiers::collectModelInfo") << "got quant TRUE : " << *i << endl;
103 m->assertPredicate(*i, true);
104 }
105 }
106 }
107 }
108
109 void TheoryQuantifiers::check(Effort e) {
110 CodeTimer codeTimer(d_theoryTime);
111
112 Debug("quantifiers-check") << "quantifiers::check(" << e << ")" << std::endl;
113 while(!done()) {
114 Node assertion = get();
115 Debug("quantifiers-assert") << "quantifiers::assert(): " << assertion << std::endl;
116 switch(assertion.getKind()) {
117 case kind::FORALL:
118 assertUniversal( assertion );
119 break;
120 case kind::NOT:
121 {
122 switch( assertion[0].getKind()) {
123 case kind::FORALL:
124 assertExistential( assertion );
125 break;
126 default:
127 Unhandled(assertion[0].getKind());
128 break;
129 }
130 }
131 break;
132 default:
133 Unhandled(assertion.getKind());
134 break;
135 }
136 }
137 // call the quantifiers engine to check
138 getQuantifiersEngine()->check( e );
139 }
140
141 void TheoryQuantifiers::propagate(Effort level){
142 //CodeTimer codeTimer(d_theoryTime);
143 //getQuantifiersEngine()->propagate( level );
144 }
145
146 Node TheoryQuantifiers::getNextDecisionRequest(){
147 return getQuantifiersEngine()->getNextDecisionRequest();
148 }
149
150 void TheoryQuantifiers::assertUniversal( Node n ){
151 Assert( n.getKind()==FORALL );
152 if( options::recurseCbqi() || !TermDb::hasInstConstAttr(n) ){
153 getQuantifiersEngine()->registerQuantifier( n );
154 getQuantifiersEngine()->assertNode( n );
155 }
156 }
157
158 void TheoryQuantifiers::assertExistential( Node n ){
159 Assert( n.getKind()== NOT && n[0].getKind()==FORALL );
160 if( options::recurseCbqi() || !TermDb::hasInstConstAttr(n[0]) ){
161 if( d_skolemized.find( n )==d_skolemized.end() ){
162 Node body = getQuantifiersEngine()->getTermDatabase()->getSkolemizedBody( n[0] );
163 NodeBuilder<> nb(kind::OR);
164 nb << n[0] << body.notNode();
165 Node lem = nb;
166 Trace("quantifiers-sk") << "Skolemize lemma : " << lem << std::endl;
167 d_out->lemma( lem, false, true );
168 d_skolemized[n] = true;
169 }
170 }
171 }
172
173 bool TheoryQuantifiers::flipDecision(){
174 //Debug("quantifiers-flip") << "No instantiation given, flip decision, level = " << d_valuation.getDecisionLevel() << std::endl;
175 //for( int i=1; i<=(int)d_valuation.getDecisionLevel(); i++ ){
176 // Debug("quantifiers-flip") << " " << d_valuation.getDecision( i ) << std::endl;
177 //}
178 //if( d_valuation.getDecisionLevel()>0 ){
179 // double r = double(rand())/double(RAND_MAX);
180 // unsigned decisionLevel = (unsigned)(r*d_valuation.getDecisionLevel());
181 // d_out->flipDecision( decisionLevel );
182 // return true;
183 //}else{
184 // return false;
185 //}
186
187 if( !d_out->flipDecision() ){
188 return restart();
189 }
190 return true;
191 }
192
193 bool TheoryQuantifiers::restart(){
194 static const int restartLimit = 0;
195 if( d_numRestarts==restartLimit ){
196 Debug("quantifiers-flip") << "No more restarts." << std::endl;
197 return false;
198 }else{
199 d_numRestarts++;
200 Debug("quantifiers-flip") << "Do restart." << std::endl;
201 return true;
202 }
203 }
204
205 void TheoryQuantifiers::setUserAttribute( const std::string& attr, Node n ){
206 QuantifiersAttributes::setUserAttribute( attr, n );
207 }