3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2026-07-23 07:22:33 +00:00

fix HO-matcher imitation curry-order and instance-assembly ordering bugs

The higher-order matcher produced ill-typed instantiations that aborted
the solve (sort-mismatch / unbound-variable exceptions), making
smt.ho_matching=true net-negative on the TPTP THF benchmarks.

Two root causes:

1. Imitation rule (ho_matcher.cpp): the select chain 'pats' is collected
   outermost-first, i.e. in reverse application order. The imitating
   lambda must curry arguments in application order (first-applied select
   binds the outermost lambda). Reversing 'pats' before building the
   domain/argument/body vectors and the lambda-wrapping loop makes the
   constructed lambda's sort agree with the flex head variable. Fixes
   unit-test ho_matcher test6c/test6d (previously asserted at
   add_binding: v->get_sort() == t->get_sort()).

2. Instance assembly (smt_quantifier.cpp on_ho_match): the fixpoint
   binding substitution used var_subst with the default std_order=true
   while the binding vector is directly indexed (binding[k] = value for
   var k). This resolved chained HO variable references against the wrong
   slots and built ill-sorted terms (assertion at rewriter_def.h:52).
   Use direct (std_order=false) substitution to match the binding layout.

Also adds defensive guards as belt-and-suspenders: subst_sorts_match
skips sort-inconsistent substitutions, an is_ground check skips bindings
with leftover de Bruijn variables, and on_ho_match catches z3_exception
to skip an unusable heuristic instance rather than aborting the solve
(re-raising only on cancellation/resource-limit).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Nikolaj Bjorner 2026-07-05 17:25:01 -07:00
parent 72d27e1cbb
commit 6c8a5cd853
3 changed files with 91 additions and 1 deletions

View file

@ -421,6 +421,16 @@ namespace euf {
// H (p1) (p2) = f(t1, .., tn)
// H -> \x1 \x2 f(H1(x1, x2), .., Hn(x1, x2))
// H1(p1, p2) = t1, .., Hn(p1, p2) = tn
//
// The select chain `pats` was collected from the outermost
// select down to the flex head, i.e. in reverse order of
// application. The imitating lambda must curry the arguments in
// application order (the first-applied select binds the
// outermost lambda), so process the applications inner-to-outer.
// Without this the constructed lambda has the argument arities
// in the wrong nesting order and its sort disagrees with the
// flex head variable (producing an ill-typed binding).
pats.reverse();
ptr_vector<sort> domain, pat_domain;
ptr_vector<expr> pat_args;
expr_ref_vector args(m), pat_vars(m), bound_args(m);
@ -824,6 +834,16 @@ namespace euf {
TRACE(ho_matching, tout << "refine " << mk_pp(p, m) << "\n" << s << "\n");
unsigned num_bound = 0, level = 0;
for (auto [v, pat] : m_pat2abs[fo_pat]) {
// Defensive: if the abstraction-variable indices in the stored
// pattern do not line up (sort-wise) with the current substitution,
// building the refined term would be ill-typed and abort the solve.
// Skip the whole refinement; a missed heuristic instance is sound.
if (!subst_sorts_match(m, pat, s, true)) {
m_trail.pop_scope(1);
return;
}
}
for (auto [v, pat] : m_pat2abs[fo_pat]) {
var_subst sub(m, true);
auto pat_refined = sub(pat, s);
@ -835,6 +855,41 @@ namespace euf {
m_trail.pop_scope(1);
}
bool ho_matcher::subst_sorts_match(ast_manager& m, expr* t, expr_ref_vector const& s, bool std_order) {
unsigned sz = s.size();
ptr_buffer<expr> es;
svector<unsigned> offs;
es.push_back(t);
offs.push_back(0);
while (!es.empty()) {
expr* e = es.back(); es.pop_back();
unsigned off = offs.back(); offs.pop_back();
if (is_var(e)) {
unsigned idx = to_var(e)->get_idx();
if (idx < off)
continue;
unsigned k = idx - off;
if (k >= sz)
continue;
expr* r = std_order ? s.get(sz - k - 1) : s.get(k);
if (r && r->get_sort() != e->get_sort())
return false;
}
else if (is_app(e)) {
for (expr* arg : *to_app(e)) {
es.push_back(arg);
offs.push_back(off);
}
}
else if (is_quantifier(e)) {
quantifier* q = to_quantifier(e);
es.push_back(q->get_expr());
offs.push_back(off + q->get_num_decls());
}
}
return true;
}
std::ostream& ho_matcher::display(std::ostream& out) const {
m_subst.display(out << "subst\n");
m_goals.display(out << "goals\n");

View file

@ -400,6 +400,13 @@ namespace euf {
void refine_ho_match(app* p, expr_ref_vector& s);
// Returns true iff applying the substitution s to t (with the given
// variable ordering) is sort-safe: every free variable of t that is
// bound by s maps to a value of the same sort. Used to defensively
// skip higher-order matches whose bindings would produce ill-typed
// instantiation terms (which would otherwise abort the whole solve).
static bool subst_sorts_match(ast_manager& m, expr* t, expr_ref_vector const& s, bool std_order);
bool is_free(app* p, unsigned i) const { return m_hopat2free_vars[p].contains(i); }
quantifier* hoq2q(quantifier* q) const { return m_hoq2q[q]; }