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

Commit fd9268c

Browse files
feat(data/list/basic): lemmas about scanl (#5454)
Co-authored-by: Rob Lewis <Rob.y.lewis@gmail.com>
1 parent eb5cf25 commit fd9268c

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

src/data/list/basic.lean

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ lemma bind_map {g : α → list β} {f : β → γ} :
178178
theorem length_eq_zero {l : list α} : length l = 0 ↔ l = [] :=
179179
⟨eq_nil_of_length_eq_zero, λ h, h.symm ▸ rfl⟩
180180

181+
@[simp] lemma length_singleton (a : α) : length [a] = 1 := rfl
182+
181183
theorem length_pos_of_mem {a : α} : ∀ {l : list α}, a ∈ l → 0 < length l
182184
| (b::l) _ := zero_lt_succ _
183185

@@ -1834,11 +1836,70 @@ end
18341836

18351837
/- scanl -/
18361838

1837-
lemma length_scanl {β : Type*} {f : α → β → α} :
1839+
section scanl
1840+
1841+
variables {f : β → α → β} {b : β} {a : α} {l : list α}
1842+
1843+
lemma length_scanl :
18381844
∀ a l, length (scanl f a l) = l.length + 1
18391845
| a [] := rfl
18401846
| a (x :: l) := by erw [length_cons, length_cons, length_scanl]
18411847

1848+
@[simp] lemma scanl_nil (b : β) : scanl f b nil = [b] := rfl
1849+
1850+
@[simp] lemma scanl_cons :
1851+
scanl f b (a :: l) = [b] ++ scanl f (f b a) l :=
1852+
by simp only [scanl, eq_self_iff_true, singleton_append, and_self]
1853+
1854+
@[simp] lemma nth_zero_scanl : (scanl f b l).nth 0 = some b :=
1855+
begin
1856+
cases l,
1857+
{ simp only [nth, scanl_nil] },
1858+
{ simp only [nth, scanl_cons, singleton_append] }
1859+
end
1860+
1861+
@[simp] lemma nth_le_zero_scanl {h : 0 < (scanl f b l).length} :
1862+
(scanl f b l).nth_le 0 h = b :=
1863+
begin
1864+
cases l,
1865+
{ simp only [nth_le, scanl_nil] },
1866+
{ simp only [nth_le, scanl_cons, singleton_append] }
1867+
end
1868+
1869+
lemma nth_succ_scanl {i : ℕ} :
1870+
(scanl f b l).nth (i + 1) = ((scanl f b l).nth i).bind (λ x, (l.nth i).map (λ y, f x y)) :=
1871+
begin
1872+
induction l with hd tl hl generalizing b i,
1873+
{ symmetry,
1874+
simp only [option.bind_eq_none', nth, forall_2_true_iff, not_false_iff, option.map_none',
1875+
scanl_nil, option.not_mem_none, forall_true_iff] },
1876+
{ simp only [nth, scanl_cons, singleton_append],
1877+
cases i,
1878+
{ simp only [option.map_some', nth_zero_scanl, nth, option.some_bind'] },
1879+
{ simp only [hl, nth] } }
1880+
end
1881+
1882+
lemma nth_le_succ_scanl {i : ℕ} {h : i + 1 < (scanl f b l).length} :
1883+
(scanl f b l).nth_le (i + 1) h =
1884+
f ((scanl f b l).nth_le i (nat.lt_of_succ_lt h))
1885+
(l.nth_le i (nat.lt_of_succ_lt_succ (lt_of_lt_of_le h (le_of_eq (length_scanl b l))))) :=
1886+
begin
1887+
induction i with i hi generalizing b l,
1888+
{ cases l,
1889+
{ simp only [length, zero_add, scanl_nil] at h,
1890+
exact absurd h (lt_irrefl 1) },
1891+
{ simp only [scanl_cons, singleton_append, nth_le_zero_scanl, nth_le] } },
1892+
{ cases l,
1893+
{ simp only [length, add_lt_iff_neg_right, scanl_nil] at h,
1894+
exact absurd h (not_lt_of_lt nat.succ_pos') },
1895+
{ simp_rw scanl_cons,
1896+
rw nth_le_append_right _,
1897+
{ simpa only [hi, length, succ_add_sub_one] },
1898+
{ simp only [length, nat.zero_le, le_add_iff_nonneg_left] } } }
1899+
end
1900+
1901+
end scanl
1902+
18421903
/- scanr -/
18431904

18441905
@[simp] theorem scanr_nil (f : α → β → β) (b : β) : scanr f b [] = [b] := rfl

0 commit comments

Comments
 (0)