3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-07-18 02:16:40 +00:00

redo representative algorithm

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2018-06-10 10:59:09 -07:00 committed by Arie Gurfinkel
parent ad153592a2
commit 0d71d85069

View file

@ -373,7 +373,7 @@ namespace qe {
ptr_buffer<expr> todo; ptr_buffer<expr> todo;
todo.push_back(t); todo.push_back(t);
while (!todo.empty()) { while (!todo.empty()) {
t = todo.back(); t = todo.back();
res = get_term(t); res = get_term(t);
if (res) { if (res) {
todo.pop_back(); todo.pop_back();
@ -659,78 +659,70 @@ expr_ref_vector term_graph::project(func_decl_ref_vector const& decls, bool excl
m_term2app.reset(); m_term2app.reset();
m_pinned.reset(); m_pinned.reset();
obj_hashtable<expr> roots; obj_hashtable<expr> eqs;
expr_ref eq(m);
ptr_vector<term> worklist; ptr_vector<term> worklist;
for (term * t : m_terms) { for (term * t : m_terms) {
if (t->is_root()) { worklist.push_back(t);
worklist.push_back(t); t->set_mark(true);
t->set_mark(true);
}
} }
while (!worklist.empty()) { while (!worklist.empty()) {
term* t = worklist.back(); term* t = worklist.back();
worklist.pop_back(); worklist.pop_back();
SASSERT(t->is_root());
t->set_mark(false); t->set_mark(false);
if (m_term2app.contains(t->get_id())) continue; if (m_term2app.contains(t->get_id()))
term* r = t; continue;
roots.reset(); if (!t->is_theory() && exclude == _decls.contains(t->get_decl_id()))
expr_ref rep(m), other(m); continue;
// walk the equivalence class of t to produce
// a representative. term& root = t->get_root();
do { bool has_rep = m_term2app.contains(root.get_id());
// if exclude = true, but t in decls, then skip expr* pure = mk_pure(*t);
// if exclude = false, but t not in decls, then skip if (!pure) continue;
if (!r->is_theory() && exclude == _decls.contains(r->get_decl_id())) {
r = &r->get_next(); // ensure that the root has a representative
continue; // either by looking up cached version,
} // computing it for the first time, or
other = mk_pure(*r); // inheriting pure.
if (other) { expr* rep = nullptr;
if (!rep) { if (root.is_theory() || exclude != _decls.contains(root.get_decl_id())) {
rep = other; rep = mk_pure(root);
roots.insert(other);
}
else if (!roots.contains(other)) {
roots.insert(other);
result.push_back(m.mk_eq(rep, other));
// give preference to non-values as roots.
if (m.is_unique_value(rep)) {
std::swap(other, rep);
}
}
}
r = &r->get_next();
} }
while (r != t); else if (has_rep) {
rep = m_term2app.find(root.get_id());
}
else {
rep = pure;
m_term2app.insert(root.get_id(), pure);
}
bool update_rep = false;
if (rep) { // Add equations between pure and rep,
// update the representative of t to the preferred one. // optionally swap the roles of rep and pure if
// used by mk_pure to determine representative of child. // pure makes a better representative.
m_term2app.insert(t->get_id(), rep); if (rep != pure) {
#if 1 if (m.is_unique_value(rep) && !m.is_unique_value(pure)) {
// The root should contain all parents relative to the equivalence class. m_term2app.insert(root.get_id(), pure);
// To ensure this, merge uses add_parent on the chosen root with respect to the old root. update_rep = true;
// To enable adding terms after merge the constructor for terms also has to ensure }
// that new terms are added as parents of the roots of their children. eq = m.mk_eq(rep, pure);
if (!eqs.contains(eq)) {
eqs.insert(eq);
result.push_back(eq);
}
}
for (term * p : term::parents(t)) { // update the worklist if this is the first
p = &p->get_root(); // representative or pure was swapped into rep.
if (!has_rep || update_rep) {
for (term * p : term::parents(root)) {
if (update_rep) m_term2app.remove(p->get_id());
if (!p->is_marked()) { if (!p->is_marked()) {
p->set_mark(true); p->set_mark(true);
worklist.push_back(p); worklist.push_back(p);
} }
} }
#else
r = t;
do {
for (term * p : term::parents(r)) {
worklist.push_back(&p->get_root());
}
r = &r->get_next();
}
while (r != t);
#endif
} }
} }
// walk other predicates than equalities // walk other predicates than equalities