3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-07-31 08:23:17 +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:
Nikolaj Bjorner 2025-06-06 11:42:31 +02:00
parent 16452fec43
commit 564830ab31
6 changed files with 109 additions and 27 deletions

View file

@ -17,6 +17,7 @@ Author:
#pragma once
#include "util/scoped_vector.h"
#include "ast/simplifiers/dependent_expr_state.h"
#include "ast/euf/euf_egraph.h"
#include "ast/euf/euf_mam.h"
@ -24,6 +25,13 @@ Author:
namespace euf {
class side_condition_solver {
public:
virtual ~side_condition_solver() = default;
virtual void add_constraint(expr* f, expr_dependency* d) = 0;
virtual bool is_true(expr* f, expr_dependency*& d) = 0;
};
class completion : public dependent_expr_simplifier, public on_binding_callback, public mam_solver {
struct stats {
@ -32,6 +40,14 @@ namespace euf {
void reset() { memset(this, 0, sizeof(*this)); }
};
struct ground_rule {
expr_ref_vector m_body;
expr_ref m_head;
expr_dependency* m_dep;
ground_rule(expr_ref_vector& b, expr_ref& h, expr_dependency* d) :
m_body(b), m_head(h), m_dep(d) {}
};
egraph m_egraph;
scoped_ptr<mam> m_mam;
enode* m_tt, *m_ff;
@ -44,10 +60,11 @@ namespace euf {
unsigned_vector m_epochs;
th_rewriter m_rewriter;
stats m_stats;
scoped_ptr<side_condition_solver> m_side_condition_solver;
ptr_vector<ground_rule> m_rules;
bool m_has_new_eq = false;
bool m_should_propagate = false;
enode* mk_enode(expr* e);
bool is_new_eq(expr* a, expr* b);
void update_has_new_eq(expr* g);
@ -65,9 +82,17 @@ namespace euf {
expr_dependency* explain_conflict();
expr_dependency* get_dependency(quantifier* q) { return m_q2dep.contains(q) ? m_q2dep[q] : nullptr; }
lbool eval_cond(expr* f, expr_dependency*& d);
lbool check_rule(ground_rule& rule);
void check_rules();
void add_rule(expr* f, expr_dependency* d);
void reset_rules();
bool is_gt(expr* a, expr* b) const;
public:
completion(ast_manager& m, dependent_expr_state& fmls);
~completion() override;
char const* name() const override { return "euf-reduce"; }
void push() override { m_egraph.push(); dependent_expr_simplifier::push(); }
void pop(unsigned n) override { dependent_expr_simplifier::pop(n); m_egraph.pop(n); }
@ -84,5 +109,7 @@ namespace euf {
void on_binding(quantifier* q, app* pat, enode* const* binding, unsigned mg, unsigned ming, unsigned mx) override;
void set_solver(side_condition_solver* s) { m_side_condition_solver = s; }
};
}