Skip to content

feat: instance diamond linter - #38781

Open
JovanGerb wants to merge 55 commits into
leanprover-community:masterfrom
JovanGerb:Jovan-instance_diamonds
Open

JovanGerb wants to merge 55 commits into
leanprover-community:masterfrom
JovanGerb:Jovan-instance_diamonds

Conversation

@JovanGerb

@JovanGerb JovanGerb commented Apr 30, 2026 •

Copy link
Copy Markdown
Contributor

This PR implements a linter that detects instance diamonds where the different instances do not unify in implicit transparency.

The idea is, for each instance, to re-synthesize its data projections temporarily removing its instance attribute. If this yields a result, we check that it is definitionally equal to the instance itself. For example, given a Ring R instance, we check that inferred Mul R, Add R, Sub R, One R, Zero R, NSMul R and ZSMul R instances all agree with the one coming from the Ring R instances.

The current version only warns on the diamonds that are not diamonds at default transparency. This will be extended in a follow-up PR.


Open in Gitpod

@JovanGerb
JovanGerb marked this pull request as draft April 30, 2026 16:06
@mathlib-bors

mathlib-bors Bot commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

This pull request is now in draft mode. No active bors state needed cleanup.

While this PR remains draft, bors will ignore commands on this PR. Mark it ready for review before using commands like bors r+ or bors try.

@github-actions

github-actions Bot commented Apr 30, 2026 •

Copy link
Copy Markdown

PR summary a5371b573f

Import changes exceeding 2%

% File
+2.02% Mathlib.Init

Import changes for modified files

Dependency changes

File Base Count Head Count Change
Mathlib.Init 99 101 +2 (+2.02%)
Mathlib.Tactic.ToExpr 100 102 +2 (+2.00%)
Mathlib.Control.Functor 144 146 +2 (+1.39%)
Mathlib.Order.CompleteLattice.PiLex 517 519 +2 (+0.39%)
Mathlib.Data.Fintype.OfMap 535 537 +2 (+0.37%)
Mathlib.Algebra.Group.Submonoid.Saturation 548 550 +2 (+0.36%)
Mathlib.Data.Fintype.Basic 561 563 +2 (+0.36%)
Mathlib.Data.Set.Finite.Basic 575 577 +2 (+0.35%)
Mathlib.CategoryTheory.Monoidal.Mon 617 619 +2 (+0.32%)
Mathlib.Order.Interval.Finset.Defs 685 687 +2 (+0.29%)
Mathlib.RingTheory.NonUnitalSubsemiring.Basic 766 768 +2 (+0.26%)
Mathlib.Algebra.Category.BoolRing 803 805 +2 (+0.25%)
Mathlib.CategoryTheory.Category.Cat.CartesianClosed 819 821 +2 (+0.24%)
Mathlib.Algebra.Algebra.NonUnitalHom 885 887 +2 (+0.23%)
Mathlib.Algebra.DirectSum.Basic 954 956 +2 (+0.21%)
Mathlib.SetTheory.Ordinal.Notation 966 968 +2 (+0.21%)
Mathlib.CategoryTheory.Monoidal.Subcategory 992 994 +2 (+0.20%)
Mathlib.CategoryTheory.Action 1121 1123 +2 (+0.18%)
Mathlib.AlgebraicTopology.Quasicategory.StrictBicategory 1272 1274 +2 (+0.16%)
Mathlib.Analysis.Normed.Group.Subgroup 1292 1294 +2 (+0.15%)
Mathlib.FieldTheory.IntermediateField.Adjoin.Defs 1318 1320 +2 (+0.15%)
Mathlib.RingTheory.PowerSeries.Basic 1355 1357 +2 (+0.15%)
Mathlib.CategoryTheory.Sites.Point.Monoidal 1392 1394 +2 (+0.14%)
Mathlib.Analysis.Normed.Group.Submodule 1470 1472 +2 (+0.14%)
Mathlib.Analysis.Normed.Ring.Basic 1486 1488 +2 (+0.13%)
Mathlib.Algebra.Homology.HomotopyCategory.HomComplexCohomology 1621 1623 +2 (+0.12%)
Mathlib.AlgebraicGeometry.StructureSheaf 2330 2332 +2 (+0.09%)
Mathlib.AlgebraicGeometry.Pullbacks 2491 2493 +2 (+0.08%)
Mathlib.Tactic 3239 3241 +2 (+0.06%)
Import changes for all files
Files Import difference
../mathlib-ci/scripts/pr_summary/import_trans_difference.sh all
There are 8700 files with changed transitive imports taking up over 380413 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)

+ A
+ B
+ Group
+ GroupWrapper
+ MulBar
+ MulFoo
+ MulFooBar
+ MulWrapper
+ MyNat
+ compareWithSynthesized
+ exposedThree
+ findDiamonds
+ instAInt₁
+ instAInt₂
+ instANat₁
+ instANat₂
+ instBMyNat₁
+ instBMyNat₂
+ instance : MulBar MyNat
+ instance : MulFoo MyNat
+ instance : MulFooBar MyNat
+ instance {α} [Group α] : Group (GroupWrapper α) := inferInstanceAs <| Group α
+ instance {α} [Mul α] : Mul (GroupWrapper α) := inferInstanceAs <| Mul α
+ instance {α} [MulBar α] : MulBar (MulWrapper α) := inferInstanceAs <| MulBar α
+ instance {α} [MulFoo α] : MulFoo (MulWrapper α) := inferInstanceAs <| MulFoo α
+ instance {α} [MulFooBar α] : MulFooBar (MulWrapper α) := inferInstanceAs <| MulFooBar α
+ instance {α} [One α] : One (GroupWrapper α) := inferInstanceAs <| One α
+ instanceDiamonds
+ mul'
+ unexposedTwo

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

Computed after the build finishes.


No changes to strong technical debt.

Increase in weak tech debt: (relative, absolute) = (1.00, 0.00)
Current number Change Type (weak)
5159 1 exposed public sections

Current commit a5371b573f
Reference commit 02a839bc62

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 Apr 30, 2026
@JovanGerb
JovanGerb temporarily deployed to cache-upload-forks June 4, 2026 16:28 — with GitHub Actions Inactive
@JovanGerb

Copy link
Copy Markdown
Contributor Author

I'm not sure if I need to insert all of the nolints in order for benchmarking to work:

!radar

@leanprover-radar

leanprover-radar commented Jun 18, 2026 •

Copy link
Copy Markdown

Benchmark results for 4785755 against 4176265 are in. There are significant results. @JovanGerb

  • 🟥 main exited with code 1

No significant changes detected.

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

Copy link
Copy Markdown
Contributor Author

!radar

@leanprover-radar

leanprover-radar commented Jun 18, 2026 •

Copy link
Copy Markdown

Benchmark results for cf01e55 against 4176265 are in. No significant results found. @JovanGerb

  • 🟥 build//instructions: +17.3G (+0.01%)

Medium changes (1🟥)

  • 🟥 lint//instructions: +1.3T (+32.82%)

Small changes (2🟥)

  • 🟥 build/module/Mathlib.Analysis.Complex.ValueDistribution.LogCounting.Basic//instructions: +2.0G (+4.62%)
  • 🟥 build/module/Mathlib.Analysis.Meromorphic.Divisor//instructions: +1.1G (+3.98%)

@mathlib-merge-conflicts mathlib-merge-conflicts 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 Jun 24, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@mathlib-merge-conflicts

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@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 11, 2026
@mathlib-merge-conflicts mathlib-merge-conflicts 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 Nov 16, 2026
@mathlib-merge-conflicts

Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

@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 16, 2026
mathlib-bors Bot pushed a commit that referenced this pull request Nov 17, 2026
…6098)

This PR fixes an (implicit reducible) instance diamond. The fix is to make the instance use `MulOpposite.unop` instead of `MulOpposite.opHomeomorph.symm`

Found by the instance diamond linter (#38781)
mathlib-bors Bot pushed a commit that referenced this pull request Nov 17, 2026
…6098)

This PR fixes an (implicit reducible) instance diamond. The fix is to make the instance use `MulOpposite.unop` instead of `MulOpposite.opHomeomorph.symm`

Found by the instance diamond linter (#38781)
@JovanGerb

Copy link
Copy Markdown
Contributor Author

!bench

@leanprover-radar

leanprover-radar commented Nov 21, 2026 •

Copy link
Copy Markdown

Benchmark results for 0e1b6eb against 3eae40f are in. There are significant results. @JovanGerb

  • 🟥 build//instructions: +7.2G (+0.01%)

Large changes (1🟥)

  • 🟥 lint//instructions: +955.1G (+25.28%)

Small changes (26✅, 41🟥)

  • 67 hidden

rw [← Complex.norm_mul_self_eq_normSq, ← sq]
exact z.sq_norm_lt_one

instance : Pow 𝕔𝔻 ℕ where

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.

splice-bot

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.

Split PR created

Split off the changes to Mathlib/Analysis/Complex/UnitDisc/Basic.lean in #46423.

mathlib-bors Bot pushed a commit that referenced this pull request Nov 25, 2026
There were two natural number power instances which differed at implicit transparency.
The `NatPowAssoc` instance is not necessary after this change, hence can be removed.

Found by the linter in #38781.

Co-authored-by: Jovan Gerbscheid <[email protected]>
Co-authored-by: JovanGerb <[email protected]>
@JovanGerb
JovanGerb marked this pull request as ready for review November 26, 2026 16:59
@JovanGerb

Copy link
Copy Markdown
Contributor Author

!radar

@github-actions github-actions Bot added the large-import Automatically added label for PRs with a significant increase in transitive imports label Nov 28, 2026
@leanprover-radar

leanprover-radar commented Nov 28, 2026 •

Copy link
Copy Markdown

Benchmark results for d43b0bf against 02a839b are in. There are significant results. @JovanGerb

  • 🟥 build//instructions: +11.4G (+0.01%)

Large changes (1🟥)

  • 🟥 lint//instructions: +821.2G (+31.60%)

Small changes (17✅, 49🟥)

  • 66 hidden

This branch was previously deployed

1 inactive (outdated) deployment
cache-upload-forks — 52a00b0b Deployed Jun 4, 2026 by JovanGerb via ci (fork) / Upload to cache #302919
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

large-import Automatically added label for PRs with a significant increase in transitive imports t-linter Linter tech debt Fixes cross-cutting technical debt, see the "technical debt counters" stream on zulip

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants