datatype_theorem_producer.cpp

Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /*!
00003  *\file datatype_theorem_producer.cpp
00004  *\brief TRUSTED implementation of recursive datatype rules
00005  *
00006  * Author: Clark Barrett
00007  *
00008  * Created: Mon Jan 10 15:43:39 2005
00009  *
00010  * <hr>
00011  * Copyright (C) 2003 by the Board of Trustees of Leland Stanford
00012  * Junior University and by New York University. 
00013  *
00014  * License to use, copy, modify, sell and/or distribute this software
00015  * and its documentation for any purpose is hereby granted without
00016  * royalty, subject to the terms and conditions defined in the \ref
00017  * LICENSE file provided with this distribution.  In particular:
00018  *
00019  * - The above copyright notice and this permission notice must appear
00020  * in all copies of the software and related documentation.
00021  *
00022  * - THE SOFTWARE IS PROVIDED "AS-IS", WITHOUT ANY WARRANTIES,
00023  * EXPRESSED OR IMPLIED.  USE IT AT YOUR OWN RISK.
00024  * 
00025  * <hr>
00026  * 
00027  */
00028 /*****************************************************************************/
00029 
00030 
00031 // This code is trusted
00032 #define _CVCL_TRUSTED_
00033 
00034 
00035 #include "datatype_theorem_producer.h"
00036 #include "theory_datatype.h"
00037 #include "theory_core.h"
00038 
00039 
00040 using namespace std;
00041 using namespace CVCL;
00042 
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 // TheoryDatatype: trusted method for creating DatatypeTheoremProducer
00046 ////////////////////////////////////////////////////////////////////
00047 
00048 
00049 DatatypeProofRules*
00050 TheoryDatatype::createProofRules() {
00051   return new DatatypeTheoremProducer(this);
00052 }
00053   
00054 
00055 ////////////////////////////////////////////////////////////////////
00056 // Proof rules
00057 ////////////////////////////////////////////////////////////////////
00058 
00059 
00060 Theorem DatatypeTheoremProducer::dummyTheorem(const CDList<Theorem>& facts,
00061                                               const Expr& e) {
00062   vector<Theorem> thmVec;
00063   for (unsigned i = 0; i < facts.size(); ++i)
00064     thmVec.push_back(facts[i]);
00065   Assumptions a(thmVec);  
00066   Proof pf;
00067   return newTheorem(e, a, pf);
00068 }
00069 
00070 
00071 Theorem DatatypeTheoremProducer::rewriteSelCons(const CDList<Theorem>& facts,
00072                                                 const Expr& e) {
00073   if (CHECK_PROOFS) {
00074     CHECK_SOUND(isSelector(e), "Selector expected");
00075     CHECK_SOUND(d_theoryDatatype->canCollapse(e), "Expected canCollapse");
00076   }
00077   Proof pf;
00078   Expr t;
00079 
00080   pair<Expr, unsigned> selectorInfo =
00081     d_theoryDatatype->getSelectorInfo(e.getOpExpr());
00082   if (isConstructor(e[0]) &&
00083       selectorInfo.first == getConstructor(e[0])) {
00084     t = e[0][selectorInfo.second];
00085   }
00086   else {
00087     Expr selTypeExpr = e.getOpExpr().getType().getExpr();
00088     Type type = Type(selTypeExpr[selTypeExpr.arity()-1]);
00089     t = d_theoryDatatype->getConstant(type);
00090   }
00091 
00092   if (withProof()) pf = newPf("rewriteSelCons", e, t);
00093 
00094   if (!isConstructor(e[0])) {
00095     vector<Theorem> thmVec;
00096     for (unsigned i = 0; i < facts.size(); ++i)
00097       thmVec.push_back(facts[i]);
00098     Assumptions a(thmVec);  
00099     return newRWTheorem(e, t, a, pf);
00100   }
00101   else {
00102     Assumptions a;
00103     return newRWTheorem(e, t, a, pf);
00104   }
00105 }
00106 
00107 
00108 Theorem DatatypeTheoremProducer::rewriteTestCons(const Expr& e) {
00109   if (CHECK_PROOFS) {
00110     CHECK_SOUND(isTester(e), "Tester expected");
00111     CHECK_SOUND(isConstructor(e[0]), "Expected Test(Cons)");
00112   }
00113   Assumptions a;
00114   Proof pf;
00115   Expr t;
00116   Expr cons = d_theoryDatatype->getConsForTester(e.getOpExpr());
00117   if (cons == getConstructor(e[0])) {
00118     t = d_theoryDatatype->trueExpr();
00119   }
00120   else {
00121     t = d_theoryDatatype->falseExpr();
00122   }
00123   if (withProof()) pf = newPf("rewriteTestCons", e, t);
00124   return newRWTheorem(e, t, a, pf);
00125 }
00126 
00127 
00128 Theorem DatatypeTheoremProducer::decompose(const Theorem& e) {
00129   if (CHECK_PROOFS) {
00130     CHECK_SOUND(e.isRewrite(), "decompose: expected rewrite");
00131   }
00132   const Expr& lhs = e.getLHS();
00133   const Expr& rhs = e.getRHS();
00134   if(CHECK_PROOFS) {
00135     CHECK_SOUND(isConstructor(lhs) && isConstructor(rhs) &&
00136                 lhs.isApply() && rhs.isApply() &&
00137                 lhs.getOpExpr() == rhs.getOpExpr() &&
00138                 lhs.arity() > 0 && lhs.arity() == rhs.arity(),
00139                 "decompose precondition violated");
00140   }
00141   Assumptions a(e);
00142   Proof pf;
00143   Expr res = lhs[0].eqExpr(rhs[0]);
00144   if (lhs.arity() > 1) {
00145     vector<Expr> args;
00146     args.push_back(res);
00147     for (int i = 1; i < lhs.arity(); ++i) {
00148       args.push_back(lhs[i].eqExpr(rhs[i]));
00149     }
00150     res = andExpr(args);
00151   }
00152   if (withProof()) pf = newPf("decompose", e.getProof());
00153   return newTheorem(res, a, pf);
00154 }
00155 
00156 
00157 Theorem DatatypeTheoremProducer::noCycle(const Expr& e) {
00158   if (CHECK_PROOFS) {
00159     CHECK_SOUND(isConstructor(e) && e.isApply() && e.arity() > 0,
00160                 "noCycle: expected constructor with children");
00161   }
00162   Assumptions a;
00163   Proof pf;
00164 
00165   Type t = e.getOpExpr().getType();
00166   t = t[t.arity()-1];
00167   const Op& reach = d_theoryDatatype->getReachablePredicate(t);
00168 
00169   vector<Expr> args;
00170   args.push_back(!Expr(reach, e, e));
00171   for (int i = 0; i < e.arity(); ++i) {
00172     if (isDatatype(e[i].getType()) &&
00173         d_theoryDatatype->getReachablePredicate(e[i].getType()) == reach)
00174       args.push_back(Expr(reach, e, e[i]));
00175   }
00176 
00177   if (withProof()) pf = newPf("noCycle", e);
00178   return newTheorem(andExpr(args), a, pf);
00179 }

Generated on Thu Apr 13 16:57:30 2006 for CVC Lite by  doxygen 1.4.4