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

reorganizing the code

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2012-10-23 21:53:34 -07:00
parent b89d35dd69
commit 9e299b88c4
101 changed files with 16 additions and 16 deletions

View file

@ -0,0 +1,309 @@
/*++
Copyright (c) 2012 Microsoft Corporation
Module Name:
goal2nlsat.cpp
Abstract:
"Compile" a goal into the nonlinear arithmetic engine.
Non-arithmetic atoms are "abstracted" into boolean variables.
Non-supported terms are "abstracted" into variables.
The mappings can be used to convert back the state of the
engine into a goal.
Author:
Leonardo (leonardo) 2012-01-02
Notes:
--*/
#include"goal2nlsat.h"
#include"goal.h"
#include"goal_util.h"
#include"nlsat_solver.h"
#include"expr2polynomial.h"
#include"expr2var.h"
#include"arith_decl_plugin.h"
#include"tactic.h"
#include"ast_smt2_pp.h"
struct goal2nlsat::imp {
struct nlsat_expr2polynomial : public expr2polynomial {
nlsat::solver & m_solver;
nlsat_expr2polynomial(nlsat::solver & s, ast_manager & m, polynomial::manager & pm, expr2var * e2v):
expr2polynomial(m, pm, e2v),
m_solver(s) {
}
virtual bool is_int(polynomial::var x) const {
return m_solver.is_int(x);
}
virtual polynomial::var mk_var(bool is_int) {
return m_solver.mk_var(is_int);
}
};
ast_manager & m;
nlsat::solver & m_solver;
polynomial::manager & m_pm;
unsynch_mpq_manager & m_qm;
arith_util m_util;
expr2var & m_a2b;
expr2var & m_t2x;
nlsat_expr2polynomial m_expr2poly;
polynomial::factor_params m_fparams;
unsigned long long m_max_memory;
bool m_factor;
volatile bool m_cancel;
imp(ast_manager & _m, params_ref const & p, nlsat::solver & s, expr2var & a2b, expr2var & t2x):
m(_m),
m_solver(s),
m_pm(s.pm()),
m_qm(s.qm()),
m_util(m),
m_a2b(a2b),
m_t2x(t2x),
m_expr2poly(m_solver, m, m_solver.pm(), &m_t2x) {
updt_params(p);
m_cancel = false;
}
void updt_params(params_ref const & p) {
m_max_memory = megabytes_to_bytes(p.get_uint(":max-memory", UINT_MAX));
m_factor = p.get_bool(":factor", true);
m_fparams.updt_params(p);
}
void set_cancel(bool f) {
m_cancel = f;
m_pm.set_cancel(f);
}
nlsat::atom::kind flip(nlsat::atom::kind k) {
switch (k) {
case nlsat::atom::EQ: return k;
case nlsat::atom::LT: return nlsat::atom::GT;
case nlsat::atom::GT: return nlsat::atom::LT;
default:
UNREACHABLE();
return k;
}
}
nlsat::bool_var factor_atom(polynomial::polynomial * p, nlsat::atom::kind k) {
sbuffer<bool> is_even;
ptr_buffer<polynomial::polynomial> ps;
polynomial::factors fs(m_pm);
m_pm.factor(p, fs, m_fparams);
TRACE("goal2nlsat_bug", tout << "factors:\n" << fs << "\n";);
SASSERT(fs.distinct_factors() > 0);
for (unsigned i = 0; i < fs.distinct_factors(); i++) {
ps.push_back(fs[i]);
is_even.push_back(fs.get_degree(i) % 2 == 0);
}
if (m_qm.is_neg(fs.get_constant()))
k = flip(k);
return m_solver.mk_ineq_atom(k, ps.size(), ps.c_ptr(), is_even.c_ptr());
}
nlsat::literal process_atom(app * f, nlsat::atom::kind k) {
SASSERT(f->get_num_args() == 2);
expr * lhs = f->get_arg(0);
expr * rhs = f->get_arg(1);
polynomial_ref p1(m_pm);
polynomial_ref p2(m_pm);
scoped_mpz d1(m_qm);
scoped_mpz d2(m_qm);
m_expr2poly.to_polynomial(lhs, p1, d1);
m_expr2poly.to_polynomial(rhs, p2, d2);
scoped_mpz lcm(m_qm);
m_qm.lcm(d1, d2, lcm);
m_qm.div(lcm, d1, d1);
m_qm.div(lcm, d2, d2);
m_qm.neg(d2);
polynomial_ref p(m_pm);
p = m_pm.addmul(d1, m_pm.mk_unit(), p1, d2, m_pm.mk_unit(), p2);
TRACE("goal2nlsat_bug", tout << "p: " << p << "\nk: " << k << "\n";);
if (is_const(p)) {
int sign;
if (is_zero(p))
sign = 0;
else
sign = m_qm.is_pos(m_pm.coeff(p, 0)) ? 1 : -1;
switch (k) {
case nlsat::atom::EQ: return sign == 0 ? nlsat::true_literal : nlsat::false_literal;
case nlsat::atom::LT: return sign < 0 ? nlsat::true_literal : nlsat::false_literal;
case nlsat::atom::GT: return sign > 0 ? nlsat::true_literal : nlsat::false_literal;
default:
UNREACHABLE();
return nlsat::true_literal;
}
}
if (m_factor) {
return nlsat::literal(factor_atom(p, k), false);
}
else {
bool is_even = false;
polynomial::polynomial * _p = p.get();
return nlsat::literal(m_solver.mk_ineq_atom(k, 1, &_p, &is_even), false);
}
}
nlsat::literal process_eq(app * f) {
return process_atom(f, nlsat::atom::EQ);
}
nlsat::literal process_le(app * f) {
return ~process_atom(f, nlsat::atom::GT);
}
nlsat::literal process_ge(app * f) {
return ~process_atom(f, nlsat::atom::LT);
}
// everything else is compiled as a boolean variable
nlsat::bool_var process_bvar(expr * f) {
if (m_a2b.is_var(f)) {
return static_cast<nlsat::bool_var>(m_a2b.to_var(f));
}
else {
nlsat::bool_var b = m_solver.mk_bool_var();
m_a2b.insert(f, b);
return b;
}
}
nlsat::literal process_atom(expr * f) {
if (m.is_eq(f)) {
if (m_util.is_int_real(to_app(f)->get_arg(0)))
return process_eq(to_app(f));
else
return nlsat::literal(process_bvar(f), false);
}
else if (m_util.is_le(f)) {
return process_le(to_app(f));
}
else if (m_util.is_ge(f)) {
return process_ge(to_app(f));
}
else if (is_app(f)) {
if (to_app(f)->get_family_id() == m.get_basic_family_id()) {
switch (to_app(f)->get_decl_kind()) {
case OP_TRUE:
case OP_FALSE:
TRACE("goal2nlsat", tout << "f: " << mk_ismt2_pp(f, m) << "\n";);
throw tactic_exception("apply simplify before applying nlsat");
case OP_AND:
case OP_OR:
case OP_IFF:
case OP_XOR:
case OP_NOT:
case OP_IMPLIES:
throw tactic_exception("convert goal into cnf before applying nlsat");
case OP_DISTINCT:
throw tactic_exception("eliminate distinct operator (use tactic '(using-params simplify :blast-distinct true)') before applying nlsat");
default:
UNREACHABLE();
return nlsat::literal(nlsat::null_bool_var, false);
}
}
else if (to_app(f)->get_family_id() == m_util.get_family_id()) {
throw tactic_exception("apply purify-arith before applying nlsat");
}
else {
return nlsat::literal(process_bvar(f), false);
}
}
else {
SASSERT(is_quantifier(f));
return nlsat::literal(process_bvar(f), false);
}
}
nlsat::literal process_literal(expr * f) {
bool neg = false;
while (m.is_not(f, f))
neg = !neg;
nlsat::literal l = process_atom(f);
if (neg)
l.neg();
return l;
}
void process(expr * f, expr_dependency * dep) {
unsigned num_lits;
expr * const * lits;
if (m.is_or(f)) {
num_lits = to_app(f)->get_num_args();
lits = to_app(f)->get_args();
}
else {
num_lits = 1;
lits = &f;
}
sbuffer<nlsat::literal> ls;
for (unsigned i = 0; i < num_lits; i++) {
ls.push_back(process_literal(lits[i]));
}
m_solver.mk_clause(ls.size(), ls.c_ptr(), dep);
}
void operator()(goal const & g) {
if (has_term_ite(g))
throw tactic_exception("eliminate term-ite before applying nlsat");
unsigned sz = g.size();
for (unsigned i = 0; i < sz; i++) {
process(g.form(i), g.dep(i));
}
}
};
struct goal2nlsat::scoped_set_imp {
goal2nlsat & m_owner;
scoped_set_imp(goal2nlsat & o, imp & i):m_owner(o) {
#pragma omp critical (tactic_cancel)
{
m_owner.m_imp = &i;
}
}
~scoped_set_imp() {
#pragma omp critical (tactic_cancel)
{
m_owner.m_imp = 0;
}
}
};
goal2nlsat::goal2nlsat() {
m_imp = 0;
}
goal2nlsat::~goal2nlsat() {
SASSERT(m_imp == 0);
}
void goal2nlsat::collect_param_descrs(param_descrs & r) {
insert_max_memory(r);
r.insert(":factor", CPK_BOOL, "(default: true) factor polynomials.");
polynomial::factor_params::get_param_descrs(r);
}
void goal2nlsat::operator()(goal const & g, params_ref const & p, nlsat::solver & s, expr2var & a2b, expr2var & t2x) {
imp local_imp(g.m(), p, s, a2b, t2x);
scoped_set_imp setter(*this, local_imp);
local_imp(g);
}
void goal2nlsat::set_cancel(bool f) {
if (m_imp)
m_imp->set_cancel(f);
}

View file

@ -0,0 +1,75 @@
/*++
Copyright (c) 2012 Microsoft Corporation
Module Name:
goal2nlsat.h
Abstract:
"Compile" a goal into the nonlinear arithmetic engine.
Non-arithmetic atoms are "abstracted" into boolean variables.
Non-supported terms are "abstracted" into variables.
The mappings can be used to convert back the state of the
engine into a goal.
Author:
Leonardo (leonardo) 2012-01-02
Notes:
--*/
#ifndef _GOAL2NLSAT_H_
#define _GOAL2NLSAT_H_
#include"nlsat_types.h"
#include"model_converter.h"
class goal;
class expr2var;
class goal2nlsat {
struct imp;
imp * m_imp;
struct scoped_set_imp;
public:
goal2nlsat();
~goal2nlsat();
static void collect_param_descrs(param_descrs & r);
/**
\brief "Compile" the goal into the given nlsat engine.
Store a mapping from atoms to boolean variables into a2b.
Store a mapping from terms into arithmetic variables into t2x.
\remark a2b and t2x m don't need to be empty. The definitions there are reused.
The input is expected to be in CNF
*/
void operator()(goal const & g, params_ref const & p, nlsat::solver & s, expr2var & a2b, expr2var & t2x);
void set_cancel(bool f);
};
class nlsat2goal {
struct imp;
imp * m_imp;
public:
nlsat2goal();
~nlsat2goal();
static void collect_param_descrs(param_descrs & r);
/**
\brief Translate the state of the nlsat engine back into a goal.
*/
void operator()(nlsat::solver const & s, expr2var const & a2b, expr2var const & t2x,
params_ref const & p, goal & g, model_converter_ref & mc);
void set_cancel(bool f);
};
#endif

View file

@ -0,0 +1,258 @@
/*++
Copyright (c) 2012 Microsoft Corporation
Module Name:
nlsat_tactic.cpp
Abstract:
Tactic for using nonlinear procedure.
Author:
Leonardo (leonardo) 2012-01-02
Notes:
--*/
#include"tactical.h"
#include"goal2nlsat.h"
#include"nlsat_solver.h"
#include"model.h"
#include"expr2var.h"
#include"arith_decl_plugin.h"
#include"ast_smt2_pp.h"
#include"z3_exception.h"
#include"algebraic_numbers.h"
class nlsat_tactic : public tactic {
struct expr_display_var_proc : public nlsat::display_var_proc {
ast_manager & m;
expr_ref_vector m_var2expr;
expr_display_var_proc(ast_manager & _m):m(_m), m_var2expr(_m) {}
virtual void operator()(std::ostream & out, nlsat::var x) const {
if (x < m_var2expr.size())
out << mk_ismt2_pp(m_var2expr.get(x), m);
else
out << "x!" << x;
}
};
struct imp {
ast_manager & m;
params_ref m_params;
expr_display_var_proc m_display_var;
nlsat::solver m_solver;
goal2nlsat m_g2nl;
imp(ast_manager & _m, params_ref const & p):
m(_m),
m_params(p),
m_display_var(_m),
m_solver(p) {
}
void updt_params(params_ref const & p) {
m_params = p;
m_solver.updt_params(p);
}
void set_cancel(bool f) {
m_solver.set_cancel(f);
m_g2nl.set_cancel(f);
}
bool contains_unsupported(expr_ref_vector & b2a, expr_ref_vector & x2t) {
for (unsigned x = 0; x < x2t.size(); x++) {
if (!is_uninterp_const(x2t.get(x))) {
TRACE("unsupported", tout << "unsupported atom:\n" << mk_ismt2_pp(x2t.get(x), m) << "\n";);
return true;
}
}
for (unsigned b = 0; b < b2a.size(); b++) {
expr * a = b2a.get(b);
if (a == 0)
continue;
if (is_uninterp_const(a))
continue;
if (m_solver.is_interpreted(b))
continue; // arithmetic atom
TRACE("unsupported", tout << "unsupported atom:\n" << mk_ismt2_pp(a, m) << "\n";);
return true; // unsupported
}
return false;
}
// Return false if nlsat assigned noninteger value to an integer variable.
bool mk_model(expr_ref_vector & b2a, expr_ref_vector & x2t, model_converter_ref & mc) {
bool ok = true;
model_ref md = alloc(model, m);
arith_util util(m);
for (unsigned x = 0; x < x2t.size(); x++) {
expr * t = x2t.get(x);
if (!is_uninterp_const(t))
continue;
expr * v;
try {
v = util.mk_numeral(m_solver.value(x), util.is_int(t));
}
catch (z3_error & ex) {
throw ex;
}
catch (z3_exception &) {
v = util.mk_to_int(util.mk_numeral(m_solver.value(x), false));
ok = false;
}
md->register_decl(to_app(t)->get_decl(), v);
}
for (unsigned b = 0; b < b2a.size(); b++) {
expr * a = b2a.get(b);
if (a == 0 || !is_uninterp_const(a))
continue;
lbool val = m_solver.bvalue(b);
if (val == l_undef)
continue; // don't care
md->register_decl(to_app(a)->get_decl(), val == l_true ? m.mk_true() : m.mk_false());
}
mc = model2model_converter(md.get());
return ok;
}
void operator()(goal_ref const & g,
goal_ref_buffer & result,
model_converter_ref & mc,
proof_converter_ref & pc,
expr_dependency_ref & core) {
SASSERT(g->is_well_sorted());
mc = 0; pc = 0; core = 0;
tactic_report report("nlsat", *g);
if (g->is_decided()) {
result.push_back(g.get());
return;
}
fail_if_proof_generation("nlsat", g);
expr2var a2b(m);
expr2var t2x(m);
m_g2nl(*g, m_params, m_solver, a2b, t2x);
m_display_var.m_var2expr.reset();
t2x.mk_inv(m_display_var.m_var2expr);
m_solver.set_display_var(m_display_var);
lbool st = m_solver.check();
if (st == l_undef) {
}
else if (st == l_true) {
expr_ref_vector x2t(m);
expr_ref_vector b2a(m);
a2b.mk_inv(b2a);
t2x.mk_inv(x2t);
if (!contains_unsupported(b2a, x2t)) {
// If mk_model is false it means that the model produced by nlsat
// assigns noninteger values to integer variables
if (mk_model(b2a, x2t, mc)) {
// result goal is trivially SAT
g->reset();
}
}
}
else {
// TODO: extract unsat core
g->assert_expr(m.mk_false(), 0, 0);
}
g->inc_depth();
result.push_back(g.get());
TRACE("nlsat", g->display(tout););
SASSERT(g->is_well_sorted());
}
};
imp * m_imp;
params_ref m_params;
statistics m_stats;
struct scoped_set_imp {
nlsat_tactic & m_owner;
scoped_set_imp(nlsat_tactic & o, imp & i):m_owner(o) {
#pragma omp critical (tactic_cancel)
{
m_owner.m_imp = &i;
}
}
~scoped_set_imp() {
m_owner.m_imp->m_solver.collect_statistics(m_owner.m_stats);
#pragma omp critical (tactic_cancel)
{
m_owner.m_imp = 0;
}
}
};
public:
nlsat_tactic(params_ref const & p):
m_params(p) {
m_imp = 0;
}
virtual tactic * translate(ast_manager & m) {
return alloc(nlsat_tactic, m_params);
}
virtual ~nlsat_tactic() {
SASSERT(m_imp == 0);
}
virtual void updt_params(params_ref const & p) {
m_params = p;
}
virtual void collect_param_descrs(param_descrs & r) {
goal2nlsat::collect_param_descrs(r);
nlsat::solver::collect_param_descrs(r);
algebraic_numbers::manager::collect_param_descrs(r);
}
virtual void operator()(goal_ref const & in,
goal_ref_buffer & result,
model_converter_ref & mc,
proof_converter_ref & pc,
expr_dependency_ref & core) {
try {
imp local_imp(in->m(), m_params);
scoped_set_imp setter(*this, local_imp);
local_imp(in, result, mc, pc, core);
}
catch (z3_error & ex) {
throw ex;
}
catch (z3_exception & ex) {
throw tactic_exception(ex.msg());
}
}
virtual void cleanup() {}
virtual void set_cancel(bool f) {
if (m_imp)
m_imp->set_cancel(f);
}
virtual void collect_statistics(statistics & st) const {
st.copy(m_stats);
}
virtual void reset_statistics() {
m_stats.reset();
}
};
tactic * mk_nlsat_tactic(ast_manager & m, params_ref const & p) {
return clean(alloc(nlsat_tactic, p));
}

View file

@ -0,0 +1,28 @@
/*++
Copyright (c) 2012 Microsoft Corporation
Module Name:
nlsat_tactic.h
Abstract:
Tactic for using nonlinear procedure.
Author:
Leonardo (leonardo) 2012-01-02
Notes:
--*/
#ifndef _NLSAT_TACTIC_H_
#define _NLSAT_TACTIC_H_
#include"params.h"
class ast_manager;
class tactic;
tactic * mk_nlsat_tactic(ast_manager & m, params_ref const & p = params_ref());
#endif

View file

@ -0,0 +1,63 @@
/*++
Copyright (c) 2012 Microsoft Corporation
Module Name:
qfnra_nlsat_tactic.h
Abstract:
Tactic based on nlsat for solving QF_NRA problems
Author:
Leonardo (leonardo) 2012-01-23
Notes:
--*/
#include"tactical.h"
#include"tseitin_cnf_tactic.h"
#include"degree_shift_tactic.h"
#include"purify_arith_tactic.h"
#include"nlsat_tactic.h"
#include"factor_tactic.h"
#include"simplify_tactic.h"
#include"elim_uncnstr_tactic.h"
#include"propagate_values_tactic.h"
#include"solve_eqs_tactic.h"
#include"elim_term_ite_tactic.h"
tactic * mk_qfnra_nlsat_tactic(ast_manager & m, params_ref const & p) {
params_ref main_p = p;
main_p.set_bool(":elim-and", true);
main_p.set_bool(":blast-distinct", true);
params_ref purify_p = p;
purify_p.set_bool(":complete", false); // temporary hack, solver does not support uninterpreted functions for encoding (div0 x) applications. So, we replace it application of this kind with an uninterpreted function symbol.
tactic * factor;
if (p.get_bool(":factor", true))
factor = mk_factor_tactic(m, p);
else
factor = mk_skip_tactic();
return and_then(and_then(using_params(mk_simplify_tactic(m, p),
main_p),
using_params(mk_purify_arith_tactic(m, p),
purify_p),
mk_propagate_values_tactic(m, p),
mk_solve_eqs_tactic(m, p),
mk_elim_uncnstr_tactic(m, p),
mk_elim_term_ite_tactic(m, p)),
and_then(/* mk_degree_shift_tactic(m, p), */ // may affect full dimensionality detection
factor,
mk_solve_eqs_tactic(m, p),
using_params(mk_simplify_tactic(m, p),
main_p),
mk_tseitin_cnf_core_tactic(m, p),
using_params(mk_simplify_tactic(m, p),
main_p),
mk_nlsat_tactic(m, p)));
}

View file

@ -0,0 +1,30 @@
/*++
Copyright (c) 2012 Microsoft Corporation
Module Name:
qfnra_nlsat_tactic.h
Abstract:
Tactic based on nlsat for solving QF_NRA problems
Author:
Leonardo (leonardo) 2012-01-23
Notes:
--*/
#ifndef _QFNRA_NLSAT_TACTIC_H_
#define _QFNRA_NLSAT_TACTIC_H_
#include"params.h"
class ast_manager;
class tactic;
tactic * mk_qfnra_nlsat_tactic(ast_manager & m, params_ref const & p = params_ref());
MK_SIMPLE_TACTIC_FACTORY(qfnra_nlsat_fct, mk_qfnra_nlsat_tactic(m, p));
#endif