Skip to content

Commit 331b33c

Browse files
committed
fix diamonds
1 parent 01ee5d5 commit 331b33c

1 file changed

Lines changed: 61 additions & 31 deletions

File tree

Mathlib/Algebra/FreeAlgebra.lean

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -160,26 +160,31 @@ namespace FreeAlgebra
160160
attribute [local instance] Pre.hasCoeGenerator Pre.hasCoeSemiring Pre.hasMul Pre.hasAdd
161161
Pre.hasZero Pre.hasOne Pre.hasSmul
162162

163-
instance {A} [CommSemiring A] [Algebra R A] : SMul R (FreeAlgebra A X) where
164-
smul r := Quot.map (algebraMap R A r * ·) fun _ _ ↦ Rel.mul_compat_right
163+
/-! Define the basic operations-/
165164

166-
instance : Semiring (FreeAlgebra R X) where
167-
add := Quot.map₂ (· + ·) (fun _ _ _ ↦ Rel.add_compat_right) fun _ _ _ ↦ Rel.add_compat_left
168-
add_assoc := by
169-
rintro ⟨⟩ ⟨⟩ ⟨⟩
170-
exact Quot.sound Rel.add_assoc
171-
zero := Quot.mk _ 0
172-
zero_add := by
173-
rintro ⟨⟩
174-
exact Quot.sound Rel.zero_add
175-
add_zero := by
176-
rintro ⟨⟩
177-
change Quot.mk _ _ = _
178-
rw [Quot.sound Rel.add_comm, Quot.sound Rel.zero_add]
179-
add_comm := by
180-
rintro ⟨⟩ ⟨⟩
181-
exact Quot.sound Rel.add_comm
182-
mul := Quot.map₂ (· * ·) (fun _ _ _ ↦ Rel.mul_compat_right) fun _ _ _ ↦ Rel.mul_compat_left
165+
instance instSMul {A} [CommSemiring A] [Algebra R A] : SMul R (FreeAlgebra A X) where
166+
smul r := Quot.map (HMul.hMul (algebraMap R A r : Pre A X)) fun _ _ ↦ Rel.mul_compat_right
167+
168+
instance instZero : Zero (FreeAlgebra R X) where zero := Quot.mk _ 0
169+
170+
instance instOne : One (FreeAlgebra R X) where one := Quot.mk _ 1
171+
172+
instance instAdd : Add (FreeAlgebra R X) where
173+
add := Quot.map₂ HAdd.hAdd (fun _ _ _ ↦ Rel.add_compat_right) fun _ _ _ ↦ Rel.add_compat_left
174+
175+
instance instMul : Mul (FreeAlgebra R X) where
176+
mul := Quot.map₂ HMul.hMul (fun _ _ _ ↦ Rel.mul_compat_right) fun _ _ _ ↦ Rel.mul_compat_left
177+
178+
-- `Quot.mk` is an implementation detail of `FreeAlgebra`, so this lemma is private
179+
private theorem mk_mul (x y : Pre R X) :
180+
Quot.mk (Rel R X) (x * y) = (HMul.hMul (self := instHMul (α := FreeAlgebra R X))
181+
(Quot.mk (Rel R X) x) (Quot.mk (Rel R X) y)) :=
182+
rfl
183+
184+
/-! Build the semiring structure. We do this one piece at a time as this is convenient for proving
185+
the `nsmul` fields. -/
186+
187+
instance instMonoidWithZero : MonoidWithZero (FreeAlgebra R X) where
183188
mul_assoc := by
184189
rintro ⟨⟩ ⟨⟩ ⟨⟩
185190
exact Quot.sound Rel.mul_assoc
@@ -190,32 +195,55 @@ instance : Semiring (FreeAlgebra R X) where
190195
mul_one := by
191196
rintro ⟨⟩
192197
exact Quot.sound Rel.mul_one
198+
zero_mul := by
199+
rintro ⟨⟩
200+
exact Quot.sound Rel.MulZeroClass.zero_mul
201+
mul_zero := by
202+
rintro ⟨⟩
203+
exact Quot.sound Rel.MulZeroClass.mul_zero
204+
205+
instance instDistrib : Distrib (FreeAlgebra R X) where
193206
left_distrib := by
194207
rintro ⟨⟩ ⟨⟩ ⟨⟩
195208
exact Quot.sound Rel.left_distrib
196209
right_distrib := by
197210
rintro ⟨⟩ ⟨⟩ ⟨⟩
198211
exact Quot.sound Rel.right_distrib
199-
zero_mul := by
212+
213+
instance instAddCommMonoid : AddCommMonoid (FreeAlgebra R X) where
214+
add_assoc := by
215+
rintro ⟨⟩ ⟨⟩ ⟨⟩
216+
exact Quot.sound Rel.add_assoc
217+
zero_add := by
200218
rintro ⟨⟩
201-
exact Quot.sound Rel.MulZeroClass.zero_mul
202-
mul_zero := by
219+
exact Quot.sound Rel.zero_add
220+
add_zero := by
203221
rintro ⟨⟩
204-
exact Quot.sound Rel.MulZeroClass.mul_zero
222+
change Quot.mk _ _ = _
223+
rw [Quot.sound Rel.add_comm, Quot.sound Rel.zero_add]
224+
add_comm := by
225+
rintro ⟨⟩ ⟨⟩
226+
exact Quot.sound Rel.add_comm
205227
nsmul := (· • ·)
206228
nsmul_zero := by
207229
rintro ⟨⟩
208230
change Quot.mk _ (_ * _) = _
209231
rw [map_zero]
210232
exact Quot.sound Rel.MulZeroClass.zero_mul
211233
nsmul_succ n := by
212-
rintro ⟨⟩
213-
change Quot.mk _ (_ * _) = Quot.mk _ _
214-
rw [map_add, map_one]
215-
sorry
234+
rintro ⟨a⟩
235+
dsimp only [HSMul.hSMul, instSMul, Quot.map]
236+
rw [map_add, map_one, add_comm, mk_mul, mk_mul, ←one_add_mul (_ : FreeAlgebra R X)]
237+
congr 1
238+
exact Quot.sound Rel.add_scalar
239+
240+
instance : Semiring (FreeAlgebra R X) where
241+
__ := instMonoidWithZero R X
242+
__ := instAddCommMonoid R X
243+
__ := instDistrib R X
216244
natCast n := Quot.mk _ (n : R)
217-
natCast_zero := sorry
218-
natCast_succ n := sorry
245+
natCast_zero := by simp; rfl
246+
natCast_succ n := by simp; exact Quot.sound Rel.add_scalar
219247

220248
instance : Inhabited (FreeAlgebra R X) :=
221249
0
@@ -233,6 +261,10 @@ instance instAlgebra {A} [CommSemiring A] [Algebra R A] : Algebra R (FreeAlgebra
233261
exact Quot.sound Rel.central_scalar
234262
smul_def' _ _ := rfl
235263

264+
-- verify there is no diamond
265+
variable (S : Type) [CommSemiring S] in
266+
example : (algebraNat : Algebra ℕ (FreeAlgebra S X)) = instAlgebra _ _ := rfl
267+
236268
instance {R S A} [CommSemiring R] [CommSemiring S] [CommSemiring A]
237269
[SMul R S] [Algebra R A] [Algebra S A] [IsScalarTower R S A] :
238270
IsScalarTower R S (FreeAlgebra A X) where
@@ -252,8 +284,6 @@ instance {S : Type _} [CommRing S] : Ring (FreeAlgebra S X) :=
252284
Algebra.semiringToRing S
253285

254286
-- verify there is no diamond
255-
variable (S : Type) [CommSemiring S] in
256-
example : (algebraNat : Algebra ℕ (FreeAlgebra S X)) = instAlgebra _ _ := rfl
257287
variable (S : Type) [CommRing S] in
258288
example : (algebraInt _ : Algebra ℤ (FreeAlgebra S X)) = instAlgebra _ _ := rfl
259289

0 commit comments

Comments
 (0)