Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 995b47e

Browse files
urkuderic-wieserYaelDillies
committed
refactor(*): use option.map₂ (#18081)
Relevant parts are forward-ported as leanprover-community/mathlib4#1439 Co-authored-by: Eric Wieser <wieser.eric@gmail.com> Co-authored-by: Yaël Dillies <yael.dillies@gmail.com>
1 parent 23c61a3 commit 995b47e

File tree

12 files changed

+114
-152
lines changed

12 files changed

+114
-152
lines changed

src/algebra/continued_fractions/convergents_equiv.lean

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ squashed into position `n`. -/
111111
lemma squash_seq_nth_of_not_terminated {gp_n gp_succ_n : pair K}
112112
(s_nth_eq : s.nth n = some gp_n) (s_succ_nth_eq : s.nth (n + 1) = some gp_succ_n) :
113113
(squash_seq s n).nth n = some ⟨gp_n.a, gp_n.b + gp_succ_n.a / gp_succ_n.b⟩ :=
114-
by simp [*, squash_seq, (seq.zip_with_nth_some (seq.nats_nth n) s_nth_eq _)]
114+
by simp [*, squash_seq]
115115

116116
/-- The values before the squashed position stay the same. -/
117117
lemma squash_seq_nth_of_lt {m : ℕ} (m_lt_n : m < n) : (squash_seq s n).nth m = s.nth m :=
@@ -123,8 +123,7 @@ begin
123123
s.ge_stable n.le_succ s_succ_nth_eq,
124124
obtain ⟨gp_m, s_mth_eq⟩ : ∃ gp_m, s.nth m = some gp_m, from
125125
s.ge_stable (le_of_lt m_lt_n) s_nth_eq,
126-
simp [*, squash_seq, (seq.zip_with_nth_some (seq.nats_nth m) s_mth_eq _),
127-
(ne_of_lt m_lt_n)] }
126+
simp [*, squash_seq, m_lt_n.ne] }
128127
end
129128

130129
/-- Squashing at position `n + 1` and taking the tail is the same as squashing the tail of the
@@ -141,19 +140,15 @@ begin
141140
{ obtain ⟨gp_succ_n, s_succ_nth_eq⟩ : ∃ gp_succ_n, s.nth (n + 1) = some gp_succ_n, from
142141
s.ge_stable (n + 1).le_succ s_succ_succ_nth_eq,
143142
-- apply extensionality with `m` and continue by cases `m = n`.
144-
ext m,
143+
ext1 m,
145144
cases decidable.em (m = n) with m_eq_n m_ne_n,
146145
{ have : s.tail.nth n = some gp_succ_n, from (s.nth_tail n).trans s_succ_nth_eq,
147-
simp [*, squash_seq, seq.nth_tail, (seq.zip_with_nth_some (seq.nats_nth n) this),
148-
(seq.zip_with_nth_some (seq.nats_nth (n + 1)) s_succ_nth_eq)] },
146+
simp [*, squash_seq] },
149147
{ have : s.tail.nth m = s.nth (m + 1), from s.nth_tail m,
150148
cases s_succ_mth_eq : s.nth (m + 1),
151149
all_goals { have s_tail_mth_eq, from this.trans s_succ_mth_eq },
152-
{ simp only [*, squash_seq, seq.nth_tail, (seq.zip_with_nth_none' s_succ_mth_eq),
153-
(seq.zip_with_nth_none' s_tail_mth_eq)] },
154-
{ simp [*, squash_seq, seq.nth_tail,
155-
(seq.zip_with_nth_some (seq.nats_nth (m + 1)) s_succ_mth_eq),
156-
(seq.zip_with_nth_some (seq.nats_nth m) s_tail_mth_eq)] } } }
150+
{ simp only [*, squash_seq, seq.nth_tail, seq.nth_zip_with, option.map₂_none_right] },
151+
{ simp [*, squash_seq] } } }
157152
end
158153

159154
/-- The auxiliary function `convergents'_aux` returns the same value for a sequence and the

src/algebra/group/with_one/defs.lean

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ this provides an example of an adjunction is proved in `algebra.category.Mon.adj
1919
Another result says that adjoining to a group an element `zero` gives a `group_with_zero`. For more
2020
information about these structures (which are not that standard in informal mathematics, see
2121
`algebra.group_with_zero.basic`)
22+
23+
## Implementation notes
24+
25+
At various points in this file, `id $` is used in at the start of a proof field in a structure. This
26+
ensures that the generated `_proof_1` lemmas are stated in terms of the algebraic operations and
27+
not `option.map`, as the latter does not typecheck once `with_zero`/`with_one` is irreducible.
2228
-/
2329

2430
universes u v w
@@ -49,7 +55,7 @@ instance [has_mul α] : has_mul (with_one α) := ⟨option.lift_or_get (*)⟩
4955
@[to_additive] instance [has_inv α] : has_inv (with_one α) := ⟨λ a, option.map has_inv.inv a⟩
5056

5157
@[to_additive] instance [has_involutive_inv α] : has_involutive_inv (with_one α) :=
52-
{ inv_inv := λ a, (option.map_map _ _ _).trans $ by simp_rw [inv_comp_inv, option.map_id, id],
58+
{ inv_inv := id $ λ a, (option.map_map _ _ _).trans $ by simp_rw [inv_comp_inv, option.map_id, id],
5359
..with_one.has_inv }
5460

5561
@[to_additive] instance [has_inv α] : inv_one_class (with_one α) :=
@@ -112,15 +118,12 @@ protected lemma cases_on {P : with_one α → Prop} :
112118
∀ (x : with_one α), P 1 → (∀ a : α, P a) → P x :=
113119
option.cases_on
114120

115-
-- the `show` statements in the proofs are important, because otherwise the generated lemmas
116-
-- `with_one.mul_one_class._proof_{1,2}` have an ill-typed statement after `with_one` is made
117-
-- irreducible.
118121
@[to_additive]
119122
instance [has_mul α] : mul_one_class (with_one α) :=
120123
{ mul := (*),
121124
one := (1),
122-
one_mul := show ∀ x : with_one α, 1 * x = x, from (option.lift_or_get_is_left_id _).1,
123-
mul_one := show ∀ x : with_one α, x * 1 = x, from (option.lift_or_get_is_right_id _).1 }
125+
one_mul := id $ (option.lift_or_get_is_left_id _).1,
126+
mul_one := id $ (option.lift_or_get_is_right_id _).1 }
124127

125128
@[to_additive]
126129
instance [semigroup α] : monoid (with_one α) :=
@@ -153,49 +156,28 @@ instance [one : has_one α] : has_one (with_zero α) :=
153156
@[simp, norm_cast] lemma coe_one [has_one α] : ((1 : α) : with_zero α) = 1 := rfl
154157

155158
instance [has_mul α] : mul_zero_class (with_zero α) :=
156-
{ mul := λ o₁ o₂, o₁.bind (λ a, option.map (λ b, a * b) o₂),
157-
zero_mul := λ a, rfl,
158-
mul_zero := λ a, by cases a; refl,
159+
{ mul := option.map₂ (*),
160+
zero_mul := id $ option.map₂_none_left (*),
161+
mul_zero := id $ option.map₂_none_right (*),
159162
..with_zero.has_zero }
160163

161164
@[simp, norm_cast] lemma coe_mul {α : Type u} [has_mul α]
162165
{a b : α} : ((a * b : α) : with_zero α) = a * b := rfl
163166

164-
@[simp] lemma zero_mul {α : Type u} [has_mul α]
165-
(a : with_zero α) : 0 * a = 0 := rfl
166-
167-
@[simp] lemma mul_zero {α : Type u} [has_mul α]
168-
(a : with_zero α) : a * 0 = 0 := by cases a; refl
169-
170167
instance [has_mul α] : no_zero_divisors (with_zero α) :=
171-
by { rintro (a|a) (b|b) h, exacts [or.inl rfl, or.inl rfl, or.inr rfl, option.no_confusion h] }
168+
λ a b, id $ option.map₂_eq_none_iff.1
172169

173170
instance [semigroup α] : semigroup_with_zero (with_zero α) :=
174-
{ mul_assoc := λ a b c, match a, b, c with
175-
| none, _, _ := rfl
176-
| some a, none, _ := rfl
177-
| some a, some b, none := rfl
178-
| some a, some b, some c := congr_arg some (mul_assoc _ _ _)
179-
end,
171+
{ mul_assoc := id $ λ _ _ _, option.map₂_assoc mul_assoc,
180172
..with_zero.mul_zero_class }
181173

182174
instance [comm_semigroup α] : comm_semigroup (with_zero α) :=
183-
{ mul_comm := λ a b, match a, b with
184-
| none, _ := (mul_zero _).symm
185-
| some a, none := rfl
186-
| some a, some b := congr_arg some (mul_comm _ _)
187-
end,
175+
{ mul_comm := id $ λ _ _, option.map₂_comm mul_comm,
188176
..with_zero.semigroup_with_zero }
189177

190178
instance [mul_one_class α] : mul_zero_one_class (with_zero α) :=
191-
{ one_mul := λ a, match a with
192-
| none := rfl
193-
| some a := congr_arg some $ one_mul _
194-
end,
195-
mul_one := λ a, match a with
196-
| none := rfl
197-
| some a := congr_arg some $ mul_one _
198-
end,
179+
{ one_mul := id $ option.map₂_left_identity one_mul,
180+
mul_one := id $ option.map₂_right_identity mul_one,
199181
..with_zero.mul_zero_class,
200182
..with_zero.has_one }
201183

@@ -234,16 +216,15 @@ instance [has_inv α] : has_inv (with_zero α) := ⟨λ a, option.map has_inv.in
234216
@[simp] lemma inv_zero [has_inv α] : (0 : with_zero α)⁻¹ = 0 := rfl
235217

236218
instance [has_involutive_inv α] : has_involutive_inv (with_zero α) :=
237-
{ inv_inv := λ a, (option.map_map _ _ _).trans $ by simp_rw [inv_comp_inv, option.map_id, id],
219+
{ inv_inv := id $ λ a, (option.map_map _ _ _).trans $ by simp_rw [inv_comp_inv, option.map_id, id],
238220
..with_zero.has_inv }
239221

240222
instance [inv_one_class α] : inv_one_class (with_zero α) :=
241223
{ inv_one := show ((1⁻¹ : α) : with_zero α) = 1, by simp,
242224
..with_zero.has_one,
243225
..with_zero.has_inv }
244226

245-
instance [has_div α] : has_div (with_zero α) :=
246-
⟨λ o₁ o₂, o₁.bind (λ a, option.map (λ b, a / b) o₂)⟩
227+
instance [has_div α] : has_div (with_zero α) := ⟨option.map₂ (/)⟩
247228

248229
@[norm_cast] lemma coe_div [has_div α] (a b : α) : ↑(a / b : α) = (a / b : with_zero α) := rfl
249230

src/algebra/monoid_algebra/grading.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ graded_algebra.of_alg_hom _
180180
dsimp,
181181
rw [decompose_aux_single, direct_sum.coe_alg_hom_of, subtype.coe_mk],
182182
end)
183-
(λ i x, by convert (decompose_aux_coe f x : _))
183+
(λ i x, by rw [decompose_aux_coe f x])
184184

185185
-- Lean can't find this later without us repeating it
186186
instance grade_by.decomposition : direct_sum.decomposition (grade_by R f) :=

src/algebra/order/monoid/with_top.lean

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ end has_one
5454
section has_add
5555
variables [has_add α] {a b c d : with_top α} {x y : α}
5656
57-
instance : has_add (with_top α) := ⟨λ o₁ o₂, o₁.bind $ λ a, o₂.map $ (+) a
57+
instance : has_add (with_top α) := ⟨option.map(+)⟩
5858
5959
@[norm_cast] lemma coe_add : ((x + y : α) : with_top α) = x + y := rfl
6060
@[norm_cast] lemma coe_bit0 : ((bit0 x : α) : with_top α) = bit0 x := rfl
@@ -201,35 +201,16 @@ end
201201
end has_add
202202
203203
instance [add_semigroup α] : add_semigroup (with_top α) :=
204-
{ add_assoc := begin
205-
repeat { refine with_top.rec_top_coe _ _; try { intro }};
206-
simp [←with_top.coe_add, add_assoc]
207-
end,
204+
{ add_assoc := λ _ _ _, option.map₂_assoc add_assoc,
208205
..with_top.has_add }
209206
210207
instance [add_comm_semigroup α] : add_comm_semigroup (with_top α) :=
211-
{ add_comm :=
212-
begin
213-
repeat { refine with_top.rec_top_coe _ _; try { intro }};
214-
simp [←with_top.coe_add, add_comm]
215-
end,
208+
{ add_comm := λ _ _, option.map₂_comm add_comm,
216209
..with_top.add_semigroup }
217210
218211
instance [add_zero_class α] : add_zero_class (with_top α) :=
219-
{ zero_add :=
220-
begin
221-
refine with_top.rec_top_coe _ _,
222-
{ simp },
223-
{ intro,
224-
rw [←with_top.coe_zero, ←with_top.coe_add, zero_add] }
225-
end,
226-
add_zero :=
227-
begin
228-
refine with_top.rec_top_coe _ _,
229-
{ simp },
230-
{ intro,
231-
rw [←with_top.coe_zero, ←with_top.coe_add, add_zero] }
232-
end,
212+
{ zero_add := option.map₂_left_identity zero_add,
213+
add_zero := option.map₂_right_identity add_zero,
233214
..with_top.has_zero,
234215
..with_top.has_add }
235216

src/algebra/order/ring/with_top.lean

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ variables [has_zero α] [has_mul α]
3030

3131
instance : mul_zero_class (with_top α) :=
3232
{ zero := 0,
33-
mul := λ m n, if m = 0 ∨ n = 0 then 0 else m.bind (λa, n.bind $ λb, ↑(a * b)),
33+
mul := λ m n, if m = 0 ∨ n = 0 then 0 else option.map₂ (*) m n,
3434
zero_mul := assume a, if_pos $ or.inl rfl,
3535
mul_zero := assume a, if_pos $ or.inr rfl }
3636

37-
lemma mul_def {a b : with_top α} :
38-
a * b = if a = 0 ∨ b = 0 then 0 else a.bind (λa, b.bind $ λb, ↑(a * b)) := rfl
37+
lemma mul_def {a b : with_top α} : a * b = if a = 0 ∨ b = 0 then 0 else option.map₂ (*) a b := rfl
3938

4039
@[simp] lemma mul_top {a : with_top α} (h : a ≠ 0) : a * ⊤ = ⊤ :=
4140
by cases a; simp [mul_def, h]; refl
@@ -46,6 +45,14 @@ by cases a; simp [mul_def, h]; refl
4645
@[simp] lemma top_mul_top : (⊤ * ⊤ : with_top α) = ⊤ :=
4746
top_mul top_ne_zero
4847

48+
instance [no_zero_divisors α] : no_zero_divisors (with_top α) :=
49+
begin
50+
refine ⟨λ a b h₁, decidable.by_contradiction $ λ h₂, _⟩,
51+
rw [mul_def, if_neg h₂] at h₁,
52+
rcases option.mem_map₂_iff.1 h₁ with ⟨a, b, (rfl : _ = _), (rfl : _ = _), hab⟩,
53+
exact h₂ ((eq_zero_or_eq_zero_of_mul_eq_zero hab).imp (congr_arg some) (congr_arg some))
54+
end
55+
4956
end has_mul
5057

5158
section mul_zero_class
@@ -55,7 +62,7 @@ variables [mul_zero_class α]
5562
@[norm_cast] lemma coe_mul {a b : α} : (↑(a * b) : with_top α) = a * b :=
5663
decidable.by_cases (assume : a = 0, by simp [this]) $ assume ha,
5764
decidable.by_cases (assume : b = 0, by simp [this]) $ assume hb,
58-
by { simp [*, mul_def], refl }
65+
by { simp [*, mul_def] }
5966

6067
lemma mul_coe {b : α} (hb : b ≠ 0) : ∀{a : with_top α}, a * b = a.bind (λa:α, ↑(a * b))
6168
| none := show (if (⊤:with_top α) = 0 ∨ (b:with_top α) = 0 then 0 else ⊤ : with_top α) = ⊤,
@@ -114,8 +121,8 @@ instance [mul_zero_one_class α] [nontrivial α] : mul_zero_one_class (with_top
114121
begin
115122
have : ∀ z, map f z = 0 ↔ z = 0,
116123
from λ z, (option.map_injective hf).eq_iff' f.to_zero_hom.with_top_map.map_zero,
117-
rcases eq_or_ne x 0 with rfl|hx, { simp },
118-
rcases eq_or_ne y 0 with rfl|hy, { simp },
124+
rcases decidable.eq_or_ne x 0 with rfl|hx, { simp },
125+
rcases decidable.eq_or_ne y 0 with rfl|hy, { simp },
119126
induction x using with_top.rec_top_coe, { simp [hy, this] },
120127
induction y using with_top.rec_top_coe,
121128
{ have : (f x : with_top S) ≠ 0, by simpa [hf.eq_iff' (map_zero f)] using hx,
@@ -124,10 +131,6 @@ instance [mul_zero_one_class α] [nontrivial α] : mul_zero_one_class (with_top
124131
end,
125132
.. f.to_zero_hom.with_top_map, .. f.to_monoid_hom.to_one_hom.with_top_map }
126133

127-
instance [mul_zero_class α] [no_zero_divisors α] : no_zero_divisors (with_top α) :=
128-
⟨λ a b, by cases a; cases b; dsimp [mul_def]; split_ifs;
129-
simp [*, none_eq_top, some_eq_coe, mul_eq_zero] at *⟩
130-
131134
instance [semigroup_with_zero α] [no_zero_divisors α] : semigroup_with_zero (with_top α) :=
132135
{ mul := (*),
133136
zero := 0,
@@ -150,7 +153,7 @@ instance [comm_monoid_with_zero α] [no_zero_divisors α] [nontrivial α] :
150153
{ mul := (*),
151154
zero := 0,
152155
mul_comm := λ a b,
153-
by simp only [or_comm, mul_def, option.bind_comm a b, mul_comm],
156+
by simp only [or_comm, mul_def, mul_comm, @option.map₂_comm _ _ _ _ a b _ mul_comm],
154157
.. with_top.monoid_with_zero }
155158

156159
variables [canonically_ordered_comm_semiring α]
@@ -202,7 +205,7 @@ instance : mul_zero_class (with_bot α) :=
202205
with_top.mul_zero_class
203206

204207
lemma mul_def {a b : with_bot α} :
205-
a * b = if a = 0 ∨ b = 0 then 0 else a.bind (λa, b.bind $ λb, ↑(a * b)) := rfl
208+
a * b = if a = 0 ∨ b = 0 then 0 else option.map₂ (*) a b := rfl
206209

207210
@[simp] lemma mul_bot {a : with_bot α} (h : a ≠ 0) : a * ⊥ = ⊥ :=
208211
with_top.mul_top h
@@ -220,9 +223,7 @@ section mul_zero_class
220223
variables [mul_zero_class α]
221224

222225
@[norm_cast] lemma coe_mul {a b : α} : (↑(a * b) : with_bot α) = a * b :=
223-
decidable.by_cases (assume : a = 0, by simp [this]) $ assume ha,
224-
decidable.by_cases (assume : b = 0, by simp [this]) $ assume hb,
225-
by { simp [*, mul_def], refl }
226+
with_top.coe_mul
226227

227228
lemma mul_coe {b : α} (hb : b ≠ 0) {a : with_bot α} : a * b = a.bind (λa:α, ↑(a * b)) :=
228229
with_top.mul_coe hb

src/data/finset/n_ary.lean

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,16 @@ lemma image_image₂_right_anticomm {f : α → β' → γ} {g : β → β'} {f'
321321
image₂ f s (t.image g) = (image₂ f' t s).image g' :=
322322
(image_image₂_antidistrib_right $ λ a b, (h_right_anticomm b a).symm).symm
323323

324+
/-- If `a` is a left identity for `f : α → β → β`, then `{a}` is a left identity for
325+
`finset.image₂ f`. -/
326+
lemma image₂_left_identity {f : α → γ → γ} {a : α} (h : ∀ b, f a b = b) (t : finset γ) :
327+
image₂ f {a} t = t :=
328+
coe_injective $ by rw [coe_image₂, coe_singleton, set.image2_left_identity h]
329+
330+
/-- If `b` is a right identity for `f : α → β → α`, then `{b}` is a right identity for
331+
`finset.image₂ f`. -/
332+
lemma image₂_right_identity {f : γ → β → γ} {b : β} (h : ∀ a, f a b = a) (s : finset γ) :
333+
image₂ f s {b} = s :=
334+
by rw [image₂_singleton_right, funext h, image_id']
335+
324336
end finset

src/data/option/n_ary.lean

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ because of the lack of universe polymorphism. -/
4242
lemma map₂_def {α β γ : Type*} (f : α → β → γ) (a : option α) (b : option β) :
4343
map₂ f a b = f <$> a <*> b := by cases a; refl
4444

45-
@[simp] lemma map₂_some_some (f : α → β → γ) (a : α) (b : β) : map₂ f (some a) (some b) = f a b :=
45+
@[simp] lemma map₂_some_some (f : α → β → γ) (a : α) (b : β) :
46+
map₂ f (some a) (some b) = some (f a b) :=
4647
rfl
48+
4749
lemma map₂_coe_coe (f : α → β → γ) (a : α) (b : β) : map₂ f a b = f a b := rfl
4850
@[simp] lemma map₂_none_left (f : α → β → γ) (b : option β) : map₂ f none b = none := rfl
4951
@[simp] lemma map₂_none_right (f : α → β → γ) (a : option α) : map₂ f a none = none :=
@@ -170,4 +172,16 @@ lemma map_map₂_right_anticomm {f : α → β' → γ} {g : β → β'} {f' :
170172
map₂ f a (b.map g) = (map₂ f' b a).map g' :=
171173
by cases a; cases b; simp [h_right_anticomm]
172174

175+
/-- If `a` is a left identity for a binary operation `f`, then `some a` is a left identity for
176+
`option.map₂ f`. -/
177+
lemma map₂_left_identity {f : α → β → β} {a : α} (h : ∀ b, f a b = b) (o : option β) :
178+
map₂ f (some a) o = o :=
179+
by { cases o, exacts [rfl, congr_arg some (h _)] }
180+
181+
/-- If `b` is a right identity for a binary operation `f`, then `some b` is a right identity for
182+
`option.map₂ f`. -/
183+
lemma map₂_right_identity {f : α → β → α} {b : β} (h : ∀ a, f a b = a) (o : option α) :
184+
map₂ f o (some b) = o :=
185+
by simp [h, map₂]
186+
173187
end option

0 commit comments

Comments
 (0)