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

Commit 0f06bcc

Browse files
committed
feat(data/list/{basic,join}): towards take_join (#17190)
Co-authored-by: Patrick Johnson <reflexivetransitiveclosure@gmail.com> Co-authored-by: madvorak <dvorakmartinbridge@seznam.cz> Co-authored-by: Martin Dvořák <dvorakmartinbridge@seznam.cz>
1 parent 4b262b8 commit 0f06bcc

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/data/list/basic.lean

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,10 @@ lemma last_eq_nth_le : ∀ (l : list α) (h : l ≠ []),
13351335
| (a :: b :: l) h := by { rw [last_cons, last_eq_nth_le (b :: l)],
13361336
refl, exact cons_ne_nil b l }
13371337

1338+
lemma nth_le_length_sub_one {l : list α} (h : l.length - 1 < l.length) :
1339+
l.nth_le (l.length - 1) h = l.last (by { rintro rfl, exact nat.lt_irrefl 0 h }) :=
1340+
(last_eq_nth_le l _).symm
1341+
13381342
@[simp] lemma nth_concat_length : ∀ (l : list α) (a : α), (l ++ [a]).nth l.length = some a
13391343
| [] a := rfl
13401344
| (b::l) a := by rw [cons_append, length_cons, nth, nth_concat_length]
@@ -1347,6 +1351,17 @@ begin
13471351
simp [h]
13481352
end
13491353

1354+
lemma take_one_drop_eq_of_lt_length {l : list α} {n : ℕ} (h : n < l.length) :
1355+
(l.drop n).take 1 = [l.nth_le n h] :=
1356+
begin
1357+
induction l with x l ih generalizing n,
1358+
{ cases h },
1359+
{ by_cases h₁ : l = [],
1360+
{ subst h₁, rw nth_le_singleton, simp at h, subst h, simp },
1361+
have h₂ := h, rw [length_cons, nat.lt_succ_iff, le_iff_eq_or_lt] at h₂,
1362+
cases n, { simp }, rw [drop, nth_le], apply ih },
1363+
end
1364+
13501365
@[ext]
13511366
theorem ext : ∀ {l₁ l₂ : list α}, (∀n, nth l₁ n = nth l₂ n) → l₁ = l₂
13521367
| [] [] h := rfl
@@ -1934,6 +1949,31 @@ end
19341949
l.take k = [] ↔ l = [] ∨ k = 0 :=
19351950
by { cases l; cases k; simp [nat.succ_ne_zero] }
19361951

1952+
lemma take_eq_take : ∀ {l : list α} {m n : ℕ},
1953+
l.take m = l.take n ↔ min m l.length = min n l.length
1954+
| [] m n := by simp
1955+
| (x :: xs) 0 0 := by simp
1956+
| (x :: xs) (m + 1) 0 := by simp
1957+
| (x :: xs) 0 (n + 1) := by simp [@eq_comm ℕ 0]
1958+
| (x :: xs) (m + 1) (n + 1) := by simp [nat.min_succ_succ, take_eq_take]
1959+
1960+
lemma take_add (l : list α) (m n : ℕ) :
1961+
l.take (m + n) = l.take m ++ (l.drop m).take n :=
1962+
begin
1963+
convert_to
1964+
take (m + n) (take m l ++ drop m l) =
1965+
take m l ++ take n (drop m l),
1966+
{ rw take_append_drop },
1967+
rw [take_append_eq_append_take, take_all_of_le, append_right_inj], swap,
1968+
{ transitivity m,
1969+
{ apply length_take_le },
1970+
{ simp }},
1971+
simp only [take_eq_take, length_take, length_drop],
1972+
generalize : l.length = k, by_cases h : m ≤ k,
1973+
{ simp [min_eq_left_iff.mpr h] },
1974+
{ push_neg at h, simp [nat.sub_eq_zero_of_le (le_of_lt h)] },
1975+
end
1976+
19371977
lemma init_eq_take (l : list α) : l.init = l.take l.length.pred :=
19381978
begin
19391979
cases l with x l,
@@ -2023,6 +2063,16 @@ theorem drop_eq_nth_le_cons : ∀ {n} {l : list α} h,
20232063
calc l.drop l.length = (l ++ []).drop l.length : by simp
20242064
... = [] : drop_left _ _
20252065

2066+
lemma drop_length_cons {l : list α} (h : l ≠ []) (a : α) :
2067+
(a :: l).drop l.length = [l.last h] :=
2068+
begin
2069+
induction l with y l ih generalizing a,
2070+
{ cases h rfl },
2071+
{ simp only [drop, length],
2072+
by_cases h₁ : l = [], { simp [h₁] },
2073+
rw last_cons h₁, exact ih h₁ y },
2074+
end
2075+
20262076
/-- Dropping the elements up to `n` in `l₁ ++ l₂` is the same as dropping the elements up to `n`
20272077
in `l₁`, dropping the elements up to `n - l₁.length` in `l₂`, and appending them. -/
20282078
lemma drop_append_eq_append_drop {l₁ l₂ : list α} {n : ℕ} :

src/data/list/join.lean

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace list
1818

1919
attribute [simp] join
2020

21-
@[simp] lemma join_nil : [([] : list α)].join = [] := rfl
21+
@[simp] lemma join_singleton (l : list α) : [l].join = l :=
22+
by rw [join, join, append_nil]
2223

2324
@[simp] lemma join_eq_nil : ∀ {L : list (list α)}, join L = [] ↔ ∀ l ∈ L, l = []
2425
| [] := iff_of_true rfl (forall_mem_nil _)
@@ -139,4 +140,12 @@ begin
139140
rw [← drop_take_succ_join_eq_nth_le, ← drop_take_succ_join_eq_nth_le, join_eq, length_eq] }
140141
end
141142

143+
lemma join_drop_length_sub_one {L : list (list α)} (h : L ≠ []) :
144+
(L.drop (L.length - 1)).join = L.last h :=
145+
begin
146+
induction L using list.reverse_rec_on,
147+
{ cases h rfl },
148+
{ simp },
149+
end
150+
142151
end list

0 commit comments

Comments
 (0)