00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "cnf_manager.h"
00023
00024 #define _CVC3_TRUSTED_
00025 #include "cnf_theorem_producer.h"
00026
00027
00028 using namespace std;
00029 using namespace CVC3;
00030 using namespace SAT;
00031
00032
00033
00034
00035
00036
00037
00038 CNF_Rules* CNF_Manager::createProofRules(TheoremManager* tm, const CLFlags& flags) {
00039 return new CNF_TheoremProducer(tm, flags);
00040 }
00041
00042
00043
00044
00045
00046
00047
00048
00049 void CNF_TheoremProducer::getSmartClauses(const Theorem& thm, vector<Theorem>& clauses)
00050 {
00051
00052 vector<Theorem> assumptions;
00053 thm.clearAllFlags();
00054 thm.GetSatAssumptions(assumptions);
00055 Proof pf;
00056 if (withProof()) {
00057 pf = newPf("learned_clause", thm.getProof());
00058 }
00059 Theorem thm2;
00060 vector<Expr> TempVec;
00061
00062 if (!thm.getExpr().isFalse()) {
00063 TempVec.push_back(thm.getExpr());
00064 }
00065 for (vector<Theorem>::size_type i = 0; i < assumptions.size(); i++) {
00066 if (thm.getExpr() == assumptions[i].getExpr()) {
00067
00068 if (!(assumptions[i].isAssump())) {
00069 getSmartClauses(assumptions[i], clauses);
00070 }
00071 return;
00072 }
00073 TempVec.push_back(assumptions[i].getExpr().negate());
00074 }
00075
00076 if (TempVec.size() == 1){
00077 thm2 = newTheorem(TempVec[0], Assumptions::emptyAssump(), pf);
00078 }
00079 else if (TempVec.size() > 1) {
00080 thm2 = newTheorem(Expr(OR, TempVec), Assumptions::emptyAssump(), pf);
00081 }
00082 else {
00083 FatalAssert(false, "Should be unreachable");
00084 }
00085 thm2.setQuantLevel(thm.getQuantLevel());
00086 clauses.push_back(thm2);
00087
00088
00089 for (vector<Theorem>::iterator itr = assumptions.begin(); itr != assumptions.end(); itr++) {
00090 if (!((*itr).isAssump())) {
00091 getSmartClauses((*itr), clauses);
00092 }
00093 }
00094 }
00095
00096
00097 void CNF_TheoremProducer::learnedClauses(const Theorem& thm,
00098 vector<Theorem>& clauses,
00099 bool newLemma)
00100 {
00101 IF_DEBUG(if(debugger.trace("cnf proofs")) {
00102 ostream& os = debugger.getOS();
00103 os << "learnedClause {" << endl;
00104 os << thm;
00105 })
00106
00107 if (!newLemma && d_smartClauses) {
00108 getSmartClauses(thm, clauses);
00109 return;
00110 }
00111
00112
00113
00114
00115
00116
00117 vector<Expr> assumptions;
00118 Proof pf;
00119 thm.getLeafAssumptions(assumptions, true );
00120
00121 vector<Expr>::iterator iend = assumptions.end();
00122 for (vector<Expr>::iterator i = assumptions.begin();
00123 i != iend; ++i) {
00124 DebugAssert(i->isAbsLiteral(), "Expected only literal assumptions");
00125 }
00126
00127 if (!thm.getExpr().isFalse())
00128 assumptions.push_back(thm.getExpr());
00129
00130 DebugAssert(assumptions.size() > 0, "Expected at least one entry");
00131
00132 Theorem thm2;
00133 if (assumptions.size() == 1) {
00134 if(withProof()) {
00135 pf = newPf("learned_clause", assumptions[0], thm.getProof());
00136 }
00137 thm2 = newTheorem(assumptions[0], Assumptions::emptyAssump(), pf);
00138 }
00139 else {
00140 Expr clauseExpr = Expr(OR, assumptions);
00141 if(withProof()) {
00142 pf = newPf("learned_clause", clauseExpr, thm.getProof());
00143 }
00144 thm2 = newTheorem(clauseExpr, Assumptions::emptyAssump(), pf);
00145 }
00146 thm2.setQuantLevel(thm.getQuantLevel());
00147 clauses.push_back(thm2);
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 }
00197
00198
00199 Theorem CNF_TheoremProducer::ifLiftRule(const Expr& e, int itePos) {
00200 if(CHECK_PROOFS) {
00201 CHECK_SOUND(e.getType().isBool(),
00202 "CNF_TheoremProducer::ifLiftRule("
00203 "input must be a Predicate: e = " + e.toString()+")");
00204 CHECK_SOUND(itePos >= 0, "itePos negative"+int2string(itePos));
00205 CHECK_SOUND(e.arity() > itePos && e[itePos].isITE(),
00206 "CNF_TheoremProducer::ifLiftRule("
00207 "input does not have an ITE: e = " + e.toString()+")");
00208 }
00209 const Expr& ite = e[itePos];
00210 const Expr& cond = ite[0];
00211 const Expr& t1 = ite[1];
00212 const Expr& t2 = ite[2];
00213
00214 if(CHECK_PROOFS) {
00215 CHECK_SOUND(cond.getType().isBool(),
00216 "CNF_TheoremProducer::ifLiftRule("
00217 "input does not have an ITE: e = " + e.toString()+")");
00218 }
00219
00220 vector<Expr> k1 = e.getKids();
00221 Op op(e.getOp());
00222
00223 k1[itePos] = t1;
00224 Expr pred1 = Expr(op, k1);
00225
00226 k1[itePos] = t2;
00227 Expr pred2 = Expr(op, k1);
00228
00229 Expr resultITE = cond.iteExpr(pred1, pred2);
00230
00231 Proof pf;
00232 if (withProof())
00233 pf = newPf("if_lift_rule", e, resultITE, d_em->newRatExpr(itePos));
00234 return newRWTheorem(e, resultITE, Assumptions::emptyAssump(), pf);
00235 }
00236
00237 Theorem CNF_TheoremProducer::CNFtranslate(const Expr& before,
00238 const Expr& after,
00239 std::string reason,
00240 int pos) {
00241
00242
00243
00244 Proof pf;
00245 if (withProof()){
00246 vector<Expr> chs ;
00247 chs.push_back(before);
00248 chs.push_back(after);
00249 chs.push_back(d_em->newRatExpr(pos));
00250 pf = newPf("CNF", d_em->newStringExpr(reason), chs );
00251 }
00252 return newTheorem(after, Assumptions::emptyAssump(), pf);
00253 }
00254
00255 Theorem CNF_TheoremProducer::CNFITEtranslate(const Expr& before,
00256 const vector<Expr>& after,
00257 const vector<Theorem>& thms,
00258 int pos){
00259 DebugAssert(3 == after.size(), "why this happens");
00260 DebugAssert(3 == thms.size(), "why this happens");
00261
00262 Proof pf;
00263 if (withProof()){
00264 DebugAssert(thms[0].getRHS() == after[0] , "this never happens");
00265 DebugAssert(thms[1].getRHS() == after[1] , "this never happens");
00266 DebugAssert(thms[2].getRHS() == after[2] , "this never happens");
00267 DebugAssert(thms[0].getLHS() == before[0] , "this never happens");
00268 DebugAssert(thms[1].getLHS() == before[1] , "this never happens");
00269 DebugAssert(thms[2].getLHS() == before[2] , "this never happens");
00270
00271 vector<Expr> chs ;
00272 chs.push_back(before);
00273 chs.push_back(after[0]);
00274 chs.push_back(after[1]);
00275 chs.push_back(after[2]);
00276 chs.push_back(d_em->newRatExpr(pos));
00277 vector<Proof> pfs;
00278 pfs.push_back(thms[0].getProof());
00279 pfs.push_back(thms[1].getProof());
00280 pfs.push_back(thms[2].getProof());
00281 pf = newPf("CNFITE", chs, pfs );
00282 }
00283
00284 Expr newe0 = after[0];
00285 Expr afterExpr = newe0.iteExpr(after[1],after[2]);
00286
00287 return newTheorem(afterExpr, Assumptions(thms), pf);
00288 }
00289
00290
00291
00292
00293
00294
00295 Theorem CNF_TheoremProducer::CNFAddUnit(const Theorem& thm){
00296
00297
00298 Proof pf;
00299 if (withProof()){
00300 pf = newPf("cnf_add_unit", thm.getExpr(), thm.getProof() );
00301 }
00302 return newTheorem(thm.getExpr(), thm.getAssumptionsRef(), pf);
00303 }
00304
00305 Theorem CNF_TheoremProducer::CNFConvert(const Expr& e, const Theorem& thm){
00306
00307
00308 Proof pf;
00309 if (withProof()){
00310 pf = newPf("cnf_convert", e, thm.getExpr(), thm.getProof() );
00311 }
00312 DebugAssert(thm.getExpr().isOr(),"convert is not or exprs");
00313 return newTheorem(thm.getExpr(), thm.getAssumptionsRef(), pf);
00314 }
00315
00316