A theorem proof can materialize public realizations into .olean and invalidate unrelated importers

Open
#16,959 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
28/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Domain
compilers

Research direction

Start by tracing the four realization entry points named in the issue: Lean.Meta.Match.MatchEqs.getEquationsForImpl, Lean.Elab.PreDefinition.Eqns.mkEqns, Lean.Meta.getUnfoldEqnFor?, and Lean.Meta.Match.MatchEqs / Match.mkMatcher. Reproduce the U/A/B1 example and inspect A's module data parts to see which proof-triggered realizations enter the public export view. The issue asks for a policy for private storage, monotonic promotion, declaration-keyed attributes, and import versus import all; the reported prototype and benchmarks provide validation targets.

Written by the indexing model from the issue text.

Description

Tested on the unmodified Lean 4.36.0 Linux release (611c117bfaf7b7ae1cfcf246ce6fba31e8f509e9).

Changing only the body of a theorem can materialize an on-demand realization (such as an equation lemma, unfold equation, or matcher splitter) into the module's public .olean. Lake then recompiles ordinary importers whose source and outputs are unchanged, and early interface publication / experimental.module workflows experience high invalidation rates on real mathematical libraries.

Minimal reproduction

Create these files in an empty directory with no package dependencies:

# lean-toolchain
leanprover/lean4:v4.36.0
# lakefile.toml
name = "proof-realization-repro"
defaultTargets = ["Repro"]

[[lean_lib]]
name = "Repro"
roots = ["U", "A", "B1"]
-- U.lean
module

@[expose] public def f (n : Nat) : Nat := n + 1
-- A.lean
module
public import U

public theorem t : True := by
  exact True.intro
-- B1.lean
module
import A

public def answer : Nat := 42

Build twice, then save the public .olean hashes and downstream depHash:

export LAKE_ARTIFACT_CACHE=false
lake -v build +B1:leanArts
lake -v build +B1:leanArts
sha256sum .lake/build/lib/lean/A.olean*
jq -r '.depHash' .lake/build/lib/lean/B1.trace

Replace only the proof of t with the following body and repeat (keeping .lake/build):

public theorem t : True := by
  have : f 0 = 1 := by rw [f]
  exact True.intro
Observed behavior

After editing only the proof body of t, Lake rebuilds both A and B1. B1's depHash changes from ced19cfec71a2e61 to 9c8e4b201f09ae44, even though all three B1 .olean parts remain byte-identical.

Inspecting A's parts with Lean.readModuleDataParts shows f.eq_1 added as a theorem in the public, server, and private views, along with its backwardDefeqAttr entry and original-kind entry. A.olean's SHA-256 changes from a7479b253c8e57a79ce9926ae6f9a596d2bf3e38ea0f008047ef7b0d800045fc to bee7589f9ac14a754062e9bbab840406bafba3dc5de2b876eaeb9a7668fa5450. U's parts are unchanged.

Real-world impact on module libraries

Auditing real multi-module libraries (WeakPFR with 52 modules and ChanHe with 75 modules) under early interface publication surfaced four recurring proof-triggered realization entry points that enter the public export view:

  1. Lean.Meta.Match.MatchEqs.getEquationsForImpl — private matcher numbered equations (match_*.eq_*).
  2. Lean.Elab.PreDefinition.Eqns.mkEqns — private recursive numbered equations and backwardDefeqAttr projections.
  3. Lean.Meta.getUnfoldEqnFor? — structural/simple unfold equations (*.eq_def, e.g. via unfold inside a proof).
  4. Lean.Meta.Match.MatchEqs / Match.mkMatcher — non-overlapping safe matcher splitter abbreviations and their inlineAttrs registrations.

In addition, while auditing early-interface reuse against checked environments, we observed a related bug in Lean.extraModUses: its registered extension name carries a _private prefix (_private.Lean.ExtraModUses.0.Lean.extraModUses), whereas an extension rejection check compared against the unadorned name Lean.extraModUses, allowing modified dependency-tracking extensions to slip through generic replay and drop (module, isExported, isMeta) entries. Comparing privateToUserName ext.name restores the expected checked-environment fallback.

Prototype validation

We tested an official-recipe Stage2 Lean 4.36.0 prototype that marks audited batch boundaries for the four realization generators above, keeps proof-triggered batches in .olean.private when not referenced publicly, monotonically promotes a batch (closing its dependencies and preserving public representations/attributes) whenever a public declaration references it, and fixes the privateToUserName check on Lean.extraModUses (stacked with deterministic ExtraModUses serialization per #16875).

On 16 physical cores (P=L=16, same worktrees and prebuilt dependencies):

Project Lean -j Stock 4.36.0 Prototype EOI off / on Effective early-published modules Invalidations / retries
WeakPFR (52 modules) 16 74.52 s 72.92 s / 67.61 s (−9.27% vs stock) 46/52 (88.5%, 90.2% of producers) 0 / 0
WeakPFR (52 modules) 2 90.17 s 88.80 s / 76.02 s (−15.69% vs stock) 47/52 (90.4%, 92.2% of producers) 0 / 0
ChanHe (75 modules) 16 47.77 s 45.74 s / 41.25 s (−13.66% vs stock) 60/75 (80.0%, 81.1% of producers) 0 / 0
ChanHe (75 modules) 2 51.01 s 50.12 s / 44.13 s (−13.49% vs stock) 60/75 (80.0%, 81.1% of producers) 0 / 0

Across both repositories, all seven artifact types (.olean, .olean.server, .olean.private, .ilean, .ir.sig, .ir, .c) and all depHash values matched byte-for-byte between EOI off and EOI on (889/889 files, 127/127 depHash), and both regular import and import all fixtures passed.

Design question

Could realizations requested only inside theorem proofs stay in .olean.private unless a public caller requires them? Because on-demand realizations can currently be reused across modules, changing this requires a clear policy for monotonic promotion, declaration-keyed attributes (backwardDefeqAttr, inlineAttrs), and differences between regular import and import all. Starting with narrow audited batches (numbered equations, unfold equations, safe splitters) appears sufficient to eliminate nearly all proof-triggered public .olean churn in practice.

Dominant language
Lean
Stars
10.5k
Forks
1.2k
Avg merge
19h 36m
Merged PRs (30d)
323

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.