@@ -13,36 +13,63 @@ class Numeric (α : Type u) where
13
13
instance Numeric.OfNat [Numeric α] : OfNat α n := ⟨Numeric.ofNat n⟩
14
14
instance [Numeric α] : Coe ℕ α := ⟨Numeric.ofNat⟩
15
15
16
- class Semiring (R : Type u) extends Monoid R, AddCommMonoid R, Numeric R where
16
+ theorem ofNat_eq_ofNat (α) (n : ℕ) [Numeric α] : Numeric.ofNat (α := α) n = OfNat.ofNat n := rfl
17
+
18
+ class Semiring (R : Type u) extends Semigroup R, AddCommSemigroup R, Numeric R where
19
+ add_zero (a : R) : a + 0 = a
20
+ zero_add (a : R) : 0 + a = a
21
+ nsmul : ℕ → R → R := nsmul_rec
22
+ nsmul_zero' : ∀ x, nsmul 0 x = 0 -- fill in with tactic once we can do this
23
+ nsmul_succ' : ∀ (n : ℕ) x, nsmul n.succ x = x + nsmul n x -- fill in with tactic
24
+
17
25
zero_mul (a : R) : 0 * a = 0
18
26
mul_zero (a : R) : a * 0 = 0
27
+
28
+ -- Monoid R
29
+ one_mul (a : R) : 1 * a = a
30
+ mul_one (a : R) : a * 1 = a
31
+ npow : ℕ → R → R := npow_rec
32
+ npow_zero' : ∀ x, npow 0 x = 1 -- fill in with tactic once we can do this
33
+ npow_succ' : ∀ (n : ℕ) x, npow n.succ x = x * npow n x -- fill in with tactic
34
+
19
35
mul_add (a b c : R) : a * (b + c) = a * b + a * c
20
36
add_mul (a b c : R) : (a + b) * c = a * c + b * c
21
- ofNat_add (a b : Nat) : ofNat (a + b) = ofNat a + ofNat b
22
- ofNat_mul (a b : Nat) : ofNat (a * b) = ofNat a * ofNat b
23
- ofNat_one : ofNat (nat_lit 1 ) = One.one
24
- ofNat_zero : ofNat (nat_lit 0 ) = Zero.zero
37
+ ofNat_succ (a : Nat) : ofNat (a + 1 ) = ofNat a + 1
25
38
26
39
section Semiring
27
40
variable {R} [Semiring R]
41
+ open Numeric
42
+
28
43
instance : MonoidWithZero R where
29
44
__ := ‹Semiring R›
30
45
31
- theorem mul_add (a b c : R) : a * (b + c) = a * b + a * c := Semiring.mul_add a b c
46
+ instance : AddCommMonoid R where
47
+ __ := ‹Semiring R›
48
+
49
+ theorem mul_add (a b c : R) : a * (b + c) = a * b + a * c := Semiring.mul_add a b c
32
50
33
- theorem add_mul {R} [Semiring R] (a b c : R) : (a + b) * c = a * c + b * c := Semiring.add_mul a b c
51
+ theorem add_mul (a b c : R) : (a + b) * c = a * c + b * c := Semiring.add_mul a b c
34
52
35
- @[simp] theorem mul_zero {R} [Semiring R] (a : R) : a * 0 = 0 := Semiring.mul_zero a
53
+ @[simp] lemma ofNat_zero : (ofNat 0 : R) = 0 := rfl
54
+ @[simp] lemma ofNat_one : (ofNat 1 : R) = 1 := rfl
36
55
37
- @[simp] theorem zero_mul {R} [Semiring R] (a : R) : 0 * a = 0 := Semiring.zero_mul a
56
+ @[simp] lemma ofNat_add : ∀ {a b}, (ofNat (a + b) : R) = ofNat a + ofNat b
57
+ | a, 0 => (add_zero _).symm
58
+ | a, b + 1 => trans (Semiring.ofNat_succ _)
59
+ (by simp [Semiring.ofNat_succ, ofNat_add (b := b), add_assoc])
38
60
39
- theorem Semiring.ofNat_pow (a n : ℕ) : Numeric.ofNat (a^n) = (Numeric.ofNat a : R)^n := by
61
+ @[simp] lemma ofNat_mul : ∀ {a b}, (ofNat (a * b) : R) = ofNat a * ofNat b
62
+ | a, 0 => by simp
63
+ | a, b + 1 => by simp [Nat.mul_succ, mul_add,
64
+ (show ofNat (a * b) = ofNat a * ofNat b from ofNat_mul)]
65
+
66
+ @[simp] theorem ofNat_pow (a n : ℕ) : Numeric.ofNat (a^n) = (Numeric.ofNat a : R)^n := by
40
67
induction n with
41
68
| zero =>
42
- rw [pow_zero, Nat.pow_zero, Semiring.ofNat_one ]
69
+ rw [pow_zero, Nat.pow_zero]
43
70
exact rfl
44
71
| succ n ih =>
45
- rw [pow_succ, Nat.pow_succ, Semiring. ofNat_mul, ih]
72
+ rw [pow_succ, Nat.pow_succ, ofNat_mul, ih]
46
73
47
74
end Semiring
48
75
@@ -52,18 +79,17 @@ class CommSemiring (R : Type u) extends Semiring R where
52
79
instance (R : Type u) [CommSemiring R] : CommMonoid R where
53
80
__ := ‹CommSemiring R›
54
81
55
- class Ring (R : Type u) extends Monoid R, AddCommGroup R, Numeric R where
56
- mul_add (a b c : R) : a * (b + c) = a * b + a * c
57
- add_mul (a b c : R) : (a + b) * c = a * c + b * c
58
- ofNat_add (a b : Nat) : ofNat (a + b) = ofNat a + ofNat b
59
- ofNat_mul (a b : Nat) : ofNat (a * b) = ofNat a * ofNat b
60
- ofNat_one : ofNat (nat_lit 1 ) = One.one
61
- ofNat_zero : ofNat (nat_lit 0 ) = Zero.zero
82
+ class Ring (R : Type u) extends Semiring R, Neg R, Sub R where
83
+ -- AddGroup R
84
+ sub := λ a b => a + -b
85
+ sub_eq_add_neg : ∀ a b : R, a - b = a + -b
86
+ gsmul : ℤ → R → R := gsmul_rec
87
+ gsmul_zero' : ∀ (a : R), gsmul 0 a = 0
88
+ gsmul_succ' (n : ℕ) (a : R) : gsmul (Int.ofNat n.succ) a = a + gsmul (Int.ofNat n) a
89
+ gsmul_neg' (n : ℕ) (a : R) : gsmul (Int.negSucc n) a = -(gsmul ↑(n.succ) a)
90
+ add_left_neg (a : R) : -a + a = 0
62
91
63
- instance (R : Type u) [Ring R] : Semiring R where
64
- zero_mul := λ a => by rw [← add_right_eq_self (a := 0 * a), ← Ring.add_mul, zero_add]
65
- mul_zero := λ a => by rw [← add_right_eq_self (a := a * 0 ), ← Ring.mul_add]; simp
66
- __ := ‹Ring R›
92
+ instance {R} [Ring R] : AddCommGroup R := { ‹Ring R› with }
67
93
68
94
class CommRing (R : Type u) extends Ring R where
69
95
mul_comm (a b : R) : a * b = b * a
@@ -77,23 +103,19 @@ instance (R : Type u) [CommRing R] : CommSemiring R where
77
103
namespace Nat
78
104
79
105
instance : Numeric Nat := ⟨id⟩
106
+
80
107
@[simp] theorem ofNat_eq_Nat (n : Nat) : Numeric.ofNat n = n := rfl
81
108
82
109
instance : CommSemiring Nat where
83
110
mul_comm := Nat.mul_comm
84
111
mul_add := Nat.left_distrib
85
112
add_mul := Nat.right_distrib
86
- ofNat_add := by simp
87
- ofNat_mul := by simp
88
- ofNat_one := rfl
89
- ofNat_zero := rfl
113
+ ofNat_succ := fun _ => rfl
90
114
mul_one := Nat.mul_one
91
115
one_mul := Nat.one_mul
92
116
npow (n x) := x ^ n
93
117
npow_zero' := Nat.pow_zero
94
118
npow_succ' n x := by simp [Nat.pow_succ, Nat.mul_comm]
95
- one := 1
96
- zero := 0
97
119
mul_assoc := Nat.mul_assoc
98
120
add_comm := Nat.add_comm
99
121
add_assoc := Nat.add_assoc
@@ -111,25 +133,18 @@ namespace Int
111
133
112
134
instance : Numeric ℤ := ⟨Int.ofNat⟩
113
135
114
- @[simp] theorem ofNat_eq_ofNat (n : ℕ): Numeric.ofNat n = ofNat n := rfl
115
-
116
136
instance : CommRing ℤ where
137
+ zero_mul := Int.zero_mul
138
+ mul_zero := Int.mul_zero
117
139
mul_comm := Int.mul_comm
118
140
mul_add := Int.distrib_left
119
141
add_mul := Int.distrib_right
120
- ofNat_add := by simp [ofNat_add]
121
- ofNat_mul := by simp [ofNat_mul]
122
- ofNat_one := rfl
123
- ofNat_zero := rfl
142
+ ofNat_succ := fun _ => rfl
124
143
mul_one := Int.mul_one
125
144
one_mul := Int.one_mul
126
145
npow (n x) := HPow.hPow x n
127
146
npow_zero' n := rfl
128
- npow_succ' n x := by
129
- rw [Int.mul_comm]
130
- exact rfl
131
- one := 1
132
- zero := 0
147
+ npow_succ' n x := by rw [Int.mul_comm]; rfl
133
148
mul_assoc := Int.mul_assoc
134
149
add_comm := Int.add_comm
135
150
add_assoc := Int.add_assoc
0 commit comments