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

Commit 840a042

Browse files
committed
feat(data/list/basic): Miscellaneous fold lemmas (#12579)
1 parent 1a581ed commit 840a042

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/data/list/basic.lean

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,6 +2102,17 @@ end
21022102
| b [] l₂ := rfl
21032103
| b (a::l₁) l₂ := by simp only [cons_append, foldr_cons, foldr_append b l₁ l₂]
21042104

2105+
@[simp] theorem foldl_fixed {a : α} : Π l : list β, foldl (λ a b, a) a l = a
2106+
| [] := rfl
2107+
| (b::l) := by rw [foldl_cons, foldl_fixed l]
2108+
2109+
@[simp] theorem foldr_fixed {b : β} : Π l : list α, foldr (λ a b, b) b l = b
2110+
| [] := rfl
2111+
| (a::l) := by rw [foldr_cons, foldr_fixed l]
2112+
2113+
@[simp] theorem foldl_combinator_K {a : α} : Π l : list β, foldl combinator.K a l = a :=
2114+
foldl_fixed
2115+
21052116
@[simp] theorem foldl_join (f : α → β → α) :
21062117
∀ (a : α) (L : list (list β)), foldl f a (join L) = foldl (foldl f) a L
21072118
| a [] := rfl

src/logic/function/iterate.lean

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,19 @@ lemma iterate_commute (m n : ℕ) : commute (λ f : α → α, f^[m]) (λ f, f^[
165165
λ f, iterate_comm f m n
166166

167167
end function
168+
169+
namespace list
170+
open function
171+
172+
theorem foldl_const (f : α → α) (a : α) (l : list β) : l.foldl (λ b _, f b) a = (f^[l.length]) a :=
173+
begin
174+
induction l with b l H generalizing a,
175+
{ refl },
176+
{ rw [length_cons, foldl, iterate_succ_apply, H] }
177+
end
178+
179+
theorem foldr_const (f : β → β) (b : β) : Π l : list α, l.foldr (λ _, f) b = (f^[l.length]) b
180+
| [] := rfl
181+
| (a::l) := by rw [length_cons, foldr, foldr_const l, iterate_succ_apply']
182+
183+
end list

0 commit comments

Comments
 (0)