CVC3

cnf_theorem_producer.cpp

Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /*!
00003  *\file cnf_theorem_producer.cpp
00004  *\brief Implementation of CNF_TheoremProducer
00005  *
00006  * Author: Clark Barrett
00007  *
00008  * Created: Thu Jan  5 05:49:24 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 
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 // class CNF_Manager trusted methods
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 // Proof rules
00045 /////////////////////////////////////////////////////////////////////////////
00046 
00047 
00048 
00049 void CNF_TheoremProducer::getSmartClauses(const Theorem& thm, vector<Theorem>& clauses)
00050 {
00051   //  DebugAssert(!thm.getExpr().isDeductionLiteral(), "Expected unproved expr");
00052   vector<Theorem> assumptions;
00053   thm.clearAllFlags();
00054   thm.GetSatAssumptions(assumptions);  
00055 //   Proof pf;
00056 //   if (withProof()) {
00057 //     pf = newPf("learned_clause_smart", thm.getProof());
00058 //   }
00059   Theorem thm2; 
00060   vector<Expr> TempVec;
00061   vector<Proof> pfs; 
00062   if (!thm.getExpr().isFalse()) {
00063     TempVec.push_back(thm.getExpr());
00064     pfs.push_back(thm.getProof());
00065   }
00066   for (vector<Theorem>::size_type i = 0; i < assumptions.size(); i++) {
00067     if (thm.getExpr() == assumptions[i].getExpr()) {
00068       // skip this clause as it is trivial
00069       if (!(assumptions[i].isAssump())) {
00070         getSmartClauses(assumptions[i], clauses);
00071       }
00072       return;
00073     }
00074     TempVec.push_back(assumptions[i].getExpr().negate());
00075     pfs.push_back(assumptions[i].getProof());
00076   }
00077 
00078   Proof pf;
00079   if (TempVec.size() == 1){
00080     if (withProof()) {
00081       pf = newPf("learned_clause_smart", TempVec[0], pfs);
00082     }
00083     thm2 = newTheorem(TempVec[0], Assumptions::emptyAssump(), pf);
00084   }
00085   else if (TempVec.size() > 1) {   
00086     if (withProof()) {
00087       pf = newPf("learned_clause_smart", Expr(OR, TempVec), pfs);
00088     }
00089     thm2 = newTheorem(Expr(OR, TempVec), Assumptions::emptyAssump(), pf);
00090   }
00091   else {
00092     FatalAssert(false, "Should be unreachable");
00093   }
00094   thm2.setQuantLevel(thm.getQuantLevel());
00095   clauses.push_back(thm2);
00096   //  thm.getExpr().setDeductionLiteral();
00097 
00098   for (vector<Theorem>::iterator itr = assumptions.begin(); itr != assumptions.end(); itr++) {
00099     if (!((*itr).isAssump())) {// && !(*itr).getExpr().isDeductionLiteral()) {
00100       getSmartClauses((*itr), clauses);
00101     }
00102   }
00103 }   
00104 
00105 
00106 void CNF_TheoremProducer::learnedClauses(const Theorem& thm,
00107                                          vector<Theorem>& clauses,
00108                                          bool newLemma)
00109 {
00110   IF_DEBUG(if(debugger.trace("cnf proofs")) {
00111     ostream& os = debugger.getOS();
00112     os << "learnedClause {" << endl;
00113     os << thm;
00114   })
00115     
00116   if (!newLemma && d_smartClauses) {
00117     getSmartClauses(thm, clauses);
00118     return;
00119   }
00120 
00121 //   if (newLemma || d_flags["dynack"].getInt() <= 0) {
00122 //    if (NewClausel == true) {
00123 //        return;
00124 //     }
00125 
00126   vector<Expr> assumptions;
00127   Proof pf;
00128   thm.getLeafAssumptions(assumptions, true /*negate*/);
00129 
00130   vector<Expr>::iterator iend = assumptions.end();
00131   for (vector<Expr>::iterator i = assumptions.begin();
00132        i != iend; ++i) {
00133     DebugAssert(i->isAbsLiteral(), "Expected only literal assumptions");
00134   }
00135 
00136   if (!thm.getExpr().isFalse())
00137     assumptions.push_back(thm.getExpr());
00138 
00139   DebugAssert(assumptions.size() > 0, "Expected at least one entry");
00140 
00141   Theorem thm2;
00142   if (assumptions.size() == 1) {
00143     if(withProof()) {
00144       pf = newPf("learned_clause", thm.getProof());
00145     }
00146     thm2 = newTheorem(assumptions[0], Assumptions::emptyAssump(), pf);
00147   }
00148   else {
00149     Expr clauseExpr = Expr(OR, assumptions);
00150     if(withProof()) {
00151       pf = newPf("learned_clause", thm.getProof());
00152     }
00153     thm2 = newTheorem(clauseExpr, Assumptions::emptyAssump(), pf);
00154   }
00155   thm2.setQuantLevel(thm.getQuantLevel());
00156   clauses.push_back(thm2);
00157 //   }
00158 //   else {
00159 
00160 //     vector<Expr> congruences;
00161 
00162 //     thm.getAssumptionsAndCong(assumptions, congruences, true /*negate*/);
00163 
00164 //     vector<Expr>::iterator i = assumptions.begin(), iend = assumptions.end();
00165 //     for (; i != iend; ++i) {
00166 //       DebugAssert(i->isAbsLiteral(), "Expected only literal assumptions");
00167 //     }
00168 
00169 //     if (!thm.getExpr().isFalse())
00170 //       assumptions.push_back(thm.getExpr());
00171 
00172 //     if(withProof()) {
00173 //       pf = newPf("learned_clause", thm.getProof());
00174 //     }
00175 
00176 //     DebugAssert(assumptions.size() > 0, "Expected at least one entry");
00177 
00178 //     Theorem thm2;
00179 //     if (assumptions.size() == 1) {
00180 //       Expr clauseExpr = assumptions[0];
00181 //       if(withProof()) {
00182 //  pf = newPf("learned_clause", clauseExpr, thm.getProof());
00183 //       }
00184 //       thm2 = newTheorem(clauseExpr, Assumptions::emptyAssump(), pf);
00185 //     }
00186 //     else {
00187 //       Expr clauseExpr = Expr(OR, assumptions);
00188 //       if(withProof()) {
00189 //  pf = newPf("learned_clause", clauseExpr, thm.getProof());
00190 //       }
00191 //       thm2 = newTheorem(clauseExpr, Assumptions::emptyAssump(), pf);
00192 //     }
00193 //     thm2.setQuantLevel(thm.getQuantLevel());
00194 //     clauses.push_back(thm2);
00195 
00196 //     for (i = congruences.begin(), iend = congruences.end(); i != iend; ++i) {
00197 //       if (withProof()) {
00198 //         pf = newPf("congruence", *i);
00199 //       }
00200 //       thm2 = newTheorem(*i, Assumptions::emptyAssump(), pf);
00201 //       thm2.setQuantLevel(thm.getQuantLevel());
00202 //       clauses.push_back(thm2);
00203 //     }
00204 //   }
00205 }
00206 
00207 
00208 Theorem CNF_TheoremProducer::ifLiftRule(const Expr& e, int itePos) {
00209   if(CHECK_PROOFS) {
00210     CHECK_SOUND(e.getType().isBool(),
00211     "CNF_TheoremProducer::ifLiftRule("
00212     "input must be a Predicate: e = " + e.toString()+")");
00213     CHECK_SOUND(itePos >= 0, "itePos negative"+int2string(itePos));
00214     CHECK_SOUND(e.arity() > itePos && e[itePos].isITE(),
00215     "CNF_TheoremProducer::ifLiftRule("
00216     "input does not have an ITE: e = " + e.toString()+")");
00217   }
00218   const Expr& ite = e[itePos];
00219   const Expr& cond = ite[0];
00220   const Expr& t1 = ite[1];
00221   const Expr& t2 = ite[2];
00222 
00223   if(CHECK_PROOFS) {
00224     CHECK_SOUND(cond.getType().isBool(),
00225     "CNF_TheoremProducer::ifLiftRule("
00226     "input does not have an ITE: e = " + e.toString()+")");
00227   }    
00228 
00229   vector<Expr> k1 = e.getKids();
00230   Op op(e.getOp());
00231 
00232   k1[itePos] = t1;
00233   Expr pred1 =  Expr(op, k1);
00234 
00235   k1[itePos] = t2;
00236   Expr pred2 =  Expr(op, k1);
00237 
00238   Expr resultITE = cond.iteExpr(pred1, pred2);
00239 
00240   Proof pf;
00241   if (withProof())
00242     pf = newPf("if_lift_rule", e, resultITE, d_em->newRatExpr(itePos));
00243   return newRWTheorem(e, resultITE, Assumptions::emptyAssump(), pf);
00244 }
00245 
00246 Theorem CNF_TheoremProducer::CNFtranslate(const Expr& before, 
00247             const Expr& after, 
00248             std::string reason, 
00249             int pos,
00250             const vector<Theorem>& thms) {
00251   //well, this is assert the e as a theorem without any justification.
00252   //change this as soon as possible
00253   //  cout << "###" << before; 
00254   Proof pf;
00255   if (withProof()){
00256     vector<Expr> chs ;
00257     chs.push_back(d_em->newStringExpr(reason));
00258     chs.push_back(before);
00259     chs.push_back(after);
00260     chs.push_back(d_em->newRatExpr(pos));
00261     vector<Proof> pfs;
00262     for(vector<Theorem>::const_iterator i = thms.begin(), iend= thms.end();
00263   i != iend; i++){
00264       pfs.push_back((*i).getProof());
00265     }
00266     pf = newPf("CNF", chs, pfs );
00267   }
00268   return newTheorem(after, Assumptions(thms), pf);
00269 }
00270 
00271 Theorem CNF_TheoremProducer::CNFITEtranslate(const Expr& before, 
00272                const vector<Expr>& after,
00273                const vector<Theorem>& thms,
00274                int pos){
00275   DebugAssert(3 == after.size(), "why this happens");
00276   DebugAssert(3 == thms.size(), "why this happens");
00277 
00278   Proof pf;
00279   if (withProof()){
00280     DebugAssert(thms[0].getRHS() == after[0] , "this never happens");
00281     DebugAssert(thms[1].getRHS() == after[1] , "this never happens");
00282     DebugAssert(thms[2].getRHS() == after[2] , "this never happens");
00283     DebugAssert(thms[0].getLHS() == before[0] , "this never happens");
00284     DebugAssert(thms[1].getLHS() == before[1] , "this never happens");
00285     DebugAssert(thms[2].getLHS() == before[2] , "this never happens");
00286     
00287     vector<Expr> chs ;
00288     chs.push_back(before);
00289     chs.push_back(after[0]);
00290     chs.push_back(after[1]);
00291     chs.push_back(after[2]);
00292     chs.push_back(d_em->newRatExpr(pos));
00293     vector<Proof> pfs;
00294     pfs.push_back(thms[0].getProof());
00295     pfs.push_back(thms[1].getProof());
00296     pfs.push_back(thms[2].getProof());
00297     pf = newPf("CNFITE", chs, pfs );
00298   }
00299 
00300   Expr newe0 = after[0];
00301   Expr afterExpr = newe0.iteExpr(after[1],after[2]);
00302   //we should produce a newRWTheorm whenever possible and then use iffmp rule to get the desired result
00303   return newTheorem(afterExpr, Assumptions(thms), pf);
00304 }
00305   
00306 
00307 
00308 
00309 
00310 // because the way of cnf translation, add unit means the theorem from other parts of cvc3, not from cnf translation
00311 Theorem CNF_TheoremProducer::CNFAddUnit(const Theorem& thm){
00312   
00313   //wrap the thm so the hol light translator know this
00314   Proof pf;
00315   if (withProof()){
00316     pf = newPf("cnf_add_unit", thm.getExpr(), thm.getProof() );
00317   }
00318   return newTheorem(thm.getExpr(), thm.getAssumptionsRef(), pf);
00319 }
00320 
00321 Theorem CNF_TheoremProducer::CNFConvert(const Expr& e, const Theorem& thm){
00322   
00323   //wrap the thm so the hol light translator know this
00324   Proof pf;
00325   if (withProof()){
00326     pf = newPf("cnf_convert", e, thm.getExpr(), thm.getProof() );
00327   }
00328   DebugAssert(thm.getExpr().isOr(),"convert is not or exprs");
00329   return newTheorem(thm.getExpr(), thm.getAssumptionsRef(), pf);
00330 }
00331 
00332