@@ -30,39 +30,51 @@ theorem ofNat_eq_iff_of_lt {x y : Nat} (h₁ : x < p) (h₂ : y < p) :
3030
3131namespace Lean.Grind
3232
33- class CommRing (α : Type u) extends Add α, Mul α, Neg α, Sub α, HPow α Nat α where
33+ class Semiring (α : Type u) extends Add α, Mul α, HPow α Nat α where
3434 [ofNat : ∀ n, OfNat α n]
3535 [natCast : NatCast α]
36- [intCast : IntCast α]
3736 add_assoc : ∀ a b c : α, a + b + c = a + (b + c)
3837 add_comm : ∀ a b : α, a + b = b + a
3938 add_zero : ∀ a : α, a + 0 = a
40- neg_add_cancel : ∀ a : α, -a + a = 0
4139 mul_assoc : ∀ a b c : α, a * b * c = a * (b * c)
42- mul_comm : ∀ a b : α, a * b = b * a
4340 mul_one : ∀ a : α, a * 1 = a
41+ one_mul : ∀ a : α, 1 * a = a
4442 left_distrib : ∀ a b c : α, a * (b + c) = a * b + a * c
43+ right_distrib : ∀ a b c : α, (a + b) * c = a * c + b * c
4544 zero_mul : ∀ a : α, 0 * a = 0
46- sub_eq_add_neg : ∀ a b : α, a - b = a + -b
45+ mul_zero : ∀ a : α, a * 0 = 0
4746 pow_zero : ∀ a : α, a ^ 0 = 1
4847 pow_succ : ∀ a : α, ∀ n : Nat, a ^ (n + 1 ) = (a ^ n) * a
4948 ofNat_succ : ∀ a : Nat, OfNat.ofNat (α := α) (a + 1 ) = OfNat.ofNat a + 1 := by intros; rfl
5049 ofNat_eq_natCast : ∀ n : Nat, OfNat.ofNat (α := α) n = Nat.cast n := by intros; rfl
50+
51+ class Ring (α : Type u) extends Semiring α, Neg α, Sub α where
52+ [intCast : IntCast α]
53+ neg_add_cancel : ∀ a : α, -a + a = 0
54+ sub_eq_add_neg : ∀ a b : α, a - b = a + -b
5155 intCast_ofNat : ∀ n : Nat, Int.cast (OfNat.ofNat (α := Int) n) = OfNat.ofNat (α := α) n := by intros; rfl
5256 intCast_neg : ∀ i : Int, Int.cast (R := α) (-i) = -Int.cast i := by intros; rfl
5357
58+ class CommSemiring (α : Type u) extends Semiring α where
59+ mul_comm : ∀ a b : α, a * b = b * a
60+ one_mul := by intro a; rw [mul_comm, mul_one]
61+ mul_zero := by intro a; rw [mul_comm, zero_mul]
62+ right_distrib := by intro a b c; rw [mul_comm, left_distrib, mul_comm c, mul_comm c]
63+
64+ class CommRing (α : Type u) extends Ring α, CommSemiring α
65+
5466-- We reduce the priority of these parent instances,
5567-- so that in downstream libraries with their own `CommRing` class,
5668-- the path `CommRing -> Add` is found before `CommRing -> Lean.Grind.CommRing -> Add`.
5769-- (And similarly for the other parents.)
58- attribute [instance 100] CommRing .toAdd CommRing .toMul CommRing.toNeg CommRing.toSub CommRing.toHPow
70+ attribute [instance 100] Semiring .toAdd Semiring .toMul Semiring.toHPow Ring.toNeg Ring.toSub
5971
6072-- This is a low-priority instance, to avoid conflicts with existing `OfNat`, `NatCast`, and `IntCast` instances.
61- attribute [instance 100] CommRing .ofNat CommRing .natCast CommRing .intCast
73+ attribute [instance 100] Semiring .ofNat Semiring .natCast Ring .intCast
6274
63- namespace CommRing
75+ namespace Semiring
6476
65- variable {α : Type u} [CommRing α]
77+ variable {α : Type u} [Semiring α]
6678
6779theorem natCast_zero : ((0 : Nat) : α) = 0 := (ofNat_eq_natCast 0 ).symm
6880theorem natCast_one : ((1 : Nat) : α) = 1 := (ofNat_eq_natCast 1 ).symm
@@ -80,24 +92,9 @@ theorem natCast_succ (n : Nat) : ((n + 1 : Nat) : α) = ((n : α) + 1) := by
8092theorem zero_add (a : α) : 0 + a = a := by
8193 rw [add_comm, add_zero]
8294
83- theorem add_neg_cancel (a : α) : a + -a = 0 := by
84- rw [add_comm, neg_add_cancel]
85-
8695theorem add_left_comm (a b c : α) : a + (b + c) = b + (a + c) := by
8796 rw [← add_assoc, ← add_assoc, add_comm a]
8897
89- theorem one_mul (a : α) : 1 * a = a := by
90- rw [mul_comm, mul_one]
91-
92- theorem right_distrib (a b c : α) : (a + b) * c = a * c + b * c := by
93- rw [mul_comm, left_distrib, mul_comm c, mul_comm c]
94-
95- theorem mul_zero (a : α) : a * 0 = 0 := by
96- rw [mul_comm, zero_mul]
97-
98- theorem mul_left_comm (a b c : α) : a * (b * c) = b * (a * c) := by
99- rw [← mul_assoc, ← mul_assoc, mul_comm a]
100-
10198theorem ofNat_mul (a b : Nat) : OfNat.ofNat (α := α) (a * b) = OfNat.ofNat a * OfNat.ofNat b := by
10299 induction b with
103100 | zero => simp [Nat.mul_zero, mul_zero]
@@ -106,6 +103,22 @@ theorem ofNat_mul (a b : Nat) : OfNat.ofNat (α := α) (a * b) = OfNat.ofNat a *
106103theorem natCast_mul (a b : Nat) : ((a * b : Nat) : α) = ((a : α) * (b : α)) := by
107104 rw [← ofNat_eq_natCast, ofNat_mul, ofNat_eq_natCast, ofNat_eq_natCast]
108105
106+ theorem pow_add (a : α) (k₁ k₂ : Nat) : a ^ (k₁ + k₂) = a^k₁ * a^k₂ := by
107+ induction k₂
108+ next => simp [pow_zero, mul_one]
109+ next k₂ ih => rw [Nat.add_succ, pow_succ, pow_succ, ih, mul_assoc]
110+
111+ end Semiring
112+
113+ namespace Ring
114+
115+ open Semiring
116+
117+ variable {α : Type u} [Ring α]
118+
119+ theorem add_neg_cancel (a : α) : a + -a = 0 := by
120+ rw [add_comm, neg_add_cancel]
121+
109122theorem add_left_inj {a b : α} (c : α) : a + c = b + c ↔ a = b :=
110123 ⟨fun h => by simpa [add_assoc, add_neg_cancel, add_zero] using (congrArg (· + -c) h),
111124 fun g => congrArg (· + c) g⟩
@@ -198,11 +211,17 @@ theorem neg_eq_neg_one_mul (a : α) : -a = (-1) * a := by
198211 rw [← right_distrib, ← intCast_neg_one, ← intCast_one (α := α)]
199212 simp [← intCast_add, intCast_zero, zero_mul]
200213
214+ theorem neg_eq_mul_neg_one (a : α) : -a = a * (-1 ) := by
215+ rw [← add_left_inj a, neg_add_cancel]
216+ conv => rhs; arg 2 ; rw [← mul_one a]
217+ rw [← left_distrib, ← intCast_neg_one, ← intCast_one (α := α)]
218+ simp [← intCast_add, intCast_zero, mul_zero]
219+
201220theorem neg_mul (a b : α) : (-a) * b = -(a * b) := by
202221 rw [neg_eq_neg_one_mul a, neg_eq_neg_one_mul (a * b), mul_assoc]
203222
204223theorem mul_neg (a b : α) : a * (-b) = -(a * b) := by
205- rw [mul_comm, neg_mul, mul_comm ]
224+ rw [neg_eq_mul_neg_one b, neg_eq_mul_neg_one (a * b), mul_assoc ]
206225
207226theorem intCast_nat_mul (x y : Nat) : ((x * y : Int) : α) = ((x : α) * (y : α)) := by
208227 rw [Int.ofNat_mul_ofNat, intCast_natCast, natCast_mul]
@@ -224,21 +243,27 @@ theorem intCast_pow (x : Int) (k : Nat) : ((x ^ k : Int) : α) = (x : α) ^ k :=
224243 next => simp [pow_zero, Int.pow_zero, intCast_one]
225244 next k ih => simp [pow_succ, Int.pow_succ, intCast_mul, *]
226245
227- theorem pow_add (a : α) (k₁ k₂ : Nat) : a ^ (k₁ + k₂) = a^k₁ * a^k₂ := by
228- induction k₂
229- next => simp [pow_zero, mul_one]
230- next k₂ ih => rw [Nat.add_succ, pow_succ, pow_succ, ih, mul_assoc]
246+ end Ring
247+
248+ namespace CommSemiring
249+
250+ open Semiring
251+
252+ variable {α : Type u} [CommSemiring α]
253+
254+ theorem mul_left_comm (a b c : α) : a * (b * c) = b * (a * c) := by
255+ rw [← mul_assoc, ← mul_assoc, mul_comm a]
231256
232- end CommRing
257+ end CommSemiring
233258
234- open CommRing
259+ open Semiring Ring CommSemiring CommRing
235260
236- class IsCharP (α : Type u) [CommRing α] (p : outParam Nat) where
261+ class IsCharP (α : Type u) [Ring α] (p : outParam Nat) where
237262 ofNat_eq_zero_iff (p) : ∀ (x : Nat), OfNat.ofNat (α := α) x = 0 ↔ x % p = 0
238263
239264namespace IsCharP
240265
241- variable (p) {α : Type u} [CommRing α] [IsCharP α p]
266+ variable (p) {α : Type u} [Ring α] [IsCharP α p]
242267
243268theorem natCast_eq_zero_iff (x : Nat) : (x : α) = 0 ↔ x % p = 0 := by
244269 rw [← ofNat_eq_natCast]
@@ -325,12 +350,12 @@ end IsCharP
325350/--
326351Special case of Mathlib's `NoZeroSMulDivisors Nat α`.
327352-/
328- class NoNatZeroDivisors (α : Type u) [CommRing α] where
353+ class NoNatZeroDivisors (α : Type u) [Ring α] where
329354 no_nat_zero_divisors : ∀ (k : Nat) (a : α), k ≠ 0 → OfNat.ofNat (α := α) k * a = 0 → a = 0
330355
331356export NoNatZeroDivisors (no_nat_zero_divisors)
332357
333- theorem no_int_zero_divisors {α : Type u} [CommRing α] [NoNatZeroDivisors α] {k : Int} {a : α}
358+ theorem no_int_zero_divisors {α : Type u} [Ring α] [NoNatZeroDivisors α] {k : Int} {a : α}
334359 : k ≠ 0 → k * a = 0 → a = 0 := by
335360 match k with
336361 | (k : Nat) =>
0 commit comments