3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-10-09 09:21:56 +00:00
z3/src/muz/spacer/spacer_util.h
Arie Gurfinkel d2b618df23
Spacer Global Guidance (#6026)
* Make spacer_sem_matcher::reset() public

* Add .clang-format for src/muz/spacer

* Mark substitution::get_bindings() as const

* Fix in spacer_antiunify

* Various helper methods in spacer_util

Minor functions to compute number of free variables, detect presence of certain
sub-expressions, etc.

The diff is ugly because of clang-format

* Add spacer_cluster for clustering lemmas

A cluster of lemmas is a set of lemmas that are all instances of the same
pattern, where a pattern is a qff formula with free variables.

Currently, the instances are required to be explicit, that is, they are all
obtained by substituting concrete values (i.e., numbers) for free variables of
the pattern.

Lemmas are clustered in cluster_db in each predicate transformer.

* Integrate spacer_cluster into spacer_context

* Custom clang-format pragmas for spacer_context

spacer_context.(cpp|h) are large and have inconsistent formatting. Disable
clang-format for them until merge with main z3 branch and re-format.

* Computation of convex closure and matrix kernel

Various LA functions. The implementations are somewhat preliminary.

Convex closure is simplemented via syntactic convex closure procedure.
Kernel computation considers many common cases.

spacer_arith_kernel_sage implements kernel computation by call external
Sage binary. It is used only for debugging and experiments. There is no
link dependence on Sage. If desired, it can be removed.

* Add spacer_concretize

* Utility methods for spacer conjecture rule

* Add spacer_expand_bnd_generalizer

Generalizes arithmetic inequality literals of the form x <= c,
by changing constant c to other constants found in the problem.

* Add spacer_global_generalizer

Global generalizer checks every new lemma against a cluster
of previously learned lemmas, and, if possible, conjectures
a new pob, that, when blocked, generalizes multiple existing
lemmas.

* Remove fp.spacer.print_json option

The option is used to dump state of spacer into json for debugging.

It has been replaced by `fp.spacer.trace_file` that allows dumping an execution
of spacer. The json file can be reconstructed from the trace file elsewhere.

* Workaround for segfault in spacer_proof_utils

Issue #3 in hgvk94/z3

Segfault in some proof reduction. Avoid by bailing out on reduction.

* Revert bug for incomplete models

* Use local fresh variables in spacer_global_generalizer

* Cleanup of spacer_convex_closure

* Allow arbitrary expressions to name cols in convex_closure

* WIP: convex closure

* WIP: convex closure

* Fix bindings order in spacer_global_generalizer

The matcher creates substitution using std_order, which is
reverse of expected order (variable 0 is last). Adjust the code
appropriately for that.

* Increase verbosity level for smt_context stats

* Dead code in qe_mbp

* bug fixes in spacer_global_generalizer::subsumer

* Partially remove dependence of size of m_alphas

I want m_alphas to potentially be greater than currently used alpha variables.
This is helpful for reusing them across multiple calls to convex closure

* Subtle bug in kernel computation

Coefficient was being passed by reference and, therefore, was
being changed indirectly.

In the process, updated the code to be more generic to avoid rational
computation in the middle of matrix manipulation.

* another test for sparse_matrix_ops::kernel

* Implementation of matrix kernel using Fraction Free Elimination

Ensures that the kernel is int for int matrices. All divisions are exact.

* clang-format sparse_matrix_ops.h

* another implementation of ffe kernel in sparse_matrix_ops

* Re-do arith_kernel and convex_closure

* update spacer_global_generalization for new subsumer

* remove spacer.gg.use_sage parameter

* cleanup of spacer_global_generalizer

* Removed dependency on sage

* fix in spacer_convex_closure

* spacer_sem_matcher: consider an additional semantic matching

disabled until it is shown useful

* spacer_global_generalizer: improve do_conjecture

 - if conjecture does not apply to pob, use lemma instead
 - better normalization
 - improve debug prints

* spacer_conjecture: formatting

* spacer_cluster: improve debug prints

* spacer_context: improve debug prints

* spacer_context: re-queue may pobs

enabled even if global re-queue is disabled

* spacer_cluster print formatting

* reset methods on pob

* cleanup of print and local variable names

* formatting

* reset generalization data once it has been used

* refactored extra pob creation during global guidance

* fix bug copying sparse matrix into spacer matrix

* bug fix in spacer_convex_closure

* formatting change in spacer_context

* spacer_cluster: get_min_lvl

chose level based on pob as well as lemmas

* spacer_context: add desired_level to pob

desired_level indicates at which level pob should be proved.
A pob will be pushed to desired_level if necessary

* spacer_context: renamed subsume stats

the name of success/failed was switched

* spacer_convex_closure: fix prototype of is_congruent_mod()

* spacer_convex_closure: hacks in infer_div_pred()

* spacer_util: do not expand literals with mod

By default, equality literal t=p is expanded into t<=p && t>=p

Disable the expansion in case t contains 'mod' operator since such
expansion is usually not helpful for divisibility

* spacer_util: rename m_util into m_arith

* spacer_util: cleanup normalize()

* spacer_util: formatting

* spacer_context: formatting cleanup on subsume and conjecture

* spacer_context: fix handling may pobs when abs_weakness is enabled

A pob might be undef, so weakness must be bumped up

* spacer_arith_kernel: enhance debug print

* spacer_global_generalizer: improve matching on conjecture

* spacer_global_generalizer: set desired level on conjecture pob

* spacer_global_generalizer: debug print

* spacer_global_generalizer: set min level on new pobs

the new level should not be higher than the pob that was generalized

* spacer_global_generalizer: do no re-create closed pobs

If a generalized pob exist and closed, do not re-create it.

* spacer_context: normalize twice

* spacer_context: forward propagate only same kind of pobs

* sketch of inductive generalizer

A better implementation of inductive generalizer that in addition to dropping
literals also attempts to weaken them.

Current implementation is a sketch to be extended based on examples/requirements.

* fix ordering in spacer_cluster_util

* fix resetting of substitution matcher in spacer_conjecture

Old code would forget to reset the substitution provided to the sem_matcher.
Thus, if the substitution was matched once (i.e., one literal of interest is
found), no other literal would be matched.

* add spacer_util is_normalized() method

used for debugging only

* simplify normalization of pob expressions

pob expressions are normalized to increase syntactic matching.
Some of the normalization rules seem out of place, so removing them for now.

* fix in spacer_global_generalizer

If conjecture fails, do not try other generalization strategies -- they will not apply.

* fix in spacer_context

do not check that may pob is blocked by existing lemmas.
It is likely to be blocked. Our goal is to block it again and generalize
to a new lemma.

This can be further improved by moving directly to generalization when pob is
blocked by existing lemmas...

Co-authored-by: hgvk94 <hgvk94@gmail.com>
2022-08-30 15:47:00 -07:00

183 lines
5.2 KiB
C++

/*++
Copyright (c) 2011 Microsoft Corporation
Module Name:
spacer_util.h
Abstract:
Utility functions for SPACER.
Author:
Krystof Hoder (t-khoder) 2011-8-19.
Arie Gurfinkel
Anvesh Komuravelli
Revision History:
--*/
#pragma once
#include "ast/arith_decl_plugin.h"
#include "ast/array_decl_plugin.h"
#include "ast/ast.h"
#include "ast/ast_pp.h"
#include "ast/ast_util.h"
#include "ast/bv_decl_plugin.h"
#include "ast/expr_map.h"
#include "model/model.h"
#include "util/obj_hashtable.h"
#include "util/ref_vector.h"
#include "util/trace.h"
#include "util/vector.h"
#include "muz/spacer/spacer_antiunify.h"
#include "util/stopwatch.h"
class model;
class model_core;
namespace spacer {
inline unsigned infty_level() { return UINT_MAX; }
inline bool is_infty_level(unsigned lvl) {
// XXX: level is 16 bits in class pob
return lvl >= 65535;
}
inline unsigned next_level(unsigned lvl) {
return is_infty_level(lvl) ? lvl : (lvl + 1);
}
inline unsigned prev_level(unsigned lvl) {
if (is_infty_level(lvl)) return infty_level();
if (lvl == 0) return 0;
return lvl - 1;
}
struct pp_level {
unsigned m_level;
pp_level(unsigned l) : m_level(l) {}
};
inline std::ostream &operator<<(std::ostream &out, pp_level const &p) {
if (is_infty_level(p.m_level)) {
return out << "oo";
} else {
return out << p.m_level;
}
}
typedef ptr_vector<app> app_vector;
typedef ptr_vector<func_decl> decl_vector;
typedef obj_hashtable<func_decl> func_decl_set;
/**
\brief hoist non-boolean if expressions.
*/
void to_mbp_benchmark(std::ostream &out, const expr *fml,
const app_ref_vector &vars);
// TBD: deprecate by qe::mbp
/**
* do the following in sequence
* 1. use qe_lite to cheaply eliminate vars
* 2. for remaining boolean vars, substitute using M
* 3. use MBP for remaining array and arith variables
* 4. for any remaining arith variables, substitute using M
*/
void qe_project(ast_manager &m, app_ref_vector &vars, expr_ref &fml, model &mdl,
bool reduce_all_selects = false, bool native_mbp = false,
bool dont_sub = false);
// deprecate
void qe_project(ast_manager &m, app_ref_vector &vars, expr_ref &fml,
model_ref &M, expr_map &map);
// TBD: sort out
void expand_literals(ast_manager &m, expr_ref_vector &conjs);
expr_ref_vector compute_implicant_literals(model &mdl,
expr_ref_vector &formula);
void simplify_bounds(expr_ref_vector &lemmas);
bool is_normalized(expr_ref e, bool use_simplify_bounds = true,
bool factor_eqs = false);
void normalize(expr *e, expr_ref &out, bool use_simplify_bounds = true,
bool factor_eqs = false);
void normalize_order(expr *e, expr_ref &out);
/**
* Ground expression by replacing all free variables by skolem
* constants. On return, out is the resulting expression, and vars is
* a map from variable ids to corresponding skolem constants.
*/
void ground_expr(expr *e, expr_ref &out, app_ref_vector &vars);
void mbqi_project(model &mdl, app_ref_vector &vars, expr_ref &fml);
bool contains_selects(expr *fml, ast_manager &m);
void get_select_indices(expr *fml, app_ref_vector &indices);
void find_decls(expr *fml, app_ref_vector &decls, std::string &prefix);
/**
* extended pretty-printer
* used for debugging
* disables aliasing of common sub-expressions
*/
struct mk_epp : public mk_pp {
params_ref m_epp_params;
expr_ref m_epp_expr;
mk_epp(ast *t, ast_manager &m, unsigned indent = 0, unsigned num_vars = 0,
char const *var_prefix = nullptr);
void rw(expr *e, expr_ref &out);
};
bool is_clause(ast_manager &m, expr *n);
bool is_literal(ast_manager &m, expr *n);
bool is_atom(ast_manager &m, expr *n);
// set f to true in model
void set_true_in_mdl(model &model, func_decl *f);
/// Returns number of free variables in \p e
unsigned get_num_vars(expr *e);
// Return all uninterpreted constants of \p q
void collect_uninterp_consts(expr *a, expr_ref_vector &out);
bool has_nonlinear_mul(expr *e, ast_manager &m);
// Returns true if \p e contains a multiplication a variable by not-a-number
bool has_nonlinear_var_mul(expr *e, ast_manager &m);
// check whether lit is an instance of mono_var_pattern
bool is_mono_var(expr *lit, ast_manager &m, arith_util &a_util);
// a mono_var_pattern has only one variable in the whole expression and is
// linear. lit is the literal with the variable
bool find_unique_mono_var_lit(const expr_ref &p, expr_ref &lit);
/// Drop all literals that numerically match \p lit, from \p fml_vec.
///
/// \p abs_fml holds the result. Returns true if any literal has been dropped
bool filter_out_lit(const expr_ref_vector &in, const expr_ref &lit,
expr_ref_vector &out);
/// Returns true if range of s is numeric
bool is_numeric_sub(const substitution &s);
// Returns true if \p e contains \p mod
bool contains_mod(const expr_ref &e);
// Returns true if \p e contains a real-valued sub-term
bool contains_real(const expr_ref &e);
// multiply fml with num and simplify rationals to ints
// fml should be in LIA/LRA/Arrays
// assumes that fml is a sum of products
void mul_by_rat(expr_ref &fml, rational num);
} // namespace spacer