mirror of
https://github.com/Z3Prover/z3
synced 2025-08-04 02:10:23 +00:00
enable conditional euf-completion with (optional) solver
This allows using z3 for limited E-saturation simplification. The tactic rewrites all assertions using the E-graph induced by the equalities and instantiated equality axioms. It does allow solving with conditionals, although this is a first inefficient cut. The following is a sample use case that rewrites to false. ``` (declare-fun prime () Int) (declare-fun add (Int Int) Int) (declare-fun mul (Int Int) Int) (declare-fun ^ (Int Int) Int) (declare-fun sub (Int Int) Int) (declare-fun i () Int) (declare-fun j () Int) (declare-fun base () Int) (declare-fun S () (Seq Int)) (declare-fun hash ((Seq Int) Int Int Int Int) Int) (assert (let ((a!1 (mul (seq.nth S i) (^ base (sub (sub j i) 1))))) (let ((a!2 (mod (add (hash S base prime (add i 1) j) a!1) prime))) (not (= (hash S base prime i j) a!2))))) (assert (forall ((x Int)) (! (= (mod (mod x prime) prime) (mod x prime)) :pattern ((mod (mod x prime) prime))))) (assert (forall ((x Int) (y Int)) (! (= (mod (mul x y) prime) (mod (mul (mod x prime) y) prime)) :pattern ((mod (mul x y) prime)) :pattern ((mod (mul (mod x prime) y) prime))))) (assert (forall ((x Int) (y Int)) (! (= (mod (mul x y) prime) (mod (mul x (mod y prime)) prime)) :pattern ((mod (mul x y) prime)) :pattern ((mod (mul x (mod y prime)) prime))))) (assert (forall ((x Int) (y Int)) (! (= (mod (add x y) prime) (mod (add x (mod y prime)) prime)) :pattern ((mod (add x y) prime)) :pattern ((mod (add x (mod y prime)) prime))))) (assert (forall ((x Int) (y Int)) (! (= (mod (add x y) prime) (mod (add (mod x prime) y) prime)) :pattern ((mod (add x y) prime)) :pattern ((mod (add (mod x prime) y) prime))))) (assert (forall ((x Int) (y Int)) (! (= (mul x (^ x y)) (^ x (add y 1))) :pattern ((mul x (^ x y)))))) (assert (forall ((x Int) (y Int)) (! (= (mul x y) (mul y x)) :pattern ((mul x y))))) (assert (forall ((x Int) (y Int)) (! (= (add x y) (add y x)) :pattern ((add x y))))) (assert (forall ((x Int) (y Int)) (! (= (mul x y) (mul y x)) :pattern ((mul x y))))) (assert (forall ((x Int) (y Int) (z Int)) (! (= (add x (add y z)) (add (add x y) z)) :pattern ((add x (add y z))) :pattern ((add (add x y) z))))) (assert (forall ((x Int) (y Int) (z Int)) (! (= (mul x (mul y z)) (mul (mul x y) z)) :pattern ((mul x (mul y z))) :pattern ((mul (mul x y) z))))) (assert (forall ((x Int) (y Int) (z Int)) (! (= (sub (sub x y) z) (sub (sub x z) y)) :pattern ((sub (sub x y) z))))) (assert (forall ((x Int) (y Int) (z Int)) (! (= (mul x (add y z)) (add (mul x y) (mul x z))) :pattern ((mul x (add y z)))))) (assert (forall ((x Int)) (! (= (sub (add x 1) 1) x) :pattern ((add x 1))))) (assert (forall ((x Int)) (! (= (add (sub x 1) 1) x) :pattern ((sub x 1))))) (assert (let ((a!1 (^ base (sub (sub (sub j 1) i) 1)))) (let ((a!2 (mod (add (hash S base prime (add i 1) (sub j 1)) (mul (seq.nth S i) a!1)) prime))) (= (hash S base prime i (sub j 1)) a!2)))) (assert (let ((a!1 (add (seq.nth S (- j 1)) (mul base (hash S base prime i (sub j 1)))))) (= (hash S base prime i j) (mod a!1 prime)))) (assert (let ((a!1 (add (seq.nth S (- j 1)) (mul base (hash S base prime (add i 1) (sub j 1)))))) (= (hash S base prime (add i 1) j) (mod a!1 prime)))) (apply euf-completion) ``` To use conditional rewriting you can ``` (assert (not (= 0 prime))) ``` and update axioms using modulus with prime to be of the form: ``` (=> (not (= 0 prime)) <original-body of quantifier>) ```
This commit is contained in:
parent
16452fec43
commit
564830ab31
6 changed files with 109 additions and 27 deletions
|
@ -8,7 +8,6 @@ z3_add_component(core_tactics
|
|||
der_tactic.cpp
|
||||
elim_term_ite_tactic.cpp
|
||||
elim_uncnstr_tactic.cpp
|
||||
euf_completion_tactic.cpp
|
||||
injectivity_tactic.cpp
|
||||
nnf_tactic.cpp
|
||||
occf_tactic.cpp
|
||||
|
@ -38,7 +37,6 @@ z3_add_component(core_tactics
|
|||
elim_uncnstr_tactic.h
|
||||
elim_uncnstr2_tactic.h
|
||||
eliminate_predicates_tactic.h
|
||||
euf_completion_tactic.h
|
||||
injectivity_tactic.h
|
||||
nnf_tactic.h
|
||||
occf_tactic.h
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
/*++
|
||||
Copyright (c) 2022 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
euf_completion_tactic.cpp
|
||||
|
||||
Abstract:
|
||||
|
||||
Tactic for simplifying with equations.
|
||||
|
||||
Author:
|
||||
|
||||
Nikolaj Bjorner (nbjorner) 2022-10-30
|
||||
|
||||
--*/
|
||||
|
||||
#include "tactic/tactic.h"
|
||||
#include "tactic/core/euf_completion_tactic.h"
|
||||
|
||||
tactic * mk_euf_completion_tactic(ast_manager& m, params_ref const& p) {
|
||||
return alloc(dependent_expr_state_tactic, m, p,
|
||||
[](auto& m, auto& p, auto &s) -> dependent_expr_simplifier* { return alloc(euf::completion, m, s); });
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
z3_add_component(portfolio
|
||||
SOURCES
|
||||
euf_completion_tactic.cpp
|
||||
default_tactic.cpp
|
||||
smt_strategic_solver.cpp
|
||||
solver2lookahead.cpp
|
||||
|
@ -16,6 +17,7 @@ z3_add_component(portfolio
|
|||
ufbv_tactic
|
||||
fd_solver
|
||||
TACTIC_HEADERS
|
||||
euf_completion_tactic.h
|
||||
default_tactic.h
|
||||
solver_subsumption_tactic.h
|
||||
|
||||
|
|
79
src/tactic/portfolio/euf_completion_tactic.cpp
Normal file
79
src/tactic/portfolio/euf_completion_tactic.cpp
Normal file
|
@ -0,0 +1,79 @@
|
|||
/*++
|
||||
Copyright (c) 2022 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
euf_completion_tactic.cpp
|
||||
|
||||
Abstract:
|
||||
|
||||
Tactic for simplifying with equations.
|
||||
|
||||
Author:
|
||||
|
||||
Nikolaj Bjorner (nbjorner) 2022-10-30
|
||||
|
||||
--*/
|
||||
|
||||
#include "tactic/tactic.h"
|
||||
#include "tactic/portfolio/euf_completion_tactic.h"
|
||||
#include "solver/solver.h"
|
||||
|
||||
class euf_side_condition_solver : public euf::side_condition_solver {
|
||||
ast_manager& m;
|
||||
params_ref m_params;
|
||||
scoped_ptr<solver> m_solver;
|
||||
expr_ref_vector m_deps;
|
||||
obj_map<expr, expr_dependency*> m_e2d;
|
||||
void init_solver() {
|
||||
if (m_solver.get())
|
||||
return;
|
||||
m_params.set_uint("smt.max_conflicts", 100);
|
||||
scoped_ptr<solver_factory> f = mk_smt_strategic_solver_factory();
|
||||
m_solver = (*f)(m, m_params, false, false, true, symbol::null);
|
||||
}
|
||||
public:
|
||||
euf_side_condition_solver(ast_manager& m, params_ref const& p) : m(m), m_params(p), m_deps(m) {}
|
||||
|
||||
void add_constraint(expr* f, expr_dependency* d) override {
|
||||
if (!is_ground(f))
|
||||
return;
|
||||
init_solver();
|
||||
expr* e_dep = nullptr;
|
||||
if (d) {
|
||||
e_dep = m.mk_fresh_const("dep", m.mk_bool_sort());
|
||||
m_deps.push_back(e_dep);
|
||||
m_e2d.insert(e_dep, d);
|
||||
}
|
||||
m_solver->assert_expr(f, e_dep);
|
||||
}
|
||||
|
||||
bool is_true(expr* f, expr_dependency*& d) override {
|
||||
d = nullptr;
|
||||
m_solver->push();
|
||||
expr_ref_vector fmls(m);
|
||||
fmls.push_back(m.mk_not(f));
|
||||
expr_ref nf(m.mk_not(f), m);
|
||||
lbool r = m_solver->check_sat(fmls);
|
||||
if (r == l_false) {
|
||||
expr_ref_vector core(m);
|
||||
m_solver->get_unsat_core(core);
|
||||
for (auto c : core)
|
||||
d = m.mk_join(d, m_e2d[c]);
|
||||
}
|
||||
m_solver->pop(1);
|
||||
return r == l_false;
|
||||
}
|
||||
};
|
||||
|
||||
static euf::completion* mk_completion(ast_manager& m, dependent_expr_state& s, params_ref const& p) {
|
||||
auto r = alloc(euf::completion, m, s);
|
||||
auto scs = alloc(euf_side_condition_solver, m, p);
|
||||
r->set_solver(scs);
|
||||
return r;
|
||||
}
|
||||
|
||||
tactic * mk_euf_completion_tactic(ast_manager& m, params_ref const& p) {
|
||||
return alloc(dependent_expr_state_tactic, m, p,
|
||||
[](auto& m, auto& p, auto &s) -> dependent_expr_simplifier* { return alloc(euf::completion, m, s); });
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue