Skip to content

Commit d7c4964

Browse files
eric-wieserkim-em
authored andcommitted
feat(Algebra/TensorAlgebra): support towers of algebras (#6073)
This is pre-work towards a base-change of clifford algebras. The main result here is ```lean @[nolint unusedArguments] instance instAlgebra {R A M} [CommSemiring R] [AddCommMonoid M] [CommSemiring A] [Algebra R A] [Module R M] [Module A M] [IsScalarTower R A M] : Algebra R (TensorAlgebra A M) ``` Note that strictly the `IsScalarTower R A M` argument isn't needed, but I'd claim the instance doesn't make any sense without it. In order to prevent diamonds in the `algebraMap` fields of the `Int` and `Nat` algebra instances, we have stop having `natCast` as an `irreducible_def`, and we have to add a missing `intCast` customization for `RingQuot`. In order to prevent diamonds in the `smul` fields there and elsewhere (such as a complex tensor algebra being a real algebra), we have to stop having the `smul` definition as an `irreducible_def`. We already had to make a similar refactor to prevent diamonds for `Algebra R (Polynomial A)`. If we backport any of this to mathlib3, we'd additionally have to change the `smul` definition to not use pattern matching. Thankfully, structure eta in Lean 4 makes that unnecessary. These diamonds are tested with inline `examples`.
1 parent 5f18557 commit d7c4964

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

Mathlib/Algebra/RingQuot.lean

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ namespace RingQuot
155155

156156
variable (r : R → R → Prop)
157157

158-
private irreducible_def natCast (n : ℕ) : RingQuot r :=
158+
-- can't be irreducible, causes diamonds in ℕ-algebras
159+
private def natCast (n : ℕ) : RingQuot r :=
159160
⟨Quot.mk _ n⟩
160161

161162
private irreducible_def zero : RingQuot r :=
@@ -196,9 +197,13 @@ private irreducible_def npow (n : ℕ) : RingQuot r → RingQuot r
196197
exact this)
197198
a⟩
198199

199-
private irreducible_def smul [Algebra S R] (n : S) : RingQuot r → RingQuot r
200+
-- note: this cannot be irreducible, as otherwise diamonds don't commute.
201+
private def smul [Algebra S R] (n : S) : RingQuot r → RingQuot r
200202
| ⟨a⟩ => ⟨Quot.map (fun a ↦ n • a) (Rel.smul n) a⟩
201203

204+
instance : NatCast (RingQuot r) :=
205+
⟨natCast r⟩
206+
202207
instance : Zero (RingQuot r) :=
203208
⟨zero r⟩
204209

@@ -265,7 +270,7 @@ theorem sub_quot {R : Type uR} [Ring R] (r : R → R → Prop) {a b} :
265270
theorem smul_quot [Algebra S R] {n : S} {a : R} :
266271
(n • ⟨Quot.mk _ a⟩ : RingQuot r) = ⟨Quot.mk _ (n • a)⟩ := by
267272
show smul r _ _ = _
268-
rw [smul_def]
273+
rw [smul]
269274
rfl
270275
#align ring_quot.smul_quot RingQuot.smul_quot
271276

@@ -327,8 +332,8 @@ instance instMonoidWithZero (r : R → R → Prop) : MonoidWithZero (RingQuot r)
327332

328333
instance instSemiring (r : R → R → Prop) : Semiring (RingQuot r) where
329334
natCast := natCast r
330-
natCast_zero := by simp [Nat.cast, natCast_def, ← zero_quot]
331-
natCast_succ := by simp [Nat.cast, natCast_def, ← one_quot, add_quot]
335+
natCast_zero := by simp [Nat.cast, natCast, ← zero_quot]
336+
natCast_succ := by simp [Nat.cast, natCast, ← one_quot, add_quot]
332337
left_distrib := by
333338
rintro ⟨⟨⟩⟩ ⟨⟨⟩⟩ ⟨⟨⟩⟩
334339
simp only [mul_quot, add_quot, left_distrib]
@@ -346,6 +351,10 @@ instance instSemiring (r : R → R → Prop) : Semiring (RingQuot r) where
346351
__ := instAddCommMonoid r
347352
__ := instMonoidWithZero r
348353

354+
-- can't be irreducible, causes diamonds in ℤ-algebras
355+
private def intCast {R : Type uR} [Ring R] (r : R → R → Prop) (z : ℤ) : RingQuot r :=
356+
⟨Quot.mk _ z⟩
357+
349358
instance instRing {R : Type uR} [Ring R] (r : R → R → Prop) : Ring (RingQuot r) :=
350359
{ RingQuot.instSemiring r with
351360
neg := Neg.neg
@@ -365,7 +374,13 @@ instance instRing {R : Type uR} [Ring R] (r : R → R → Prop) : Ring (RingQuot
365374
simp [smul_quot, add_quot, add_mul, add_comm]
366375
zsmul_neg' := by
367376
rintro n ⟨⟨⟩⟩
368-
simp [smul_quot, neg_quot, add_mul] }
377+
simp [smul_quot, neg_quot, add_mul]
378+
intCast := intCast r
379+
intCast_ofNat := fun n => congrArg RingQuot.mk <| by
380+
exact congrArg (Quot.mk _) (Int.cast_ofNat _)
381+
intCast_negSucc := fun n => congrArg RingQuot.mk <| by
382+
simp_rw [neg_def]
383+
exact congrArg (Quot.mk _) (Int.cast_negSucc n) }
369384

370385
instance instCommSemiring {R : Type uR} [CommSemiring R] (r : R → R → Prop) :
371386
CommSemiring (RingQuot r) :=

Mathlib/LinearAlgebra/TensorAlgebra/Basic.lean

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,39 @@ def TensorAlgebra :=
6767
-- Porting note: Expanded `deriving Inhabited, Semiring, Algebra`
6868
instance : Inhabited (TensorAlgebra R M) := RingQuot.instInhabitedRingQuot _
6969
instance : Semiring (TensorAlgebra R M) := RingQuot.instSemiring _
70-
instance : Algebra R (TensorAlgebra R M) := RingQuot.instAlgebraRingQuotInstSemiring _
70+
71+
-- `IsScalarTower` is not needed, but the instance isn't really canonical without it.
72+
@[nolint unusedArguments]
73+
instance instAlgebra {R A M} [CommSemiring R] [AddCommMonoid M] [CommSemiring A]
74+
[Algebra R A] [Module R M] [Module A M]
75+
[IsScalarTower R A M] :
76+
Algebra R (TensorAlgebra A M) :=
77+
RingQuot.instAlgebraRingQuotInstSemiring _
78+
79+
-- verify there is no diamond
80+
example : (algebraNat : Algebra ℕ (TensorAlgebra R M)) = instAlgebra := rfl
81+
82+
instance {R S A M} [CommSemiring R] [CommSemiring S] [AddCommMonoid M] [CommSemiring A]
83+
[Algebra R A] [Algebra S A] [Module R M] [Module S M] [Module A M]
84+
[IsScalarTower R A M] [IsScalarTower S A M] [SMulCommClass R S A] :
85+
SMulCommClass R S (TensorAlgebra A M) :=
86+
RingQuot.instSMulCommClassRingQuotInstSMulRingQuotInstSMulRingQuot _
87+
88+
instance {R S A M} [CommSemiring R] [CommSemiring S] [AddCommMonoid M] [CommSemiring A]
89+
[SMul R S] [Algebra R A] [Algebra S A] [Module R M] [Module S M] [Module A M]
90+
[IsScalarTower R A M] [IsScalarTower S A M] [IsScalarTower R S A] :
91+
IsScalarTower R S (TensorAlgebra A M) :=
92+
RingQuot.instIsScalarTowerRingQuotInstSMulRingQuotInstSMulRingQuot _
7193

7294
namespace TensorAlgebra
7395

7496
instance {S : Type _} [CommRing S] [Module S M] : Ring (TensorAlgebra S M) :=
7597
RingQuot.instRing (Rel S M)
7698

99+
-- verify there is no diamond
100+
variable (S M : Type) [CommRing S] [AddCommGroup M] [Module S M] in
101+
example : (algebraInt _ : Algebra ℤ (TensorAlgebra S M)) = instAlgebra := rfl
102+
77103
variable {M}
78104

79105
/-- The canonical linear map `M →ₗ[R] TensorAlgebra R M`.

0 commit comments

Comments
 (0)