00001
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef __Term__
00035 #define __Term__
00036
00037
00038 class TermList;
00039 class Substitution;
00040
00041
00042 #include <string>
00043
00044
00045 #include "Signature.hpp"
00046 #include "Memory.hpp"
00047 #include "Var.hpp"
00048
00049 #include "VampireKernel.hpp"
00050
00051
00052
00053
00054
00055 class Term {
00056 private:
00057 class Data;
00058 class VarData;
00059 class CompoundData;
00060 class NumericData;
00061
00062 public:
00063 enum Tag {
00064 VAR = 0,
00065 NUMERIC = 1,
00066 COMPOUND = 2
00067 };
00068
00069
00070 Term ();
00071 Term (Signature::Fun* f, const TermList& args);
00072 Term (Signature::Fun* f);
00073 Term (const Term& t);
00074 ~Term ();
00075 explicit Term (Var v);
00076 explicit Term (double number);
00077 Term (const VampireKernel::Subterm* term, const VampireKernel& kernel);
00078 void operator= (const Term& rhs);
00079
00080
00081 Tag tag () const;
00082 int var () const;
00083 double number () const;
00084 Signature::Fun* functor () const;
00085 const TermList& args () const;
00086 bool operator == (const Term& rhs) const;
00087 bool isvar () const;
00088
00089
00090 void* operator new (size_t);
00091
00092
00093 bool occurs ( const Signature::Fun* ) const;
00094 bool occurs ( Var v ) const;
00095 bool equal (const Term& t) const;
00096 Compare compare (const Term& l) const;
00097 bool equalUpTo (const Term& r, Var x, Var y) const;
00098 bool defines (const Term& lhs) const;
00099 void apply (const Substitution& subst);
00100 void rectify (Substitution&, Var& last, VarList& freeVars);
00101 bool hasVarsNotIn (VarListList) const;
00102 bool isRenamingOf (Term t, Substitution& sbst) const;
00103 void occurring (bool* occurrences, Var max) const;
00104
00105
00106 static const char* Term::varPrefix;
00107 private:
00108
00109 Data* _data;
00110 };
00111
00112
00113 class TermList
00114 : public Lst<Term>
00115 {
00116 public:
00117
00118 TermList ();
00119 TermList (const TermList&);
00120 explicit TermList (const Term& t);
00121 TermList (const Term& head, const TermList& tail);
00122 explicit TermList (LstData<Term>*);
00123 explicit TermList (const VampireKernel::Subterm* term);
00124 TermList (const VampireKernel::Subterm* term, const VampireKernel& kernel);
00125
00126
00127 const TermList& tail () const
00128 { return static_cast<const TermList&>(Lst<Term>::tail()); }
00129
00130
00131 bool occurs ( const Signature::Fun* ) const;
00132 bool occurs ( Var v ) const;
00133 bool equal (TermList t) const;
00134 Compare compare (TermList l) const;
00135 bool equalUpTo (TermList rs, Var x, Var y) const;
00136 bool hasVarsNotIn (VarListList) const;
00137 bool varsOnly () const;
00138 void apply (const Substitution& subst);
00139 void rectify (Substitution&, Var& last, VarList& freeVars);
00140 void buildFrom (VarList vs);
00141 bool isRenamingOf (TermList t, Substitution& sbst) const;
00142 void occurring (bool* occurrences, Var max) const;
00143 };
00144
00145
00146 class Term::Data
00147 {
00148 public:
00149 Data ();
00150 explicit Data (Tag tag);
00151 ~Data ();
00152
00153 Term::Tag tag () const;
00154
00155 void ref ();
00156 void deref ();
00157 void destroy ();
00158
00159 protected:
00160
00161 int _counter;
00162 Term::Tag _tag;
00163 };
00164
00165
00166 class Term::VarData
00167 : public Term::Data
00168 # if DEBUG_PREPRO
00169 , public Memory <CID_VTERM>
00170 # endif
00171 {
00172 public:
00173 explicit VarData (Var v);
00174 Var var () const;
00175
00176 private:
00177
00178 Var _var;
00179 };
00180
00181
00182 class Term::CompoundData
00183 : public Term::Data
00184 # if DEBUG_PREPRO
00185 , public Memory <CID_CTERM>
00186 # endif
00187 {
00188 public:
00189 CompoundData (Signature::Fun* f, const TermList& args);
00190 explicit CompoundData (Signature::Fun* f);
00191 Signature::Fun* functor () const;
00192 const TermList& args () const;
00193
00194 private:
00195
00196 Signature::Fun* _functor;
00197 TermList _args;
00198 };
00199
00200
00201 class Term::NumericData
00202 : public Term::Data
00203 # if DEBUG_PREPRO
00204 , public Memory <CID_NTERM>
00205 # endif
00206 {
00207 public:
00208 explicit NumericData (double f);
00209 double number () const;
00210
00211 private:
00212
00213 double _number;
00214 };
00215
00216
00217 ostream& operator << ( ostream&, const Term );
00218
00219
00220
00221 inline
00222 Term::Term ()
00223 :
00224 _data (0)
00225 {
00226 }
00227
00228
00229 inline
00230 Term::~Term ()
00231 {
00232 if (_data) {
00233 _data->deref ();
00234 }
00235 }
00236
00237
00238
00239 inline
00240 Term::Term (const Term& t)
00241 :
00242 _data (t._data)
00243 {
00244 if (_data) {
00245 _data->ref ();
00246 }
00247 }
00248
00249
00250
00251
00252 inline
00253 Term::Term (Signature::Fun* f, const TermList& args)
00254 :
00255 _data (new CompoundData (f,args))
00256 {
00257 }
00258
00259
00260
00261
00262 inline
00263 Term::Term (Signature::Fun* f)
00264 :
00265 _data (new CompoundData (f))
00266 {
00267 }
00268
00269
00270
00271
00272 inline
00273 Term::Term (Var v)
00274 :
00275 _data (new VarData (v))
00276 {
00277 }
00278
00279
00280
00281
00282 inline
00283 Term::Term (double d)
00284 :
00285 _data (new NumericData (d))
00286 {
00287 }
00288
00289
00290 inline
00291 Term::Tag Term::tag () const
00292 {
00293 return _data->tag();
00294 }
00295
00296
00297 inline
00298 bool Term::isvar () const
00299 {
00300 return _data->tag() == VAR;
00301 }
00302
00303
00304 inline
00305 bool Term::operator == (const Term& rhs) const
00306 {
00307 ASS (_data && rhs._data);
00308
00309 return _data == rhs._data;
00310 }
00311
00312
00313
00314
00315 inline
00316 Var Term::var () const
00317 {
00318 ASS (this && tag() == VAR);
00319
00320 return (static_cast<Term::VarData*>(_data))->var();
00321 }
00322
00323
00324
00325
00326 inline
00327 Signature::Fun* Term::functor () const
00328 {
00329 ASS (this && tag() == COMPOUND);
00330
00331 return (static_cast<Term::CompoundData*>(_data))->functor();
00332 }
00333
00334
00335
00336
00337 inline
00338 const TermList& Term::args () const
00339 {
00340 ASS (this && tag() == COMPOUND);
00341
00342 return (static_cast<Term::CompoundData*>(_data))->args();
00343 }
00344
00345
00346
00347
00348 inline
00349 double Term::number () const
00350 {
00351 ASS (this && tag() == NUMERIC);
00352
00353 return (static_cast<Term::NumericData*>(_data))->number();
00354 }
00355
00356
00357
00358
00359 inline
00360 Term::Data::Data (Term::Tag tag)
00361 :
00362 _counter (1),
00363 _tag (tag)
00364 {
00365 }
00366
00367
00368 inline
00369 Term::Data::~Data ()
00370 {
00371 TRACER( "Term::Data::~Data" );
00372
00373 ASS (_counter == 0);
00374 }
00375
00376
00377 inline
00378 void Term::Data::ref ()
00379 {
00380 ASS (this);
00381
00382 _counter++;
00383 }
00384
00385
00386 inline
00387 Term::Tag Term::Data::tag () const
00388 {
00389 ASS (this);
00390
00391 return _tag;
00392 }
00393
00394
00395 inline
00396 void Term::Data::deref ()
00397 {
00398 ASS (this);
00399 ASS (_counter > 0);
00400 _counter--;
00401
00402 if (_counter == 0) {
00403 destroy ();
00404 }
00405 }
00406
00407
00408
00409
00410 inline
00411 Term::VarData::VarData (Var v)
00412 :
00413 Data (VAR),
00414 _var (v)
00415 {
00416 }
00417
00418
00419 inline
00420 Var Term::VarData::var () const
00421 {
00422 ASS(_tag == VAR);
00423
00424 return _var;
00425 }
00426
00427
00428
00429
00430 inline
00431 Term::NumericData::NumericData (double value)
00432 :
00433 Data (NUMERIC),
00434 _number (value)
00435 {
00436 }
00437
00438
00439 inline
00440 double Term::NumericData::number () const
00441 {
00442 ASS(_tag == NUMERIC);
00443
00444 return _number;
00445 }
00446
00447
00448
00449
00450 inline
00451 Term::CompoundData::CompoundData (Signature::Fun* f, const TermList& args)
00452 :
00453 Data (COMPOUND),
00454 _functor (f),
00455 _args (args)
00456 {
00457 }
00458
00459
00460 inline
00461 Term::CompoundData::CompoundData (Signature::Fun* f)
00462 :
00463 Data (COMPOUND),
00464 _functor (f)
00465 {
00466 }
00467
00468
00469 inline
00470 Signature::Fun* Term::CompoundData::functor () const
00471 {
00472 ASS (this && _tag == COMPOUND);
00473
00474 return _functor;
00475 }
00476
00477
00478 inline
00479 const TermList& Term::CompoundData::args () const
00480 {
00481 ASS (this && _tag == COMPOUND);
00482
00483 return _args;
00484 }
00485
00486
00487
00488
00489 inline
00490 TermList::TermList ()
00491 :
00492 Lst<Term> ()
00493 {
00494 }
00495
00496
00497
00498
00499
00500 inline
00501 TermList::TermList (const TermList& ts)
00502 :
00503 Lst<Term> (ts)
00504 {
00505 }
00506
00507
00508
00509
00510 inline
00511 TermList::TermList (LstData<Term>* d)
00512 :
00513 Lst<Term> (d)
00514 {
00515 }
00516
00517
00518
00519
00520 inline
00521 TermList::TermList (const Term &hd, const TermList& tl)
00522 :
00523 Lst<Term> (hd,tl)
00524 {
00525 }
00526
00527
00528
00529
00530 inline
00531 TermList::TermList (const Term &hd)
00532 :
00533 Lst<Term> (hd)
00534 {
00535 }
00536
00537
00538 #endif // __Term__
00539