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

Commit d2ae43f

Browse files
committed
feat(data/list/basic): lemmas about nth of take and append (#5449)
1 parent 3b9cbdf commit d2ae43f

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/data/list/basic.lean

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,12 +1100,22 @@ lemma nth_le_append_right : ∀ {l₁ l₂ : list α} {n : ℕ} (h₁ : l₁.len
11001100
(list.repeat a n).nth_le m h = a :=
11011101
eq_of_mem_repeat (nth_le_mem _ _ _)
11021102

1103-
lemma nth_append {l₁ l₂ : list α} {n : ℕ} (hn : n < l₁.length) :
1103+
lemma nth_append {l₁ l₂ : list α} {n : ℕ} (hn : n < l₁.length) :
11041104
(l₁ ++ l₂).nth n = l₁.nth n :=
11051105
have hn' : n < (l₁ ++ l₂).length := lt_of_lt_of_le hn
11061106
(by rw length_append; exact le_add_right _ _),
11071107
by rw [nth_le_nth hn, nth_le_nth hn', nth_le_append]
11081108

1109+
lemma nth_append_right {l₁ l₂ : list α} {n : ℕ} (hn : l₁.length ≤ n) :
1110+
(l₁ ++ l₂).nth n = l₂.nth (n - l₁.length) :=
1111+
begin
1112+
by_cases hl : n < (l₁ ++ l₂).length,
1113+
{ rw [nth_le_nth hl, nth_le_nth, nth_le_append_right hn] },
1114+
{ rw [nth_len_le (le_of_not_lt hl), nth_len_le],
1115+
rw [not_lt, length_append] at hl,
1116+
exact nat.le_sub_left_of_add_le hl }
1117+
end
1118+
11091119
lemma last_eq_nth_le : ∀ (l : list α) (h : l ≠ []),
11101120
last l h = l.nth_le (l.length - 1) (sub_lt (length_pos_of_ne_nil h) one_pos)
11111121
| [] h := rfl
@@ -1519,6 +1529,33 @@ lemma nth_le_take' (L : list α) {i j : ℕ} (hi : i < (L.take j).length) :
15191529
nth_le (L.take j) i hi = nth_le L i (lt_of_lt_of_le hi (by simp [le_refl])) :=
15201530
by { simp at hi, rw nth_le_take L _ hi.1 }
15211531

1532+
lemma nth_take {l : list α} {n m : ℕ} (h : m < n) :
1533+
(l.take n).nth m = l.nth m :=
1534+
begin
1535+
induction n with n hn generalizing l m,
1536+
{ simp only [nat.nat_zero_eq_zero] at h,
1537+
exact absurd h (not_lt_of_le m.zero_le) },
1538+
{ cases l with hd tl,
1539+
{ simp only [take_nil] },
1540+
{ cases m,
1541+
{ simp only [nth, take] },
1542+
{ simpa only using hn (nat.lt_of_succ_lt_succ h) } } },
1543+
end
1544+
1545+
@[simp] lemma nth_take_of_succ {l : list α} {n : ℕ} :
1546+
(l.take (n + 1)).nth n = l.nth n :=
1547+
nth_take (nat.lt_succ_self n)
1548+
1549+
lemma take_succ {l : list α} {n : ℕ} :
1550+
l.take (n + 1) = l.take n ++ (l.nth n).to_list :=
1551+
begin
1552+
induction l with hd tl hl generalizing n,
1553+
{ simp only [option.to_list, nth, take_nil, append_nil]},
1554+
{ cases n,
1555+
{ simp only [option.to_list, nth, eq_self_iff_true, and_self, take, nil_append] },
1556+
{ simp only [hl, cons_append, nth, eq_self_iff_true, and_self, take] } }
1557+
end
1558+
15221559
@[simp] theorem drop_nil : ∀ n, drop n [] = ([] : list α)
15231560
| 0 := rfl
15241561
| (n+1) := rfl

0 commit comments

Comments
 (0)