mirror of
https://github.com/Z3Prover/z3
synced 2025-04-23 09:05:31 +00:00
Add (updated and general) solve_for functionality for arithmetic, add congruence_explain to API to retrieve explanation for why two terms are congruent Tweak handling of smt.qi.max_instantations
Add API solve_for(vars). It takes a list of variables and returns a triangular solved form for the variables. Currently for arithmetic. The solved form is a list with elements of the form (var, term, guard). Variables solved in the tail of the list do not occur before in the list. For example it can return a solution [(x, z, True), (y, x + z, True)] because first x was solved to be z, then y was solved to be x + z which is the same as 2z. Add congruent_explain that retuns an explanation for congruent terms. Terms congruent in the final state after calling SimpleSolver().check() can be queried for an explanation, i.e., a list of literals that collectively entail the equality under congruence closure. The literals are asserted in the final state of search. Adjust smt_context cancellation for the smt.qi.max_instantiations parameter. It gets checked when qi-queue elements are consumed. Prior it was checked on insertion time, which didn't allow for processing as many instantations as there were in the queue. Moreover, it would not cancel the solver. So it would keep adding instantations to the queue when it was full / depleted the configuration limit.
This commit is contained in:
parent
e4ab2944fe
commit
87f7a20e14
31 changed files with 428 additions and 117 deletions
|
@ -153,6 +153,11 @@ namespace smt {
|
|||
if (m_context.get_cancel_flag()) {
|
||||
break;
|
||||
}
|
||||
if (m_stats.m_num_instances > m_params.m_qi_max_instances) {
|
||||
m_context.set_reason_unknown("maximum number of quantifier instances was reached");
|
||||
m_context.set_internal_completed();
|
||||
break;
|
||||
}
|
||||
fingerprint * f = curr.m_qb;
|
||||
quantifier * qa = static_cast<quantifier*>(f->get_data());
|
||||
|
||||
|
|
|
@ -51,7 +51,7 @@ namespace smt {
|
|||
cost_evaluator m_evaluator;
|
||||
cached_var_subst m_subst;
|
||||
svector<float> m_vals;
|
||||
double m_eager_cost_threshold;
|
||||
double m_eager_cost_threshold = 0;
|
||||
struct entry {
|
||||
fingerprint * m_qb;
|
||||
float m_cost;
|
||||
|
|
|
@ -104,7 +104,7 @@ namespace smt {
|
|||
*/
|
||||
|
||||
bool context::get_cancel_flag() {
|
||||
if (l_true == m_sls_completed && !m.limit().suspended()) {
|
||||
if (l_true == m_internal_completed && !m.limit().suspended()) {
|
||||
m_last_search_failure = CANCELED;
|
||||
return true;
|
||||
}
|
||||
|
@ -3509,11 +3509,11 @@ namespace smt {
|
|||
display_profile(verbose_stream());
|
||||
if (r == l_true && get_cancel_flag())
|
||||
r = l_undef;
|
||||
if (r == l_undef && m_sls_completed == l_true && has_sls_model()) {
|
||||
if (r == l_undef && m_internal_completed == l_true && has_sls_model()) {
|
||||
m_last_search_failure = OK;
|
||||
r = l_true;
|
||||
}
|
||||
m_sls_completed = l_false;
|
||||
m_internal_completed = l_false;
|
||||
if (r == l_true && gparams::get_value("model_validate") == "true") {
|
||||
recfun::util u(m);
|
||||
if (u.get_rec_funs().empty() && m_proto_model) {
|
||||
|
@ -3753,7 +3753,7 @@ namespace smt {
|
|||
m_phase_default = false;
|
||||
m_case_split_queue ->init_search_eh();
|
||||
m_next_progress_sample = 0;
|
||||
m_sls_completed = l_undef;
|
||||
m_internal_completed = l_undef;
|
||||
if (m.has_type_vars() && !m_theories.get_plugin(poly_family_id))
|
||||
register_plugin(alloc(theory_polymorphism, *this));
|
||||
TRACE("literal_occ", display_literal_num_occs(tout););
|
||||
|
@ -4653,16 +4653,13 @@ namespace smt {
|
|||
if (th == nullptr)
|
||||
return false;
|
||||
return th->get_value(n, value);
|
||||
}
|
||||
|
||||
void context::solve_for(vector<solution>& sol) {
|
||||
for (auto th : m_theories)
|
||||
if (th)
|
||||
th->solve_for(sol);
|
||||
}
|
||||
|
||||
bool context::solve_for(enode * n, expr_ref & term) {
|
||||
sort * s = n->get_sort();
|
||||
family_id fid = s->get_family_id();
|
||||
theory * th = get_theory(fid);
|
||||
if (th == nullptr)
|
||||
return false;
|
||||
return th->solve_for(n, term);
|
||||
}
|
||||
|
||||
bool context::update_model(bool refinalize) {
|
||||
final_check_status fcs = FC_DONE;
|
||||
|
|
|
@ -130,7 +130,7 @@ namespace smt {
|
|||
class parallel* m_par = nullptr;
|
||||
unsigned m_par_index = 0;
|
||||
bool m_internalizing_assertions = false;
|
||||
lbool m_sls_completed = l_undef;
|
||||
lbool m_internal_completed = l_undef;
|
||||
|
||||
|
||||
// -----------------------------------
|
||||
|
@ -291,9 +291,9 @@ namespace smt {
|
|||
|
||||
bool get_cancel_flag();
|
||||
|
||||
void set_sls_completed() {
|
||||
if (m_sls_completed == l_undef)
|
||||
m_sls_completed = l_true;
|
||||
void set_internal_completed() {
|
||||
if (m_internal_completed == l_undef)
|
||||
m_internal_completed = l_true;
|
||||
}
|
||||
|
||||
region & get_region() {
|
||||
|
@ -1377,13 +1377,13 @@ namespace smt {
|
|||
|
||||
// -----------------------------------
|
||||
//
|
||||
// Model checking... (must be improved)
|
||||
// Value extraction and solving
|
||||
//
|
||||
// -----------------------------------
|
||||
public:
|
||||
bool get_value(enode * n, expr_ref & value);
|
||||
|
||||
bool solve_for(enode* n, expr_ref& term);
|
||||
void solve_for(vector<solution>& sol);
|
||||
|
||||
// -----------------------------------
|
||||
//
|
||||
|
|
|
@ -20,6 +20,7 @@ Revision History:
|
|||
#include "smt/smt_context.h"
|
||||
#include "smt/smt_lookahead.h"
|
||||
#include "ast/ast_smt2_pp.h"
|
||||
#include "ast/ast_util.h"
|
||||
#include "smt/params/smt_params_helper.hpp"
|
||||
|
||||
namespace smt {
|
||||
|
@ -213,11 +214,14 @@ namespace smt {
|
|||
return out;
|
||||
}
|
||||
|
||||
bool kernel::solve_for(expr* e, expr_ref& term) {
|
||||
smt::enode* n = m_imp->m_kernel.find_enode(e);
|
||||
if (!n)
|
||||
return false;
|
||||
return m_imp->m_kernel.solve_for(n, term);
|
||||
void kernel::solve_for(vector<solver::solution>& sol) {
|
||||
vector<smt::solution> solution;
|
||||
for (auto const& [v, t, g] : sol)
|
||||
solution.push_back({ v, t, g });
|
||||
m_imp->m_kernel.solve_for(solution);
|
||||
sol.reset();
|
||||
for (auto s : solution)
|
||||
sol.push_back({ s.var, s.term, s.guard });
|
||||
}
|
||||
|
||||
expr* kernel::congruence_root(expr * e) {
|
||||
|
@ -234,6 +238,21 @@ namespace smt {
|
|||
return n->get_next()->get_expr();
|
||||
}
|
||||
|
||||
expr_ref kernel::congruence_explain(expr* a, expr* b) {
|
||||
auto& ctx = m_imp->m_kernel;
|
||||
ast_manager& m = ctx.get_manager();
|
||||
smt::enode* n1 = ctx.find_enode(a);
|
||||
smt::enode* n2 = ctx.find_enode(b);
|
||||
if (!n1 || !n2 || n1->get_root() != n2->get_root())
|
||||
return expr_ref(m.mk_eq(a, b), m);
|
||||
literal_vector lits;
|
||||
ctx.get_cr().eq2literals(n1, n2, lits);
|
||||
expr_ref_vector es(m);
|
||||
for (auto lit : lits)
|
||||
es.push_back(ctx.literal2expr(lit));
|
||||
return mk_and(es);
|
||||
}
|
||||
|
||||
void kernel::collect_statistics(::statistics & st) const {
|
||||
m_imp->m_kernel.collect_statistics(st);
|
||||
}
|
||||
|
|
|
@ -246,7 +246,9 @@ namespace smt {
|
|||
|
||||
expr* congruence_root(expr* e);
|
||||
|
||||
bool solve_for(expr* e, expr_ref& term);
|
||||
expr_ref congruence_explain(expr* a, expr* b);
|
||||
|
||||
void solve_for(vector<solver::solution>& s);
|
||||
|
||||
/**
|
||||
\brief retrieve depth of variables from decision stack.
|
||||
|
|
|
@ -133,7 +133,7 @@ namespace smt {
|
|||
q::quantifier_stat_gen m_qstat_gen;
|
||||
ptr_vector<quantifier> m_quantifiers;
|
||||
scoped_ptr<quantifier_manager_plugin> m_plugin;
|
||||
unsigned m_num_instances;
|
||||
unsigned m_num_instances = 0;
|
||||
|
||||
imp(quantifier_manager & wrapper, context & ctx, smt_params & p, quantifier_manager_plugin * plugin):
|
||||
m_wrapper(wrapper),
|
||||
|
@ -142,7 +142,6 @@ namespace smt {
|
|||
m_qi_queue(m_wrapper, ctx, p),
|
||||
m_qstat_gen(ctx.get_manager(), ctx.get_region()),
|
||||
m_plugin(plugin) {
|
||||
m_num_instances = 0;
|
||||
m_qi_queue.setup();
|
||||
}
|
||||
|
||||
|
@ -297,9 +296,7 @@ namespace smt {
|
|||
vector<std::tuple<enode *, enode *>> & used_enodes) {
|
||||
|
||||
max_generation = std::max(max_generation, get_generation(q));
|
||||
if (m_num_instances > m_params.m_qi_max_instances) {
|
||||
return false;
|
||||
}
|
||||
|
||||
get_stat(q)->update_max_generation(max_generation);
|
||||
fingerprint * f = m_context.add_fingerprint(q, q->get_id(), num_bindings, bindings, def);
|
||||
if (f) {
|
||||
|
|
|
@ -337,7 +337,8 @@ namespace {
|
|||
|
||||
expr* congruence_next(expr* e) override { return m_context.congruence_next(e); }
|
||||
expr* congruence_root(expr* e) override { return m_context.congruence_root(e); }
|
||||
bool solve_for(expr* e, expr_ref& term) override { return m_context.solve_for(e, term); }
|
||||
expr_ref congruence_explain(expr* a, expr* b) override { return m_context.congruence_explain(a, b); }
|
||||
void solve_for(vector<solver::solution>& s) override { m_context.solve_for(s); }
|
||||
|
||||
|
||||
expr_ref_vector cube(expr_ref_vector& vars, unsigned cutoff) override {
|
||||
|
|
|
@ -29,6 +29,12 @@ namespace smt {
|
|||
class model_generator;
|
||||
class model_value_proc;
|
||||
|
||||
struct solution {
|
||||
expr* var;
|
||||
expr_ref term;
|
||||
expr_ref guard;
|
||||
};
|
||||
|
||||
class theory {
|
||||
protected:
|
||||
theory_id m_id;
|
||||
|
@ -605,7 +611,7 @@ namespace smt {
|
|||
|
||||
virtual char const * get_name() const { return "unknown"; }
|
||||
|
||||
virtual bool solve_for(enode* n, expr_ref& r) { return false; }
|
||||
virtual void solve_for(vector<solution>& s) {}
|
||||
|
||||
// -----------------------------------
|
||||
//
|
||||
|
|
|
@ -3169,7 +3169,7 @@ public:
|
|||
typedef std::pair<lp::constraint_index, rational> constraint_bound;
|
||||
vector<constraint_bound> m_lower_terms;
|
||||
vector<constraint_bound> m_upper_terms;
|
||||
|
||||
|
||||
void propagate_eqs(lp::lpvar t, lp::constraint_index ci1, lp::lconstraint_kind k, api_bound& b, rational const& value) {
|
||||
u_dependency* ci2 = nullptr;
|
||||
auto pair = [&]() { return lp().dep_manager().mk_join(lp().dep_manager().mk_leaf(ci1), ci2); };
|
||||
|
@ -3392,9 +3392,8 @@ public:
|
|||
}
|
||||
|
||||
void set_evidence(lp::constraint_index idx, literal_vector& core, svector<enode_pair>& eqs) {
|
||||
if (idx == UINT_MAX) {
|
||||
return;
|
||||
}
|
||||
if (idx == UINT_MAX)
|
||||
return;
|
||||
switch (m_constraint_sources[idx]) {
|
||||
case inequality_source: {
|
||||
literal lit = m_inequalities[idx];
|
||||
|
@ -3629,33 +3628,116 @@ public:
|
|||
return lp().has_upper_bound(vi, dep, val, is_strict);
|
||||
}
|
||||
|
||||
bool solve_for(enode* n, expr_ref& term) {
|
||||
theory_var v = n->get_th_var(get_id());
|
||||
if (!is_registered_var(v))
|
||||
return false;
|
||||
lpvar vi = get_lpvar(v);
|
||||
lp::lar_term t;
|
||||
rational coeff;
|
||||
if (!lp().solve_for(vi, t, coeff))
|
||||
return false;
|
||||
rational lc(1);
|
||||
if (is_int(v)) {
|
||||
lc = denominator(coeff);
|
||||
for (auto const& cv : t)
|
||||
lc = lcm(denominator(cv.coeff()), lc);
|
||||
if (lc != 1) {
|
||||
coeff *= lc;
|
||||
t *= lc;
|
||||
}
|
||||
}
|
||||
term = mk_term(t, is_int(v));
|
||||
if (coeff != 0)
|
||||
term = a.mk_add(a.mk_numeral(coeff, is_int(v)), term);
|
||||
if (lc != 1)
|
||||
term = a.mk_idiv(term, a.mk_numeral(lc, true));
|
||||
return true;
|
||||
void solve_fixed(enode* n, lpvar j, expr_ref& term, expr_ref& guard) {
|
||||
term = a.mk_numeral(lp().get_value(j), a.is_int(n->get_expr()));
|
||||
reset_evidence();
|
||||
add_explain(j);
|
||||
guard = extract_explain();
|
||||
}
|
||||
|
||||
void add_explain(unsigned j) {
|
||||
auto d = lp().get_bound_constraint_witnesses_for_column(j);
|
||||
set_evidence(d, m_core, m_eqs);
|
||||
}
|
||||
|
||||
expr_ref extract_explain() {
|
||||
expr_ref_vector es(m);
|
||||
for (auto [l, r] : m_eqs)
|
||||
es.push_back(a.mk_eq(l->get_expr(), r->get_expr()));
|
||||
for (auto l : m_core)
|
||||
es.push_back(ctx().literal2expr(l));
|
||||
// remove duplicats from es:
|
||||
std::stable_sort(es.data(), es.data() + es.size());
|
||||
unsigned j = 0;
|
||||
for (unsigned i = 0; i < es.size(); ++i) {
|
||||
if (i > 0 && es.get(i) == es.get(i - 1))
|
||||
continue;
|
||||
es[j++] = es.get(i);
|
||||
}
|
||||
es.shrink(j);
|
||||
return mk_and(es);
|
||||
}
|
||||
|
||||
void solve_term(enode* n, lp::lar_term & lt, expr_ref& term, expr_ref& guard) {
|
||||
bool is_int = a.is_int(n->get_expr());
|
||||
bool all_int = is_int;
|
||||
lp::lar_term t;
|
||||
rational coeff(0);
|
||||
expr_ref_vector guards(m);
|
||||
reset_evidence();
|
||||
for (auto const& cv : lt) {
|
||||
if (lp().column_is_fixed(cv.j())) {
|
||||
coeff += lp().get_value(cv.j()) * cv.coeff();
|
||||
add_explain(cv.j());
|
||||
}
|
||||
else
|
||||
t.add_monomial(cv.coeff(), cv.j());
|
||||
}
|
||||
guards.push_back(extract_explain());
|
||||
rational lc = denominator(coeff);
|
||||
for (auto const& cv : t) {
|
||||
lc = lcm(denominator(cv.coeff()), lc);
|
||||
all_int &= lp().column_is_int(cv.j());
|
||||
}
|
||||
if (lc != 1)
|
||||
t *= lc, coeff *= lc;
|
||||
term = mk_term(t, is_int);
|
||||
if (coeff != 0)
|
||||
term = a.mk_add(term, a.mk_numeral(coeff, is_int));
|
||||
|
||||
if (lc == 1) {
|
||||
guard = mk_and(guards);
|
||||
return;
|
||||
}
|
||||
expr_ref lce(a.mk_numeral(lc, true), m);
|
||||
if (all_int)
|
||||
guards.push_back(m.mk_eq(a.mk_mod(term, lce), a.mk_int(0)));
|
||||
else if (is_int)
|
||||
guards.push_back(a.mk_is_int(a.mk_div(term, lce)));
|
||||
term = a.mk_idiv(term, lce);
|
||||
guard = mk_and(guards);
|
||||
}
|
||||
|
||||
void solve_for(vector<solution>& solutions) {
|
||||
unsigned_vector vars;
|
||||
unsigned j = 0;
|
||||
for (auto [e, t, g] : solutions) {
|
||||
auto n = get_enode(e);
|
||||
if (!n) {
|
||||
solutions[j++] = { e, t, g };
|
||||
continue;
|
||||
}
|
||||
|
||||
theory_var v = n->get_th_var(get_id());
|
||||
if (!is_registered_var(v))
|
||||
solutions[j++] = { e, t, g };
|
||||
else
|
||||
vars.push_back(get_lpvar(v));
|
||||
}
|
||||
solutions.shrink(j);
|
||||
|
||||
expr_ref term(m), guard(m);
|
||||
vector<lp::lar_solver::solution> sols;
|
||||
lp().solve_for(vars, sols);
|
||||
uint_set seen;
|
||||
for (auto& s : sols) {
|
||||
auto n = get_enode(lp().local_to_external(s.j));
|
||||
if (lp().column_is_fixed(s.j))
|
||||
solve_fixed(n, s.j, term, guard);
|
||||
else
|
||||
solve_term(n, s.t, term, guard);
|
||||
solutions.push_back({ n->get_expr(), term, guard});
|
||||
seen.insert(s.j);
|
||||
}
|
||||
for (auto j : vars) {
|
||||
if (seen.contains(j) || !lp().column_is_fixed(j))
|
||||
continue;
|
||||
auto n = get_enode(lp().local_to_external(j));
|
||||
solve_fixed(n, j, term, guard);
|
||||
solutions.push_back({ n->get_expr(), term, guard });
|
||||
}
|
||||
}
|
||||
|
||||
bool get_upper(enode* n, expr_ref& r) {
|
||||
bool is_strict;
|
||||
rational val;
|
||||
|
@ -4166,8 +4248,9 @@ bool theory_lra::get_lower(enode* n, rational& r, bool& is_strict) {
|
|||
bool theory_lra::get_upper(enode* n, rational& r, bool& is_strict) {
|
||||
return m_imp->get_upper(n, r, is_strict);
|
||||
}
|
||||
bool theory_lra::solve_for(enode* n, expr_ref& r) {
|
||||
return m_imp->solve_for(n, r);
|
||||
|
||||
void theory_lra::solve_for(vector<solution>& sol) {
|
||||
m_imp->solve_for(sol);
|
||||
}
|
||||
|
||||
void theory_lra::display(std::ostream & out) const {
|
||||
|
|
|
@ -93,7 +93,7 @@ namespace smt {
|
|||
bool get_upper(enode* n, expr_ref& r);
|
||||
bool get_lower(enode* n, rational& r, bool& is_strict);
|
||||
bool get_upper(enode* n, rational& r, bool& is_strict);
|
||||
bool solve_for(enode* n, expr_ref& r) override;
|
||||
void solve_for(vector<solution>& s) override;
|
||||
|
||||
void display(std::ostream & out) const override;
|
||||
|
||||
|
|
|
@ -58,7 +58,7 @@ namespace smt {
|
|||
}
|
||||
|
||||
void theory_sls::set_finished() {
|
||||
ctx.set_sls_completed();
|
||||
ctx.set_internal_completed();
|
||||
}
|
||||
|
||||
bool theory_sls::get_smt_value(expr* v, expr_ref& value) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue