00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 #ifndef _cvcl__include__vcl_h_
00031 #define _cvcl__include__vcl_h_
00032 
00033 #include <queue>
00034 #include <fstream>
00035 
00036 #include "vc.h"
00037 #include "command_line_flags.h"
00038 #include "statistics.h"
00039 #include "cdmap.h"
00040 
00041 namespace CVCL {
00042 
00043   class SearchEngine;
00044   class Theory;
00045   class TheoryCore;
00046   class TheoryUF;
00047   class TheoryArith;
00048   class TheoryArray;
00049   class TheoryQuant;
00050   class TheoryRecords;
00051   class TheorySimulate;
00052   class TheoryBitvector;
00053   class TheoryDatatype;
00054   class Translator;
00055 
00056 typedef std::map<const std::string, Expr, ltstr> Str_To_Expr; 
00057   
00058 class VCL : public ValidityChecker {
00059 
00060 
00061   ExprManager* d_em;
00062   ContextManager* d_cm;
00063   TheoremManager* d_tm;
00064   SearchEngine* d_se;
00065 
00066 
00067   TheoryCore* d_theoryCore;
00068   TheoryUF* d_theoryUF;
00069   TheoryArith* d_theoryArith;
00070   TheoryArray* d_theoryArray;
00071   TheoryQuant* d_theoryQuant;
00072   TheoryRecords* d_theoryRecords;
00073   TheorySimulate* d_theorySimulate;
00074   TheoryBitvector* d_theoryBitvector;
00075   TheoryDatatype* d_theoryDatatype;
00076   Translator* d_translator;
00077 
00078 
00079 
00080   std::vector<Theory*> d_theories;
00081 
00082 
00083   CLFlags *d_flags;
00084 
00085 
00086   std::vector<int> d_scopeStack;
00087 
00088 
00089   Str_To_Expr d_vars;
00090 
00091 
00092   Statistics d_statistics;
00093 
00094 
00095   size_t d_nextIdx;
00096 
00097 
00098   class UserAssertion {
00099     size_t d_idx;
00100     Theorem d_thm; 
00101     Theorem d_tcc; 
00102   public:
00103 
00104     UserAssertion() { }
00105 
00106     UserAssertion(const Theorem& thm, const Theorem& tcc, size_t idx)
00107       : d_idx(idx), d_thm(thm), d_tcc(tcc) { }
00108 
00109     const Theorem& thm() const { return d_thm; }
00110 
00111     const Theorem& tcc() const { return d_tcc; }
00112 
00113     operator Theorem() { return d_thm; }
00114 
00115     friend bool operator<(const UserAssertion& a1, const UserAssertion& a2) {
00116       return (a1.d_idx < a2.d_idx);
00117     }
00118   };
00119 
00120 
00121   CDMap<Expr,UserAssertion>* d_userAssertions;
00122 
00123 
00124 
00125 
00126   Theorem3 d_lastQuery;
00127 
00128 
00129   Theorem d_lastQueryTCC;
00130 
00131 
00132   Theorem3 d_lastClosure;
00133 
00134 
00135   std::ostream* d_osdump;
00136   std::ostream* d_osdumpTranslate;
00137   std::ofstream d_osdumpFile, d_osdumpTranslateFile;
00138   std::ifstream d_tmpFile;
00139   const bool& d_translate;
00140   bool d_dump, d_dumpFileOpen, d_translateFileOpen;
00141 
00142   
00143 
00144 
00145   Theorem3 deriveClosure(const Theorem3& thm);
00146 
00147 
00148 
00149 
00150 
00151 
00152   void getAssumptionsRec(const Theorem& thm,
00153                          std::set<UserAssertion>& assumptions,
00154                          bool addTCCs);
00155 
00156 
00157   void getAssumptions(const Assumptions& a, std::vector<Expr>& assumptions);
00158 
00159   IF_DEBUG(
00160 
00161     void dumpTrace(int scope);
00162   )
00163 
00164 public:
00165   
00166   VCL(const CLFlags& flags);
00167   ~VCL();
00168 
00169   
00170 
00171   CLFlags& getFlags() const { return *d_flags; }
00172   void reprocessFlags();
00173 
00174   Type boolType();
00175   Type realType();
00176   Type intType();
00177   Type subrangeType(const Expr& l, const Expr& r);
00178   Type subtypeType(const Expr& pred);
00179   Type tupleType(const Type& type0, const Type& type1);
00180   Type tupleType(const Type& type0, const Type& type1, const Type& type2);
00181   Type tupleType(const std::vector<Type>& types);
00182   Type recordType(const std::string& field, const Type& type);
00183   Type recordType(const std::string& field0, const Type& type0,
00184                   const std::string& field1, const Type& type1);
00185   Type recordType(const std::string& field0, const Type& type0,
00186                   const std::string& field1, const Type& type1,
00187                   const std::string& field2, const Type& type2);
00188   Type recordType(const std::vector<std::string>& fields,
00189                   const std::vector<Type>& types);
00190   Type dataType(const std::string& name,
00191                 const std::string& constructor,
00192                 const std::vector<std::string>& selectors,
00193                 const std::vector<Expr>& types);
00194   Type dataType(const std::string& name,
00195                 const std::vector<std::string>& constructors,
00196                 const std::vector<std::vector<std::string> >& selectors,
00197                 const std::vector<std::vector<Expr> >& types);
00198   void dataType(const std::vector<std::string>& names,
00199                 const std::vector<std::vector<std::string> >& constructors,
00200                 const std::vector<std::vector<std::vector<std::string> > >& selectors,
00201                 const std::vector<std::vector<std::vector<Expr> > >& types,
00202                 std::vector<Type>& returnTypes);
00203   Type arrayType(const Type& typeIndex, const Type& typeData);
00204   Type funType(const Type& typeDom, const Type& typeRan);
00205   Type funType(const std::vector<Type>& typeDom, const Type& typeRan);
00206   Type createType(const std::string& typeName);
00207   Type createType(const std::string& typeName, const Type& def);
00208   Type lookupType(const std::string& typeName);
00209 
00210   ExprManager* getEM() { return d_em; }
00211   Expr varExpr(const std::string& name, const Type& type);
00212   Expr varExpr(const std::string& name, const Type& type, const Expr& def);
00213   Expr boundVarExpr(const std::string& name, const std::string& uid,
00214                     const Type& type);
00215   Expr lookupVar(const std::string& name, Type* type);
00216   Type getType(const Expr& e);
00217   Type getBaseType(const Expr& e);
00218   Type getBaseType(const Type& e);
00219   Expr getTypePred(const Type&t, const Expr& e);
00220   Expr stringExpr(const std::string& str);
00221   Expr idExpr(const std::string& name);
00222   Expr listExpr(const std::vector<Expr>& kids);
00223   Expr listExpr(const Expr& e1);
00224   Expr listExpr(const Expr& e1, const Expr& e2);
00225   Expr listExpr(const Expr& e1, const Expr& e2, const Expr& e3);
00226   Expr listExpr(const std::string& op, const std::vector<Expr>& kids);
00227   Expr listExpr(const std::string& op, const Expr& e1);
00228   Expr listExpr(const std::string& op, const Expr& e1,
00229                 const Expr& e2);
00230   Expr listExpr(const std::string& op, const Expr& e1,
00231                 const Expr& e2, const Expr& e3);
00232   void printExpr(const Expr& e);
00233   void printExpr(const Expr& e, std::ostream& os);
00234   Expr parseExpr(const Expr& e);
00235   Type parseType(const Expr& e);
00236   Expr importExpr(const Expr& e);
00237   Type importType(const Type& t);
00238 
00239   Expr trueExpr();
00240   Expr falseExpr();
00241   Expr notExpr(const Expr& child);
00242   Expr andExpr(const Expr& left, const Expr& right);
00243   Expr andExpr(const std::vector<Expr>& children);
00244   Expr orExpr(const Expr& left, const Expr& right);
00245   Expr orExpr(const std::vector<Expr>& children);
00246   Expr impliesExpr(const Expr& hyp, const Expr& conc);
00247   Expr iffExpr(const Expr& left, const Expr& right);
00248   Expr eqExpr(const Expr& child0, const Expr& child1);
00249   Expr iteExpr(const Expr& ifpart, const Expr& thenpart, const Expr& elsepart);
00250 
00251   Op createOp(const std::string& name, const Type& type);
00252   Op createOp(const std::string& name, const Type& type, const Expr& def);
00253   Expr funExpr(const Op& op, const Expr& child);
00254   Expr funExpr(const Op& op, const Expr& left, const Expr& right);
00255   Expr funExpr(const Op& op, const Expr& child0, const Expr& child1, const Expr& child2);
00256   Expr funExpr(const Op& op, const std::vector<Expr>& children);
00257 
00258   Expr ratExpr(int n, int d);
00259   Expr ratExpr(const std::string& n, const std::string& d, int base);
00260   Expr ratExpr(const std::string& n, int base);
00261   Expr uminusExpr(const Expr& child);
00262   Expr plusExpr(const Expr& left, const Expr& right);
00263   Expr minusExpr(const Expr& left, const Expr& right);
00264   Expr multExpr(const Expr& left, const Expr& right);
00265   Expr powExpr(const Expr& x, const Expr& n);
00266   Expr divideExpr(const Expr& left, const Expr& right);
00267   Expr ltExpr(const Expr& left, const Expr& right);
00268   Expr leExpr(const Expr& left, const Expr& right);
00269   Expr gtExpr(const Expr& left, const Expr& right);
00270   Expr geExpr(const Expr& left, const Expr& right);
00271 
00272   Expr recordExpr(const std::string& field, const Expr& expr);
00273   Expr recordExpr(const std::string& field0, const Expr& expr0,
00274                   const std::string& field1, const Expr& expr1);
00275   Expr recordExpr(const std::string& field0, const Expr& expr0,
00276                   const std::string& field1, const Expr& expr1,
00277                   const std::string& field2, const Expr& expr2);
00278   Expr recordExpr(const std::vector<std::string>& fields,
00279                   const std::vector<Expr>& exprs);
00280   Expr recSelectExpr(const Expr& record, const std::string& field);
00281   Expr recUpdateExpr(const Expr& record, const std::string& field,
00282                      const Expr& newValue);
00283 
00284   Expr readExpr(const Expr& array, const Expr& index);
00285   Expr writeExpr(const Expr& array, const Expr& index, const Expr& newValue);
00286 
00287   Expr tupleExpr(const std::vector<Expr>& exprs);
00288   Expr tupleSelectExpr(const Expr& tuple, int index);
00289   Expr tupleUpdateExpr(const Expr& tuple, int index, const Expr& newValue);
00290   Expr datatypeConsExpr(const std::string& constructor,
00291                         const std::vector<Expr>& args);
00292   Expr datatypeSelExpr(const std::string& selector, const Expr& arg);
00293   Expr datatypeTestExpr(const std::string& constructor, const Expr& arg);
00294   Expr forallExpr(const std::vector<Expr>& vars, const Expr& body);
00295   Expr existsExpr(const std::vector<Expr>& vars, const Expr& body);
00296   Op lambdaExpr(const std::vector<Expr>& vars, const Expr& body);
00297   Expr simulateExpr(const Expr& f, const Expr& s0,
00298                     const std::vector<Expr>& inputs, const Expr& n);
00299 
00300   void setResourceLimit(unsigned limit);
00301   void assertFormula(const Expr& e);
00302   void registerAtom(const Expr& e);
00303   Expr getImpliedLiteral();
00304   Expr simplify(const Expr& e);
00305   Theorem3 simplifyThm(const Expr& e);
00306   Theorem simplifyThm2(const Expr& e);
00307   void printV(const Expr& e);
00308   bool query(const Expr& e, bool isTCC = false);
00309   bool checkUnsat(const Expr& e);
00310   bool checkContinue();
00311   bool restart(const Expr& e);
00312   void returnFromCheck();
00313   void getUserAssumptions(std::vector<Expr>& assumptions);
00314   void getInternalAssumptions(std::vector<Expr>& assumptions);
00315   void getAssumptions(std::vector<Expr>& assumptions);
00316   void getAssumptionsUsed(std::vector<Expr>& assumptions);
00317   void getCounterExample(std::vector<Expr>& assertions, bool inOrder);
00318   void getConcreteModel(ExprMap<Expr> & m);
00319   bool inconsistent(std::vector<Expr>& assumptions);
00320   bool incomplete();
00321   bool incomplete(std::vector<std::string>& reasons);
00322   Proof getProof();
00323   const Expr& getTCC();
00324   void getAssumptionsTCC(std::vector<Expr>& assumptions);
00325   const Proof& getProofTCC();
00326   const Expr& getClosure();
00327   const Proof& getProofClosure();
00328 
00329   int stackLevel();
00330   void push();
00331   void pop();
00332   void popto(int stackLevel);
00333   int scopeLevel();
00334   void pushScope();
00335   void popScope();
00336   void poptoScope(int scopeLevel);
00337   Context* getCurrentContext();
00338 
00339   void loadFile(const std::string& fileName,
00340                 InputLanguage lang = PRESENTATION_LANG,
00341                 bool interactive = false);
00342   void loadFile(std::istream& is,
00343                 InputLanguage lang = PRESENTATION_LANG,
00344                 bool interactive = false);
00345 
00346   Statistics& getStatistics() { return d_statistics; }
00347   void printStatistics() { std::cout << d_statistics << std::endl; }
00348 
00349 };
00350 
00351 }
00352 
00353 #endif