Skip to content

feat: extend unit-like criteria for eta - #15372

Merged
nomeata merged 10 commits into
leanprover:masterfrom
arthur-adjedj:eta-unit
Oct 20, 2026
Merged

nomeata merged 10 commits into
leanprover:masterfrom
arthur-adjedj:eta-unit

Conversation

@arthur-adjedj

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

Copy link
Copy Markdown
Contributor

This PR expands the unit-like criteria to handle pi-types whose codomain are unit-like, and structures whose fields are all unit-like or proof-irrelevant.

Since isDefEqUnitLike runs on every failing defeq tests, it is bound to be triggered and return negative answers often in the elaborator. As such, an elaborator-level cache is added to answer quickly when possible. The kernel probably doesn't need such a cache, as it usually only encounters well-typed terms, and thus only runs the equivalent function when necessary.

This PR also adds a tests documenting the #13364 failure.
Related to #14977
Closes #2258.

@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
@downstream-lean4

Copy link
Copy Markdown

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

@arthur-adjedj

Copy link
Copy Markdown
Contributor Author

changelog-language

@github-actions github-actions Bot added the changelog-language Language features and metaprograms label Oct 11, 2026
@arthur-adjedj
arthur-adjedj marked this pull request as ready for review October 11, 2026 12:40
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]>
Comment thread tests/elab/2258.lean Resolved
@arthur-adjedj

Copy link
Copy Markdown
Contributor Author

!bench

@leanprover-radar

leanprover-radar commented Oct 12, 2026 •

Copy link
Copy Markdown

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

  • 🟥 build//instructions: +7.3G (+0.07%)

Large changes (1✅, 3🟥)

  • ✅ elab/big_struct_dep1//instructions: -80.2M (-1.74%)
  • 🟥 elab/lift_lets_dag//instructions: +106.9M (+0.92%)
  • 🟥 elab/lift_lets_dag//task-clock: +531ms (+58.10%)
  • 🟥 elab/lift_lets_dag//wall-clock: +532ms (+57.46%)

Small changes (3✅, 38🟥)

  • 🟥 build/module/Init.Data.Iterators.Lemmas.Combinators.Take//instructions: +7.4M (+0.30%)
  • 🟥 build/module/Init.Data.Order.PackageFactories//instructions: +20.8M (+0.43%)
  • 🟥 build/module/Init.Data.Range.Polymorphic.SInt//instructions: +22.1M (+0.35%)
  • 🟥 build/module/Init.Data.Range.Polymorphic.UInt//instructions: +22.5M (+0.55%)
  • 🟥 build/module/Init.Data.Rat.Lemmas//instructions: +45.5M (+0.46%)
  • 🟥 build/module/Init.Data.SInt.Bitwise//instructions: +61.3M (+0.57%)
  • 🟥 build/module/Init.Data.String.Lemmas.Pattern.Char//instructions: +18.8M (+0.47%)
  • 🟥 build/module/Init.Data.String.Lemmas.Pattern.TakeDrop.Basic//instructions: +22.6M (+0.38%)
  • 🟥 build/module/Init.Data.String.Lemmas.Pattern.TakeDrop.Pred//instructions: +17.5M (+0.33%)
  • 🟥 build/module/Init.Data.String.Lemmas.Splits//instructions: +14.9M (+0.31%)
  • 🟥 build/module/Init.Data.String.Subslice//instructions: +2.5M (+0.24%)
  • 🟥 build/module/Init.Data.UInt.Bitwise//instructions: +87.8M (+0.45%)
  • 🟥 build/module/Lean.Compiler.LCNF.ToLCNF//instructions: +64.7M (+0.43%)
  • 🟥 build/module/Lean.Compiler.NameMangling//instructions: +22.7M (+0.40%)
  • 🟥 build/module/Lean.Meta.Basic//instructions: +50.9M (+0.26%)
  • 🟥 build/module/Lean.Meta.Constructions.BRecOn//instructions: +15.3M (+0.36%)
  • 🟥 build/module/Lean.Meta.Constructions.CasesOn//instructions: +8.0M (+0.45%)
  • 🟥 build/module/Lean.Meta.Constructions.CtorElim//instructions: +21.5M (+0.70%)
  • 🟥 build/module/Lean.Meta.ExprDefEq//instructions: +330.3M (+1.60%) (reduced significance based on *//lines)
  • 🟥 build/module/Lean.Meta.SplitSparseCasesOn//instructions: +10.2M (+0.39%)
  • and 21 more

@nomeata

nomeata commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Another complaint from Claude:

#15372 introduces a new non-termination. Take structure S (T : Type) where x : T, and a type LoopTy with no normal form (built from the Coquand-paradox proof). The false statement ∀ a b : S LoopTy, a = b is rejected immediately on v4.34.1, but the PR's kernel doesn't terminate. On the failure path, its unit-like check normalises the field type LoopTy, which nothing normalised before. This seems inherent to deciding unit-likeness by type, so a fix probably can't avoid it entirely. But a statement that used to be rejected promptly now hangs

Ok, maybe not so bad given that we start with a loopy type to begin with.

@arthur-adjedj

Copy link
Copy Markdown
Contributor Author

Another complaint from Claude:

#15372 introduces a new non-termination. Take structure S (T : Type) where x : T, and a type LoopTy with no normal form (built from the Coquand-paradox proof). The false statement ∀ a b : S LoopTy, a = b is rejected immediately on v4.34.1, but the PR's kernel doesn't terminate. On the failure path, its unit-like check normalises the field type LoopTy, which nothing normalised before. This seems inherent to deciding unit-likeness by type, so a fix probably can't avoid it entirely. But a statement that used to be rejected promptly now hangs

Ok, maybe not so bad given that we start with a loopy type to begin with.

I don't think this complaint is relevant. Once the rest of the renovations are merged, all types should strongly normalize.

Comment thread src/kernel/type_checker.cpp Resolved
@Kha

Kha commented Oct 20, 2026

Copy link
Copy Markdown
Member

@nomeata Should the cache be a realizable value instead as it only depends on the declaration? I can give that a try

if (!is_constant(I))
return false;
name I_name = const_name(I);
if (!is_non_rec_structure(env(), I_name))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!is_non_rec_structure(env(), I_name))
// same check as in try_eta_struct_core
if (!is_non_rec_structure(env(), I_name))

@nomeata

nomeata commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

@nomeata Should the cache be a realizable value instead as it only depends on the declaration? I can give that a try

It also depends on the instantiation, so unfortunately not.

Comment thread src/Lean/Meta/ExprDefEq.lean Resolved
@arthur-adjedj

Copy link
Copy Markdown
Contributor Author

!bench

@leanprover-radar

leanprover-radar commented Oct 20, 2026 •

Copy link
Copy Markdown

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

  • 🟥 build//instructions: +12.7G (+0.12%)

Large changes (2🟥)

  • 🟥 elab/big_struct_dep1//instructions: +629.5M (+13.68%)
  • 🟥 elab/lift_lets_dag//instructions: +106.9M (+0.92%)

Medium changes (2🟥)

  • 🟥 build/module/Std.Data.DHashMap.Internal.RawLemmas//instructions: +1.6G (+0.67%)
  • 🟥 misc/import Std.Data.DHashMap.Internal.RawLemmas//instructions: +1.6G (+0.75%)

Small changes (1✅, 222🟥)

  • 🟥 build/module/Init.Data.Array.Attach//instructions: +18.0M (+0.19%)
  • 🟥 build/module/Init.Data.Array.QSort.Basic//instructions: +19.2M (+0.20%)
  • 🟥 build/module/Init.Data.BitVec.Bitblast//instructions: +141.9M (+0.30%)
  • 🟥 build/module/Init.Data.BitVec.Lemmas//instructions: +137.3M (+0.12%)
  • 🟥 build/module/Init.Data.BitVec.Package//instructions: +1.8M (+0.27%)
  • 🟥 build/module/Init.Data.Char.Lemmas//instructions: +2.0M (+0.25%)
  • 🟥 build/module/Init.Data.Dyadic.Instances//instructions: +2.0M (+0.24%)
  • 🟥 build/module/Init.Data.Fin.Lemmas//instructions: +18.3M (+0.18%)
  • 🟥 build/module/Init.Data.Fin.Package//instructions: +2.3M (+0.32%)
  • 🟥 build/module/Init.Data.Iterators.Lemmas.Combinators.Append//instructions: +8.2M (+0.30%)
  • 🟥 build/module/Init.Data.Iterators.Lemmas.Combinators.FilterMap//instructions: +48.9M (+0.29%)
  • 🟥 build/module/Init.Data.Iterators.Lemmas.Combinators.FlatMap//instructions: +27.6M (+0.48%)
  • 🟥 build/module/Init.Data.Iterators.Lemmas.Combinators.Monadic.FilterMap//instructions: +64.6M (+0.20%)
  • 🟥 build/module/Init.Data.Iterators.Lemmas.Combinators.Monadic.ULift//instructions: +5.1M (+0.23%)
  • 🟥 build/module/Init.Data.Iterators.Lemmas.Combinators.Take//instructions: +13.5M (+0.54%)
  • 🟥 build/module/Init.Data.Iterators.Lemmas.Combinators.ULift//instructions: +3.4M (+0.23%)
  • 🟥 build/module/Init.Data.Iterators.Lemmas.Consumers.Access//instructions: +970.0k (+0.16%)
  • 🟥 build/module/Init.Data.Iterators.PostconditionMonad//instructions: +6.6M (+0.28%)
  • 🟥 build/module/Init.Data.List.Attach//instructions: +11.2M (+0.15%)
  • 🟥 build/module/Init.Data.List.Sort.Lemmas//instructions: +58.6M (+0.41%)
  • and 202 more
  • and 1 hidden

@nomeata
nomeata enabled auto-merge October 20, 2026 12:14
@nomeata
nomeata added this pull request to the merge queue Oct 20, 2026
Merged via the queue into leanprover:master with commit 43d3355 Oct 20, 2026
17 checks passed
downstream-lean4 Bot added a commit to leanprover/downstream-lean4 that referenced this pull request Oct 21, 2026
This is the adaptation PR for leanprover/lean4#15372.

---------

Co-authored-by: downstream-lean4[bot] <downstream-lean4[bot]@users.noreply.github.com>
Co-authored-by: arthur-adjedj <[email protected]>
nomeata pushed a commit that referenced this pull request Nov 9, 2026
…ctor

This PR regenerates InLean from con-leche, which now has the two η rules
of #15372 and #15373, verified: a type is unit-like when its head normal
form is a function type into a unit-like type or a structure whose fields
are all proofs or unit-like (`unitLikeLoopI`), and a partially applied
structure constructor is compared with the other side after both are
applied to fresh variables up to the full application
(`structEtaExpandI`). `elab/2258`, `elab/match4`, `elab/13371` and
`elab/12520_1` now pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
arthur-adjedj added a commit to arthur-adjedj/lean4 that referenced this pull request Nov 15, 2026
arthur-adjedj added a commit to arthur-adjedj/lean4 that referenced this pull request Nov 17, 2026
meerkatone pushed a commit to meerkatone/lean4 that referenced this pull request Nov 17, 2026
…6443)

This PR reverts leanprover#15372. The PR introduced a performance regression,
adding around 2.2% instructions to Mathlib. The feature will still be
merged again in the future, once better caching solutions have been
found.
@Garmelon

Copy link
Copy Markdown
Contributor

This PR was reverted by #16443, so I'm removing it from the changelog.

@Garmelon Garmelon added changelog-no Do not include this PR in the release changelog and removed changelog-language Language features and metaprograms labels Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-no Do not include this PR in the release changelog 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.

DefEq transitivity failures for unit-like eta

6 participants