Skip to content

feat: theorems are opaque - #12973

Merged
nomeata merged 5 commits into
masterfrom
joachim/opaque-thm
Mar 23, 2026
Merged

nomeata merged 5 commits into
masterfrom
joachim/opaque-thm

Conversation

@nomeata

@nomeata nomeata commented Mar 18, 2026 •

Copy link
Copy Markdown
Collaborator

This PR makes theorems opaque in almost all ways, including in the kernel.

Already now, because of proof irrelevance, theorems are almost never unfolded. Furthermore, the import handling allows conflicting theorem declaration with same type and different values. This is sound, but would be confusing if the value, and thus the import order, matters for completeness.

So with this change, a theorem becomes more like an opaque: It has a value (for soundness), but it is never unfolded during reduction or type checking. There are still some places in meta code that have to peek into theorems (e.g. FunInd, wfrec processing), but these are code transformations, not reduction.

One place where reducing proofs is necessary is reducing Acc.rec eliminating into Type. With this change, all proofs that need to be reducable that way have to be def, not theorem. This is already the case due to the module system. This does not affect uses of Acc via well-founded recursion, because that has already been made opaque in #5182. This moves the reduction behavior of Acc.rec further into the “supported by the theory but not relied upon by regular Lean“ corner.

Fixes #12804

nomeata and others added 3 commits March 18, 2026 18:41
This PR make theorems completely opaque, similar to `opaque` declarations:

- Kernel: `has_value()` returns false for theorems, preventing delta reduction
- Elaborator: `value?`, `hasValue`, `value\!` require `allowOpaque := true`
  for theorems (same as for opaques)
- WHNF never unfolds theorems, even at `TransparencyMode.all`
- The `delta` tactic rejects theorems
- WF-recursive theorems no longer get `@[irreducible]` (redundant)

Internal code that legitimately needs theorem values (e.g. `@[defeq]`
inference, `foldAndCollect` in FunInd, constant dependency tracking,
proof abstraction unfolding) explicitly opts in with `allowOpaque := true`.

Closes #12804

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Instead of using `unfoldDefinition?` (which involves smart unfolding,
transparency checks, etc.), FunInd now directly does delta+beta using
`value? (allowOpaque := true)` and `betaRev` to look through proof
theorems in `foldAndCollect`.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Split the test into `thmsOpaque.lean` (theorems are opaque) and
`issue12804.lean` (reducibility attributes rejected on theorems).

Revert the unnecessary `allowOpaque := true` in simp's `unfold?` — the
arity check is dead code for theorems since `unfoldDefinitionAny?`
won't unfold them anyway.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@nomeata nomeata added the changelog-language Language features and metaprograms label Mar 18, 2026
@nomeata

nomeata commented Mar 18, 2026

Copy link
Copy Markdown
Collaborator Author

!radar

@leanprover-radar

leanprover-radar commented Mar 18, 2026 •

Copy link
Copy Markdown

Benchmark results for c7831b9 against 6714601 are in. There are no significant changes. @nomeata

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

Small changes (1✅, 3🟥)

  • 🟥 build/module/Lean.Elab.PreDefinition.Mutual//instructions: +24.0M (+1.83%)
  • ✅ build/module/Lean.Meta.GetUnfoldableConst//instructions: -21.9M (-1.86%)
  • 🟥 elab/charactersIn//task-clock: +121ms (+5.59%)
  • 🟥 elab/charactersIn//wall-clock: +124ms (+5.68%)

@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 Mar 18, 2026
@leanprover-bot

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-03-17 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-03-18 21:25:01)

mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 18, 2026
@github-actions github-actions Bot added the mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN label Mar 18, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 18, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added the breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan label Mar 18, 2026
@mathlib-lean-pr-testing

Copy link
Copy Markdown

Mathlib CI status (docs):

This allows callers like mathlib's `to_additive` to delta-expand
theorem auxiliary lemmas (e.g. `_simp_*`, `_gcongr_*`).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@mathlib-lean-pr-testing

Copy link
Copy Markdown

Mathlib CI status (docs):

mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 19, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 19, 2026
@mathlib-lean-pr-testing

Copy link
Copy Markdown

Mathlib CI status (docs):

@mathlib-lean-pr-testing

Copy link
Copy Markdown

Mathlib CI status (docs):

@mathlib-lean-pr-testing

Copy link
Copy Markdown

Mathlib CI status (docs):

@mathlib-lean-pr-testing

Copy link
Copy Markdown

Mathlib CI status (docs):

@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added builds-mathlib CI has verified that Mathlib builds against this PR and removed breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan labels Mar 19, 2026
@mathlib-lean-pr-testing

Copy link
Copy Markdown

Mathlib CI status (docs):

@nomeata
nomeata marked this pull request as ready for review March 20, 2026 08:51
@nomeata
nomeata requested a review from leodemoura as a code owner March 20, 2026 08:51
Comment thread src/kernel/declaration.h Resolved
Comment thread src/kernel/declaration.h Resolved
Comment thread src/Lean/Declaration.lean Resolved
Comment thread src/Lean/Declaration.lean Resolved
Comment thread src/Lean/Declaration.lean Resolved
Co-authored-by: Joachim Breitner <[email protected]>
@nomeata
nomeata enabled auto-merge March 23, 2026 13:43
@nomeata
nomeata added this pull request to the merge queue Mar 23, 2026
Merged via the queue into master with commit 720cbd6 Mar 23, 2026
18 checks passed
nomeata added a commit that referenced this pull request Aug 25, 2026
This PR completes #12973: theorems are now also opaque to the kernel, which until now would still delta-reduce them. So the value of a theorem no longer affects whether a declaration type-checks, and conflicting theorem declarations with the same type can no longer make type checking depend on import order.

The `Lean`-side `ConstantInfo.hasValue` was already adjusted in #12973, but the kernel uses the C++ `constant_info::has_value`, which was missed. That is what `type_checker::is_delta` consults, so `Acc.rec` applications, for instance, still reduced through theorem proofs in the kernel even though the elaborator refused to.

Also switches `has_constructor` in `src/library/util.cpp` to an explicit `is_constructor` check, so it no longer piggybacks on `has_value`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01XzL163q5FYpbZmT8EHQb3j
yermakoffivan pushed a commit to yermakoffivan/lean4 that referenced this pull request Nov 23, 2026
This PR completes leanprover#12973: theorems are now opaque to the kernel as well,
which until now would still delta-reduce them. The value of a theorem no
longer affects whether a declaration type-checks, so conflicting theorem
declarations of the same type can no longer make type checking depend on
import order.

The `Lean`-side `ConstantInfo.hasValue` was already adjusted in leanprover#12973,
but the kernel consults the C++ `constant_info::has_value`, which was
missed. That is what `type_checker::is_delta` looks at, so the kernel's
`whnf` still unfolded theorems even though the elaborator refused to.

`has_constructor` in `src/library/util.cpp` switches to an explicit
`is_constructor` check, so it no longer piggybacks on `has_value`.

This is part of the kernel renovation leanprover#14977.

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-mathlib CI has verified that Mathlib builds against this PR changelog-language Language features and metaprograms mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN 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.

Theorems proved by well-founded recursion are confusingly marked @[irreducible]

3 participants