Skip to content

[Merged by Bors] - refactor: Quaternion as abbrev - #43078

Closed
Parcly-Taxel wants to merge 14 commits into
leanprover-community:masterfrom
Parcly-Taxel:quaternion-helper
Closed

Parcly-Taxel wants to merge 14 commits into
leanprover-community:masterfrom
Parcly-Taxel:quaternion-helper

Conversation

@Parcly-Taxel

@Parcly-Taxel Parcly-Taxel commented Aug 24, 2026 •

Copy link
Copy Markdown
Collaborator

@github-actions

github-actions Bot commented Aug 24, 2026 •

Copy link
Copy Markdown

PR summary 7157d57284

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff (regex)

+ instance : Add ℍ[R,c₁,c₂,c₃] where add a b := ⟨a.1 + b.1, a.2 + b.2, a.3 + b.3, a.4 + b.4⟩
+ instance : Neg ℍ[R,c₁,c₂,c₃] where neg a := ⟨-a.1, -a.2, -a.3, -a.4⟩
+ instance : Sub ℍ[R,c₁,c₂,c₃] where sub a b := ⟨a.1 - b.1, a.2 - b.2, a.3 - b.3, a.4 - b.4⟩
- algebra
- instNontrivial
- instRing
- instance : Add ℍ[R,c₁,c₂,c₃]
- instance : Inhabited ℍ[R] := inferInstanceAs <| Inhabited ℍ[R,-1,0,-1]
- instance : IsStarNormal a := inferInstanceAs <| IsStarNormal (R := ℍ[R,-1,0,-1]) a
- instance : Module.Finite R ℍ[R] := inferInstanceAs <| Module.Finite R ℍ[R,-1,0,-1]
- instance : Module.Free R ℍ[R] := inferInstanceAs <| Module.Free R ℍ[R,-1,0,-1]
- instance : Neg ℍ[R,c₁,c₂,c₃] := ⟨fun a => ⟨-a.1, -a.2, -a.3, -a.4⟩⟩
- instance : Star ℍ[R] := inferInstanceAs <| Star ℍ[R,-1,0,-1]
- instance : StarRing ℍ[R] := inferInstanceAs <| StarRing ℍ[R,-1,0,-1]
- instance : Sub ℍ[R,c₁,c₂,c₃]
- instance [Monoid S] [MulAction S R] : MulAction S ℍ[R]
- instance [SMul S R] : SMul S ℍ[R] := inferInstanceAs <| SMul S ℍ[R,-1,0,-1]
- instance [SMul S R] [SMul T R] [SMulCommClass S T R] : SMulCommClass S T ℍ[R]
- instance [SMul S T] [SMul S R] [SMul T R] [IsScalarTower S T R] : IsScalarTower S T ℍ[R]
- instance [Semiring S] [DistribMulAction S R] : DistribMulAction S ℍ[R]
- instance [Semiring S] [Module S R] : Module S ℍ[R]
- instance {R : Type*} [Zero R] [One R] [Neg R] [Nontrivial R] : Nontrivial ℍ[R]
- instance {R : Type*} [Zero R] [One R] [Neg R] [Subsingleton R] : Subsingleton ℍ[R]

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

  • +0 new declarations
  • −17 removed declarations
-Quaternion.algebra
-Quaternion.instDistribMulAction
-Quaternion.instFinite
-Quaternion.instFree
-Quaternion.instInhabited
-Quaternion.instIsScalarTower
-Quaternion.instIsStarNormal
-Quaternion.instModule
-Quaternion.instMulAction
-Quaternion.instNontrivial
-Quaternion.instRing
-Quaternion.instSMul
-Quaternion.instSMulCommClass
-Quaternion.instStar
-Quaternion.instStarRing
-instNontrivialQuaternion
-instSubsingletonQuaternion

Decrease in strong tech debt: (relative, absolute) = (2.34, 0.00)
Current number Change Type (strong)
backward.isDefEq.respectTransparency 4814 -3
backward.isDefEq.respectTransparency.types 2481 -2
No changes to weak technical debt.

Current commit 7157d57284
Reference commit 738f2ab17a

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-algebra Algebra (groups, rings, fields, etc) label Aug 24, 2026
@Whysoserioushah

Copy link
Copy Markdown
Collaborator

!radar

@leanprover-radar

leanprover-radar commented Aug 24, 2026 •

Copy link
Copy Markdown

Benchmark results for c3975c8 against cf65d43 are in. No significant results found. @Whysoserioushah

  • 🟥 build//instructions: +24.6G (+0.02%)

Medium changes (4🟥)

  • 🟥 build/module/Mathlib.Algebra.DualQuaternion//instructions: +4.0G (+24.39%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Mathlib.Algebra.Quaternion//instructions: +10.1G (+10.24%)
  • 🟥 build/module/Mathlib.Analysis.Normed.Algebra.QuaternionExponential//instructions: +3.6G (+27.30%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Mathlib.Analysis.Quaternion//instructions: +4.4G (+27.98%) (reduced significance based on absolute threshold)

Small changes (1✅)

  • ✅ build/module/Mathlib.CategoryTheory.Sites.SheafCohomology.Basic//instructions: -1.8G (-0.74%)

@wwylele

wwylele commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

I think Quaternion should be abbrev. Most instances defined below in this file are defined using inferInstanceAs, showing that we usually want Quaternion to inherit instances from QuaternionAlgebra

@Whysoserioushah

Copy link
Copy Markdown
Collaborator

Yeah maybe change it to abbrev is a better idea

@Parcly-Taxel Parcly-Taxel changed the title chore: instance-reducible Quaternion chore: Quaternion as abbrev Aug 24, 2026
@Parcly-Taxel

Copy link
Copy Markdown
Collaborator Author

Yeah maybe change it to abbrev is a better idea

Done.

@Parcly-Taxel

Copy link
Copy Markdown
Collaborator Author

!radar

@leanprover-radar

leanprover-radar commented Aug 24, 2026 •

Copy link
Copy Markdown

Benchmark results for da5a560 against cf65d43 are in. There are significant results. @Parcly-Taxel

  • 🟥 main exited with code 1

No significant changes detected.

@Whysoserioushah

Copy link
Copy Markdown
Collaborator

maybe the instances could be removed as well?

@github-actions github-actions Bot added the tech debt Fixes cross-cutting technical debt, see the "technical debt counters" stream on zulip label Aug 24, 2026
@Parcly-Taxel

Copy link
Copy Markdown
Collaborator Author

!radar

@leanprover-radar

leanprover-radar commented Aug 24, 2026 •

Copy link
Copy Markdown

Benchmark results for 600cbcf against cf65d43 are in. No significant results found. @Parcly-Taxel

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

Medium changes (2🟥)

  • 🟥 build/module/Mathlib.Algebra.DualQuaternion//instructions: +3.6G (+21.93%) (reduced significance based on absolute threshold)
  • 🟥 build/module/Mathlib.Analysis.Quaternion//instructions: +1.2G (+7.80%)

Small changes (1✅, 1🟥)

  • 🟥 build/module/Mathlib.Analysis.Normed.Algebra.QuaternionExponential//instructions: +837.4M (+6.37%) (reduced significance based on absolute threshold)
  • ✅ build/module/Mathlib.CategoryTheory.Sites.SheafCohomology.Basic//instructions: -1.6G (-0.66%)

@Parcly-Taxel

Copy link
Copy Markdown
Collaborator Author

!radar

@leanprover-radar

leanprover-radar commented Aug 24, 2026 •

Copy link
Copy Markdown

Benchmark results for 15721d2 against cf65d43 are in. There are significant results. @Parcly-Taxel @Whysoserioushah

  • 🟥 build//instructions: +61.6G (+0.04%)

Large changes (1🟥)

  • 🟥 build/module/Mathlib.Algebra.DualQuaternion//instructions: +6.6G (+39.94%)

Medium changes (3🟥)

  • 🟥 build/module/Mathlib.Algebra.Quaternion//instructions: +8.6G (+8.74%)
  • 🟥 build/module/Mathlib.Analysis.Normed.Algebra.QuaternionExponential//instructions: +1.5G (+11.46%)
  • 🟥 build/module/Mathlib.Analysis.Quaternion//instructions: +2.2G (+14.32%)

Small changes (1✅)

  • ✅ build/module/Mathlib.CategoryTheory.Sites.SheafCohomology.Basic//instructions: -1.0G (-0.42%)

@Parcly-Taxel

Copy link
Copy Markdown
Collaborator Author

!radar

@leanprover-radar

leanprover-radar commented Aug 24, 2026 •

Copy link
Copy Markdown

Benchmark results for c642bb4 against be7cb66 are in. There are significant results. @Parcly-Taxel

  • 🟥 build//instructions: +1.8G (+0.00%)

Large changes (1🟥)

  • 🟥 build/module/Mathlib.Algebra.DualQuaternion//instructions: +6.5G (+39.05%)

Medium changes (1🟥)

  • 🟥 build/module/Mathlib.Analysis.Quaternion//instructions: +1.2G (+7.98%)

Small changes (1🟥)

  • 🟥 build/module/Mathlib.Analysis.Normed.Algebra.QuaternionExponential//instructions: +825.1M (+6.28%) (reduced significance based on absolute threshold)

@Parcly-Taxel

Copy link
Copy Markdown
Collaborator Author

Benchmark results for c642bb4 against be7cb66 are in. There are significant results. @Parcly-Taxel

* 🟥 `build//instructions`: **+1.8G** (**+0.00%**)

Large changes (1🟥)

* 🟥 `build/module/Mathlib.Algebra.DualQuaternion//instructions`: **+6.5G** (**+39.05%**)

Medium changes (1🟥)

* 🟥 `build/module/Mathlib.Analysis.Quaternion//instructions`: **+1.2G** (**+7.98%**)

Small changes (1🟥)

* 🟥 `build/module/Mathlib.Analysis.Normed.Algebra.QuaternionExponential//instructions`: **+825.1M** (**+6.28%**) (reduced significance based on absolute threshold)

I got rid of tech debt, so the significance is overrated.

@plp127

plp127 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

What if we keep all the instances for Quaternion and just prove them by inferInstance? Does that take away some of the slowdown?

@Parcly-Taxel

Copy link
Copy Markdown
Collaborator Author

!radar

@leanprover-radar

leanprover-radar commented Aug 24, 2026 •

Copy link
Copy Markdown

Benchmark results for d5fff03 against f1c2273 are in. There are significant results. @Parcly-Taxel

  • 🟥 build//instructions: +1.0G (+0.00%)

Large changes (1🟥)

  • 🟥 build/module/Mathlib.Algebra.DualQuaternion//instructions: +8.3G (+50.75%)

Medium changes (2🟥)

  • 🟥 build/module/Mathlib.Analysis.Normed.Algebra.QuaternionExponential//instructions: +1.2G (+9.26%)
  • 🟥 build/module/Mathlib.Analysis.Quaternion//instructions: +1.8G (+11.44%)

Small changes (1🟥)

  • 🟥 build/module/Mathlib.Algebra.Quaternion//instructions: +2.3G (+2.39%)

@Parcly-Taxel Parcly-Taxel changed the title chore: Quaternion as abbrev refactor: Quaternion as abbrev Aug 24, 2026
@Parcly-Taxel

Copy link
Copy Markdown
Collaborator Author

@plp127 ⬆️

Comment thread Mathlib/Algebra/Quaternion.lean Resolved
@joelriou joelriou added the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Aug 30, 2026
@Parcly-Taxel Parcly-Taxel removed the awaiting-author Reply -awaiting-author to remove the label on your PR once you have addressed all comments. label Aug 30, 2026
@Parcly-Taxel

Copy link
Copy Markdown
Collaborator Author

!radar

@leanprover-radar

leanprover-radar commented Aug 30, 2026 •

Copy link
Copy Markdown

Benchmark results for a9dc117 against b87b4e8 are in. There are significant results. @Parcly-Taxel

  • 🟥 build//instructions: +1.3G (+0.00%)

Large changes (1🟥)

  • 🟥 build/module/Mathlib.Algebra.DualQuaternion//instructions: +5.8G (+35.29%)

Small changes (2🟥)

  • 🟥 build/module/Mathlib.Analysis.Normed.Algebra.QuaternionExponential//instructions: +792.7M (+6.08%)
  • 🟥 build/module/Mathlib.Analysis.Quaternion//instructions: +1.1G (+6.76%)

@jcommelin

Copy link
Copy Markdown
Member

I can see that simp high works. But what is the underlying reasoning? Should this be applied elsewhere? Or did we stumble on a local optimum?

@Parcly-Taxel

Copy link
Copy Markdown
Collaborator Author

!radar

@leanprover-radar

leanprover-radar commented Aug 31, 2026 •

Copy link
Copy Markdown

Benchmark results for 85eeaed against 852d20a are in. There are significant results. @Parcly-Taxel

  • 🟥 main exited with code 1

No significant changes detected.

@Parcly-Taxel

Copy link
Copy Markdown
Collaborator Author

!radar

@leanprover-radar

leanprover-radar commented Aug 31, 2026 •

Copy link
Copy Markdown

Benchmark results for 7157d57 against 738f2ab are in. No significant results found. @Parcly-Taxel

  • ✅ build//instructions: -3.4G (-0.00%)

Small changes (1✅, 3🟥)

  • 🟥 build/module/Mathlib.Algebra.DualQuaternion//instructions: +792.9M (+4.85%)
  • 🟥 build/module/Mathlib.Analysis.Normed.Algebra.QuaternionExponential//instructions: +795.8M (+6.10%)
  • 🟥 build/module/Mathlib.Analysis.Quaternion//instructions: +983.4M (+6.31%)
  • ✅ build/module/Mathlib.Tactic.Linter.TacticDocumentation//instructions: -27.4M (-1.45%)

@Parcly-Taxel

Copy link
Copy Markdown
Collaborator Author

I can see that simp high works. But what is the underlying reasoning? Should this be applied elsewhere? Or did we stumble on a local optimum?

simp high lemmas will be applied first; given that the usage of QuaternionAlgebra in dependent files is pretty much exclusively the Quaternion version, there is a speedup.

@jcommelin

Copy link
Copy Markdown
Member

Thanks 🎉

bors merge

@mathlib-bors mathlib-bors Bot added the ready-to-merge This PR has been sent to bors. label Sep 1, 2026
mathlib-bors Bot pushed a commit that referenced this pull request Sep 1, 2026
* Mark `Quaternion` as an `abbrev`. Cf. [#mathlib4 > Transparency problem with quaternions @ 💬](https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Transparency.20problem.20with.20quaternions/near/618254305) and [this comment](#43078 (comment)).
* The instances for `Quaternion` that used to be proved by `inferInstanceAs` are removed.
* Remove five `set_option`s as a consequence.
* Move `normSq_ratCast` so that it loses the assumptions `[LinearOrder R] [IsStrictOrderedRing R]`.

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

mathlib-bors Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

@mathlib-bors mathlib-bors Bot changed the title refactor: Quaternion as abbrev [Merged by Bors] - refactor: Quaternion as abbrev Sep 1, 2026
@mathlib-bors mathlib-bors Bot closed this Sep 1, 2026
@Parcly-Taxel
Parcly-Taxel deleted the quaternion-helper branch September 1, 2026 14:16
joelriou pushed a commit to joelriou/mathlib4 that referenced this pull request Sep 5, 2026
* Mark `Quaternion` as an `abbrev`. Cf. [#mathlib4 > Transparency problem with quaternions @ 💬](https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Transparency.20problem.20with.20quaternions/near/618254305) and [this comment](leanprover-community#43078 (comment)).
* The instances for `Quaternion` that used to be proved by `inferInstanceAs` are removed.
* Remove five `set_option`s as a consequence.
* Move `normSq_ratCast` so that it loses the assumptions `[LinearOrder R] [IsStrictOrderedRing R]`.

Co-authored-by: Parcly Taxel <[email protected]>
@JovanGerb

Copy link
Copy Markdown
Contributor

The instance diamond linter noticed a problem introduced by this PR, namely that Quaternion.coe is a def instead of an abbrev for QuaternionAlgebra.coe.

Do you think Quaternion.coe should be deprecated altogether? Then the CoeTC instance on Quaternion can be inherited from QuaternionAlgebra automatically.

@JovanGerb

Copy link
Copy Markdown
Contributor

Ok, I have made #46446 which does this, and also makes Quaternion.im into an abbrev.

mathlib-bors Bot pushed a commit that referenced this pull request Nov 30, 2026
This PR is a follow up to #43078 which made `Quaternion` an `abbrev` for `QuaternionAlgebra`. To match this, we want that the operations on `Quaternion` are also `abbrev`s for those on `QuaternionAlgebra` where possible. As a result we can remove most `@[simp]` and `@[norm_cast]` tags on `Quaternion` lemmas.

This PR removes an instance diamond and some `simpNF` technical debt. It also removes some unnecessary `scoped simp`.
TJHeeringa pushed a commit to TJHeeringa/mathlib4 that referenced this pull request Dec 2, 2026
…community#46446)

This PR is a follow up to leanprover-community#43078 which made `Quaternion` an `abbrev` for `QuaternionAlgebra`. To match this, we want that the operations on `Quaternion` are also `abbrev`s for those on `QuaternionAlgebra` where possible. As a result we can remove most `@[simp]` and `@[norm_cast]` tags on `Quaternion` lemmas.

This PR removes an instance diamond and some `simpNF` technical debt. It also removes some unnecessary `scoped simp`.
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-algebra Algebra (groups, rings, fields, etc) 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.

9 participants