Skip to content

fix: eta-expand recursor majors before running whnf - #15351

Closed
arthur-adjedj wants to merge 5 commits into
leanprover:masterfrom
arthur-adjedj:eta-before-whnf
Closed

arthur-adjedj wants to merge 5 commits into
leanprover:masterfrom
arthur-adjedj:eta-before-whnf

Conversation

@arthur-adjedj

@arthur-adjedj arthur-adjedj commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

This PR ensures that majors of structure recursors get eta-expanded before attempting to reduce them to weak-head normal form. This change is done in preparation for #15371, which allows for eta-expanding structure majors that are Prop subsingletons, and is necessary to ensure such majors don't get reduced (as they may not normalize)

While this change does not change which terms are accepted by the kernel's type-checker, it can lead to different unification problems at the elaborator level, and can thus produce errors that would need slight tweaks to be fixed.

Related to #14977.

@arthur-adjedj arthur-adjedj changed the title fix: eta-expand majors before running whnf fix: eta-expand recursor majors before running whnf Sep 27, 2026
@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Sep 27, 2026
@arthur-adjedj

Copy link
Copy Markdown
Contributor Author

downstream

@arthur-adjedj

Copy link
Copy Markdown
Contributor Author

!bench

@leanprover-radar

leanprover-radar commented Sep 27, 2026 •

Copy link
Copy Markdown

Benchmark results for edbdc1e against 2c2bdd9 are in. There are significant results. @arthur-adjedj

  • 🟥 build//instructions: +575.7M (+0.01%)

Large changes (1🟥)

  • 🟥 elab/whnfMatcherImplicitTransparencyCaching//instructions: +272.5M (+1.14%)

Medium changes (1🟥)

  • 🟥 elab/reduceMatch//instructions: +99.4M (+0.90%)

Small changes (17✅, 34🟥)

  • ✅ build/module/Init.Data.Array.QSort.Basic//instructions: -30.4M (-0.31%)
  • 🟥 build/module/Init.Data.Array.Sort.Lemmas//instructions: +17.9M (+0.60%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Init.Data.ByteArray.Lemmas//instructions: +20.2M (+0.39%)
  • 🟥 build/module/Init.Data.Fin.Lemmas//instructions: +36.7M (+0.37%)
  • 🟥 build/module/Init.Data.Iterators.Lemmas.Combinators.Monadic.FlatMap//instructions: +39.6M (+0.54%)
  • 🟥 build/module/Init.Data.Ord.String//instructions: +5.3M (+0.71%)
  • 🟥 build/module/Init.Data.Prod//instructions: +4.2M (+0.55%)
  • ✅ build/module/Init.Data.String.Decode//instructions: -262.3M (-1.11%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Init.Data.String.Legacy//instructions: +15.8M (+0.92%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Init.LawfulBEqTactics//instructions: +32.3M (+1.83%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Lake.Build.Facets//instructions: +98.4M (+1.21%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Lake.Build.Infos//instructions: +18.2M (+0.54%)
  • 🟥 build/module/Lake.Build.Trace//instructions: +25.6M (+1.18%)
  • 🟥 build/module/Lake.Config.ConfigDecl//instructions: +24.8M (+0.58%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Lake.Config.ExternLib//instructions: +5.4M (+0.52%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Lake.Config.InputFile//instructions: +33.7M (+1.26%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Lake.Config.Package//instructions: +15.2M (+0.57%)
  • 🟥 build/module/Lake.Toml.Data.Value//instructions: +9.2M (+0.43%)
  • 🟥 build/module/Lean.Compiler.NameMangling//instructions: +31.9M (+0.56%)
  • 🟥 build/module/Lean.Elab.InfoTree.Types//instructions: +10.2M (+0.38%)
  • and 31 more

@arthur-adjedj

Copy link
Copy Markdown
Contributor Author

changelog-language

@arthur-adjedj
arthur-adjedj marked this pull request as ready for review October 11, 2026 11:29
@github-actions github-actions Bot added the changelog-language Language features and metaprograms label Oct 11, 2026
nomeata added a commit to leanprover/lean-kernel-arena that referenced this pull request Oct 11, 2026
…264)

Tests for the eta-related changes planned in leanprover/lean4#14977:

* corner-cases/eta-struct-major-no-whnf: structure eta on a recursor
major before reducing it (leanprover/lean4#15351)
* corner-cases/eta-struct-major-and: structure eta on a major in `Prop`,
like `And` (leanprover/lean4#15371, leanprover/lean4#3213)
* corner-cases/eta-struct-major-and-no-whnf: both together, with a
non-normalizing proof as major
* corner-cases/eta-unit-like-{pi,struct,proof-field,nested}: the
extended unit-like criterion (leanprover/lean4#15372,
leanprover/lean4#2258)
* corner-cases/eta-rec-struct: eta-expansion of a structure recursor
missing its major (leanprover/lean4#15374)
* bugs/eta-ctor-alias: a soundness bug in a draft of
leanprover/lean4#15373

and point the existing corner-cases/eta-ctor and corner-cases/eta-rule-k
at leanprover/lean4#15373 and leanprover/lean4#15374.

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
warning: this test leads to an infinite loop rather than a maxHeartBeats/maxRecDepth error in previous Lean versions
@downstream-lean4

Copy link
Copy Markdown

The adaptation PR for this PR is leanprover/downstream-lean4#110.

@nomeata

nomeata commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

This comment was written by Claude (an AI assistant), on behalf of @nomeata, who reviewed it before posting.

This PR makes some decide +kernel proofs noticeably slower. We noticed it because con-leche, an independent kernel checker, adopted the same order (structure η on the recursor major before whnf). We then measured the PR's kernel directly, against nightly-2026-09-24, which is where the branch forks from master. The PR toolchain is pr-release-15351-fb2d7ef.

Small reproducer. On its own, the example costs 4.51 G instructions on the base and 6.76 G with the PR, about +50 %. This is measured with perf stat -e instructions:u, minus the same file without the example.

inductive L | a | b | c | d | e
  deriving DecidableEq

structure R where
  fst : Bool × L
  snd : Bool × L
  tag : Nat

def ls : List L := [.a, .b, .c, .d, .e]

def xs : List R :=
  (List.range 4).flatMap fun t => ls.flatMap fun l => ls.map fun l' => ⟨(t % 2 == 0, l), (t < 2, l'), t⟩

def count : Nat := (xs.flatMap fun p => xs.filter fun q => decide (p.snd = q.fst)).length
example : count = 1000 := by decide +kernel

The kernel's diagnostics change as follows:

unfolds base PR
Bool.casesOn 10,041 25,000
decide 15,058 35,201
Prod.rec 12,000 20,000
instDecidableEqBool — 10,000

In the wild. We re-checked the openai/math project's AuslanderReiten challenge (Lean v4.34.1 / Mathlib export, with the arena's official checker built on each toolchain):

instructions peak RSS
base 3.86 T 3.15 GB
PR 4.48 T (+16 %) 4.49 GB (+43 %)

On CocycleData.valid_false_n._proof_1_1 the unfolds go from 2.39 M to 3.70 M. Bool.casesOn alone goes from 14.3 k to 240 k. Another large export, LaughlinFock, shows no change (+0.4 %).

Mechanism. The Prod.casesOn major inside instDecidableEqProd is a projection such as m.snd. With this PR, to_cnstr_when_structure turns it into Prod.mk m.snd.1 m.snd.2 first. That is already a constructor application, so the following whnf stops there. The fields reach instDecidableEqBool and instDecidableEqL unreduced, as projections of that particular record.

Before the PR, whnf produced canonical values like (false, L.c). The inner comparisons were then drawn from a handful of closed terms, which the whnf/defeq caches answer after the first time. Now every comparison term mentions its own record, so nothing hits the cache.

Possible mitigation. I don't have one that fits #15371 cleanly. Two directions:

  • Restrict η-before-whnf to the majors that need it, i.e. structures in Prop (for feat: eta-expand recursor major for subsingleton structures #15371), and keep whnf first for Type-valued structures.
  • Or whnf first and use η only as the fallback when whnf does not reach a constructor. That doesn't help if the point is that whnf of the major must not be attempted at all.

@arthur-adjedj

arthur-adjedj commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

I was worried that this would have such a performance impact, but expected the caching mechanisms to help enough for this to not be an issue... only doing η-before-whnf on subsingleton structures feels a bit too hacky for my liking. Another solution could instead be to avoid running whnf on Prop-valued majors, though this might cause breakages if merged before #15371. The rule still is that proof should not get reduced ever, so doing things that way might make more sense.

@nomeata

nomeata commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Yes, I think not reducing proofs is the right guiding principle.

Tangential, recursors for inductives propositions, should they even get rules? If we never reduce them, they are essentially dead code, aren't they?

@arthur-adjedj

arthur-adjedj commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

For propositions that don't have large elimination, once #1531, #15371 and #13851 are merged, it should be dead code yes. For now though, it's not. Happy to add this as part of the renovation plan and fix it once the relevant PRs are merged.

@arthur-adjedj
arthur-adjedj marked this pull request as draft October 14, 2026 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-language Language features and metaprograms downstream Request a downstream-lean4 adaptation PR. toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants