Skip to content

chore: import cleanup (lake shake, redundant_imports, remove import Mathlib) - #1701

Merged
zhikaip merged 5 commits into
masterfrom
shake_imports
Sep 30, 2026
Merged

zhikaip merged 5 commits into
masterfrom
shake_imports

Conversation

@zhikaip

@zhikaip zhikaip commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Cleans up imports across Physlib, QuantumInfo and PhyslibAlpha. No declarations are added, removed or changed.

Compared with master, no module's import closure grows. Total closure size drops by 47% in QuantumInfo, 5% in PhyslibAlpha and 2.9% in Physlib. The project now depends on 4,279 of Mathlib's 8,529 modules instead of all of them, and a full build is 5,522 jobs instead of 9,890.

Commits

  1. chore: minimize imports with lake shake: lake shake --add-public --keep-implied --keep-prefix --fix on Physlib and QuantumInfo, with corrections:
    • Removes the imports shake adds only to keep positivity/norm_num/Aesop extensions in scope (e.g. Matroid.Init, EReal.Operations), which nothing uses. They are replaced by what #min_imports reports.
    • Keeps imports shake can't see being used: example/#synth uses, native_decide meta imports (SU5 ChargeSpectrum/ZMod), and a deriving Fintype (SU5 Potential).
  2. chore: remove transitively implied imports reported by redundant_imports: 88 imports in 63 files. The 17 remaining reports are false positives: 11 imports are needed under the module system (the checker ignores import visibility), and 6 are import all.
  3. chore: replace import Mathlib by the imports actually used: in QuantumInfo/ForMathlib/{Filter,Majorization,ULift}, PhyslibAlpha/ProbabilisticTheory/HilbertSpace/{State/Vector,Trace} and PhyslibAlpha/QuantumMechanics/StinespringDilation. Downstream files get the imports they had been receiving through Mathlib. Shake can't fix these files: with --keep-prefix it always keeps import Mathlib.
  4. chore(QuantumInfo): remove imports unused after dropping import Mathlib: 5 imports.
  5. chore: drop added imports that the files build without: each import added by the commits above was removed in turn and kept out when the file still builds; 48 go. Most were modules of a lemma or extension that simp/positivity happened to use, which shake treats as required. PauliMatrices/SelfAdjoint now imports Mathlib.Tactic.Positivity itself.

Non-import changes (dead code that kept imports alive)

  • Relativity/Tensors/RealTensor/Vector/Pre/Basic, …/Matrix/Pre, Relativity/Fermions/Weyl/Contraction: remove open CategoryTheory.MonoidalCategory, which nothing in these files uses.
  • Units/Dimension: remove unused open NNReal.
  • Relativity/Tensors/Reindexing: shrink the unused variable block to {C : Type}.

Reviewer map

  1. Commit 1, starting with the non-import changes above, then skim the import-only diffs.
  2. Commit 3: the six former import Mathlib files, then the downstream additions.
  3. Commits 2, 4 and 5: essentially deletions only.

Testing

  • lake build passes for Physlib, QuantumInfo and PhyslibAlpha.
  • lake exe lint_all passes; the Physlib Lean linter passes.
  • runPhyslibAlphaLinters, noAlphaImports, alphaFileImports and the style linters pass.

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for this pull-request (PR). If this is your first PR, welcome to the community!

Below is what will happen next. Please read carefully if you are not familiar with the process. You may open other PRs while this one is being reviewed, and can stack PRs on top of each other, so don't let these steps slow you down.

  1. Some automated checks will be run on your PR. You can see the results of these checks at the buttom of your PR page. If any of these checks fail, you will need to fix the issues before your PR can be merged. You can learn more about these here, including how to run them locally, which is sometimes quicker than relying on the GitHub Actions. If you have never had a PR merged before, you may have to wait for a reviewer to manually start these checks (this is for security).

  2. A reviewer will look at your PR and may ask you to make changes. This may happen a couple of days after you submit your PR, so you may need to be patient. But it should not be longer than that - if it is please bring it to the attention of the community on the Zulip. The level of review will depend on where your PR is submitted. If it is submitted to ./Physlib or ./QuantumInfo, the review will be more thorough than if it is submitted to ./PhyslibAlpha. You can find out more about what the review process is looking for in our review guidelines. If a reviewer adds an awaiting-author label to your PR, address the review comments, then please remove that label by adding a comment with -awaiting-author. This helps us keep track of reviews.

  3. The reviewer will either approve your PR, or request more changes (in which case we return to step 2). Once your PR is approved, it will be merged by a maintainer, this should happen shortly after approval, though you may get more comments at this stage.

Tip: The easiest way to get have a fast review is to submit a PR that is small and self-contained, and has clear documentation explaining why things are the way they are in your chages.

If you have any problems or questions, please reach out to the community on the Zulip.

@zhikaip
zhikaip marked this pull request as draft September 30, 2026 07:16
@zhikaip
zhikaip force-pushed the shake_imports branch 2 times, most recently from 15321cb to 0b3dc77 Compare September 30, 2026 09:10
Apply `lake shake --add-public --keep-implied --keep-prefix --fix` to Physlib
and QuantumInfo, with the following corrections:

- Drop the imports shake adds only to keep `positivity`/`norm_num`/Aesop
  extensions visible ("extra rev use"); replace them by the imports
  `#min_imports` reports, plus the ones downstream files actually use.
- Keep imports shake cannot see being used: `example`s and `#synth` checks
  (Units/Examples, Units/ParametricDimensionExamples,
  HarmonicOscillator/OneDimension/Examples, HermitianMat/Inner),
  `native_decide` meta imports (SU5 ChargeSpectrum/ZMod), and Capacity.
- Import `Mathlib.Tactic.DeriveFintype` in SU5/Potential, which derives
  `Fintype`.
- Remove `open`s of namespaces nothing in the file uses and an unused
  `variable` block in Tensors/Reindexing, which kept otherwise unneeded
  imports alive.

Co-authored-by: Claude Opus 5.5 <no-reply+claude-opus-5-5@anthropic.com>
zhikaip and others added 4 commits September 30, 2026 10:48
…orts`

Remove the imports that `lake exe redundant_imports` reports as implied by
another import of the same file. Kept the ones that are not actually
re-exported under the module system (the checker ignores import
visibility) and `import all` imports, which also expose private
declarations.

Co-authored-by: Claude Opus 5.5 <no-reply+claude-opus-5-5@anthropic.com>
`QuantumInfo/ForMathlib/{Filter,Majorization,ULift}` and
`PhyslibAlpha/ProbabilisticTheory/HilbertSpace/{State/Vector,Trace}` and
`PhyslibAlpha/QuantumMechanics/StinespringDilation` imported all of Mathlib,
which put the whole library in the import closure of every file depending
on them. Replace each by the imports `#min_imports` reports, and add to the
downstream files the imports they had been getting through `Mathlib`.

`lake shake` cannot fix these: every module registering a `positivity`,
`norm_num` or Aesop extension counts as used by a file that could see it,
and `--keep-prefix` then rolls these up into `Mathlib` again.

Co-authored-by: Claude Opus 5.5 <no-reply+claude-opus-5-5@anthropic.com>
…lib`

A second `lake shake` pass, now that `import Mathlib` no longer hides them,
finds five unused imports in `Channels/MatrixMap`, `Channels/Dual`,
`ForMathlib/Unitary`, `ResourceTheory/FreeState` and
`ResourceTheory/HypothesisTesting`.

Co-authored-by: Claude Opus 5.5 <no-reply+claude-opus-5-5@anthropic.com>
Tried removing each import this branch adds, one at a time, and kept the
removal when the file still builds: 48 were unnecessary. Most came from
`lake shake` requiring the module of a lemma or extension that `simp` or
`positivity` happened to use (e.g. `Real.ringHom_apply` from
`Mathlib.Algebra.Order.Archimedean.Real.Hom` in `Dynamics/Hamiltonian`
and `Vacuum/IsPlaneWave`), or from `#min_imports` suggesting an
instance-providing module the file can also do without.
`PauliMatrices/SelfAdjoint` now imports `Mathlib.Tactic.Positivity`
itself, which it had been getting through `PauliMatrices/Basic`.

Co-authored-by: Claude Opus 5.5 <no-reply+claude-opus-5-5@anthropic.com>
@zhikaip
zhikaip marked this pull request as ready for review September 30, 2026 11:23

@jstoobysmith jstoobysmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. Many thanks feel free to merge when ready.

@jstoobysmith jstoobysmith added the ready-to-merge This PR is approved and will be merged shortly label Sep 30, 2026
@zhikaip
zhikaip merged commit af484f7 into master Sep 30, 2026
9 checks passed
@zhikaip
zhikaip deleted the shake_imports branch September 30, 2026 16:10
@nateabr

nateabr commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

I think this PR has broken CI for open PRs. The checks were failing on #1702 and I did some searching with claude and it seems removing import Mathlib.Data.Nat.SuccPred from
/Relativity/Tensors/Contraction/SuccSuccAbove.lean leaves two hanging simp arguments (add_lt_add_iff_left,
add_le_add_iff_left) at line 284, have opened #1703 as a fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

medium ready-to-merge This PR is approved and will be merged shortly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants