Skip to content

[Merged by Bors] - feat: lint against numeric projections - #45341

Closed
thorimur wants to merge 109 commits into
leanprover-community:masterfrom
thorimur:num-proj-lint
Closed

thorimur wants to merge 109 commits into
leanprover-community:masterfrom
thorimur:num-proj-lint

Conversation

@thorimur

@thorimur thorimur commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

This PR lints against numeric structure projections such as foo.42, and interactively suggests replacing them with the appropriate field name projection instead. This was discussed on Zulip.

Structures may be exempted from the linter and permitted to use numeric projections by either attaching @[allow_numeric_proj] to the structure at the point it was defined, or by hardcoding them into the linter's list of exceptions (if they were defined upstream).

This PR allows terms whose types begin with

Prod, PProd, Sigma, PSigma, And, Iff, Subtype, Fin, Complex, Quaternion

to use numeric projections. This also allows type definitions wrapping these types to use numeric projections, as only the projection itself is looked at. (This may be changed in the future.)

However, note that Subtype and Fin will be non-exempted shortly.

We also update the Archive and Counterexamples.

Disclosure: although I don't use Claude to write important code, it so happened that Claude edited a key part of the file without being asked, after simply being asked to locate certain API(!). However, the resulting changes were sensible, so I've retained them. Claude also (this time on request!) assisted with testing the many situations in which numeric projections can appear.


Note: the linter itself + Archive/Counterexample changes may be reviewed via the bors stack link, and review comments may be added from there too. See also the bors stack comment.

@github-actions github-actions Bot added the large-import Automatically added label for PRs with a significant increase in transitive imports label Oct 27, 2026
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

PR summary 2031cb460c

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 8685 files with changed transitive imports taking up over 379676 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)

+ Bar
+ C
+ Fn
+ Foo
+ Ind
+ MyAnd
+ MyBar
+ MyFn
+ MySubtype
+ allowsNumericProj
+ disallowedNumericProjFn?
+ numericProjFn?
+ numericProjections
+ upstreamAllowed
+ which
+ withAux

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

  • +3 new declarations
  • −0 removed declarations
+Mathlib.Linter.allowNumericProjExt
+Mathlib.Linter.allowsNumericProj
+Mathlib.Linter.linter.style.numericProjections

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

Current commit 2031cb460c
Reference commit a97078fa10

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

@github-actions github-actions Bot added the t-linter Linter label Oct 27, 2026
These commands made the changes in this commit.
To make the same fixes yourself, run them in your mathlib4 checkout.

@JovanGerb JovanGerb 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.

Thanks, the approach of this PR looks goot to me

throwError "Expected `{.ofConstName n}` to be a structure.")

/-- We can't add to a tag attribute later on, so hardcode the upstream exempt declarations here. -/
def upstreamAllowed := #[``Prod, ``PProd, ``Sigma, ``PSigma, ``And, ``Iff, ``Subtype, ``Fin]

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.

This is something that requires a thorough Zulip discussion first, but I would like to propose that we restrict this list to

Suggested change
def upstreamAllowed := #[``Prod, ``PProd, ``Sigma, ``PSigma, ``And, ``Iff, ``Subtype, ``Fin]
def upstreamAllowed := #[``Prod, ``PProd, ``Sigma, ``PSigma]

This would of course require many fixes in Mathlib before it can be compliant.

For the ones that I removed in the suggestion above, it is usually easier to tell the type of the object from the source when the projection is named. One of the pain point with eg. Subtype is that many types are def-eq to some subtype, and in the source you will often encounter x.1 for some x in such a type that you might not know to be a subtype of the top of your head . It takes longer to human-parse x.1 than it takes to understand x.val, which makes it immediately clear that x is some subtype-like object. similarly, x.property makes it immediate that you are looking at some property while x.2 will take longer to parse and will not make clear that you are looking at a proof.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I never use numeric projections so I'd even support making it empty, but I think realistically such a linter won't get support without allowing the basics.

btw on the flip side, perhaps Complex and QuaternionAlgebra should be allowed since they're meant to be tuples.

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.

Of the ones you propose to remove, I'm not so sure about And, because I think of And as a tuple, and like to write .1/.2 for 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.

Perhaps the best way to navigate any such decision here will be to only lint against the really uncontroversial ones first, then gradually shrink the list :)

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.

Good point, but I still think Iff and And are quite uncontroversial.

@github-actions github-actions Bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Oct 27, 2026
@github-actions github-actions Bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Oct 27, 2026
@mathlib-bors

mathlib-bors Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

This pull request is no longer linked; it will merge on its own.

mathlib-bors Bot pushed a commit that referenced this pull request Nov 18, 2026
…mathlib (#45389)

This PR replaces numeric projections such as `.2` with the named version of that projection, with the exception of certain types. This was generated using `scripts/runSkimmer.sh` in the output of #45341, plus some manual fixes and an LLM whitespace-only fix for handling (hundreds of) long lines.

Co-authored-by: Jireh Loreaux <[email protected]>
@mathlib-dependent-issues mathlib-dependent-issues Bot removed the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Nov 18, 2026
@mathlib-dependent-issues

Copy link
Copy Markdown

Takes num-proj-lint-skimmer (leanprover-community#45389) from upstream/master, where it has landed.
@faenuccio

Copy link
Copy Markdown
Contributor

@thorimur @robin-carlier The question has been discussed in the maintainers stream and we came to the conclusion that this linter is certainly very useful and we'd like it to be merged. Concerning the exceptions, it was noted that for

``Prod, ``PProd, ``Sigma, ``PSigma, ``And, ``Iff

using numbers is pretty uncontroversial, since also for And and Iff there is a clear "first" and "second" component and. Most importantly, in these two last cases both terms belong to Prop types and so they're somewhat of the same kind. This is certainly not the case for terms in Subtype and Fin, hence we agree that the linter should still fire on those.

All in all, I'm delegating this with the request that the list of exceptions be

``Prod, ``PProd, ``Sigma, ``PSigma, ``And, ``Iff``

Thanks once more for all your work!

@faenuccio

Copy link
Copy Markdown
Contributor

bors d+

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

mathlib-bors Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

✌️ thorimur can now approve this pull request until 2026-12-02 10:24 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.

@thorimur

Copy link
Copy Markdown
Contributor Author

@faenuccio Great, thanks so much! :) I am wondering: is `Prod, ``PProd, ``Sigma, ``PSigma, ``And, ``Iff the full list? I.e., should I also remove the attribute from Complex and Quaternion?

Further I am wondering if logistically it would make sense to merge this PR now with the current list of exemptions (to prevent Mathlib from introducing any more numeric projections in the meantime), and then shrink the list immediately in a follow-up PR (which may acquire merge conflicts quickly), namely #46196 (to which I'll add Complex and Quaternion if desired). Does that sound reasonable?

@mathlib-bors

mathlib-bors Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

#46196 is now stacked on #45341: both are in a linked bundle and merge in the same batch, or not at all. The batch applies #45341's changes first. Each pull request still needs its own bors r+; bors unlink removes the link.

Review: #46196's own changes: the diff of #46196 without #45341's changes.

View this bundle in bors (sign in with GitHub to view).

@mathlib-bors

mathlib-bors Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

This pull request is no longer linked; it will merge on its own.

@thorimur

Copy link
Copy Markdown
Contributor Author

Oh, wow, just non-exempting Subtype and Fin (#46196) produces a ±6,000 line diff. For comparison, the adaptations so far for every other numeric projection totaled about ±3,500. This is seeming to acquire merge conflicts almost immediately. 😅

Would it be reasonable to do the following:

  • merge this PR now so that no other numeric projections creep in in the meantime
  • remove the Subtype exemption in another PR immediately after
  • likewise remove the Fin exemption in another, separate PR immediately after
  • similarly for Complex and Quaternion

@Parcly-Taxel

Parcly-Taxel commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Would it be reasonable to do the following:
merge this PR now so that no other numeric projections creep in in the meantime

Yes, this is perfectly reasonable.

@thorimur

Copy link
Copy Markdown
Contributor Author

Thanks for the last-minute review @Deicyde ! :)

@thorimur

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 Nov 19, 2026
mathlib-bors Bot pushed a commit that referenced this pull request Nov 19, 2026
This PR lints against numeric structure projections such as `foo.42`, and interactively suggests replacing them with the appropriate field name projection instead. This was discussed [on Zulip](https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Linting.20against.20numeric.20projections/with/635575542).

Structures may be exempted from the linter and permitted to use numeric projections by either attaching `@[allow_numeric_proj]` to the structure at the point it was defined, or by hardcoding them into the linter's list of exceptions (if they were defined upstream).

This PR allows terms whose types begin with
```
Prod, PProd, Sigma, PSigma, And, Subtype, Fin, Complex, Quaternion
```
to use numeric projections. This also allows type definitions wrapping these types to use numeric projections, as only the projection itself is looked at. (This may be changed in the future.)

However, note that `Subtype` and `Fin` will be non-exempted shortly.

We also update the Archive and Counterexamples.

Disclosure: although I don't use Claude to write important code, it so happened that Claude edited a key part of the file without being asked, after simply being asked to locate certain API(!). However, the resulting changes were sensible, so I've retained them. Claude also (this time on request!) assisted with testing the many situations in which numeric projections can appear.

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Co-authored-by: Jireh Loreaux <[email protected]>
@mathlib-bors mathlib-bors Bot added the bors-staging This PR is currently being built by bors on the staging branch. label Nov 19, 2026
@thorimur

Copy link
Copy Markdown
Contributor Author

I'll also note here that merging now and then addressing Subtype/Fin in a subsequent PR was maintainer-approved over on Zulip (same thread as in PR description).

@mathlib-bors

mathlib-bors Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

@mathlib-bors mathlib-bors Bot changed the title feat: lint against numeric projections [Merged by Bors] - feat: lint against numeric projections Nov 19, 2026
@mathlib-bors mathlib-bors Bot closed this Nov 19, 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 Nov 19, 2026
TJHeeringa pushed a commit to TJHeeringa/mathlib4 that referenced this pull request Dec 2, 2026
This PR lints against numeric structure projections such as `foo.42`, and interactively suggests replacing them with the appropriate field name projection instead. This was discussed [on Zulip](https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Linting.20against.20numeric.20projections/with/635575542).

Structures may be exempted from the linter and permitted to use numeric projections by either attaching `@[allow_numeric_proj]` to the structure at the point it was defined, or by hardcoding them into the linter's list of exceptions (if they were defined upstream).

This PR allows terms whose types begin with
```
Prod, PProd, Sigma, PSigma, And, Subtype, Fin, Complex, Quaternion
```
to use numeric projections. This also allows type definitions wrapping these types to use numeric projections, as only the projection itself is looked at. (This may be changed in the future.)

However, note that `Subtype` and `Fin` will be non-exempted shortly.

We also update the Archive and Counterexamples.

Disclosure: although I don't use Claude to write important code, it so happened that Claude edited a key part of the file without being asked, after simply being asked to locate certain API(!). However, the resulting changes were sensible, so I've retained them. Claude also (this time on request!) assisted with testing the many situations in which numeric projections can appear.

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Co-authored-by: Jireh Loreaux <[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. 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.

10 participants