Skip to content

Commit

Permalink
Spacer Global Guidance (#6026)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
agurfinkel and hgvk94 committed Aug 30, 2022
1 parent 1a79d92 commit d2b618d
Show file tree
Hide file tree
Showing 41 changed files with 6,033 additions and 1,887 deletions.
2 changes: 1 addition & 1 deletion src/ast/substitution/substitution.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class substitution {
return find(to_var(v.get_expr()), v.get_offset(), r);
}

void get_binding(unsigned binding_num, var_offset& var, expr_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));
}
Expand Down
4 changes: 4 additions & 0 deletions src/math/simplex/simplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ namespace simplex {
sparse_matrix_ops::kernel(M, K);
}

void kernel_ffe(sparse_matrix<mpq_ext> &M, vector<vector<rational>> &K) {
sparse_matrix_ops::kernel_ffe(M, K);
}

void ensure_rational_solution(simplex<mpq_ext>& S) {
rational delta(1);
for (unsigned i = 0; i < S.get_num_vars(); ++i) {
Expand Down
1 change: 1 addition & 0 deletions src/math/simplex/simplex.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,6 @@ namespace simplex {
void ensure_rational_solution(simplex<mpq_ext>& s);

void kernel(sparse_matrix<mpq_ext>& s, vector<vector<rational>>& K);
void kernel_ffe(sparse_matrix<mpq_ext> &s, vector<vector<rational>> &K);
};

1 change: 1 addition & 0 deletions src/math/simplex/sparse_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ namespace simplex {
void add_var(row r, numeral const& n, var_t var);
void add(row r, numeral const& n, row src);
void mul(row r, numeral const& n);
void div(row r, numeral const& n);
void neg(row r);
void del(row r);

Expand Down
19 changes: 19 additions & 0 deletions src/math/simplex/sparse_matrix_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,25 @@ namespace simplex {
}
}

/**
\brief Set row <- n/row
*/
template <typename Ext>
void sparse_matrix<Ext>::div(row r, numeral const &n) {
SASSERT(!m.is_zero(n));
if (m.is_one(n)) {
// no op
} else if (m.is_minus_one(n)) {
neg(r);
} else {
row_iterator it = row_begin(r);
row_iterator end = row_end(r);
for (; it != end; ++it) {
m.div(it->m_coeff, n, it->m_coeff);
}
}
}

/**
\brief Delete row.
*/
Expand Down
159 changes: 156 additions & 3 deletions src/math/simplex/sparse_matrix_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class sparse_matrix_ops {
for (auto [row, row_entry] : M.get_rows(k)) {
if (c[row.id()] != 0) continue;
auto &m_jk = row_entry->m_coeff;
if (mpq_manager<false>::is_zero(m_jk)) continue;
if (m.is_zero(m_jk)) continue;

// D = rational(-1) / m_jk;
m.set(D, m_jk);
Expand All @@ -68,9 +68,10 @@ class sparse_matrix_ops {
K.push_back(vector<rational>());
for (unsigned i = 0; i < n_vars; ++i) {
if (d[i] > 0) {
auto r = sparse_matrix<mpq_ext>::row(d[i] - 1);
auto r = typename sparse_matrix<Ext>::row(d[i] - 1);
K.back().push_back(rational(M.get_coeff(r, k)));
} else if (i == k)
}
else if (i == k)
K.back().push_back(rational(1));
else
K.back().push_back(rational(0));
Expand All @@ -81,5 +82,157 @@ class sparse_matrix_ops {
static void kernel(sparse_matrix<mpq_ext> &M, vector<vector<rational>> &K) {
kernel<mpq_ext>(M, K);
}

/// \brief Kernel computation using fraction-free-elimination
///
template <typename Ext>
static void kernel_ffe(sparse_matrix<Ext> &M, vector<vector<rational>> &K) {
using scoped_numeral = typename Ext::scoped_numeral;

/// Based on George Nakos, Peter R. Turner, Robert M. Williams:
/// Fraction-free algorithms for linear and polynomial equations. SIGSAM
/// Bull. 31(3): 11-19 (1997)
vector<unsigned> d, c;
unsigned n_vars = M.num_vars(), n_rows = M.num_rows();
c.resize(n_rows, 0u);
d.resize(n_vars, 0u);

auto &m = M.get_manager();
scoped_numeral m_ik(m);
scoped_numeral m_jk(m);
scoped_numeral last_pv(m);

m.set(last_pv, 1);

for (unsigned k = 0; k < n_vars; ++k) {
d[k] = 0;
for (auto [row, row_entry] : M.get_rows(k)) {
if (c[row.id()] != 0) continue;
auto &m_jk_ref = row_entry->m_coeff;
if (m.is_zero(m_jk_ref))
// XXX: should not happen, the matrix is sparse
continue;

// this a pivot column
m.set(m_jk, m_jk_ref);

// ensure that pivot is negative
if (m.is_pos(m_jk_ref)) { M.neg(row); }
else { m.neg(m_jk); }
// m_jk is abs(M[j]][k])

for (auto row_i : M.get_rows()) {
if (row_i.id() == row.id()) continue;

m.set(m_ik, M.get_coeff(row_i, k));
// row_i *= m_jk
M.mul(row_i, m_jk);
if (!m.is_zero(m_ik)) {
// row_i += m_ik * row
M.add(row_i, m_ik, row);
}
M.div(row_i, last_pv);
}
c[row.id()] = k + 1;
d[k] = row.id() + 1;
m.set(last_pv, m_jk);
break;
}
}

for (unsigned k = 0; k < n_vars; ++k) {
if (d[k] != 0) continue;
K.push_back(vector<rational>());
for (unsigned i = 0; i < n_vars; ++i) {
if (d[i] > 0) {
auto r = typename sparse_matrix<Ext>::row(d[i] - 1);
K.back().push_back(rational(M.get_coeff(r, k)));
}
else if (i == k)
K.back().push_back(rational(last_pv));
else
K.back().push_back(rational(0));
}
}
}

static void kernel_ffe(sparse_matrix<mpq_ext> &M,
vector<vector<rational>> &K) {
kernel_ffe<mpq_ext>(M, K);
}


template <typename Ext>
static void kernel_ffe(sparse_matrix<Ext> &M, sparse_matrix<Ext> &K,
vector<unsigned> &basics) {
using scoped_numeral = typename Ext::scoped_numeral;

/// Based on George Nakos, Peter R. Turner, Robert M. Williams:
/// Fraction-free algorithms for linear and polynomial equations. SIGSAM
/// Bull. 31(3): 11-19 (1997)
vector<unsigned> d, c;
unsigned n_vars = M.num_vars(), n_rows = M.num_rows();
c.resize(n_rows, 0u);
d.resize(n_vars, 0u);

auto &m = M.get_manager();
scoped_numeral m_ik(m);
scoped_numeral m_jk(m);
scoped_numeral last_pv(m);

m.set(last_pv, 1);

for (unsigned k = 0; k < n_vars; ++k) {
d[k] = 0;
for (auto [row, row_entry] : M.get_rows(k)) {
if (c[row.id()] != 0) continue;
auto &m_jk_ref = row_entry->m_coeff;
if (m.is_zero(m_jk_ref))
// XXX: should not happen, the matrix is sparse
continue;

// this a pivot column
m.set(m_jk, m_jk_ref);

// ensure that pivot is negative
if (m.is_pos(m_jk_ref)) { M.neg(row); }
else { m.neg(m_jk); }
// m_jk is abs(M[j]][k])

for (auto row_i : M.get_rows()) {
if (row_i.id() == row.id()) continue;

m.set(m_ik, M.get_coeff(row_i, k));
// row_i *= m_jk
M.mul(row_i, m_jk);
if (!m.is_zero(m_ik)) {
// row_i += m_ik * row
M.add(row_i, m_ik, row);
}
M.div(row_i, last_pv);
}
c[row.id()] = k + 1;
d[k] = row.id() + 1;
m.set(last_pv, m_jk);
break;
}
}

K.ensure_var(n_vars - 1);
for (unsigned k = 0; k < n_vars; ++k) {
if (d[k] != 0) continue;
auto row = K.mk_row();
basics.push_back(k);
for (unsigned i = 0; i < n_vars; ++i) {
if (d[i] > 0) {
auto r = typename sparse_matrix<Ext>::row(d[i] - 1);
K.add_var(row, M.get_coeff(r, k), i);
}
else if (i == k)
K.add_var(row, last_pv, i);
}
}
}

};
} // namespace simplex
7 changes: 6 additions & 1 deletion src/muz/base/fp_params.pyg
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ def_module_params('fp',
('spacer.p3.share_lemmas', BOOL, False, 'Share frame lemmas'),
('spacer.p3.share_invariants', BOOL, False, "Share invariants lemmas"),
('spacer.min_level', UINT, 0, 'Minimal level to explore'),
('spacer.print_json', SYMBOL, '', 'Print pobs tree in JSON format to a given file'),
('spacer.trace_file', SYMBOL, '', 'Log file for progress events'),
('spacer.ctp', BOOL, True, 'Enable counterexample-to-pushing'),
('spacer.use_inc_clause', BOOL, True, 'Use incremental clause to represent trans'),
Expand All @@ -181,4 +180,10 @@ def_module_params('fp',
('spacer.use_lim_num_gen', BOOL, False, 'Enable limit numbers generalizer to get smaller numbers'),
('spacer.logic', SYMBOL, '', 'SMT-LIB logic to configure internal SMT solvers'),
('spacer.arith.solver', UINT, 2, 'arithmetic solver: 0 - no solver, 1 - bellman-ford based solver (diff. logic only), 2 - simplex based solver, 3 - floyd-warshall based solver (diff. logic only) and no theory combination 4 - utvpi, 5 - infinitary lra, 6 - lra solver'),
('spacer.global', BOOL, False, 'Enable global guidance'),
('spacer.gg.concretize', BOOL, True, 'Enable global guidance concretize'),
('spacer.gg.conjecture', BOOL, True, 'Enable global guidance conjecture'),
('spacer.gg.subsume', BOOL, True, 'Enable global guidance subsume'),
('spacer.use_iuc', BOOL, True, 'Enable Interpolating Unsat Core(IUC) for lemma generalization'),
('spacer.expand_bnd', BOOL, False, 'Enable expand-bound lemma generalization'),
))
8 changes: 8 additions & 0 deletions src/muz/spacer/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
BasedOnStyle: LLVM
AllowShortFunctionsOnASingleLine: All
IndentWidth: '4'
AllowShortBlocksOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
...
10 changes: 9 additions & 1 deletion src/muz/spacer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ z3_add_component(spacer
spacer_prop_solver.cpp
spacer_sym_mux.cpp
spacer_util.cpp
spacer_cluster_util.cpp
spacer_iuc_solver.cpp
spacer_legacy_mbp.cpp
spacer_proof_utils.cpp
Expand All @@ -22,12 +23,19 @@ z3_add_component(spacer
spacer_sem_matcher.cpp
spacer_quant_generalizer.cpp
spacer_arith_generalizers.cpp
spacer_global_generalizer.cpp
spacer_ind_lemma_generalizer.cpp
spacer_expand_bnd_generalizer.cpp
spacer_cluster.cpp
spacer_callback.cpp
spacer_json.cpp
spacer_iuc_proof.cpp
spacer_mbc.cpp
spacer_pdr.cpp
spacer_sat_answer.cpp
spacer_concretize.cpp
spacer_convex_closure.cpp
spacer_conjecture.cpp
spacer_arith_kernel.cpp
COMPONENT_DEPENDENCIES
arith_tactics
core_tactics
Expand Down
1 change: 1 addition & 0 deletions src/muz/spacer/spacer_antiunify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ void anti_unifier::operator()(expr *e1, expr *e2, expr_ref &res,
m_pinned.push_back(u);
m_cache.insert(n1, n2, u);
}
m_todo.pop_back();
}

expr *r;
Expand Down
Loading

0 comments on commit d2b618d

Please sign in to comment.