@@ -1335,6 +1335,10 @@ lemma last_eq_nth_le : ∀ (l : list α) (h : l ≠ []),
1335
1335
| (a :: b :: l) h := by { rw [last_cons, last_eq_nth_le (b :: l)],
1336
1336
refl, exact cons_ne_nil b l }
1337
1337
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
+
1338
1342
@[simp] lemma nth_concat_length : ∀ (l : list α) (a : α), (l ++ [a]).nth l.length = some a
1339
1343
| [] a := rfl
1340
1344
| (b::l) a := by rw [cons_append, length_cons, nth, nth_concat_length]
@@ -1347,6 +1351,17 @@ begin
1347
1351
simp [h]
1348
1352
end
1349
1353
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
+
1350
1365
@[ext]
1351
1366
theorem ext : ∀ {l₁ l₂ : list α}, (∀n, nth l₁ n = nth l₂ n) → l₁ = l₂
1352
1367
| [] [] h := rfl
@@ -1934,6 +1949,31 @@ end
1934
1949
l.take k = [] ↔ l = [] ∨ k = 0 :=
1935
1950
by { cases l; cases k; simp [nat.succ_ne_zero] }
1936
1951
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
+
1937
1977
lemma init_eq_take (l : list α) : l.init = l.take l.length.pred :=
1938
1978
begin
1939
1979
cases l with x l,
@@ -2023,6 +2063,16 @@ theorem drop_eq_nth_le_cons : ∀ {n} {l : list α} h,
2023
2063
calc l.drop l.length = (l ++ []).drop l.length : by simp
2024
2064
... = [] : drop_left _ _
2025
2065
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
+
2026
2076
/-- Dropping the elements up to `n` in `l₁ ++ l₂` is the same as dropping the elements up to `n`
2027
2077
in `l₁`, dropping the elements up to `n - l₁.length` in `l₂`, and appending them. -/
2028
2078
lemma drop_append_eq_append_drop {l₁ l₂ : list α} {n : ℕ} :
0 commit comments