Skip to content

Commit f50b40f

Browse files
committed
perf: shortcut instances for Semiring (#39719)
This PR adds shortcut instances that make sure synthesis using `Semiring` doesn't go through the other ring-like classes. This matches the new definition of `Semiring`.
1 parent 9dc3933 commit f50b40f

7 files changed

Lines changed: 23 additions & 19 deletions

File tree

Mathlib/Algebra/Ring/Defs.lean

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ class Semiring (α : Type u) extends AddCommMonoid α, MonoidWithZero α, NonUni
146146
/-- A `Ring` is a `Semiring` with negation making it an additive group. -/
147147
class Ring (R : Type u) extends Semiring R, AddCommGroup R, AddGroupWithOne R
148148

149+
-- Add some short-cut instances to avoid going through the less used ring type classes.
150+
instance [Semiring α] : Distrib α := inferInstance
151+
instance [Semiring α] : MulZeroClass α := inferInstance
152+
instance [Semiring α] : MulZeroOneClass α := inferInstance
153+
attribute [instance] Semiring.toMonoid
154+
149155
/-!
150156
### Semirings
151157
-/

Mathlib/Algebra/Ring/Ext.lean

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,10 @@ theorem toNonUnitalRing_injective :
339339

340340
theorem toNonAssocRing_injective :
341341
Function.Injective (@toNonAssocRing R) := by
342-
intro _ _ _
343-
ext <;> congr
342+
intro _ _ h
343+
ext x y
344+
· exact congrArg (·.toAdd.add x y) h
345+
· exact congrArg (·.toMul.mul x y) h
344346

345347
theorem toSemiring_injective :
346348
Function.Injective (@toSemiring R) := by

Mathlib/Analysis/Quaternion.lean

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The following notation is available with `open Quaternion` or `open scoped Quate
3232
quaternion, normed ring, normed space, normed algebra
3333
-/
3434

35-
@[expose] public section
35+
@[expose] public noncomputable section
3636

3737

3838
@[inherit_doc] scoped[Quaternion] notation "ℍ" => Quaternion ℝ
@@ -50,7 +50,7 @@ theorem inner_self (a : ℍ) : ⟪a, a⟫ = normSq a :=
5050
theorem inner_def (a b : ℍ) : ⟪a, b⟫ = (a * star b).re :=
5151
rfl
5252

53-
noncomputable instance : NormedAddCommGroup ℍ :=
53+
instance : NormedAddCommGroup ℍ :=
5454
@InnerProductSpace.Core.toNormedAddCommGroup ℝ ℍ _ _ _
5555
{ toInner := inferInstance
5656
conj_inner_symm := fun x y => by simp [inner_def, mul_comm]
@@ -59,7 +59,7 @@ noncomputable instance : NormedAddCommGroup ℍ :=
5959
add_left := fun x y z => by simp only [inner_def, add_mul, re_add]
6060
smul_left := fun x y r => by simp [inner_def] }
6161

62-
noncomputable instance : InnerProductSpace ℝ ℍ :=
62+
instance : InnerProductSpace ℝ ℍ :=
6363
InnerProductSpace.ofCore _
6464

6565
theorem normSq_eq_norm_mul_self (a : ℍ) : normSq a = ‖a‖ * ‖a‖ := by
@@ -84,11 +84,11 @@ theorem norm_star (a : ℍ) : ‖star a‖ = ‖a‖ := by
8484
theorem nnnorm_star (a : ℍ) : ‖star a‖₊ = ‖a‖₊ :=
8585
Subtype.ext <| norm_star a
8686

87-
noncomputable instance : NormedDivisionRing ℍ where
87+
instance : NormedDivisionRing ℍ where
8888
dist_eq _ _ := rfl
8989
norm_mul _ _ := by simp_rw [norm_eq_sqrt_real_inner, inner_self]; simp
9090

91-
noncomputable instance : NormedAlgebra ℝ ℍ where
91+
instance : NormedAlgebra ℝ ℍ where
9292
norm_smul_le := norm_smul_le
9393
toAlgebra := Quaternion.algebra
9494

@@ -139,7 +139,7 @@ theorem coeComplex_coe (r : ℝ) : ((r : ℂ) : ℍ) = r :=
139139
rfl
140140

141141
/-- Coercion `ℂ →ₐ[ℝ] ℍ` as an algebra homomorphism. -/
142-
noncomputable def ofComplex : ℂ →ₐ[ℝ] ℍ where
142+
def ofComplex : ℂ →ₐ[ℝ] ℍ where
143143
toFun := (↑)
144144
map_one' := rfl
145145
map_zero' := rfl
@@ -159,7 +159,7 @@ lemma norm_toLp_equivTuple (x : ℍ) : ‖WithLp.toLp 2 (equivTuple ℝ x)‖ =
159159

160160
/-- `QuaternionAlgebra.linearEquivTuple` as a `LinearIsometryEquiv`. -/
161161
@[simps apply symm_apply]
162-
noncomputable def linearIsometryEquivTuple : ℍ ≃ₗᵢ[ℝ] EuclideanSpace ℝ (Fin 4) :=
162+
def linearIsometryEquivTuple : ℍ ≃ₗᵢ[ℝ] EuclideanSpace ℝ (Fin 4) :=
163163
{ (QuaternionAlgebra.linearEquivTuple (-1 : ℝ) (0 : ℝ) (-1 : ℝ)).trans
164164
(WithLp.linearEquiv 2 ℝ (Fin 4 → ℝ)).symm with
165165
toFun := fun a => !₂[a.1, a.2, a.3, a.4]

Mathlib/RingTheory/EuclideanDomain.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def gcdMonoid (R) [EuclideanDomain R] [DecidableEq R] : GCDMonoid R where
7676
gcd_dvd_left := gcd_dvd_left
7777
gcd_dvd_right := gcd_dvd_right
7878
dvd_gcd := dvd_gcd
79-
gcd_mul_lcm a b := by rw [EuclideanDomain.gcd_mul_lcm]
79+
gcd_mul_lcm a b := by rw [EuclideanDomain.gcd_mul_lcm]; rfl
8080
lcm_zero_left := lcm_zero_left
8181
lcm_zero_right := lcm_zero_right
8282

Mathlib/Tactic/Ring/Basic.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,7 @@ theorem Nat.smul_eq_mul {n n' : ℕ} {r : R} (hr : n = r) (hn : n' = n) (a : R)
357357
subst_vars
358358
simp only [nsmul_eq_mul]
359359

360-
omit [CommSemiring R] in
361-
theorem Int.smul_eq_mul {n n' : ℤ} {r : R} [CommRing R] (hr : n = r) (hn : n' = n) (a : R) :
360+
theorem Int.smul_eq_mul {R} {n n' : ℤ} {r : R} [CommRing R] (hr : n = r) (hn : n' = n) (a : R) :
362361
n' • a = r * a := by
363362
subst_vars
364363
simp only [zsmul_eq_mul]

Mathlib/Topology/Algebra/InfiniteSum/Ring.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,10 @@ theorem tprod_one_add [T2Space α] (h : Summable (∏ i ∈ ·, f i)) :
317317
HasProd.tprod_eq <| hasProd_one_add_of_hasSum_prod h.hasSum
318318

319319
section Ordered
320-
variable [LinearOrder ι] [LocallyFiniteOrderBot ι] [T2Space α]
320+
variable [LinearOrder ι] [LocallyFiniteOrderBot ι]
321321

322322
/-- The infinite version of `Finset.prod_one_add_ordered`. -/
323-
theorem tprod_one_add_ordered [ContinuousAdd α]
323+
theorem tprod_one_add_ordered [T2Space α] [ContinuousAdd α]
324324
(hsum : Summable fun i ↦ f i * ∏ j ∈ Iio i, (1 + f j))
325325
(hprod : Multipliable (1 + f ·)) :
326326
∏' i, (1 + f i) = 1 + ∑' i, f i * ∏ j ∈ Iio i, (1 + f j) := by
@@ -339,9 +339,9 @@ theorem tprod_one_add_ordered [ContinuousAdd α]
339339
congr
340340
grind
341341

342-
omit [CommSemiring α] in
343342
/-- The infinite version of `Finset.prod_one_sub_ordered`. -/
344-
theorem tprod_one_sub_ordered [CommRing α] [IsTopologicalAddGroup α]
343+
theorem tprod_one_sub_ordered {α : Type*} {f : ι → α}
344+
[CommRing α] [TopologicalSpace α] [T2Space α] [IsTopologicalAddGroup α]
345345
(hsum : Summable fun i ↦ f i * ∏ j ∈ Iio i, (1 - f j))
346346
(hprod : Multipliable (1 - f ·)) :
347347
∏' i, (1 - f i) = 1 - ∑' i, f i * ∏ j ∈ Iio i, (1 - f j) := by

MathlibTest/hintAll.lean

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,6 @@ info: Try these:
203203
[apply] norm_num
204204
Remaining subgoals:
205205
⊢ a /ₚ u₁ + b /ₚ u₁ = (a + b) /ₚ u₁
206-
[apply] ring_nf
207-
Remaining subgoals:
208-
⊢ a /ₚ u₁ + b /ₚ u₁ = (a + b) /ₚ u₁
209206
[apply] abel_nf
210207
Remaining subgoals:
211208
⊢ a /ₚ u₁ + b /ₚ u₁ = (a + b) /ₚ u₁

0 commit comments

Comments
 (0)