Massage the AST so that identical sub-formula share the same

reference-counted formula*.  One can't call constructors for AST
items anymore, everything need to be acquired through instance()
class methods.

* src/ltlast/formula.cc, src/ltlast/refformula.cc,
src/ltlast/refformula.hh: New files.
* src/ltlast/Makefile.am (libltlast_la_SOURCES): Add them.
* src/ltlast/atomic_prop.cc, src/ltlast/atomic_prop.hh,
src/ltlast/unop.cc, src/ltlast/unop.hh,
src/ltlast/binop.cc, src/ltlast/binop.hh: Make the constructor
and destructor protected.  Define a static function `instance()'
to get an instance with specific argument.  Use a map called
`instances' to store all known instances.  Inherit from
ref_formula.
* src/ltlast/constant.hh, src/ltlast/constant.cc: Protect
the constructor and destructor.  Provide the false_instance()
and true_instance() functions instead.
* src/formula.hh (ref, unref, ref_, unref_): New methods.
* src/ltlast/multop.cc, src/ltlast/multop.hh: Protect
the constructor, destructor, as well as the add() method.
Provides the instance(), and add() class methods instead.
Store children_ as a pointer.
* src/ltlenv/defaultenv.cc (require): Adjust to
call atomic_prop::instance.
* src/ltlparse/ltlparse.yy: Adjust to call instance() functions
instead of constructors.
* src/ltltest/Makefile.am (LDADD): Tweak library ordering.
* src/ltlvisit/clone.hh (clone_visitor): Inherit from visitor,
not const_visitor, and adjust all prototypes appropriately.
* src/ltlvisit/clone.cc (clone_visitor): Likewise.
Call ref() or instance() methods instead of copy constructors.
* src/ltlvisit/equals.cc: Simplify atomic_prop and constant
cases.
* src/ltlvisit/lunabbrev.hh, src/ltlvisit/lunabbrev.cc,
src/ltlvisit/tunabbrev.hh, src/ltlvisit/tunabbrev.cc,
src/ltlvisit/nenoform.hh, src/ltlvisit/nenoform.cc: Use instance()
methods instead of constructor.  Make these children of visitor, not
const_visitor.
* src/ltltest/readltl.c (main): Do not delete the formula.
This commit is contained in:
Alexandre Duret-Lutz 2003-05-15 13:39:39 +00:00
parent f1838ab8ef
commit 5f6d8b6234
29 changed files with 548 additions and 253 deletions

View file

@ -1,5 +1,46 @@
2003-05-15 Alexandre Duret-Lutz <aduret@src.lip6.fr> 2003-05-15 Alexandre Duret-Lutz <aduret@src.lip6.fr>
Massage the AST so that identical sub-formula share the same
reference-counted formula*. One can't call constructors for AST
items anymore, everything need to be acquired through instance()
class methods.
* src/ltlast/formula.cc, src/ltlast/refformula.cc,
src/ltlast/refformula.hh: New files.
* src/ltlast/Makefile.am (libltlast_la_SOURCES): Add them.
* src/ltlast/atomic_prop.cc, src/ltlast/atomic_prop.hh,
src/ltlast/unop.cc, src/ltlast/unop.hh,
src/ltlast/binop.cc, src/ltlast/binop.hh: Make the constructor
and destructor protected. Define a static function `instance()'
to get an instance with specific argument. Use a map called
`instances' to store all known instances. Inherit from
ref_formula.
* src/ltlast/constant.hh, src/ltlast/constant.cc: Protect
the constructor and destructor. Provide the false_instance()
and true_instance() functions instead.
* src/formula.hh (ref, unref, ref_, unref_): New methods.
* src/ltlast/multop.cc, src/ltlast/multop.hh: Protect
the constructor, destructor, as well as the add() method.
Provides the instance(), and add() class methods instead.
Store children_ as a pointer.
* src/ltlenv/defaultenv.cc (require): Adjust to
call atomic_prop::instance.
* src/ltlparse/ltlparse.yy: Adjust to call instance() functions
instead of constructors.
* src/ltltest/Makefile.am (LDADD): Tweak library ordering.
* src/ltlvisit/clone.hh (clone_visitor): Inherit from visitor,
not const_visitor, and adjust all prototypes appropriately.
* src/ltlvisit/clone.cc (clone_visitor): Likewise.
Call ref() or instance() methods instead of copy constructors.
* src/ltlvisit/equals.cc: Simplify atomic_prop and constant
cases.
* src/ltlvisit/lunabbrev.hh, src/ltlvisit/lunabbrev.cc,
src/ltlvisit/tunabbrev.hh, src/ltlvisit/tunabbrev.cc,
src/ltlvisit/nenoform.hh, src/ltlvisit/nenoform.cc: Use instance()
methods instead of constructor. Make these children of visitor, not
const_visitor.
* src/ltltest/readltl.c (main): Do not delete the formula.
* src/ltlparse/ltlscan.ll (to_parse_size): Declare as size_t to * src/ltlparse/ltlscan.ll (to_parse_size): Declare as size_t to
remove a warning with newer versions of Flex. remove a warning with newer versions of Flex.

View file

@ -11,10 +11,12 @@ libltlast_la_SOURCES = \
constant.cc \ constant.cc \
constant.hh \ constant.hh \
formula.hh \ formula.hh \
formula.cc \
multop.cc \ multop.cc \
multop.hh \ multop.hh \
predecl.hh \ predecl.hh \
refformula.cc \
refformula.hh \
unop.cc \ unop.cc \
unop.hh \ unop.hh \
visitor.hh visitor.hh

View file

@ -5,7 +5,7 @@ namespace spot
{ {
namespace ltl namespace ltl
{ {
atomic_prop::atomic_prop(const std::string& name, environment& env) atomic_prop::atomic_prop(const std::string& name, environment& env)
: name_(name), env_(&env) : name_(name), env_(&env)
{ {
@ -15,29 +15,45 @@ namespace spot
{ {
} }
void void
atomic_prop::accept(visitor& v) atomic_prop::accept(visitor& v)
{ {
v.visit(this); v.visit(this);
} }
void void
atomic_prop::accept(const_visitor& v) const atomic_prop::accept(const_visitor& v) const
{ {
v.visit(this); v.visit(this);
} }
const std::string& const std::string&
atomic_prop::name() const atomic_prop::name() const
{ {
return name_; return name_;
} }
environment& environment&
atomic_prop::env() const atomic_prop::env() const
{ {
return *env_; return *env_;
} }
atomic_prop::map atomic_prop::instances;
atomic_prop*
atomic_prop::instance(const std::string& name, environment& env)
{
pair p(name, &env);
map::iterator i = instances.find(p);
if (i != instances.end())
{
return static_cast<atomic_prop*>(i->second->ref());
}
atomic_prop* ap = new atomic_prop(name, env);
instances[p] = ap;
return static_cast<atomic_prop*>(ap->ref());
}
} }
} }

View file

@ -2,7 +2,8 @@
# define SPOT_LTLAST_ATOMIC_PROP_HH # define SPOT_LTLAST_ATOMIC_PROP_HH
#include <string> #include <string>
#include "formula.hh" #include <map>
#include "refformula.hh"
#include "ltlenv/environment.hh" #include "ltlenv/environment.hh"
namespace spot namespace spot
@ -11,13 +12,12 @@ namespace spot
{ {
/// Atomic propositions. /// Atomic propositions.
class atomic_prop : public formula class atomic_prop : public ref_formula
{ {
public: public:
/// Build an atomic proposition with name \a name in /// Build an atomic proposition with name \a name in
/// environment \a env. /// environment \a env.
atomic_prop(const std::string& name, environment& env); static atomic_prop* instance(const std::string& name, environment& env);
virtual ~atomic_prop();
virtual void accept(visitor& visitor); virtual void accept(visitor& visitor);
virtual void accept(const_visitor& visitor) const; virtual void accept(const_visitor& visitor) const;
@ -26,6 +26,14 @@ namespace spot
const std::string& name() const; const std::string& name() const;
/// Get the environment of the atomic proposition. /// Get the environment of the atomic proposition.
environment& env() const; environment& env() const;
protected:
atomic_prop(const std::string& name, environment& env);
virtual ~atomic_prop();
typedef std::pair<std::string, environment*> pair;
typedef std::map<pair, atomic_prop*> map;
static map instances;
private: private:
std::string name_; std::string name_;
environment* env_; environment* env_;

View file

@ -5,7 +5,7 @@
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
binop::binop(type op, formula* first, formula* second) binop::binop(type op, formula* first, formula* second)
: op_(op), first_(first), second_(second) : op_(op), first_(first), second_(second)
{ {
@ -45,19 +45,19 @@ namespace spot
return second_; return second_;
} }
formula* formula*
binop::second() binop::second()
{ {
return second_; return second_;
} }
binop::type binop::type
binop::op() const binop::op() const
{ {
return op_; return op_;
} }
const char* const char*
binop::op_name() const binop::op_name() const
{ {
switch (op_) switch (op_)
@ -78,5 +78,22 @@ namespace spot
return 0; return 0;
} }
binop::map binop::instances;
binop*
binop::instance(type op, formula* first, formula* second)
{
pairf pf(first, second);
pair p(op, pf);
map::iterator i = instances.find(p);
if (i != instances.end())
{
return static_cast<binop*>(i->second->ref());
}
binop* ap = new binop(op, first, second);
instances[p] = ap;
return static_cast<binop*>(ap->ref());
}
} }
} }

View file

@ -1,6 +1,7 @@
#ifndef SPOT_LTLAST_BINOP_HH #ifndef SPOT_LTLAST_BINOP_HH
# define SPOT_LTLAST_BINOP_HH # define SPOT_LTLAST_BINOP_HH
#include <map>
#include "formula.hh" #include "formula.hh"
namespace spot namespace spot
@ -18,8 +19,9 @@ namespace spot
/// are often nested we represent them as multops. /// are often nested we represent them as multops.
enum type { Xor, Implies, Equiv, U, R }; enum type { Xor, Implies, Equiv, U, R };
binop(type op, formula* first, formula* second); /// Build an unary operator with operation \a op and
virtual ~binop(); /// children \a first and \a second.
static binop* instance(type op, formula* first, formula* second);
virtual void accept(visitor& v); virtual void accept(visitor& v);
virtual void accept(const_visitor& v) const; virtual void accept(const_visitor& v) const;
@ -38,6 +40,15 @@ namespace spot
/// Get the type of this operator, as a string. /// Get the type of this operator, as a string.
const char* op_name() const; const char* op_name() const;
protected:
typedef std::pair<formula*, formula*> pairf;
typedef std::pair<type, pairf> pair;
typedef std::map<pair, formula*> map;
static map instances;
binop(type op, formula* first, formula* second);
virtual ~binop();
private: private:
type op_; type op_;
formula* first_; formula* first_;

View file

@ -5,7 +5,7 @@
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
constant::constant(type val) constant::constant(type val)
: val_(val) : val_(val)
{ {
@ -27,13 +27,13 @@ namespace spot
v.visit(this); v.visit(this);
} }
constant::type constant::type
constant::val() const constant::val() const
{ {
return val_; return val_;
} }
const char* const char*
constant::val_name() const constant::val_name() const
{ {
switch (val_) switch (val_)
@ -48,5 +48,18 @@ namespace spot
return 0; return 0;
} }
constant*
constant::false_instance()
{
static constant f(constant::False);
return &f;
}
constant*
constant::true_instance()
{
static constant t(constant::True);
return &t;
}
} }
} }

View file

@ -13,10 +13,6 @@ namespace spot
{ {
public: public:
enum type { False, True }; enum type { False, True };
constant(type val);
virtual ~constant();
virtual void accept(visitor& v); virtual void accept(visitor& v);
virtual void accept(const_visitor& v) const; virtual void accept(const_visitor& v) const;
@ -25,6 +21,15 @@ namespace spot
/// Return the value of the constant as a string. /// Return the value of the constant as a string.
const char* val_name() const; const char* val_name() const;
/// Get the sole instance of spot::ltl::constant::constant(True).
static constant* true_instance();
/// Get the sole instance of spot::ltl::constant::constant(False).
static constant* false_instance();
protected:
constant(type val);
virtual ~constant();
private: private:
type val_; type val_;
}; };

34
src/ltlast/formula.cc Normal file
View file

@ -0,0 +1,34 @@
#include "formula.hh"
namespace spot
{
namespace ltl
{
formula*
formula::ref()
{
ref_();
return this;
}
void
formula::unref(formula* f)
{
if (f->unref_())
delete f;
}
void
formula::ref_()
{
// Not reference counted by default.
}
bool
formula::unref_()
{
// Not reference counted by default.
return false;
}
}
}

View file

@ -3,20 +3,32 @@
#include "predecl.hh" #include "predecl.hh"
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
/// \brief An LTL formula. /// \brief An LTL formula.
/// ///
/// The only way you can work with a formula is to /// The only way you can work with a formula is to
/// build a spot::ltl::visitor or spot::ltl::const_visitor. /// build a spot::ltl::visitor or spot::ltl::const_visitor.
class formula class formula
{ {
public: public:
virtual void accept(visitor& v) = 0; virtual void accept(visitor& v) = 0;
virtual void accept(const_visitor& v) const = 0; virtual void accept(const_visitor& v) const = 0;
/// \brief clone this formula
formula* ref();
/// \brief release formula
static void unref(formula* f);
protected:
/// \brief increment reference counter if any
virtual void ref_();
/// \brief decrement reference counter if any, return true when
/// the instance must be delete (usually when the counter hits 0).
virtual bool unref_();
}; };
} }

View file

@ -6,42 +6,15 @@
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
multop::multop(type op) multop::multop(type op, vec* v)
: op_(op) : op_(op), children_(v)
{ {
} }
multop::multop(type op, formula* first, formula* second)
: op_(op)
{
children_.reserve(2);
add(first);
add(second);
}
void
multop::add(formula* f)
{
// If the formula we add is itself a multop for the same operator,
// merge its children with ours.
multop* p = dynamic_cast<multop*>(f);
if (p && p->op() == op())
{
unsigned ps = p->size();
for (unsigned i = 0; i < ps; ++i)
children_.push_back(p->nth(i));
// that sub-formula is now useless
delete f;
}
else
{
children_.push_back(f);
}
}
multop::~multop() multop::~multop()
{ {
delete children_;
} }
void void
@ -59,28 +32,28 @@ namespace spot
unsigned unsigned
multop::size() const multop::size() const
{ {
return children_.size(); return children_->size();
} }
const formula* const formula*
multop::nth(unsigned n) const multop::nth(unsigned n) const
{ {
return children_[n]; return (*children_)[n];
} }
formula* formula*
multop::nth(unsigned n) multop::nth(unsigned n)
{ {
return children_[n]; return (*children_)[n];
} }
multop::type multop::type
multop::op() const multop::op() const
{ {
return op_; return op_;
} }
const char* const char*
multop::op_name() const multop::op_name() const
{ {
switch (op_) switch (op_)
@ -95,5 +68,69 @@ namespace spot
return 0; return 0;
} }
multop::map multop::instances;
multop*
multop::instance(type op, vec* v)
{
pair p(op, v);
map::iterator i = instances.find(p);
if (i != instances.end())
{
delete v;
return static_cast<multop*>(i->second->ref());
}
multop* ap = new multop(op, v);
instances[p] = ap;
return static_cast<multop*>(ap->ref());
}
multop*
multop::instance(type op)
{
return instance(op, new vec);
}
multop*
multop::instance(type op, formula* first, formula* second)
{
vec* v = new vec;
multop::add(op, v, first);
multop::add(op, v, second);
return instance(op, v);
}
multop::vec*
multop::add(type op, vec* v, formula* f)
{
// If the formula we add is itself a multop for the same operator,
// merge its children.
multop* p = dynamic_cast<multop*>(f);
if (p && p->op() == op)
{
unsigned ps = p->size();
for (unsigned i = 0; i < ps; ++i)
v->push_back(p->nth(i));
// that sub-formula is now useless
formula::unref(f);
}
else
{
v->push_back(f);
}
return v;
}
void
multop::add(multop** m, formula* f)
{
vec* v = new vec(*(*m)->children_);
type op = (*m)->op();
multop::add(op, v, f);
formula::unref(*m);
*m = instance(op, v);
}
} }
} }

View file

@ -2,13 +2,14 @@
# define SPOT_LTLAST_MULTOP_HH # define SPOT_LTLAST_MULTOP_HH
#include <vector> #include <vector>
#include <map>
#include "formula.hh" #include "formula.hh"
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
/// \brief Multi-operand operators. /// \brief Multi-operand operators.
/// ///
/// These operators are considered commutative and associative. /// These operators are considered commutative and associative.
@ -17,25 +18,29 @@ namespace spot
public: public:
enum type { Or, And }; enum type { Or, And };
/// \brief Build a spot::ltl::multop with no child. /// \brief Build a spot::ltl::multop with no child.
/// ///
/// This has little value unless you call multop::add later. /// This has little value unless you call multop::add later.
multop(type op); static multop* instance(type op);
/// \brief Build a spot::ltl::multop with two children. /// \brief Build a spot::ltl::multop with two children.
/// ///
/// If one of the children itself is a spot::ltl::multop /// If one of the children itself is a spot::ltl::multop
/// with the same type, it will be merged. I.e., children /// with the same type, it will be merged. I.e., children
/// if that child will be added, and that child itself will /// if that child will be added, and that child itself will
/// be destroyed. /// be destroyed.
multop(type op, formula* first, formula* second); static multop* instance(type op, formula* first, formula* second);
/// \brief Add another child to this operator. /// \brief Add another child to this operator.
/// ///
/// If \a f itself is a spot::ltl::multop with the same type, it /// If \a f itself is a spot::ltl::multop with the same type, it
/// will be merged. I.e., children of \a f will be added, and /// will be merged. I.e., children of \a f will be added, and
/// that \a f will will be destroyed. /// that \a f will will be destroyed.
void add(formula* f); ///
/// Note that this function overwrites the supplied ltl::multop pointer.
virtual ~multop(); /// The old value is released and should not be used after this.
static void add(multop** m, formula* f);
virtual void accept(visitor& v); virtual void accept(visitor& v);
virtual void accept(const_visitor& v) const; virtual void accept(const_visitor& v) const;
@ -56,9 +61,21 @@ namespace spot
/// Get the type of this operator, as a string. /// Get the type of this operator, as a string.
const char* op_name() const; const char* op_name() const;
protected:
typedef std::vector<formula*> vec;
typedef std::pair<type, vec*> pair;
typedef std::map<pair, formula*> map;
static map instances;
multop(type op, vec* v);
static multop* instance(type op, vec* v);
static vec* multop::add(type op, vec* v, formula* f);
virtual ~multop();
private: private:
type op_; type op_;
std::vector<formula*> children_; vec* children_;
}; };
} }

27
src/ltlast/refformula.cc Normal file
View file

@ -0,0 +1,27 @@
#include "refformula.hh"
#include <cassert>
namespace spot
{
namespace ltl
{
ref_formula::ref_formula()
: ref_count_(0)
{
}
void
ref_formula::ref_()
{
++ref_count_;
}
bool
ref_formula::unref_()
{
assert(ref_count_ > 0);
return !--ref_count_;
}
}
}

25
src/ltlast/refformula.hh Normal file
View file

@ -0,0 +1,25 @@
#ifndef SPOT_LTLAST_REFFORMULAE_HH
# define SPOT_LTLAST_REFFORMULAE_HH
#include "formula.hh"
namespace spot
{
namespace ltl
{
/// \brief A reference-counted LTL formula.
class ref_formula : public formula
{
protected:
ref_formula();
void ref_();
bool unref_();
private:
unsigned ref_count_;
};
}
}
#endif // SPOT_LTLAST_REFFORMULAE_HH

View file

@ -5,7 +5,7 @@
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
unop::unop(type op, formula* child) unop::unop(type op, formula* child)
: op_(op), child_(child) : op_(op), child_(child)
{ {
@ -39,13 +39,13 @@ namespace spot
return child_; return child_;
} }
unop::type unop::type
unop::op() const unop::op() const
{ {
return op_; return op_;
} }
const char* const char*
unop::op_name() const unop::op_name() const
{ {
switch (op_) switch (op_)
@ -64,5 +64,21 @@ namespace spot
return 0; return 0;
} }
unop::map unop::instances;
unop*
unop::instance(type op, formula* child)
{
pair p(op, child);
map::iterator i = instances.find(p);
if (i != instances.end())
{
return static_cast<unop*>(i->second->ref());
}
unop* ap = new unop(op, child);
instances[p] = ap;
return static_cast<unop*>(ap->ref());
}
} }
} }

View file

@ -1,21 +1,23 @@
#ifndef SPOT_LTLAST_UNOP_HH #ifndef SPOT_LTLAST_UNOP_HH
# define SPOT_LTLAST_UNOP_HH # define SPOT_LTLAST_UNOP_HH
#include <map>
#include "formula.hh" #include "formula.hh"
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
/// Unary operator. /// Unary operator.
class unop : public formula class unop : public formula
{ {
public: public:
enum type { Not, X, F, G }; enum type { Not, X, F, G };
unop(type op, formula* child); /// Build an unary operator with operation \a op and
virtual ~unop(); /// child \a child.
static unop* instance(type op, formula* child);
virtual void accept(visitor& v); virtual void accept(visitor& v);
virtual void accept(const_visitor& v) const; virtual void accept(const_visitor& v) const;
@ -30,6 +32,14 @@ namespace spot
/// Get the type of this operator, as a string. /// Get the type of this operator, as a string.
const char* op_name() const; const char* op_name() const;
protected:
typedef std::pair<type, formula*> pair;
typedef std::map<pair, formula*> map;
static map instances;
unop(type op, formula* child);
virtual ~unop();
private: private:
type op_; type op_;
formula* child_; formula* child_;

View file

@ -9,7 +9,7 @@ namespace spot
formula* formula*
default_environment::require(const std::string& s) default_environment::require(const std::string& s)
{ {
return new atomic_prop(s, *this); return atomic_prop::instance(s, *this);
} }
const std::string& const std::string&
@ -23,7 +23,7 @@ namespace spot
{ {
} }
default_environment& default_environment&
default_environment::instance() default_environment::instance()
{ {
static default_environment* singleton = new default_environment(); static default_environment* singleton = new default_environment();

View file

@ -101,15 +101,15 @@ subformula: ATOMIC_PROP
delete $1; delete $1;
} }
| CONST_TRUE | CONST_TRUE
{ $$ = new constant(constant::True); } { $$ = constant::true_instance(); }
| CONST_FALSE | CONST_FALSE
{ $$ = new constant(constant::False); } { $$ = constant::false_instance(); }
| PAR_OPEN subformula PAR_CLOSE | PAR_OPEN subformula PAR_CLOSE
{ $$ = $2; } { $$ = $2; }
| PAR_OPEN error PAR_CLOSE | PAR_OPEN error PAR_CLOSE
{ error_list.push_back(parse_error(@$, { error_list.push_back(parse_error(@$,
"treating this parenthetical block as false")); "treating this parenthetical block as false"));
$$ = new constant(constant::False); $$ = constant::false_instance();
} }
| PAR_OPEN subformula many_errors PAR_CLOSE | PAR_OPEN subformula many_errors PAR_CLOSE
{ error_list.push_back(parse_error(@3, { error_list.push_back(parse_error(@3,
@ -117,27 +117,27 @@ subformula: ATOMIC_PROP
$$ = $2; $$ = $2;
} }
| OP_NOT subformula | OP_NOT subformula
{ $$ = new unop(unop::Not, $2); } { $$ = unop::instance(unop::Not, $2); }
| subformula OP_AND subformula | subformula OP_AND subformula
{ $$ = new multop(multop::And, $1, $3); } { $$ = multop::instance(multop::And, $1, $3); }
| subformula OP_OR subformula | subformula OP_OR subformula
{ $$ = new multop(multop::Or, $1, $3); } { $$ = multop::instance(multop::Or, $1, $3); }
| subformula OP_XOR subformula | subformula OP_XOR subformula
{ $$ = new binop(binop::Xor, $1, $3); } { $$ = binop::instance(binop::Xor, $1, $3); }
| subformula OP_IMPLIES subformula | subformula OP_IMPLIES subformula
{ $$ = new binop(binop::Implies, $1, $3); } { $$ = binop::instance(binop::Implies, $1, $3); }
| subformula OP_EQUIV subformula | subformula OP_EQUIV subformula
{ $$ = new binop(binop::Equiv, $1, $3); } { $$ = binop::instance(binop::Equiv, $1, $3); }
| subformula OP_U subformula | subformula OP_U subformula
{ $$ = new binop(binop::U, $1, $3); } { $$ = binop::instance(binop::U, $1, $3); }
| subformula OP_R subformula | subformula OP_R subformula
{ $$ = new binop(binop::R, $1, $3); } { $$ = binop::instance(binop::R, $1, $3); }
| OP_F subformula | OP_F subformula
{ $$ = new unop(unop::F, $2); } { $$ = unop::instance(unop::F, $2); }
| OP_G subformula | OP_G subformula
{ $$ = new unop(unop::G, $2); } { $$ = unop::instance(unop::G, $2); }
| OP_X subformula | OP_X subformula
{ $$ = new unop(unop::X, $2); } { $$ = unop::instance(unop::X, $2); }
// | subformula many_errors // | subformula many_errors
// { error_list->push_back(parse_error(@2, // { error_list->push_back(parse_error(@2,
// "ignoring these unexpected trailing tokens")); // "ignoring these unexpected trailing tokens"));

View file

@ -1,8 +1,8 @@
AM_CPPFLAGS = -I$(srcdir)/.. AM_CPPFLAGS = -I$(srcdir)/..
LDADD = ../ltlparse/libltlparse.la \ LDADD = ../ltlparse/libltlparse.la \
../ltlvisit/libltlvisit.la \ ../ltlvisit/libltlvisit.la \
../ltlast/libltlast.la \ ../ltlenv/libltlenv.la \
../ltlenv/libltlenv.la ../ltlast/libltlast.la
check_SCRIPTS = defs check_SCRIPTS = defs
# Keep this sorted alphabetically. # Keep this sorted alphabetically.

View file

@ -46,7 +46,6 @@ main(int argc, char** argv)
spot::ltl::dump(*f, std::cout); spot::ltl::dump(*f, std::cout);
std::cout << std::endl; std::cout << std::endl;
#endif #endif
delete f;
} }
else else
{ {

View file

@ -1,7 +1,7 @@
#include "ltlast/allnodes.hh" #include "ltlast/allnodes.hh"
#include "clone.hh" #include "clone.hh"
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
@ -18,52 +18,52 @@ namespace spot
{ {
return result_; return result_;
} }
void void
clone_visitor::visit(const atomic_prop* ap) clone_visitor::visit(atomic_prop* ap)
{ {
result_ = new atomic_prop(ap->name(), ap->env()); result_ = ap->ref();
} }
void void
clone_visitor::visit(const constant* c) clone_visitor::visit(constant* c)
{ {
result_ = new constant(c->val()); result_ = c->ref();
} }
void void
clone_visitor::visit(const unop* uo) clone_visitor::visit(unop* uo)
{ {
result_ = new unop(uo->op(), recurse(uo->child())); result_ = unop::instance(uo->op(), recurse(uo->child()));
} }
void void
clone_visitor::visit(const binop* bo) clone_visitor::visit(binop* bo)
{ {
result_ = new binop(bo->op(), result_ = binop::instance(bo->op(),
recurse(bo->first()), recurse(bo->second())); recurse(bo->first()), recurse(bo->second()));
} }
void void
clone_visitor::visit(const multop* mo) clone_visitor::visit(multop* mo)
{ {
multop* res = new multop(mo->op()); multop* res = multop::instance(mo->op());
unsigned mos = mo->size(); unsigned mos = mo->size();
for (unsigned i = 0; i < mos; ++i) for (unsigned i = 0; i < mos; ++i)
{ {
res->add(recurse(mo->nth(i))); multop::add(&res, recurse(mo->nth(i)));
} }
result_ = res; result_ = res;
} }
formula* formula*
clone_visitor::recurse(const formula* f) clone_visitor::recurse(formula* f)
{ {
return clone(f); return clone(f);
} }
formula* formula*
clone(const formula* f) clone(formula* f)
{ {
clone_visitor v; clone_visitor v;
f->accept(v); f->accept(v);

View file

@ -4,7 +4,7 @@
#include "ltlast/formula.hh" #include "ltlast/formula.hh"
#include "ltlast/visitor.hh" #include "ltlast/visitor.hh"
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
@ -14,28 +14,28 @@ namespace spot
/// to derive from it and override part of its methods. /// to derive from it and override part of its methods.
/// But if you just want the functionality, consider using /// But if you just want the functionality, consider using
/// spot::ltl::clone instead. /// spot::ltl::clone instead.
class clone_visitor : public const_visitor class clone_visitor : public visitor
{ {
public: public:
clone_visitor(); clone_visitor();
virtual ~clone_visitor(); virtual ~clone_visitor();
formula* result() const; formula* result() const;
void visit(const atomic_prop* ap); void visit(atomic_prop* ap);
void visit(const unop* uo); void visit(unop* uo);
void visit(const binop* bo); void visit(binop* bo);
void visit(const multop* mo); void visit(multop* mo);
void visit(const constant* c); void visit(constant* c);
virtual formula* recurse(const formula* f); virtual formula* recurse(formula* f);
protected: protected:
formula* result_; formula* result_;
}; };
/// \brief Clone a formula. /// \brief Clone a formula.
formula* clone(const formula* f); formula* clone(formula* f);
} }
} }

View file

@ -2,7 +2,7 @@
#include "equals.hh" #include "equals.hh"
#include "ltlast/allnodes.hh" #include "ltlast/allnodes.hh"
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
@ -15,26 +15,22 @@ namespace spot
{ {
} }
bool bool
equals_visitor::result() const equals_visitor::result() const
{ {
return result_; return result_;
} }
void void
equals_visitor::visit(const atomic_prop* ap) equals_visitor::visit(const atomic_prop* ap)
{ {
const atomic_prop* p = dynamic_cast<const atomic_prop*>(f_); result_ = f_ == ap;
if (p && p->name() == ap->name())
result_ = true;
} }
void void
equals_visitor::visit(const constant* c) equals_visitor::visit(const constant* c)
{ {
const constant* p = dynamic_cast<const constant*>(f_); result_ = f_ == c;
if (p && p->val() == c->val())
result_ = true;
} }
void void
@ -53,34 +49,34 @@ namespace spot
const binop* p = dynamic_cast<const binop*>(f_); const binop* p = dynamic_cast<const binop*>(f_);
if (!p || p->op() != bo->op()) if (!p || p->op() != bo->op())
return; return;
// The current visitor will descend the left branch. // The current visitor will descend the left branch.
// Build a second visitor for the right branch. // Build a second visitor for the right branch.
equals_visitor v2(p->second()); equals_visitor v2(p->second());
f_ = p->first(); f_ = p->first();
bo->first()->accept(*this); bo->first()->accept(*this);
if (result_ == false) if (result_ == false)
return; return;
bo->second()->accept(v2); bo->second()->accept(v2);
result_ = v2.result(); result_ = v2.result();
} }
void void
equals_visitor::visit(const multop* m) equals_visitor::visit(const multop* m)
{ {
const multop* p = dynamic_cast<const multop*>(f_); const multop* p = dynamic_cast<const multop*>(f_);
if (!p || p->op() != m->op()) if (!p || p->op() != m->op())
return; return;
// This check is a bit more complicated than other checks // This check is a bit more complicated than other checks
// because And(a, b, c) is equal to And(c, a, b, a). // because And(a, b, c) is equal to And(c, a, b, a).
unsigned m_size = m->size(); unsigned m_size = m->size();
unsigned p_size = p->size(); unsigned p_size = p->size();
std::vector<bool> p_seen(p_size, false); std::vector<bool> p_seen(p_size, false);
for (unsigned nf = 0; nf < m_size; ++nf) for (unsigned nf = 0; nf < m_size; ++nf)
{ {
unsigned np; unsigned np;
@ -102,7 +98,7 @@ namespace spot
// of `p'. That doesn't means that both formula are equal. // of `p'. That doesn't means that both formula are equal.
// Condider m = And(a, b, c) against p = And(c, d, a, b). // Condider m = And(a, b, c) against p = And(c, d, a, b).
// We should now check if any unmarked (accodring to p_seen) // We should now check if any unmarked (accodring to p_seen)
// child of `p' has an counterpart in `m'. Because `m' might // child of `p' has an counterpart in `m'. Because `m' might
// contain duplicate children, its faster to test that // contain duplicate children, its faster to test that
// unmarked children of `p' have a counterpart in marked children // unmarked children of `p' have a counterpart in marked children
// of `p'. // of `p'.
@ -111,25 +107,25 @@ namespace spot
// Consider only unmarked children. // Consider only unmarked children.
if (p_seen[np]) if (p_seen[np])
continue; continue;
// Compare with marked children. // Compare with marked children.
unsigned np2; unsigned np2;
const formula *pnth = p->nth(np); const formula *pnth = p->nth(np);
for (np2 = 0; np2 < p_size; ++np2) for (np2 = 0; np2 < p_size; ++np2)
if (p_seen[np2] && equals(p->nth(np2), pnth)) if (p_seen[np2] && equals(p->nth(np2), pnth))
break; break;
// No match? Too bad. // No match? Too bad.
if (np2 == p_size) if (np2 == p_size)
return; return;
} }
// The two formulas match. // The two formulas match.
result_ = true; result_ = true;
} }
bool bool
equals(const formula* f1, const formula* f2) equals(const formula* f1, const formula* f2)
{ {
equals_visitor v(f1); equals_visitor v(f1);

View file

@ -1,7 +1,7 @@
#include "ltlast/allnodes.hh" #include "ltlast/allnodes.hh"
#include "lunabbrev.hh" #include "lunabbrev.hh"
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
@ -13,8 +13,8 @@ namespace spot
{ {
} }
void void
unabbreviate_logic_visitor::visit(const binop* bo) unabbreviate_logic_visitor::visit(binop* bo)
{ {
formula* f1 = recurse(bo->first()); formula* f1 = recurse(bo->first());
formula* f2 = recurse(bo->second()); formula* f2 = recurse(bo->second());
@ -22,43 +22,48 @@ namespace spot
{ {
/* f1 ^ f2 == (f1 & !f2) | (f2 & !f1) */ /* f1 ^ f2 == (f1 & !f2) | (f2 & !f1) */
case binop::Xor: case binop::Xor:
result_ = new multop(multop::Or, result_ = multop::instance(multop::Or,
new multop(multop::And, f1, multop::instance(multop::And, f1,
new unop(unop::Not, f2)), unop::instance(unop::Not,
new multop(multop::And, f2, f2)),
new unop(unop::Not, f1))); multop::instance(multop::And, f2,
unop::instance(unop::Not,
f1)));
return; return;
/* f1 => f2 == !f1 | f2 */ /* f1 => f2 == !f1 | f2 */
case binop::Implies: case binop::Implies:
result_ = new multop(multop::Or, new unop(unop::Not, f1), f2); result_ = multop::instance(multop::Or,
unop::instance(unop::Not, f1), f2);
return; return;
/* f1 <=> f2 == (f1 & f2) | (!f1 & !f2) */ /* f1 <=> f2 == (f1 & f2) | (!f1 & !f2) */
case binop::Equiv: case binop::Equiv:
result_ = new multop(multop::Or, result_ = multop::instance(multop::Or,
new multop(multop::And, f1, f2), multop::instance(multop::And, f1, f2),
new multop(multop::And, multop::instance(multop::And,
new unop(unop::Not, f1), unop::instance(unop::Not,
new unop(unop::Not, f2))); f1),
unop::instance(unop::Not,
f2)));
return; return;
/* f1 U f2 == f1 U f2 */ /* f1 U f2 == f1 U f2 */
/* f1 R f2 == f1 R f2 */ /* f1 R f2 == f1 R f2 */
case binop::U: case binop::U:
case binop::R: case binop::R:
result_ = new binop(bo->op(), f1, f2); result_ = binop::instance(bo->op(), f1, f2);
return; return;
} }
/* Unreachable code. */ /* Unreachable code. */
assert(0); assert(0);
} }
formula* formula*
unabbreviate_logic_visitor::recurse(const formula* f) unabbreviate_logic_visitor::recurse(formula* f)
{ {
return unabbreviate_logic(f); return unabbreviate_logic(f);
} }
formula* formula*
unabbreviate_logic(const formula* f) unabbreviate_logic(formula* f)
{ {
unabbreviate_logic_visitor v; unabbreviate_logic_visitor v;
f->accept(v); f->accept(v);

View file

@ -3,11 +3,11 @@
#include "clone.hh" #include "clone.hh"
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
/// \brief Clone and rewrite a formula to remove most of the /// \brief Clone and rewrite a formula to remove most of the
/// abbreviated logical operators. /// abbreviated logical operators.
/// ///
/// This will rewrite binary operators such as binop::Implies, /// This will rewrite binary operators such as binop::Implies,
@ -26,18 +26,18 @@ namespace spot
virtual ~unabbreviate_logic_visitor(); virtual ~unabbreviate_logic_visitor();
using super::visit; using super::visit;
void visit(const binop* bo); void visit(binop* bo);
virtual formula* recurse(const formula* f); virtual formula* recurse(formula* f);
}; };
/// \brief Clone rewrite a formula to remove most of the abbreviated /// \brief Clone rewrite a formula to remove most of the abbreviated
/// logical operators. /// logical operators.
/// ///
/// This will rewrite binary operators such as binop::Implies, /// This will rewrite binary operators such as binop::Implies,
/// binop::Equals, and binop::Xor, using only unop::Not, multop::Or, /// binop::Equals, and binop::Xor, using only unop::Not, multop::Or,
/// and multop::And. /// and multop::And.
formula* unabbreviate_logic(const formula* f); formula* unabbreviate_logic(formula* f);
} }
} }

View file

@ -6,7 +6,7 @@ namespace spot
namespace ltl namespace ltl
{ {
class negative_normal_form_visitor : public const_visitor class negative_normal_form_visitor : public visitor
{ {
public: public:
negative_normal_form_visitor(bool negated) negative_normal_form_visitor(bool negated)
@ -14,7 +14,7 @@ namespace spot
{ {
} }
virtual virtual
~negative_normal_form_visitor() ~negative_normal_form_visitor()
{ {
} }
@ -23,43 +23,43 @@ namespace spot
{ {
return result_; return result_;
} }
void void
visit(const atomic_prop* ap) visit(atomic_prop* ap)
{ {
formula* f = new atomic_prop(ap->name(), ap->env()); formula* f = ap->ref();
if (negated_) if (negated_)
result_ = new unop(unop::Not, f); result_ = unop::instance(unop::Not, f);
else else
result_ = f; result_ = f;
} }
void void
visit(const constant* c) visit(constant* c)
{ {
if (! negated_) if (! negated_)
{ {
result_ = new constant(c->val()); result_ = c;
return; return;
} }
switch (c->val()) switch (c->val())
{ {
case constant::True: case constant::True:
result_ = new constant(constant::False); result_ = constant::false_instance();
return; return;
case constant::False: case constant::False:
result_ = new constant(constant::True); result_ = constant::true_instance();
return; return;
} }
/* Unreachable code. */ /* Unreachable code. */
assert(0); assert(0);
} }
void void
visit(const unop* uo) visit(unop* uo)
{ {
const formula* f = uo->child(); formula* f = uo->child();
switch (uo->op()) switch (uo->op())
{ {
case unop::Not: case unop::Not:
@ -67,63 +67,67 @@ namespace spot
return; return;
case unop::X: case unop::X:
/* !Xa == X!a */ /* !Xa == X!a */
result_ = new unop(unop::X, recurse(f)); result_ = unop::instance(unop::X, recurse(f));
return; return;
case unop::F: case unop::F:
/* !Fa == G!a */ /* !Fa == G!a */
result_ = new unop(negated_ ? unop::G : unop::F, recurse(f)); result_ = unop::instance(negated_ ? unop::G : unop::F, recurse(f));
return; return;
case unop::G: case unop::G:
/* !Ga == F!a */ /* !Ga == F!a */
result_ = new unop(negated_ ? unop::F : unop::G, recurse(f)); result_ = unop::instance(negated_ ? unop::F : unop::G, recurse(f));
return; return;
} }
/* Unreachable code. */ /* Unreachable code. */
assert(0); assert(0);
} }
void void
visit(const binop* bo) visit(binop* bo)
{ {
const formula* f1 = bo->first(); formula* f1 = bo->first();
const formula* f2 = bo->second(); formula* f2 = bo->second();
switch (bo->op()) switch (bo->op())
{ {
case binop::Xor: case binop::Xor:
/* !(a ^ b) == a <=> b */ /* !(a ^ b) == a <=> b */
result_ = new binop(negated_ ? binop::Equiv : binop::Xor, result_ = binop::instance(negated_ ? binop::Equiv : binop::Xor,
recurse_(f1, false), recurse_(f2, false)); recurse_(f1, false),
recurse_(f2, false));
return; return;
case binop::Equiv: case binop::Equiv:
/* !(a <=> b) == a ^ b */ /* !(a <=> b) == a ^ b */
result_ = new binop(negated_ ? binop::Xor : binop::Equiv, result_ = binop::instance(negated_ ? binop::Xor : binop::Equiv,
recurse_(f1, false), recurse_(f2, false)); recurse_(f1, false),
recurse_(f2, false));
return; return;
case binop::Implies: case binop::Implies:
if (negated_) if (negated_)
/* !(a => b) == a & !b */ /* !(a => b) == a & !b */
result_ = new multop(multop::And, result_ = multop::instance(multop::And,
recurse_(f1, false), recurse_(f2, true)); recurse_(f1, false),
recurse_(f2, true));
else else
result_ = new binop(binop::Implies, recurse(f1), recurse(f2)); result_ = binop::instance(binop::Implies,
recurse(f1), recurse(f2));
return; return;
case binop::U: case binop::U:
/* !(a U b) == !a R !b */ /* !(a U b) == !a R !b */
result_ = new binop(negated_ ? binop::R : binop::U, result_ = binop::instance(negated_ ? binop::R : binop::U,
recurse(f1), recurse(f2)); recurse(f1), recurse(f2));
return; return;
case binop::R: case binop::R:
/* !(a R b) == !a U !b */ /* !(a R b) == !a U !b */
result_ = new binop(negated_ ? binop::U : binop::R, result_ = binop::instance(negated_ ? binop::U : binop::R,
recurse(f1), recurse(f2)); recurse(f1), recurse(f2));
return; return;
} }
/* Unreachable code. */ /* Unreachable code. */
assert(0); assert(0);
} }
void void
visit(const multop* mo) visit(multop* mo)
{ {
/* !(a & b & c) == !a | !b | !c */ /* !(a & b & c) == !a | !b | !c */
/* !(a | b | c) == !a & !b & !c */ /* !(a | b | c) == !a & !b & !c */
@ -138,32 +142,32 @@ namespace spot
op = multop::And; op = multop::And;
break; break;
} }
multop* res = new multop(op); multop* res = multop::instance(op);
unsigned mos = mo->size(); unsigned mos = mo->size();
for (unsigned i = 0; i < mos; ++i) for (unsigned i = 0; i < mos; ++i)
res->add(recurse(mo->nth(i))); multop::add(&res, recurse(mo->nth(i)));
result_ = res; result_ = res;
} }
formula* formula*
recurse_(const formula* f, bool negated) recurse_(formula* f, bool negated)
{ {
return negative_normal_form(f, negated); return negative_normal_form(f, negated);
} }
formula* formula*
recurse(const formula* f) recurse(formula* f)
{ {
return recurse_(f, negated_); return recurse_(f, negated_);
} }
protected: protected:
formula* result_; formula* result_;
bool negated_; bool negated_;
}; };
formula* formula*
negative_normal_form(const formula* f, bool negated) negative_normal_form(formula* f, bool negated)
{ {
negative_normal_form_visitor v(negated); negative_normal_form_visitor v(negated);
f->accept(v); f->accept(v);

View file

@ -4,13 +4,13 @@
#include "ltlast/formula.hh" #include "ltlast/formula.hh"
#include "ltlast/visitor.hh" #include "ltlast/visitor.hh"
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
/// \brief Build the negative normal form of \a f. /// \brief Build the negative normal form of \a f.
/// ///
/// All negations of the formula are pushed in front of the /// All negations of the formula are pushed in front of the
/// atomic propositions. /// atomic propositions.
/// ///
/// \param f The formula to normalize. /// \param f The formula to normalize.
@ -22,7 +22,7 @@ namespace spot
/// or spot::ltl::unabbreviate_ltl first. (Calling these functions /// or spot::ltl::unabbreviate_ltl first. (Calling these functions
/// after spot::ltl::negative_normal_form would likely produce a /// after spot::ltl::negative_normal_form would likely produce a
/// formula which is not in negative normal form.) /// formula which is not in negative normal form.)
formula* negative_normal_form(const formula* f, bool negated = false); formula* negative_normal_form(formula* f, bool negated = false);
} }
} }

View file

@ -1,7 +1,7 @@
#include "ltlast/allnodes.hh" #include "ltlast/allnodes.hh"
#include "tunabbrev.hh" #include "tunabbrev.hh"
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
@ -13,8 +13,8 @@ namespace spot
{ {
} }
void void
unabbreviate_ltl_visitor::visit(const unop* uo) unabbreviate_ltl_visitor::visit(unop* uo)
{ {
switch (uo->op()) switch (uo->op())
{ {
@ -23,26 +23,26 @@ namespace spot
this->super::visit(uo); this->super::visit(uo);
return; return;
case unop::F: case unop::F:
result_ = new binop(binop::U, result_ = binop::instance(binop::U,
new constant(constant::True), constant::true_instance(),
recurse(uo->child())); recurse(uo->child()));
return; return;
case unop::G: case unop::G:
result_ = new binop(binop::R, result_ = binop::instance(binop::R,
new constant(constant::False), constant::false_instance(),
recurse(uo->child())); recurse(uo->child()));
return; return;
} }
} }
formula* formula*
unabbreviate_ltl_visitor::recurse(const formula* f) unabbreviate_ltl_visitor::recurse(formula* f)
{ {
return unabbreviate_ltl(f); return unabbreviate_ltl(f);
} }
formula* formula*
unabbreviate_ltl(const formula* f) unabbreviate_ltl(formula* f)
{ {
unabbreviate_ltl_visitor v; unabbreviate_ltl_visitor v;
f->accept(v); f->accept(v);

View file

@ -4,11 +4,11 @@
#include "ltlast/formula.hh" #include "ltlast/formula.hh"
#include "ltlvisit/lunabbrev.hh" #include "ltlvisit/lunabbrev.hh"
namespace spot namespace spot
{ {
namespace ltl namespace ltl
{ {
/// \brief Clone and rewrite a formula to remove most of the /// \brief Clone and rewrite a formula to remove most of the
/// abbreviated LTL and logical operators. /// abbreviated LTL and logical operators.
/// ///
/// The rewriting performed on logical operator is /// The rewriting performed on logical operator is
@ -28,12 +28,12 @@ namespace spot
unabbreviate_ltl_visitor(); unabbreviate_ltl_visitor();
virtual ~unabbreviate_ltl_visitor(); virtual ~unabbreviate_ltl_visitor();
void visit(const unop* uo); void visit(unop* uo);
formula* recurse(const formula* f); formula* recurse(formula* f);
}; };
/// \brief Clone and rewrite a formula to remove most of the /// \brief Clone and rewrite a formula to remove most of the
/// abbreviated LTL and logical operators. /// abbreviated LTL and logical operators.
/// ///
/// The rewriting performed on logical operator is /// The rewriting performed on logical operator is
@ -41,7 +41,7 @@ namespace spot
/// ///
/// This will also rewrite unary operators such as unop::F, /// This will also rewrite unary operators such as unop::F,
/// and unop::G, using only binop::U, and binop::R. /// and unop::G, using only binop::U, and binop::R.
formula* unabbreviate_ltl(const formula* f); formula* unabbreviate_ltl(formula* f);
} }
} }