@@ -2,44 +2,45 @@ import Mathlib.Init.Data.Int.Basic
2
2
import Mathlib.Algebra.GroupWithZero.Defs
3
3
import Mathlib.Algebra.Group.Basic
4
4
import Mathlib.Tactic.Spread
5
- /-
6
-
7
- # Semirings and rings
5
+ import Mathlib.Util.WhatsNew
8
6
9
- -/
7
+ @[reducible, inline]
8
+ protected def Nat.cast [Add α] [Zero α] [One α] : Nat → α
9
+ | 0 => 0
10
+ | 1 => 1
11
+ | n + 1 => Nat.cast n + 1
10
12
11
- class Numeric (α : Type u) where
12
- ofNat : Nat → α
13
+ @[simp] lemma Nat.cast_zero [Add α] [Zero α] [One α] : Nat.cast 0 = (0 : α) := rfl
14
+ @[simp] lemma Nat.cast_one [Add α] [Zero α] [One α] : Nat.cast 1 = (1 : α) := rfl
15
+ lemma Nat.cast_succ_succ [Add α] [Zero α] [One α] : Nat.cast (n+2 ) = ((n + 1 ).cast + 1 : α) := rfl
13
16
14
- instance Numeric.OfNat [Numeric α] : OfNat α n := ⟨Numeric.ofNat n⟩
15
- instance [Numeric α] : CoeTail ℕ α := ⟨Numeric.ofNat⟩
17
+ @[simp]
18
+ lemma Nat.cast_Nat : ∀ {n : Nat}, n.cast = n
19
+ | 0 => rfl
20
+ | 1 => rfl
21
+ | n + 2 => by simp [Nat.cast, cast_Nat]; rfl
16
22
17
- theorem ofNat_eq_ofNat (α) (n : ℕ) [Numeric α] : Numeric.ofNat (α := α) n = OfNat.ofNat n := rfl
23
+ @[simp]
24
+ lemma Nat.cast_Int : ∀ {n : Nat}, n.cast = (n : Int)
25
+ | 0 => rfl
26
+ | 1 => rfl
27
+ | n + 2 => by simp [Nat.cast, cast_Int]; rfl
18
28
19
- class Semiring (R : Type u) extends Semigroup R, AddCommSemigroup R, Numeric R where
20
- add_zero (a : R) : a + 0 = a
21
- zero_add (a : R) : 0 + a = a
22
- nsmul : ℕ → R → R := nsmul_rec
23
- nsmul_zero' : ∀ x, nsmul 0 x = 0 -- fill in with tactic once we can do this
24
- nsmul_succ' : ∀ (n : ℕ) x, nsmul n.succ x = x + nsmul n x -- fill in with tactic
29
+ class HasNumerals (α : Type u) extends Add α, Zero α, One α
25
30
26
- zero_mul (a : R) : 0 * a = 0
27
- mul_zero (a : R) : a * 0 = 0
31
+ instance (priority := low) [HasNumerals α] : OfNat α n where
32
+ ofNat := n.cast
28
33
29
- -- Monoid R
30
- one_mul (a : R) : 1 * a = a
31
- mul_one (a : R) : a * 1 = a
32
- npow : ℕ → R → R := npow_rec
33
- npow_zero' : ∀ x, npow 0 x = 1 -- fill in with tactic once we can do this
34
- npow_succ' : ∀ (n : ℕ) x, npow n.succ x = x * npow n x -- fill in with tactic
34
+ /-
35
+ # Semirings and rings
36
+ -/
35
37
38
+ class Semiring (R : Type u) extends AddCommMonoid R, MonoidWithZero R, HasNumerals R where
36
39
mul_add (a b c : R) : a * (b + c) = a * b + a * c
37
40
add_mul (a b c : R) : (a + b) * c = a * c + b * c
38
- ofNat_succ (a : Nat) : ofNat (a + 1 ) = ofNat a + 1
39
41
40
42
section Semiring
41
43
variable {R} [Semiring R]
42
- open Numeric
43
44
44
45
instance : MonoidWithZero R where
45
46
__ := ‹Semiring R›
@@ -51,31 +52,35 @@ theorem mul_add (a b c : R) : a * (b + c) = a * b + a * c := Semiring.mul_add a
51
52
52
53
theorem add_mul (a b c : R) : (a + b) * c = a * c + b * c := Semiring.add_mul a b c
53
54
54
- @[simp] lemma ofNat_zero : (ofNat 0 : R ) = 0 := rfl
55
- @[simp] lemma ofNat_one : (ofNat 1 : R) = 1 := rfl
55
+ lemma Nat.cast_succ {R} [Semiring R] {n : ℕ} : Nat.cast (n + 1 ) = (Nat.cast n + 1 : R) := by
56
+ cases n <;> simp [Nat.cast_succ_succ]
56
57
57
- @[simp] lemma ofNat_add : ∀ {a b}, (ofNat (a + b) : R) = ofNat a + ofNat b
58
- | a, 0 => (add_zero _).symm
59
- | a, b + 1 => trans (Semiring.ofNat_succ _)
60
- (by simp [Semiring.ofNat_succ, ofNat_add (b := b), add_assoc])
58
+ lemma Nat.cast_succ' {R} [Semiring R] {n : ℕ} : Nat.cast n.succ = (Nat.cast n + 1 : R) :=
59
+ Nat.cast_succ
61
60
62
- @[simp] lemma ofNat_mul : ∀ {a b}, (ofNat (a * b) : R) = ofNat a * ofNat b
63
- | a, 0 => by simp
64
- | a, b + 1 => by simp [Nat.mul_succ, mul_add,
65
- (show ofNat (a * b) = ofNat a * ofNat b from ofNat_mul)]
61
+ lemma Nat.cast_add {R} [Semiring R] {m n : ℕ} : (m + n).cast = (m.cast + n.cast : R) := by
62
+ induction n generalizing m
63
+ case zero => simp
64
+ case succ n ih =>
65
+ show Nat.cast ((m + n) + 1 ) = _ + Nat.cast (n + 1 )
66
+ simp [Nat.cast_succ, ih, add_assoc]
66
67
67
- @[simp] theorem ofNat_pow (a n : ℕ) : Numeric.ofNat (a^n) = (Numeric.ofNat a : R)^n := by
68
- induction n with
69
- | zero =>
70
- rw [pow_zero, Nat.pow_zero]
71
- exact rfl
72
- | succ n ih =>
73
- rw [pow_succ, Nat.pow_succ, ofNat_mul, ih]
68
+ lemma Nat.cast_mul {R} [Semiring R] {m n : ℕ} : (m * n).cast = (m.cast * n.cast : R) := by
69
+ induction n generalizing m <;> simp_all [mul_succ, cast_add, cast_succ', mul_add]
70
+
71
+ lemma Nat.pow_succ' {m n : Nat} : m ^ n.succ = m * m ^ n := by
72
+ rw [Nat.pow_succ, Nat.mul_comm]
73
+
74
+ lemma Nat.cast_pow {R} [Semiring R] {m n : ℕ} : (m ^ n).cast = (m.cast ^ n : R) := by
75
+ induction n generalizing m <;>
76
+ simp_all [cast_mul, cast_add, cast_succ', Nat.pow_succ', _root_.pow_succ', pow_zero]
74
77
75
78
end Semiring
76
79
77
80
class CommSemiring (R : Type u) extends Semiring R where
78
81
mul_comm (a b : R) : a * b = b * a
82
+ -- TODO: doesn't work
83
+ add_mul a b c := (by rw [mul_comm, mul_add, mul_comm c, mul_comm c])
79
84
80
85
instance (R : Type u) [CommSemiring R] : CommMonoid R where
81
86
__ := ‹CommSemiring R›
@@ -92,6 +97,11 @@ class Ring (R : Type u) extends Semiring R, Neg R, Sub R where
92
97
93
98
instance {R} [Ring R] : AddCommGroup R := { ‹Ring R› with }
94
99
100
+ theorem neg_mul_eq_neg_mul {R} [Ring R] (a b : R) : -(a * b) = (-a) * b :=
101
+ Eq.symm <| eq_of_sub_eq_zero' <| by
102
+ rw [sub_eq_add_neg, neg_neg (a * b) /- TODO: why is arg necessary? -/ ]
103
+ rw [← add_mul, neg_add_self a /- TODO: why is arg necessary? -/ , zero_mul]
104
+
95
105
class CommRing (R : Type u) extends Ring R where
96
106
mul_comm (a b : R) : a * b = b * a
97
107
@@ -103,15 +113,10 @@ instance (R : Type u) [CommRing R] : CommSemiring R where
103
113
104
114
namespace Nat
105
115
106
- instance : Numeric Nat := ⟨id⟩
107
-
108
- @[simp] theorem ofNat_eq_Nat (n : Nat) : Numeric.ofNat n = n := rfl
109
-
110
116
instance : CommSemiring ℕ where
111
117
mul_comm := Nat.mul_comm
112
118
mul_add := Nat.left_distrib
113
119
add_mul := Nat.right_distrib
114
- ofNat_succ := fun _ => rfl
115
120
mul_one := Nat.mul_one
116
121
one_mul := Nat.one_mul
117
122
npow (n x) := x ^ n
@@ -132,15 +137,12 @@ end Nat
132
137
133
138
namespace Int
134
139
135
- instance : Numeric ℤ := ⟨Int.ofNat⟩
136
-
137
140
instance : CommRing ℤ where
138
141
zero_mul := Int.zero_mul
139
142
mul_zero := Int.mul_zero
140
143
mul_comm := Int.mul_comm
141
144
mul_add := Int.distrib_left
142
145
add_mul := Int.distrib_right
143
- ofNat_succ := fun _ => rfl
144
146
mul_one := Int.mul_one
145
147
one_mul := Int.one_mul
146
148
npow (n x) := HPow.hPow x n
0 commit comments