Get rid of all dynamic_cast<>s while working on LTL formulae.

They are too slow.

* src/ltlast/formula.hh (opkind, kind, kind_): Use an enum
to indicate the actual kind of the formula.  This way we can
check the kind of a formula without relying on dynamic_cast.
* src/ltlast/atomic_prop.cc, src/ltlast/automatop.cc,
src/ltlast/binop.cc, src/ltlast/bunop.cc, src/ltlast/constant.cc,
src/ltlast/multop.cc, src/ltlast/refformula.cc,
src/ltlast/refformula.hh, src/ltlast/unop.cc: Adjust constructors.
* src/ltlvisit/basicreduce.cc, src/ltlvisit/mark.cc,
src/ltlvisit/reduce.cc, src/ltlvisit/syntimpl.cc,
src/ltlvisit/tostring.cc: Replace all dynamic_cast by a
call to kind() followed by a static_cast.
This commit is contained in:
Alexandre Duret-Lutz 2010-12-09 13:19:44 +01:00
parent 48cde88b9b
commit 957ba664b7
15 changed files with 743 additions and 609 deletions

View file

@ -32,7 +32,7 @@ namespace spot
{ {
atomic_prop::atomic_prop(const std::string& name, environment& env) atomic_prop::atomic_prop(const std::string& name, environment& env)
: name_(name), env_(&env) : ref_formula(AtomicProp), name_(name), env_(&env)
{ {
is.boolean = true; is.boolean = true;
is.sugar_free_boolean = true; is.sugar_free_boolean = true;

View file

@ -28,7 +28,7 @@ namespace spot
namespace ltl namespace ltl
{ {
automatop::automatop(const nfa::ptr nfa, vec* v, bool negated) automatop::automatop(const nfa::ptr nfa, vec* v, bool negated)
: nfa_(nfa), children_(v), negated_(negated) : ref_formula(AutomatOp), nfa_(nfa), children_(v), negated_(negated)
{ {
is.boolean = false; is.boolean = false;
is.sugar_free_boolean = true; is.sugar_free_boolean = true;

View file

@ -34,7 +34,7 @@ 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) : ref_formula(BinOp), op_(op), first_(first), second_(second)
{ {
// Beware: (f U g) is purely eventual if both operands // Beware: (f U g) is purely eventual if both operands
// are purely eventual, unlike in the proceedings of // are purely eventual, unlike in the proceedings of

View file

@ -31,7 +31,7 @@ namespace spot
namespace ltl namespace ltl
{ {
bunop::bunop(type op, formula* child, unsigned min, unsigned max) bunop::bunop(type op, formula* child, unsigned min, unsigned max)
: op_(op), child_(child), min_(min), max_(max) : ref_formula(BUnOp), op_(op), child_(child), min_(min), max_(max)
{ {
props = child->get_props(); props = child->get_props();
@ -314,9 +314,9 @@ namespace spot
// - Exp[*i..j][*min..max] = Exp[*i(min)..j(max)] // - Exp[*i..j][*min..max] = Exp[*i(min)..j(max)]
// if i*(min+1)<=j(min)+1. // if i*(min+1)<=j(min)+1.
bunop* s = dynamic_cast<bunop*>(child); if (child->kind() == BUnOp)
if (s)
{ {
bunop* s = static_cast<bunop*>(child);
unsigned i = s->min(); unsigned i = s->min();
unsigned j = s->max(); unsigned j = s->max();

View file

@ -34,7 +34,7 @@ namespace spot
constant constant::empty_word_instance_(constant::EmptyWord); constant constant::empty_word_instance_(constant::EmptyWord);
constant::constant(type val) constant::constant(type val)
: val_(val) : formula(Constant), val_(val)
{ {
switch (val) switch (val)
{ {

View file

@ -71,11 +71,20 @@ namespace spot
class formula class formula
{ {
public: public:
formula() : count_(max_count++) /// Kind of a sub-formula
enum opkind { Constant,
AtomicProp,
UnOp,
BinOp,
MultOp,
BUnOp,
AutomatOp };
formula(opkind k) : count_(max_count++), kind_(k)
{ {
// If the counter of formulae ever loops, we want to skip the // If the counter of formulae ever loops, we want to skip the
// first three values, because they are permanently associated // first three values, because they are permanently associated
// to constants, and its convenient to have constants smaller // to constants, and it is convenient to have constants smaller
// than all other formulae. // than all other formulae.
if (max_count == 0) if (max_count == 0)
max_count = 3; max_count = 3;
@ -100,6 +109,12 @@ namespace spot
/// Return a canonic representation of the formula /// Return a canonic representation of the formula
virtual std::string dump() const = 0; virtual std::string dump() const = 0;
/// Return the kind of the top-level operator.
opkind kind() const
{
return kind_;
}
//////////////// ////////////////
// Properties // // Properties //
//////////////// ////////////////
@ -279,6 +294,7 @@ namespace spot
private: private:
/// \brief Number of formulae created so far. /// \brief Number of formulae created so far.
static size_t max_count; static size_t max_count;
opkind kind_;
}; };
/// \brief Strict Weak Ordering for <code>const formula*</code>. /// \brief Strict Weak Ordering for <code>const formula*</code>.

View file

@ -34,7 +34,7 @@ namespace spot
namespace ltl namespace ltl
{ {
multop::multop(type op, vec* v) multop::multop(type op, vec* v)
: op_(op), children_(v) : ref_formula(MultOp), op_(op), children_(v)
{ {
unsigned s = v->size(); unsigned s = v->size();
assert(s > 1); assert(s > 1);
@ -177,17 +177,19 @@ namespace spot
vec::iterator i = v->begin(); vec::iterator i = v->begin();
while (i != v->end()) while (i != v->end())
{ {
multop* p = dynamic_cast<multop*>(*i); if ((*i)->kind() == MultOp)
if (p && p->op() == op) {
multop* p = static_cast<multop*>(*i);
if (p->op() == op)
{ {
unsigned ps = p->size(); unsigned ps = p->size();
for (unsigned n = 0; n < ps; ++n) for (unsigned n = 0; n < ps; ++n)
inlined.push_back(p->nth(n)->clone()); inlined.push_back(p->nth(n)->clone());
(*i)->destroy(); (*i)->destroy();
i = v->erase(i); i = v->erase(i);
continue;
}
} }
else
{
// All operator except "Concat" and "Fusion" are // All operator except "Concat" and "Fusion" are
// commutative, so we just keep a list of the inlined // commutative, so we just keep a list of the inlined
// arguments that should later be added to the vector. // arguments that should later be added to the vector.
@ -197,7 +199,6 @@ namespace spot
inlined.push_back(*i); inlined.push_back(*i);
++i; ++i;
} }
}
if (op == Concat || op == Fusion) if (op == Concat || op == Fusion)
*v = inlined; *v = inlined;
else else

View file

@ -1,3 +1,5 @@
// Copyright (C) 2010 Laboratoire de Recherche de Developpement de
// l'EPITA (LRDE).
// Copyright (C) 2003, 2004 Laboratoire d'Informatique de Paris 6 (LIP6), // Copyright (C) 2003, 2004 Laboratoire d'Informatique de Paris 6 (LIP6),
// département Systèmes Répartis Coopératifs (SRC), Université Pierre // département Systèmes Répartis Coopératifs (SRC), Université Pierre
// et Marie Curie. // et Marie Curie.
@ -26,8 +28,8 @@ namespace spot
{ {
namespace ltl namespace ltl
{ {
ref_formula::ref_formula() ref_formula::ref_formula(opkind k)
: ref_counter_(0) : formula(k), ref_counter_(0)
{ {
} }

View file

@ -1,6 +1,8 @@
// Copyright (C) 2003, 2004, 2005 Laboratoire d'Informatique de Paris 6 (LIP6), // Copyright (C) 2010 Laboratoire de Recherche de Developpement de
// département Systèmes Répartis Coopératifs (SRC), Université Pierre // l'EPITA (LRDE).
// et Marie Curie. // Copyright (C) 2003, 2004, 2005 Laboratoire d'Informatique de Paris
// 6 (LIP6), département Systèmes Répartis Coopératifs (SRC),
// Université Pierre et Marie Curie.
// //
// This file is part of Spot, a model checking library. // This file is part of Spot, a model checking library.
// //
@ -37,7 +39,7 @@ namespace spot
{ {
protected: protected:
virtual ~ref_formula(); virtual ~ref_formula();
ref_formula(); ref_formula(opkind k);
void ref_(); void ref_();
bool unref_(); bool unref_();
/// Number of references to this formula. /// Number of references to this formula.

View file

@ -33,13 +33,13 @@ namespace spot
namespace ltl namespace ltl
{ {
unop::unop(type op, formula* child) unop::unop(type op, formula* child)
: op_(op), child_(child) : ref_formula(UnOp), op_(op), child_(child)
{ {
props = child->get_props(); props = child->get_props();
switch (op) switch (op)
{ {
case Not: case Not:
is.in_nenoform = !!dynamic_cast<atomic_prop*>(child); is.in_nenoform = (child->kind() == AtomicProp);
is.accepting_eword = false; is.accepting_eword = false;
break; break;
case X: case X:
@ -162,11 +162,14 @@ namespace spot
{ {
case F: case F:
case G: case G:
{
if (child->kind() == UnOp)
{ {
// F and G are idempotent. // F and G are idempotent.
unop* u = dynamic_cast<unop*>(child); unop* u = static_cast<unop*>(child);
if (u && u->op() == op) if (u->op() == op)
return u; return u;
}
// F(0) = G(0) = 0 // F(0) = G(0) = 0
// F(1) = G(1) = 1 // F(1) = G(1) = 1
@ -191,9 +194,9 @@ namespace spot
return bunop::instance(bunop::Star, return bunop::instance(bunop::Star,
constant::true_instance(), 1); constant::true_instance(), 1);
unop* u = dynamic_cast<unop*>(child); if (child->kind() == UnOp)
if (u)
{ {
unop* u = static_cast<unop*>(child);
// "Not" is an involution. // "Not" is an involution.
if (u->op() == op) if (u->op() == op)
{ {

View file

@ -33,27 +33,31 @@ namespace spot
bool bool
is_GF(const formula* f) is_GF(const formula* f)
{ {
const unop* op = dynamic_cast<const unop*>(f); if (f->kind() != formula::UnOp)
if (op && op->op() == unop::G)
{
const unop* opchild = dynamic_cast<const unop*>(op->child());
if (opchild && opchild->op() == unop::F)
return true;
}
return false; return false;
const unop* op = static_cast<const unop*>(f);
if (op->op() != unop::G)
return false;
const formula* c = op->child();
if (c->kind() != formula::UnOp)
return false;
const unop* opchild = static_cast<const unop*>(c);
return opchild->op() == unop::F;
} }
bool bool
is_FG(const formula* f) is_FG(const formula* f)
{ {
const unop* op = dynamic_cast<const unop*>(f); if (f->kind() != formula::UnOp)
if (op && op->op() == unop::F)
{
const unop* opchild = dynamic_cast<const unop*>(op->child());
if (opchild && opchild->op() == unop::G)
return true;
}
return false; return false;
const unop* op = static_cast<const unop*>(f);
if (op->op() != unop::F)
return false;
const formula* c = op->child();
if (c->kind() != formula::UnOp)
return false;
const unop* opchild = static_cast<const unop*>(c);
return opchild->op() == unop::G;
} }
namespace namespace
@ -134,7 +138,7 @@ namespace spot
case unop::X: case unop::X:
// X(true) = true // X(true) = true
// X(false) = false // X(false) = false
if (dynamic_cast<constant*>(result_)) if (result_->kind() == formula::Constant)
return; return;
// XGF(f) = GF(f) // XGF(f) = GF(f)
@ -143,9 +147,9 @@ namespace spot
// X(f1 & GF(f2)) = X(f1) & GF(F2) // X(f1 & GF(f2)) = X(f1) & GF(F2)
// X(f1 | GF(f2)) = X(f1) | GF(F2) // X(f1 | GF(f2)) = X(f1) | GF(F2)
mo = dynamic_cast<multop*>(result_); if (result_->kind() == formula::MultOp)
if (mo)
{ {
mo = static_cast<multop*>(result_);
result_ = param_case(mo, unop::X, mo->op()); result_ = param_case(mo, unop::X, mo->op());
return; return;
} }
@ -156,23 +160,25 @@ namespace spot
case unop::F: case unop::F:
// F(true) = true // F(true) = true
// F(false) = false // F(false) = false
if (dynamic_cast<constant*>(result_)) if (result_->kind() == formula::Constant)
return; return;
// FX(a) = XF(a) // FX(a) = XF(a)
u = dynamic_cast<unop*>(result_); if (result_->kind() == formula::UnOp)
if (u && u->op() == unop::X) {
u = static_cast<unop*>(result_);
if (u->op() == unop::X)
{ {
formula* res = formula* res =
unop::instance(unop::X, unop::instance(unop::X, unop::instance
unop::instance(unop::F, (unop::F, basic_reduce(u->child())));
basic_reduce(u->child())));
u->destroy(); u->destroy();
// FXX(a) = XXF(a) ... // FXX(a) = XXF(a) ...
result_ = basic_reduce(res); result_ = basic_reduce(res);
res->destroy(); res->destroy();
return; return;
} }
}
#if 0 #if 0
// F(f1 & GF(f2)) = F(f1) & GF(F2) // F(f1 & GF(f2)) = F(f1) & GF(F2)
@ -189,12 +195,15 @@ namespace spot
// GF(f2)) | (a & GF(b))) is indeed easier to translate. // GF(f2)) | (a & GF(b))) is indeed easier to translate.
// //
// So let's not consider this rewriting rule. // So let's not consider this rewriting rule.
mo = dynamic_cast<multop*>(result_); if (result_->kind() == formula::MultOp)
if (mo && mo->op() == multop::And) {
mo = static_cast<multop*>(result_);
if (mo->op() == multop::And)
{ {
result_ = param_case(mo, unop::F, multop::And); result_ = param_case(mo, unop::F, multop::And);
return; return;
} }
}
#endif #endif
result_ = unop::instance(unop::F, result_); result_ = unop::instance(unop::F, result_);
@ -203,27 +212,31 @@ namespace spot
case unop::G: case unop::G:
// G(true) = true // G(true) = true
// G(false) = false // G(false) = false
if (dynamic_cast<constant*>(result_)) if (result_->kind() == formula::Constant)
return; return;
// G(a R b) = G(b) // G(a R b) = G(b)
bo = dynamic_cast<binop*>(result_); if (result_->kind() == formula::BinOp)
if (bo && bo->op() == binop::R) {
bo = static_cast<binop*>(result_);
if (bo->op() == binop::R)
{ {
result_ = unop::instance(unop::G, result_ = unop::instance(unop::G,
basic_reduce(bo->second())); basic_reduce(bo->second()));
bo->destroy(); bo->destroy();
return; return;
} }
}
// GX(a) = XG(a) // GX(a) = XG(a)
u = dynamic_cast<unop*>(result_); if (result_->kind() == formula::UnOp)
if (u && u->op() == unop::X) {
u = static_cast<unop*>(result_);
if (u->op() == unop::X)
{ {
formula* res = formula* res =
unop::instance(unop::X, unop::instance(unop::X, unop::instance
unop::instance(unop::G, (unop::G, basic_reduce(u->child())));
basic_reduce(u->child())));
u->destroy(); u->destroy();
// GXX(a) = XXG(a) ... // GXX(a) = XXG(a) ...
// GXF(a) = XGF(a) = GF(a) ... // GXF(a) = XGF(a) = GF(a) ...
@ -231,14 +244,18 @@ namespace spot
res->destroy(); res->destroy();
return; return;
} }
}
// G(f1 | GF(f2)) = G(f1) | GF(F2) // G(f1 | GF(f2)) = G(f1) | GF(F2)
mo = dynamic_cast<multop*>(result_); if (result_->kind() == formula::MultOp)
if (mo && mo->op() == multop::Or) {
mo = static_cast<multop*>(result_);
if (mo->op() == multop::Or)
{ {
result_ = param_case(mo, unop::G, multop::Or); result_ = param_case(mo, unop::G, multop::Or);
return; return;
} }
}
result_ = unop::instance(unop::G, result_); result_ = unop::instance(unop::G, result_);
return; return;
@ -297,19 +314,23 @@ namespace spot
// a R true = true // a R true = true
// a W true = true // a W true = true
// a M false = false // a M false = false
if (dynamic_cast<constant*>(f2)) if (f2->kind() == formula::Constant)
{ {
result_ = f2; result_ = f2;
f1->destroy(); f1->destroy();
return; return;
} }
// Same effect as dynamic_cast<unop*>, only faster.
unop* fu1 =
(f1->kind() == formula::UnOp) ? static_cast<unop*>(f1) : 0;
unop* fu2 =
(f2->kind() == formula::UnOp) ? static_cast<unop*>(f2) : 0;
// X(a) U X(b) = X(a U b) // X(a) U X(b) = X(a U b)
// X(a) R X(b) = X(a R b) // X(a) R X(b) = X(a R b)
// X(a) W X(b) = X(a W b) // X(a) W X(b) = X(a W b)
// X(a) M X(b) = X(a M b) // X(a) M X(b) = X(a M b)
unop* fu1 = dynamic_cast<unop*>(f1);
unop* fu2 = dynamic_cast<unop*>(f2);
if (fu1 && fu2 if (fu1 && fu2
&& fu1->op() == unop::X && fu1->op() == unop::X
&& fu2->op() == unop::X) && fu2->op() == unop::X)
@ -337,14 +358,18 @@ namespace spot
// a U (b | G(a)) = a W b // a U (b | G(a)) = a W b
// a W (b | G(a)) = a W b // a W (b | G(a)) = a W b
multop* fm2 = dynamic_cast<multop*>(f2); if (f2->kind() == formula::MultOp)
if (fm2 && fm2->op() == multop::Or) {
multop* fm2 = static_cast<multop*>(f2);
if (fm2->op() == multop::Or)
{ {
int s = fm2->size(); int s = fm2->size();
for (int i = 0; i < s; ++i) for (int i = 0; i < s; ++i)
{ {
unop* c = dynamic_cast<unop*>(fm2->nth(i)); if (fm2->nth(i)->kind() != formula::UnOp)
if (c && c->op() == unop::G && c->child() == f1) continue;
unop* c = static_cast<unop*>(fm2->nth(i));
if (c->op() == unop::G && c->child() == f1)
{ {
multop::vec* v = new multop::vec; multop::vec* v = new multop::vec;
v->reserve(s - 1); v->reserve(s - 1);
@ -354,16 +379,16 @@ namespace spot
// skip j=i // skip j=i
for (++j; j < s; ++j) for (++j; j < s; ++j)
v->push_back(fm2->nth(j)->clone()); v->push_back(fm2->nth(j)->clone());
result_ = result_ = binop::instance
binop::instance(binop::W, f1, (binop::W, f1,
multop::instance(multop::Or, multop::instance(multop::Or, v));
v));
f2->destroy(); f2->destroy();
return; return;
} }
} }
} }
} }
}
else if (op == binop::M || op == binop::R) else if (op == binop::M || op == binop::R)
{ {
// a R Fa = Fa // a R Fa = Fa
@ -377,14 +402,18 @@ namespace spot
// a R (b & F(a)) = a M b // a R (b & F(a)) = a M b
// a M (b & F(a)) = a M b // a M (b & F(a)) = a M b
multop* fm2 = dynamic_cast<multop*>(f2); if (f2->kind() == formula::MultOp)
if (fm2 && fm2->op() == multop::And) {
multop* fm2 = static_cast<multop*>(f2);
if (fm2->op() == multop::And)
{ {
int s = fm2->size(); int s = fm2->size();
for (int i = 0; i < s; ++i) for (int i = 0; i < s; ++i)
{ {
unop* c = dynamic_cast<unop*>(fm2->nth(i)); if (fm2->nth(i)->kind() != formula::UnOp)
if (c && c->op() == unop::F && c->child() == f1) continue;
unop* c = static_cast<unop*>(fm2->nth(i));
if (c->op() == unop::F && c->child() == f1)
{ {
multop::vec* v = new multop::vec; multop::vec* v = new multop::vec;
v->reserve(s - 1); v->reserve(s - 1);
@ -394,16 +423,16 @@ namespace spot
// skip j=i // skip j=i
for (++j; j < s; ++j) for (++j; j < s; ++j)
v->push_back(fm2->nth(j)->clone()); v->push_back(fm2->nth(j)->clone());
result_ = result_ = binop::instance
binop::instance(binop::M, f1, (binop::M, f1,
multop::instance(multop::And, multop::instance(multop::And, v));
v));
f2->destroy(); f2->destroy();
return; return;
} }
} }
} }
} }
}
result_ = binop::instance(op, f1, f2); result_ = binop::instance(op, f1, f2);
return; return;
@ -449,10 +478,11 @@ namespace spot
// of the vector to mark them as redundant. Skip them. // of the vector to mark them as redundant. Skip them.
if (!*i) if (!*i)
continue; continue;
unop* uo = dynamic_cast<unop*>(*i); switch ((*i)->kind())
binop* bo = dynamic_cast<binop*>(*i);
if (uo)
{ {
case formula::UnOp:
{
unop* uo = static_cast<unop*>(*i);
if (uo->op() == unop::X) if (uo->op() == unop::X)
{ {
// Xa & Xb = X(a & b) // Xa & Xb = X(a & b)
@ -461,7 +491,7 @@ namespace spot
else if (is_FG(*i)) else if (is_FG(*i))
{ {
// FG(a) & FG(b) = FG(a & b) // FG(a) & FG(b) = FG(a & b)
unop* uo2 = dynamic_cast<unop*>(uo->child()); unop* uo2 = static_cast<unop*>(uo->child());
tmpFG->push_back(uo2->child()->clone()); tmpFG->push_back(uo2->child()->clone());
} }
else if (uo->op() == unop::G) else if (uo->op() == unop::G)
@ -482,9 +512,10 @@ namespace spot
{ {
if (!*j) if (!*j)
continue; continue;
binop* b = dynamic_cast<binop*>(*j); if ((*i)->kind() != formula::BinOp)
if (b && (b->op() == binop::R continue;
|| b->op() == binop::M) binop* b = static_cast<binop*>(*j);
if ((b->op() == binop::R || b->op() == binop::M)
&& b->first() == a) && b->first() == a)
{ {
formula* r = formula* r =
@ -496,7 +527,7 @@ namespace spot
*j = 0; *j = 0;
rewritten = true; rewritten = true;
} }
else if (b && (b->op() == binop::W else if ((b->op() == binop::W
|| b->op() == binop::U) || b->op() == binop::U)
&& b->second() == a) && b->second() == a)
{ {
@ -517,16 +548,18 @@ namespace spot
{ {
tmpOther->push_back((*i)->clone()); tmpOther->push_back((*i)->clone());
} }
break;
} }
else if (bo) case formula::BinOp:
{ {
binop* bo = static_cast<binop*>(*i);
if (bo->op() == binop::U || bo->op() == binop::W) if (bo->op() == binop::U || bo->op() == binop::W)
{ {
// (a U b) & (c U b) = (a & c) U b // (a U b) & (c U b) = (a & c) U b
// (a U b) & (c W b) = (a & c) U b // (a U b) & (c W b) = (a & c) U b
// (a W b) & (c W b) = (a & c) W b // (a W b) & (c W b) = (a & c) W b
bool weak = true; bool weak = true;
formula* ftmp = dynamic_cast<binop*>(*i)->second(); formula* ftmp = bo->second();
multop::vec* right = new multop::vec; multop::vec* right = new multop::vec;
for (multop::vec::iterator j = i; j != res->end(); for (multop::vec::iterator j = i; j != res->end();
j++) j++)
@ -535,16 +568,22 @@ namespace spot
continue; continue;
// (a U b) & Fb = a U b. // (a U b) & Fb = a U b.
// (a W b) & Fb = a U b. // (a W b) & Fb = a U b.
unop* uo2 = dynamic_cast<unop*>(*j); if ((*j)->kind() == formula::UnOp)
if (uo2 && uo2->op() == unop::F {
unop* uo2 = static_cast<unop*>(*j);
if (uo2->op() == unop::F
&& uo2->child() == ftmp) && uo2->child() == ftmp)
{ {
(*j)->destroy(); (*j)->destroy();
*j = 0; *j = 0;
weak = false; weak = false;
continue;
} }
binop* bo2 = dynamic_cast<binop*>(*j); }
if (bo2 && (bo2->op() == binop::U if ((*j)->kind() == formula::BinOp)
{
binop* bo2 = static_cast<binop*>(*j);
if ((bo2->op() == binop::U
|| bo2->op() == binop::W) || bo2->op() == binop::W)
&& ftmp == bo2->second()) && ftmp == bo2->second())
{ {
@ -555,6 +594,8 @@ namespace spot
{ {
(*j)->destroy(); (*j)->destroy();
*j = 0; *j = 0;
continue;
}
} }
} }
} }
@ -571,7 +612,7 @@ namespace spot
// (a R b) & (a R c) = a R (b & c) // (a R b) & (a R c) = a R (b & c)
// (a R b) & (a M c) = a M (b & c) // (a R b) & (a M c) = a M (b & c)
bool weak = true; bool weak = true;
formula* ftmp = dynamic_cast<binop*>(*i)->first(); formula* ftmp = bo->first();
multop::vec* right = new multop::vec; multop::vec* right = new multop::vec;
for (multop::vec::iterator j = i; j != res->end(); for (multop::vec::iterator j = i; j != res->end();
j++) j++)
@ -580,26 +621,35 @@ namespace spot
continue; continue;
// (a R b) & Fa = a M b. // (a R b) & Fa = a M b.
// (a M b) & Fa = a M b. // (a M b) & Fa = a M b.
unop* uo2 = dynamic_cast<unop*>(*j); if ((*j)->kind() == formula::UnOp)
if (uo2 && uo2->op() == unop::F {
unop* uo2 = static_cast<unop*>(*j);
if (uo2->op() == unop::F
&& uo2->child() == ftmp) && uo2->child() == ftmp)
{ {
(*j)->destroy(); (*j)->destroy();
*j = 0; *j = 0;
weak = false; weak = false;
continue;
} }
binop* bo2 = dynamic_cast<binop*>(*j); }
if (bo2 && (bo2->op() == binop::R if ((*j)->kind() == formula::BinOp)
{
binop* bo2 = static_cast<binop*>(*j);
if ((bo2->op() == binop::R
|| bo2->op() == binop::M) || bo2->op() == binop::M)
&& ftmp == bo2->first()) && ftmp == bo2->first())
{ {
if (bo2->op() == binop::M) if (bo2->op() == binop::M)
weak = false; weak = false;
right->push_back(bo2->second()->clone()); right->push_back
(bo2->second()->clone());
if (j != i) if (j != i)
{ {
(*j)->destroy(); (*j)->destroy();
*j = 0; *j = 0;
continue;
}
} }
} }
} }
@ -615,9 +665,9 @@ namespace spot
{ {
tmpOther->push_back((*i)->clone()); tmpOther->push_back((*i)->clone());
} }
break;
} }
else default:
{
tmpOther->push_back((*i)->clone()); tmpOther->push_back((*i)->clone());
} }
(*i)->destroy(); (*i)->destroy();
@ -630,10 +680,11 @@ namespace spot
{ {
if (!*i) if (!*i)
continue; continue;
unop* uo = dynamic_cast<unop*>(*i); switch ((*i)->kind())
binop* bo = dynamic_cast<binop*>(*i);
if (uo)
{ {
case formula::UnOp:
{
unop* uo = static_cast<unop*>(*i);
if (uo->op() == unop::X) if (uo->op() == unop::X)
{ {
// Xa | Xb = X(a | b) // Xa | Xb = X(a | b)
@ -642,7 +693,7 @@ namespace spot
else if (is_GF(*i)) else if (is_GF(*i))
{ {
// GF(a) | GF(b) = GF(a | b) // GF(a) | GF(b) = GF(a | b)
unop* uo2 = dynamic_cast<unop*>(uo->child()); unop* uo2 = static_cast<unop*>(uo->child());
tmpGF->push_back(uo2->child()->clone()); tmpGF->push_back(uo2->child()->clone());
} }
else if (uo->op() == unop::F) else if (uo->op() == unop::F)
@ -661,9 +712,10 @@ namespace spot
{ {
if (!*j) if (!*j)
continue; continue;
binop* b = dynamic_cast<binop*>(*j); if ((*j)->kind() != formula::BinOp)
if (b && (b->op() == binop::U continue;
|| b->op() == binop::W) binop* b = static_cast<binop*>(*j);
if ((b->op() == binop::U || b->op() == binop::W)
&& b->first() == a) && b->first() == a)
{ {
formula* r = formula* r =
@ -683,9 +735,11 @@ namespace spot
{ {
tmpOther->push_back((*i)->clone()); tmpOther->push_back((*i)->clone());
} }
break;
} }
else if (bo) case formula::BinOp:
{ {
binop* bo = static_cast<binop*>(*i);
if (bo->op() == binop::U || bo->op() == binop::W) if (bo->op() == binop::U || bo->op() == binop::W)
{ {
// (a U b) | (a U c) = a U (b | c) // (a U b) | (a U c) = a U (b | c)
@ -700,26 +754,35 @@ namespace spot
continue; continue;
// (a U b) | Ga = a W b. // (a U b) | Ga = a W b.
// (a W b) | Ga = a W b. // (a W b) | Ga = a W b.
unop* uo2 = dynamic_cast<unop*>(*j); if ((*j)->kind() == formula::UnOp)
if (uo2 && uo2->op() == unop::G {
unop* uo2 = static_cast<unop*>(*j);
if (uo2->op() == unop::G
&& uo2->child() == ftmp) && uo2->child() == ftmp)
{ {
(*j)->destroy(); (*j)->destroy();
*j = 0; *j = 0;
weak = true; weak = true;
continue;
} }
binop* bo2 = dynamic_cast<binop*>(*j); }
if (bo2 && (bo2->op() == binop::U || if ((*j)->kind() == formula::BinOp)
{
binop* bo2 = static_cast<binop*>(*j);
if ((bo2->op() == binop::U ||
bo2->op() == binop::W) bo2->op() == binop::W)
&& ftmp == bo2->first()) && ftmp == bo2->first())
{ {
if (bo2->op() == binop::W) if (bo2->op() == binop::W)
weak = true; weak = true;
right->push_back(bo2->second()->clone()); right->push_back
(bo2->second()->clone());
if (j != i) if (j != i)
{ {
(*j)->destroy(); (*j)->destroy();
*j = 0; *j = 0;
continue;
}
} }
} }
} }
@ -736,7 +799,7 @@ namespace spot
// (a R b) | (c M b) = (a | c) R b // (a R b) | (c M b) = (a | c) R b
// (a M b) | (c M b) = (a | c) M b // (a M b) | (c M b) = (a | c) M b
bool weak = false; bool weak = false;
formula* ftmp = dynamic_cast<binop*>(*i)->second(); formula* ftmp = bo->second();
multop::vec* right = new multop::vec; multop::vec* right = new multop::vec;
for (multop::vec::iterator j = i; j != res->end(); for (multop::vec::iterator j = i; j != res->end();
j++) j++)
@ -745,16 +808,22 @@ namespace spot
continue; continue;
// (a R b) | Gb = a R b. // (a R b) | Gb = a R b.
// (a M b) | Gb = a R b. // (a M b) | Gb = a R b.
unop* uo2 = dynamic_cast<unop*>(*j); if ((*j)->kind() == formula::UnOp)
if (uo2 && uo2->op() == unop::G {
unop* uo2 = static_cast<unop*>(*j);
if (uo2->op() == unop::G
&& uo2->child() == ftmp) && uo2->child() == ftmp)
{ {
(*j)->destroy(); (*j)->destroy();
*j = 0; *j = 0;
weak = true; weak = true;
continue;
} }
binop* bo2 = dynamic_cast<binop*>(*j); }
if (bo2 && (bo2->op() == binop::R if ((*j)->kind() == formula::BinOp)
{
binop* bo2 = static_cast<binop*>(*j);
if ((bo2->op() == binop::R
|| bo2->op() == binop::M) || bo2->op() == binop::M)
&& ftmp == bo2->second()) && ftmp == bo2->second())
{ {
@ -765,6 +834,8 @@ namespace spot
{ {
(*j)->destroy(); (*j)->destroy();
*j = 0; *j = 0;
continue;
}
} }
} }
} }
@ -780,14 +851,13 @@ namespace spot
{ {
tmpOther->push_back((*i)->clone()); tmpOther->push_back((*i)->clone());
} }
break;
} }
else default:
{
tmpOther->push_back((*i)->clone()); tmpOther->push_back((*i)->clone());
} }
(*i)->destroy(); (*i)->destroy();
} }
break; break;
case multop::Concat: case multop::Concat:
case multop::AndNLM: case multop::AndNLM:

View file

@ -221,13 +221,13 @@ namespace spot
{ {
formula* f = mo->nth(i); formula* f = mo->nth(i);
binop* bo = dynamic_cast<binop*>(f); if (f->kind() != formula::BinOp)
if (!bo)
{ {
res->push_back(recurse(f)); res->push_back(recurse(f));
} }
else else
{ {
binop* bo = static_cast<binop*>(f);
switch (bo->op()) switch (bo->op())
{ {
case binop::Xor: case binop::Xor:

View file

@ -179,9 +179,10 @@ namespace spot
} }
/* a < b => a U (b U c) = (b U c) */ /* a < b => a U (b U c) = (b U c) */
/* a < b => a U (b W c) = (b W c) */ /* a < b => a U (b W c) = (b W c) */
if (f2->kind() == formula::BinOp)
{ {
binop* bo = dynamic_cast<binop*>(f2); binop* bo = static_cast<binop*>(f2);
if (bo && (bo->op() == binop::U || bo->op() == binop::W) if ((bo->op() == binop::U || bo->op() == binop::W)
&& syntactic_implication(f1, bo->first())) && syntactic_implication(f1, bo->first()))
{ {
result_ = f2; result_ = f2;
@ -206,22 +207,21 @@ namespace spot
f1->destroy(); f1->destroy();
return; return;
} }
if (f2->kind() == formula::BinOp)
{
/* b < a => a R (b R c) = b R c */ /* b < a => a R (b R c) = b R c */
/* b < a => a R (b M c) = b M c */ /* b < a => a R (b M c) = b M c */
{ binop* bo = static_cast<binop*>(f2);
binop* bo = dynamic_cast<binop*>(f2); if ((bo->op() == binop::R || bo->op() == binop::M)
if (bo && (bo->op() == binop::R || bo->op() == binop::M)
&& syntactic_implication(bo->first(), f1)) && syntactic_implication(bo->first(), f1))
{ {
result_ = f2; result_ = f2;
f1->destroy(); f1->destroy();
return; return;
} }
}
/* a < b => a R (b R c) = a R c */ /* a < b => a R (b R c) = a R c */
{ if (bo->op() == binop::R
binop* bo = dynamic_cast<binop*>(f2);
if (bo && bo->op() == binop::R
&& syntactic_implication(f1, bo->first())) && syntactic_implication(f1, bo->first()))
{ {
result_ = binop::instance(binop::R, f1, result_ = binop::instance(binop::R, f1,
@ -249,9 +249,10 @@ namespace spot
return; return;
} }
/* a < b => a W (b W c) = (b W c) */ /* a < b => a W (b W c) = (b W c) */
if (f2->kind() == formula::BinOp)
{ {
binop* bo = dynamic_cast<binop*>(f2); binop* bo = static_cast<binop*>(f2);
if (bo && bo->op() == binop::W if (bo->op() == binop::W
&& syntactic_implication(f1, bo->first())) && syntactic_implication(f1, bo->first()))
{ {
result_ = f2; result_ = f2;
@ -277,22 +278,21 @@ namespace spot
f2->destroy(); f2->destroy();
return; return;
} }
/* b < a => a M (b M c) = b M c */ if (f2->kind() == formula::BinOp)
{ {
binop* bo = dynamic_cast<binop*>(f2); /* b < a => a M (b M c) = b M c */
if (bo && bo->op() == binop::M binop* bo = static_cast<binop*>(f2);
if (bo->op() == binop::M
&& syntactic_implication(bo->first(), f1)) && syntactic_implication(bo->first(), f1))
{ {
result_ = f2; result_ = f2;
f1->destroy(); f1->destroy();
return; return;
} }
}
/* a < b => a M (b M c) = a M c */ /* a < b => a M (b M c) = a M c */
/* a < b => a M (b R c) = a M c */ /* a < b => a M (b R c) = a M c */
{ if ((bo->op() == binop::M || bo->op() == binop::R)
binop* bo = dynamic_cast<binop*>(f2);
if (bo && (bo->op() == binop::M || bo->op() == binop::R)
&& syntactic_implication(f1, bo->first())) && syntactic_implication(f1, bo->first()))
{ {
result_ = binop::instance(binop::M, f1, result_ = binop::instance(binop::M, f1,

View file

@ -96,8 +96,10 @@ namespace spot
return; return;
case unop::X: case unop::X:
{ {
const unop* op = dynamic_cast<const unop*>(f); if (f->kind() != formula::UnOp)
if (op && op->op() == unop::X) return;
const unop* op = static_cast<const unop*>(f);
if (op->op() == unop::X)
result_ = syntactic_implication(op->child(), f1); result_ = syntactic_implication(op->child(), f1);
} }
return; return;
@ -124,8 +126,6 @@ namespace spot
{ {
const formula* f1 = bo->first(); const formula* f1 = bo->first();
const formula* f2 = bo->second(); const formula* f2 = bo->second();
const binop* fb = dynamic_cast<const binop*>(f);
const unop* fu = dynamic_cast<const unop*>(f);
switch (bo->op()) switch (bo->op())
{ {
case binop::Xor: case binop::Xor:
@ -141,39 +141,55 @@ namespace spot
result_ = true; result_ = true;
return; return;
case binop::R: case binop::R:
if (fb && fb->op() == binop::R) if (f->kind() == formula::BinOp)
if (syntactic_implication(fb->first(), f1) && {
syntactic_implication(fb->second(), f2)) const binop* fb = static_cast<const binop*>(f);
if (fb->op() == binop::R
&& syntactic_implication(fb->first(), f1)
&& syntactic_implication(fb->second(), f2))
{ {
result_ = true; result_ = true;
return; return;
} }
if (fu && fu->op() == unop::G) }
if (f1 == constant::false_instance() && if (f->kind() == formula::UnOp)
syntactic_implication(fu->child(), f2)) {
const unop* fu = static_cast<const unop*>(f);
if (fu->op() == unop::G
&& f1 == constant::false_instance()
&& syntactic_implication(fu->child(), f2))
{ {
result_ = true; result_ = true;
return; return;
} }
}
if (syntactic_implication(f, f1) if (syntactic_implication(f, f1)
&& syntactic_implication(f, f2)) && syntactic_implication(f, f2))
result_ = true; result_ = true;
return; return;
case binop::M: case binop::M:
if (fb && fb->op() == binop::M) if (f->kind() == formula::BinOp)
if (syntactic_implication(fb->first(), f1) && {
syntactic_implication(fb->second(), f2)) const binop* fb = static_cast<const binop*>(f);
if (fb->op() == binop::M
&& syntactic_implication(fb->first(), f1)
&& syntactic_implication(fb->second(), f2))
{ {
result_ = true; result_ = true;
return; return;
} }
if (fu && fu->op() == unop::F) }
if (f2 == constant::true_instance() && if (f->kind() == formula::UnOp)
syntactic_implication(fu->child(), f1)) {
const unop* fu = static_cast<const unop*>(f);
if (fu->op() == unop::F
&& f2 == constant::true_instance()
&& syntactic_implication(fu->child(), f1))
{ {
result_ = true; result_ = true;
return; return;
} }
}
if (syntactic_implication(f, f1) if (syntactic_implication(f, f1)
&& syntactic_implication(f, f2)) && syntactic_implication(f, f2))
result_ = true; result_ = true;
@ -238,8 +254,10 @@ namespace spot
bool bool
special_case(const binop* f2) special_case(const binop* f2)
{ {
const binop* fb = dynamic_cast<const binop*>(f); if (f->kind() != formula::BinOp)
if (fb && fb->op() == f2->op() return false;
const binop* fb = static_cast<const binop*>(f);
if (fb->op() == f2->op()
&& syntactic_implication(f2->first(), fb->first()) && syntactic_implication(f2->first(), fb->first())
&& syntactic_implication(f2->second(), fb->second())) && syntactic_implication(f2->second(), fb->second()))
return true; return true;
@ -249,10 +267,9 @@ namespace spot
bool bool
special_case_check(const formula* f2) special_case_check(const formula* f2)
{ {
const binop* f2b = dynamic_cast<const binop*>(f2); if (f2->kind() != formula::BinOp)
if (!f2b)
return false; return false;
return special_case(f2b); return special_case(static_cast<const binop*>(f2));
} }
int int
@ -307,9 +324,10 @@ namespace spot
result_ = true; result_ = true;
return; return;
case unop::X: case unop::X:
if (f->kind() == formula::UnOp)
{ {
const unop* op = dynamic_cast<const unop*>(f); const unop* op = static_cast<const unop*>(f);
if (op && op->op() == unop::X) if (op->op() == unop::X)
result_ = syntactic_implication(f1, op->child()); result_ = syntactic_implication(f1, op->child());
} }
return; return;
@ -367,8 +385,6 @@ namespace spot
const formula* f1 = bo->first(); const formula* f1 = bo->first();
const formula* f2 = bo->second(); const formula* f2 = bo->second();
const binop* fb = dynamic_cast<const binop*>(f);
const unop* fu = dynamic_cast<const unop*>(f);
switch (bo->op()) switch (bo->op())
{ {
case binop::Xor: case binop::Xor:
@ -380,63 +396,87 @@ namespace spot
return; return;
case binop::U: case binop::U:
/* (a < c) && (c < d) => a U b < c U d */ /* (a < c) && (c < d) => a U b < c U d */
if (fb && fb->op() == binop::U) if (f->kind() == formula::BinOp)
if (syntactic_implication(f1, fb->first()) && {
syntactic_implication(f2, fb->second())) const binop* fb = static_cast<const binop*>(f);
if (fb->op() == binop::U
&& syntactic_implication(f1, fb->first())
&& syntactic_implication(f2, fb->second()))
{ {
result_ = true; result_ = true;
return; return;
} }
if (fu && fu->op() == unop::F) }
if (f1 == constant::true_instance() && if (f->kind() == formula::UnOp)
syntactic_implication(f2, fu->child())) {
const unop* fu = static_cast<const unop*>(f);
if (fu->op() == unop::F
&& f1 == constant::true_instance()
&& syntactic_implication(f2, fu->child()))
{ {
result_ = true; result_ = true;
return; return;
} }
}
if (syntactic_implication(f1, f) if (syntactic_implication(f1, f)
&& syntactic_implication(f2, f)) && syntactic_implication(f2, f))
result_ = true; result_ = true;
return; return;
case binop::W: case binop::W:
/* (a < c) && (c < d) => a W b < c W d */ /* (a < c) && (c < d) => a W b < c W d */
if (fb && fb->op() == binop::W) if (f->kind() == formula::BinOp)
if (syntactic_implication(f1, fb->first()) && {
syntactic_implication(f2, fb->second())) const binop* fb = static_cast<const binop*>(f);
if (fb->op() == binop::W
&& syntactic_implication(f1, fb->first())
&& syntactic_implication(f2, fb->second()))
{ {
result_ = true; result_ = true;
return; return;
} }
if (fu && fu->op() == unop::G) }
if (f2 == constant::false_instance() && if (f->kind() == formula::UnOp)
syntactic_implication(f1, fu->child())) {
const unop* fu = static_cast<const unop*>(f);
if (fu && fu->op() == unop::G
&& f2 == constant::false_instance()
&& syntactic_implication(f1, fu->child()))
{ {
result_ = true; result_ = true;
return; return;
} }
}
if (syntactic_implication(f1, f) if (syntactic_implication(f1, f)
&& syntactic_implication(f2, f)) && syntactic_implication(f2, f))
result_ = true; result_ = true;
return; return;
case binop::R: case binop::R:
if (fu && fu->op() == unop::G) if (f->kind() == formula::UnOp)
if (f1 == constant::false_instance() && {
syntactic_implication(f2, fu->child())) const unop* fu = static_cast<const unop*>(f);
if (fu->op() == unop::G
&& f1 == constant::false_instance()
&& syntactic_implication(f2, fu->child()))
{ {
result_ = true; result_ = true;
return; return;
} }
}
if (syntactic_implication(f2, f)) if (syntactic_implication(f2, f))
result_ = true; result_ = true;
return; return;
case binop::M: case binop::M:
if (fu && fu->op() == unop::F) if (f->kind() == formula::UnOp)
if (f2 == constant::true_instance() && {
syntactic_implication(f1, fu->child())) const unop* fu = static_cast<const unop*>(f);
if (fu->op() == unop::F
&& f2 == constant::true_instance()
&& syntactic_implication(f1, fu->child()))
{ {
result_ = true; result_ = true;
return; return;
} }
}
if (syntactic_implication(f2, f)) if (syntactic_implication(f2, f))
result_ = true; result_ = true;
return; return;

View file

@ -188,7 +188,7 @@ namespace spot
// a[*] is OK, no need to print {a}[*]. // a[*] is OK, no need to print {a}[*].
// However want braces for {!a}[*], the only unary // However want braces for {!a}[*], the only unary
// operator that can be nested with [*]. // operator that can be nested with [*].
bool need_parent = !!dynamic_cast<const unop*>(bo->child()); bool need_parent = (bo->child()->kind() == formula::UnOp);
if (need_parent || full_parent_) if (need_parent || full_parent_)
openp(); openp();
@ -206,7 +206,7 @@ namespace spot
top_level_ = false; top_level_ = false;
// The parser treats F0, F1, G0, G1, X0, and X1 as atomic // The parser treats F0, F1, G0, G1, X0, and X1 as atomic
// propositions. So make sure we output F(0), G(1), etc. // propositions. So make sure we output F(0), G(1), etc.
bool need_parent = !!dynamic_cast<const constant*>(uo->child()); bool need_parent = (uo->child()->kind() == formula::Constant);
bool top_level = top_level_; bool top_level = top_level_;
if (full_parent_) if (full_parent_)
@ -456,7 +456,7 @@ namespace spot
case unop::X: case unop::X:
// The parser treats X0, and X1 as atomic // The parser treats X0, and X1 as atomic
// propositions. So make sure we output X(0) and X(1). // propositions. So make sure we output X(0) and X(1).
need_parent = !!dynamic_cast<const constant*>(uo->child()); need_parent = (uo->child()->kind() == formula::Constant);
if (full_parent_) if (full_parent_)
need_parent = false; need_parent = false;
os_ << "X"; os_ << "X";