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

QEL: Fast Approximated Quantifier Elimination (#6820)

* qe_lite: cleanup and comment

no change to code

* mbp_arrays: refactor out partial equality (peq)

Partial array equality, PEQ, is used as an intermediate
expression during MBP for arrays. We need to factor it out
so that it can be shared between MBP-QEL and existing MBP.

Partial array equality (peq) is used in MBP for arrays.
Factoring this out to be used by multiple MBP implementations.

* rewriter: new rewrite rules

These rules are specializes for terms that are created in QEL.
QEL commit is comming later

* datatype_rw: new rewrite rule for ADTs

The rule handles this special case:

    (cons (head x) (tail x)) --> x

* array_rewriter rules for rewriting PEQs

Special rules to simplify PEQs

* th_rewriter: wire PEQ simplifications

* spacer_iuc: avoid terms with default in IUC

Spacer prfers to not have a term representing default value of an array.
This guides IUC from picking such terms in interpolation

* mbp_term_graph: replace root with repr

* mbp_term_graph: formatting

* mbp_term_graph: class_props, getters, setters

Class properties allow to keep information for an equivalence class.

Getters and setters for terms allow accessing information

* mbp_term_graph: auxiliary methods for qel

QEL commit is comming later in the history

* mbp_term_graph: bug fix

* mbp_term_graph: pick, refine repr, compute cgrnd

* mbp_term_graph: internalize deq

* mbp_term_graph: constructor

* mbp_term_graph: optionally internalize equalities

Reperesent equalities explicitly by nodes in the term_graph

* qel

* formatting

* comments on term_lt

* get terms and other api for mbp_qel

* plugins for mbp_qel

* mbp_qel_util: utilities for mbp_qel

* qe_mbp: QEL-based mbp

* qel: expose QEL API

* spacer: replace qe_lite in qe_project_spacer by qel

This changes the default projection engine that spacer uses.

* cmd_context: debug commands for qel and mbp_qel

New commands are

  mbp-qel -- MBP with term graphs
  qel     -- QEL with term graphs
  qe-lite -- older qelite

* qe_mbp: model-based rewriters for arrays

* qe_mbp: QEL-based projection functions

* qsat: wire in QEL-based mbp

* qsat: debug code

* qsat: maybe a bug fix

Changed the code to follow the paper by adding all predicates above a given
level, not just predicates of immediately preceding level.

* chore: use new api to create solver in qsat

* mbp_term_graph use all_of idiom

* feat: solver for integer multiplication

* array_peq: formatting, no change to code

* mbp_qel_util: block comment + format

* mbt_term_graph: clang-format

* bug fix. Move dt rewrite to qe_mbp

* array_peq: add header

* run clang format on mbp plugins

* clang format on mul solver

* format do-while

* format

* format do-while

* update release notes

---------

Co-authored-by: hgvk94 <hgvk94@gmail.com>
Co-authored-by: Isabel Garcia <igarciac@uwaterloo.ca>
This commit is contained in:
Arie Gurfinkel 2023-08-02 12:34:06 -04:00 committed by GitHub
parent 5b2519d7a3
commit 51d3c279d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
35 changed files with 4170 additions and 1517 deletions

View file

@ -3,6 +3,7 @@ z3_add_component(ast
act_cache.cpp
arith_decl_plugin.cpp
array_decl_plugin.cpp
array_peq.cpp
ast.cpp
ast_ll_pp.cpp
ast_lt.cpp

107
src/ast/array_peq.cpp Normal file
View file

@ -0,0 +1,107 @@
/*++
Copyright (c) 2023 Microsoft Corporation
Module Name:
array_peq.cpp
Abstract:
Partial equality for arrays
Author:
Nikolaj Bjorner (nbjorner) 2015-06-13
Hari Govind V K
Revision History:
--*/
#include "ast/array_peq.h"
#define PARTIAL_EQ "!partial_eq"
bool is_partial_eq(const func_decl *f) {
SASSERT(f);
return f->get_name() == PARTIAL_EQ;
}
bool is_partial_eq(const app *a) {
SASSERT(a);
return is_partial_eq(a->get_decl());
}
app_ref mk_peq(expr *e0, expr *e1, vector<expr_ref_vector> const &indices,
ast_manager &m) {
peq p(e0, e1, indices, m);
return p.mk_peq();
}
app_ref peq::mk_eq(app_ref_vector &aux_consts, bool stores_on_rhs) {
if (!m_eq) {
expr_ref lhs(m_lhs, m), rhs(m_rhs, m);
if (!stores_on_rhs) { std::swap(lhs, rhs); }
// lhs = (...(store (store rhs i0 v0) i1 v1)...)
sort *val_sort = get_array_range(lhs->get_sort());
for (expr_ref_vector const &diff : m_diff_indices) {
ptr_vector<expr> store_args;
store_args.push_back(rhs);
store_args.append(diff.size(), diff.data());
app_ref val(m.mk_fresh_const("diff", val_sort), m);
store_args.push_back(val);
aux_consts.push_back(val);
rhs = m_arr_u.mk_store(store_args);
}
m_eq = m.mk_eq(lhs, rhs);
}
return m_eq;
}
app_ref peq::mk_peq() {
if (!m_peq) {
ptr_vector<expr> args;
args.push_back(m_lhs);
args.push_back(m_rhs);
for (auto const &v : m_diff_indices) {
args.append(v.size(), v.data());
}
m_peq = m.mk_app(m_decl, args.size(), args.data());
}
return m_peq;
}
peq::peq(expr *lhs, expr *rhs, vector<expr_ref_vector> const &diff_indices,
ast_manager &m)
: m(m), m_lhs(lhs, m), m_rhs(rhs, m), m_diff_indices(diff_indices),
m_decl(m), m_peq(m), m_eq(m), m_arr_u(m) {
SASSERT(m_arr_u.is_array(lhs));
SASSERT(m_arr_u.is_array(rhs));
SASSERT(lhs->get_sort() == rhs->get_sort());
ptr_vector<sort> sorts;
sorts.push_back(m_lhs->get_sort());
sorts.push_back(m_rhs->get_sort());
for (auto const &v : diff_indices) {
SASSERT(v.size() == get_array_arity(m_lhs->get_sort()));
for (expr *e : v) sorts.push_back(e->get_sort());
}
m_decl = m.mk_func_decl(symbol(PARTIAL_EQ), sorts.size(), sorts.data(),
m.mk_bool_sort());
}
peq::peq(app *p, ast_manager &m)
: m(m), m_lhs(p->get_arg(0), m), m_rhs(p->get_arg(1), m),
m_decl(p->get_decl(), m), m_peq(p, m), m_eq(m), m_arr_u(m),
m_name(symbol(PARTIAL_EQ)) {
SASSERT(is_partial_eq(p));
SASSERT(m_arr_u.is_array(m_lhs));
SASSERT(m_arr_u.is_array(m_rhs));
SASSERT(m_lhs->get_sort() == m_rhs->get_sort());
unsigned arity = get_array_arity(m_lhs->get_sort());
for (unsigned i = 2; i < p->get_num_args(); i += arity) {
SASSERT(arity + i <= p->get_num_args());
expr_ref_vector vec(m);
vec.append(arity, p->get_args() + i);
m_diff_indices.push_back(std::move(vec));
}
}

91
src/ast/array_peq.h Normal file
View file

@ -0,0 +1,91 @@
/*++
Copyright (c) 2023 Microsoft Corporation
Module Name:
array_peq.h
Abstract:
Partial equality for arrays
Author:
Nikolaj Bjorner (nbjorner) 2015-06-13
Hari Govind V K
Revision History:
--*/
#pragma once
#include "ast/array_decl_plugin.h"
#include "ast/ast.h"
/**
* \brief utility class for partial equalities
*
* A partial equality (a ==I b), for two arrays a, b and a finite set of indices
* I holds iff (forall i :: i \not\in I => a[i] == b[i]). In other words, peq is
* a restricted form of the extensionality axiom
*
* Using this class, we denote (a =I b) as f(a,b,i0,i1,...),
* where f is an uninterpreted predicate with the name PARTIAL_EQ and
* I = {i0,i1,...}
*/
class peq {
ast_manager &m;
expr_ref m_lhs;
expr_ref m_rhs;
vector<expr_ref_vector> m_diff_indices;
func_decl_ref m_decl; // the partial equality declaration
app_ref m_peq; // partial equality application
app_ref m_eq; // equivalent std equality using def. of partial eq
array_util m_arr_u;
symbol m_name;
public:
peq(app *p, ast_manager &m);
peq(expr *lhs, expr *rhs, vector<expr_ref_vector> const &diff_indices,
ast_manager &m);
expr_ref lhs() { return m_lhs; }
expr_ref rhs() { return m_rhs; }
void get_diff_indices(vector<expr_ref_vector> &result) {
result.append(m_diff_indices);
}
/** Convert peq into a peq expression */
app_ref mk_peq();
/** Convert peq into an equality
For peq of the form (a =I b) returns (a = b[i0 := v0, i1 := v1, ...])
where i0, i1 \in I, and v0, v1 are fresh skolem constants
Skolems are returned in aux_consts
The left and right hand arguments are reversed when stores_on_rhs is
false
*/
app_ref mk_eq(app_ref_vector &aux_consts, bool stores_on_rhs = true);
};
/**
* mk (e0 ==indices e1)
*
* result has stores if either e0 or e1 or an index term has stores
*/
app_ref mk_peq(expr *e0, expr *e1, vector<expr_ref_vector> const &indices,
ast_manager &m);
bool is_partial_eq(const func_decl *f);
bool is_partial_eq(const app *a);
inline bool is_peq(const func_decl *f) { return is_partial_eq(f); }
inline bool is_peq(const app *a) { return is_partial_eq(a); }

View file

@ -24,6 +24,7 @@ Notes:
#include "ast/rewriter/var_subst.h"
#include "params/array_rewriter_params.hpp"
#include "util/util.h"
#include "ast/array_peq.h"
void array_rewriter::updt_params(params_ref const & _p) {
array_rewriter_params p(_p);
@ -40,8 +41,48 @@ void array_rewriter::get_param_descrs(param_descrs & r) {
}
br_status array_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) {
br_status st = BR_FAILED;
// BEGIN: rewrite rules for PEQs
if (is_partial_eq(f)) {
SASSERT(num_args >= 2);
expr *e0, *e1;
e0 = args[0];
e1 = args[1];
expr_ref a(m()), val(m());
expr_ref_vector vindex(m());
if (e0 == e1) {
// t peq t --> true
result = m().mk_true();
st = BR_DONE;
}
else if (m_util.is_store_ext(e0, a, vindex, val)) {
if (num_args == 2 && a == e1) {
// (a[i := x] peq_{\emptyset} a) ---> a[i] == x
mk_select(vindex.size(), vindex.data(), result);
result = m().mk_eq(result, val);
st = BR_REWRITE_FULL;
}
else if (a == e1 && vindex.size() == num_args + 2) {
// a [i: = x] peq_{i} a -- > true
bool all_eq = true;
for (unsigned i = 0, sz = vindex.size(); all_eq && i < sz;
++i) {
all_eq &= vindex.get(i) == args[2+i];
}
if (all_eq) {
result = m().mk_true();
st = BR_DONE;
}
}
}
return st;
}
// END: rewrite rules for PEQs
SASSERT(f->get_family_id() == get_fid());
br_status st;
switch (f->get_decl_kind()) {
case OP_SELECT:
st = mk_select_core(num_args, args, result);

View file

@ -21,7 +21,8 @@ Notes:
br_status datatype_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) {
SASSERT(f->get_family_id() == get_fid());
switch(f->get_decl_kind()) {
case OP_DT_CONSTRUCTOR: return BR_FAILED;
case OP_DT_CONSTRUCTOR:
return BR_FAILED;
case OP_DT_RECOGNISER:
SASSERT(num_args == 1);
result = m_util.mk_is(m_util.get_recognizer_constructor(f), args[0]);

View file

@ -39,6 +39,7 @@ Notes:
#include "ast/ast_util.h"
#include "ast/well_sorted.h"
#include "ast/for_each_expr.h"
#include "ast/array_peq.h"
namespace {
struct th_rewriter_cfg : public default_rewriter_cfg {
@ -644,6 +645,10 @@ struct th_rewriter_cfg : public default_rewriter_cfg {
else
st = pull_ite(result);
}
if (st == BR_FAILED && f->get_family_id() == null_family_id && is_partial_eq(f)) {
st = m_ar_rw.mk_app_core(f, num, args, result);
}
CTRACE("th_rewriter_step", st != BR_FAILED,
tout << f->get_name() << "\n";
for (unsigned i = 0; i < num; i++) tout << mk_ismt2_pp(args[i], m()) << "\n";