Skip to content

[Merged by Bors] - chore: make 639 public imports private where nothing downstream needs them - #46228

Closed
Vilin97 wants to merge 1 commit into
leanprover-community:masterfrom
Vilin97:privatize-imports-1
Closed

Vilin97 wants to merge 1 commit into
leanprover-community:masterfrom
Vilin97:privatize-imports-1

Conversation

@Vilin97

@Vilin97 Vilin97 commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

This PR turns 639 public import A lines into plain import A. No imports are added anywhere else.

The idea, in plain words

In a module file B:

  • public import A means: B uses A, and every file that imports B also gets A.
  • import A means: B uses A, but keeps it to itself.

We switch public import A to import A in B when both of these hold:

  1. B does not need A in anything it exports: theorem statements, the types of definitions, and the bodies of exposed definitions only use things B can see without A. Typically A is only used inside proofs.
  2. No file that imports B (directly or indirectly) relies on getting A through B: every such file that uses something from A already imports it some other way.

Because of (2), nothing else has to change: this PR only edits the import lines of the files whose imports become private.

Example

Mathlib/Data/Fin/Basic.lean had public import Mathlib.Data.Int.DivMod, but it only uses the Int.emod lemmas inside proofs, and every file downstream of Fin.Basic that uses Int.DivMod already reaches it another way. So it becomes import Mathlib.Data.Int.DivMod, and about 6,500 modules that import Fin.Basic no longer load Int.DivMod through it.

How the candidates were chosen

This is the same idea as #43918 (which found such imports by trial builds). Here the candidates come from the compiled .olean files, using the analysis lake shake already does (which modules each declaration's statement and proof depend on, split into public and private uses), with one addition: proofs nested inside exposed definitions count as public, since Lean elaborates them in the public scope. Since making one import private can make another one costly, the candidates are accepted one at a time, each re-checked against those already accepted.

Skipped on purpose:

  • module -- shake: keep-all files and deprecated-module shims, whose job is to re-export;
  • imports marked shake: keep (except the ones only needed for examples);
  • imports that would hide, downstream, a module that registers options, aesop rule sets, simp attributes or positivity extensions;
  • files using backward.privateInPublic;
  • a handful of imports whose demotion broke deriving or @[simps!] in a full build.

Overall, the total number of modules loaded across all Mathlib files (the sum of the import closures) goes down by about 1%, and 7,194 of 8,726 modules load fewer files.

Other changes

  • Mathlib/Combinatorics/SimpleGraph/Extremal/Turan.lean: grw [Nat.mod_le] now closes its goal, so the exact after it is removed.

As with #43918, this is a semantic change for downstream projects: something that used to arrive through B may now need an explicit import.

Checked with lake build Mathlib Archive Counterexamples and lake test.

Benchmark (!bench, against master): build//instructions −95.4G (−0.09%). The largest single change is Mathlib.Condensed.Discrete.Colimit (−31%); there are two small (about +1G, low-significance) per-module regressions. For comparison, #43918 measured −0.12%.

This is the first of a stack of three PRs, ordered from most to least conservative.

Use of AI

This PR was prepared with Claude Code (Claude Opus). It wrote the analysis tooling (a small Lean program built on lake shake's dependency computation, plus Python scripts that pick the imports and edit the headers), ran the builds that found and removed the bad candidates, and drafted this description. All edits to Mathlib/ are produced mechanically by those scripts. The hand fix listed above is the only non-header change.

🤖 Generated with Claude Code

@Vilin97

Vilin97 commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

!bench

@github-actions github-actions Bot added the new-contributor This PR was made by a contributor with at most 5 merged PRs. Welcome to the community! label Nov 19, 2026
@github-actions

Copy link
Copy Markdown

Welcome new contributor!

Thank you for contributing to Mathlib! If you haven't done so already, please review our contribution guidelines, as well as the style guide and naming conventions. In particular, we kindly remind contributors that we have guidelines regarding the use of AI when making pull requests.

We use a review queue to manage reviews. If your PR does not appear there, it is probably because it is not successfully building (i.e., it doesn't have a green checkmark), has the awaiting-author tag, or another reason described in the Lifecycle of a PR. The review dashboard has a dedicated webpage which shows whether your PR is on the review queue, and (if not), why.

If you haven't already done so, please come to Zulip and join the Lean community.
Thank you again for joining our community.

@leanprover-radar

leanprover-radar commented Nov 19, 2026 •

Copy link
Copy Markdown

Benchmark results for 41b22a9 against 11aadab are in. There are significant results. @Vilin97

  • ✅ build//instructions: -101.1G (-0.09%)

Large changes (1✅)

  • ✅ build/module/Mathlib.Condensed.Discrete.Colimit//instructions: -18.1G (-31.39%)

Medium changes (2🟥)

  • 🟥 build/module/Mathlib.AlgebraicTopology.SimplicialSet.Subcomplex//instructions: +1.1G (+7.75%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Mathlib.Topology.Sets.CompactOpenCovered//instructions: +1.3G (+4.71%) (reduced significance based on absolute threshold)

Small changes (534✅, 67🟥)

  • 601 hidden

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

PR summary aba48159f1

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff (regex)

No declarations were harmed in the making of this PR! 🐙

You can run this locally as follows
## from your `mathlib4` directory:
git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci

## summary with just the declaration names:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
../mathlib-ci/scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh in the mathlib-ci repository contains some details about this script.

Declarations diff (Lean)

✅ Lean-aware diff — post-build, computed from the Lean environment (commit aba4815).

  • +0 new declarations
  • −0 removed declarations

No declaration differences.


No changes to strong technical debt.
No changes to weak technical debt.

Current commit aba48159f1
Reference commit 426b896108

This script lives in the mathlib-ci repository. To run it locally, from your mathlib4 directory:

git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci
../mathlib-ci/scripts/reporting/technical-debt-metrics.py pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@Vilin97
Vilin97 force-pushed the privatize-imports-1 branch from 41b22a9 to 23c6e98 Compare November 19, 2026 09:32
@Vilin97 Vilin97 changed the title chore: make 660 public imports private where nothing downstream needs them chore: make 639 public imports private where nothing downstream needs them Nov 19, 2026
@Vilin97

Vilin97 commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator Author

!bench

@leanprover-radar

leanprover-radar commented Nov 19, 2026 •

Copy link
Copy Markdown

Benchmark results for 23c6e98 against 11aadab are in. There are significant results. @Vilin97

  • ✅ build//instructions: -95.4G (-0.09%)

Large changes (1✅)

  • ✅ build/module/Mathlib.Condensed.Discrete.Colimit//instructions: -18.1G (-31.38%)

Medium changes (2🟥)

  • 🟥 build/module/Mathlib.AlgebraicTopology.SimplicialSet.Subcomplex//instructions: +1.1G (+7.73%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Mathlib.Topology.Sets.CompactOpenCovered//instructions: +1.3G (+4.68%) (reduced significance based on absolute threshold)

Small changes (493✅, 71🟥)

  • 564 hidden

…ds them

Each `public import A` in a file `B` becomes `import A` when `B`'s public interface
(statements, types, exposed bodies) does not need `A`, and no module downstream of `B`
relies on getting `A` through `B`. No imports are added elsewhere.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Vilin97
Vilin97 force-pushed the privatize-imports-1 branch from 23c6e98 to aba4815 Compare November 20, 2026 17:33
@Vilin97
Vilin97 marked this pull request as ready for review November 20, 2026 17:33
@kim-em

kim-em commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

bors merge

@mathlib-bors mathlib-bors Bot added the ready-to-merge This PR has been sent to bors. label Nov 21, 2026
mathlib-bors Bot pushed a commit that referenced this pull request Nov 21, 2026
…ds them (#46228)

This PR turns 639 `public import A` lines into plain `import A`. No imports are added anywhere else.

### The idea, in plain words

In a `module` file `B`:
* `public import A` means: `B` uses `A`, **and** every file that imports `B` also gets `A`.
* `import A` means: `B` uses `A`, but keeps it to itself.

We switch `public import A` to `import A` in `B` when both of these hold:
1. `B` does not need `A` in anything it exports: theorem statements, the types of definitions, and the bodies of exposed definitions only use things `B` can see without `A`. Typically `A` is only used inside proofs.
2. No file that imports `B` (directly or indirectly) relies on getting `A` through `B`: every such file that uses something from `A` already imports it some other way.

Because of (2), nothing else has to change: this PR only edits the import lines of the files whose imports become private.

### Example

`Mathlib/Data/Fin/Basic.lean` had `public import Mathlib.Data.Int.DivMod`, but it only uses the `Int.emod` lemmas inside proofs, and every file downstream of `Fin.Basic` that uses `Int.DivMod` already reaches it another way. So it becomes `import Mathlib.Data.Int.DivMod`, and about 6,500 modules that import `Fin.Basic` no longer load `Int.DivMod` through it.

### How the candidates were chosen

This is the same idea as #43918 (which found such imports by trial builds). Here the candidates come from the compiled `.olean` files, using the analysis `lake shake` already does (which modules each declaration's statement and proof depend on, split into public and private uses), with one addition: proofs nested inside exposed definitions count as public, since Lean elaborates them in the public scope. Since making one import private can make another one costly, the candidates are accepted one at a time, each re-checked against those already accepted.

Skipped on purpose:
* `module -- shake: keep-all` files and deprecated-module shims, whose job is to re-export;
* imports marked `shake: keep` (except the ones only needed for `example`s);
* imports that would hide, downstream, a module that registers options, aesop rule sets, simp attributes or `positivity` extensions;
* files using `backward.privateInPublic`;
* a handful of imports whose demotion broke `deriving` or `@[simps!]` in a full build.

Overall, the total number of modules loaded across all Mathlib files (the sum of the import closures) goes down by about 1%, and 7,194 of 8,726 modules load fewer files.

### Other changes

* `Mathlib/Combinatorics/SimpleGraph/Extremal/Turan.lean`: `grw [Nat.mod_le]` now closes its goal, so the `exact` after it is removed.

As with #43918, this is a semantic change for downstream projects: something that used to arrive through `B` may now need an explicit import.

Checked with `lake build Mathlib Archive Counterexamples` and `lake test`.

Benchmark (`!bench`, against `master`): `build//instructions` **−95.4G (−0.09%)**. The largest single change is `Mathlib.Condensed.Discrete.Colimit` (−31%); there are two small (about +1G, low-significance) per-module regressions. For comparison, #43918 measured −0.12%.

This is the first of a stack of three PRs, ordered from most to least conservative.

### Use of AI

This PR was prepared with Claude Code (Claude Opus). It wrote the analysis tooling (a small Lean program built on `lake shake`'s dependency computation, plus Python scripts that pick the imports and edit the headers), ran the builds that found and removed the bad candidates, and drafted this description. All edits to `Mathlib/` are produced mechanically by those scripts. The hand fix listed above is the only non-header change.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@mathlib-bors mathlib-bors Bot added the bors-staging This PR is currently being built by bors on the staging branch. label Nov 21, 2026
@mathlib-bors

mathlib-bors Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

@mathlib-bors mathlib-bors Bot changed the title chore: make 639 public imports private where nothing downstream needs them [Merged by Bors] - chore: make 639 public imports private where nothing downstream needs them Nov 21, 2026
@mathlib-bors mathlib-bors Bot closed this Nov 21, 2026
TJHeeringa pushed a commit to TJHeeringa/mathlib4 that referenced this pull request Dec 2, 2026
…ds them (leanprover-community#46228)

This PR turns 639 `public import A` lines into plain `import A`. No imports are added anywhere else.

### The idea, in plain words

In a `module` file `B`:
* `public import A` means: `B` uses `A`, **and** every file that imports `B` also gets `A`.
* `import A` means: `B` uses `A`, but keeps it to itself.

We switch `public import A` to `import A` in `B` when both of these hold:
1. `B` does not need `A` in anything it exports: theorem statements, the types of definitions, and the bodies of exposed definitions only use things `B` can see without `A`. Typically `A` is only used inside proofs.
2. No file that imports `B` (directly or indirectly) relies on getting `A` through `B`: every such file that uses something from `A` already imports it some other way.

Because of (2), nothing else has to change: this PR only edits the import lines of the files whose imports become private.

### Example

`Mathlib/Data/Fin/Basic.lean` had `public import Mathlib.Data.Int.DivMod`, but it only uses the `Int.emod` lemmas inside proofs, and every file downstream of `Fin.Basic` that uses `Int.DivMod` already reaches it another way. So it becomes `import Mathlib.Data.Int.DivMod`, and about 6,500 modules that import `Fin.Basic` no longer load `Int.DivMod` through it.

### How the candidates were chosen

This is the same idea as leanprover-community#43918 (which found such imports by trial builds). Here the candidates come from the compiled `.olean` files, using the analysis `lake shake` already does (which modules each declaration's statement and proof depend on, split into public and private uses), with one addition: proofs nested inside exposed definitions count as public, since Lean elaborates them in the public scope. Since making one import private can make another one costly, the candidates are accepted one at a time, each re-checked against those already accepted.

Skipped on purpose:
* `module -- shake: keep-all` files and deprecated-module shims, whose job is to re-export;
* imports marked `shake: keep` (except the ones only needed for `example`s);
* imports that would hide, downstream, a module that registers options, aesop rule sets, simp attributes or `positivity` extensions;
* files using `backward.privateInPublic`;
* a handful of imports whose demotion broke `deriving` or `@[simps!]` in a full build.

Overall, the total number of modules loaded across all Mathlib files (the sum of the import closures) goes down by about 1%, and 7,194 of 8,726 modules load fewer files.

### Other changes

* `Mathlib/Combinatorics/SimpleGraph/Extremal/Turan.lean`: `grw [Nat.mod_le]` now closes its goal, so the `exact` after it is removed.

As with leanprover-community#43918, this is a semantic change for downstream projects: something that used to arrive through `B` may now need an explicit import.

Checked with `lake build Mathlib Archive Counterexamples` and `lake test`.

Benchmark (`!bench`, against `master`): `build//instructions` **−95.4G (−0.09%)**. The largest single change is `Mathlib.Condensed.Discrete.Colimit` (−31%); there are two small (about +1G, low-significance) per-module regressions. For comparison, leanprover-community#43918 measured −0.12%.

This is the first of a stack of three PRs, ordered from most to least conservative.

### Use of AI

This PR was prepared with Claude Code (Claude Opus). It wrote the analysis tooling (a small Lean program built on `lake shake`'s dependency computation, plus Python scripts that pick the imports and edit the headers), ran the builds that found and removed the bad candidates, and drafted this description. All edits to `Mathlib/` are produced mechanically by those scripts. The hand fix listed above is the only non-header change.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bors-staging This PR is currently being built by bors on the staging branch. new-contributor This PR was made by a contributor with at most 5 merged PRs. Welcome to the community! ready-to-merge This PR has been sent to bors.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants