theorem_value.h

Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /*!
00003  * \file theorem_value.h
00004  * 
00005  * Author: Sergey Berezin
00006  * 
00007  * Created: Dec 10 01:03:34 GMT 2002
00008  *
00009  * <hr>
00010  * Copyright (C) 2003 by the Board of Trustees of Leland Stanford
00011  * Junior University and by New York University. 
00012  *
00013  * License to use, copy, modify, sell and/or distribute this software
00014  * and its documentation for any purpose is hereby granted without
00015  * royalty, subject to the terms and conditions defined in the \ref
00016  * LICENSE file provided with this distribution.  In particular:
00017  *
00018  * - The above copyright notice and this permission notice must appear
00019  * in all copies of the software and related documentation.
00020  *
00021  * - THE SOFTWARE IS PROVIDED "AS-IS", WITHOUT ANY WARRANTIES,
00022  * EXPRESSED OR IMPLIED.  USE IT AT YOUR OWN RISK.
00023  * 
00024  * <hr>
00025  * 
00026  */
00027 /*****************************************************************************/
00028 // CLASS: TheoremValue
00029 //
00030 // AUTHOR: Sergey Berezin, 07/05/02
00031 //
00032 // Abstract:
00033 //
00034 // A class representing a proven fact in CVC.  It stores the theorem
00035 // as a CVC expression, and in the appropriate modes also the set of
00036 // assumptions and the proof.
00037 //
00038 // The idea is to allow only a few trusted classes to create values of
00039 // this class.  If all the critical computations in the decision
00040 // procedures are done through the use of Theorems, then soundness of
00041 // these decision procedures will rely only on the soundness of the
00042 // methods in the trusted classes (the inference rules).
00043 //
00044 // Thus, proof checking can effectively be done at run-time on the
00045 // fly.  Or the soundness may be potentially proven by static analysis
00046 // and many run-time checks can then be optimized away.
00047 //
00048 // This theorem_value.h file should NOT be used by the decision
00049 // procedures.  Use theorem.h instead.
00050 //
00051 ////////////////////////////////////////////////////////////////////////
00052 
00053 #ifndef _CVC_lite__theorem_value_h_
00054 #define _CVC_lite__theorem_value_h_
00055 
00056 #include "theorem.h"
00057 #include "theorem_manager.h"
00058 
00059 namespace CVCL {
00060 
00061   class TheoremValue
00062   {
00063     // These are the only classes that can create new TheoremValue classes
00064     friend class Theorem;
00065     friend class RWTheoremValue;
00066     friend class ReflexivityTheoremValue;
00067 
00068   protected:
00069     //! Theorem Manager
00070     TheoremManager* d_tm;
00071 
00072     //! The expression representing a theorem
00073     Expr d_thm;
00074 
00075     //! The assumptions for the theorem
00076     Assumptions d_assump;
00077 
00078     //! Proof of the theorem
00079     Proof d_proof;
00080 
00081     //! How many pointers to this theorem value
00082     int d_refcount : 16;
00083 
00084     //! Largest scope level of the assumptions
00085     int d_scopeLevel : 16;
00086 
00087     //! Temporary flag used during traversals
00088     unsigned d_flag;
00089 
00090     //! Temporary cache used during traversals
00091     int d_cachedValue : 29;
00092 
00093     bool d_isAssump : 1;
00094     bool d_expand : 1; //!< whether it should this be expanded in graph traversal
00095     bool d_clauselit : 1;  //!< whether it belongs to the conflict clause
00096 
00097 
00098   private:
00099     // Constructor.   
00100     /*!
00101      * NOTE: it is private; only friend classes can call it.
00102      *
00103      * If the assumptions refer to only one theorem, grab the
00104      * assumptions of that theorem instead.
00105      */
00106     TheoremValue(TheoremManager* tm, const Expr &thm,
00107                  const Assumptions& assump, const Proof& pf, 
00108                  bool isAssump, int scope = -1) :
00109       d_tm(tm), d_thm(thm), d_proof(pf), d_refcount(0),
00110       d_scopeLevel(0), d_flag(0), d_isAssump(isAssump) {
00111       DebugAssert(d_tm->isActive(), "TheoremValue()");
00112       if (isAssump) {
00113         if (scope == -1) d_scopeLevel = tm->getCM()->scopeLevel();
00114         else d_scopeLevel = scope;
00115       }
00116       else {
00117         d_assump = assump;
00118         d_assump.setConst(); // Make assumptions constant
00119         if (!d_assump.isNull() && !d_assump.empty()) {
00120           //TODO: this is probably inefficient
00121           const Assumptions::iterator iend = d_assump.end();
00122           for (Assumptions::iterator i = d_assump.begin(); 
00123                i != iend; ++i)
00124             if (i->getScope() > d_scopeLevel)
00125               d_scopeLevel = i->getScope();
00126         }
00127       }
00128     }
00129     // Disable copy constructor and assignment
00130     TheoremValue(const TheoremValue& t) {
00131       FatalAssert(false, "TheoremValue() copy constructor called");
00132     }
00133     TheoremValue& operator=(const TheoremValue& t) {
00134       FatalAssert(false, "TheoremValue assignment operator called");
00135       return *this;
00136     }
00137 
00138     bool isFlagged() const {
00139       return d_flag == d_tm->getFlag();
00140     }
00141 
00142     void clearAllFlags() {
00143       d_tm->clearAllFlags();
00144     }
00145     
00146     void setFlag() {
00147       d_flag = d_tm->getFlag();
00148     }
00149 
00150     void setCachedValue(int value) {
00151       d_cachedValue = value;
00152     }
00153 
00154     int getCachedValue() const {
00155       return d_cachedValue;
00156     }
00157 
00158     void setExpandFlag(bool val) {
00159       d_expand = val;
00160     }
00161 
00162     bool getExpandFlag() {
00163       return d_expand;
00164     }
00165     
00166     void setLitFlag(bool val) {
00167       d_clauselit = val;
00168     }
00169 
00170     bool getLitFlag() {
00171       return d_clauselit;
00172     }
00173 
00174     int getScope() {
00175       return d_scopeLevel;
00176     }
00177 
00178 //     virtual bool isRewrite() const { return d_thm.isEq() || d_thm.isIff(); }
00179     virtual bool isRewrite() const { return false; }
00180 
00181     virtual const Expr& getExpr() const { return d_thm; }
00182     virtual const Expr& getLHS() const {
00183       //      TRACE("getExpr","TheoremValue::getLHS called (",d_thm,")");
00184       DebugAssert(isRewrite(),
00185                   "TheoremValue::getLHS() called on non-rewrite theorem:\n"
00186                   +toString());
00187       return d_thm[0];
00188     }
00189     virtual const Expr& getRHS() const {
00190       //      TRACE("getExpr","TheoremValue::getRHS called (",d_thm,")");
00191       DebugAssert(isRewrite(),
00192                   "TheoremValue::getRHS() called on non-rewrite theorem:\n"
00193                   +toString());
00194       return d_thm[1];
00195     }
00196 
00197     const Assumptions& getAssumptionsRef() const { return d_assump; }
00198 
00199     bool isAssump() const { return d_isAssump; }
00200     const Proof& getProof() { return d_proof; }
00201 
00202   public:
00203     // Destructor
00204     virtual ~TheoremValue() { 
00205       DebugAssert(d_refcount == 0,
00206                   "Thm::TheoremValue::~TheoremValue(): refcount != 0.");
00207     }
00208 
00209     std::string toString() const {
00210       return d_assump.toString() + " |- " + getExpr().toString();
00211     }
00212 
00213     // Memory management
00214     virtual MemoryManager* getMM() { return d_tm->getMM(); }
00215 
00216     void* operator new(size_t size, MemoryManager* mm) {
00217       return mm->newData(size);
00218     }
00219 
00220     void operator delete(void* d) { }
00221 
00222   }; // end of class TheoremValue
00223 
00224 ///////////////////////////////////////////////////////////////////////////////
00225 //                                                                           //
00226 // Class: RWTheoremValue                                                     //
00227 // Author: Clark Barrett                                                     //
00228 // Created: Fri May  2 12:51:55 2003                                         //
00229 // Description: A special subclass for theorems of the form A |- t=t' or     //
00230 //              A |- phi iff phi'.  The actual Expr is only created on       //
00231 //              demand.  The idea is that getLHS and getRHS should be used   //
00232 //              whenever possible, avoiding creating unnecessary Expr's.     //
00233 //                                                                           //
00234 ///////////////////////////////////////////////////////////////////////////////
00235   class RWTheoremValue :public TheoremValue
00236   {
00237     friend class Theorem;
00238 
00239   protected:
00240     // d_thm in the base class contains the full expression, which is
00241     // only constructed on demand.
00242     Expr d_lhs;
00243     Expr d_rhs;
00244 
00245   private:
00246     // Constructor.   NOTE: it is private; only friend classes can call it.
00247     RWTheoremValue(TheoremManager* tm, const Expr& lhs, const Expr& rhs,
00248                    const Assumptions& assump, const Proof& pf, bool isAssump,
00249                    int scope = -1)
00250       : TheoremValue(tm, Expr(), assump, pf, isAssump, scope),
00251         d_lhs(lhs), d_rhs(rhs) { }
00252 
00253     // Sometimes we have the full expression already created
00254     RWTheoremValue(TheoremManager* tm, const Expr& thm,
00255                    const Assumptions& assump, const Proof& pf, bool isAssump,
00256                    int scope = -1)
00257       : TheoremValue(tm, thm, assump, pf, isAssump, scope),
00258         d_lhs(thm[0]), d_rhs(thm[1]) { }
00259 
00260     const Expr& getExpr() const {
00261       if (d_thm.isNull()) {
00262         bool isBool = d_lhs.getType().isBool();
00263         // have to fake out the const qualifier
00264         Expr* pexpr = const_cast<Expr*>(&d_thm);
00265         *pexpr = isBool ? d_lhs.iffExpr(d_rhs) : d_lhs.eqExpr(d_rhs);
00266         //      TRACE("getExpr", "getExpr called on RWTheorem (", d_thm, ")");
00267       }
00268       return d_thm;
00269     }
00270 
00271     const Expr& getLHS() const { return d_lhs; }
00272     const Expr& getRHS() const { return d_rhs; }
00273 
00274   public:
00275     // Destructor
00276     ~RWTheoremValue() {}
00277 
00278     bool isRewrite() const { return true; }
00279 
00280     // Memory management
00281     MemoryManager* getMM() { return d_tm->getRWMM(); }
00282 
00283     void* operator new(size_t size, MemoryManager* mm) {
00284       return mm->newData(size);
00285     }
00286 
00287     void operator delete(void* d) { }
00288 
00289   }; // end of class RWTheoremValue
00290 
00291   //! A special subclass for reflexivity theorems: e = e and e <=> e
00292   /*! Such theorems are created extremely often, and do not contain
00293    * any useful information other than the expression itself.
00294    */
00295   class ReflexivityTheoremValue: public TheoremValue {
00296     friend class Theorem;
00297   private:
00298     // The expression 'e' in e = e or e <=> e
00299     Expr d_expr;
00300     // We do not take assumptions, only the proof object
00301     ReflexivityTheoremValue(TheoremManager* tm, const Expr& e, const Proof& pf)
00302       : TheoremValue(tm, Expr(), Assumptions(), pf, false), d_expr(e) { }
00303 
00304     ~ReflexivityTheoremValue() { }
00305 
00306     bool isRewrite() const { return true; }
00307     const Expr& getLHS() const { return d_expr; }
00308     const Expr& getRHS() const { return d_expr; }
00309 
00310     const Expr& getExpr() const {
00311       if (d_thm.isNull()) {
00312         bool isBool = d_expr.getType().isBool();
00313         // have to fake out the const qualifier
00314         Expr* pexpr = const_cast<Expr*>(&d_thm);
00315         *pexpr = isBool ? d_expr.iffExpr(d_expr) : d_expr.eqExpr(d_expr);
00316       }
00317       return d_thm;
00318     }
00319 
00320     // Memory management
00321     MemoryManager* getMM() { return d_tm->getReflMM(); }
00322 
00323     void* operator new(size_t size, MemoryManager* mm) {
00324       return mm->newData(size);
00325     }
00326 
00327     void operator delete(void* d) { }
00328 
00329   }; // end of class ReflexivityTheoremValue
00330 
00331 }; // end of namespace CVCL
00332 
00333 #endif

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