3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-24 09:35:32 +00:00

move bound_manager to simplifiers, add bound manager to extract_eqs for solve-eqs #6532

This commit is contained in:
Nikolaj Bjorner 2023-01-12 12:42:20 -08:00
parent e5e16268cc
commit 25b0b1430c
17 changed files with 86 additions and 59 deletions

View file

@ -2,7 +2,6 @@ z3_add_component(arith_tactics
SOURCES
add_bounds_tactic.cpp
arith_bounds_tactic.cpp
bound_manager.cpp
bound_propagator.cpp
bv2int_rewriter.cpp
bv2real_rewriter.cpp

View file

@ -19,7 +19,7 @@ Revision History:
#include "tactic/tactical.h"
#include "ast/arith_decl_plugin.h"
#include "ast/ast_smt2_pp.h"
#include "tactic/arith/bound_manager.h"
#include "ast/simplifiers/bound_manager.h"
struct is_unbounded_proc {
struct found {};
@ -41,7 +41,8 @@ struct is_unbounded_proc {
bool is_unbounded(goal const & g) {
ast_manager & m = g.m();
bound_manager bm(m);
bm(g);
for (unsigned i = 0; i < g.size(); ++i)
bm(g.form(i), g.dep(i), g.pr(i));
is_unbounded_proc proc(bm);
return test(g, proc);
}

View file

@ -1,295 +0,0 @@
/*++
Copyright (c) 2011 Microsoft Corporation
Module Name:
bound_manager.cpp
Abstract:
Collect bounds.
Author:
Leonardo (leonardo) 2011-05-16
Notes:
--*/
#include "tactic/arith/bound_manager.h"
#include "ast/ast_smt2_pp.h"
#include "ast/ast_pp.h"
#include "tactic/goal.h"
bound_manager::bound_manager(ast_manager & m):
m_util(m),
m_bounded_vars(m) {
}
bound_manager::~bound_manager() {
reset();
}
bound_manager* bound_manager::translate(ast_manager& dst_m) {
bound_manager* result = alloc(bound_manager, dst_m);
ast_translation tr(m(), dst_m);
expr_dependency_translation edtr(tr);
for (auto& kv : m_lowers) result->m_lowers.insert(tr(kv.m_key), kv.m_value);
for (auto& kv : m_uppers) result->m_uppers.insert(tr(kv.m_key), kv.m_value);
for (auto& kv : m_lower_deps) result->m_lower_deps.insert(tr(kv.m_key), edtr(kv.m_value));
for (auto& kv : m_upper_deps) result->m_upper_deps.insert(tr(kv.m_key), edtr(kv.m_value));
for (expr* e : m_bounded_vars) result->m_bounded_vars.push_back(tr(e));
return result;
}
static decl_kind swap_decl(decl_kind k) {
switch (k) {
case OP_LE: return OP_GE;
case OP_LT: return OP_GT;
case OP_GE: return OP_LE;
case OP_GT: return OP_LT;
default:
UNREACHABLE();
return k;
}
}
decl_kind bound_manager::neg(decl_kind k) {
switch (k) {
case OP_LE: return OP_GT;
case OP_LT: return OP_GE;
case OP_GE: return OP_LT;
case OP_GT: return OP_LE;
default:
UNREACHABLE();
return k;
}
}
void bound_manager::norm(numeral & n, decl_kind & k) {
switch (k) {
case OP_LE: return;
case OP_GE: return;
case OP_LT:
// x < n --> x <= n-1
n--;
k = OP_LE;
return;
case OP_GT:
// x > n --> x >= n+1
n++;
k = OP_GE;
return;
default:
return;
}
}
static bool is_lower(decl_kind k) {
return k == OP_GT || k == OP_GE;
}
static bool is_strict(decl_kind k) {
return k == OP_LT || k == OP_GT;
}
bool bound_manager::is_numeral(expr* v, numeral& n, bool& is_int) {
expr* w;
if (m_util.is_uminus(v, w) && is_numeral(w, n, is_int)) {
n.neg();
return true;
}
return m_util.is_numeral(v, n, is_int);
}
void bound_manager::operator()(expr * f, expr_dependency * d) {
TRACE("bound_manager", tout << "processing:\n" << mk_ismt2_pp(f, m()) << "\n";);
expr * v;
numeral n;
if (is_disjunctive_bound(f, d))
return;
if (is_equality_bound(f, d))
return;
bool pos = true;
while (m().is_not(f, f))
pos = !pos;
if (!is_app(f))
return;
app * t = to_app(f);
if (t->get_family_id() != m_util.get_family_id())
return;
decl_kind k = t->get_decl_kind();
if (k != OP_LE && k != OP_GE && k != OP_LT && k != OP_GT)
return;
expr * lhs = t->get_arg(0);
expr * rhs = t->get_arg(1);
bool is_int;
if (is_uninterp_const(lhs) && is_numeral(rhs, n, is_int)) {
v = lhs;
}
else if (is_uninterp_const(rhs) && is_numeral(lhs, n, is_int)) {
v = rhs;
k = swap_decl(k);
}
else {
return;
}
if (!pos)
k = neg(k);
if (is_int)
norm(n, k);
TRACE("bound_manager", tout << "found bound for:\n" << mk_ismt2_pp(v, m()) << "\n";);
bool strict = is_strict(k);
if (is_lower(k)) {
insert_lower(v, strict, n, d);
}
else {
insert_upper(v, strict, n, d);
}
}
void bound_manager::insert_upper(expr * v, bool strict, numeral const & n, expr_dependency * d) {
limit old;
if (m_uppers.find(v, old)) {
if (n < old.first || (n == old.first && strict && !old.second)) {
// improved bound
m_uppers.insert(v, limit(n, strict));
if (d)
m_upper_deps.insert(v, d);
}
}
else {
m_uppers.insert(v, limit(n, strict));
if (d)
m_upper_deps.insert(v, d);
if (!m_lowers.contains(v)) {
m_bounded_vars.push_back(v);
}
}
}
void bound_manager::insert_lower(expr * v, bool strict, numeral const & n, expr_dependency * d) {
limit old;
if (m_lowers.find(v, old)) {
if (n > old.first || (n == old.first && strict && !old.second)) {
// improved bound
m_lowers.insert(v, limit(n, strict));
if (d)
m_lower_deps.insert(v, d);
}
}
else {
m_lowers.insert(v, limit(n, strict));
if (d)
m_lower_deps.insert(v, d);
if (!m_uppers.contains(v)) {
m_bounded_vars.push_back(v);
}
}
}
bool bound_manager::is_equality_bound(expr * f, expr_dependency * d) {
expr* x, *y;
if (!m().is_eq(f, x, y)) {
return false;
}
if (!is_uninterp_const(x)) {
std::swap(x, y);
}
numeral n;
bool is_int;
if (is_uninterp_const(x) && is_numeral(y, n, is_int)) {
insert_lower(x, false, n, d);
insert_upper(x, false, n, d);
return true;
}
else {
return false;
}
}
bool bound_manager::is_disjunctive_bound(expr * f, expr_dependency * d) {
numeral lo, hi, n;
if (!m().is_or(f)) return false;
unsigned sz = to_app(f)->get_num_args();
if (sz == 0) return false;
expr * x, * y, * v = nullptr;
bool is_int;
for (unsigned i = 0; i < sz; ++i) {
expr * e = to_app(f)->get_arg(i);
if (!m().is_eq(e, x, y)) return false;
if (is_uninterp_const(x) &&
is_numeral(y, n, is_int) && is_int &&
(x == v || v == nullptr)) {
if (v == nullptr) { v = x; lo = hi = n; }
if (n < lo) lo = n;
if (n > hi) hi = n;
}
else if (is_uninterp_const(y) &&
is_numeral(x, n, is_int) && is_int &&
(y == v || v == nullptr)) {
if (v == nullptr) { v = y; lo = hi = n; }
if (n < lo) lo = n;
if (n > hi) hi = n;
}
else {
return false;
}
}
TRACE("bound_manager", tout << "bounds: " << lo << " " << hi << "\n";);
insert_lower(v, false, lo, d);
insert_upper(v, false, hi, d);
return true;
}
void bound_manager::operator()(goal const & g) {
if (g.proofs_enabled())
return;
unsigned sz = g.size();
for (unsigned i = 0; i < sz; i++) {
operator()(g.form(i), g.dep(i));
}
}
void bound_manager::reset() {
m_bounded_vars.finalize();
m_lowers.finalize();
m_uppers.finalize();
m_lower_deps.finalize();
m_upper_deps.finalize();
}
bool bound_manager::inconsistent() const {
for (auto const& [k,v] : m_lowers) {
limit const& lim1 = v;
limit lim2;
if (m_uppers.find(k, lim2)) {
if (lim1.first > lim2.first)
return true;
if (lim1.first == lim2.first &&
!lim1.second && lim2.second) {
return true;
}
}
}
return false;
}
void bound_manager::display(std::ostream & out) const {
numeral n; bool strict;
for (iterator it = begin(); it != end(); ++it) {
expr * v = *it;
if (has_lower(v, n, strict))
out << n << " " << (strict ? "<" : "<=");
else
out << "-oo <";
out << " " << mk_ismt2_pp(v, m()) << " ";
if (has_upper(v, n, strict))
out << (strict ? "<" : "<=") << " " << n;
else
out << "< oo";
out << "\n";
}
}

View file

@ -1,113 +0,0 @@
/*++
Copyright (c) 2011 Microsoft Corporation
Module Name:
bound_manager.h
Abstract:
Collect bounds.
Author:
Leonardo (leonardo) 2011-05-16
Notes:
--*/
#pragma once
#include "ast/ast.h"
#include "ast/arith_decl_plugin.h"
class goal;
class bound_manager {
public:
typedef rational numeral;
private:
typedef std::pair<numeral, bool> limit;
arith_util m_util;
obj_map<expr, limit> m_lowers;
obj_map<expr, limit> m_uppers;
obj_map<expr, expr_dependency*> m_lower_deps;
obj_map<expr, expr_dependency*> m_upper_deps;
expr_ref_vector m_bounded_vars;
bool is_disjunctive_bound(expr * f, expr_dependency * d);
bool is_equality_bound(expr * f, expr_dependency * d);
bool is_numeral(expr* v, rational& n, bool& is_int);
void insert_lower(expr * v, bool strict, numeral const & n, expr_dependency * d);
void insert_upper(expr * v, bool strict, numeral const & n, expr_dependency * d);
public:
static decl_kind neg(decl_kind k);
static void norm(numeral & n, decl_kind & k);
bound_manager(ast_manager & m);
~bound_manager();
bound_manager* translate(ast_manager& dst_m);
ast_manager & m() const { return m_util.get_manager(); }
void operator()(goal const & g);
void operator()(expr * n, expr_dependency * d = nullptr);
bool has_lower(expr * c, numeral & v, bool & strict) const {
limit l;
if (m_lowers.find(c, l)) {
v = l.first;
strict = l.second;
return true;
}
return false;
}
bool has_upper(expr * c, numeral & v, bool & strict) const {
limit l;
if (m_uppers.find(c, l)) {
v = l.first;
strict = l.second;
return true;
}
return false;
}
expr_dependency * lower_dep(expr * c) const {
expr_dependency * d;
if (m_lower_deps.find(c, d))
return d;
return nullptr;
}
expr_dependency * upper_dep(expr * c) const {
expr_dependency * d;
if (m_upper_deps.find(c, d))
return d;
return nullptr;
}
bool inconsistent() const;
bool has_lower(expr * c) const {
return m_lowers.contains(c);
}
bool has_upper(expr * c) const {
return m_uppers.contains(c);
}
typedef ptr_vector<expr>::const_iterator iterator;
/**
\brief Iterator for all bounded constants.
*/
iterator begin() const { return m_bounded_vars.begin(); }
iterator end() const { return m_bounded_vars.end(); }
void reset();
// for debugging purposes
void display(std::ostream & out) const;
};

View file

@ -18,7 +18,7 @@ Notes:
--*/
#include "tactic/tactical.h"
#include "tactic/arith/bound_manager.h"
#include "ast/simplifiers/bound_manager.h"
#include "ast/ast_pp.h"
#include "ast/arith_decl_plugin.h"
#include "ast/bv_decl_plugin.h"
@ -179,7 +179,8 @@ public:
tactic_report report("eq2bv", *g);
m_bounds(*g);
for (unsigned i = 0; i < g->size(); ++i)
m_bounds(g->form(i), g->dep(i), g->pr(i));
if (m_bounds.inconsistent() || g->proofs_enabled()) {
g->inc_depth();

View file

@ -24,7 +24,7 @@ Notes:
#include "ast/ast_util.h"
#include "ast/ast_pp_util.h"
#include "tactic/tactical.h"
#include "tactic/arith/bound_manager.h"
#include "ast/simplifiers/bound_manager.h"
#include "ast/converters/generic_model_converter.h"
class lia2card_tactic : public tactic {
@ -180,7 +180,8 @@ public:
tactic_report report("lia2card", *g);
bound_manager bounds(m);
bounds(*g);
for (unsigned i = 0; i < g->size(); ++i)
bounds(g->form(i), g->dep(i), g->pr(i));
for (expr* x : bounds) {
checkpoint();

View file

@ -17,7 +17,7 @@ Revision History:
--*/
#include "tactic/tactical.h"
#include "tactic/arith/bound_manager.h"
#include "ast/simplifiers/bound_manager.h"
#include "ast/rewriter/th_rewriter.h"
#include "ast/for_each_expr.h"
#include "ast/converters/generic_model_converter.h"
@ -197,7 +197,8 @@ class lia2pb_tactic : public tactic {
return;
}
m_bm(*g);
for (unsigned i = 0; i < g->size(); ++i)
m_bm(g->form(i), g->dep(i), g->pr(i));
TRACE("lia2pb", m_bm.display(tout););

View file

@ -28,7 +28,7 @@ Notes:
#include "tactic/arith/bv2int_rewriter.h"
#include "tactic/arith/bv2real_rewriter.h"
#include "ast/converters/generic_model_converter.h"
#include "tactic/arith/bound_manager.h"
#include "ast/simplifiers/bound_manager.h"
#include "util/obj_pair_hashtable.h"
#include "ast/ast_smt2_pp.h"
@ -89,7 +89,8 @@ class nla2bv_tactic : public tactic {
);
tactic_report report("nla->bv", g);
m_fmc = alloc(generic_model_converter, m_manager, "nla2bv");
m_bounds(g);
for (unsigned i = 0; i < g.size(); ++i)
m_bounds(g.form(i), g.dep(i), g.pr(i));
collect_power2(g);
switch (collect_vars(g)) {
case has_num:

View file

@ -19,7 +19,7 @@ Revision History:
--*/
#include "tactic/tactical.h"
#include "tactic/arith/bound_manager.h"
#include "ast/simplifiers/bound_manager.h"
#include "ast/rewriter/th_rewriter.h"
#include "ast/converters/generic_model_converter.h"
#include "ast/arith_decl_plugin.h"
@ -67,13 +67,11 @@ class normalize_bounds_tactic : public tactic {
}
bool has_lowers() {
bound_manager::iterator it = m_bm.begin();
bound_manager::iterator end = m_bm.end();
for (; it != end; ++it) {
for (auto* e : m_bm) {
TRACE("normalize_bounds_tactic",
rational val; bool strict;
tout << mk_ismt2_pp(*it, m) << " has_lower: " << m_bm.has_lower(*it, val, strict) << " val: " << val << "\n";);
if (is_target(*it))
tout << mk_ismt2_pp(e, m) << " has_lower: " << m_bm.has_lower(e, val, strict) << " val: " << val << "\n";);
if (is_target(e))
return true;
}
return false;
@ -83,8 +81,9 @@ class normalize_bounds_tactic : public tactic {
bool produce_models = in->models_enabled();
bool produce_proofs = in->proofs_enabled();
tactic_report report("normalize-bounds", *in);
m_bm(*in);
for (unsigned i = 0; i < in->size(); ++i)
m_bm(in->form(i), in->dep(i), in->pr(i));
if (!has_lowers()) {
result.push_back(in.get());

View file

@ -19,7 +19,7 @@ Notes:
#pragma once
#include "ast/converters/model_converter.h"
#include "tactic/arith/bound_manager.h"
#include "ast/simplifiers/bound_manager.h"
class pb2bv_model_converter : public model_converter {
typedef std::pair<func_decl *, func_decl *> func_decl_pair;

View file

@ -28,7 +28,7 @@ Notes:
#include "ast/rewriter/rewriter_def.h"
#include "ast/rewriter/pb2bv_rewriter.h"
#include "tactic/tactical.h"
#include "tactic/arith/bound_manager.h"
#include "ast/simplifiers/bound_manager.h"
#include "ast/converters/generic_model_converter.h"
#include "tactic/arith/pb2bv_model_converter.h"
#include "tactic/arith/pb2bv_tactic.h"
@ -913,7 +913,9 @@ private:
return;
}
m_bm(*g);
unsigned size = g->size();
for (unsigned i = 0; i < size; i++)
m_bm(g->form(i), g->dep(i), g->pr(i));
TRACE("pb2bv", m_bm.display(tout););
@ -924,7 +926,6 @@ private:
throw_tactic(p.e);
}
unsigned size = g->size();
expr_ref_vector new_exprs(m);
expr_dependency_ref_vector new_deps(m);
@ -1042,7 +1043,8 @@ struct is_pb_probe : public probe {
try {
ast_manager & m = g.m();
bound_manager bm(m);
bm(g);
for (unsigned i = 0; i < g.size(); i++)
bm(g.form(i), g.dep(i), g.pr(i));
arith_util a_util(m);
pb_util pb(m);
expr_fast_mark1 visited;

View file

@ -23,7 +23,7 @@ Notes:
#include "ast/converters/generic_model_converter.h"
#include "ast/ast_pp.h"
#include "model/model_smt2_pp.h"
#include "tactic/arith/bound_manager.h"
#include "ast/simplifiers/bound_manager.h"
#include "tactic/arith/bv2int_rewriter.h"
#include "ast/rewriter/expr_safe_replace.h"
#include "ast/bv_decl_plugin.h"
@ -330,9 +330,8 @@ private:
if (m_assertions.empty()) return;
m_flushed = true;
bound_manager& bm = *m_bounds.back();
for (expr* a : m_assertions) {
bm(a);
}
for (expr* a : m_assertions)
bm(a, nullptr, nullptr);
TRACE("int2bv", bm.display(tout););
expr_safe_replace sub(m);
accumulate_sub(sub);

View file

@ -32,14 +32,15 @@ Notes:
#include "tactic/aig/aig_tactic.h"
#include "tactic/smtlogics/smt_tactic.h"
#include "sat/tactic/sat_tactic.h"
#include "tactic/arith/bound_manager.h"
#include "ast/simplifiers/bound_manager.h"
#include "tactic/arith/probe_arith.h"
struct quasi_pb_probe : public probe {
result operator()(goal const & g) override {
bool found_non_01 = false;
bound_manager bm(g.m());
bm(g);
for (unsigned i = 0; i < g.size(); ++i)
bm(g.form(i), g.dep(i), g.pr(i));
rational l, u; bool st;
for (expr * t : bm) {
if (bm.has_lower(t, l, st) && bm.has_upper(t, u, st) && (l.is_zero() || l.is_one()) && (u.is_zero() || u.is_one()))