3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-08 10:25:18 +00:00
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2018-06-11 13:55:04 -07:00 committed by Arie Gurfinkel
parent 6e61a7c1b2
commit 44a32bc076
3 changed files with 160 additions and 113 deletions

View file

@ -19,12 +19,32 @@ Revision History:
--*/
#include "ast/ast_util.h"
#include "ast/rewriter/bool_rewriter.h"
#include "solver/solver.h"
#include "qe/qe_mbi.h"
namespace qe {
lbool mbi_plugin::check(func_decl_ref_vector const& vars, expr_ref_vector& lits, model_ref& mdl) {
SASSERT(lits.empty());
while (true) {
switch ((*this)(vars, lits, mdl)) {
case mbi_sat:
return l_true;
case mbi_unsat:
if (lits.empty()) return l_false;
block(lits);
break;
case mbi_undef:
return l_undef;
case mbi_augment:
break;
}
}
}
// -------------------------------
// prop_mbi
@ -116,6 +136,7 @@ namespace qe {
blocks.push_back(expr_ref_vector(m));
blocks.push_back(expr_ref_vector(m));
mbi_result last_res = mbi_undef;
bool_rewriter rw(m);
while (true) {
auto* t1 = turn ? &a : &b;
auto* t2 = turn ? &b : &a;
@ -156,10 +177,32 @@ namespace qe {
}
/**
* TBD: also implement the one-sided versions that create clausal interpolants.
* One-sided pogo creates clausal interpolants.
* It creates a set of consequences of b that are inconsistent with a.
*/
lbool interpolator::pogo(mbi_plugin& a, mbi_plugin& b, func_decl_ref_vector const& vars, expr_ref& itp) {
NOT_IMPLEMENTED_YET();
return l_undef;
expr_ref_vector lits(m), itps(m);
while (true) {
model_ref mdl;
lits.reset();
switch (a.check(vars, lits, mdl)) {
case l_true:
switch (b.check(vars, lits, mdl)) {
case l_true:
return l_true;
case l_false:
a.block(lits);
itps.push_back(mk_not(mk_and(lits)));
break;
case l_undef:
return l_undef;
}
case l_false:
itp = mk_and(itps);
return l_false;
case l_undef:
return l_undef;
}
}
}
};

View file

@ -56,6 +56,12 @@ namespace qe {
* \brief Block conjunction of lits from future mbi_augment or mbi_sat.
*/
virtual void block(expr_ref_vector const& lits) = 0;
/**
* \brief perform a full check, consume internal auguments if necessary.
*/
lbool check(func_decl_ref_vector const& vars, expr_ref_vector& lits, model_ref& mdl);
};
class prop_mbi_plugin : public mbi_plugin {

View file

@ -35,27 +35,27 @@ namespace qe {
term* m_next;
// -- eq class size
unsigned m_class_size;
// -- general purpose mark
unsigned m_mark:1;
// -- general purpose second mark
unsigned m_mark2:1;
// -- is an interpreted constant
unsigned m_interpreted:1;
// -- terms that contain this term as a child
ptr_vector<term> m_parents;
// arguments of term.
ptr_vector<term> m_children;
public:
term(expr* v, u_map<term*>& app2term) :
m_expr(v),
m_root(this),
m_root(this),
m_next(this),
m_class_size(1),
m_mark(false),
m_class_size(1),
m_mark(false),
m_mark2(false),
m_interpreted(false) {
if (!is_app()) return;
@ -65,9 +65,9 @@ namespace qe {
m_children.push_back(t);
}
}
~term() {}
class parents {
term const& t;
public:
@ -76,29 +76,29 @@ namespace qe {
ptr_vector<term>::const_iterator begin() const { return t.m_parents.begin(); }
ptr_vector<term>::const_iterator end() const { return t.m_parents.end(); }
};
class children {
term const& t;
term const& t;
public:
children(term const& _t):t(_t) {}
children(term const* _t):t(*_t) {}
ptr_vector<term>::const_iterator begin() const { return t.m_children.begin(); }
ptr_vector<term>::const_iterator end() const { return t.m_children.end(); }
};
// Congruence table hash function is based on
// roots of children and function declaration.
unsigned get_hash() const {
unsigned a, b, c;
a = b = c = get_decl_id();
a = b = c = get_decl_id();
for (term * ch : children(this)) {
a = ch->get_root().get_id();
mix(a, b, c);
}
return c;
}
static bool cg_eq(term const * t1, term const * t2) {
if (t1->get_decl_id() != t2->get_decl_id()) return false;
if (t1->m_children.size() != t2->m_children.size()) return false;
@ -107,16 +107,16 @@ namespace qe {
}
return true;
}
unsigned get_id() const { return m_expr->get_id();}
unsigned get_decl_id() const { return is_app() ? get_app()->get_decl()->get_id() : m_expr->get_id(); }
bool is_marked() const {return m_mark;}
void set_mark(bool v){m_mark = v;}
bool is_marked2() const {return m_mark2;} // NSB: where is this used?
void set_mark2(bool v){m_mark2 = v;} // NSB: where is this used?
bool is_interpreted() const {return m_interpreted;}
bool is_theory() const { return !is_app() || get_app()->get_family_id() != null_family_id; }
void mark_as_interpreted() {m_interpreted=true;}
@ -124,26 +124,26 @@ namespace qe {
bool is_app() const {return ::is_app(m_expr);}
app *get_app() const {return is_app() ? to_app(m_expr) : nullptr;}
unsigned get_num_args() const { return is_app() ? get_app()->get_num_args() : 0; }
term &get_root() const {return *m_root;}
bool is_root() const {return m_root == this;}
void set_root(term &r) {m_root = &r;}
term &get_next() const {return *m_next;}
void add_parent(term* p) { m_parents.push_back(p); }
unsigned get_class_size() const {return m_class_size;}
void merge_eq_class(term &b) {
std::swap(this->m_next, b.m_next);
m_class_size += b.get_class_size();
// -- reset (useful for debugging)
b.m_class_size = 0;
}
// -- make this term the root of its equivalence class
void mk_root() {
if (is_root()) return;
term *curr = this;
do {
if (curr->is_root()) {
@ -158,26 +158,26 @@ namespace qe {
while (curr != this);
}
};
class arith_term_graph_plugin : public term_graph_plugin {
term_graph &m_g;
ast_manager &m;
arith_util m_arith;
public:
arith_term_graph_plugin(term_graph &g) :
term_graph_plugin (g.get_ast_manager().mk_family_id("arith")),
m_g(g), m(g.get_ast_manager()), m_arith(m) {(void)m_g;}
virtual ~arith_term_graph_plugin() {}
bool mk_eq_core (expr *_e1, expr *_e2, expr_ref &res) {
expr *e1, *e2;
e1 = _e1;
e2 = _e2;
if (m_arith.is_zero(e1)) {
std::swap(e1, e2);
}
@ -196,7 +196,7 @@ namespace qe {
res = m.mk_eq(e1, e2);
return true;
}
app* mk_le_zero(expr *arg) {
expr *e1, *e2, *e3;
if (m_arith.is_add(arg, e1, e2)) {
@ -226,7 +226,7 @@ namespace qe {
}
return m_arith.mk_ge(arg, mk_zero());
}
bool mk_le_core (expr *arg1, expr * arg2, expr_ref &result) {
// t <= -1 ==> t < 0 ==> ! (t >= 0)
rational n;
@ -245,13 +245,13 @@ namespace qe {
}
return false;
}
expr * mk_zero () {return m_arith.mk_numeral (rational (0), true);}
bool is_one (expr const * n) const {
rational val;
return m_arith.is_numeral (n, val) && val.is_one ();
}
bool mk_ge_core (expr * arg1, expr * arg2, expr_ref &result) {
// t >= 1 ==> t > 0 ==> ! (t <= 0)
rational n;
@ -270,17 +270,17 @@ namespace qe {
}
return false;
}
expr_ref process_lit (expr *_lit) override {
expr *lit = _lit;
expr *e1, *e2;
// strip negation
bool is_neg = m.is_not(lit);
if (is_neg) {
lit = to_app(to_app(lit)->get_arg(0));
}
expr_ref res(m);
res = lit;
if (m.is_eq (lit, e1, e2)) {
@ -292,12 +292,12 @@ namespace qe {
else if (m_arith.is_ge(lit, e1, e2)) {
mk_ge_core(e1, e2, res);
}
// restore negation
if (is_neg) {
res = mk_not(m, res);
}
return res;
}
};
@ -309,16 +309,16 @@ namespace qe {
term_graph::term_graph(ast_manager &man) : m(man), m_lits(m), m_pinned(m) {
m_plugins.register_plugin (alloc(arith_term_graph_plugin, *this));
}
term_graph::~term_graph() {
reset();
}
static family_id get_family_id(ast_manager &m, expr *lit) {
if (m.is_not(lit, lit))
if (m.is_not(lit, lit))
return get_family_id(m, lit);
expr *a = nullptr, *b = nullptr;
expr *a = nullptr, *b = nullptr;
// deal with equality using sort of range
if (m.is_eq (lit, a, b)) {
return get_sort (a)->get_family_id();
@ -331,10 +331,10 @@ namespace qe {
return null_family_id;
}
}
void term_graph::add_lit(expr *l) {
expr_ref lit(m);
family_id fid = get_family_id (m, l);
term_graph_plugin *pin = m_plugins.get_plugin(fid);
if (pin) {
@ -345,16 +345,16 @@ namespace qe {
m_lits.push_back(lit);
internalize_lit(lit);
}
bool term_graph::is_internalized(expr *a) {
return m_app2term.contains(a->get_id());
}
term* term_graph::get_term(expr *a) {
term *res;
return m_app2term.find (a->get_id(), res) ? res : nullptr;
}
term *term_graph::mk_term(expr *a) {
term * t = alloc(term, a, m_app2term);
if (t->get_num_args() == 0 && m.is_unique_value(a)){
@ -365,8 +365,8 @@ namespace qe {
m_app2term.insert(a->get_id(), t);
return t;
}
term* term_graph::internalize_term(expr *t) {
term* term_graph::internalize_term(expr *t) {
term* res = get_term(t);
if (res) return res;
ptr_buffer<expr> todo;
@ -381,7 +381,7 @@ namespace qe {
unsigned sz = todo.size();
if (is_app(t)) {
for (expr * arg : *::to_app(t)) {
if (!get_term(arg))
if (!get_term(arg))
todo.push_back(arg);
}
}
@ -392,17 +392,17 @@ namespace qe {
SASSERT(res);
return res;
}
void term_graph::internalize_eq(expr *a1, expr* a2) {
SASSERT(m_merge.empty());
merge(internalize_term(a1)->get_root(), internalize_term(a2)->get_root());
merge(*internalize_term(a1), *internalize_term(a2));
merge_flush();
SASSERT(m_merge.empty());
}
void term_graph::internalize_lit(expr* lit) {
expr *e1 = nullptr, *e2 = nullptr;
if (m.is_eq (lit, e1, e2)) {
if (m.is_eq (lit, e1, e2)) {
internalize_eq (e1, e2);
}
else {
@ -422,19 +422,17 @@ namespace qe {
void term_graph::merge(term &t1, term &t2) {
// -- merge might invalidate term2app cache
m_term2app.reset();
m_pinned.reset();
SASSERT(t1.is_root());
SASSERT(t2.is_root());
if (&t1 == &t2) return;
term *a = &t1;
term *b = &t2;
m_pinned.reset();
term *a = &t1.get_root();
term *b = &t2.get_root();
if (a == b) return;
if (a->get_class_size() > b->get_class_size()) {
std::swap(a, b);
}
// Remove parents of it from the cg table.
for (term* p : term::parents(b)) {
if (!p->is_marked()) {
@ -442,15 +440,15 @@ namespace qe {
m_cg_table.erase(p);
}
}
// make 'a' be the root of the equivalence class of 'b'
// make 'a' be the root of the equivalence class of 'b'
b->set_root(*a);
for (term *it = &b->get_next(); it != b; it = &it->get_next()) {
it->set_root(*a);
}
// merge equivalence classes
a->merge_eq_class(*b);
// Insert parents of b's old equilvalence class into the cg table
for (term* p : term::parents(a)) {
if (p->is_marked()) {
@ -462,16 +460,16 @@ namespace qe {
m_merge.push_back(std::make_pair(p, p_old));
}
}
}
}
}
expr* term_graph::mk_app_core (expr *e) {
if (is_app(e)) {
expr_ref_buffer kids(m);
app* a = ::to_app(e);
for (expr * arg : *a) {
kids.push_back (mk_app(arg));
}
}
app* res = m.mk_app(a->get_decl(), a->get_num_args(), kids.c_ptr());
m_pinned.push_back(res);
return res;
@ -483,44 +481,44 @@ namespace qe {
expr_ref term_graph::mk_app(term const &r) {
SASSERT(r.is_root());
if (r.get_num_args() == 0) {
return expr_ref(r.get_expr(), m);
}
expr* res = nullptr;
if (m_term2app.find(r.get_id(), res)) {
return expr_ref(res, m);
}
res = mk_app_core (r.get_app());
m_term2app.insert(r.get_id(), res);
return expr_ref(res, m);
}
expr_ref term_graph::mk_app(expr *a) {
term *t = get_term(a);
if (!t)
if (!t)
return expr_ref(a, m);
else
else
return mk_app(t->get_root());
}
void term_graph::mk_equalities(term const &t, expr_ref_vector &out) {
SASSERT(t.is_root());
expr_ref rep(mk_app(t), m);
for (term *it = &t.get_next(); it != &t; it = &it->get_next()) {
expr* mem = mk_app_core(it->get_app());
out.push_back (m.mk_eq (rep, mem));
}
}
void term_graph::mk_all_equalities(term const &t, expr_ref_vector &out) {
mk_equalities(t, out);
for (term *it = &t.get_next(); it != &t; it = &it->get_next ()) {
expr* a1 = mk_app_core (it->get_app());
for (term *it2 = &it->get_next(); it2 != &t; it2 = &it2->get_next()) {
@ -529,22 +527,22 @@ namespace qe {
}
}
}
void term_graph::reset_marks() {
for (term * t : m_terms) {
t->set_mark(false);
}
}
/// Order of preference for roots of equivalence classes
/// XXX This should be factored out to let clients control the preference
bool term_graph::term_lt(term const &t1, term const &t2) {
// prefer constants over applications
// prefer uninterpreted constants over values
// prefer smaller expressions over larger ones
if (t1.get_num_args() == 0 || t2.get_num_args() == 0) {
if (t1.get_num_args() == t2.get_num_args()) {
if (t1.get_num_args() == t2.get_num_args()) {
// t1.get_num_args() == t2.get_num_args() == 0
if (m.is_value(t1.get_expr()) == m.is_value(t2.get_expr()))
return t1.get_id() < t2.get_id();
@ -552,7 +550,7 @@ namespace qe {
}
return t1.get_num_args() < t2.get_num_args();
}
unsigned sz1 = get_num_exprs(t1.get_expr());
unsigned sz2 = get_num_exprs(t1.get_expr());
return sz1 < sz2;
@ -564,7 +562,7 @@ namespace qe {
it->set_mark(true);
if (term_lt(*it, *r)) { r = it; }
}
// -- if found something better, make it the new root
if (r != &t) {
r->mk_root();
@ -574,12 +572,12 @@ namespace qe {
/// Choose better roots for equivalence classes
void term_graph::pick_roots() {
for (term* t : m_terms) {
if (!t->is_marked() && t->is_root())
if (!t->is_marked() && t->is_root())
pick_root(*t);
}
reset_marks();
}
void term_graph::display(std::ostream &out) {
for (term * t : m_terms) {
out << mk_pp(t->get_expr(), m) << " is root " << t->is_root()
@ -588,33 +586,33 @@ namespace qe {
<< "\n";
}
}
void term_graph::to_lits (expr_ref_vector &lits, bool all_equalities) {
pick_roots();
for (expr * a : m_lits) {
if (is_internalized(a)) {
lits.push_back (::to_app(mk_app(a)));
}
}
for (term * t : m_terms) {
if (!t->is_root())
if (!t->is_root())
continue;
else if (all_equalities)
mk_all_equalities (*t, lits);
else
else if (all_equalities)
mk_all_equalities (*t, lits);
else
mk_equalities(*t, lits);
}
}
expr_ref term_graph::to_app() {
expr_ref_vector lits(m);
to_lits(lits);
return mk_and(lits);
}
void term_graph::reset() {
m_term2app.reset();
m_pinned.reset();
@ -624,7 +622,7 @@ namespace qe {
m_lits.reset();
m_cg_table.reset();
}
expr* term_graph::mk_pure(term& t) {
expr* e = nullptr;
if (m_term2app.find(t.get_id(), e)) return e;
@ -641,7 +639,7 @@ namespace qe {
m_term2app.insert(t.get_id(), result);
return result;
}
expr_ref_vector term_graph::project(func_decl_ref_vector const& decls, bool exclude) {
u_map<bool> _decls;
for (func_decl* f : decls) _decls.insert(f->get_id(), true);
@ -649,27 +647,27 @@ namespace qe {
// use work-list + marking to propagate.
// - produce equalities over represented classes.
// - produce other literals over represented classes
// (walk disequalities in m_lits and represent lhs/rhs over decls or excluding decls)
// (walk disequalities in m_lits and represent lhs/rhs over decls or excluding decls)
expr_ref_vector result(m);
m_term2app.reset();
m_pinned.reset();
obj_hashtable<expr> eqs;
expr_ref eq(m);
ptr_vector<term> worklist;
for (term * t : m_terms) {
worklist.push_back(t);
t->set_mark(true);
t->set_mark(true);
}
while (!worklist.empty()) {
term* t = worklist.back();
worklist.pop_back();
t->set_mark(false);
if (m_term2app.contains(t->get_id()))
if (m_term2app.contains(t->get_id()))
continue;
if (!t->is_theory() && exclude == _decls.contains(t->get_decl_id()))
if (!t->is_theory() && exclude == _decls.contains(t->get_decl_id()))
continue;
term& root = t->get_root();
@ -678,7 +676,7 @@ namespace qe {
if (!pure) continue;
// ensure that the root has a representative
// either by looking up cached version,
// either by looking up cached version,
// computing it for the first time, or
// inheriting pure.
expr* rep = nullptr;
@ -694,7 +692,7 @@ namespace qe {
}
bool update_rep = false;
// Add equations between pure and rep,
// Add equations between pure and rep,
// optionally swap the roles of rep and pure if
// pure makes a better representative.
if (rep != pure) {
@ -709,7 +707,7 @@ namespace qe {
}
}
// update the worklist if this is the first
// update the worklist if this is the first
// representative or pure was swapped into rep.
if (!has_rep || update_rep) {
for (term * p : term::parents(root)) {
@ -726,10 +724,10 @@ namespace qe {
if (!m.is_eq(e) && m_term2app.find(get_term(e)->get_root().get_id(), e)) {
result.push_back(e);
}
}
}
// Here we could also walk equivalence classes that contain interpreted values by sort and
// extract disequalities bewteen non-unique value representatives.
// these disequalities are implied and can be mined using other means, such as
// these disequalities are implied and can be mined using other means, such as
// theory aware core minimization
m_term2app.reset();
m_pinned.reset();