Skip to content

Commit 5759c85

Browse files
committed
feat: generalize Mathlib.Algebra.BigOperators + CharP + Star + misc others (#23195)
This is one of a series of PRs that generalizes type classes across Mathlib. These are generated using a new linter that tries to re-elaborate theorem definitions with more general type classes to see if it succeeds. It will accept the generalization if deleting the entire type class causes the theorem to fail to compile, and the old type class can not simply be re-synthesized with the new declaration. Otherwise, the generalization is rejected as the type class is not being generalized, but can simply be replaced by implicit type class synthesis or an implicit type class in a variable block being pulled in. The linter currently output debug statements indicating source file positions where type classes should be generalized, and a script then makes those edits. This file contains a subset of those generalizations. The linter and the script performing re-writes is available in commit 5e2b704. Also see discussion on Zulip here: https://leanprover.zulipchat.com/#narrow/channel/270676-lean4/topic/Elab.20to.20generalize.20type.20classes.20for.20theorems/near/498862988 https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Elab.20to.20generalize.20type.20classes.20for.20theorems/near/501288855
1 parent ad232f9 commit 5759c85

File tree

17 files changed

+47
-46
lines changed

17 files changed

+47
-46
lines changed

Mathlib/Algebra/AddConstMap/Basic.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ theorem map_ofNat [AddMonoidWithOne G] [AddMonoidWithOne H] [AddConstMapClass F
128128
f ofNat(n) = f 0 + ofNat(n) := map_nat f n
129129

130130
@[scoped simp]
131-
theorem map_const_add [AddCommSemigroup G] [Add H] [AddConstMapClass F G H a b]
131+
theorem map_const_add [AddCommMagma G] [Add H] [AddConstMapClass F G H a b]
132132
(f : F) (x : G) : f (a + x) = f x + b := by
133133
rw [add_comm, map_add_const]
134134

Mathlib/Algebra/AlgebraicCard.lean

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ open Cardinal Polynomial
2525

2626
namespace Algebraic
2727

28-
theorem infinite_of_charZero (R A : Type*) [CommRing R] [IsDomain R] [Ring A] [Algebra R A]
29-
[CharZero A] : { x : A | IsAlgebraic R x }.Infinite :=
30-
infinite_of_injective_forall_mem Nat.cast_injective isAlgebraic_nat
28+
theorem infinite_of_charZero (R A : Type*) [CommRing R] [Ring A] [Algebra R A]
29+
[CharZero A] : { x : A | IsAlgebraic R x }.Infinite := by
30+
letI := MulActionWithZero.nontrivial R A
31+
exact infinite_of_injective_forall_mem Nat.cast_injective isAlgebraic_nat
3132

32-
theorem aleph0_le_cardinalMk_of_charZero (R A : Type*) [CommRing R] [IsDomain R] [Ring A]
33+
theorem aleph0_le_cardinalMk_of_charZero (R A : Type*) [CommRing R] [Ring A]
3334
[Algebra R A] [CharZero A] : ℵ₀ ≤ #{ x : A // IsAlgebraic R x } :=
3435
infinite_iff.1 (Set.infinite_coe_iff.2 <| infinite_of_charZero R A)
3536

@@ -80,7 +81,7 @@ protected theorem countable : Set.Countable { x : A | IsAlgebraic R x } := by
8081
simp
8182

8283
@[simp]
83-
theorem cardinalMk_of_countable_of_charZero [CharZero A] [IsDomain R] :
84+
theorem cardinalMk_of_countable_of_charZero [CharZero A] :
8485
#{ x : A // IsAlgebraic R x } = ℵ₀ :=
8586
(Algebraic.countable R A).le_aleph0.antisymm (aleph0_le_cardinalMk_of_charZero R A)
8687

Mathlib/Algebra/BigOperators/Fin.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ theorem prod_ofFn {n : ℕ} {f : Fin n → α} : (ofFn f).prod = ∏ i, f i :=
454454
end CommMonoid
455455

456456
@[to_additive]
457-
theorem alternatingProd_eq_finset_prod {G : Type*} [CommGroup G] :
457+
theorem alternatingProd_eq_finset_prod {G : Type*} [DivisionCommMonoid G] :
458458
∀ (L : List G), alternatingProd L = ∏ i : Fin L.length, L[i] ^ (-1 : ℤ) ^ (i : ℕ)
459459
| [] => by
460460
rw [alternatingProd, Finset.prod_eq_one]

Mathlib/Algebra/BigOperators/Finprod.lean

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ theorem finsum_smul {R M : Type*} [Ring R] [AddCommGroup M] [Module R M] [NoZero
304304

305305
/-- The `NoZeroSMulDivisors` makes sure that the result holds even when the support of `f` is
306306
infinite. For a more usual version assuming `(support f).Finite` instead, see `smul_finsum'`. -/
307-
theorem smul_finsum {R M : Type*} [Ring R] [AddCommGroup M] [Module R M] [NoZeroSMulDivisors R M]
308-
(c : R) (f : ι → M) : (c • ∑ᶠ i, f i) = ∑ᶠ i, c • f i := by
307+
theorem smul_finsum {R M : Type*} [Semiring R] [AddCommGroup M] [Module R M]
308+
[NoZeroSMulDivisors R M] (c : R) (f : ι → M) : (c • ∑ᶠ i, f i) = ∑ᶠ i, c • f i := by
309309
rcases eq_or_ne c 0 with (rfl | hc)
310310
· simp
311311
· exact (smulAddHom R M c).map_finsum_of_injective (smul_right_injective M hc) _
@@ -1030,12 +1030,12 @@ theorem prod_finprod_comm (s : Finset α) (f : α → β → M) (h : ∀ a ∈ s
10301030
(∏ a ∈ s, ∏ᶠ b : β, f a b) = ∏ᶠ b : β, ∏ a ∈ s, f a b :=
10311031
(finprod_prod_comm s (fun b a => f a b) h).symm
10321032

1033-
theorem mul_finsum {R : Type*} [Semiring R] (f : α → R) (r : R) (h : (support f).Finite) :
1034-
(r * ∑ᶠ a : α, f a) = ∑ᶠ a : α, r * f a :=
1033+
theorem mul_finsum {R : Type*} [NonUnitalNonAssocSemiring R] (f : α → R) (r : R)
1034+
(h : (support f).Finite) : (r * ∑ᶠ a : α, f a) = ∑ᶠ a : α, r * f a :=
10351035
(AddMonoidHom.mulLeft r).map_finsum h
10361036

1037-
theorem finsum_mul {R : Type*} [Semiring R] (f : α → R) (r : R) (h : (support f).Finite) :
1038-
(∑ᶠ a : α, f a) * r = ∑ᶠ a : α, f a * r :=
1037+
theorem finsum_mul {R : Type*} [NonUnitalNonAssocSemiring R] (f : α → R) (r : R)
1038+
(h : (support f).Finite) : (∑ᶠ a : α, f a) * r = ∑ᶠ a : α, f a * r :=
10391039
(AddMonoidHom.mulRight r).map_finsum h
10401040

10411041
@[to_additive (attr := simp)]

Mathlib/Algebra/BigOperators/GroupWithZero/Action.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ end
9494
namespace List
9595

9696
@[to_additive]
97-
theorem smul_prod [Monoid α] [Monoid β] [MulAction α β] [IsScalarTower α β β] [SMulCommClass α β β]
98-
(l : List β) (m : α) :
97+
theorem smul_prod [Monoid α] [MulOneClass β] [MulAction α β] [IsScalarTower α β β]
98+
[SMulCommClass α β β] (l : List β) (m : α) :
9999
m ^ l.length • l.prod = (l.map (m • ·)).prod := by
100100
induction l with
101101
| nil => simp

Mathlib/Algebra/BigOperators/Pi.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ theorem prod_mk_prod {α β γ : Type*} [CommMonoid α] [CommMonoid β] (s : Fin
6060
Finset.induction_on s rfl (by simp +contextual [Prod.ext_iff])
6161

6262
/-- decomposing `x : ι → R` as a sum along the canonical basis -/
63-
theorem pi_eq_sum_univ {ι : Type*} [Fintype ι] [DecidableEq ι] {R : Type*} [Semiring R]
63+
theorem pi_eq_sum_univ {ι : Type*} [Fintype ι] [DecidableEq ι] {R : Type*} [NonAssocSemiring R]
6464
(x : ι → R) : x = ∑ i, (x i) • fun j => if i = j then (1 : R) else 0 := by
6565
ext
6666
simp

Mathlib/Algebra/BigOperators/Ring/List.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ lemma dvd_sum [NonUnitalSemiring R] {a} {l : List R} (h : ∀ x ∈ l, a ∣ x)
8989
rw [List.sum_cons]
9090
exact dvd_add (h _ mem_cons_self) (ih fun x hx ↦ h x (mem_cons_of_mem _ hx))
9191

92-
@[simp] lemma sum_zipWith_distrib_left [Semiring R] (f : ι → κ → R) (a : R) :
92+
@[simp] lemma sum_zipWith_distrib_left [NonUnitalNonAssocSemiring R] (f : ι → κ → R) (a : R) :
9393
∀ (l₁ : List ι) (l₂ : List κ),
9494
(zipWith (fun i j ↦ a * f i j) l₁ l₂).sum = a * (zipWith f l₁ l₂).sum
9595
| [], _ => by simp

Mathlib/Algebra/BigOperators/RingEquiv.lean

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ variable {α R S : Type*}
1919
protected theorem map_list_prod [Semiring R] [Semiring S] (f : R ≃+* S) (l : List R) :
2020
f l.prod = (l.map f).prod := map_list_prod f l
2121

22-
protected theorem map_list_sum [NonAssocSemiring R] [NonAssocSemiring S] (f : R ≃+* S)
23-
(l : List R) : f l.sum = (l.map f).sum := map_list_sum f l
22+
protected theorem map_list_sum [NonUnitalNonAssocSemiring R] [NonUnitalNonAssocSemiring S]
23+
(f : R ≃+* S) (l : List R) : f l.sum = (l.map f).sum := map_list_sum f l
2424

2525
/-- An isomorphism into the opposite ring acts on the product by acting on the reversed elements -/
2626
protected theorem unop_map_list_prod [Semiring R] [Semiring S] (f : R ≃+* Sᵐᵒᵖ) (l : List R) :
@@ -31,16 +31,16 @@ protected theorem map_multiset_prod [CommSemiring R] [CommSemiring S] (f : R ≃
3131
(s : Multiset R) : f s.prod = (s.map f).prod :=
3232
map_multiset_prod f s
3333

34-
protected theorem map_multiset_sum [NonAssocSemiring R] [NonAssocSemiring S] (f : R ≃+* S)
35-
(s : Multiset R) : f s.sum = (s.map f).sum :=
34+
protected theorem map_multiset_sum [NonUnitalNonAssocSemiring R] [NonUnitalNonAssocSemiring S]
35+
(f : R ≃+* S) (s : Multiset R) : f s.sum = (s.map f).sum :=
3636
map_multiset_sum f s
3737

3838
protected theorem map_prod [CommSemiring R] [CommSemiring S] (g : R ≃+* S) (f : α → R)
3939
(s : Finset α) : g (∏ x ∈ s, f x) = ∏ x ∈ s, g (f x) :=
4040
map_prod g f s
4141

42-
protected theorem map_sum [NonAssocSemiring R] [NonAssocSemiring S] (g : R ≃+* S) (f : α → R)
43-
(s : Finset α) : g (∑ x ∈ s, f x) = ∑ x ∈ s, g (f x) :=
42+
protected theorem map_sum [NonUnitalNonAssocSemiring R] [NonUnitalNonAssocSemiring S] (g : R ≃+* S)
43+
(f : α → R) (s : Finset α) : g (∑ x ∈ s, f x) = ∑ x ∈ s, g (f x) :=
4444
map_sum g f s
4545

4646
end RingEquiv

Mathlib/Algebra/CharP/Algebra.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ theorem CharP.dvd_of_ringHom {R A : Type*} [NonAssocSemiring R] [NonAssocSemirin
3535
rw [← map_natCast f p, CharP.cast_eq_zero, map_zero]
3636

3737
/-- Given `R →+* A`, where `R` is a domain with `char R > 0`, then `char A = char R`. -/
38-
theorem CharP.of_ringHom_of_ne_zero {R A : Type*} [Ring R] [NoZeroDivisors R]
38+
theorem CharP.of_ringHom_of_ne_zero {R A : Type*} [NonAssocSemiring R] [NoZeroDivisors R]
3939
[NonAssocSemiring A] [Nontrivial A]
4040
(f : R →+* A) (p : ℕ) (hp : p ≠ 0) [CharP R p] : CharP A p := by
4141
have := f.domain_nontrivial
@@ -88,23 +88,23 @@ protected theorem RingHom.charP_iff {R A : Type*} [NonAssocSemiring R] [NonAssoc
8888
/-- If a ring homomorphism `R →+* A` is injective then `A` has the same exponential characteristic
8989
as `R`. -/
9090
lemma expChar_of_injective_ringHom {R A : Type*}
91-
[Semiring R] [Semiring A] {f : R →+* A} (h : Function.Injective f)
91+
[NonAssocSemiring R] [NonAssocSemiring A] {f : R →+* A} (h : Function.Injective f)
9292
(q : ℕ) [hR : ExpChar R q] : ExpChar A q := by
9393
rcases hR with _ | hprime
9494
· haveI := charZero_of_injective_ringHom h; exact .zero
9595
haveI := charP_of_injective_ringHom h q; exact .prime hprime
9696

9797
/-- If `R →+* A` is injective, and `A` is of exponential characteristic `p`, then `R` is also of
9898
exponential characteristic `p`. Similar to `RingHom.charZero`. -/
99-
lemma RingHom.expChar {R A : Type*} [Semiring R] [Semiring A] (f : R →+* A)
99+
lemma RingHom.expChar {R A : Type*} [NonAssocSemiring R] [NonAssocSemiring A] (f : R →+* A)
100100
(H : Function.Injective f) (p : ℕ) [ExpChar A p] : ExpChar R p := by
101101
cases ‹ExpChar A p› with
102102
| zero => haveI := f.charZero; exact .zero
103103
| prime hp => haveI := f.charP H p; exact .prime hp
104104

105105
/-- If `R →+* A` is injective, then `R` is of exponential characteristic `p` if and only if `A` is
106106
also of exponential characteristic `p`. Similar to `RingHom.charZero_iff`. -/
107-
lemma RingHom.expChar_iff {R A : Type*} [Semiring R] [Semiring A] (f : R →+* A)
107+
lemma RingHom.expChar_iff {R A : Type*} [NonAssocSemiring R] [NonAssocSemiring A] (f : R →+* A)
108108
(H : Function.Injective f) (p : ℕ) : ExpChar R p ↔ ExpChar A p :=
109109
fun _ ↦ expChar_of_injective_ringHom H p, fun _ ↦ f.expChar H p⟩
110110

@@ -149,7 +149,7 @@ end QAlgebra
149149
An algebra over a field has the same characteristic as the field.
150150
-/
151151

152-
lemma RingHom.charP_iff_charP {K L : Type*} [DivisionRing K] [Semiring L] [Nontrivial L]
152+
lemma RingHom.charP_iff_charP {K L : Type*} [DivisionRing K] [NonAssocSemiring L] [Nontrivial L]
153153
(f : K →+* L) (p : ℕ) : CharP K p ↔ CharP L p := by
154154
simp only [charP_iff, ← f.injective.eq_iff, map_natCast f, map_zero f]
155155

Mathlib/Algebra/CharP/Defs.lean

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ lemma Nat.cast_ringChar : (ringChar R : R) = 0 := by rw [ringChar.spec]
166166

167167
end ringChar
168168

169-
lemma CharP.neg_one_ne_one [Ring R] (p : ℕ) [CharP R p] [Fact (2 < p)] : (-1 : R) ≠ (1 : R) := by
169+
lemma CharP.neg_one_ne_one [AddGroupWithOne R] (p : ℕ) [CharP R p] [Fact (2 < p)] :
170+
(-1 : R) ≠ (1 : R) := by
170171
rw [ne_comm, ← sub_ne_zero, sub_neg_eq_add, one_add_one_eq_two, ← Nat.cast_two, Ne,
171172
CharP.cast_eq_zero_iff R p 2]
172173
exact fun h ↦ (Fact.out : 2 < p).not_le <| Nat.le_of_dvd Nat.zero_lt_two h

0 commit comments

Comments
 (0)