Main Page | Namespace List | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

Formula.hpp

Go to the documentation of this file.
00001 //
00002 //  file Formula.hpp
00003 //  defines class Formula
00004 //
00005 
00006 #ifndef __formula__
00007 #define __formula__
00008 
00009 
00010 #include "Atom.hpp"
00011 
00012 
00013 class Position;
00014 class Substitution;
00015 class FormulaList;
00016 class InferenceList;
00017 
00018 
00019 class Formula 
00020 {
00021  private:
00022   class Data;
00023   class AtomicData;
00024   class UnaryData;
00025   class BinaryData;
00026   class QuantifiedData;
00027   class JunctionData;
00028 
00029  public:
00035   enum Connective {
00036     ATOM = 0,   
00037     AND = 1,
00038     OR = 2,
00039     IMP = 3,
00040     IFF = 4,
00041     XOR = 5,
00042     NOT = 6,
00043     FORALL = 7,
00044     EXISTS = 8
00045   }; // enum Formula::Connective
00046 
00047  public:
00048   // constructors/destructors
00049   Formula ();
00050   Formula (const Formula& f);
00051   ~Formula ();
00052   void operator= (const Formula& rhs);
00053   Formula ( Connective c, const Formula& l, const Formula& r); // for binary formulas
00054   Formula ( Connective c, const Formula& a );            // for unary formulas
00055   Formula ( Connective c, const FormulaList& fs );       // for and/or formulas
00056   Formula (Connective c, const VarList& vs, const Formula& a); // for quantified formulas
00057   explicit Formula ( const Atom& a );                    // for atomic formulas
00058   void makeJunction (Connective c, Formula& lhs, Formula& rhs); // for or-and formulas
00059 
00060   // structure
00061   Connective connective () const;
00062   const FormulaList& args () const;
00063   const Formula& left () const;
00064   const Formula& right () const;
00065   const Formula& qarg () const;
00066   const VarList& vars () const;
00067   const Formula& uarg () const;
00068   const Atom& atom () const;
00069   bool operator == (const Formula& rhs) const;
00070   bool isNull () const; 
00071   void makeNull (); 
00072 
00073   // declared but not defined, to prevent on-heap allocation
00074   void* operator new (size_t);
00075 
00076   // miscellaneous
00077   bool isEqualityAxiom () const;  // is one of the equality axioms
00078   bool hasFreeVars () const; 
00079   void flatten ();  // flatten the formula
00080   void ennf (bool polarity);
00081   void rectify (); // renames variables in the order of their occurrence
00082   void rectify (VarList& originalVars, TermList& answerAtom); // same
00083   void rectify (Substitution&, Var& last, VarList& freeVars); 
00084   void removeIff ();
00085   void skolemizeNNF ();
00086   void skolemizeNNF (Substitution& subst, VarList); 
00087   bool isPredicateDefinition (Atom& lhs, Formula& rhs ) const;
00088   bool isFunctionDefinition (Term& lhs, Term& rhs) const; 
00089   bool occurs (const Signature::Pred* p) const; // predicate symbol f occurs in the formula
00090   int universalPrefixLength () const; 
00091 
00092   // miniscoping
00093   void miniscope (InferenceList&);
00094   void miniscope (InferenceList&, const Position&);
00095   void occurring (const VarList& input, 
00096                   VarList& output, 
00097                   VarList& removedVars) const; 
00098   void topMiniscope (Connective, const VarList&, 
00099                      InferenceList&, const Position&);
00100  private:
00101   // structure
00102   Data* _data;
00103 
00104   // auxiliary functions
00105   bool hasFreeVars (VarListList) const; 
00106  
00107   // equality axiom check
00108   bool isReflexivityAxiom () const;
00109   bool isFunctionReflexivityAxiom () const;
00110   bool isPredicateReflexivityAxiom () const;
00111   bool isSymmetryAxiom () const;
00112   bool isTransitivityAxiom () const;
00113 
00114   // miniscoping
00115   void occurring (bool* occurrences, Var max) const;
00116   static void splitVarList (const VarList& in, 
00117                             VarList& out, 
00118                             VarList& removed,
00119                             bool* occurrences);
00120 }; // class Formula
00121 
00122 
00123 class FormulaList 
00124   : public Lst <Formula>
00125 {
00126  public:
00127   // constructors
00128   FormulaList ();
00129   FormulaList (const FormulaList&);
00130   explicit FormulaList (const Formula& t); // one-element list
00131   FormulaList (const Formula& head, const FormulaList& tail);
00132   explicit FormulaList (LstData<Formula>*);
00133 
00134   // inherited functions
00135   const FormulaList& tail () const
00136     { return static_cast<const FormulaList&>(Lst<Formula>::tail()); }
00137 
00138   // miscellaneous
00139   void flatten (Formula::Connective);
00140   void ennf (Formula::Connective andOr, bool polarity);
00141   void rectify (Substitution&, Var& last, VarList& freeVars); 
00142   void removeIff ();
00143   void skolemizeNNF (Substitution& subst, VarList); 
00144   void miniscope (Formula::Connective con, 
00145                   InferenceList&, const Position&, 
00146                   int argNumber, 
00147                   const FormulaList& lst);
00148   void topMiniscope (Formula::Connective, 
00149                      const VarList& vars,
00150                      InferenceList& inf,
00151                      const Position& pos,
00152                      int index); // auxiliary for Formula::topMiniscope
00153   static void appendN (const FormulaList& fst, 
00154                        const FormulaList& snd,
00155                        int N,
00156                        FormulaList& result); // auxiliary for miniscope/5
00157 }; // class FormulaList
00158 
00159 
00160 class Formula::Data 
00161 {
00162  public:
00163   Data (Connective c);
00164   ~Data ();
00165 
00166   void ref ();
00167   void deref ();
00168 
00169   Connective connective () const;
00170 
00171  protected:
00172   // structure
00173   int _counter;
00174   Connective _connective;
00175 
00176   // auxiliary functions
00177   void destroy ();
00178 }; // class Formula::Data
00179 
00180 
00181 class Formula::AtomicData :
00182   public Formula::Data
00183 {
00184  public:
00185   explicit AtomicData (const Atom& atom);
00186 
00187   const Atom& atom () const;
00188 
00189  protected:
00190   // structure
00191   Atom _atom;
00192 }; // class Formula::AtomicData
00193 
00194 
00195 class Formula::UnaryData :
00196   public Formula::Data
00197 {
00198  public:
00199   UnaryData (Connective c, const Formula& arg);
00200 
00201   const Formula& arg () const;
00202 
00203  protected:
00204   // structure
00205   Formula _arg;
00206 }; // class Formula::UnaryData
00207 
00208 
00209 class Formula::BinaryData :
00210   public Formula::Data
00211 {
00212  public:
00213   BinaryData (Connective c, const Formula& lhs, const Formula& rhs);
00214 
00215   const Formula& left () const;
00216   const Formula& right () const;
00217 
00218  protected:
00219   // structure
00220   Formula _lhs;
00221   Formula _rhs;
00222 }; // class Formula::BinaryData
00223 
00224 
00225 class Formula::JunctionData :
00226   public Formula::Data
00227 {
00228  public:
00229   JunctionData (Connective c, const FormulaList& lhs);
00230 
00231   const FormulaList& args () const;
00232 
00233  protected:
00234   // structure
00235   FormulaList _args;
00236 }; // class Formula::JunctionData
00237 
00238 
00239 class Formula::QuantifiedData :
00240   public Formula::Data
00241 #   if DEBUG_PREPRO
00242     , public Memory <CID_QFORMULA>
00243 #   endif
00244 {
00245  public:
00246   QuantifiedData (Connective c, const VarList& vs, const Formula& arg);
00247 
00248   const Formula& arg () const;
00249   const VarList& vars () const;
00250 
00251  protected:
00252   // structure
00253   VarList _vars;
00254   Formula _arg;
00255 }; // class Formula::QuantifiedData
00256 
00257 
00258 // ******************* Formula definitions ************************
00259 
00260 inline
00261 Formula::Formula () 
00262   : 
00263   _data (0) 
00264 {
00265 } // Formula::Formula
00266 
00267 
00268 // 15/04/2003 Torrevieja
00269 inline
00270 bool Formula::isNull () const
00271 {
00272   return _data == 0;
00273 } // Formula::isNull
00274 
00275 
00276 // for quantified formulas
00277 // 29/08/2002 Torrevieja
00278 inline
00279 Formula::Formula (Connective c, const VarList& vs, const Formula& a)
00280   :
00281   _data (new QuantifiedData(c,vs,a))
00282 {
00283 } // Formula::Formula (Connective c, const VarList& vs, const Formula& a)
00284 
00285 
00286 // for atomic formulas
00287 // 30/08/2002 Torrevieja
00288 inline
00289 Formula::Formula (const Atom& atom)
00290   :
00291   _data (new AtomicData(atom))
00292 {
00293 } // Formula::Formula (Connective c, const Atom& arg)
00294 
00295 
00296 // for unary formulas
00297 // 29/08/2002 Torrevieja
00298 inline
00299 Formula::Formula (Connective c, const Formula& arg)
00300   :
00301   _data (new UnaryData(c,arg))
00302 {
00303 } // Formula::Formula (Connective c, const Formula& arg)
00304 
00305 
00306 // for binary formulas
00307 // 29/08/2002 Torrevieja
00308 inline
00309 Formula::Formula (Connective c, const Formula& lhs, const Formula& rhs)
00310   :
00311   _data (new BinaryData(c,lhs,rhs))
00312 {
00313 } // Formula::Formula (Connective c, const Formula& lhs, const Formula& rhs)
00314 
00315 
00316 // for multi-ary formulas
00317 // 30/08/2002 Torrevieja
00318 inline
00319 Formula::Formula (Connective c, const FormulaList& args)
00320   :
00321   _data (new JunctionData(c,args))
00322 {
00323 } // Formula::Formula (Connective c, const FormulaList& args)
00324 
00325 
00326 inline
00327 Formula::~Formula () 
00328 {
00329   if (_data) {
00330     _data->deref ();
00331   }
00332 } // Formula::~Formula
00333 
00334 
00335 // 25/08/2002 Torrevieja
00336 inline
00337 Formula::Formula (const Formula& t)
00338   :
00339   _data (t._data)
00340 {
00341   if (_data) {
00342     _data->ref ();
00343   }
00344 } // Formula::Formula
00345 
00346 
00347 inline
00348 bool Formula::operator == (const Formula& rhs) const
00349 { 
00350   ASS (_data && rhs._data);
00351 
00352   return _data == rhs._data; 
00353 } // Formula::operator ==
00354 
00355 
00356 inline
00357 Formula::Connective Formula::connective () const
00358 {
00359   return _data->connective();
00360 } // Formula::args ()
00361 
00362 
00363 inline
00364 const Atom& Formula::atom () const
00365 {
00366   ASS (connective() == ATOM);
00367 
00368   return (static_cast<AtomicData*>(_data))->atom();
00369 } // Formula::atom ()
00370 
00371 
00372 inline
00373 const FormulaList& Formula::args () const
00374 {
00375   ASS (connective() == AND || connective() == OR);
00376 
00377   return (static_cast<JunctionData*>(_data))->args();
00378 } // Formula::args ()
00379 
00380 
00381 inline
00382 const Formula& Formula::left () const
00383 {
00384   ASS (connective() == IFF || 
00385                       connective() == XOR || 
00386                       connective() == IMP);
00387 
00388   return (static_cast<BinaryData*>(_data))->left();
00389 } // Formula::left ()
00390 
00391 
00392 inline
00393 const Formula& Formula::right () const
00394 {
00395   ASS (connective() == IFF || 
00396                       connective() == XOR || 
00397                       connective() == IMP);
00398 
00399   return (static_cast<BinaryData*>(_data))->right();
00400 } // Formula::right ()
00401 
00402 
00403 inline
00404 const Formula& Formula::uarg () const
00405 {
00406   ASS (connective() == NOT);
00407 
00408   return (static_cast<UnaryData*>(_data))->arg();
00409 } // Formula::uarg ()
00410 
00411 
00412 inline
00413 const Formula& Formula::qarg () const
00414 {
00415   ASS (connective() == FORALL || connective() == EXISTS);
00416 
00417   return (static_cast<QuantifiedData*>(_data))->arg();
00418 } // Formula::qarg ()
00419 
00420 
00421 inline
00422 const VarList& Formula::vars () const
00423 {
00424   ASS (connective() == FORALL || connective() == EXISTS);
00425 
00426   return (static_cast<QuantifiedData*>(_data))->vars();
00427 } // Formula::vars ()
00428 
00429 
00430 // **************** Formula::Data definitions *********************
00431 
00432 inline
00433 Formula::Data::Data (Connective c)
00434   : 
00435   _counter (1),
00436   _connective (c)
00437 {
00438 } // Formula::Data::Data (bool isVar)
00439 
00440 
00441 inline 
00442 Formula::Data::~Data ()
00443 {
00444   TRACER( "Formula::Data::~Data" );
00445 
00446   ASS (_counter == 0);
00447 } // Formula::Data::~Data ()
00448 
00449 
00450 inline
00451 void Formula::Data::ref () 
00452 { 
00453   ASS (this);
00454 
00455   _counter++;
00456 } // Formula::Data::ref ()
00457 
00458 
00459 inline
00460 void Formula::Data::deref () 
00461 { 
00462   ASS (this);
00463   ASS (_counter > 0);
00464   _counter--;
00465 
00466   if (_counter == 0) {
00467     destroy ();
00468   }
00469 } // Formula::Data::deref ()
00470 
00471 
00472 inline
00473 Formula::Connective Formula::Data::connective () const
00474 { 
00475   ASS (this);
00476 
00477   return _connective;
00478 } // Formula::Data::connective ()
00479 
00480 
00481 // **************** Formula::AtomicData definitions *********************
00482 
00483 
00484 inline
00485 Formula::AtomicData::AtomicData (const Atom& atom) 
00486   :
00487   Data (ATOM),
00488   _atom (atom)
00489 { 
00490 } // AtomicData::AtomicData
00491 
00492 
00493 inline
00494 const Atom& Formula::AtomicData::atom () const
00495 {
00496   return _atom; 
00497 } // Formula::AtomicData::atom ()
00498 
00499 
00500 // **************** Formula::UnaryData definitions *********************
00501 
00502 
00503 inline
00504 Formula::UnaryData::UnaryData (Connective c, 
00505                                const Formula& arg) 
00506   :
00507   Data (c),
00508   _arg (arg)
00509 { 
00510   ASS ( c == NOT );
00511 } // UnaryData::UnaryData
00512 
00513 
00514 inline
00515 const Formula& Formula::UnaryData::arg () const
00516 {
00517   return _arg; 
00518 } // Formula::UnaryData::arg ()
00519 
00520 
00521 // **************** Formula::BinaryData definitions *********************
00522 
00523 
00524 inline
00525 Formula::BinaryData::BinaryData (Connective c, 
00526                                  const Formula& lhs, 
00527                                  const Formula& rhs)
00528   :
00529   Data (c),
00530   _lhs (lhs),
00531   _rhs (rhs)
00532 { 
00533   ASS ( c == IMP || c == IFF || c == XOR );
00534 } // BinaryData::BinaryData
00535 
00536 
00537 inline
00538 const Formula& Formula::BinaryData::left () const
00539 {
00540   return _lhs; 
00541 } // Formula::BinaryData::left ()
00542 
00543 
00544 inline
00545 const Formula& Formula::BinaryData::right () const
00546 {
00547   return _rhs; 
00548 } // Formula::BinaryData::right ()
00549 
00550 
00551 // **************** Formula::JunctionData definitions *********************
00552 
00553 
00554 inline
00555 Formula::JunctionData::JunctionData (Connective c, const FormulaList& args) 
00556   :
00557   Data (c),
00558   _args (args)
00559 { 
00560   ASS ( c == AND || c == OR );
00561 } // JunctionData::JunctionData
00562 
00563 
00564 inline
00565 const FormulaList& Formula::JunctionData::args () const
00566 {
00567   return _args; 
00568 } // Formula::JunctionData::args ()
00569 
00570 
00571 // **************** Formula::QuantifiedData definitions *********************
00572 
00573 
00574 inline
00575 Formula::QuantifiedData::QuantifiedData (Connective c, 
00576                                          const VarList& vs, 
00577                                          const Formula& arg)
00578   :
00579   Data (c),
00580   _vars (vs),
00581   _arg (arg)
00582 { 
00583   ASS ( c == FORALL || c == EXISTS );
00584 } // QuantifiedData::QuantifiedData
00585 
00586 
00587 inline
00588 const Formula& Formula::QuantifiedData::arg () const
00589 {
00590   return _arg; 
00591 } // Formula::QuantifiedData::arg ()
00592 
00593 
00594 inline
00595 const VarList& Formula::QuantifiedData::vars () const
00596 {
00597   return _vars; 
00598 } // Formula::QuantifiedData::vars ()
00599 
00600 
00601 // ******************* FormulaList definitions ************************
00602 
00603 inline
00604 FormulaList::FormulaList () 
00605   : 
00606   Lst<Formula> ()
00607 {
00608 } // FormulaList::FormulaList
00609 
00610 
00611 // copy constructor
00612 // 25/08/2002 Torrevieja
00613 inline
00614 FormulaList::FormulaList (const FormulaList& ts)
00615   :
00616   Lst<Formula> (ts)
00617 {
00618 } // FormulaList::FormulaList
00619 
00620 
00621 // almost a copy constructor
00622 // 25/08/2002 Torrevieja
00623 inline
00624 FormulaList::FormulaList (LstData<Formula>* d)
00625   :
00626   Lst<Formula> (d)
00627 {
00628 } // FormulaList::FormulaList
00629 
00630 
00631 // 'cons' list constructor
00632 // 25/08/2002 Torrevieja
00633 inline
00634 FormulaList::FormulaList (const Formula &hd, const FormulaList& tl)
00635   :
00636   Lst<Formula> (hd,tl)
00637 {
00638 } // FormulaList::FormulaList
00639 
00640 
00641 // one-element list constructor
00642 // 25/08/2002 Torrevieja
00643 inline
00644 FormulaList::FormulaList (const Formula &hd)
00645   :
00646   Lst<Formula> (hd)
00647 {
00648 } // FormulaList::FormulaList
00649 
00650 
00651 #endif // __formula__

Generated on Sat Jun 28 15:08:57 2003 for Vampire by doxygen 1.3.2