feat: add Environment.hasExposedBody helper#13868
Open
kim-em wants to merge 4 commits into
Open
Conversation
The idiom env.setExporting true |>.find? n |>.any (·.hasValue) answers a real, self-contained question — does `env` export a body for `n` to downstream modules? — and was duplicated inline in at least eight places under five different local names (`exposed`, `exported`, `isExposed`, `bodyExposed`, plus inline uses). It also appears in Mathlib (`Mathlib/Tactic/Simps/Basic.lean`, `Mathlib/Tactic/Translate/Core.lean`). Pull it out as a small `Environment.hasExposedBody` helper with a docstring clarifying the corner cases (axioms, declarations not in the environment, the non-module case). Update all eight inline callers under `src/Lean/` to use it. Two of the callers (`Lean/Meta/Eqns.lean`, `Lean/Meta/MethodSpecs.lean`) wrap the check with `!env.header.isModule || …` so that the non-module case is treated as exposed; they keep that guard explicitly, calling `env.hasExposedBody n` for the inner check. This is a pure refactor; no behaviour change is intended.
Per review: - Rewrite the `hasExposedBody` docstring. The previous wording said `n` could be a "definition (or `instance`, `theorem`, etc.)" — but `ConstantInfo.hasValue` with the default `allowOpaque := false` returns `true` only for `defnInfo`, so the function returns `false` for theorems and opaques. State that plainly, and re-frame the non-module note: the suggested `!env.header.isModule || env.hasExposedBody n` pattern is a *policy* (bypass the body-exposed check outside modules) rather than a fact about `n`'s body. - In `Lean/Meta/MethodSpecs.lean`, bind `env` once rather than calling `(← getEnv)` twice in the same line. - Add a small test in `tests/elab/hasExposedBody.lean` covering exposed `def`, sealed `def`, theorem, opaque, axiom, inductive, and a missing name.
Environment.hasExposedBodyThe helper was placed right after setExporting (line ~660), but Environment.find? isn't defined until line ~855 in the same file. Move the helper to right after find? so it's visible at the use site.
kim-em
added a commit
to kim-em/mathlib4
that referenced
this pull request
May 28, 2026
Vierkantor flagged that the `setExporting true |>.find? n |>.any (·.hasValue)` idiom should really live in core. leanprover/lean4#13868 adds `Environment.hasExposedBody` for exactly this. Leave the inline check in place for now (so the PR remains buildable on the current toolchain), and add a note next to it pointing at the follow-up refactor. Also note the other in-tree caller of the same idiom in `Mathlib/Tactic/Translate/Core.lean` even though it is strictly off-topic for this PR — it'll save a future grep.
|
Mathlib CI status (docs):
|
Collaborator
|
Reference manual CI status:
|
`guard` needs an `Alternative` instance, which `CommandElabM` lacks, so the test failed to elaborate. Use `unless`/`if` + `throwError` instead, which need no `Alternative`.
Kha
approved these changes
May 29, 2026
Comment on lines
+844
to
+856
| /-- Returns `true` iff `n` resolves, in `env`'s exported view, to a `def` with a value. | ||
|
|
||
| Concretely this checks whether downstream modules can see a reducible body for `n` | ||
| (an exposed definition or an `abbrev`). Returns `false` for theorems and opaque | ||
| declarations (this uses `hasValue` with `allowOpaque := false`), axioms, inductives, | ||
| constructors, recursors, declarations not in the environment, and `def`s whose body | ||
| is sealed by the module system. | ||
|
|
||
| Outside the module system, `setExporting true` is a no-op, so this collapses to | ||
| "does `n` resolve to a `def` in the current environment?". Callers that instead | ||
| want to *bypass* the body-exposed check entirely outside modules (e.g. for name- | ||
| privacy decisions, where there is no sealing boundary anyway) should write that | ||
| policy explicitly: `!env.header.isModule || env.hasExposedBody n`. -/ |
Member
There was a problem hiding this comment.
Too much information. "Checks if, in the public scope (Environment.isExporting), the given name refers to a definition with a visible body, i.e. ConstantInfo.hasValue. Recall that outside the module system, this is any definition."
I'm pretty sure the !isModule cases are simply wrong
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds
Lean.Environment.hasExposedBody— a small helper that asks "doesenvexport a body fornto downstream modules?". The idiomwas duplicated inline in at least eight places in
src/Lean/, under five different local names (exposed,exported,isExposed,bodyExposed, plus inline uses). It also appears in Mathlib (Mathlib/Tactic/Simps/Basic.lean,Mathlib/Tactic/Translate/Core.lean).Pull it out, document the corner cases (axioms, declarations not in the environment, the non-module case), and update all eight inline call sites under
src/Lean/:Lean/Elab/Deriving/Basic.leanLean/Elab/PreDefinition/{WF,Structural,PartialFixpoint}/Eqns.leanLean/Meta/Constructions/SparseCasesOn.leanLean/Meta/Eqns.leanLean/Meta/MethodSpecs.leanThe last two wrap the check with
!env.header.isModule || …to treat the non-module case uniformly as exposed; they keep that guard explicitly, callingenv.hasExposedBody nfor the inner check.One related site in
Lean/Elab/Print.leanuses.any (·.isDefinition)instead of.any (·.hasValue)(a strictly narrower check that only matchesdefs, not theorems/opaques), so it's left as-is rather than coerced into the new helper.This is a pure refactor; no behaviour change is intended.
🤖 Prepared with Claude Code