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

experimental feature to access congruence closure of SimpleSolver

This update includes an experimental feature to access a congruence closure data-structure after search.
It comes with several caveats as pre-processing is free to eliminate terms. It is therefore necessary to use a solver that does not eliminate the terms you want to track for congruence of. This is partially addressed by using SimpleSolver or incremental mode solving.

```python
from z3 import *
s = SimpleSolver()
x, y, z = Ints('x y z')
s.add(x == y)
s.add(y == z)
s.check()
print(s.root(x), s.root(y), s.root(z))
print(s.next(x), s.next(y), s.next(z))
```
This commit is contained in:
Nikolaj Bjorner 2022-12-30 21:41:27 -08:00
parent c0f1f33898
commit f6d411d54b
21 changed files with 145 additions and 12 deletions

View file

@ -81,7 +81,7 @@ public:
*
*/
bool operator()(func_decl* f, unsigned num, expr* const* args, expr_ref& r, expr_ref& side_cond) override {
bool operator()(func_decl* f, unsigned num, expr* const* args, expr_ref& r, proof_ref& pr) override {
SASSERT(f->get_family_id() == m.get_basic_family_id());
switch (f->get_decl_kind()) {
case OP_ITE:
@ -233,7 +233,7 @@ public:
}
bool operator()(func_decl* f, unsigned num, expr* const* args, expr_ref& r, expr_ref& side_cond) override {
bool operator()(func_decl* f, unsigned num, expr* const* args, expr_ref& r, proof_ref& pr) override {
SASSERT(f->get_family_id() == a.get_family_id());
switch (f->get_decl_kind()) {
case OP_ADD:
@ -531,7 +531,7 @@ class bv_expr_inverter : public iexpr_inverter {
* y := 0
*
*/
bool operator()(func_decl* f, unsigned num, expr* const* args, expr_ref& r, expr_ref& side_cond) override {
bool operator()(func_decl* f, unsigned num, expr* const* args, expr_ref& r, proof_ref& pr) override {
SASSERT(f->get_family_id() == bv.get_family_id());
switch (f->get_decl_kind()) {
case OP_BADD:
@ -611,7 +611,7 @@ public:
family_id get_fid() const override { return a.get_family_id(); }
bool operator()(func_decl* f, unsigned num, expr* const* args, expr_ref& r, expr_ref& side_cond) override {
bool operator()(func_decl* f, unsigned num, expr* const* args, expr_ref& r, proof_ref& pr) override {
SASSERT(f->get_family_id() == a.get_family_id());
switch (f->get_decl_kind()) {
case OP_SELECT:
@ -679,7 +679,7 @@ public:
* head(x) -> fresh
* x := cons(fresh, arb)
*/
bool operator()(func_decl* f, unsigned num, expr* const* args, expr_ref& r, expr_ref& side_cond) override {
bool operator()(func_decl* f, unsigned num, expr* const* args, expr_ref& r, proof_ref& pr) override {
if (dt.is_accessor(f)) {
SASSERT(num == 1);
if (uncnstr(args[0])) {
@ -799,7 +799,7 @@ expr_inverter::expr_inverter(ast_manager& m): iexpr_inverter(m) {
}
bool expr_inverter::operator()(func_decl* f, unsigned num, expr* const* args, expr_ref& new_expr, expr_ref& side_cond) {
bool expr_inverter::operator()(func_decl* f, unsigned num, expr* const* args, expr_ref& new_expr, proof_ref& pr) {
if (num == 0)
return false;
@ -812,7 +812,7 @@ bool expr_inverter::operator()(func_decl* f, unsigned num, expr* const* args, ex
return false;
auto* p = m_inverters.get(fid, nullptr);
return p && (*p)(f, num, args, new_expr, side_cond);
return p && (*p)(f, num, args, new_expr, pr);
}
bool expr_inverter::mk_diff(expr* t, expr_ref& r) {
@ -849,3 +849,10 @@ void expr_inverter::set_model_converter(generic_model_converter* mc) {
if (p)
p->set_model_converter(mc);
}
void expr_inverter::set_produce_proofs(bool pr) {
m_produce_proofs = pr;
for (auto* p : m_inverters)
if (p)
p->set_produce_proofs(pr);
}

View file

@ -24,6 +24,7 @@ protected:
ast_manager& m;
std::function<bool(expr*)> m_is_var;
generic_model_converter_ref m_mc;
bool m_produce_proofs = false;
bool uncnstr(expr* e) const { return m_is_var(e); }
bool uncnstr(unsigned num, expr * const * args) const;
@ -37,8 +38,9 @@ public:
virtual ~iexpr_inverter() {}
virtual void set_is_var(std::function<bool(expr*)>& is_var) { m_is_var = is_var; }
virtual void set_model_converter(generic_model_converter* mc) { m_mc = mc; }
virtual void set_produce_proofs(bool p) { m_produce_proofs = true; }
virtual bool operator()(func_decl* f, unsigned n, expr* const* args, expr_ref& new_expr, expr_ref& side_cond) = 0;
virtual bool operator()(func_decl* f, unsigned n, expr* const* args, expr_ref& new_expr, proof_ref& pr) = 0;
virtual bool mk_diff(expr* t, expr_ref& r) = 0;
virtual family_id get_fid() const = 0;
};
@ -49,9 +51,10 @@ class expr_inverter : public iexpr_inverter {
public:
expr_inverter(ast_manager& m);
~expr_inverter() override;
bool operator()(func_decl* f, unsigned n, expr* const* args, expr_ref& new_expr, expr_ref& side_cond) override;
bool operator()(func_decl* f, unsigned n, expr* const* args, expr_ref& new_expr, proof_ref& pr) override;
bool mk_diff(expr* t, expr_ref& r) override;
void set_is_var(std::function<bool(expr*)>& is_var) override;
void set_model_converter(generic_model_converter* mc) override;
void set_produce_proofs(bool p) override;
family_id get_fid() const override { return null_family_id; }
};

View file

@ -62,7 +62,8 @@ bool elim_unconstrained::is_var_lt(int v1, int v2) const {
void elim_unconstrained::eliminate() {
while (!m_heap.empty()) {
expr_ref r(m), side_cond(m);
expr_ref r(m);
proof_ref pr(m);
int v = m_heap.erase_min();
node& n = get_node(v);
if (n.m_refcount == 0)
@ -84,7 +85,7 @@ void elim_unconstrained::eliminate() {
unsigned sz = m_args.size();
for (expr* arg : *to_app(t))
m_args.push_back(reconstruct_term(get_node(arg)));
bool inverted = m_inverter(t->get_decl(), to_app(t)->get_num_args(), m_args.data() + sz, r, side_cond);
bool inverted = m_inverter(t->get_decl(), to_app(t)->get_num_args(), m_args.data() + sz, r, pr);
n.m_refcount = 0;
m_args.shrink(sz);
if (!inverted) {
@ -113,7 +114,7 @@ void elim_unconstrained::eliminate() {
IF_VERBOSE(11, verbose_stream() << mk_bounded_pp(get_node(v).m_orig, m) << " " << mk_bounded_pp(t, m) << " -> " << r << " " << get_node(e).m_refcount << "\n";);
SASSERT(!side_cond && "not implemented to add side conditions\n");
SASSERT(!pr && "not implemented to add proofs\n");
}
}