Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 45 additions & 29 deletions Mathlib/Algebra/Quaternion.lean
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ theorem coe_injective : Function.Injective (coe : R → ℍ[R,c₁,c₂,c₃]) :
theorem coe_inj {x y : R} : (x : ℍ[R,c₁,c₂,c₃]) = y ↔ x = y :=
coe_injective.eq_iff

@[simps]
instance : Zero ℍ[R,c₁,c₂,c₃] := ⟨⟨0, 0, 0, 0⟩⟩
@[simps!]
instance : Zero ℍ[R,c₁,c₂,c₃] := fast_instance% (equivProd c₁ c₂ c₃).zero
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this produce a worse term?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure, but the simp lemmas are just the same as before, so that should be no difference

Copy link
Member

Choose a reason for hiding this comment

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

In terms of unification I think the previous approach was better.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

But unification with what? With other instances? They are all like that now

Copy link
Member

@eric-wieser eric-wieser Oct 20, 2025

Choose a reason for hiding this comment

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

Are you sure? Can you show me the effect of #print on the AddCommGroup instance before and after this line being changed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is not possible. fast_instance% complains about the data fields not matching up to some transparency

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that's useful information! I was hoping it would use a higher level of transparency here.

In that case, I think it would be best to define things via the Function.Injective constructors. One argument for why this new zero instance is bad is that at runtime it's now a bunch of if statements to index a vector, rather than a hard-coded zero.

Copy link
Collaborator Author

@YaelDillies YaelDillies Oct 20, 2025

Choose a reason for hiding this comment

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

No actually it is not using vectors. It used to be, because I was using equivTuple : _ \equiv (Fin 4 \r R), but I am now using equivProd : _ \equiv R \times R \times R \times R... unless you're claiming this too is bad at runtime?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I missed that! Indeed equivProd is less bad, but still not as good as the direct constructor.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Bad enough that I should use Function.Injective.foo? I was quite happy that I finally got to use the TransferInstance files!


@[scoped simp] theorem im_zero : (0 : ℍ[R,c₁,c₂,c₃]).im = 0 := rfl

Expand Down Expand Up @@ -205,16 +205,26 @@ end Zero
section Add
variable [Add R]

@[simps]
instance : Add ℍ[R,c₁,c₂,c₃] :=
⟨fun a b => ⟨a.1 + b.1, a.2 + b.2, a.3 + b.3, a.4 + b.4⟩⟩
@[simps!]
instance : Add ℍ[R,c₁,c₂,c₃] := fast_instance% (equivProd c₁ c₂ c₃).add

@[simp]
theorem mk_add_mk (a₁ a₂ a₃ a₄ b₁ b₂ b₃ b₄ : R) :
(mk a₁ a₂ a₃ a₄ : ℍ[R,c₁,c₂,c₃]) + mk b₁ b₂ b₃ b₄ =
mk (a₁ + b₁) (a₂ + b₂) (a₃ + b₃) (a₄ + b₄) :=
rfl

/-- The additive equivalence between a quaternion algebra over `R` and `Fin 4 → R`. -/
def addEquivTuple (c₁ c₂ c₃ : R) : ℍ[R,c₁,c₂,c₃] ≃+ (Fin 4 → R) where
toEquiv := equivTuple ..
map_add' _ _ := by ext i; fin_cases i <;> rfl

@[simp]
lemma coe_addEquivTuple (c₁ c₂ c₃ : R) : ⇑(addEquivTuple c₁ c₂ c₃) = equivTuple c₁ c₂ c₃ := rfl

@[simp] lemma coe_symm_addEquivTuple (c₁ c₂ c₃ : R) :
⇑(addEquivTuple c₁ c₂ c₃).symm = (equivTuple c₁ c₂ c₃).symm := rfl

end Add

section AddZeroClass
Expand All @@ -233,8 +243,8 @@ end AddZeroClass
section Neg
variable [Neg R]

@[simps]
instance : Neg ℍ[R,c₁,c₂,c₃] := ⟨fun a => ⟨-a.1, -a.2, -a.3, -a.4⟩⟩
@[simps!]
instance : Neg ℍ[R,c₁,c₂,c₃] := fast_instance% (equivProd c₁ c₂ c₃).Neg

@[simp]
theorem neg_mk (a₁ a₂ a₃ a₄ : R) : -(mk a₁ a₂ a₃ a₄ : ℍ[R,c₁,c₂,c₃]) = ⟨-a₁, -a₂, -a₃, -a₄⟩ :=
Expand All @@ -253,9 +263,8 @@ variable [AddGroup R]
@[simp, norm_cast]
theorem coe_neg : ((-x : R) : ℍ[R,c₁,c₂,c₃]) = -x := by ext <;> simp

@[simps]
instance : Sub ℍ[R,c₁,c₂,c₃] :=
⟨fun a b => ⟨a.1 - b.1, a.2 - b.2, a.3 - b.3, a.4 - b.4⟩⟩
@[simps!]
instance : Sub ℍ[R,c₁,c₂,c₃] := fast_instance% (equivProd c₁ c₂ c₃).sub

@[simp] theorem im_sub : (a - b).im = a.im - b.im :=
QuaternionAlgebra.ext (sub_zero _).symm rfl rfl rfl
Expand Down Expand Up @@ -292,6 +301,9 @@ theorem sub_re_self : a - a.re = a.im :=

end AddGroup

instance [AddCommGroup R] : AddCommGroup ℍ[R,c₁,c₂,c₃] :=
fast_instance% (equivProd c₁ c₂ c₃).addCommGroup

section Ring
variable [Ring R]

Expand Down Expand Up @@ -327,14 +339,13 @@ section SMul

variable [SMul S R] [SMul T R] (s : S)

@[simps]
instance : SMul S ℍ[R,c₁,c₂,c₃] where smul s a := ⟨s • a.1, s • a.2, s • a.3, s • a.4⟩
@[simps!]
instance : SMul S ℍ[R,c₁,c₂,c₃] := fast_instance% (equivProd ..).smul _

instance [SMul S T] [IsScalarTower S T R] : IsScalarTower S T ℍ[R,c₁,c₂,c₃] where
smul_assoc s t x := by ext <;> exact smul_assoc _ _ _
instance [SMul S T] [IsScalarTower S T R] : IsScalarTower S T ℍ[R,c₁,c₂,c₃] :=
(equivTuple ..).isScalarTower ..

instance [SMulCommClass S T R] : SMulCommClass S T ℍ[R,c₁,c₂,c₃] where
smul_comm s t x := by ext <;> exact smul_comm _ _ _
instance [SMulCommClass S T R] : SMulCommClass S T ℍ[R,c₁,c₂,c₃] := (equivTuple ..).smulCommClass ..

@[simp] theorem im_smul {S} [CommRing R] [SMulZeroClass S R] (s : S) : (s • a).im = s • a.im :=
QuaternionAlgebra.ext (smul_zero s).symm rfl rfl rfl
Expand All @@ -348,14 +359,19 @@ theorem smul_mk (re im_i im_j im_k : R) :

end SMul

instance [Monoid S] [MulAction S R] : MulAction S ℍ[R,c₁,c₂,c₃] :=
fast_instance% (equivProd ..).mulAction _

@[simp, norm_cast]
theorem coe_smul [Zero R] [SMulZeroClass S R] (s : S) (r : R) :
(↑(s • r) : ℍ[R,c₁,c₂,c₃]) = s • (r : ℍ[R,c₁,c₂,c₃]) :=
QuaternionAlgebra.ext rfl (smul_zero _).symm (smul_zero _).symm (smul_zero _).symm

instance [AddCommGroup R] : AddCommGroup ℍ[R,c₁,c₂,c₃] :=
(equivProd c₁ c₂ c₃).injective.addCommGroup _ rfl (fun _ _ ↦ rfl) (fun _ ↦ rfl) (fun _ _ ↦ rfl)
(fun _ _ ↦ rfl) (fun _ _ ↦ rfl)
instance [Semiring S] [AddCommGroup R] [DistribMulAction S R] : DistribMulAction S ℍ[R,c₁,c₂,c₃] :=
fast_instance% (equivProd ..).distribMulAction _

instance [Semiring S] [AddCommGroup R] [Module S R] : Module S ℍ[R,c₁,c₂,c₃] :=
fast_instance% (equivProd ..).module _

section AddCommGroupWithOne
variable [AddCommGroupWithOne R]
Expand Down Expand Up @@ -486,10 +502,7 @@ lemma coe_ofNat {n : ℕ} [n.AtLeastTwo] :
((ofNat(n) : R) : ℍ[R,c₁,c₂,c₃]) = (ofNat(n) : ℍ[R,c₁,c₂,c₃]) :=
rfl

-- TODO: add weaker `MulAction`, `DistribMulAction`, and `Module` instances (and repeat them
-- for `ℍ[R]`)
instance [CommSemiring S] [Algebra S R] : Algebra S ℍ[R,c₁,c₂,c₃] where
smul := (· • ·)
algebraMap :=
{ toFun s := coe (algebraMap S R s)
map_one' := by simp only [map_one, coe_one]
Expand Down Expand Up @@ -544,13 +557,7 @@ def imKₗ : ℍ[R,c₁,c₂,c₃] →ₗ[R] R where
map_smul' _ _ := rfl

/-- `QuaternionAlgebra.equivTuple` as a linear equivalence. -/
def linearEquivTuple : ℍ[R,c₁,c₂,c₃] ≃ₗ[R] Fin 4 → R :=
LinearEquiv.symm -- proofs are not `rfl` in the forward direction
{ (equivTuple c₁ c₂ c₃).symm with
toFun := (equivTuple c₁ c₂ c₃).symm
invFun := equivTuple c₁ c₂ c₃
map_add' := fun _ _ => rfl
map_smul' := fun _ _ => rfl }
def linearEquivTuple : ℍ[R,c₁,c₂,c₃] ≃ₗ[R] Fin 4 → R := (equivTuple ..).linearEquiv _

@[simp]
theorem coe_linearEquivTuple :
Expand Down Expand Up @@ -779,6 +786,15 @@ instance [SMul S T] [SMul S R] [SMul T R] [IsScalarTower S T R] : IsScalarTower
instance [SMul S R] [SMul T R] [SMulCommClass S T R] : SMulCommClass S T ℍ[R] :=
inferInstanceAs <| SMulCommClass S T ℍ[R,-1,0,-1]

instance [Monoid S] [MulAction S R] : MulAction S ℍ[R] :=
inferInstanceAs <| MulAction S ℍ[R,-1,0,-1]

instance [Semiring S] [DistribMulAction S R] : DistribMulAction S ℍ[R] :=
inferInstanceAs <| DistribMulAction S ℍ[R,-1,0,-1]

instance [Semiring S] [Module S R] : Module S ℍ[R] :=
inferInstanceAs <| Module S ℍ[R,-1,0,-1]

protected instance algebra [CommSemiring S] [Algebra S R] : Algebra S ℍ[R] :=
inferInstanceAs <| Algebra S ℍ[R,-1,0,-1]

Expand Down