Skip to content

[Merged by Bors] - perf(Linter): bundle instance linters into one linter - #44436

Closed
JovanGerb wants to merge 12 commits into
leanprover-community:masterfrom
JovanGerb:Jovan-instance_linters
Closed

JovanGerb wants to merge 12 commits into
leanprover-community:masterfrom
JovanGerb:Jovan-instance_linters

Conversation

@JovanGerb

@JovanGerb JovanGerb commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

This PR bundles the three instance linters we have into a single linter for performance. The new linter lives in Mathlib.Tactic.Linter.DeclType, because it can accommodate any linter that acts on declaration types.

Note: we could implement some kind of extensible framework for declaration type linters. But that could make the performance worse again. It would probably be more trouble than it's worth.

This is in preparation for adding another instance linter: the concrete instance linter. After this PR, it should be possible to implement this with minimal performance cost.


@JovanGerb

Copy link
Copy Markdown
Contributor Author

!bench

@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

PR summary 0579efd528

Import changes exceeding 2%

% File
+4.55% Mathlib.Tactic.Linter.UnusedInstancesInType

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference
../mathlib-ci/scripts/pr_summary/import_trans_difference.sh all
There are 8514 files with changed transitive imports taking up over 372064 characters: this is too many to display!
You can run this locally from your mathlib4 directory:

git clone https://github.com/leanprover-community/mathlib-ci.git ../mathlib-ci


Declarations diff (regex)

+ _root_.Lean.ConstantVal.logOnUnusedInstancesInTypeWhere
+ declTypeLinter
- _root_.Lean.Syntax.logUnusedInstancesInTheoremsWhere

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 0579efd).

  • +5 new declarations
  • −0 removed declarations
+Mathlib.Linter.OverlappingInstances.linter.overlappingInstances
+Mathlib.Linter.OverlappingInstances.overlappingInstances
+Mathlib.Linter.UnusedInstancesInType.unusedDecidableInType
+Mathlib.Linter.UnusedInstancesInType.unusedFintypeInType
+Mathlib.Linter.declTypeLinter

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

Current commit 0579efd528
Reference commit 87fc9444ea

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).

@leanprover-radar

leanprover-radar commented Oct 2, 2026 •

Copy link
Copy Markdown

Benchmark results for 30ddd61 against 87fc944 are in. There are significant results. @JovanGerb

  • ✅ build//instructions: -520.8G (-0.39%)

Large changes (1✅)

  • 1 hidden

Small changes (58✅)

  • ✅ build/module/Mathlib.Algebra.Algebra.Equiv//instructions: -326.6M (-0.89%)
  • ✅ build/module/Mathlib.Algebra.Algebra.Subalgebra.Basic//instructions: -336.1M (-0.82%)
  • ✅ build/module/Mathlib.Algebra.GradedFunLike//instructions: -11.6M (-0.38%)
  • ✅ build/module/Mathlib.Algebra.Group.FunLike//instructions: -38.0M (-0.39%)
  • ✅ build/module/Mathlib.Algebra.Group.IsApply//instructions: -35.7M (-0.61%) (reduced significance based on absolute threshold)
  • ✅ build/module/Mathlib.Algebra.Group.Subsemigroup.Lemmas//instructions: -6.6M (-0.22%)
  • ✅ build/module/Mathlib.Algebra.Lie.Basic//instructions: -344.9M (-0.78%)
  • ✅ build/module/Mathlib.Algebra.Notation.Bracket//instructions: -6.1M (-0.33%)
  • ✅ build/module/Mathlib.Algebra.Order.Hom.GroupNorm//instructions: -20.6M (-0.41%)
  • ✅ build/module/Mathlib.Algebra.Order.Hom.RingNorm//instructions: -9.4M (-0.27%)
  • ✅ build/module/Mathlib.Algebra.Order.Monoid.Multiset//instructions: -6.4M (-0.25%)
  • ✅ build/module/Mathlib.Algebra.Order.ToIntervalMod//instructions: -429.8M (-0.64%)
  • ✅ build/module/Mathlib.Algebra.Ring.FunLike//instructions: -11.1M (-0.26%)
  • ✅ build/module/Mathlib.Algebra.SkewMonoidAlgebra.Basic//instructions: -335.9M (-0.62%)
  • ✅ build/module/Mathlib.Algebra.Star.StarAlgHom//instructions: -365.4M (-0.85%)
  • ✅ build/module/Mathlib.Analysis.Complex.ValueDistribution.LogCounting.Truncated//instructions: -26.0M (-0.24%)
  • ✅ build/module/Mathlib.Analysis.Normed.Group.RadialFunction//instructions: -19.7M (-0.43%)
  • ✅ build/module/Mathlib.Analysis.Real.OfDigits//instructions: -426.1M (-3.13%)
  • ✅ build/module/Mathlib.Basic.FunLike.Basic//instructions: -20.3M (-0.66%)
  • ✅ build/module/Mathlib.Basic.FunLike.Embedding//instructions: -6.3M (-0.32%)
  • and 38 more

@JovanGerb JovanGerb changed the title perf(Linter/Instances): bundle instance linters into one linter perf(Linter): bundle instance linters into one linter Oct 2, 2026
@JovanGerb
JovanGerb requested a review from thorimur October 2, 2026 19:21
Comment thread Mathlib/Tactic/Linter/DeclType.lean Outdated
Comment on lines +10 to +11
import all Mathlib.Tactic.Linter.UnusedInstancesInType
import all Mathlib.Tactic.Linter.OverlappingInstances

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a reason we don't want to/can't simply make the relevant declarations public? Does it hurt performance or something? (If so we should document it :) )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can! Either that or using import all works. Would you prefer to make them public?

@thorimur thorimur Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

On the one hand, I don't want to have to deprecate them if we change the type signature, but on the other, I'd like to banish all import alls. XD

How about this: we make the necessary final definitions public, and we put them in the Mathlib.Linter.UnusedInstancesInType.Lean.ConstantVal namespace, since these are specifically the "constantval versions". (Then we can use dot notation thanks to DNRO.)

Similarly let's also take the _root_ off of declarations in that file and rely instead on DNRO, since iirc those declarations are touched as well :)

@github-actions github-actions Bot added the large-import Automatically added label for PRs with a significant increase in transitive imports label Oct 4, 2026
{importFintypeOfFiniteNote?.getD m!""}"

initialize addLinter unusedFintypeInType
def unusedFintypeInType (thm : ConstantVal) (bodyRef : Syntax) : CoreM Unit := do

@thorimur thorimur Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I pushed a commit that passes the bodyRef and does the same logging-on-type approximation we did in the overlapping instances linter. If we're changing the type signature, I feel we may as well :)

I also added two small tests for it.

Fun fact, this builds the source files if you downgrade the batteries import to non-public, but causes an interpreter error 😁

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah right, I guess the module system still has some rough edges around meta code.

Comment thread Mathlib/Lean/Elab/InfoTree.lean Resolved
Comment thread Mathlib/Tactic/Linter/DeclType.lean Resolved
-/
@[nolint unusedArguments] -- TODO: we plan to use `_cmd` in future
def _root_.Lean.Syntax.logUnusedInstancesInTheoremsWhere (_cmd : Syntax)
def logUnusedInstancesInTheorem (thm : ConstantVal)

@thorimur thorimur Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually, what about

Suggested change
def logUnusedInstancesInTheorem (thm : ConstantVal)
def Lean.ConstantVal.logUnusedInstancesInTypeWhere (thm : ConstantVal)

(and dot notation below)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I would say it is a bit redundant to set up dot notation here. It doesn't save us any characters at the use site. (And this function could just as naturally take a ConstantInfo or TheoremInfo)

@thorimur thorimur Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My thinking is mostly that it makes the Where more readable and also tells us what this is "acting on": thm.logUnusedInstancesInTypeWhere filter. Also aligns it with onUnusedInstancesWhere.

(More broadly, is the purpose of dot notation to save characters? I thought it was mostly for conceptual organization.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not a fan of using "where" in the name like this (I don't really know what it is supposed to mean).

I don't think the purpose of dot notation is to describe the type: you can just look at the type directly for that. I do think the purpose is to save characters. E.g. for Array.push, without dot notation it would need to be called e.g. arrayPush which is longer.

So I really do think this definition should just live in a UnusedInstancesInType (or similar) namespace.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"Where" means that it's subject to the provided condition, as in "log on all unused instances in the type where the instances are a variant of Decidable", for example. (Kind of like a more natural-language P in e.g. eraseP, I guess.)

Hmm, I disagree re: the purpose of dot notation. I think it's a readability and API exposure affordance inspired ultimately by the notion of methods in OOP, right? Meant primarily to connote that this is somehow the object whose data we are acting on (whether to modify it or extract something from it). I generally read x.foo a b as something like "act on x using foo according to a and b". (And also for discoverability, both so that a. lets you find Array.push via autocomplete even when the namespace is not open, though that's not really relevant in this case.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No, just to be clear, my reasoning for the public constant being in the namespace is totally disjoint from my dot notation preferences in general (and is not "for" dot notation at all).

For these particular public definitions I anticipate some churn in the future, and "putting them in the namespace of one of the arguments" seems like as good a means as any to allow us to deprecate it in the future if/when the type changes. (So that we don't change the type of a public facing decl, which would be a breaking change. I mean, in principle. Probably no one is going to call this in practice even if it's public. XD) But any other means of making the name more specific and therefore deprecatable when the type changed would do just as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Anyways, I wouldn't really count this definition as public, in the sense that it is "morally" private. That was my idea behind making it actually private in the first place.

(I was wondering more generally if maybe all meta definitions that are part of the implementation of a specific tactic/linter should be made private, because they are never meant to be used anywhere else. This is just annoying when the implementation itself spans multiple files, like here.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sure, I agree. I just seek to prevent hypothetical frustrating experiences from occurring! 😁 If you're very against it I won't force it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(Also, yes, I agree managing the API surface is good for meta code just as it is for other code! :) )

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I weakly support Thomas' suggestion to rename (I think his suggestion is a better name but also it is a nearly-private declaration so it is not worth dropping this PR over it).

@thorimur thorimur added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Oct 4, 2026
@thorimur thorimur self-assigned this Oct 4, 2026
@JovanGerb JovanGerb removed the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Oct 5, 2026
@thorimur

thorimur commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

I think this is nearly ready: can you please update the docstrings to match the new type signatures? Also, let's call that private function logOnUnusedInstancesInTypeWhere. We should keep the Where at least since everything else in the file still uses it, for now, and get rid of Theorem in the name since now it is not responsible for getting theorems. :)

maintainer delegate

@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown

🚀 Pull request has been placed on the maintainer queue by thorimur.

@mathlib-triage mathlib-triage Bot added the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Oct 5, 2026
@Vierkantor Vierkantor added the t-linter Linter label Oct 5, 2026
for (bodyRef, ctx, info) in t.getDeclBodyInfos do
if overlap then
overlappingInstances bodyRef ctx info
if let some declName := ctx.parentDecl? then

@Vierkantor Vierkantor Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we want to do a micro-optimization by checking for decidable || fintype before the later checks? (I suppose in Mathlib this isn't going to make an impact either way since both linters are going to be enabled throughout.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think that findTheoremConstVal? and Command.liftCoreM are sufficiently slow that we need to do this optimization. (And to be honest I can't really imagine a scenario where you would want only one of these two linters)

@Vierkantor Vierkantor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM otherwise!

bors d+

-/
@[nolint unusedArguments] -- TODO: we plan to use `_cmd` in future
def _root_.Lean.Syntax.logUnusedInstancesInTheoremsWhere (_cmd : Syntax)
def logUnusedInstancesInTheorem (thm : ConstantVal)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I weakly support Thomas' suggestion to rename (I think his suggestion is a better name but also it is a nearly-private declaration so it is not worth dropping this PR over it).

@mathlib-bors mathlib-bors Bot added the delegated This pull request has been delegated to the PR author (or occasionally another non-maintainer). label Oct 5, 2026
@mathlib-bors

mathlib-bors Bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

✌️ JovanGerb can now approve this pull request until 2026-10-19 11:41 UTC (in 2 weeks). To approve and merge, reply with bors r+. More detailed instructions are available here.

⚠️ This delegation only covers changes within Archive/**, Counterexamples/**, docs/**, DownstreamTest/**, Mathlib/**, MathlibTest/**, Wanted/**, widget/**, Archive.lean, Counterexamples.lean, docs.lean, Mathlib.lean, Wanted.lean; an author commit touching anything else will revoke it. Bors also revokes it if a later push changes too many files for it to check the full list — even if it stays within scope.

@mathlib-triage mathlib-triage Bot removed the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Oct 5, 2026
@JovanGerb

Copy link
Copy Markdown
Contributor Author

@thorimur, which docstrings did you want me to update?

@JovanGerb
JovanGerb force-pushed the Jovan-instance_linters branch from b36bdda to 148ab14 Compare October 5, 2026 12:31
@thorimur

thorimur commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

logUnusedEtc. refers to the type of the log argument which now takes in fewer arguments than before; I wasn't sure if there were other cases :)

@JovanGerb

Copy link
Copy Markdown
Contributor Author

bors r+

@mathlib-bors mathlib-bors Bot added the ready-to-merge This PR has been sent to bors. label Oct 6, 2026
mathlib-bors Bot pushed a commit that referenced this pull request Oct 6, 2026
This PR bundles the three instance linters we have into a single linter for performance. The new linter lives in `Mathlib.Tactic.Linter.DeclType`, because it can accommodate any linter that acts on declaration types.

Note: we could implement some kind of extensible framework for declaration type linters. But that could make the performance worse again. It would probably be more trouble than it's worth.

This is in preparation for adding another instance linter: the concrete instance linter. After this PR, it should be possible to implement this with minimal performance cost.

Co-authored-by: thorimur <[email protected]>
@mathlib-bors mathlib-bors Bot added the bors-staging This PR is currently being built by bors on the staging branch. label Oct 6, 2026
@mathlib-bors

mathlib-bors Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

@mathlib-bors mathlib-bors Bot changed the title perf(Linter): bundle instance linters into one linter [Merged by Bors] - perf(Linter): bundle instance linters into one linter Oct 6, 2026
@mathlib-bors mathlib-bors Bot closed this Oct 6, 2026
@mathlib-bors mathlib-bors Bot removed the delegated This pull request has been delegated to the PR author (or occasionally another non-maintainer). label Oct 6, 2026
kim-em added a commit that referenced this pull request Oct 12, 2026
Most conflicts come from master's regrouping of public and private imports
(#44512, #44513): resolved by taking master's import layout and reapplying
this PR's meta/removal changes line by line.

- Mathlib/Init.lean: keep master's new `Batteries.Tactic.Alias` import and
  `Linter.DeclType` (which bundles the instance linters, #44436), the latter
  as a `public meta import`.
- Mathlib/Data/Ineq.lean: drop the duplicate `ToFormat` instance as master
  did (#44383); the parsing functions stay moved to `Mathlib.Tactic.Ineq`.
- Control/Monad/{Basic,Cont}, Lean/Expr/Basic, FieldSimp,
  Linarith/Lemmas, LinearCombination/Lemmas: re-add this PR's new imports
  (`Batteries.Data.Except`, `Lean.ProjFns`, `Lean.Structure`,
  `Mathlib.Tactic.Ineq`) in master's public import group.
- Mathlib/Tactic/Push.lean: drop the `public import Lean.Elab.ConfigEval`
  that git's auto-merge reintroduced.
- Mathlib/Tactic/Echelon/Core.lean: master added `public import Qq` to this
  `public meta section` file; make it `public meta import Qq`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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. large-import Automatically added label for PRs with a significant increase in transitive imports ready-to-merge This PR has been sent to bors. t-linter Linter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants