Skip to content

Commit 177ae4e

Browse files
feat: relate List.mapAccumr to List.foldr (#5390)
Add lemmas that rewrite an application of `List.mapAccumr` or `List.mapAccumr_2` into an application of `List.foldr` Co-authored-by: Chris Hughes <chrishughes24@gmail.com>
1 parent 8743888 commit 177ae4e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Mathlib/Data/List/Lemmas.lean

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Authors: Yakov Pechersky, Yury Kudryashov
1010
-/
1111
import Mathlib.Data.Set.Function
1212
import Mathlib.Data.List.Basic
13+
import Mathlib.Init.Data.List.Lemmas
1314

1415
/-! # Some lemmas about lists involving sets
1516
@@ -82,4 +83,36 @@ theorem foldl_range_eq_of_range_eq {f : α → β → α} {g : α → γ → α}
8283
(foldl_range_subset_of_range_subset hfg.ge a)
8384
#align list.foldl_range_eq_of_range_eq List.foldl_range_eq_of_range_eq
8485

86+
87+
88+
/-!
89+
### MapAccumr and Foldr
90+
Some lemmas relation `mapAccumr` and `foldr`
91+
-/
92+
section MapAccumr
93+
94+
theorem mapAccumr_eq_foldr (f : α → σ → σ × β) : ∀ (as : List α) (s : σ),
95+
mapAccumr f as s = List.foldr (fun a s =>
96+
let r := f a s.1
97+
(r.1, r.2 :: s.2)
98+
) (s, []) as
99+
| [], s => rfl
100+
| a :: as, s => by
101+
simp only [mapAccumr, foldr, mapAccumr_eq_foldr f as]
102+
103+
theorem mapAccumr₂_eq_foldr (f : α → β → σ → σ × φ) :
104+
∀ (as : List α) (bs : List β) (s : σ),
105+
mapAccumr₂ f as bs s = foldr (fun ab s =>
106+
let r := f ab.1 ab.2 s.1
107+
(r.1, r.2 :: s.2)
108+
) (s, []) (as.zip bs)
109+
| [], [], s => rfl
110+
| a :: as, [], s => rfl
111+
| [], b :: bs, s => rfl
112+
| a :: as, b :: bs, s => by
113+
simp only [mapAccumr₂, foldr, mapAccumr₂_eq_foldr f as]
114+
rfl
115+
116+
end MapAccumr
117+
85118
end List

0 commit comments

Comments
 (0)