perf: try eta-expansion before delta-reduction in the kernel - #14927
Draft
Kha wants to merge 1 commit into
Draft
Conversation
This PR fixes `(kernel) deterministic timeout` errors on proof terms whose two sides differ only by eta, such as the ones `simp` produces when rewriting with a function-valued equation. The kernel now answers those definitional equality checks as quickly as `Meta.isDefEq` already did. `Meta.isExprDefEqExpensive` tries `isDefEqEta` before `whnfCore` and lazy delta-reduction, but `type_checker::is_def_eq_core` only tried `try_eta_expansion` after `lazy_delta_reduction`. So for `f a =?= fun x => f a x` the kernel delta-normalized `f a` instead of eta-expanding it, and as soon as the unfolded side became a lambda itself, `quick_is_def_eq` committed to `is_def_eq_binding` without backtracking and descended into the normalized subterms. When `f` is a structural recursion over a tree, that is exponential in the tree's depth: the reproducer in `tests/elab/14803.lean` took 19s at depth 8 and timed out at depth 10, and now takes 0.1s. Since the kernel has no metavariables, `(fun x => t) =?= s` holds iff `(fun x => t) =?= (fun x => s x)`, so trying eta first cannot lose solutions. The `try_eta_expansion` call after `lazy_delta_reduction` is kept because that reduction updates both sides and may expose a lambda the earlier check did not see. Closes #14803 Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Member
Author
|
!bench |
Member
Author
|
!bench mathlib |
|
Benchmark results for e5bda08 against fd0efc4 are in. There are significant results. @Kha
Large changes (3✅)
Small changes (1✅, 21🟥)
|
|
Benchmark results for leanprover-community/mathlib4-nightly-testing@75f0e66 against leanprover-community/mathlib4-nightly-testing@7e7506d are in. No significant results found. @Kha
Medium changes (2🟥)
Small changes (5🟥)
|
Collaborator
|
Reference manual CI status:
|
|
Mathlib CI status (docs):
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes
(kernel) deterministic timeouterrors on proof terms whose two sides differ only by eta, such as the onessimpproduces when rewriting with a function-valued equation. The kernel now answers those definitional equality checks as quickly asMeta.isDefEqalready did.Meta.isExprDefEqExpensivetriesisDefEqEtabeforewhnfCoreand lazy delta-reduction, buttype_checker::is_def_eq_coreonly triedtry_eta_expansionafterlazy_delta_reduction. So forf a =?= fun x => f a xthe kernel delta-normalizedf ainstead of eta-expanding it, and as soon as the unfolded side became a lambda itself,quick_is_def_eqcommitted tois_def_eq_bindingwithout backtracking and descended into the normalized subterms. Whenfis a structural recursion over a tree, that is exponential in the tree's depth: the reproducer intests/elab/14803.leantook 19s at depth 8 and timed out at depth 10, and now takes 0.1s.Since the kernel has no metavariables,
(fun x => t) =?= sholds iff(fun x => t) =?= (fun x => s x), so trying eta first cannot lose solutions. Thetry_eta_expansioncall afterlazy_delta_reductionis kept because that reduction updates both sides and may expose a lambda the earlier check did not see.Closes #14803