3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-24 01:25:31 +00:00

create simplifier_solver wrapper to supply simplifier layer

move sat_smt_preprocess to solver
fix bugs in model_reconstruction_trail for dependency replay

This is a preparatory step for exposing pre-processing as tactics.
This commit is contained in:
Nikolaj Bjorner 2023-01-30 16:12:25 -08:00
parent 304b316314
commit dd0decfe5d
11 changed files with 474 additions and 248 deletions

View file

@ -1,7 +1,6 @@
z3_add_component(sat_solver
SOURCES
inc_sat_solver.cpp
sat_smt_preprocess.cpp
sat_smt_solver.cpp
COMPONENT_DEPENDENCIES
aig_tactic

View file

@ -1,106 +0,0 @@
/*++
Copyright (c) 2022 Microsoft Corporation
Module Name:
sat_smt_preprocess.cpp
Abstract:
SAT pre-process
Author:
Nikolaj Bjorner (nbjorner) 2022-11-28
--*/
#include "ast/rewriter/rewriter_def.h"
#include "ast/simplifiers/bit_blaster.h"
#include "ast/simplifiers/max_bv_sharing.h"
#include "ast/simplifiers/card2bv.h"
#include "ast/simplifiers/propagate_values.h"
#include "ast/simplifiers/rewriter_simplifier.h"
#include "ast/simplifiers/solve_eqs.h"
#include "ast/simplifiers/bv_slice.h"
#include "ast/simplifiers/eliminate_predicates.h"
#include "ast/simplifiers/elim_unconstrained.h"
#include "ast/simplifiers/pull_nested_quantifiers.h"
#include "ast/simplifiers/distribute_forall.h"
#include "ast/simplifiers/refine_inj_axiom.h"
#include "ast/simplifiers/elim_bounds.h"
#include "ast/simplifiers/bit2int.h"
#include "ast/simplifiers/bv_elim.h"
#include "ast/simplifiers/push_ite.h"
#include "ast/simplifiers/elim_term_ite.h"
#include "ast/simplifiers/flatten_clauses.h"
#include "ast/simplifiers/cnf_nnf.h"
#include "sat/sat_params.hpp"
#include "smt/params/smt_params.h"
#include "sat/sat_solver/sat_smt_preprocess.h"
#include "qe/lite/qe_lite.h"
void init_preprocess(ast_manager& m, params_ref const& p, seq_simplifier& s, dependent_expr_state& st) {
sat_params sp(p);
smt_params smtp(p);
if (sp.euf() || sp.smt()) {
s.add_simplifier(alloc(rewriter_simplifier, m, p, st));
if (smtp.m_propagate_values) s.add_simplifier(alloc(propagate_values, m, p, st));
if (smtp.m_solve_eqs) s.add_simplifier(alloc(euf::solve_eqs, m, st));
if (smtp.m_elim_unconstrained) s.add_simplifier(alloc(elim_unconstrained, m, st));
if (smtp.m_nnf_cnf) s.add_simplifier(alloc(cnf_nnf_simplifier, m, p, st));
if (smtp.m_macro_finder || smtp.m_quasi_macros) s.add_simplifier(alloc(eliminate_predicates, m, st));
if (smtp.m_qe_lite) s.add_simplifier(mk_qe_lite_simplifer(m, p, st));
if (smtp.m_pull_nested_quantifiers) s.add_simplifier(alloc(pull_nested_quantifiers_simplifier, m, p, st));
if (smtp.m_max_bv_sharing) s.add_simplifier(mk_max_bv_sharing(m, p, st));
if (smtp.m_refine_inj_axiom) s.add_simplifier(alloc(refine_inj_axiom_simplifier, m, p, st));
if (smtp.m_bv_size_reduce) s.add_simplifier(alloc(bv::slice, m, st));
if (smtp.m_distribute_forall) s.add_simplifier(alloc(distribute_forall_simplifier, m, p, st));
if (smtp.m_eliminate_bounds) s.add_simplifier(alloc(elim_bounds_simplifier, m, p, st));
if (smtp.m_simplify_bit2int) s.add_simplifier(alloc(bit2int_simplifier, m, p, st));
if (smtp.m_bb_quantifiers) s.add_simplifier(alloc(bv::elim_simplifier, m, p, st));
if (smtp.m_eliminate_term_ite && smtp.m_lift_ite != lift_ite_kind::LI_FULL) s.add_simplifier(alloc(elim_term_ite_simplifier, m, p, st));
if (smtp.m_lift_ite != lift_ite_kind::LI_NONE) s.add_simplifier(alloc(push_ite_simplifier, m, p, st, smtp.m_lift_ite == lift_ite_kind::LI_CONSERVATIVE));
if (smtp.m_ng_lift_ite != lift_ite_kind::LI_NONE) s.add_simplifier(alloc(ng_push_ite_simplifier, m, p, st, smtp.m_ng_lift_ite == lift_ite_kind::LI_CONSERVATIVE));
s.add_simplifier(alloc(flatten_clauses, m, p, st));
//
// add:
// euf_completion?
//
// add: make it externally programmable
//
#if 0
if (!invoke(m_apply_quasi_macros)) return;
#endif
}
else {
params_ref simp1_p = p;
simp1_p.set_bool("som", true);
simp1_p.set_bool("pull_cheap_ite", true);
simp1_p.set_bool("push_ite_bv", false);
simp1_p.set_bool("local_ctx", true);
simp1_p.set_uint("local_ctx_limit", 10000000);
simp1_p.set_bool("flat", true); // required by som
simp1_p.set_bool("hoist_mul", false); // required by som
simp1_p.set_bool("elim_and", true);
simp1_p.set_bool("blast_distinct", true);
simp1_p.set_bool("flat_and_or", false);
params_ref simp2_p = p;
simp2_p.set_bool("flat", false);
simp2_p.set_bool("flat_and_or", false);
s.add_simplifier(alloc(rewriter_simplifier, m, p, st));
s.add_simplifier(alloc(propagate_values, m, p, st));
s.add_simplifier(alloc(card2bv, m, p, st));
s.add_simplifier(alloc(rewriter_simplifier, m, simp1_p, st));
s.add_simplifier(mk_max_bv_sharing(m, p, st));
s.add_simplifier(alloc(bit_blaster_simplifier, m, p, st));
s.add_simplifier(alloc(rewriter_simplifier, m, simp2_p, st));
}
}

View file

@ -1,25 +0,0 @@
/*++
Copyright (c) 2022 Microsoft Corporation
Module Name:
sat_smt_preprocess.h
Abstract:
SAT pre-process initialization
It collects the functionality associated with
initializing pre-processing for the sat-smt solver.
Author:
Nikolaj Bjorner (nbjorner) 2022-11-28
--*/
#pragma once
#include "ast/simplifiers/seq_simplifier.h"
void init_preprocess(ast_manager& m, params_ref const& p, seq_simplifier& s, dependent_expr_state& st);

View file

@ -17,14 +17,10 @@ Author:
Notes:
- add translation for preprocess state.
- If the pre-processors are stateful, they need to be properly translated.
- add back get_consequences, maybe or just have them handled by inc_sat_solver
- could also port the layered solver used by smtfd and used by get_consequences to simplifiers
- port various pre-processing to simplifiers
- qe-lite, fm-elimination, ite-lifting, other from asserted_formulas
--*/
@ -36,7 +32,7 @@ Notes:
#include "model/model_smt2_pp.h"
#include "model/model_evaluator.h"
#include "sat/sat_solver.h"
#include "sat/sat_solver/sat_smt_preprocess.h"
#include "solver/simplifier_solver.h"
#include "sat/sat_params.hpp"
#include "sat/smt/euf_solver.h"
#include "sat/tactic/goal2sat.h"
@ -47,54 +43,6 @@ Notes:
// incremental SAT solver.
class sat_smt_solver : public solver {
struct dep_expr_state : public dependent_expr_state {
sat_smt_solver& s;
model_reconstruction_trail m_reconstruction_trail;
dep_expr_state(sat_smt_solver& s):dependent_expr_state(s.m), s(s), m_reconstruction_trail(s.m, m_trail) {}
~dep_expr_state() override {}
virtual unsigned qtail() const override { return s.m_fmls.size(); }
dependent_expr const& operator[](unsigned i) override { return s.m_fmls[i]; }
void update(unsigned i, dependent_expr const& j) override { SASSERT(j.fml()); s.m_fmls[i] = j; }
void add(dependent_expr const& j) override { s.m_fmls.push_back(j); }
bool inconsistent() override { return s.m_solver.inconsistent(); }
model_reconstruction_trail& model_trail() override { return m_reconstruction_trail; }
std::ostream& display(std::ostream& out) const override {
unsigned i = 0;
for (auto const& d : s.m_fmls) {
if (i > 0 && i == qhead())
out << "---- head ---\n";
out << d << "\n";
++i;
}
m_reconstruction_trail.display(out);
return out;
}
void append(generic_model_converter& mc) { model_trail().append(mc); }
void replay(unsigned qhead, expr_ref_vector& assumptions) { m_reconstruction_trail.replay(qhead, assumptions, *this); }
void flatten_suffix() override {
expr_mark seen;
unsigned j = qhead();
for (unsigned i = qhead(); i < qtail(); ++i) {
expr* f = s.m_fmls[i].fml();
if (seen.is_marked(f))
continue;
seen.mark(f, true);
if (s.m.is_true(f))
continue;
if (s.m.is_and(f)) {
auto* d = s.m_fmls[i].dep();
for (expr* arg : *to_app(f))
s.m_fmls.push_back(dependent_expr(s.m, arg, nullptr, d));
continue;
}
if (i != j)
s.m_fmls[j] = s.m_fmls[i];
++j;
}
s.m_fmls.shrink(j);
}
};
struct dependency2assumptions {
ast_manager& m;
trail_stack& m_trail;
@ -152,15 +100,12 @@ class sat_smt_solver : public solver {
mutable sat::solver m_solver;
params_ref m_params;
vector<dependent_expr> m_fmls;
dep_expr_state m_preprocess_state;
seq_simplifier m_preprocess;
trail_stack& m_trail;
trail_stack m_trail;
dependency2assumptions m_dep;
goal2sat m_goal2sat;
expr_ref_vector m_assumptions, m_core, m_ors, m_aux_fmls, m_internalized_fmls;
unsigned m_qhead = 0;
expr_ref_vector m_assumptions, m_core, m_ors, m_fmls, m_internalized_fmls;
atom2bool_var m_map;
generic_model_converter_ref m_mc;
mutable model_converter_ref m_cached_mc;
mutable ref<sat2goal::mc> m_sat_mc;
std::string m_unknown = "no reason given";
@ -168,20 +113,16 @@ class sat_smt_solver : public solver {
// this allows to access the internal state of the SAT solver and carry on partial results.
bool m_internalized_converted = false; // have internalized formulas been converted back
bool is_internalized() const { return m_preprocess_state.qhead() == m_fmls.size(); }
bool is_internalized() const { return m_qhead == m_fmls.size(); }
public:
sat_smt_solver(ast_manager& m, params_ref const& p):
solver(m),
m_solver(p, m.limit()),
m_preprocess_state(*this),
m_preprocess(m, p, m_preprocess_state),
m_trail(m_preprocess_state.m_trail),
m_dep(m, m_trail),
m_assumptions(m), m_core(m), m_ors(m), m_aux_fmls(m), m_internalized_fmls(m),
m_assumptions(m), m_core(m), m_ors(m), m_fmls(m), m_internalized_fmls(m),
m_map(m) {
updt_params(p);
init_preprocess();
m_solver.set_incremental(true);
}
@ -203,13 +144,10 @@ public:
}
// TODO: copy preprocess state
for (auto const& [k, v] : m_dep.m_dep2orig) result->m_dep.insert(tr(v), tr(k));
for (dependent_expr const& f : m_fmls) result->m_fmls.push_back(dependent_expr(tr, f));
for (expr* f : m_assumptions) result->m_assumptions.push_back(tr(f));
for (auto & kv : m_map) result->m_map.insert(tr(kv.m_key), kv.m_value);
for (expr* f : m_internalized_fmls) result->m_internalized_fmls.push_back(tr(f));
if (m_mc) result->m_mc = dynamic_cast<generic_model_converter*>(m_mc->translate(tr));
result->m_dep.copy(tr, m_dep);
if (m_sat_mc) result->m_sat_mc = dynamic_cast<sat2goal::mc*>(m_sat_mc->translate(tr));
result->m_internalized_converted = m_internalized_converted;
return result;
}
@ -234,8 +172,6 @@ public:
expr_ref_vector assumptions(m);
for (unsigned i = 0; i < sz; ++i)
assumptions.push_back(ensure_literal(_assumptions[i]));
for (expr* a : assumptions)
m_preprocess_state.freeze(a);
TRACE("sat", tout << assumptions << "\n";);
lbool r = internalize_formulas(assumptions);
if (r != l_true)
@ -284,17 +220,15 @@ public:
m_solver.user_push();
m_goal2sat.user_push();
m_map.push();
m_preprocess_state.push();
m_preprocess.push();
m_trail.push_scope();
m_trail.push(restore_vector(m_assumptions));
m_trail.push(restore_vector(m_fmls));
m_trail.push(value_trail(m_qhead));
}
void pop(unsigned n) override {
n = std::min(n, m_trail.get_num_scopes()); // allow sat_smt_solver to take over for another solver.
m_preprocess.pop(n);
m_preprocess_state.pop(n);
m_trail.pop_scope(n);
m_map.pop(n);
m_goal2sat.user_pop(n);
m_solver.user_pop(n);
@ -345,18 +279,32 @@ public:
return a;
expr* new_dep = m.mk_fresh_const("dep", m.mk_bool_sort());
expr* fml = m.mk_iff(new_dep, a);
m_fmls.push_back(dependent_expr(m, fml, nullptr, nullptr));
m_fmls.push_back(fml);
m_dep.insert(a, new_dep);
return new_dep;
}
void assert_expr_core2(expr * t, expr * a) override {
a = ensure_literal(a);
m_fmls.push_back(dependent_expr(m, t, nullptr, m.mk_leaf(a)));
m_ors.reset();
m_ors.push_back(t);
if (m.is_and(a)) {
for (expr* arg : *to_app(a)) {
arg = ensure_literal(arg);
m_ors.push_back(mk_not(m, arg));
m_assumptions.push_back(arg);
}
}
else {
a = ensure_literal(a);
m_assumptions.push_back(a);
m_ors.push_back(mk_not(m, a));
}
flatten_or(m_ors);
m_fmls.push_back(mk_or(m_ors));
}
void assert_expr_core(expr * t) override {
m_fmls.push_back(dependent_expr(m, t, nullptr, nullptr));
m_fmls.push_back(t);
}
ast_manager& get_manager() const override { return m; }
@ -367,7 +315,6 @@ public:
solver::collect_param_descrs(r);
goal2sat::collect_param_descrs(r);
sat::solver::collect_param_descrs(r);
m_preprocess.collect_param_descrs(r);
}
void updt_params(params_ref const & p) override {
@ -377,13 +324,11 @@ public:
m_params.set_sym("pb.solver", sp.pb_solver());
m_solver.updt_params(m_params);
m_solver.set_incremental(true);
m_preprocess.updt_params(m_params);
if (sp.smt())
ensure_euf();
}
void collect_statistics(statistics & st) const override {
m_preprocess.collect_statistics(st);
m_solver.collect_statistics(st);
}
@ -531,7 +476,7 @@ public:
expr * get_assertion(unsigned idx) const override {
if (is_internalized() && m_internalized_converted)
return m_internalized_fmls[idx];
return m_fmls[idx].fml();
return m_fmls.get(idx);
}
unsigned get_num_assumptions() const override {
@ -549,7 +494,7 @@ public:
return m_cached_mc;
if (is_internalized() && m_internalized_converted) {
if (m_sat_mc) m_sat_mc->flush_smc(m_solver, m_map);
m_cached_mc = concat(solver::get_model_converter().get(), m_mc.get(), m_sat_mc.get());
m_cached_mc = concat(solver::get_model_converter().get(), m_sat_mc.get());
TRACE("sat", m_cached_mc->display(tout););
return m_cached_mc;
}
@ -574,10 +519,6 @@ public:
m_internalized_converted = true;
}
void init_preprocess() {
::init_preprocess(m, m_params, m_preprocess, m_preprocess_state);
}
euf::solver* get_euf() {
return dynamic_cast<euf::solver*>(m_solver.get_extension());
}
@ -646,50 +587,20 @@ private:
if (is_internalized() && assumptions.empty())
return l_true;
unsigned qhead = m_preprocess_state.qhead();
TRACE("sat", tout << "qhead " << qhead << "\n");
TRACE("sat", tout << "qhead " << m_qhead << "\n");
m_internalized_converted = false;
m_preprocess_state.replay(qhead, assumptions);
m_preprocess.reduce();
if (!m.inc())
return l_undef;
m_preprocess_state.advance_qhead();
m_mc = alloc(generic_model_converter, m, "sat-model-converter");
m_preprocess_state.append(*m_mc);
m_solver.pop_to_base_level();
m_aux_fmls.reset();
for (; qhead < m_fmls.size(); ++qhead)
add_with_dependency(m_fmls[qhead]);
init_goal2sat();
m_goal2sat(m_aux_fmls.size(), m_aux_fmls.data());
m_goal2sat(m_fmls.size() - m_qhead, m_fmls.data() + m_qhead);
if (!m_sat_mc)
m_sat_mc = alloc(sat2goal::mc, m);
m_sat_mc->flush_smc(m_solver, m_map);
m_qhead = m_fmls.size();
return m.inc() ? l_true : l_undef;
}
ptr_vector<expr> m_deps;
void add_with_dependency(dependent_expr const& de) {
if (!de.dep()) {
m_aux_fmls.push_back(de.fml());
return;
}
m_deps.reset();
m.linearize(de.dep(), m_deps);
m_ors.reset();
m_ors.push_back(de.fml());
flatten_or(m_ors);
for (expr* d : m_deps) {
SASSERT(m.is_bool(d));
SASSERT(is_literal(d));
m_assumptions.push_back(d);
m_ors.push_back(mk_not(m, d));
}
m_aux_fmls.push_back(mk_or(m_ors));
}
void extract_core() {
m_core.reset();
if (m_dep.m_literals.empty())
@ -720,7 +631,7 @@ private:
mdl = nullptr;
if (!m_solver.model_is_current())
return;
if (m_fmls.size() > m_preprocess_state.qhead())
if (m_fmls.size() > m_qhead)
return;
TRACE("sat", m_solver.display_model(tout););
CTRACE("sat", m_sat_mc, m_sat_mc->display(tout););
@ -751,7 +662,6 @@ private:
if (m_sat_mc)
(*m_sat_mc)(mdl);
m_goal2sat.update_model(mdl);
TRACE("sat", model_smt2_pp(tout, m, *mdl, 0););
@ -760,25 +670,24 @@ private:
model_evaluator eval(*mdl);
eval.set_model_completion(true);
bool all_true = true;
for (dependent_expr const& d : m_fmls) {
if (has_quantifiers(d.fml()))
for (expr* f : m_fmls) {
if (has_quantifiers(f))
continue;
expr_ref tmp(m);
eval(d.fml(), tmp);
eval(f, tmp);
if (m.limit().is_canceled())
return;
CTRACE("sat", !m.is_true(tmp),
tout << "Evaluation failed: " << mk_pp(d.fml(), m) << " to " << tmp << "\n";
tout << "Evaluation failed: " << mk_pp(f, m) << " to " << tmp << "\n";
model_smt2_pp(tout, m, *(mdl.get()), 0););
if (m.is_false(tmp)) {
IF_VERBOSE(0, verbose_stream() << "failed to verify: " << mk_pp(d.fml(), m) << "\n");
IF_VERBOSE(0, verbose_stream() << "failed to verify: " << mk_pp(f, m) << "\n");
IF_VERBOSE(0, verbose_stream() << "evaluated to " << tmp << "\n");
all_true = false;
}
}
if (!all_true) {
IF_VERBOSE(0, verbose_stream() << m_params << "\n");
IF_VERBOSE(0, if (m_mc) m_mc->display(verbose_stream() << "mc0\n"));
IF_VERBOSE(0, for (auto const& kv : m_map) verbose_stream() << mk_pp(kv.m_key, m) << " |-> " << kv.m_value << "\n");
exit(0);
}
@ -786,13 +695,11 @@ private:
IF_VERBOSE(1, verbose_stream() << "solution verified\n");
}
}
TRACE("sat", m_mc->display(tout););
(*m_mc)(mdl);
}
};
solver* mk_sat_smt_solver(ast_manager& m, params_ref const& p) {
return alloc(sat_smt_solver, m, p);
return mk_simplifier_solver(alloc(sat_smt_solver, m, p));
}