Skip to content

Commit 3d63bb8

Browse files
committed
fix: remove zero diamond (#72)
1 parent 60afa55 commit 3d63bb8

File tree

4 files changed

+86
-75
lines changed

4 files changed

+86
-75
lines changed

Mathlib/Algebra/Group/Defs.lean

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,24 @@ import Mathlib.Data.Int.Basic -- *only* for notation ℤ which should be in a "p
33
import Mathlib.Tactic.Spread
44

55
/-!
6-
76
# Typeclasses for monoids and groups etc
8-
97
-/
108

11-
/-
12-
13-
## Stuff which was in core Lean 3
14-
15-
-- this should also be in a "prelude" -- it is not in mathlib3's algebra.group.defs
16-
-/
9+
local macro "ofNat_class" Class:ident n:num : command =>
10+
let field := Lean.mkIdent <| Class.getId.eraseMacroScopes.getString!.toLower
11+
`(class $Class:ident.{u} (α : Type u) where
12+
$field:ident : α
1713

18-
class Zero (α : Type u) where
19-
zero : α
14+
instance {α} [$Class α] : OfNat α (nat_lit $n) where
15+
ofNat := ‹$Class α›.1
2016

21-
instance instOfNatZero [Zero α] : OfNat α (nat_lit 0) where
22-
ofNat := Zero.zero
17+
instance {α} [OfNat α (nat_lit $n)] : $Class α where
18+
$field:ident := $n)
2319

24-
class One (α : Type u) where
25-
one : α
20+
example : Nat := 0 -- terminate macro block
2621

27-
instance instOfNatOne [One α] : OfNat α (nat_lit 1) where
28-
ofNat := One.one
22+
ofNat_class Zero 0
23+
ofNat_class One 1
2924

3025
class Inv (α : Type u) where
3126
inv : α → α
@@ -198,13 +193,13 @@ be "the canonical one".
198193
199194
-/
200195

201-
class SubNegMonoid (A : Type u) extends AddMonoid A, Neg A, Sub A :=
202-
(sub := λ a b => a + -b)
203-
(sub_eq_add_neg : ∀ a b : A, a - b = a + -b)
204-
(gsmul : ℤ → A → A := gsmul_rec)
205-
(gsmul_zero' : ∀ (a : A), gsmul 0 a = 0)
206-
(gsmul_succ' (n : ℕ) (a : A) : gsmul (Int.ofNat n.succ) a = a + gsmul (Int.ofNat n) a)
207-
(gsmul_neg' (n : ℕ) (a : A) : gsmul (Int.negSucc n) a = -(gsmul ↑(n.succ) a))
196+
class SubNegMonoid (A : Type u) extends AddMonoid A, Neg A, Sub A where
197+
sub := λ a b => a + -b
198+
sub_eq_add_neg : ∀ a b : A, a - b = a + -b
199+
gsmul : ℤ → A → A := gsmul_rec
200+
gsmul_zero' : ∀ (a : A), gsmul 0 a = 0
201+
gsmul_succ' (n : ℕ) (a : A) : gsmul (Int.ofNat n.succ) a = a + gsmul (Int.ofNat n) a
202+
gsmul_neg' (n : ℕ) (a : A) : gsmul (Int.negSucc n) a = -(gsmul ↑(n.succ) a)
208203

209204
/-
210205

Mathlib/Algebra/GroupWithZero/Defs.lean

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ class MonoidWithZero (M₀ : Type u) extends Monoid M₀, Zero M₀ where
44
zero_mul (a : M₀) : 0 * a = 0
55
mul_zero (a : M₀) : a * 0 = 0
66

7-
class GroupWithZero (G₀ : Type u) extends DivInvMonoid G₀, Zero G₀ where
7+
export MonoidWithZero (zero_mul mul_zero)
8+
attribute [simp] zero_mul mul_zero
9+
10+
class GroupWithZero (G₀ : Type u) extends DivInvMonoid G₀, MonoidWithZero G₀ where
811
exists_pair_ne : ∃ (x y : G₀), x ≠ y
9-
zero_mul (a : G₀) : 0 * a = 0
10-
mul_zero (a : G₀) : a * 0 = 0
1112
inv_zero : (0 : G₀)⁻¹ = 0
1213
mul_inv_cancel (a : G₀) : a ≠ 0 → a * a⁻¹ = 1
1314

14-
instance (G₀ : Type u) [h : GroupWithZero G₀] : MonoidWithZero G₀ :=
15-
{h with }
15+
export GroupWithZero (inv_zero)
16+
attribute [simp] inv_zero

Mathlib/Algebra/Ring/Basic.lean

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,63 @@ class Numeric (α : Type u) where
1313
instance Numeric.OfNat [Numeric α] : OfNat α n := ⟨Numeric.ofNat n⟩
1414
instance [Numeric α] : Coe ℕ α := ⟨Numeric.ofNat⟩
1515

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+
1725
zero_mul (a : R) : 0 * a = 0
1826
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+
1935
mul_add (a b c : R) : a * (b + c) = a * b + a * c
2036
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
2538

2639
section Semiring
2740
variable {R} [Semiring R]
41+
open Numeric
42+
2843
instance : MonoidWithZero R where
2944
__ := ‹Semiring R›
3045

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
3250

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
3452

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
3655

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])
3860

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
4067
induction n with
4168
| zero =>
42-
rw [pow_zero, Nat.pow_zero, Semiring.ofNat_one]
69+
rw [pow_zero, Nat.pow_zero]
4370
exact rfl
4471
| succ n ih =>
45-
rw [pow_succ, Nat.pow_succ, Semiring.ofNat_mul, ih]
72+
rw [pow_succ, Nat.pow_succ, ofNat_mul, ih]
4673

4774
end Semiring
4875

@@ -52,18 +79,17 @@ class CommSemiring (R : Type u) extends Semiring R where
5279
instance (R : Type u) [CommSemiring R] : CommMonoid R where
5380
__ := ‹CommSemiring R›
5481

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
6291

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 }
6793

6894
class CommRing (R : Type u) extends Ring R where
6995
mul_comm (a b : R) : a * b = b * a
@@ -77,23 +103,19 @@ instance (R : Type u) [CommRing R] : CommSemiring R where
77103
namespace Nat
78104

79105
instance : Numeric Nat := ⟨id⟩
106+
80107
@[simp] theorem ofNat_eq_Nat (n : Nat) : Numeric.ofNat n = n := rfl
81108

82109
instance : CommSemiring Nat where
83110
mul_comm := Nat.mul_comm
84111
mul_add := Nat.left_distrib
85112
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
90114
mul_one := Nat.mul_one
91115
one_mul := Nat.one_mul
92116
npow (n x) := x ^ n
93117
npow_zero' := Nat.pow_zero
94118
npow_succ' n x := by simp [Nat.pow_succ, Nat.mul_comm]
95-
one := 1
96-
zero := 0
97119
mul_assoc := Nat.mul_assoc
98120
add_comm := Nat.add_comm
99121
add_assoc := Nat.add_assoc
@@ -111,25 +133,18 @@ namespace Int
111133

112134
instance : Numeric ℤ := ⟨Int.ofNat⟩
113135

114-
@[simp] theorem ofNat_eq_ofNat (n : ℕ): Numeric.ofNat n = ofNat n := rfl
115-
116136
instance : CommRing ℤ where
137+
zero_mul := Int.zero_mul
138+
mul_zero := Int.mul_zero
117139
mul_comm := Int.mul_comm
118140
mul_add := Int.distrib_left
119141
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
124143
mul_one := Int.mul_one
125144
one_mul := Int.one_mul
126145
npow (n x) := HPow.hPow x n
127146
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
133148
mul_assoc := Int.mul_assoc
134149
add_comm := Int.add_comm
135150
add_assoc := Int.add_assoc

Mathlib/Tactic/NormNum.lean

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class LawfulOfNat (α) [Semiring α] (n) [OfNat α n] : Prop where
4141
isNat_ofNat : isNat (OfNat.ofNat n : α) n
4242

4343
instance (α) [Semiring α] : LawfulOfNat α n := ⟨rfl⟩
44-
instance (α) [Semiring α] : LawfulOfNat α (nat_lit 0) := ⟨Semiring.ofNat_zero.symm
45-
instance (α) [Semiring α] : LawfulOfNat α (nat_lit 1) := ⟨Semiring.ofNat_one.symm
44+
instance (α) [Semiring α] : LawfulOfNat α (nat_lit 0) := ⟨rfl
45+
instance (α) [Semiring α] : LawfulOfNat α (nat_lit 1) := ⟨rfl
4646
instance : LawfulOfNat Nat n := ⟨rfl⟩
4747
instance : LawfulOfNat Int n := ⟨rfl⟩
4848

@@ -51,24 +51,24 @@ theorem isNat_rawNat (n : ℕ) : isNat n n := rfl
5151
class LawfulZero (α) [Semiring α] [Zero α] : Prop where
5252
isNat_zero : isNat (Zero.zero : α) (nat_lit 0)
5353

54-
instance (α) [Semiring α] : LawfulZero α := ⟨Semiring.ofNat_zero.symm
54+
instance (α) [Semiring α] : LawfulZero α := ⟨rfl
5555

5656
class LawfulOne (α) [Semiring α] [One α] : Prop where
5757
isNat_one : isNat (One.one : α) (nat_lit 1)
5858

59-
instance (α) [Semiring α] : LawfulOne α := ⟨Semiring.ofNat_one.symm
59+
instance (α) [Semiring α] : LawfulOne α := ⟨rfl
6060

6161
theorem isNat_add {α} [Semiring α] : (a b : α) → (a' b' c : Nat) →
6262
isNat a a' → isNat b b' → Nat.add a' b' = c → isNat (a + b) c
63-
| _, _, _, _, _, rfl, rfl, rfl => (Semiring.ofNat_add _ _).symm
63+
| _, _, _, _, _, rfl, rfl, rfl => ofNat_add.symm
6464

6565
theorem isNat_mul {α} [Semiring α] : (a b : α) → (a' b' c : Nat) →
6666
isNat a a' → isNat b b' → Nat.mul a' b' = c → isNat (a * b) c
67-
| _, _, _, _, _, rfl, rfl, rfl => (Semiring.ofNat_mul _ _).symm
67+
| _, _, _, _, _, rfl, rfl, rfl => ofNat_mul.symm
6868

6969
theorem isNat_pow {α} [Semiring α] : (a : α) → (b a' b' c : Nat) →
7070
isNat a a' → isNat b b' → Nat.pow a' b' = c → isNat (a ^ b) c
71-
| _, _, _, _, _, rfl, rfl, rfl => (Semiring.ofNat_pow _ _).symm
71+
| _, _, _, _, _, rfl, rfl, rfl => (ofNat_pow _ _).symm
7272

7373
def instSemiringNat : Semiring Nat := inferInstance
7474

0 commit comments

Comments
 (0)