3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-24 01:25:31 +00:00
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2018-10-18 10:01:32 -07:00
parent 2f5f546990
commit d22a0d04ed
6 changed files with 144 additions and 126 deletions

View file

@ -859,6 +859,10 @@ namespace smt {
void mk_th_axiom(theory_id tid, literal l1, literal l2, literal l3, unsigned num_params = 0, parameter * params = nullptr);
void mk_th_axiom(theory_id tid, literal_vector const& ls, unsigned num_params = 0, parameter * params = nullptr) {
mk_th_axiom(tid, ls.size(), ls.c_ptr(), num_params, params);
}
/*
* Provide a hint to the core solver that the specified literals form a "theory case split".
* The core solver will enforce the condition that exactly one of these literals can be

View file

@ -29,6 +29,7 @@ namespace smt {
theory_recfun::theory_recfun(ast_manager & m)
: theory(m.mk_family_id("recfun")),
m(m),
m_plugin(*reinterpret_cast<recfun_decl_plugin*>(m.get_plugin(get_family_id()))),
m_util(m_plugin.u()),
m_trail(*this),
@ -42,8 +43,8 @@ namespace smt {
theory_recfun::~theory_recfun() {
reset_queues();
for (auto & kv : m_guards) {
m().dec_ref(kv.m_key);
for (expr* g : m_guards) {
m.dec_ref(g);
}
m_guards.reset();
}
@ -61,28 +62,26 @@ namespace smt {
}
bool theory_recfun::internalize_atom(app * atom, bool gate_ctx) {
context & ctx = get_context();
for (expr * arg : *atom) {
ctx.internalize(arg, false);
ctx().internalize(arg, false);
}
if (!ctx.e_internalized(atom)) {
ctx.mk_enode(atom, false, true, false);
if (!ctx().e_internalized(atom)) {
ctx().mk_enode(atom, false, true, false);
}
if (!ctx.b_internalized(atom)) {
bool_var v = ctx.mk_bool_var(atom);
ctx.set_var_theory(v, get_id());
if (!ctx().b_internalized(atom)) {
bool_var v = ctx().mk_bool_var(atom);
ctx().set_var_theory(v, get_id());
}
return true;
}
bool theory_recfun::internalize_term(app * term) {
context & ctx = get_context();
for (expr* e : *term) {
ctx.internalize(e, false);
ctx().internalize(e, false);
}
// the internalization of the arguments may have triggered the internalization of term.
if (!ctx.e_internalized(term)) {
ctx.mk_enode(term, false, false, true);
if (!ctx().e_internalized(term)) {
ctx().mk_enode(term, false, false, true);
}
return true;
}
@ -108,7 +107,7 @@ namespace smt {
void theory_recfun::relevant_eh(app * n) {
SASSERT(ctx().relevancy());
if (u().is_defined(n)) {
TRACEFN("relevant_eh: (defined) " << mk_pp(n, m()));
TRACEFN("relevant_eh: (defined) " << mk_pp(n, m));
case_expansion e(u(), n);
push_case_expand(std::move(e));
}
@ -143,7 +142,7 @@ namespace smt {
for (literal_vector & c : m_q_clauses) {
TRACEFN("add axiom " << pp_lits(ctx(), c));
ctx().mk_th_axiom(get_id(), c.size(), c.c_ptr());
ctx().mk_th_axiom(get_id(), c);
}
m_q_clauses.clear();
@ -176,8 +175,8 @@ namespace smt {
c.push_back(~mk_literal(dlimit));
SASSERT(ctx().get_assignment(c.back()) == l_true);
}
for (auto& kv : m_guards) {
c.push_back(~ mk_literal(kv.m_key));
for (expr * g : m_guards) {
c.push_back(~ mk_literal(g));
}
TRACEFN("max-depth limit: add clause " << pp_lits(ctx(), c));
SASSERT(std::all_of(c.begin(), c.end(), [&](literal & l) { return ctx().get_assignment(l) == l_false; })); // conflict
@ -185,18 +184,21 @@ namespace smt {
m_q_clauses.push_back(std::move(c));
}
// if `is_true` and `v = C_f_i(t1...tn)`, then body-expand i-th case of `f(t1...tn)`
/**
* if `is_true` and `v = C_f_i(t1...tn)`,
* then body-expand i-th case of `f(t1...tn)`
*/
void theory_recfun::assign_eh(bool_var v, bool is_true) {
expr* e = get_context().bool_var2expr(v);
expr* e = ctx().bool_var2expr(v);
if (!is_true || !is_app(e)) return;
app* a = to_app(e);
if (u().is_case_pred(a)) {
TRACEFN("assign_case_pred_true "<< mk_pp(e,m()));
TRACEFN("assign_case_pred_true "<< mk_pp(e,m));
// add to set of local assumptions, for depth-limit purpose
SASSERT(!m_guards.contains(e));
m_guards.insert(e, empty());
m().inc_ref(e);
insert_ref_map<theory_recfun,guard_set,ast_manager,expr*> trail_elt(m(), m_guards, e);
m_guards.insert(e);
m.inc_ref(e);
insert_ref_map<theory_recfun,guard_set,ast_manager,expr*> trail_elt(m, m_guards, e);
m_trail.push(trail_elt);
if (m_guards.size() > get_max_depth()) {
@ -215,18 +217,17 @@ namespace smt {
expr_ref theory_recfun::apply_args(recfun::vars const & vars,
ptr_vector<expr> const & args,
expr * e) {
// check that var order is standard
SASSERT(vars.size() == 0 || vars[vars.size()-1]->get_idx() == 0);
var_subst subst(m(), true);
expr_ref new_body(m());
SASSERT(is_standard_order(vars));
var_subst subst(m, true);
expr_ref new_body(m);
new_body = subst(e, args.size(), args.c_ptr());
ctx().get_rewriter()(new_body); // simplify
return new_body;
}
app_ref theory_recfun::apply_pred(recfun::case_pred const & p,
ptr_vector<expr> const & args) {
return app_ref(u().mk_case_pred(p, args), m());
ptr_vector<expr> const & args) {
return app_ref(u().mk_case_pred(p, args), m);
}
literal theory_recfun::mk_literal(expr* e) {
@ -242,47 +243,54 @@ namespace smt {
return lit;
}
/**
* For functions f(args) that are given as macros f(vs) = rhs
*
* 1. substitute `e.args` for `vs` into the macro rhs
* 2. add unit clause `f(args) = rhs`
*/
void theory_recfun::assert_macro_axiom(case_expansion & e) {
TRACEFN("assert_macro_axiom " << pp_case_expansion(e, m()));
SASSERT(e.m_def->is_fun_macro());
auto & vars = e.m_def->get_vars();
expr_ref lhs(e.m_lhs, m());
// substitute `e.args` into the macro RHS
expr_ref rhs(apply_args(vars, e.m_args, e.m_def->get_macro_rhs()), m());
TRACEFN("macro expansion yields" << mk_pp(rhs,m()));
// add unit clause `lhs = rhs`
expr_ref lhs(e.m_lhs, m);
expr_ref rhs(apply_args(vars, e.m_args, e.m_def->get_macro_rhs()), m);
literal lit = mk_eq_lit(lhs, rhs);
TRACEFN("assert_macro_axiom: " << pp_lit(ctx(), lit));
ctx().mk_th_axiom(get_id(), 1, &lit);
TRACEFN("case expansion " << pp_case_expansion(e, m) << "\n" <<
"macro expansion yields " << mk_pp(rhs,m) << "\n" <<
"literal " << pp_lit(ctx(), lit));
}
/**
* Add case axioms for every case expansion path.
*
* assert `p(args) <=> And(guards)` (with CNF on the fly)
*
* also body-expand paths that do not depend on any defined fun
*/
void theory_recfun::assert_case_axioms(case_expansion & e) {
TRACEFN("assert_case_axioms "<< pp_case_expansion(e,m())
TRACEFN("assert_case_axioms "<< pp_case_expansion(e,m)
<< " with " << e.m_def->get_cases().size() << " cases");
SASSERT(e.m_def->is_fun_defined());
// add case-axioms for all case-paths
auto & vars = e.m_def->get_vars();
for (recfun::case_def const & c : e.m_def->get_cases()) {
// applied predicate to `args`
literal_vector guards;
for (auto & g : c.get_guards()) {
expr_ref guard = apply_args(vars, e.m_args, g);
guards.push_back(~mk_literal(guard));
}
app_ref pred_applied = apply_pred(c.get_pred(), e.m_args);
SASSERT(u().owns_app(pred_applied));
literal concl = mk_literal(pred_applied);
// assert `p(args) <=> And(guards)` (with CNF on the fly)
for (literal g : guards) {
literal c[2] = {~ concl, ~g};
literal_vector guards;
guards.push_back(concl);
for (auto & g : c.get_guards()) {
expr_ref ga = apply_args(vars, e.m_args, g);
literal guard = mk_literal(ga);
guards.push_back(~guard);
literal c[2] = {~concl, guard};
ctx().mk_th_axiom(get_id(), 2, c);
}
guards.push_back(concl);
ctx().mk_th_axiom(get_id(), guards.size(), guards.c_ptr());
ctx().mk_th_axiom(get_id(), guards);
// also body-expand paths that do not depend on any defined fun
if (c.is_immediate()) {
body_expansion be(c, e.m_args);
assert_body_axiom(be);
@ -290,20 +298,21 @@ namespace smt {
}
}
/**
* For a guarded definition guards => f(vars) = rhs
* and occurrence f(args)
*
* substitute `args` for `vars` in guards, and rhs
* add axiom guards[args/vars] => f(args) = rhs[args/vars]
*
*/
void theory_recfun::assert_body_axiom(body_expansion & e) {
TRACEFN("assert_body_axioms "<< pp_body_expansion(e,m()));
recfun::def & d = *e.m_cdef->get_def();
auto & vars = d.get_vars();
auto & args = e.m_args;
// check that var order is standard
SASSERT(vars.size() == 0 || vars[vars.size()-1]->get_idx() == 0);
expr_ref lhs(u().mk_fun_defined(d, args), m());
// substitute `e.args` into the RHS of this particular case
SASSERT(is_standard_order(vars));
expr_ref lhs(u().mk_fun_defined(d, args), m);
expr_ref rhs = apply_args(vars, args, e.m_cdef->get_rhs());
// substitute `e.args` into the guard of this particular case, to make
// the `condition` part of the clause `conds => lhs=rhs`
// now build the axiom `conds => lhs = rhs`
literal_vector clause;
for (auto & g : e.m_cdef->get_guards()) {
@ -311,8 +320,9 @@ namespace smt {
clause.push_back(~mk_literal(guard));
}
clause.push_back(mk_eq_lit(lhs, rhs));
TRACEFN("assert_body_axiom " << pp_lits(ctx(), clause));
ctx().mk_th_axiom(get_id(), clause.size(), clause.c_ptr());
ctx().mk_th_axiom(get_id(), clause);
TRACEFN("body " << pp_body_expansion(e,m));
TRACEFN("clause " << pp_lits(ctx(), clause));
}
final_check_status theory_recfun::final_check_eh() {
@ -321,15 +331,14 @@ namespace smt {
void theory_recfun::add_theory_assumptions(expr_ref_vector & assumptions) {
app_ref dlimit = m_util.mk_depth_limit_pred(get_max_depth());
TRACEFN("add_theory_assumption " << mk_pp(dlimit.get(), m()));
TRACEFN("add_theory_assumption " << mk_pp(dlimit.get(), m));
assumptions.push_back(dlimit);
}
// if `dlimit` occurs in unsat core, return "unknown"
lbool theory_recfun::validate_unsat_core(expr_ref_vector & unsat_core) {
for (auto & e : unsat_core) {
if (is_app(e) && m_util.is_depth_limit(to_app(e)))
if (u().is_depth_limit(e))
return l_undef;
}
return l_false;

View file

@ -33,7 +33,7 @@ namespace smt {
stats() { reset(); }
};
// one case-expansion of `f(t1tn)`
// one case-expansion of `f(t1...tn)`
struct case_expansion {
expr * m_lhs; // the term to expand
recfun_def * m_def;
@ -64,18 +64,16 @@ namespace smt {
friend std::ostream& operator<<(std::ostream&, pp_case_expansion const &);
// one body-expansion of `f(t1…tn)` using a `C_f_i(t1…tn)`
// one body-expansion of `f(t1...tn)` using a `C_f_i(t1...tn)`
struct body_expansion {
recfun_case_def const * m_cdef;
ptr_vector<expr> m_args;
body_expansion(recfun_util& u, app * n) : m_cdef(0), m_args() {
SASSERT(u.is_case_pred(n));
func_decl * d = n->get_decl();
const symbol& name = d->get_name();
m_cdef = &u.get_case_def(name);
for (unsigned i = 0; i < n->get_num_args(); ++i)
m_args.push_back(n->get_arg(i));
m_cdef = &u.get_case_def(n->get_name());
for (expr * arg : *n)
m_args.push_back(arg);
}
body_expansion(recfun_case_def const & d, ptr_vector<expr> & args) : m_cdef(&d), m_args(args) {}
body_expansion(body_expansion const & from): m_cdef(from.m_cdef), m_args(from.m_args) {}
@ -90,11 +88,11 @@ namespace smt {
friend std::ostream& operator<<(std::ostream&, pp_body_expansion const &);
struct empty{};
typedef trail_stack<theory_recfun> th_trail_stack;
typedef obj_map<expr, empty> guard_set;
typedef obj_hashtable<expr> guard_set;
ast_manager& m;
recfun_decl_plugin& m_plugin;
recfun_util& m_util;
stats m_stats;
@ -107,7 +105,6 @@ namespace smt {
vector<literal_vector> m_q_clauses;
recfun_util & u() const { return m_util; }
ast_manager & m() { return get_manager(); }
bool is_defined(app * f) const { return u().is_defined(f); }
bool is_case_pred(app * f) const { return u().is_case_pred(f); }
@ -123,6 +120,7 @@ namespace smt {
void max_depth_conflict(void);
literal mk_literal(expr* e);
literal mk_eq_lit(expr* l, expr* r);
bool is_standard_order(recfun::vars const& vars) const { return vars.size() == 0 || vars[vars.size()-1]->get_idx() == 0; }
protected:
void push_case_expand(case_expansion&& e) { m_q_case_expand.push_back(e); }
void push_body_expand(body_expansion&& e) { m_q_body_expand.push_back(e); }