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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #ifndef _CVC_lite__expr_h_
00048 #include "expr.h"
00049 #endif
00050
00051 #ifndef _CVC_lite__expr_value_h_
00052 #define _CVC_lite__expr_value_h_
00053
00054 #include "theorem.h"
00055 #include "type.h"
00056
00057
00058 #define PRIME 131
00059
00060 namespace CVCL {
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 class ExprValue {
00078 friend class Expr;
00079 friend class Expr::iterator;
00080 friend class ExprManager;
00081 friend class ::CInterface;
00082 friend class ExprApply;
00083
00084
00085 ExprIndex d_index;
00086
00087
00088 int d_refcount;
00089
00090
00091 unsigned d_flag;
00092
00093
00094 size_t d_hash;
00095
00096
00097 CDO<Theorem>* d_find;
00098
00099
00100 Type d_type;
00101
00102
00103 Expr d_tcc;
00104
00105
00106 Theorem d_subtypePred;
00107
00108
00109 NotifyList* d_notifyList;
00110
00111
00112 Theorem d_simpCache;
00113
00114
00115 unsigned d_simpCacheTag;
00116
00117
00118 CDFlags d_dynamicFlags;
00119
00120
00121 int d_height;
00122
00123
00124 int d_highestKid;
00125
00126
00127 Expr d_simpFrom;
00128
00129 protected:
00130
00131 ExprManager* d_em;
00132
00133
00134
00135 int d_kind;
00136
00137
00138
00139 private:
00140
00141
00142 void setIndex(ExprIndex idx) { d_index = idx; }
00143
00144
00145 void incRefcount() { ++d_refcount; }
00146
00147
00148 void decRefcount() {
00149
00150
00151 FatalAssert(d_refcount > 0, "Mis-handled the ref. counting");
00152 if((--d_refcount) == 0) d_em->gc(this);
00153 }
00154
00155
00156
00157
00158 size_t hash() const {
00159 if(d_hash == 0)
00160 const_cast<ExprValue*>(this)->d_hash = computeHash();
00161 return d_hash;
00162 }
00163
00164
00165 int getHeight() const { return d_height; }
00166
00167
00168 int getHighestKid() const { return d_highestKid; }
00169
00170
00171 const Expr& getSimpFrom() const { return d_simpFrom; }
00172
00173
00174 void setSimpFrom(const Expr& simpFrom) { d_simpFrom = simpFrom; }
00175
00176 protected:
00177
00178
00179
00180
00181 static std::hash<char*> s_charHash;
00182 static std::hash<long int> s_intHash;
00183
00184 static size_t pointerHash(void* p) { return s_intHash((long int)p); }
00185
00186 static size_t hash(const int kind, const std::vector<Expr>& kids);
00187
00188 static size_t hash(const int n) { return s_intHash((long int)n); }
00189
00190
00191 MemoryManager* getMM(size_t MMIndex) {
00192 DebugAssert(d_em!=NULL, "ExprValue::getMM()");
00193 return d_em->getMM(MMIndex);
00194 }
00195
00196
00197 ExprValue* rebuild(ExprManager* em) const
00198 { return copy(em, 0); }
00199
00200
00201 Expr rebuild(Expr e, ExprManager* em) const
00202 { return em->rebuildRec(e); }
00203
00204
00205
00206
00207
00208 virtual size_t computeHash() const { return hash(d_kind); }
00209
00210
00211 virtual ExprValue* copy(ExprManager* em, ExprIndex idx) const;
00212
00213 public:
00214
00215 ExprValue(ExprManager* em, int kind, ExprIndex idx = 0)
00216 : d_index(idx), d_refcount(0), d_flag(0),
00217 d_hash(0), d_find(NULL), d_notifyList(NULL),
00218 d_simpCacheTag(0),
00219 d_dynamicFlags(em->getCurrentContext()),
00220 d_height(0), d_highestKid(-1),
00221 d_em(em), d_kind(kind)
00222 {
00223 DebugAssert(em != NULL, "NULL ExprManager is given to ExprValue()");
00224 DebugAssert(em->isKindRegistered(kind),
00225 ("ExprValue(kind = " + int2string(kind)
00226 + ")): kind is not registered").c_str());
00227 DebugAssert(kind != APPLY, "Only ExprApply should have APPLY kind");
00228 }
00229
00230 virtual ~ExprValue();
00231
00232
00233 int getKind() const { return d_kind; }
00234
00235
00236 void* operator new(size_t size, MemoryManager* mm)
00237 { return mm->newData(size); }
00238
00239
00240 void operator delete(void*) { }
00241
00242
00243 virtual size_t getMMIndex() const { return EXPR_VALUE; }
00244
00245
00246 virtual bool operator==(const ExprValue& ev2) const;
00247
00248
00249
00250
00251
00252
00253
00254
00255 virtual const ExprValue* getExprValue() const
00256 { throw Exception("Illegal call to getExprValue()"); }
00257
00258 virtual bool isString() const { return false; }
00259
00260 virtual bool isRational() const { return false; }
00261
00262 virtual bool isVar() const { return false; }
00263
00264 virtual bool isApply() const { return false; }
00265
00266 virtual bool isSymbol() const { return false; }
00267
00268 virtual bool isClosure() const { return false; }
00269
00270
00271 virtual const std::vector<Expr>& getKids() const
00272 { return d_em->getEmptyVector(); }
00273
00274
00275
00276
00277 virtual unsigned arity() const { return 0; }
00278
00279
00280 virtual CDO<Theorem>* getSig() const {
00281 DebugAssert(false, "getSig() is called on ExprValue");
00282 return NULL;
00283 }
00284
00285 virtual CDO<Theorem>* getRep() const {
00286 DebugAssert(false, "getRep() is called on ExprValue");
00287 return NULL;
00288 }
00289
00290 virtual void setSig(CDO<Theorem>* sig) {
00291 DebugAssert(false, "setSig() is called on ExprValue");
00292 }
00293
00294 virtual void setRep(CDO<Theorem>* rep) {
00295 DebugAssert(false, "setRep() is called on ExprValue");
00296 }
00297
00298 virtual const std::string& getUid() const {
00299 static std::string null;
00300 DebugAssert(false, "ExprValue::getUid() called in base class");
00301 return null;
00302 }
00303
00304 virtual const std::string& getString() const {
00305 DebugAssert(false, "getString() is called on ExprValue");
00306 static std::string s("");
00307 return s;
00308 }
00309
00310 virtual const Rational& getRational() const {
00311 DebugAssert(false, "getRational() is called on ExprValue");
00312 static Rational r(0);
00313 return r;
00314 }
00315
00316
00317 virtual const std::string& ExprValue::getName() const {
00318 static std::string ret = "";
00319 DebugAssert(false, "getName() is called on ExprValue");
00320 return ret;
00321 }
00322
00323
00324 virtual const Expr& getVar() const {
00325 DebugAssert(false, "getVar() is called on ExprValue");
00326 static Expr null;
00327 return null;
00328 }
00329
00330
00331 virtual Op getOp() const {
00332 DebugAssert(false, "getOp() is called on ExprValue");
00333 return Op(NULL_KIND);
00334 }
00335
00336 virtual const std::vector<Expr>& getVars() const {
00337 DebugAssert(false, "getVars() is called on ExprValue");
00338 static std::vector<Expr> null;
00339 return null;
00340 }
00341
00342 virtual const Expr& getBody() const {
00343 DebugAssert(false, "getBody() is called on ExprValue");
00344 static Expr null;
00345 return null;
00346 }
00347
00348 virtual const Expr& getExistential() const {
00349 DebugAssert(false, "getExistential() is called on ExprValue");
00350 static Expr null;
00351 return null;
00352 }
00353 virtual int getBoundIndex() const {
00354 DebugAssert(false, "getIndex() is called on ExprValue");
00355 return 0;
00356 }
00357
00358 virtual const std::vector<std::string>& getFields() const {
00359 DebugAssert(false, "getFields() is called on ExprValue");
00360 static std::vector<std::string> null;
00361 return null;
00362 }
00363 virtual const std::string& getField() const {
00364 DebugAssert(false, "getField() is called on ExprValue");
00365 static std::string null;
00366 return null;
00367 }
00368 virtual int getTupleIndex() const {
00369 DebugAssert(false, "getTupleIndex() is called on ExprValue");
00370 return 0;
00371 }
00372
00373 };
00374
00375
00376 class ExprNode: public ExprValue {
00377 friend class Expr;
00378 friend class ExprManager;
00379
00380 protected:
00381
00382 std::vector<Expr> d_children;
00383
00384
00385 CDO<Theorem>* d_sig;
00386 CDO<Theorem>* d_rep;
00387
00388 private:
00389
00390
00391 size_t getMMIndex() const { return EXPR_NODE; }
00392
00393 protected:
00394
00395 unsigned arity() const { return d_children.size(); }
00396
00397
00398 const std::vector<Expr>& getKids() const { return d_children; }
00399
00400
00401 size_t computeHash() const {
00402 return ExprValue::hash(d_kind, d_children);
00403 }
00404
00405
00406 virtual ExprValue* copy(ExprManager* em, ExprIndex idx = 0) const;
00407
00408 public:
00409
00410 ExprNode(ExprManager* em, int kind, const std::vector<Expr>& kids,
00411 unsigned idx = 0)
00412 : ExprValue(em, kind, idx), d_children(kids), d_sig(NULL), d_rep(NULL) { }
00413
00414 virtual ~ExprNode();
00415
00416
00417 void* operator new(size_t size, MemoryManager* mm)
00418 { return mm->newData(size); }
00419
00420
00421 void operator delete(void*) { }
00422
00423
00424 virtual bool operator==(const ExprValue& ev2) const;
00425
00426 virtual CDO<Theorem>* getSig() const { return d_sig; }
00427 virtual CDO<Theorem>* getRep() const { return d_rep; }
00428
00429 virtual void setRep(CDO<Theorem>* rep) { d_rep = rep; }
00430 virtual void setSig(CDO<Theorem>* sig) { d_sig = sig; }
00431
00432 };
00433
00434 class ExprApply: public ExprNode {
00435 friend class Expr;
00436 friend class ExprManager;
00437 private:
00438 Expr d_opExpr;
00439 protected:
00440 size_t getMMIndex() const { return EXPR_APPLY; }
00441 size_t computeHash() const {
00442 return PRIME*ExprNode::computeHash() + d_opExpr.hash();
00443 }
00444 Op getOp() const { return Op(d_opExpr); }
00445 bool isApply() const { return true; }
00446
00447 ExprValue* copy(ExprManager* em, ExprIndex idx = 0) const;
00448 public:
00449
00450 ExprApply(ExprManager* em, const Op& op, unsigned idx = 0)
00451 : ExprNode(em, NULL_KIND, em->getEmptyVector(), idx),
00452 d_opExpr(op.getExpr())
00453 { DebugAssert(!op.getExpr().isNull(), "Expected non-null Op");
00454 d_kind = APPLY; }
00455 ExprApply(ExprManager* em, const Op& op,
00456 const std::vector<Expr>& kids, unsigned idx = 0)
00457 : ExprNode(em, NULL_KIND, kids, idx), d_opExpr(op.getExpr())
00458 { DebugAssert(!op.getExpr().isNull(), "Expected non-null Op");
00459 d_kind = APPLY; }
00460 virtual ~ExprApply() { }
00461
00462 bool operator==(const ExprValue& ev2) const;
00463
00464 void* operator new(size_t size, MemoryManager* mm) {
00465 return mm->newData(size);
00466 }
00467 void operator delete(void*) { }
00468 };
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531 class ExprString: public ExprValue {
00532 friend class Expr;
00533 friend class ExprManager;
00534 private:
00535 std::string d_str;
00536
00537
00538 static size_t hash(const std::string& str) {
00539 return s_charHash(str.c_str());
00540 }
00541
00542
00543 virtual size_t getMMIndex() const { return EXPR_STRING; }
00544
00545 protected:
00546
00547 virtual size_t computeHash() const { return hash(d_str); }
00548
00549 virtual bool isString() const { return true; }
00550 virtual const std::string& getString() const { return d_str; }
00551
00552
00553 virtual ExprValue* copy(ExprManager* em, ExprIndex idx = 0) const;
00554 public:
00555
00556 ExprString(ExprManager* em, const std::string& s, unsigned idx = 0)
00557 : ExprValue(em, STRING_EXPR, idx), d_str(s) { }
00558
00559 virtual ~ExprString() { }
00560
00561 virtual bool operator==(const ExprValue& ev2) const;
00562
00563 void* operator new(size_t size, MemoryManager* mm) {
00564 return mm->newData(size);
00565 }
00566 void operator delete(void*) { }
00567 };
00568
00569 class ExprSkolem: public ExprValue {
00570 friend class Expr;
00571 friend class ExprManager;
00572 private:
00573 Expr d_quant;
00574 int d_idx;
00575 const Expr& getExistential() const {return d_quant;}
00576 int getBoundIndex() const {return d_idx;}
00577
00578
00579 size_t getMMIndex() const { return EXPR_SKOLEM;}
00580
00581 protected:
00582 size_t computeHash() const {
00583 size_t res = getExistential().getBody().hash();
00584 res = PRIME*res + getBoundIndex();
00585 return res;
00586 }
00587
00588 bool operator==(const ExprValue& ev2) const;
00589
00590
00591 ExprValue* copy(ExprManager* em, ExprIndex idx = 0) const;
00592 bool isVar() const { return true; }
00593
00594 public:
00595
00596 ExprSkolem(ExprManager* em, int index, const Expr& exist, unsigned idx = 0)
00597 : ExprValue(em, SKOLEM_VAR, idx), d_quant(exist), d_idx(index) { }
00598
00599 virtual ~ExprSkolem() { }
00600
00601 void* operator new(size_t size, MemoryManager* mm) {
00602 return mm->newData(size);
00603 }
00604 void operator delete(void*) { }
00605 };
00606
00607 class ExprRational: public ExprValue {
00608 friend class Expr;
00609 friend class ExprManager;
00610 private:
00611 Rational d_r;
00612
00613 virtual const Rational& getRational() const { return d_r; }
00614
00615
00616 static size_t hash(const Rational& r) {
00617 return s_charHash(r.toString().c_str());
00618 }
00619
00620
00621 virtual size_t getMMIndex() const { return EXPR_RATIONAL; }
00622
00623 protected:
00624
00625 virtual size_t computeHash() const { return hash(d_r); }
00626 virtual bool operator==(const ExprValue& ev2) const;
00627
00628 virtual ExprValue* copy(ExprManager* em, ExprIndex idx = 0) const;
00629 virtual bool isRational() const { return true; }
00630
00631 public:
00632
00633 ExprRational(ExprManager* em, const Rational& r, unsigned idx = 0)
00634 : ExprValue(em, RATIONAL_EXPR, idx), d_r(r) { }
00635
00636 virtual ~ExprRational() { }
00637
00638 void* operator new(size_t size, MemoryManager* mm) {
00639 return mm->newData(size);
00640 }
00641 void operator delete(void*) { }
00642 };
00643
00644
00645 class ExprVar: public ExprValue {
00646 friend class Expr;
00647 friend class ExprManager;
00648 private:
00649 std::string d_name;
00650
00651 virtual const std::string& getName() const { return d_name; }
00652
00653
00654 virtual size_t getMMIndex() const { return EXPR_UCONST; }
00655 protected:
00656
00657 virtual size_t computeHash() const {
00658 return s_charHash(d_name.c_str());
00659 }
00660 virtual bool isVar() const { return true; }
00661
00662
00663 virtual ExprValue* copy(ExprManager* em, ExprIndex idx = 0) const;
00664
00665 public:
00666
00667 ExprVar(ExprManager *em, const std::string& name, unsigned idx = 0)
00668 : ExprValue(em, UCONST, idx), d_name(name) { }
00669
00670 virtual ~ExprVar() { }
00671
00672 virtual bool operator==(const ExprValue& ev2) const;
00673
00674 void* operator new(size_t size, MemoryManager* mm) {
00675 return mm->newData(size);
00676 }
00677 void operator delete(void*) { }
00678 };
00679
00680
00681 class ExprSymbol: public ExprValue {
00682 friend class Expr;
00683 friend class ExprManager;
00684 private:
00685 std::string d_name;
00686
00687 virtual const std::string& getName() const { return d_name; }
00688
00689
00690 virtual size_t getMMIndex() const { return EXPR_SYMBOL; }
00691 protected:
00692
00693 virtual size_t computeHash() const {
00694 return s_charHash(d_name.c_str())*PRIME + s_intHash(d_kind);
00695 }
00696
00697 virtual ExprValue* copy(ExprManager* em, ExprIndex idx = 0) const;
00698 bool isSymbol() const { return true; }
00699
00700 public:
00701
00702 ExprSymbol(ExprManager *em, int kind, const std::string& name,
00703 unsigned idx = 0)
00704 : ExprValue(em, kind, idx), d_name(name) { }
00705
00706 virtual ~ExprSymbol() { }
00707
00708 virtual bool operator==(const ExprValue& ev2) const;
00709
00710 void* operator new(size_t size, MemoryManager* mm) {
00711 return mm->newData(size);
00712 }
00713 void operator delete(void*) { }
00714 };
00715
00716 class ExprBoundVar: public ExprValue {
00717 friend class Expr;
00718 friend class ExprManager;
00719 private:
00720 std::string d_name;
00721 std::string d_uid;
00722
00723 virtual const std::string& getName() const { return d_name; }
00724 virtual const std::string& getUid() const { return d_uid; }
00725
00726
00727 virtual size_t getMMIndex() const { return EXPR_BOUND_VAR; }
00728 protected:
00729
00730 virtual size_t computeHash() const {
00731 return s_charHash(d_name.c_str())*PRIME + s_charHash(d_uid.c_str());
00732 }
00733 virtual bool isVar() const { return true; }
00734
00735 virtual ExprValue* copy(ExprManager* em, ExprIndex idx = 0) const;
00736
00737 public:
00738
00739 ExprBoundVar(ExprManager *em, const std::string& name,
00740 const std::string& uid, unsigned idx = 0)
00741 : ExprValue(em, BOUND_VAR, idx), d_name(name), d_uid(uid) { }
00742
00743 virtual ~ExprBoundVar() { }
00744
00745 virtual bool operator==(const ExprValue& ev2) const;
00746
00747 void* operator new(size_t size, MemoryManager* mm) {
00748 return mm->newData(size);
00749 }
00750 void operator delete(void*) { }
00751 };
00752
00753
00754
00755 class ExprClosure: public ExprValue {
00756 friend class Expr;
00757 friend class ExprManager;
00758 private:
00759
00760 std::vector<Expr> d_vars;
00761
00762 Expr d_body;
00763
00764
00765 virtual size_t getMMIndex() const { return EXPR_CLOSURE; }
00766
00767 virtual const std::vector<Expr>& getVars() const { return d_vars; }
00768 virtual const Expr& getBody() const { return d_body; }
00769
00770
00771 protected:
00772
00773 size_t computeHash() const;
00774
00775 ExprValue* copy(ExprManager* em, ExprIndex idx = 0) const;
00776
00777 public:
00778
00779 ExprClosure(ExprManager *em, int kind, const std::vector<Expr>& vars,
00780 const Expr& body, unsigned idx = 0)
00781 : ExprValue(em, kind, idx), d_vars(vars), d_body(body) { }
00782
00783 virtual ~ExprClosure() { }
00784
00785 bool operator==(const ExprValue& ev2) const;
00786
00787 void* operator new(size_t size, MemoryManager* mm) {
00788 return mm->newData(size);
00789 }
00790 void operator delete(void*) { }
00791 virtual bool isClosure() const { return true; }
00792 };
00793
00794 }
00795
00796 #endif