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 #ifndef _cvc3__variable_h_
00027 #define _cvc3__variable_h_
00028
00029 #include "expr.h"
00030
00031 namespace CVC3 {
00032
00033 class VariableManager;
00034 class VariableValue;
00035 class Clause;
00036 class SearchEngineRules;
00037
00038
00039 class Variable {
00040 private:
00041 VariableValue* d_val;
00042
00043 Theorem deriveThmRec(bool checkAssump) const;
00044 public:
00045
00046 Variable(): d_val(NULL) { }
00047
00048
00049 Variable(VariableManager* vm, const Expr& e);
00050
00051 Variable(const Variable& l);
00052
00053 ~Variable();
00054
00055 Variable& operator=(const Variable& l);
00056
00057 bool isNull() const { return d_val == NULL; }
00058
00059
00060
00061
00062
00063 const Expr& getExpr() const;
00064
00065
00066 const Expr& getNegExpr() const;
00067
00068
00069 int getValue() const;
00070
00071
00072 int getScope() const;
00073 const Theorem& getTheorem() const;
00074 const Clause& getAntecedent() const;
00075
00076
00077 int getAntecedentIdx() const;
00078
00079
00080
00081 const Theorem& getAssumpThm() const;
00082
00083
00084
00085 void setValue(int val, const Clause& c, int idx);
00086
00087
00088
00089 void setValue(const Theorem& thm);
00090 void setValue(const Theorem& thm, int scope);
00091
00092 void setAssumpThm(const Theorem& a, int scope);
00093
00094
00095
00096
00097 Theorem deriveTheorem() const;
00098
00099
00100 unsigned& count(bool neg);
00101 unsigned& countPrev(bool neg);
00102 int& score(bool neg);
00103 bool& added(bool neg);
00104
00105 unsigned count(bool neg) const;
00106 unsigned countPrev(bool neg) const;
00107 int score(bool neg) const;
00108 bool added(bool neg) const;
00109
00110 std::vector<std::pair<Clause, int> >& wp(bool neg) const;
00111
00112 friend bool operator==(const Variable& l1, const Variable& l2) {
00113 return l1.d_val == l2.d_val;
00114 }
00115
00116 friend std::ostream& operator<<(std::ostream& os, const Variable& l);
00117 std::string toString() const;
00118 };
00119
00120 class Literal {
00121 private:
00122 Variable d_var;
00123 bool d_negative;
00124 public:
00125
00126 Literal(const Variable& v, bool positive = true)
00127 : d_var(v), d_negative(!positive) { }
00128
00129 Literal(): d_negative(false) { }
00130
00131
00132 Literal(VariableManager* vm, const Expr& e)
00133 : d_var(vm, (e.isNot())? e[0] : e), d_negative(e.isNot()) { }
00134 Variable& getVar() { return d_var; }
00135 const Variable& getVar() const { return d_var; }
00136 bool isPositive() const { return !d_negative; }
00137 bool isNegative() const { return d_negative; }
00138 bool isNull() const { return d_var.isNull(); }
00139
00140 const Expr& getExpr() const {
00141 if(d_negative) return d_var.getNegExpr();
00142 else return d_var.getExpr();
00143 }
00144 int getValue() const {
00145 if(d_negative) return -(d_var.getValue());
00146 else return d_var.getValue();
00147 }
00148 int getScope() const { return getVar().getScope(); }
00149
00150
00151
00152
00153 void setValue(const Theorem& thm) {
00154 d_var.setValue(thm, thm.getScope());
00155 }
00156 void setValue(const Theorem& thm, int scope) {
00157 d_var.setValue(thm, scope);
00158 }
00159 const Theorem& getTheorem() const { return d_var.getTheorem(); }
00160
00161
00162
00163
00164 Theorem deriveTheorem() const { return d_var.deriveTheorem(); }
00165
00166 unsigned& count() { return d_var.count(d_negative); }
00167 unsigned& countPrev() { return d_var.countPrev(d_negative); }
00168 int& score() { return d_var.score(d_negative); }
00169 bool& added() { return d_var.added(d_negative); }
00170
00171 unsigned count() const { return d_var.count(d_negative); }
00172 unsigned countPrev() const { return d_var.countPrev(d_negative); }
00173 int score() const { return d_var.score(d_negative); }
00174 bool added() const { return d_var.added(d_negative); }
00175
00176 std::vector<std::pair<Clause, int> >& wp() const
00177 { return d_var.wp(d_negative); }
00178
00179 friend std::ostream& operator<<(std::ostream& os, const Literal& l);
00180 std::string toString() const;
00181
00182 friend bool operator==(const Literal& l1, const Literal& l2) {
00183 return (l1.d_negative == l2.d_negative && l1.d_var==l1.d_var);
00184 }
00185 };
00186
00187
00188 inline Literal operator!(const Variable& v) {
00189 return Literal(v, false);
00190 }
00191
00192 inline Literal operator!(const Literal& l) {
00193 return Literal(l.getVar(), l.isNegative());
00194 }
00195
00196 std::ostream& operator<<(std::ostream& os, const Literal& l);
00197
00198 }
00199
00200
00201 #include "clause.h"
00202
00203 namespace CVC3 {
00204
00205
00206 class VariableValue {
00207 friend class Variable;
00208 friend class VariableManager;
00209 private:
00210 VariableManager* d_vm;
00211 int d_refcount;
00212
00213 Expr d_expr;
00214
00215 Expr d_neg;
00216
00217
00218
00219
00220 unsigned d_count;
00221 unsigned d_countPrev;
00222 int d_score;
00223
00224 unsigned d_negCount;
00225 unsigned d_negCountPrev;
00226 int d_negScore;
00227
00228 bool d_added;
00229 bool d_negAdded;
00230
00231 std::vector<std::pair<Clause, int> > d_wp;
00232 std::vector<std::pair<Clause, int> > d_negwp;
00233
00234
00235
00236
00237 CDO<int>* d_val;
00238 CDO<int>* d_scope;
00239
00240 CDO<Theorem>* d_thm;
00241 CDO<Clause>* d_ante;
00242 CDO<int>* d_anteIdx;
00243 CDO<Theorem>* d_assump;
00244
00245 VariableValue(VariableManager* vm, const Expr& e)
00246 : d_vm(vm), d_refcount(0), d_expr(e),
00247 d_count(0), d_countPrev(0), d_score(0),
00248 d_negCount(0), d_negCountPrev(0), d_negScore(0),
00249 d_added(false), d_negAdded(false),
00250 d_val(NULL), d_scope(NULL), d_thm(NULL),
00251 d_ante(NULL), d_anteIdx(NULL), d_assump(NULL) { }
00252 public:
00253 ~VariableValue();
00254
00255 const Expr& getExpr() const { return d_expr; }
00256
00257 const Expr& getNegExpr() const {
00258 if(d_neg.isNull()) {
00259 const_cast<VariableValue*>(this)->d_neg
00260 = d_expr.negate();
00261 }
00262 return d_neg;
00263 }
00264
00265 int getValue() const {
00266 if(d_val==NULL) return 0;
00267 else return d_val->get();
00268 }
00269
00270 int getScope() const {
00271 if(d_scope==NULL) return 0;
00272 else return d_scope->get();
00273 }
00274
00275 const Theorem& getTheorem() const {
00276 static Theorem null;
00277 if(d_thm==NULL) return null;
00278 else return d_thm->get();
00279 }
00280
00281 const Clause& getAntecedent() const {
00282 static Clause null;
00283 if(d_ante==NULL) return null;
00284 else return d_ante->get();
00285 }
00286
00287 int getAntecedentIdx() const {
00288 if(d_anteIdx==NULL) return 0;
00289 else return d_anteIdx->get();
00290 }
00291
00292 const Theorem& getAssumpThm() const {
00293 static Theorem null;
00294 if(d_assump==NULL) return null;
00295 else return d_assump->get();
00296 }
00297
00298
00299
00300 void setValue(int val, const Clause& c, int idx);
00301
00302
00303 void setValue(const Theorem& thm, int scope);
00304
00305 void setAssumpThm(const Theorem& a, int scope);
00306
00307
00308 unsigned& count(bool neg) {
00309 if(neg) return d_negCount;
00310 else return d_count;
00311 }
00312 unsigned& countPrev(bool neg) {
00313 if(neg) return d_negCountPrev;
00314 else return d_countPrev;
00315 }
00316 int& score(bool neg) {
00317 if(neg) return d_negScore;
00318 else return d_score;
00319 }
00320 bool& added(bool neg) {
00321 if(neg) return d_negAdded;
00322 else return d_added;
00323 }
00324
00325
00326 void* operator new(size_t size, MemoryManager* mm) {
00327 return mm->newData(size);
00328 }
00329 void operator delete(void* pMem, MemoryManager* mm) {
00330 mm->deleteData(pMem);
00331 }
00332 void operator delete(void*) { }
00333
00334
00335 friend std::ostream& operator<<(std::ostream& os, const VariableValue& v);
00336 friend bool operator==(const VariableValue& v1, const VariableValue& v2) {
00337 return v1.d_expr == v2.d_expr;
00338 }
00339 };
00340
00341
00342 inline unsigned& Variable::count(bool neg) { return d_val->count(neg); }
00343 inline unsigned& Variable::countPrev(bool neg)
00344 { return d_val->countPrev(neg); }
00345 inline int& Variable::score(bool neg) { return d_val->score(neg); }
00346 inline bool& Variable::added(bool neg) { return d_val->added(neg); }
00347
00348 inline unsigned Variable::count(bool neg) const { return d_val->count(neg); }
00349 inline unsigned Variable::countPrev(bool neg) const
00350 { return d_val->countPrev(neg); }
00351 inline int Variable::score(bool neg) const { return d_val->score(neg); }
00352 inline bool Variable::added(bool neg) const { return d_val->added(neg); }
00353
00354 inline std::vector<std::pair<Clause, int> >& Variable::wp(bool neg) const {
00355 if(neg) return d_val->d_negwp;
00356 else return d_val->d_wp;
00357 }
00358
00359
00360 class VariableManagerNotifyObj;
00361
00362
00363 class VariableManager {
00364 friend class Variable;
00365 friend class VariableValue;
00366 private:
00367 ContextManager* d_cm;
00368 MemoryManager* d_mm;
00369 SearchEngineRules* d_rules;
00370 VariableManagerNotifyObj* d_notifyObj;
00371
00372
00373
00374
00375 bool d_disableGC;
00376
00377 bool d_postponeGC;
00378
00379 std::vector<VariableValue*> d_deleted;
00380
00381
00382 class HashLV {
00383 public:
00384 size_t operator()(VariableValue* v) const { return v->getExpr().hash(); }
00385 };
00386 class EqLV {
00387 public:
00388 bool operator()(const VariableValue* lv1, const VariableValue* lv2) const
00389 { return lv1->getExpr() == lv2->getExpr(); }
00390 };
00391
00392
00393 typedef std::hash_set<VariableValue*, HashLV, EqLV> VariableValueSet;
00394 VariableValueSet d_varSet;
00395
00396
00397 VariableValue* newVariableValue(const Expr& e);
00398
00399 public:
00400
00401 VariableManager(ContextManager* cm, SearchEngineRules* rules,
00402 const std::string& mmFlag);
00403
00404 ~VariableManager();
00405
00406
00407 void gc(VariableValue* v);
00408
00409 void postponeGC() { d_postponeGC = true; }
00410
00411 void resumeGC();
00412
00413 ContextManager* getCM() const { return d_cm; }
00414 SearchEngineRules* getRules() const { return d_rules; }
00415
00416 };
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432 class VariableManagerNotifyObj: public ContextNotifyObj {
00433 VariableManager* d_vm;
00434 public:
00435
00436 VariableManagerNotifyObj(VariableManager* vm, Context* cxt)
00437 : ContextNotifyObj(cxt), d_vm(vm) { }
00438
00439 void notifyPre(void) { d_vm->postponeGC(); }
00440 void notify(void) { d_vm->resumeGC(); }
00441 };
00442
00443
00444 }
00445 #endif