-
Notifications
You must be signed in to change notification settings - Fork 257
/
Defs.lean
398 lines (322 loc) · 13.2 KB
/
Defs.lean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Johan Commelin
-/
import Mathlib.Algebra.GroupWithZero.Defs
import Mathlib.Data.Nat.Cast.Defs
import Mathlib.Data.Option.Defs
import Mathlib.Data.Option.NAry
import Mathlib.Logic.Nontrivial.Basic
import Mathlib.Tactic.Common
#align_import algebra.group.with_one.defs from "leanprover-community/mathlib"@"995b47e555f1b6297c7cf16855f1023e355219fb"
/-!
# Adjoining a zero/one to semigroups and related algebraic structures
This file contains different results about adjoining an element to an algebraic structure which then
behaves like a zero or a one. An example is adjoining a one to a semigroup to obtain a monoid. That
this provides an example of an adjunction is proved in `Algebra.Category.MonCat.Adjunctions`.
Another result says that adjoining to a group an element `zero` gives a `GroupWithZero`. For more
information about these structures (which are not that standard in informal mathematics, see
`Algebra.GroupWithZero.Basic`)
## Porting notes
In Lean 3, we use `id` here and there to get correct types of proofs. This is required because
`WithOne` and `WithZero` are marked as `Irreducible` at the end of
`Mathlib.Algebra.Group.WithOne.Defs`, so proofs that use `Option α` instead of `WithOne α` no
longer typecheck. In Lean 4, both types are plain `def`s, so we don't need these `id`s.
## TODO
`WithOne.coe_mul` and `WithZero.coe_mul` have inconsistent use of implicit parameters
-/
universe u v w
variable {α : Type u} {β : Type v} {γ : Type w}
/-- Add an extra element `1` to a type -/
@[to_additive "Add an extra element `0` to a type"]
def WithOne (α) :=
Option α
#align with_one WithOne
#align with_zero WithZero
namespace WithOne
instance [Repr α] : Repr (WithZero α) :=
⟨fun o _ =>
match o with
| none => "0"
| some a => "↑" ++ repr a⟩
@[to_additive]
instance [Repr α] : Repr (WithOne α) :=
⟨fun o _ =>
match o with
| none => "1"
| some a => "↑" ++ repr a⟩
@[to_additive]
instance monad : Monad WithOne :=
instMonadOption
@[to_additive]
instance one : One (WithOne α) :=
⟨none⟩
#align with_one.has_one WithOne.one
#align with_zero.has_zero WithZero.zero
@[to_additive]
instance mul [Mul α] : Mul (WithOne α) :=
⟨Option.liftOrGet (· * ·)⟩
#align with_one.has_mul WithOne.mul
#align with_zero.has_add WithZero.add
@[to_additive]
instance inv [Inv α] : Inv (WithOne α) :=
⟨fun a => Option.map Inv.inv a⟩
#align with_one.has_inv WithOne.inv
#align with_zero.has_neg WithZero.neg
@[to_additive]
instance invOneClass [Inv α] : InvOneClass (WithOne α) :=
{ WithOne.one, WithOne.inv with inv_one := rfl }
@[to_additive]
instance inhabited : Inhabited (WithOne α) :=
⟨1⟩
@[to_additive]
instance nontrivial [Nonempty α] : Nontrivial (WithOne α) :=
Option.nontrivial
-- Porting note: this new declaration is here to make `((a : α): WithOne α)` have type `WithOne α`;
-- otherwise the coercion kicks in and it becomes `Option.some a : WithOne α` which
-- becomes `Option.some a : Option α`.
/-- The canonical map from `α` into `WithOne α` -/
@[to_additive (attr := coe) "The canonical map from `α` into `WithZero α`"]
def coe : α → WithOne α :=
Option.some
@[to_additive]
instance coeTC : CoeTC α (WithOne α) :=
⟨coe⟩
/-- Recursor for `WithOne` using the preferred forms `1` and `↑a`. -/
@[to_additive (attr := elab_as_elim)
"Recursor for `WithZero` using the preferred forms `0` and `↑a`."]
def recOneCoe {C : WithOne α → Sort*} (h₁ : C 1) (h₂ : ∀ a : α, C a) : ∀ n : WithOne α, C n
| Option.none => h₁
| Option.some x => h₂ x
#align with_one.rec_one_coe WithOne.recOneCoe
#align with_zero.rec_zero_coe WithZero.recZeroCoe
-- Porting note: in Lean 3 the to-additivised declaration
-- would automatically get this; right now in Lean 4...I don't
-- know if it does or not, and I don't know how to check, so
-- I'll add it manually just to be sure.
attribute [elab_as_elim] WithZero.recZeroCoe
/-- Deconstruct an `x : WithOne α` to the underlying value in `α`, given a proof that `x ≠ 1`. -/
@[to_additive unzero
"Deconstruct an `x : WithZero α` to the underlying value in `α`, given a proof that `x ≠ 0`."]
def unone : ∀ {x : WithOne α}, x ≠ 1 → α | (x : α), _ => x
#align with_one.unone WithOne.unone
#align with_zero.unzero WithZero.unzero
@[to_additive (attr := simp) unzero_coe]
theorem unone_coe {x : α} (hx : (x : WithOne α) ≠ 1) : unone hx = x :=
rfl
#align with_one.unone_coe WithOne.unone_coe
#align with_zero.unzero_coe WithZero.unzero_coe
@[to_additive (attr := simp) coe_unzero]
lemma coe_unone : ∀ {x : WithOne α} (hx : x ≠ 1), unone hx = x
| (x : α), _ => rfl
#align with_one.coe_unone WithOne.coe_unone
#align with_zero.coe_unzero WithZero.coe_unzero
-- Porting note: in Lean 4 the `some_eq_coe` lemmas present in the lean 3 version
-- of this file are syntactic tautologies
#noalign with_one.some_eq_coe
#noalign with_zero.some_eq_coe
@[to_additive (attr := simp)]
theorem coe_ne_one {a : α} : (a : WithOne α) ≠ (1 : WithOne α) :=
Option.some_ne_none a
#align with_one.coe_ne_one WithOne.coe_ne_one
#align with_zero.coe_ne_zero WithZero.coe_ne_zero
@[to_additive (attr := simp)]
theorem one_ne_coe {a : α} : (1 : WithOne α) ≠ a :=
coe_ne_one.symm
#align with_one.one_ne_coe WithOne.one_ne_coe
#align with_zero.zero_ne_coe WithZero.zero_ne_coe
@[to_additive]
theorem ne_one_iff_exists {x : WithOne α} : x ≠ 1 ↔ ∃ a : α, ↑a = x :=
Option.ne_none_iff_exists
#align with_one.ne_one_iff_exists WithOne.ne_one_iff_exists
#align with_zero.ne_zero_iff_exists WithZero.ne_zero_iff_exists
@[to_additive]
instance canLift : CanLift (WithOne α) α (↑) fun a => a ≠ 1 where
prf _ := ne_one_iff_exists.1
#align with_one.can_lift WithOne.canLift
#align with_zero.can_lift WithZero.canLift
@[to_additive (attr := simp, norm_cast)]
theorem coe_inj {a b : α} : (a : WithOne α) = b ↔ a = b :=
Option.some_inj
#align with_one.coe_inj WithOne.coe_inj
#align with_zero.coe_inj WithZero.coe_inj
@[to_additive (attr := elab_as_elim)]
protected theorem cases_on {P : WithOne α → Prop} : ∀ x : WithOne α, P 1 → (∀ a : α, P a) → P x :=
Option.casesOn
#align with_one.cases_on WithOne.cases_on
#align with_zero.cases_on WithZero.cases_on
-- Porting note: I don't know if `elab_as_elim` is being added to the additivised declaration.
attribute [elab_as_elim] WithZero.cases_on
@[to_additive]
instance mulOneClass [Mul α] : MulOneClass (WithOne α) where
mul := (· * ·)
one := 1
one_mul := (Option.liftOrGet_isId _).left_id
mul_one := (Option.liftOrGet_isId _).right_id
@[to_additive (attr := simp, norm_cast)]
lemma coe_mul [Mul α] (a b : α) : (↑(a * b) : WithOne α) = a * b := rfl
#align with_one.coe_mul WithOne.coe_mul
#align with_zero.coe_add WithZero.coe_add
@[to_additive]
instance monoid [Semigroup α] : Monoid (WithOne α) where
__ := mulOneClass
mul_assoc a b c := match a, b, c with
| 1, b, c => by simp
| (a : α), 1, c => by simp
| (a : α), (b : α), 1 => by simp
| (a : α), (b : α), (c : α) => by simp_rw [← coe_mul, mul_assoc]
@[to_additive]
instance commMonoid [CommSemigroup α] : CommMonoid (WithOne α) where
mul_comm := fun a b => match a, b with
| (a : α), (b : α) => congr_arg some (mul_comm a b)
| (_ : α), 1 => rfl
| 1, (_ : α) => rfl
| 1, 1 => rfl
@[to_additive (attr := simp, norm_cast)]
theorem coe_inv [Inv α] (a : α) : ((a⁻¹ : α) : WithOne α) = (a : WithOne α)⁻¹ :=
rfl
#align with_one.coe_inv WithOne.coe_inv
#align with_zero.coe_neg WithZero.coe_neg
end WithOne
namespace WithZero
instance one [one : One α] : One (WithZero α) :=
{ one with }
@[simp, norm_cast]
theorem coe_one [One α] : ((1 : α) : WithZero α) = 1 :=
rfl
#align with_zero.coe_one WithZero.coe_one
instance mulZeroClass [Mul α] : MulZeroClass (WithZero α) :=
{ WithZero.zero with
mul := Option.map₂ (· * ·),
zero_mul := Option.map₂_none_left (· * ·),
mul_zero := Option.map₂_none_right (· * ·) }
@[simp, norm_cast]
theorem coe_mul {α : Type u} [Mul α] {a b : α} : ((a * b : α) : WithZero α) = a * b :=
rfl
#align with_zero.coe_mul WithZero.coe_mul
instance noZeroDivisors [Mul α] : NoZeroDivisors (WithZero α) :=
⟨Option.map₂_eq_none_iff.1⟩
instance semigroupWithZero [Semigroup α] : SemigroupWithZero (WithZero α) :=
{ WithZero.mulZeroClass with
mul_assoc := fun _ _ _ => Option.map₂_assoc mul_assoc }
instance commSemigroup [CommSemigroup α] : CommSemigroup (WithZero α) :=
{ WithZero.semigroupWithZero with
mul_comm := fun _ _ => Option.map₂_comm mul_comm }
instance mulZeroOneClass [MulOneClass α] : MulZeroOneClass (WithZero α) :=
{ WithZero.mulZeroClass, WithZero.one with
one_mul := Option.map₂_left_identity one_mul,
mul_one := Option.map₂_right_identity mul_one }
instance pow [One α] [Pow α ℕ] : Pow (WithZero α) ℕ :=
⟨fun x n =>
match x, n with
| none, 0 => 1
| none, _ + 1 => 0
| some x, n => ↑(x ^ n)⟩
@[simp, norm_cast]
theorem coe_pow [One α] [Pow α ℕ] {a : α} (n : ℕ) :
↑(a ^ n : α) = ((a : WithZero α) ^ n : WithZero α) :=
rfl
#align with_zero.coe_pow WithZero.coe_pow
instance monoidWithZero [Monoid α] : MonoidWithZero (WithZero α) :=
{ WithZero.mulZeroOneClass, WithZero.semigroupWithZero with
npow := fun n x => x ^ n,
npow_zero := fun x =>
match x with
| none => rfl
| some x => congr_arg some <| pow_zero x,
npow_succ := fun n x =>
match x with
| none => by
change 0 ^ (n + 1) = 0 ^ n * 0
simp only [mul_zero]; rfl
| some x => congr_arg some <| pow_succ x n }
instance commMonoidWithZero [CommMonoid α] : CommMonoidWithZero (WithZero α) :=
{ WithZero.monoidWithZero, WithZero.commSemigroup with }
/-- Given an inverse operation on `α` there is an inverse operation
on `WithZero α` sending `0` to `0`. -/
instance inv [Inv α] : Inv (WithZero α) :=
⟨fun a => Option.map Inv.inv a⟩
@[simp, norm_cast]
theorem coe_inv [Inv α] (a : α) : ((a⁻¹ : α) : WithZero α) = (↑a)⁻¹ :=
rfl
#align with_zero.coe_inv WithZero.coe_inv
@[simp]
theorem inv_zero [Inv α] : (0 : WithZero α)⁻¹ = 0 :=
rfl
#align with_zero.inv_zero WithZero.inv_zero
instance invOneClass [InvOneClass α] : InvOneClass (WithZero α) :=
{ WithZero.one, WithZero.inv with inv_one := show ((1⁻¹ : α) : WithZero α) = 1 by simp }
instance div [Div α] : Div (WithZero α) :=
⟨Option.map₂ (· / ·)⟩
@[norm_cast]
theorem coe_div [Div α] (a b : α) : ↑(a / b : α) = (a / b : WithZero α) :=
rfl
#align with_zero.coe_div WithZero.coe_div
instance [One α] [Pow α ℤ] : Pow (WithZero α) ℤ :=
⟨fun x n =>
match x, n with
| none, Int.ofNat 0 => 1
| none, Int.ofNat (Nat.succ _) => 0
| none, Int.negSucc _ => 0
| some x, n => ↑(x ^ n)⟩
@[simp, norm_cast]
theorem coe_zpow [DivInvMonoid α] {a : α} (n : ℤ) : ↑(a ^ n) = (↑a : WithZero α) ^ n :=
rfl
#align with_zero.coe_zpow WithZero.coe_zpow
instance divInvMonoid [DivInvMonoid α] : DivInvMonoid (WithZero α) :=
{ WithZero.div, WithZero.inv, WithZero.monoidWithZero with
div_eq_mul_inv := fun a b =>
match a, b with
| none, _ => rfl
| some _, none => rfl
| some a, some b => congr_arg some (div_eq_mul_inv a b),
zpow := fun n x => x ^ n,
zpow_zero' := fun x =>
match x with
| none => rfl
| some x => congr_arg some <| zpow_zero x,
zpow_succ' := fun n x =>
match x with
| none => by
change 0 ^ _ = 0 ^ _ * 0
simp only [mul_zero]
rfl
| some x => congr_arg some <| DivInvMonoid.zpow_succ' n x,
zpow_neg' := fun n x =>
match x with
| none => rfl
| some x => congr_arg some <| DivInvMonoid.zpow_neg' n x }
instance divInvOneMonoid [DivInvOneMonoid α] : DivInvOneMonoid (WithZero α) :=
{ WithZero.divInvMonoid, WithZero.invOneClass with }
section Group
variable [Group α]
/-- if `G` is a group then `WithZero G` is a group with zero. -/
instance groupWithZero : GroupWithZero (WithZero α) :=
{ WithZero.monoidWithZero, WithZero.divInvMonoid, WithZero.nontrivial with
inv_zero := inv_zero,
mul_inv_cancel := fun a ha ↦ by
lift a to α using ha
norm_cast
apply mul_right_inv }
end Group
instance commGroupWithZero [CommGroup α] : CommGroupWithZero (WithZero α) :=
{ WithZero.groupWithZero, WithZero.commMonoidWithZero with }
instance addMonoidWithOne [AddMonoidWithOne α] : AddMonoidWithOne (WithZero α) :=
{ WithZero.addMonoid, WithZero.one with
natCast := fun n => if n = 0 then 0 else (n.cast : α),
natCast_zero := rfl,
natCast_succ := fun n => by
cases n with
| zero => show (((1 : ℕ) : α) : WithZero α) = 0 + 1; · rw [Nat.cast_one, coe_one, zero_add]
| succ n =>
show (((n + 2 : ℕ) : α) : WithZero α) = ((n + 1 : ℕ) : α) + 1
rw [Nat.cast_succ, coe_add, coe_one]
}
end WithZero
-- Check that we haven't needed to import all the basic lemmas about groups,
-- by asserting a random sample don't exist here:
assert_not_exists inv_involutive
assert_not_exists div_right_inj
assert_not_exists pow_ite
assert_not_exists Ring