Skip to content

feat: eta-expand partial recursor apps for structures/K-like types - #15374

Merged
nomeata merged 11 commits into
leanprover:masterfrom
arthur-adjedj:eta-expand-partial-recursors
Nov 24, 2026
Merged

nomeata merged 11 commits into
leanprover:masterfrom
arthur-adjedj:eta-expand-partial-recursors

Conversation

@arthur-adjedj

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

Copy link
Copy Markdown
Contributor

This PR eta-expands partial recursor applications when the major is missing and is an eta-expandable structure or of a K-like type.

Related to #14977
Second part of #12520

@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

@downstream-lean4 downstream-lean4 Bot added the downstream Request a downstream-lean4 adaptation PR. label Sep 28, 2026
@arthur-adjedj
arthur-adjedj force-pushed the eta-expand-partial-recursors branch from d414622 to 00960f8 Compare October 11, 2026 08:59
@arthur-adjedj
arthur-adjedj marked this pull request as ready for review October 11, 2026 11:17
@arthur-adjedj

Copy link
Copy Markdown
Contributor Author

changelog-language

@arthur-adjedj
arthur-adjedj force-pushed the eta-expand-partial-recursors branch from 1cc09a5 to 683d4d3 Compare October 11, 2026 11:20
@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]>
@nomeata

nomeata commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

Claude, while testing this change in con-leche, found this. Is this an issue?

#15374 does not terminate on a bare recursor. ∀ g, @Eq.rec.{1,1} = g hangs the PR's kernel; v4.34.1 rejects it at once. Eta-expanding gives Eq.rec x⃗ =?= g x⃗, where the K rescue fails at the fresh indices. is_def_eq_app then compares Eq.rec =?= g again, one binder deeper, forever. Any bare recursor compared with a same-typed non-lambda should trigger it, e.g. Exists.rec too

@arthur-adjedj

arthur-adjedj commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Is this an issue?

Definitely yes ! This cannot happen on structure recursors since the recursor will always reduce once applied to a major, but the eta-expansion needs to be more fine-grained in the case of k-like inductives (e.g if the type of an appropriate mk_nullary_cnstr is defeq to the expected major's)

@arthur-adjedj

arthur-adjedj commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor Author

To give a more detailed answer to the following sentence:

Any bare recursor compared with a same-typed non-lambda should trigger it, e.g. Exists.rec too

The eta-expansion only happens on recursors for k-like and structure types. For structure types, the type is either eta-expandable (e.g Prod), or lives in Prop/not always Type and either has large elimination or not. If they have large elimination (e.g And), they will eta-expand their major once #15371 lands, and if they don't have large elimination (e.g Exist) , then any application of the recursor is a proof, and will always be picked up by proof-irrelevance before any eta-expansion happens (once #15103 lands). As such, the recursor call always disappears following the eta-expansion on structures. This PR is definitely the hardest to get correct in my opinion, and relies on invariants from other PRs to produce the right results.

@arthur-adjedj

arthur-adjedj commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Rather than first eta-expand then wait for whnf to erase the recursor call away, one more robust option could be to call reduce_recursor directly in the eta-expansion call and, as a fallback, fail if the function doesn't give any progress, though it would make the code more complex.

@arthur-adjedj
arthur-adjedj force-pushed the eta-expand-partial-recursors branch from 82637e2 to b81b431 Compare October 13, 2026 10:15
@arthur-adjedj

Copy link
Copy Markdown
Contributor Author

!bench

@leanprover-radar

leanprover-radar commented Oct 13, 2026 •

Copy link
Copy Markdown

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

  • 🟥 build//instructions: +15.9G (+0.15%)

Large changes (1🟥)

  • 🟥 elab/simp_local//instructions: +1.0G (+3.51%)

Medium changes (1✅, 3🟥)

  • 🟥 build/module/Std.Tactic.BVDecide.Bitblast.BVExpr.Circuit.Lemmas.Expr//instructions: +1.0G (+1.25%)
  • ✅ elab/lift_lets_dag//instructions: -76.2M (-0.66%)
  • 🟥 elab/reduceMatch//instructions: +94.2M (+0.85%)
  • 🟥 elab/whnfMatcherImplicitTransparencyCaching//instructions: +188.5M (+0.79%)

Small changes (13✅, 173🟥)

  • 🟥 build/module/Init.Control.ExceptCps//instructions: +1.9M (+0.19%)
  • 🟥 build/module/Init.Control.Lawful.Instances//instructions: +15.2M (+0.25%)
  • 🟥 build/module/Init.Control.Lawful.MonadAttach.Instances//instructions: +4.5M (+0.30%)
  • 🟥 build/module/Init.Control.StateCps//instructions: +2.2M (+0.24%)
  • 🟥 build/module/Init.Data.Array.Attach//instructions: +30.2M (+0.32%)
  • 🟥 build/module/Init.Data.Array.Basic//instructions: +32.9M (+0.33%)
  • 🟥 build/module/Init.Data.Array.BasicAux//instructions: +3.7M (+0.28%)
  • 🟥 build/module/Init.Data.Array.Erase//instructions: +20.1M (+0.30%)
  • 🟥 build/module/Init.Data.Array.Find//instructions: +29.0M (+0.32%)
  • 🟥 build/module/Init.Data.Array.InsertIdx//instructions: +5.7M (+0.23%)
  • 🟥 build/module/Init.Data.Array.Lemmas//instructions: +144.4M (+0.29%)
  • 🟥 build/module/Init.Data.Array.Lex.Lemmas//instructions: +38.4M (+0.42%)
  • 🟥 build/module/Init.Data.Array.MapIdx//instructions: +17.4M (+0.21%)
  • 🟥 build/module/Init.Data.Array.Range//instructions: +10.6M (+0.30%)
  • 🟥 build/module/Init.Data.Array.Sort.Lemmas//instructions: +23.0M (+0.77%)
  • 🟥 build/module/Init.Data.BitVec.Bitblast//instructions: +151.0M (+0.32%)
  • 🟥 build/module/Init.Data.BitVec.Lemmas//instructions: +339.3M (+0.31%)
  • 🟥 build/module/Init.Data.ByteArray.Lemmas//instructions: +30.7M (+0.60%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Init.Data.Char.Ordinal//instructions: +17.2M (+0.31%)
  • 🟥 build/module/Init.Data.Dyadic.Basic//instructions: +38.6M (+0.30%)
  • and 166 more

@arthur-adjedj
arthur-adjedj marked this pull request as draft October 14, 2026 21:34
@arthur-adjedj

Copy link
Copy Markdown
Contributor Author

!bench

@leanprover-radar

leanprover-radar commented Oct 18, 2026 •

Copy link
Copy Markdown

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

  • 🟥 build//instructions: +14.0G (+0.13%)

Large changes (2🟥)

  • 🟥 elab/simp_local//instructions: +959.7M (+3.30%)
  • 🟥 elab/whnfMatcherImplicitTransparencyCaching//instructions: +205.3M (+0.86%)

Medium changes (1🟥)

  • 🟥 elab/reduceMatch//instructions: +100.8M (+0.91%)

Small changes (14✅, 185🟥)

  • 🟥 build/module/Init.BinderPredicates//instructions: +2.6M (+0.13%)
  • 🟥 build/module/Init.Control.StateCps//instructions: +1.6M (+0.18%)
  • 🟥 build/module/Init.Data.Array.Attach//instructions: +26.3M (+0.28%)
  • 🟥 build/module/Init.Data.Array.Basic//instructions: +29.6M (+0.29%)
  • 🟥 build/module/Init.Data.Array.BinSearch//instructions: +9.0M (+0.16%)
  • 🟥 build/module/Init.Data.Array.Erase//instructions: +18.9M (+0.28%)
  • 🟥 build/module/Init.Data.Array.Extract//instructions: +63.7M (+0.19%)
  • 🟥 build/module/Init.Data.Array.Find//instructions: +23.1M (+0.25%)
  • 🟥 build/module/Init.Data.Array.InsertIdx//instructions: +6.5M (+0.26%)
  • 🟥 build/module/Init.Data.Array.Lemmas//instructions: +136.2M (+0.28%)
  • 🟥 build/module/Init.Data.Array.MapIdx//instructions: +16.0M (+0.20%)
  • 🟥 build/module/Init.Data.Array.Monadic//instructions: +12.6M (+0.23%)
  • 🟥 build/module/Init.Data.Array.Range//instructions: +9.6M (+0.27%)
  • 🟥 build/module/Init.Data.Array.Sort.Basic//instructions: +5.2M (+0.21%)
  • 🟥 build/module/Init.Data.Array.Sort.Lemmas//instructions: +22.8M (+0.77%)
  • 🟥 build/module/Init.Data.BitVec.Bitblast//instructions: +156.2M (+0.33%)
  • 🟥 build/module/Init.Data.BitVec.Lemmas//instructions: +367.6M (+0.33%)
  • 🟥 build/module/Init.Data.ByteArray.Lemmas//instructions: +32.5M (+0.63%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Init.Data.Char.Ordinal//instructions: +16.5M (+0.30%)
  • 🟥 build/module/Init.Data.Dyadic.Basic//instructions: +39.1M (+0.30%)
  • and 179 more

@arthur-adjedj
arthur-adjedj force-pushed the eta-expand-partial-recursors branch from d979a09 to c641702 Compare October 21, 2026 19:38
@arthur-adjedj
arthur-adjedj force-pushed the eta-expand-partial-recursors branch from 707bbe7 to 56e0c5d Compare November 7, 2026 08:59
@arthur-adjedj
arthur-adjedj force-pushed the eta-expand-partial-recursors branch from 2f119c0 to 18e159d Compare November 24, 2026 08:29
@arthur-adjedj
arthur-adjedj marked this pull request as ready for review November 24, 2026 08:29
@downstream-lean4

Copy link
Copy Markdown

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

@nomeata
nomeata added this pull request to the merge queue Nov 24, 2026
Merged via the queue into leanprover:master with commit be78f9e Nov 24, 2026
20 checks passed
nomeata added a commit to arthur-adjedj/lean4 that referenced this pull request Nov 24, 2026
Conflict resolutions:
* Lean/Meta/WHNF.lean: `reduceRec` is public (master) and keeps the early exits for recursors without rules or large elimination.
* kernel/type_checker.cpp, Lean/Meta/ExprDefEq.lean: the recursor eta-expansion of leanprover#15374 tests structure eta with `has_eta`/`hasEta`, as the rest of the branch does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WD3k8hwfVBznY9FEMvR4xw
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.

3 participants