Skip to content

Commit cd255ae

Browse files
committed
lemmas about list iterators
1 parent 9e0c59e commit cd255ae

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

src/Std/Data/Iterators/Basic.lean

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,21 @@ def IterStep.mapIterator {α' : Type u'} (f : α → α') : IterStep α β → I
183183
| .skip it => .skip (f it)
184184
| .done => .done
185185

186+
@[simp]
187+
theorem IterStep.mapIterator_yield {α' : Type u'} {f : α → α'} {it : α} {out : β} :
188+
(IterStep.yield it out).mapIterator f = IterStep.yield (f it) out :=
189+
rfl
190+
191+
@[simp]
192+
theorem IterStep.mapIterator_skip {α' : Type u'} {f : α → α'} {it : α} :
193+
(IterStep.skip it (β := β)).mapIterator f = IterStep.skip (f it) :=
194+
rfl
195+
196+
@[simp]
197+
theorem IterStep.mapIterator_done {α' : Type u'} {f : α → α'} :
198+
(IterStep.done (α := α) (β := β)).mapIterator f = IterStep.done :=
199+
rfl
200+
186201
@[simp]
187202
theorem IterStep.mapIterator_mapIterator {α' : Type u'} {α'' : Type u''}
188203
{f : α → α'} {g : α' → α''} {step : IterStep α β} :
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/-
2+
Copyright (c) 2025 Lean FRO, LLC. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Paul Reichert
5+
-/
6+
prelude
7+
import Std.Data.Iterators.Consumers
8+
import Std.Data.Iterators.Lemmas.Producers.Monadic.List
9+
10+
/-!
11+
# Lemmas about list iterators
12+
13+
This module provides lemmas about the interactions of `List.iter` with `Iter.step` and various
14+
collectors.
15+
-/
16+
17+
namespace Std.Iterators
18+
19+
variable {β : Type w}
20+
21+
theorem _root_.List.step_iter_nil :
22+
(([] : List β).iter).step = ⟨.done, rfl⟩ := by
23+
simp only [Iter.step, IterM.step, Iterator.step]; rfl
24+
25+
theorem _root_.List.step_iter_cons {x : β} {xs : List β} :
26+
((x :: xs).iter).step = ⟨.yield xs.iter x, rfl⟩ := by
27+
simp only [List.iter, List.iterM, IterM.step, Iterator.step]; rfl
28+
29+
theorem _root_.List.toArray_iter {m : Type w → Type w'} [Monad m] [LawfulMonad m] {l : List β} :
30+
(l.iterM m).toArray = pure l.toArray := by
31+
simp only [IterM.toArray, ListIterator.toArrayMapped_eq]
32+
rw [List.mapM_pure, map_pure, List.map_id']
33+
34+
theorem _root_.List.toList_iter {m : Type w → Type w'} [Monad m] [LawfulMonad m] {l : List β} :
35+
(l.iterM m).toList = pure l := by
36+
rw [← IterM.toList_toArray, List.toArray_iterM, map_pure, List.toList_toArray]
37+
38+
theorem _root_.List.toListRev_iter {m : Type w → Type w'} [Monad m] [LawfulMonad m] {l : List β} :
39+
(l.iterM m).toListRev = pure l.reverse := by
40+
simp [IterM.toListRev_eq, List.toList_iterM]
41+
42+
end Std.Iterators
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/-
2+
Copyright (c) 2025 Lean FRO, LLC. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Paul Reichert
5+
-/
6+
prelude
7+
import Std.Data.Iterators.Producers.Monadic.List
8+
import Std.Data.Iterators.Consumers
9+
import Std.Data.Iterators.Lemmas.Consumers.Monadic
10+
11+
/-!
12+
# Lemmas about list iterators
13+
14+
This module provides lemmas about the interactions of `List.iterM` with `IterM.step` and various
15+
collectors.
16+
-/
17+
18+
namespace Std.Iterators
19+
20+
variable {m : Type w → Type w'} [Monad m] {β : Type w}
21+
22+
theorem _root_.List.step_iterM_nil :
23+
(([] : List β).iterM m).step = pure ⟨.done, rfl⟩ := by
24+
simp only [IterM.step, Iterator.step]; rfl
25+
26+
theorem _root_.List.step_iterM_cons {x : β} {xs : List β} :
27+
((x :: xs).iterM m).step = pure ⟨.yield (xs.iterM m) x, rfl⟩ := by
28+
simp only [List.iterM, IterM.step, Iterator.step]; rfl
29+
30+
theorem ListIterator.toArrayMapped_eq [LawfulMonad m]
31+
{β : Type w} {γ : Type w} {f : β → m γ} {l : List β} :
32+
IteratorCollect.toArrayMapped f (l.iterM m) = List.toArray <$> l.mapM f := by
33+
rw [LawfulIteratorCollect.toArrayMapped_eq]
34+
induction l with
35+
| nil =>
36+
rw [IterM.DefaultConsumers.toArrayMapped_of_step]
37+
simp [List.step_iterM_nil]
38+
| cons x xs ih =>
39+
rw [IterM.DefaultConsumers.toArrayMapped_of_step]
40+
simp [List.step_iterM_cons, List.mapM_cons, pure_bind, ih]
41+
42+
theorem _root_.List.toArray_iterM [LawfulMonad m] {l : List β} :
43+
(l.iterM m).toArray = pure l.toArray := by
44+
simp only [IterM.toArray, ListIterator.toArrayMapped_eq]
45+
rw [List.mapM_pure, map_pure, List.map_id']
46+
47+
theorem _root_.List.toList_iterM [LawfulMonad m] {l : List β} :
48+
(l.iterM m).toList = pure l := by
49+
rw [← IterM.toList_toArray, List.toArray_iterM, map_pure, List.toList_toArray]
50+
51+
theorem _root_.List.toListRev_iterM [LawfulMonad m] {l : List β} :
52+
(l.iterM m).toListRev = pure l.reverse := by
53+
simp [IterM.toListRev_eq, List.toList_iterM]
54+
55+
end Std.Iterators

0 commit comments

Comments
 (0)