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

Commit cf9386b

Browse files
feat(data/list/basic): add lemma map_eq_repeat_iff (#17832)
This small PR adds the following API lemma for lists: ```lean lemma map_eq_repeat_iff {α β} {l : list α} {f : α → β} {b : β} : l.map f = repeat b l.length ↔ (∀ x ∈ l, f x = b) ``` See this [Zulip thread](https://leanprover.zulipchat.com/#narrow/stream/217875-Is-there-code-for-X.3F/topic/More.20general.20version.20of.20.60list.2Emap_const.60.3F/near/314118320). (It also fixes a harmless typo in the proof of `last_map`.)
1 parent 4f4e7fa commit cf9386b

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

src/data/list/basic.lean

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,6 @@ lemma subset_singleton_iff {a : α} : ∀ L : list α, L ⊆ [a] ↔ ∃ n, L =
506506
exact ⟨n.succ, by simp [mem_singleton.mp h.1]⟩
507507
end
508508

509-
@[simp] theorem map_const (l : list α) (b : β) : map (function.const α b) l = repeat b l.length :=
510-
by induction l; [refl, simp only [*, map]]; split; refl
511-
512-
theorem eq_of_mem_map_const {b₁ b₂ : β} {l : list α} (h : b₁ ∈ map (function.const α b₂) l) :
513-
b₁ = b₂ :=
514-
by rw map_const at h; exact eq_of_mem_repeat h
515-
516509
@[simp] theorem map_repeat (f : α → β) (a : α) (n) : map f (repeat a n) = repeat (f a) n :=
517510
by induction n; [refl, simp only [*, repeat, map]]; split; refl
518511

@@ -1791,13 +1784,30 @@ by { induction as, { refl }, { simp! [*, apply_ite (map f)] } }
17911784
lemma last_map (f : α → β) {l : list α} (hl : l ≠ []) :
17921785
(l.map f).last (mt eq_nil_of_map_eq_nil hl) = f (l.last hl) :=
17931786
begin
1794-
induction l with l_ih l_tl l_ih,
1787+
induction l with l_hd l_tl l_ih,
17951788
{ apply (hl rfl).elim },
17961789
{ cases l_tl,
17971790
{ simp },
17981791
{ simpa using l_ih } }
17991792
end
18001793

1794+
lemma map_eq_repeat_iff {l : list α} {f : α → β} {b : β} :
1795+
l.map f = repeat b l.length ↔ (∀ x ∈ l, f x = b) :=
1796+
begin
1797+
induction l with x l' ih,
1798+
{ simp only [repeat, length, not_mem_nil, is_empty.forall_iff, implies_true_iff,
1799+
map_nil, eq_self_iff_true], },
1800+
{ simp only [map, length, mem_cons_iff, forall_eq_or_imp, repeat_succ, and.congr_right_iff],
1801+
exact λ _, ih, }
1802+
end
1803+
1804+
@[simp] theorem map_const (l : list α) (b : β) : map (function.const α b) l = repeat b l.length :=
1805+
map_eq_repeat_iff.mpr (λ x _, rfl)
1806+
1807+
theorem eq_of_mem_map_const {b₁ b₂ : β} {l : list α} (h : b₁ ∈ map (function.const α b₂) l) :
1808+
b₁ = b₂ :=
1809+
by rw map_const at h; exact eq_of_mem_repeat h
1810+
18011811
/-! ### map₂ -/
18021812

18031813
theorem nil_map₂ (f : α → β → γ) (l : list β) : map₂ f [] l = [] :=

0 commit comments

Comments
 (0)