00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 #ifndef _cvc3__include__theory_arith3_h_
00022 #define _cvc3__include__theory_arith3_h_
00023 
00024 #include "theory_arith.h"
00025 
00026 namespace CVC3 {
00027 
00028 class TheoryArith3 :public TheoryArith {
00029   CDList<Theorem> d_diseq;  
00030   CDO<size_t> d_diseqIdx; 
00031   ArithProofRules* d_rules;
00032   CDO<bool> d_inModelCreation;
00033 
00034 
00035   class FreeConst {
00036   private:
00037     Rational d_r;
00038     bool d_strict;
00039   public:
00040     FreeConst() { }
00041     FreeConst(const Rational& r, bool strict): d_r(r), d_strict(strict) { }
00042     const Rational& getConst() const { return d_r; }
00043     bool strict() const { return d_strict; }
00044   };
00045 
00046   friend std::ostream& operator<<(std::ostream& os, const FreeConst& fc);
00047 
00048 
00049   class Ineq {
00050   private:
00051     Theorem d_ineq; 
00052     bool d_rhs; 
00053     const FreeConst* d_const; 
00054 
00055     Ineq() { }
00056   public:
00057 
00058     Ineq(const Theorem& ineq, bool varOnRHS, const FreeConst& c):
00059       d_ineq(ineq), d_rhs(varOnRHS), d_const(&c) { }
00060 
00061     const Theorem& ineq() const { return d_ineq; }
00062 
00063     const FreeConst& getConst() const { return *d_const; }
00064 
00065     bool varOnRHS() const { return d_rhs; }
00066 
00067     bool varOnLHS() const { return !d_rhs; }
00068 
00069     operator Theorem() const { return d_ineq; }
00070   };
00071 
00072   friend std::ostream& operator<<(std::ostream& os, const Ineq& ineq);
00073 
00074 
00075   ExprMap<CDList<Ineq> *> d_inequalitiesRightDB;
00076 
00077 
00078   ExprMap<CDList<Ineq> *> d_inequalitiesLeftDB;
00079 
00080 
00081 
00082 
00083 
00084 
00085 
00086 
00087 
00088   CDMap<Expr, FreeConst> d_freeConstDB;
00089 
00090   
00091   CDList<Theorem> d_buffer; 
00092 
00093   CDO<size_t> d_bufferIdx; 
00094 
00095   const int* d_bufferThres; 
00096 
00097   
00098 
00099 
00100 
00101   CDMap<Expr, int> d_countRight;
00102 
00103 
00104 
00105   CDMap<Expr, int> d_countLeft;
00106 
00107 
00108   CDMap<Expr, bool> d_sharedTerms;
00109 
00110 
00111   CDMap<Expr, bool> d_sharedVars;
00112 
00113   
00114   
00115   class VarOrderGraph {
00116     ExprMap<std::vector<Expr> > d_edges;
00117     ExprMap<bool> d_cache;
00118     bool dfs(const Expr& e1, const Expr& e2);
00119   public:
00120     void addEdge(const Expr& e1, const Expr& e2);
00121     
00122     bool lessThan(const Expr& e1, const Expr& e2);
00123     
00124     
00125     void selectLargest(const std::vector<Expr>& v1, std::vector<Expr>& v2);
00126     
00127     
00128     void selectSmallest( std::vector<Expr>& v1, std::vector<Expr>& v2);
00129   };
00130   
00131   VarOrderGraph d_graph;
00132 
00133   
00134 
00135 
00136 
00137   Theorem isIntegerThm(const Expr& e);
00138 
00139 
00140 
00141   Theorem isIntegerDerive(const Expr& isIntE, const Theorem& thm);
00142 
00143 
00144   const Rational& freeConstIneq(const Expr& ineq, bool varOnRHS);
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152   const FreeConst& updateSubsumptionDB(const Expr& ineq, bool varOnRHS,
00153               bool& subsumed);
00154 
00155   bool kidsCanonical(const Expr& e);
00156 
00157 
00158   Theorem canon(const Expr& e);
00159 
00160 
00161 
00162   Theorem canonSimplify(const Expr& e);
00163 
00164 
00165 
00166 
00167   Theorem canonSimplify(const Theorem& thm) {
00168     return transitivityRule(thm, canonSimplify(thm.getRHS()));
00169   }
00170 
00171 
00172   Theorem canonPred(const Theorem& thm);
00173 
00174 
00175 
00176   Theorem canonPredEquiv(const Theorem& thm);
00177 
00178 
00179   Theorem doSolve(const Theorem& e);
00180 
00181 
00182   Theorem canonConjunctionEquiv(const Theorem& thm);
00183 
00184 
00185   
00186   bool pickIntEqMonomial(const Expr& right, Expr& isolated, bool& nonlin);
00187 
00188 
00189   Theorem processRealEq(const Theorem& eqn);
00190 
00191 
00192   Theorem processIntEq(const Theorem& eqn);
00193 
00194 
00195   Theorem processSimpleIntEq(const Theorem& eqn);
00196 
00197 
00198   void processBuffer();
00199 
00200 
00201   Theorem isolateVariable(const Theorem& inputThm, bool& e1);
00202 
00203 
00204   void updateStats(const Rational& c, const Expr& var);
00205 
00206 
00207   void updateStats(const Expr& monomial);
00208 
00209 
00210   void addToBuffer(const Theorem& thm);
00211 
00212 
00213 
00214   Expr computeNormalFactor(const Expr& rhs);
00215 
00216 
00217   Theorem normalize(const Expr& e);
00218 
00219 
00220 
00221 
00222 
00223   Theorem normalize(const Theorem& thm);
00224 
00225   Expr pickMonomial(const Expr& right);
00226 
00227   void getFactors(const Expr& e, std::set<Expr>& factors);
00228 
00229  public: 
00230 
00231   void separateMonomial(const Expr& e, Expr& c, Expr& var);
00232 
00233   bool isInteger(const Expr& e) { return !(isIntegerThm(e).isNull()); }
00234 
00235 
00236  private:
00237 
00238   bool lessThanVar(const Expr& isolatedVar, const Expr& var2);
00239 
00240 
00241   bool isStale(const Expr& e);
00242 
00243 
00244   bool isStale(const Ineq& ineq);
00245 
00246   void projectInequalities(const Theorem& theInequality,bool isolatedVarOnRHS);
00247 
00248   void assignVariables(std::vector<Expr>&v);
00249 
00250   void findRationalBound(const Expr& varSide, const Expr& ratSide, 
00251        const Expr& var,
00252        Rational &r);
00253 
00254   bool findBounds(const Expr& e, Rational& lub, Rational&  glb);
00255 
00256   Theorem normalizeProjectIneqs(const Theorem& ineqThm1,
00257         const Theorem& ineqThm2);
00258 
00259 
00260   Theorem solvedForm(const std::vector<Theorem>& solvedEqs);
00261 
00262 
00263 
00264 
00265   Theorem substAndCanonize(const Expr& t, ExprMap<Theorem>& subst);
00266 
00267 
00268 
00269 
00270 
00271   Theorem substAndCanonize(const Theorem& eq, ExprMap<Theorem>& subst);
00272 
00273 
00274   void collectVars(const Expr& e, std::vector<Expr>& vars,
00275        std::set<Expr>& cache);
00276 
00277 
00278 
00279 
00280   void processFiniteInterval(const Theorem& alphaLEax,
00281            const Theorem& bxLEbeta);
00282 
00283 
00284   void processFiniteIntervals(const Expr& x);
00285 
00286 
00287   void setupRec(const Expr& e);
00288 
00289 public:
00290   TheoryArith3(TheoryCore* core);
00291   ~TheoryArith3();
00292 
00293   
00294   
00295   ArithProofRules* createProofRules3();
00296 
00297   
00298   void addSharedTerm(const Expr& e);
00299   void assertFact(const Theorem& e);
00300   void refineCounterExample();
00301   void computeModelBasic(const std::vector<Expr>& v);
00302   void computeModel(const Expr& e, std::vector<Expr>& vars);
00303   void checkSat(bool fullEffort);
00304   Theorem rewrite(const Expr& e);
00305   void setup(const Expr& e);
00306   void update(const Theorem& e, const Expr& d);
00307   Theorem solve(const Theorem& e);
00308   void checkAssertEqInvariant(const Theorem& e);
00309   void checkType(const Expr& e);
00310   Cardinality finiteTypeInfo(Expr& e, Unsigned& n,
00311                              bool enumerate, bool computeSize);
00312   void computeType(const Expr& e);
00313   Type computeBaseType(const Type& t);
00314   void computeModelTerm(const Expr& e, std::vector<Expr>& v);
00315   Expr computeTypePred(const Type& t, const Expr& e);
00316   Expr computeTCC(const Expr& e);
00317   ExprStream& print(ExprStream& os, const Expr& e);
00318   Expr parseExprOp(const Expr& e);
00319 
00320 private:
00321   
00322   
00323   CDMap<Expr, Rational> maxCoefficientLeft;
00324   CDMap<Expr, Rational> maxCoefficientRight;
00325   
00326 
00327   CDMap<Expr, Rational> fixedMaxCoefficient;  
00328   
00329 
00330 
00331 
00332 
00333 
00334   Rational currentMaxCoefficient(Expr var);
00335   
00336 
00337 
00338 
00339 
00340 
00341 
00342  
00343   void fixCurrentMaxCoefficient(Expr variable, Rational max);
00344   
00345 
00346 
00347 
00348   void selectSmallestByCoefficient(std::vector<Expr> input, std::vector<Expr>& output);
00349 };
00350 
00351 }
00352 
00353 #endif