This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
/
with_one.lean
257 lines (196 loc) · 7.68 KB
/
with_one.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
/-
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 algebra.ring.basic
import data.equiv.basic
universes u v
variable {α : Type u}
/-- Add an extra element `1` to a type -/
@[to_additive "Add an extra element `0` to a type"]
def with_one (α) := option α
namespace with_one
@[to_additive]
instance : monad with_one := option.monad
@[to_additive]
instance : has_one (with_one α) := ⟨none⟩
@[to_additive]
instance : inhabited (with_one α) := ⟨1⟩
@[to_additive]
instance [nonempty α] : nontrivial (with_one α) := option.nontrivial
@[to_additive]
instance : has_coe_t α (with_one α) := ⟨some⟩
@[to_additive]
lemma some_eq_coe {a : α} : (some a : with_one α) = ↑a := rfl
@[simp, to_additive]
lemma coe_ne_one {a : α} : (a : with_one α) ≠ (1 : with_one α) :=
option.some_ne_none a
@[simp, to_additive]
lemma one_ne_coe {a : α} : (1 : with_one α) ≠ a :=
coe_ne_one.symm
@[to_additive]
lemma ne_one_iff_exists {x : with_one α} : x ≠ 1 ↔ ∃ (a : α), ↑a = x :=
option.ne_none_iff_exists
-- `to_additive` fails to generate some meta info around eqn lemmas, so `lift` doesn't work
-- unless we explicitly define this instance
instance : can_lift (with_one α) α :=
{ coe := coe,
cond := λ a, a ≠ 1,
prf := λ a, ne_one_iff_exists.1 }
@[simp, to_additive]
lemma coe_inj {a b : α} : (a : with_one α) = b ↔ a = b :=
option.some_inj
attribute [norm_cast] coe_inj with_zero.coe_inj
@[elab_as_eliminator, to_additive]
protected lemma cases_on {P : with_one α → Prop} :
∀ (x : with_one α), P 1 → (∀ a : α, P a) → P x :=
option.cases_on
@[to_additive]
instance [has_mul α] : has_mul (with_one α) :=
{ mul := option.lift_or_get (*) }
@[to_additive]
instance [semigroup α] : monoid (with_one α) :=
{ mul_assoc := (option.lift_or_get_assoc _).1,
one_mul := (option.lift_or_get_is_left_id _).1,
mul_one := (option.lift_or_get_is_right_id _).1,
..with_one.has_one,
..with_one.has_mul }
@[to_additive]
instance [comm_semigroup α] : comm_monoid (with_one α) :=
{ mul_comm := (option.lift_or_get_comm _).1,
..with_one.monoid }
/-- `coe` as a bundled morphism -/
@[simps apply, to_additive "`coe` as a bundled morphism"]
def coe_mul_hom [has_mul α] : mul_hom α (with_one α) :=
{ to_fun := coe, map_mul' := λ x y, rfl }
section lift
variables [semigroup α] {β : Type v} [monoid β]
/-- Lift a semigroup homomorphism `f` to a bundled monoid homorphism. -/
@[to_additive "Lift an add_semigroup homomorphism `f` to a bundled add_monoid homorphism."]
def lift : mul_hom α β ≃ (with_one α →* β) :=
{ to_fun := λ f,
{ to_fun := λ x, option.cases_on x 1 f,
map_one' := rfl,
map_mul' := λ x y,
with_one.cases_on x (by { rw one_mul, exact (one_mul _).symm }) $ λ x,
with_one.cases_on y (by { rw mul_one, exact (mul_one _).symm }) $ λ y,
f.map_mul x y },
inv_fun := λ F, F.to_mul_hom.comp coe_mul_hom,
left_inv := λ f, mul_hom.ext $ λ x, rfl,
right_inv := λ F, monoid_hom.ext $ λ x, with_one.cases_on x F.map_one.symm $ λ x, rfl }
variables (f : mul_hom α β)
@[simp, to_additive]
lemma lift_coe (x : α) : lift f x = f x := rfl
@[simp, to_additive]
lemma lift_one : lift f 1 = 1 := rfl
@[to_additive]
theorem lift_unique (f : with_one α →* β) : f = lift (f.to_mul_hom.comp coe_mul_hom) :=
(lift.apply_symm_apply f).symm
end lift
section map
variables {β : Type v} [semigroup α] [semigroup β]
/-- Given a multiplicative map from `α → β` returns a monoid homomorphism
from `with_one α` to `with_one β` -/
@[to_additive "Given an additive map from `α → β` returns an add_monoid homomorphism
from `with_zero α` to `with_zero β`"]
def map (f : mul_hom α β) : with_one α →* with_one β :=
lift (coe_mul_hom.comp f)
end map
attribute [irreducible] with_one
@[simp, norm_cast, to_additive]
lemma coe_mul [has_mul α] (a b : α) : ((a * b : α) : with_one α) = a * b := rfl
end with_one
namespace with_zero
-- `to_additive` fails to generate some meta info around eqn lemmas, so `lift` doesn't work
-- unless we explicitly define this instance
instance : can_lift (with_zero α) α :=
{ coe := coe,
cond := λ a, a ≠ 0,
prf := λ a, ne_zero_iff_exists.1 }
attribute [to_additive] with_one.can_lift
instance [one : has_one α] : has_one (with_zero α) :=
{ ..one }
@[simp, norm_cast] lemma coe_one [has_one α] : ((1 : α) : with_zero α) = 1 := rfl
instance [has_mul α] : mul_zero_class (with_zero α) :=
{ mul := λ o₁ o₂, o₁.bind (λ a, option.map (λ b, a * b) o₂),
zero_mul := λ a, rfl,
mul_zero := λ a, by cases a; refl,
..with_zero.has_zero }
@[simp, norm_cast] lemma coe_mul {α : Type u} [has_mul α]
{a b : α} : ((a * b : α) : with_zero α) = a * b := rfl
@[simp] lemma zero_mul {α : Type u} [has_mul α]
(a : with_zero α) : 0 * a = 0 := rfl
@[simp] lemma mul_zero {α : Type u} [has_mul α]
(a : with_zero α) : a * 0 = 0 := by cases a; refl
instance [semigroup α] : semigroup (with_zero α) :=
{ mul_assoc := λ a b c, match a, b, c with
| none, _, _ := rfl
| some a, none, _ := rfl
| some a, some b, none := rfl
| some a, some b, some c := congr_arg some (mul_assoc _ _ _)
end,
..with_zero.mul_zero_class }
instance [comm_semigroup α] : comm_semigroup (with_zero α) :=
{ mul_comm := λ a b, match a, b with
| none, _ := (mul_zero _).symm
| some a, none := rfl
| some a, some b := congr_arg some (mul_comm _ _)
end,
..with_zero.semigroup }
instance [monoid α] : monoid_with_zero (with_zero α) :=
{ one_mul := λ a, match a with
| none := rfl
| some a := congr_arg some $ one_mul _
end,
mul_one := λ a, match a with
| none := rfl
| some a := congr_arg some $ mul_one _
end,
..with_zero.mul_zero_class,
..with_zero.has_one,
..with_zero.semigroup }
instance [comm_monoid α] : comm_monoid_with_zero (with_zero α) :=
{ ..with_zero.monoid_with_zero, ..with_zero.comm_semigroup }
/-- Given an inverse operation on `α` there is an inverse operation
on `with_zero α` sending `0` to `0`-/
definition inv [has_inv α] (x : with_zero α) : with_zero α :=
do a ← x, return a⁻¹
instance [has_inv α] : has_inv (with_zero α) := ⟨with_zero.inv⟩
@[simp, norm_cast] lemma coe_inv [has_inv α] (a : α) :
((a⁻¹ : α) : with_zero α) = a⁻¹ := rfl
@[simp] lemma inv_zero [has_inv α] :
(0 : with_zero α)⁻¹ = 0 := rfl
section group
variables [group α]
@[simp] lemma inv_one : (1 : with_zero α)⁻¹ = 1 :=
show ((1⁻¹ : α) : with_zero α) = 1, by simp
/-- if `G` is a group then `with_zero G` is a group with zero. -/
instance : group_with_zero (with_zero α) :=
{ inv_zero := inv_zero,
mul_inv_cancel := by { intros a ha, lift a to α using ha, norm_cast, apply mul_right_inv },
.. with_zero.monoid_with_zero,
.. with_zero.has_inv,
.. with_zero.nontrivial }
@[norm_cast]
lemma div_coe (a b : α) : (a : with_zero α) / b = (a * b⁻¹ : α) := rfl
end group
instance [comm_group α] : comm_group_with_zero (with_zero α) :=
{ .. with_zero.group_with_zero, .. with_zero.comm_monoid_with_zero }
instance [semiring α] : semiring (with_zero α) :=
{ left_distrib := λ a b c, begin
cases a with a, {refl},
cases b with b; cases c with c; try {refl},
exact congr_arg some (left_distrib _ _ _)
end,
right_distrib := λ a b c, begin
cases c with c,
{ change (a + b) * 0 = a * 0 + b * 0, simp },
cases a with a; cases b with b; try {refl},
exact congr_arg some (right_distrib _ _ _)
end,
..with_zero.add_comm_monoid,
..with_zero.mul_zero_class,
..with_zero.monoid_with_zero }
attribute [irreducible] with_zero
end with_zero