CVC3
|
00001 /*****************************************************************************/ 00002 /*! 00003 *\file cnf_manager.cpp 00004 *\brief Implementation of CNF_Manager 00005 * 00006 * Author: Clark Barrett 00007 * 00008 * Created: Thu Jan 5 02:30:02 2006 00009 * 00010 * <hr> 00011 * 00012 * License to use, copy, modify, sell and/or distribute this software 00013 * and its documentation for any purpose is hereby granted without 00014 * royalty, subject to the terms and conditions defined in the \ref 00015 * LICENSE file provided with this distribution. 00016 * 00017 * <hr> 00018 */ 00019 /*****************************************************************************/ 00020 00021 00022 #include "cnf_manager.h" 00023 #include "cnf_rules.h" 00024 #include "common_proof_rules.h" 00025 #include "theorem_manager.h" 00026 #include "vc.h" 00027 #include "command_line_flags.h" 00028 00029 00030 using namespace std; 00031 using namespace CVC3; 00032 using namespace SAT; 00033 00034 00035 CNF_Manager::CNF_Manager(TheoremManager* tm, Statistics& statistics, 00036 const CLFlags& flags) 00037 : d_vc(NULL), 00038 d_commonRules(tm->getRules()), 00039 // d_theorems(tm->getCM()->getCurrentContext()), 00040 d_clauseIdNext(0), 00041 // d_translated(tm->getCM()->getCurrentContext()), 00042 d_bottomScope(-1), 00043 d_statistics(statistics), 00044 d_flags(flags), 00045 d_nullExpr(tm->getEM()->getNullExpr()), 00046 d_cnfCallback(NULL) 00047 { 00048 d_rules = createProofRules(tm, flags); 00049 // Push dummy varinfo onto d_varInfo since Var's are indexed from 1 not 0 00050 Varinfo v; 00051 d_varInfo.push_back(v); 00052 if (flags["minimizeClauses"].getBool()) { 00053 CLFlags flags = ValidityChecker::createFlags(); 00054 flags.setFlag("minimizeClauses",false); 00055 d_vc = ValidityChecker::create(flags); 00056 } 00057 } 00058 00059 00060 CNF_Manager::~CNF_Manager() 00061 { 00062 if (d_vc) delete d_vc; 00063 delete d_rules; 00064 } 00065 00066 00067 void CNF_Manager::registerAtom(const Expr& e, const Theorem& thm) 00068 { 00069 DebugAssert(!e.isRegisteredAtom() || e.isUserRegisteredAtom(), "Atom already registered"); 00070 if (d_cnfCallback && !e.isRegisteredAtom()) d_cnfCallback->registerAtom(e, thm); 00071 } 00072 00073 00074 Theorem CNF_Manager::replaceITErec(const Expr& e, Var v, bool translateOnly) 00075 { 00076 // Quick exit for atomic expressions 00077 if (e.isAtomic()) return d_commonRules->reflexivityRule(e); 00078 00079 // Check cache 00080 Theorem thm; 00081 bool foundInCache = false; 00082 ExprHashMap<Theorem>::iterator iMap = d_iteMap.find(e); 00083 if (iMap != d_iteMap.end()) { 00084 thm = (*iMap).second; 00085 foundInCache = true; 00086 } 00087 00088 if (e.getKind() == ITE) { 00089 // Replace non-Bool ITE expressions 00090 DebugAssert(!e.getType().isBool(), "Expected non-Bool ITE"); 00091 // generate e = x for new x 00092 if (!foundInCache) thm = d_commonRules->varIntroSkolem(e); 00093 Theorem thm2 = d_commonRules->symmetryRule(thm); 00094 thm2 = d_commonRules->iffMP(thm2, d_rules->ifLiftRule(thm2.getExpr(), 1)); 00095 d_translateQueueVars.push_back(v); 00096 d_translateQueueThms.push_back(thm2); 00097 d_translateQueueFlags.push_back(translateOnly); 00098 } 00099 else { 00100 // Recursively traverse, replacing ITE's 00101 vector<Theorem> thms; 00102 vector<unsigned> changed; 00103 unsigned index = 0; 00104 Expr::iterator i, iend; 00105 if (foundInCache) { 00106 for(i = e.begin(), iend = e.end(); i!=iend; ++i, ++index) { 00107 replaceITErec(*i, v, translateOnly); 00108 } 00109 } 00110 else { 00111 for(i = e.begin(), iend = e.end(); i!=iend; ++i, ++index) { 00112 thm = replaceITErec(*i, v, translateOnly); 00113 if (!thm.isRefl()) { 00114 thms.push_back(thm); 00115 changed.push_back(index); 00116 } 00117 } 00118 if(changed.size() > 0) { 00119 thm = d_commonRules->substitutivityRule(e, changed, thms); 00120 } 00121 else thm = d_commonRules->reflexivityRule(e); 00122 } 00123 } 00124 00125 // Update cache and return 00126 if (!foundInCache) d_iteMap[e] = thm; 00127 return thm; 00128 } 00129 00130 00131 Expr CNF_Manager::concreteExpr(const CVC3::Expr& e, const Lit& literal){ 00132 if ( e.isTrue() || e.isFalse() || 00133 (e.isNot() && (e[0].isTrue() || e[0].isFalse()))) 00134 return e; 00135 else 00136 return concreteLit(literal); 00137 } 00138 00139 Theorem CNF_Manager::concreteThm(const CVC3::Expr& ine){ 00140 Theorem ret = d_iteMap[ine]; 00141 if (ret.isNull()) { 00142 ret = d_commonRules->reflexivityRule(ine); 00143 } 00144 return ret; 00145 } 00146 00147 Lit CNF_Manager::translateExprRec(const Expr& e, CNF_Formula& cnf, const Theorem& thmIn) 00148 { 00149 if (e.isFalse()) return Lit::getFalse(); 00150 if (e.isTrue()) return Lit::getTrue(); 00151 if (e.isNot()) return !translateExprRec(e[0], cnf, thmIn); 00152 00153 ExprHashMap<Var>::iterator iMap = d_cnfVars.find(e); 00154 00155 if (e.isTranslated()) { 00156 DebugAssert(iMap != d_cnfVars.end(), "Translated expr should be in map"); 00157 return Lit((*iMap).second); 00158 } 00159 else e.setTranslated(d_bottomScope); 00160 00161 Var v(int(d_varInfo.size())); 00162 bool translateOnly = false; 00163 00164 if (iMap != d_cnfVars.end()) { 00165 v = (*iMap).second; 00166 translateOnly = true; 00167 d_varInfo[v].fanouts.clear(); 00168 } 00169 else { 00170 d_varInfo.resize(v+1); 00171 d_varInfo.back().expr = e; 00172 d_cnfVars[e] = v; 00173 } 00174 00175 Expr::iterator i, iend; 00176 bool isAnd = false; 00177 switch (e.getKind()) { 00178 case AND: 00179 isAnd = true; 00180 case OR: { 00181 00182 vector<Lit> lits; 00183 unsigned idx; 00184 for (i = e.begin(), iend = e.end(); i != iend; ++i) { 00185 lits.push_back(translateExprRec(*i, cnf, thmIn)); 00186 } 00187 00188 vector<Expr> new_chls; 00189 vector<Theorem> thms; 00190 for (idx = 0; idx < lits.size(); ++idx) { 00191 new_chls.push_back(concreteExpr(e[idx],lits[idx])); 00192 thms.push_back(concreteThm(e[idx])); 00193 } 00194 00195 Expr after = (isAnd ? Expr(AND,new_chls) : Expr(OR,new_chls)) ; 00196 00197 // DebugAssert(concreteExpr(e,Lit(v)) == e,"why here"); 00198 00199 for (idx = 0; idx < lits.size(); ++idx) { 00200 cnf.newClause(); 00201 cnf.addLiteral(Lit(v),isAnd); 00202 cnf.addLiteral(lits[idx], !isAnd); 00203 00204 // DebugAssert(concreteExpr(e[idx],lits[idx]) == e[idx], "why here"); 00205 00206 std::string reasonStr = (isAnd ? "and_mid" : "or_mid"); 00207 00208 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFtranslate(e, after, reasonStr, idx,thms)); // by yeting 00209 } 00210 00211 cnf.newClause(); 00212 cnf.addLiteral(Lit(v),!isAnd); 00213 for (idx = 0; idx < lits.size(); ++idx) { 00214 cnf.addLiteral(lits[idx], isAnd); 00215 } 00216 00217 std::string reasonStr = (isAnd ? "and_final" : "or_final") ; 00218 00219 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFtranslate(e, after, reasonStr, 0, thms)); // by yeting 00220 DebugAssert(!d_flags["cnf-formula"].getBool(), "Found impossible case when cnf-formula is enabled"); 00221 break; 00222 } 00223 case IMPLIES: { 00224 Lit arg0 = translateExprRec(e[0], cnf, thmIn); 00225 Lit arg1 = translateExprRec(e[1], cnf, thmIn); 00226 00227 vector<Theorem> thms; 00228 thms.push_back(concreteThm(e[0])); 00229 thms.push_back(concreteThm(e[1])); 00230 00231 Expr left = (concreteExpr(e[0],arg0)); 00232 Expr right = (concreteExpr(e[1],arg1)); 00233 Expr after = left.impExpr(right); 00234 00235 00236 // DebugAssert(concreteExpr(e, Lit(v)) == e, "why here"); 00237 // DebugAssert(concreteExpr(e[0], arg0) == e[0], "why here"); 00238 // DebugAssert(concreteExpr(e[1], arg1) == e[1], "why here"); 00239 00240 cnf.newClause(); 00241 cnf.addLiteral(Lit(v)); 00242 cnf.addLiteral(arg0); 00243 00244 cnf.getCurrentClause().setClauseTheorem( d_rules->CNFtranslate(e, after, "imp", 0,thms)); // by yeting 00245 00246 cnf.newClause(); 00247 cnf.addLiteral(Lit(v)); 00248 cnf.addLiteral(arg1,true); 00249 00250 cnf.getCurrentClause().setClauseTheorem( d_rules->CNFtranslate(e, after, "imp", 1, thms)); // by yeting 00251 00252 cnf.newClause(); 00253 cnf.addLiteral(Lit(v),true); 00254 cnf.addLiteral(arg0,true); 00255 cnf.addLiteral(arg1); 00256 00257 cnf.getCurrentClause().setClauseTheorem( d_rules->CNFtranslate(e, after, "imp", 2,thms)); // by yeting 00258 DebugAssert(!d_flags["cnf-formula"].getBool(), "Found impossible case when cnf-formula is enabled"); 00259 break; 00260 } 00261 case IFF: { 00262 Lit arg0 = translateExprRec(e[0], cnf, thmIn); 00263 Lit arg1 = translateExprRec(e[1], cnf, thmIn); 00264 00265 // DebugAssert(concreteExpr(e, Lit(v)) == e, "why here"); 00266 // DebugAssert(concreteExpr(e[0], arg0) == e[0], "why here"); 00267 // DebugAssert(concreteExpr(e[1], arg1) == e[1], "why here"); 00268 vector<Theorem> thms; 00269 thms.push_back(concreteThm(e[0])); 00270 thms.push_back(concreteThm(e[1])); 00271 00272 Expr left = (concreteExpr(e[0],arg0)); 00273 Expr right = (concreteExpr(e[1],arg1)); 00274 Expr after = left.iffExpr(right); 00275 00276 00277 cnf.newClause(); 00278 cnf.addLiteral(Lit(v)); 00279 cnf.addLiteral(arg0); 00280 cnf.addLiteral(arg1); 00281 00282 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFtranslate(e, after, "iff", 0, thms)); // by yeting 00283 00284 cnf.newClause(); 00285 cnf.addLiteral(Lit(v)); 00286 cnf.addLiteral(arg0,true); 00287 cnf.addLiteral(arg1,true); 00288 00289 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFtranslate(e, after, "iff", 1, thms)); // by yeting 00290 00291 cnf.newClause(); 00292 cnf.addLiteral(Lit(v),true); 00293 cnf.addLiteral(arg0,true); 00294 cnf.addLiteral(arg1); 00295 00296 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFtranslate(e, after, "iff", 2, thms)); // by yeting 00297 00298 cnf.newClause(); 00299 cnf.addLiteral(Lit(v),true); 00300 cnf.addLiteral(arg0); 00301 cnf.addLiteral(arg1,true); 00302 00303 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFtranslate(e, after, "iff", 3, thms)); // by yeting 00304 DebugAssert(!d_flags["cnf-formula"].getBool(), "Found impossible case when cnf-formula is enabled"); 00305 break; 00306 } 00307 case XOR: { 00308 00309 Lit arg0 = translateExprRec(e[0], cnf, thmIn); 00310 Lit arg1 = translateExprRec(e[1], cnf, thmIn); 00311 00312 // DebugAssert(concreteExpr(e, Lit(v)) == e, "why here"); 00313 // DebugAssert(concreteExpr(e[0], arg0) == e[0], "why here"); 00314 // DebugAssert(concreteExpr(e[1], arg1) == e[1], "why here"); 00315 00316 vector<Theorem> thms; 00317 thms.push_back(concreteThm(e[0])); 00318 thms.push_back(concreteThm(e[1])); 00319 00320 Expr left = (concreteExpr(e[0],arg0)); 00321 Expr right = (concreteExpr(e[1],arg1)); 00322 Expr after = left.xorExpr(right); 00323 00324 00325 cnf.newClause(); 00326 cnf.addLiteral(Lit(v),true); 00327 cnf.addLiteral(arg0); 00328 cnf.addLiteral(arg1); 00329 00330 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFtranslate(e, after, "xor", 0, thms)); // by yeting 00331 00332 cnf.newClause(); 00333 cnf.addLiteral(Lit(v),true); 00334 cnf.addLiteral(arg0,true); 00335 cnf.addLiteral(arg1,true); 00336 00337 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFtranslate(e, after, "xor", 1, thms)); // by yeting 00338 00339 cnf.newClause(); 00340 cnf.addLiteral(Lit(v)); 00341 cnf.addLiteral(arg0,true); 00342 cnf.addLiteral(arg1); 00343 00344 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFtranslate(e, after, "xor", 2, thms)); // by yeting 00345 00346 cnf.newClause(); 00347 cnf.addLiteral(Lit(v)); 00348 cnf.addLiteral(arg0); 00349 cnf.addLiteral(arg1,true); 00350 00351 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFtranslate(e, after, "xor", 3,thms)); // by yeting 00352 DebugAssert(!d_flags["cnf-formula"].getBool(), "Found impossible case when cnf-formula is enabled"); 00353 break; 00354 } 00355 case ITE: 00356 { 00357 00358 Lit arg0 = translateExprRec(e[0], cnf, thmIn); 00359 Lit arg1 = translateExprRec(e[1], cnf, thmIn); 00360 Lit arg2 = translateExprRec(e[2], cnf, thmIn); 00361 00362 00363 Expr aftere0 = concreteExpr(e[0], arg0); 00364 Expr aftere1 = concreteExpr(e[1], arg1); 00365 Expr aftere2 = concreteExpr(e[2], arg2); 00366 00367 vector<Expr> after ; 00368 after.push_back(aftere0); 00369 after.push_back(aftere1); 00370 after.push_back(aftere2); 00371 00372 Theorem e0thm; 00373 Theorem e1thm; 00374 Theorem e2thm; 00375 00376 { e0thm = d_iteMap[e[0]]; 00377 if (e0thm.isNull()) e0thm = d_commonRules->reflexivityRule(e[0]); 00378 e1thm = d_iteMap[e[1]]; 00379 if (e1thm.isNull()) e1thm = d_commonRules->reflexivityRule(e[1]); 00380 e2thm = d_iteMap[e[2]]; 00381 if (e2thm.isNull()) e2thm = d_commonRules->reflexivityRule(e[2]); 00382 } 00383 00384 vector<Theorem> thms ; 00385 thms.push_back(e0thm); 00386 thms.push_back(e1thm); 00387 thms.push_back(e2thm); 00388 00389 00390 00391 cnf.newClause(); 00392 cnf.addLiteral(Lit(v),true); 00393 cnf.addLiteral(arg0); 00394 cnf.addLiteral(arg2); 00395 00396 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFITEtranslate(e, after,thms, 1)); // by yeting 00397 00398 cnf.newClause(); 00399 cnf.addLiteral(Lit(v)); 00400 cnf.addLiteral(arg0); 00401 cnf.addLiteral(arg2,true); 00402 00403 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFITEtranslate(e, after,thms, 2)); // by yeting 00404 00405 cnf.newClause(); 00406 cnf.addLiteral(Lit(v)); 00407 cnf.addLiteral(arg0,true); 00408 cnf.addLiteral(arg1,true); 00409 00410 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFITEtranslate(e, after,thms, 3)); // by yeting 00411 00412 cnf.newClause(); 00413 cnf.addLiteral(Lit(v),true); 00414 cnf.addLiteral(arg0,true); 00415 cnf.addLiteral(arg1); 00416 00417 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFITEtranslate(e, after,thms, 4)); // by yeting 00418 00419 cnf.newClause(); 00420 cnf.addLiteral(Lit(v)); 00421 cnf.addLiteral(arg1,true); 00422 cnf.addLiteral(arg2,true); 00423 00424 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFITEtranslate(e, after,thms, 5)); // by yeting 00425 00426 cnf.newClause(); 00427 cnf.addLiteral(Lit(v),true); 00428 cnf.addLiteral(arg1); 00429 cnf.addLiteral(arg2); 00430 00431 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFITEtranslate(e, after,thms, 6)); // by yeting 00432 DebugAssert(!d_flags["cnf-formula"].getBool(), "Found impossible case when cnf-formula is enabled"); 00433 break; 00434 } 00435 default: 00436 { 00437 DebugAssert(!e.isAbsAtomicFormula() || d_varInfo[v].expr == e, 00438 "Corrupted Varinfo"); 00439 if (e.isAbsAtomicFormula()) { 00440 registerAtom(e, thmIn); 00441 return Lit(v); 00442 } 00443 00444 DebugAssert(!d_flags["cnf-formula"].getBool(), "Found impossible case when cnf-formula is enabled"); 00445 00446 Theorem thm = replaceITErec(e, v, translateOnly); 00447 const Expr& e2 = thm.getRHS(); 00448 DebugAssert(e2.isAbsAtomicFormula(), "Expected AbsAtomicFormula"); 00449 if (e2.isTranslated()) { 00450 // Ugly corner case: we happen to create an expression that has been 00451 // created before. We remove the current variable and fix up the 00452 // translation stack. 00453 if (translateOnly) { 00454 DebugAssert(v == d_cnfVars[e2], "Expected literal match"); 00455 } 00456 else { 00457 d_varInfo.resize(v); 00458 while (!d_translateQueueVars.empty() && 00459 d_translateQueueVars.back() == v) { 00460 d_translateQueueVars.pop_back(); 00461 } 00462 DebugAssert(d_cnfVars.find(e2) != d_cnfVars.end(), 00463 "Expected existing literal"); 00464 v = d_cnfVars[e2]; 00465 d_cnfVars[e] = v; 00466 while (d_translateQueueVars.size() < d_translateQueueThms.size()) { 00467 d_translateQueueVars.push_back(v); 00468 } 00469 } 00470 } 00471 else { 00472 e2.setTranslated(d_bottomScope); 00473 // Corner case: don't register reflexive equality 00474 if (!e2.isEq() || e2[0] != e2[1]) registerAtom(e2, thmIn); 00475 if (!translateOnly) { 00476 if (d_cnfVars.find(e2) == d_cnfVars.end()) { 00477 d_varInfo[v].expr = e2; 00478 d_cnfVars[e2] = v; 00479 } 00480 else { 00481 // Same corner case in an untranslated expr 00482 d_varInfo.resize(v); 00483 while (!d_translateQueueVars.empty() && 00484 d_translateQueueVars.back() == v) { 00485 d_translateQueueVars.pop_back(); 00486 } 00487 v = d_cnfVars[e2]; 00488 d_cnfVars[e] = v; 00489 while (d_translateQueueVars.size() < d_translateQueueThms.size()) { 00490 d_translateQueueVars.push_back(v); 00491 } 00492 } 00493 } 00494 } 00495 return Lit(v); 00496 } 00497 } 00498 00499 // Record fanins / fanouts 00500 Lit l; 00501 for (i = e.begin(), iend = e.end(); i != iend; ++i) { 00502 l = getCNFLit(*i); 00503 DebugAssert(!l.isNull(), "Expected non-null literal"); 00504 if (!translateOnly) d_varInfo[v].fanins.push_back(l); 00505 if (l.isVar()) d_varInfo[l.getVar()].fanouts.push_back(v); 00506 } 00507 return Lit(v); 00508 } 00509 00510 Lit CNF_Manager::translateExpr(const Theorem& thmIn, CNF_Formula& cnf) 00511 { 00512 Lit l; 00513 Var v; 00514 Expr e = thmIn.getExpr(); 00515 Theorem thm; 00516 bool translateOnly; 00517 00518 Lit ret = translateExprRec(e, cnf, thmIn); 00519 00520 while (d_translateQueueVars.size()) { 00521 v = d_translateQueueVars.front(); 00522 d_translateQueueVars.pop_front(); 00523 thm = d_translateQueueThms.front(); 00524 d_translateQueueThms.pop_front(); 00525 translateOnly = d_translateQueueFlags.front(); 00526 d_translateQueueFlags.pop_front(); 00527 l = translateExprRec(thm.getExpr(), cnf, thmIn); 00528 cnf.newClause(); 00529 cnf.addLiteral(l); 00530 cnf.registerUnit(); 00531 00532 Theorem newThm = d_rules->CNFAddUnit(thm); 00533 // d_theorems.insert(d_clauseIdNext, thm); 00534 // cnf.getCurrentClause().setClauseTheorem(thmIn); // by yeting 00535 cnf.getCurrentClause().setClauseTheorem(newThm); // by yeting 00536 00537 /* 00538 cout<<"set clause theorem 1" << thm << endl; 00539 cout<<"set clause theorem 2 " << thmIn << endl; 00540 cout<<"set clause print" ; cnf.getCurrentClause().print() ; cout<<endl; 00541 cout<<"set clause id " << d_clauseIdNext << endl; 00542 */ 00543 if (!translateOnly) d_varInfo[v].fanins.push_back(l); 00544 d_varInfo[l.getVar()].fanouts.push_back(v); 00545 } 00546 return ret; 00547 } 00548 00549 00550 void CNF_Manager::cons(unsigned lb, unsigned ub, const Expr& e2, vector<unsigned>& newLits) 00551 { 00552 if (lb == ub) { 00553 newLits.push_back(lb); 00554 return; 00555 } 00556 unsigned new_lb = (ub-lb+1)/2 + lb; 00557 unsigned index; 00558 QueryResult res; 00559 d_vc->push(); 00560 for (index = new_lb; index <= ub; ++index) { 00561 d_vc->assertFormula(e2[index].negate()); 00562 } 00563 res = d_vc->query(d_vc->falseExpr()); 00564 d_vc->pop(); 00565 if (res == VALID) { 00566 cons(new_lb, ub, e2, newLits); 00567 return; 00568 } 00569 00570 unsigned new_ub = new_lb-1; 00571 d_vc->push(); 00572 for (index = lb; index <= new_ub; ++index) { 00573 d_vc->assertFormula(e2[index].negate()); 00574 } 00575 res = d_vc->query(d_vc->falseExpr()); 00576 if (res == VALID) { 00577 d_vc->pop(); 00578 cons(lb, new_ub, e2, newLits); 00579 return; 00580 } 00581 00582 cons(new_lb, ub, e2, newLits); 00583 d_vc->pop(); 00584 d_vc->push(); 00585 for (index = 0; index < newLits.size(); ++index) { 00586 d_vc->assertFormula(e2[newLits[index]].negate()); 00587 } 00588 cons(lb, new_ub, e2, newLits); 00589 d_vc->pop(); 00590 } 00591 00592 00593 void CNF_Manager::convertLemma(const Theorem& thm, CNF_Formula& cnf) 00594 { 00595 DebugAssert(cnf.empty(), "Expected empty cnf"); 00596 vector<Theorem> clauses; 00597 00598 d_rules->learnedClauses(thm, clauses, false); 00599 00600 vector<Theorem>::iterator i = clauses.begin(), iend = clauses.end(); 00601 for (; i < iend; ++i) { 00602 // for dumping lemmas: 00603 cnf.newClause(); 00604 Expr e = (*i).getExpr(); 00605 if (!e.isOr()) { 00606 DebugAssert(!getCNFLit(e).isNull(), "Unknown literal"); 00607 cnf.addLiteral(getCNFLit(e)); 00608 cnf.registerUnit(); 00609 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFAddUnit(*i)); 00610 } 00611 else { 00612 Expr::iterator jend = e.end(); 00613 for (Expr::iterator j = e.begin(); j != jend; ++j) { 00614 DebugAssert(!getCNFLit(*j).isNull(), "Unknown literal"); 00615 cnf.addLiteral(getCNFLit(*j)); 00616 } 00617 cnf.getCurrentClause().setClauseTheorem(d_rules->CNFConvert(e, *i)); 00618 } 00619 } 00620 } 00621 00622 00623 Lit CNF_Manager::addAssumption(const Theorem& thm, CNF_Formula& cnf) 00624 { 00625 if (d_flags["cnf-formula"].getBool()) { 00626 Expr e = thm.getExpr(); 00627 DebugAssert(e.isOr() 00628 || (e.isNot() && e[0].isFalse()) 00629 || (e.isNot() && e[0].isTrue()), 00630 "expr:" + e.toString() + " is not an OR Expr or Ture or False" ); 00631 cnf.newClause(); 00632 if (e.isOr()){ 00633 for (int i = 0; i < e.arity(); i++){ 00634 Lit l = (translateExprRec(e[i], cnf, thm)); 00635 cnf.addLiteral(l); 00636 } 00637 cnf.getCurrentClause().setClauseTheorem(thm); 00638 return translateExprRec(e[0], cnf, thm) ;; 00639 } 00640 else { 00641 Lit l = translateExpr(thm, cnf); 00642 cnf.addLiteral(l); 00643 cnf.registerUnit(); 00644 cnf.getCurrentClause().setClauseTheorem(thm); 00645 return l; 00646 } 00647 } 00648 00649 00650 Lit l = translateExpr(thm, cnf); 00651 cnf.newClause(); 00652 cnf.addLiteral(l); 00653 cnf.registerUnit(); 00654 00655 00656 // if(concreteLit(l) != thm.getExpr()){ 00657 // cout<<"fail addunit 3" << endl; 00658 // } 00659 00660 Theorem newThm = d_rules->CNFAddUnit(thm); 00661 // d_theorems[d_clauseIdNext] = thm; 00662 cnf.getCurrentClause().setClauseTheorem(newThm); // by yeting 00663 /* 00664 cout<<"set clause theorem addassumption" << thm << endl; 00665 cout<<"set clause print" ; 00666 cnf.getCurrentClause().print() ; 00667 cout<<endl; 00668 cout<<"set clause id " << d_clauseIdNext << endl; 00669 */ 00670 return l; 00671 } 00672 00673 00674 Lit CNF_Manager::addLemma(Theorem thm, CNF_Formula& cnf) 00675 { 00676 00677 vector<Theorem> clauses; 00678 d_rules->learnedClauses(thm, clauses, true); 00679 DebugAssert(clauses.size() == 1, "expected single clause"); 00680 00681 Lit l = translateExpr(clauses[0], cnf); 00682 cnf.newClause(); 00683 cnf.addLiteral(l); 00684 cnf.registerUnit(); 00685 00686 00687 // if(concreteLit(l) != clauses[0].getExpr()){ 00688 // cout<<"fail addunit 4" << endl; 00689 // } 00690 00691 Theorem newThm = d_rules->CNFAddUnit(clauses[0]); 00692 // d_theorems.insert(d_clauseIdNext, clause); 00693 cnf.getCurrentClause().setClauseTheorem(newThm); //by yeting 00694 00695 /* 00696 cout<<"set clause theorem addlemma" << thm << endl; 00697 cout<<"set clause print" ; 00698 cnf.getCurrentClause().print() ; 00699 cout<<endl; 00700 cout<<"set clause id " << d_clauseIdNext << endl; 00701 */ 00702 return l; 00703 } 00704