Skip to content

[Merged by Bors] - chore: do not exempt Subtype or Fin from the numeric projections linter - #46196

Closed
thorimur wants to merge 126 commits into
leanprover-community:masterfrom
thorimur:num-proj-lint-subtype-fin
Closed

thorimur wants to merge 126 commits into
leanprover-community:masterfrom
thorimur:num-proj-lint-subtype-fin

Conversation

@thorimur

@thorimur thorimur commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

This PR removes Subtype and Fin from the exemptions list of the numeric projections linter, and replaces numeric projections on them with field name projections throughout mathlib.

This PR also changes the type of

-lemma coe_toInteger … : hζ.toInteger.1 = ζ := rfl
+lemma coe_toInteger … : hζ.toInteger.val = ζ := rfl

where val now means NumberField.RingOfIntegers.val, not Subtype.val. This is the "better" version as it doesn't need as much unfolding, and allows us to remove a now-unnecessary terminal simp below.

This PR includes an LLM-generated whitespace-only diff in 8f5818b to fix long lines, and manual fixes around congr and for long lines which benefitted from manual intervention (1743398).


Note: the nontrivial changes in this PR can also be reviewed by looking at the output of the following Lean code in a new file, which gets everything from the git diff which isn't either whitespace or an expected projection replacement:

Details
import Lean

open Lean

/-- Run `git diff --merge-base upstream/master --word-diff-regex={regex} --word-diff=porcelain`,
where by default `regex` is just `.`. -/
def gitDiff (regex := ".") : IO String := do
  IO.Process.run {
    cmd := "git"
    args := #["diff", "--merge-base", "upstream/master",
      s!"--word-diff-regex={regex}", "--word-diff=porcelain"]
  }

/-- A granular diff. By default there shouldn't be any overlap between `removed` and `added`. -/
structure AtomicDiff where
  removed : String := ""
  added : String := ""

def AtomicDiff.isEmpty (diff : AtomicDiff) := diff.removed.isEmpty && diff.added.isEmpty

def AtomicDiff.isWhitespace (diff : AtomicDiff) :=
  diff.removed.trimAscii.isEmpty && diff.added.trimAscii.isEmpty

/-- `true` iff the diff removes (only) digits, and adds (only) identifier-like characters
(`Lean.isIdRest` is the predicate for what can be in the rest of an identifier after `.`). -/
def AtomicDiff.numProjLike (diff : AtomicDiff) :=
  !diff.removed.trimAscii.isEmpty && !diff.added.trimAscii.isEmpty
    && diff.removed.trimAscii.all (·.isDigit)
    && diff.added.trimAscii.all isIdRest

def AtomicDiff.isNumProjReplacement (diff : AtomicDiff) (proj : Nat × String) :=
  diff.removed.trimAscii == toString proj.1 && diff.added.trimAscii == proj.2

def AtomicDiff.isAmong (diff : AtomicDiff) (pairs : Array (Nat × String)) :=
  pairs.any diff.isNumProjReplacement

instance : ToString AtomicDiff where
  toString d := s!"-{d.removed}\n+{d.added}"

/-- Given a list of lines output by `git diff --word-diff=porcelain` for a specific file with the
header stripped, get the clusters of contiguous lines for removals and additions, which start with
`-` or `+`. Ignore newlines completely, which are `~`. (Unfortunately this means newlines are
absent from the diff, but I'm not sure how to work around that.) -/
def atomicDiffs (lines : List String.Slice) : Array AtomicDiff := Id.run do
  let mut diffs := #[]
  let mut currentDiff : AtomicDiff := {}
  for line in lines do
    -- Ignore newlines (continue building up diff)
    if line == "~" then continue
    -- Build up diff
    else if let some r := line.dropPrefix? '-' then
      currentDiff := { currentDiff with removed := currentDiff.removed ++ r }
    else if let some a := line.dropPrefix? '+' then
      currentDiff := { currentDiff with added := currentDiff.added ++ a }
    else -- On first non-newline line that's not part of the diff, save the diff and reset it
      unless currentDiff.isEmpty do
        diffs := diffs.push currentDiff
        currentDiff := {}
  -- If somehow we have a diff going, save it
  unless currentDiff.isEmpty do
    diffs := diffs.push currentDiff
  return diffs

/-- Parse a full `git diff --word-diff=porcelain` output, and associate files to the diffs within them. -/
def extractDiffs (input : String) : Std.HashMap String (Array AtomicDiff) := Id.run do
  let mut diffs : Std.HashMap String (Array AtomicDiff) := {}
  -- The output for each file starts with `diff --git `
  for chunk in input.split "\ndiff --git " do
    -- Split into lines, then get the header (`diff git ... / --- a/<file> / +++ b/<file>`)
    -- by taking lines until the first `@@` from `@@ -<pos> +<pos> @@`.
    let (headerLines, bodyLines) := (chunk.split '\n').toList.span (!·.startsWith "@@")
    -- (Hypothetically, files without textual changes (e.g. binary files) have no `+++ ` line.)
    let some file := headerLines.findSome? (·.dropPrefix? "+++ b/") | continue
    diffs := diffs.alter file.copy fun
      | arr? => arr?.getD #[] ++ atomicDiffs bodyLines
  return diffs

/--
info: [(Mathlib/NumberTheory/NumberField/Cyclotomic/Ideal.lean,
  [-    simp [ζ]
   +]),
 (Mathlib/GroupTheory/GroupAction/SubMulAction/Combination.lean,
  [-
   +(,
   -
   +)]),
 (Mathlib/RingTheory/LocalRing/ResidueField/Fiber.lean,
  [-;
   +       ,
   -;
   +       ]),
 (Mathlib/RingTheory/Smooth/Kaehler.lean,
  [-;
   +     ]),
 (Mathlib/Data/ZMod/Basic.lean,
  [-refin
   +l,
   -
   +t,
   -
   +:=,
   -        ⟨(
   +,
   -)
   +        refine ⟨this,
   -           (Na
   +,
   -.c
   +,
   -ne
   +,
   -eRemainde
   +.p,
   - h (cast x : ZM
   +,
   -d m).val (cas
   +per,
   - x : ZMod n).val).2
   +y]),
 (Mathlib/FieldTheory/SeparablyGenerated.lean,
  [-
   +(,
   -
   +)]),
 (Mathlib/NumberTheory/Cyclotomic/Basic.lean,
  [-
   +(,
   -
   +)]),
 (Mathlib/AlgebraicGeometry/Morphisms/Flat.lean,
  [-2;
   +property     ]),
 (Mathlib/GroupTheory/Sylow.lean,
  [-fun
   +,
   -T
   +,
   -=>
   +,
   -
   +fun,
   -
   +T,
   -
   +=>]),
 (Mathlib/RepresentationTheory/FiniteIndex.lean,
  [-(
   +,
   -)
   +]),
 (Mathlib/Order/SuccPred/Basic.lean,
  [-
   +(,
   -
   +),
   -
   +(,
   -
   +)]),
 (Mathlib/Topology/Covering/Quotient.lean,
  [-;
   +     ]),
 (Mathlib/RingTheory/AlgebraicIndependent/AlgebraicClosure.lean,
  [-
   +(,
   -
   +)])]
-/
#guard_msgs in
run_cmd
  let diffs := extractDiffs <|← gitDiff
  let weirdDiffs := diffs.filterMap fun mod diffs => do
    guard !(mod ∈ ["Mathlib/Init.lean", "Mathlib/Tactic/Linter/NumericProjection.lean", "MathlibTest/Linter/NumericProjection.lean"])
    let weirdDiffs := diffs
      |>.filter (!·.isWhitespace) -- whitespace-only diffs are fine
      |>.filter (!·.isAmong #[(1, "val"), (2, "property"), (2, "isLt")])
    guard !weirdDiffs.isEmpty
    some weirdDiffs
  logInfo m!"{weirdDiffs.toArray}"

thorimur and others added 30 commits October 26, 2026 20:21
These commands made the changes in this commit.
To make the same fixes yourself, run them in your mathlib4 checkout.
…g asked an unrelated question. sure. that's fine. sigh.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Whitespace-only: wrap lines over 100 characters introduced by the
`scripts/runSkimmer.sh` rewrite.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@thorimur

Copy link
Copy Markdown
Contributor Author

bors unlink

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

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

$h.val is parsed as $(h.val), which causes the Eq.val errors in CI

@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 19, 2026
@mathlib-dependent-issues

Copy link
Copy Markdown

This PR/issue depends on:

@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 Nov 19, 2026
thorimur and others added 7 commits November 18, 2026 23:18
Reflows the lines pushed over 100 characters by replacing numeric
projections with field names. This commit only changes whitespace; check with
`git diff --word-diff=porcelain --word-diff-regex='[^[:space:]]+'`,
which should show no added or removed words.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bryangingechen

Copy link
Copy Markdown
Contributor

Here's the output of the prototype declaration diff:


Declarations diff (Lean)

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

Shape: statement changes. 1 definition body changed; 4 proofs changed.

  • +0 new declarations
  • −0 removed declarations
definition bodies changed (1)
  • instReprPNat
proofs changed (statement unchanged) (4)
  • Mathlib.NumberTheory.NumberField.Cyclotomic.Ideal (1)
    • IsCyclotomicExtension.Rat.inertiaDeg_eq_of_not_dvd
  • Mathlib.Order.Interval.Finset.Basic (3)
    • Finset.Icc_map_sectR
    • Finset.Ioo_map_sectL
    • Finset.uIcc_map_sectR
Not counted as changes (5)

statements differing only in instance paths (5)

  • IsPrimitiveRoot.coe_toInteger — pretty-prints identically (instance-path difference)
  • NumberField.house.exists_ne_zero_int_vec_house_le — pretty-prints identically (instance-path difference)
  • divisorsAntidiagonalFactors_eq — pretty-prints identically (instance-path difference)
  • sigmaAntidiagonalEquivProd_symm_apply_fst — pretty-prints identically (instance-path difference)
  • sigmaAntidiagonalEquivProd_symm_apply_snd — pretty-prints identically (instance-path difference)

@bryangingechen

Copy link
Copy Markdown
Contributor

Everything looks reasonable to me. Let's get this in before it picks up conflicts.

Thanks!
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
…linter (#46196)

This PR removes `Subtype` and `Fin` from the exemptions list of the numeric projections linter, and replaces numeric projections on them with field name projections throughout mathlib.

This PR also changes the type of
```
-lemma coe_toInteger … : hζ.toInteger.1 = ζ := rfl
+lemma coe_toInteger … : hζ.toInteger.val = ζ := rfl
```
where `val` now means `NumberField.RingOfIntegers.val`, not `Subtype.val`. This is the "better" version as it doesn't need as much unfolding, and allows us to remove a now-unnecessary terminal `simp` below.

This PR includes an LLM-generated whitespace-only diff in 8f5818b to fix long lines, and manual fixes around `congr` and for long lines which benefitted from manual intervention (1743398).

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
@mathlib-bors

mathlib-bors Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

@mathlib-bors mathlib-bors Bot changed the title chore: do not exempt Subtype or Fin from the numeric projections linter [Merged by Bors] - chore: do not exempt Subtype or Fin from the numeric projections linter Nov 19, 2026
@mathlib-bors mathlib-bors Bot closed this Nov 19, 2026
TJHeeringa pushed a commit to TJHeeringa/mathlib4 that referenced this pull request Dec 2, 2026
…linter (leanprover-community#46196)

This PR removes `Subtype` and `Fin` from the exemptions list of the numeric projections linter, and replaces numeric projections on them with field name projections throughout mathlib.

This PR also changes the type of
```
-lemma coe_toInteger … : hζ.toInteger.1 = ζ := rfl
+lemma coe_toInteger … : hζ.toInteger.val = ζ := rfl
```
where `val` now means `NumberField.RingOfIntegers.val`, not `Subtype.val`. This is the "better" version as it doesn't need as much unfolding, and allows us to remove a now-unnecessary terminal `simp` below.

This PR includes an LLM-generated whitespace-only diff in 8f5818b to fix long lines, and manual fixes around `congr` and for long lines which benefitted from manual intervention (1743398).

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants