Disentinel/grafema / .claude/skills/lean4-theorem-value-access
lean4-theorem-value-access
VerifiedFix missing theorem proof terms when analyzing Lean 4 environments via importModules. Use when: (1) ConstantInfo.value? returns none for theorems despite TheoremVal.value being Expr, (2) building code graph / dependency extractor for Lean 4 and getting 0 proof dependency edges, (3) Lean 4.30+ project where theorem proofs appear missing from loaded environment, (4) analyzing Mathlib or any Lean 4 project and proof terms are empty. Root cause: breaking change in Lean 4.30 — value? treats theorems as opaque by default.
Lean 4.30+ Theorem Value Access Breaking Change
Problem
When loading a Lean 4 environment via importModules and iterating over constants,
ConstantInfo.value? returns none for ALL theorems, even though the proof terms
are present in the .olean.private files and loaded into memory.
Context / Trigger Conditions
- Building a Lean 4 code analyzer, dependency extractor, or proof graph tool
- Using
importModulesto load an environment (e.g., Mathlib) ci.value?returnsnonefor.thmInfoconstants- 0 proof dependency edges in output despite theorems existing
- Lean toolchain version is 4.30.0-rc1 or later
Root Cause
Breaking change between Lean 4.29.1 and 4.30.0-rc2 in ConstantInfo.value?:
-- Lean 4.29.1 (old behavior):
| .thmInfo {value, ..} => some value
-- Lean 4.30.0-rc2 (new behavior):
| .thmInfo {value, ..} => if allowOpaque then some value else none
In 4.30+, theorems are treated the same as opaqueInfo — their values are hidden
unless allowOpaque := true is explicitly passed. The default allowOpaque = false
causes value? to return none for all theorems.
The proof terms ARE loaded into memory (.olean.private files contain them, and
importModules loads at OLeanLevel.private by default). The data is there — the
accessor just hides it.
Solution
Option A: Pass allowOpaque := true (simplest fix)
if let some val := ci.value? (allowOpaque := true) then
-- val is the proof term
Option B: Pattern match directly (most reliable)
let valInfo := match ci with
| .defnInfo v => some (v.value, "VALUE_USES")
| .thmInfo v => some (v.value, "PROOF_USES")
| _ => none
if let some (val, edgeType) := valInfo then
let deps := val.getUsedConstantsAsSet
-- process deps
Option B is preferred for tools that need to distinguish definition bodies from
proof terms, since allowOpaque := true conflates theorems with opaque declarations.
Verification
-- This should print true for theorems in Lean 4.30+:
let some ci := env.find? `SomeTheorem | ...
match ci with
| .thmInfo v =>
eprintln s!"value? default: {ci.value?.isSome}" -- false
eprintln s!"value? opaque: {(ci.value? (allowOpaque := true)).isSome}" -- true
eprintln s!"direct access: {v.value.getUsedConstants.size}" -- >0
| _ => ...
Related Facts
.oleanfiles have THREE levels:.olean(public),.olean.server(IDE),.olean.private(full proofs)importModulesdefaults toOLeanLevel.private— proofs are loadedExpr.foldConstssignature:(e : Expr) (init : α) (f : Name → α → α) : α— Name is first arg, accumulator secondExpr.getUsedConstantsAsSetreturnsNameSet(=Std.TreeSet Name), NOTNameHashSet(=HashSet Name)NameSetdoesn't have.fold— usefor dep in nameSet doinstead- Mathlib scale: 354K declarations, 11.3M edges (including 4.4M PROOF_USES)
Notes
- This change aligns with proof irrelevance: for type checking, proof content doesn't matter. Tools that DO need proofs (checkers, analyzers, graph builders) must opt in.
- The
value!function also changed — it panics for theorems unlessallowOpaque := true. ConstantInfo.getUsedConstantsAsSet(on ConstantInfo, not Expr) also usesvalue?internally, so it will miss proof dependencies too.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |