mirror of
https://github.com/Z3Prover/z3
synced 2025-08-15 23:35:26 +00:00
* 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>
219 lines
7.2 KiB
C++
219 lines
7.2 KiB
C++
/*++
|
|
Copyright (c) 2006 Microsoft Corporation
|
|
|
|
Module Name:
|
|
|
|
substitution.h
|
|
|
|
Abstract:
|
|
|
|
A substitution, that is, a mapping from (variable, offset) to (expr, offset).
|
|
We use offsets in order to avoid creating variants of terms.
|
|
|
|
Author:
|
|
|
|
Leonardo de Moura (leonardo) 2008-02-01.
|
|
|
|
Revision History:
|
|
|
|
nbjorner 2013-01-21:
|
|
- reset the apply cache on pop_scope to make sure that popped
|
|
substitutions are invalidated.
|
|
- reset_cache if a new binding was added after application
|
|
of a substitution.
|
|
- Add m_refs to make sure terms in the range of the
|
|
substitution have the same life-time as the substitution.
|
|
- Remove reset_subst() function. If called without resetting the cache, then
|
|
results of applying substitutions are incoherent.
|
|
- Replace UNREACHABLE segment for quantifiers by recursive invocation of substitution
|
|
that updates expressions within quantifiers. It shifts all variables in the domain
|
|
of the current substitution by the number of quantified variables.
|
|
|
|
--*/
|
|
#pragma once
|
|
|
|
#include "ast/substitution/expr_offset_map.h"
|
|
#include "ast/substitution/var_offset_map.h"
|
|
#include "ast/ast_pp.h"
|
|
|
|
/**
|
|
\brief A mapping from (variable,offset) to expr_offset.
|
|
*/
|
|
class substitution {
|
|
ast_manager & m_manager;
|
|
var_offset_map<expr_offset> m_subst;
|
|
|
|
// field for backtracking
|
|
typedef std::pair<unsigned, unsigned> var_offset;
|
|
svector<var_offset> m_vars;
|
|
expr_ref_vector m_refs;
|
|
unsigned_vector m_scopes;
|
|
|
|
// fields for applying substitutions
|
|
svector<expr_offset> m_todo;
|
|
expr_offset_map<expr *> m_apply_cache;
|
|
expr_ref_vector m_new_exprs;
|
|
|
|
// fields for checking for cycles
|
|
enum color { White, Grey, Black };
|
|
expr_offset_map<color> m_color;
|
|
|
|
|
|
// keep track of how substitution state was last updated.
|
|
enum state { CLEAN, APPLY, INSERT };
|
|
state m_state;
|
|
|
|
#ifdef Z3DEBUG
|
|
unsigned m_max_offset_since_reset;
|
|
#endif
|
|
void apply_visit(expr_offset const & n, bool & visited);
|
|
|
|
|
|
color get_color(expr_offset const & p) const;
|
|
void set_color(expr_offset const & p, color c);
|
|
|
|
void visit(expr_offset const & p, bool & visited);
|
|
bool visit_children(expr_offset const & p);
|
|
bool acyclic(expr_offset p);
|
|
|
|
public:
|
|
substitution(ast_manager & m);
|
|
ast_manager & get_manager() const { return m_manager; }
|
|
|
|
// -----------------------------------
|
|
//
|
|
// Reserve memory for the given number of
|
|
// offsets and variables.
|
|
//
|
|
// -----------------------------------
|
|
|
|
void reserve(unsigned num_offsets, unsigned num_vars) { m_subst.reserve(num_offsets, num_vars); }
|
|
void reserve_offsets(unsigned num_offsets) { m_subst.reserve_offsets(num_offsets); }
|
|
void reserve_vars(unsigned num_vars) { m_subst.reserve_vars(num_vars); }
|
|
|
|
// -----------------------------------
|
|
//
|
|
// Reset functions
|
|
//
|
|
// -----------------------------------
|
|
|
|
// reset everything
|
|
void reset();
|
|
// reset only the substitution application cache
|
|
void reset_cache();
|
|
|
|
// -----------------------------------
|
|
//
|
|
// Backtracking
|
|
//
|
|
// -----------------------------------
|
|
|
|
void push_scope() { m_scopes.push_back(m_vars.size()); }
|
|
void pop_scope(unsigned num_scopes = 1);
|
|
unsigned get_scope_lvl() { return m_scopes.size(); }
|
|
bool top_scope_has_bindings() const { return m_scopes.empty() ? !m_vars.empty() : m_scopes.back() < m_vars.size(); }
|
|
unsigned get_num_bindings() const { return m_vars.size(); }
|
|
|
|
|
|
// -----------------------------------
|
|
//
|
|
// Cycle detection
|
|
//
|
|
// -----------------------------------
|
|
|
|
bool acyclic();
|
|
|
|
// -----------------------------------
|
|
//
|
|
// Insertion & Lookup
|
|
//
|
|
// get_binding supplies a way to inspect the substitution.
|
|
//
|
|
// -----------------------------------
|
|
|
|
void insert(unsigned v_idx, unsigned offset, expr_offset const & t) {
|
|
TRACE("subst_insert", tout << "inserting: #" << v_idx << ":" << offset << " --> " << mk_pp(t.get_expr(), m_manager)
|
|
<< ":" << t.get_offset() << "\n";);
|
|
m_vars.push_back(var_offset(v_idx, offset));
|
|
m_refs.push_back(t.get_expr());
|
|
m_subst.insert(v_idx, offset, t);
|
|
m_state = INSERT;
|
|
}
|
|
void insert(var * v, unsigned offset, expr_offset const & t) { insert(v->get_idx(), offset, t); }
|
|
void insert(expr_offset v, expr_offset const & t) {
|
|
SASSERT(is_var(v.get_expr()));
|
|
insert(to_var(v.get_expr()), v.get_offset(), t);
|
|
}
|
|
|
|
bool find(unsigned v_idx, unsigned offset, expr_offset & r) const { return m_subst.find(v_idx, offset, r); }
|
|
bool find(var * v, unsigned offset, expr_offset & r) const { return find(v->get_idx(), offset, r); }
|
|
bool find(expr_offset v, expr_offset & r) const {
|
|
SASSERT(is_var(v.get_expr()));
|
|
return find(to_var(v.get_expr()), v.get_offset(), r);
|
|
}
|
|
|
|
void get_binding(unsigned binding_num, var_offset& var, expr_offset& r) const {
|
|
var = m_vars[binding_num];
|
|
VERIFY(m_subst.find(var.first, var.second, r));
|
|
}
|
|
|
|
bool contains(var * v, unsigned offset) { expr_offset r; return find(v, offset, r); }
|
|
|
|
// -----------------------------------
|
|
//
|
|
// Application
|
|
//
|
|
// -----------------------------------
|
|
|
|
/**
|
|
\brief Apply the current substitution to the given
|
|
expression+offset. The result is an expression.
|
|
|
|
The argument num_actual_offsets is the maximum offset used in a
|
|
insert method since the last reset.
|
|
|
|
The argument deltas is an array of size num_actual_offsets. It contains
|
|
the variable delta for each offset. A free variable x:i in an expression offset t:j is mapped
|
|
to the variable x+delta[i].
|
|
*/
|
|
void apply(unsigned num_actual_offsets, unsigned const * deltas, expr_offset const & n, expr_ref & result) {
|
|
apply(num_actual_offsets, deltas, n, expr_offset(nullptr, 0), expr_offset(nullptr, 0), result);
|
|
}
|
|
|
|
/**
|
|
\brief Similar to the previous method, but occurrences of s in n are substituted by t.
|
|
If s != expr_offset(0,0), then the cache is reset before and after the execution of this procedure.
|
|
*/
|
|
void apply(unsigned num_actual_offsets, unsigned const* deltas, expr_offset const & n, expr_offset const & s, expr_offset const & t, expr_ref & result);
|
|
|
|
void apply(expr * n, expr_ref & result) {
|
|
unsigned deltas[1] = { 0 };
|
|
apply(1, deltas, expr_offset(n, 0), result);
|
|
}
|
|
|
|
// -----------------------------------
|
|
//
|
|
// Debugging
|
|
//
|
|
// -----------------------------------
|
|
|
|
/**
|
|
\brief Dump the current substitution (for debugging purposes).
|
|
*/
|
|
void display(std::ostream & out, unsigned num_actual_offsets, unsigned const * deltas);
|
|
|
|
/**
|
|
\brief Dump the current substitution without normalizing expressions (for debugging purposes).
|
|
*/
|
|
void display(std::ostream & out);
|
|
|
|
|
|
// -----------------------------------
|
|
//
|
|
// Compare terms modulo a substitution
|
|
//
|
|
// -----------------------------------
|
|
bool compare(expr_offset t1, expr_offset t2);
|
|
|
|
};
|
|
|