Skip to content

fix: build casesOn for propositions from projections - #14925

Merged
nomeata merged 4 commits into
masterfrom
joachim/prop-casesOn-projections
Aug 25, 2026
Merged

nomeata merged 4 commits into
masterfrom
joachim/prop-casesOn-projections

Conversation

@nomeata

@nomeata nomeata commented Aug 25, 2026 •

Copy link
Copy Markdown
Collaborator

This PR makes casesOn and recOn of a proposition apply the minor premise to the projections of the major premise, instead of going through the recursor. A recursor only reduces once its major premise is a constructor application, which a proof may never become, so And.casesOn and friends no longer require the proof itself to reduce.

This matters for proofs that abstractNestedProofs has moved into auxiliary theorems, whose values are not available for reduction: the module system does not expose them outside their defining module, and theorems are opaque. An abstracted proof of an And that is stored in a Subtype and later pattern-matched, or eliminated into data directly, otherwise makes the enclosing definition ill-typed.

Lean.mkCasesOnViaProjs? applies to a proposition with a single constructor, no indices and no recursion, whose recursor supports large elimination. Large elimination forces every field to be a proof, which is what makes the projections available, since projecting data out of a proposition is what Exists may not do. The minor premise expects the fields of I.mk x₁ … xₙ rather than those of the major premise, but both are proofs of the same proposition, so proof irrelevance identifies them.

Such a type is neither recursive nor indexed, so its recOn has the same type as its casesOn, and Lean.mkRecOnViaCasesOn? simply defines the former to be the latter. mkAuxConstructions therefore builds casesOn before recOn.

tests/elab_fail/etaStructIssue.lean moves to tests/elab: both of its sides now reduce to mkNat e applied to a proof of F e, which proof irrelevance identifies, and the elaborator and the kernel agree on that.

On the cost: the kernel infers the type of a projection by walking the constructor's telescope up to the projected field, so kernel-checking a casesOn built this way is quadratic in the number of fields. tests/elab_bench/bv_stress_structures_{1,2}.lean declare five 201-field propositions and so hit this squarely: +21% and +18% instructions when both casesOn and recOn were built from projections, halved to +8.5% and +7.1% by having recOn reuse casesOn. Applying the projection functions rather than raw Expr.proj would make the check linear (measured 7x faster at 400 fields), but they are not generated yet when mkAuxConstructions runs; memoizing the telescope walk in type_checker::infer_proj would remove the regression entirely. Neither is pursued here, as propositions with that many fields are rare.

This PR makes `casesOn` and `recOn` of a proposition apply the minor premise to the projections of the major premise, instead of going through the recursor. A recursor only reduces once its major premise is a constructor application, which a proof may never become, so `And.casesOn` and friends no longer require the proof itself to reduce.

This matters for proofs that `abstractNestedProofs` has moved into auxiliary theorems, whose values are not available for reduction: the module system does not expose them outside their defining module, and theorems are opaque. An abstracted proof of an `And` that is stored in a `Subtype` and later pattern-matched, or eliminated into data directly, otherwise makes the enclosing definition ill-typed.

`Lean.mkCasesOnViaProjs?` applies to a proposition with a single constructor, no indices and no recursion, whose recursor supports large elimination. Large elimination forces every field to be a proof, which is what makes the projections available, since projecting data out of a proposition is what `Exists` may not do. The minor premise expects the fields of `I.mk x₁ … xₙ` rather than those of the major premise, but both are proofs of the same proposition, so proof irrelevance identifies them.

`tests/elab_fail/etaStructIssue.lean` moves to `tests/elab`: both of its sides now reduce to `mkNat e` applied to a proof of `F e`, which proof irrelevance identifies, and the elaborator and the kernel agree on that.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01XzL163q5FYpbZmT8EHQb3j
@nomeata nomeata added the changelog-language Language features and metaprograms label Aug 25, 2026
@nomeata

nomeata commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator Author

!bench

@leanprover-radar

leanprover-radar commented Aug 25, 2026 •

Copy link
Copy Markdown

Benchmark results for 8f5e057 against 47680c2 are in. There are significant results. @nomeata

  • ✅ build//instructions: -1.4G (-0.01%)

Large changes (1✅, 2🟥)

  • ✅ compiled/rbmap_checkpoint//instructions: -735.1M (-5.47%)
  • 🟥 elab/bv_stress_structures_1//instructions: +3.4G (+23.09%)
  • 🟥 elab/bv_stress_structures_2//instructions: +3.5G (+19.83%)

Medium changes (1🟥)

  • 🟥 compiled/rbmap_checkpoint//maxrss: +2MiB (+0.08%)

Small changes (3✅, 4🟥)

  • 🟥 build/module/Lean.Meta.Constructions.CasesOn//instructions: +622.7M (+46.42%) (reduced significance based on *//lines)
  • 🟥 build/module/Lean.Meta.Constructions.RecOn//instructions: +39.6M (+3.22%)
  • ✅ compiled/rbmap_checkpoint//task-clock: -101ms (-6.33%)
  • ✅ compiled/rbmap_checkpoint//wall-clock: -103ms (-6.42%)
  • 🟥 elab/bv_stress_structures_1//wall-clock: +258ms (+24.21%)
  • 🟥 elab/bv_stress_structures_2//wall-clock: +254ms (+20.95%)
  • ✅ elab/whnfMatcherImplicitTransparencyCaching//instructions: -69.0M (-0.28%)

@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 Aug 25, 2026
@mathlib-lean-pr-testing

mathlib-lean-pr-testing Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 47680c2e62831da4daa01abe567b3e4fc509fbae --onto fd0efc4306a7773c2cd4e079ddaa907426d0f5da. You can force Mathlib CI using the force-mathlib-ci label. (2026-08-25 10:16:43)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 0dc10a41e6be57ef72d7f103c98c263620076b6c --onto fd0efc4306a7773c2cd4e079ddaa907426d0f5da. You can force Mathlib CI using the force-mathlib-ci label. (2026-08-25 11:44:59)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 0dc10a41e6be57ef72d7f103c98c263620076b6c --onto 4620e4d946957ae7c582764c79eb93cc779f77e2. You can force Mathlib CI using the force-mathlib-ci label. (2026-08-25 21:26:38)

@leanprover-bot

leanprover-bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 47680c2e62831da4daa01abe567b3e4fc509fbae --onto 16e77c407779fde9a649adf3478204d1915371a3. You can force reference manual CI using the force-manual-ci label. (2026-08-25 10:16:45)
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 0dc10a41e6be57ef72d7f103c98c263620076b6c --onto 16e77c407779fde9a649adf3478204d1915371a3. You can force reference manual CI using the force-manual-ci label. (2026-08-25 11:45:01)

nomeata and others added 3 commits August 25, 2026 10:51
Replaces the inline level-parameter comparison with the predicate from #14909, which documents the extra-motive-universe encoding at its own definition.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01GSbnoYQjyFAGmX75t7snrX
A type eligible for `mkCasesOnViaProjs?` is neither recursive nor indexed, so its `recOn` has the same type as its `casesOn`. `mkRecOnViaCasesOn?` defines the former to be the latter instead of building the projections a second time, and `mkAuxConstructions` builds `casesOn` first.

The kernel infers the type of a projection by walking the constructor's telescope up to the projected field, so kernel-checking a `casesOn` built this way is quadratic in the number of fields. This halves that cost: `elab/bv_stress_structures_1` goes from +21% to +8.5% instructions over `master`, `elab/bv_stress_structures_2` from +18% to +7.0%.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@nomeata

nomeata commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator Author

!bench

@leanprover-radar

leanprover-radar commented Aug 25, 2026 •

Copy link
Copy Markdown

Benchmark results for 4be7849 against 0dc10a4 are in. There are significant results. @nomeata

  • ✅ build//instructions: -139.5M (-0.00%)

Large changes (2🟥)

  • 🟥 elab/bv_stress_structures_1//instructions: +1.4G (+9.46%)
  • 🟥 elab/bv_stress_structures_2//instructions: +1.4G (+8.08%)

Small changes (1✅, 2🟥)

  • 🟥 build/module/Lean.Meta.Constructions.CasesOn//instructions: +667.9M (+49.54%) (reduced significance based on *//lines)
  • 🟥 build/module/Lean.Meta.Constructions.RecOn//instructions: +132.2M (+10.73%) (reduced significance based on *//lines)
  • ✅ elab/whnfMatcherImplicitTransparencyCaching//instructions: -50.5M (-0.21%)

@nomeata
nomeata added this pull request to the merge queue Aug 25, 2026
Merged via the queue into master with commit 30ad6f5 Aug 25, 2026
21 checks passed
@nomeata nomeata mentioned this pull request Aug 29, 2026
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 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