Skip to content

Commit 73fba0b

Browse files
committed
add tests
1 parent 9a75271 commit 73fba0b

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

src/Std/Data/Iterators/Lemmas/Consumers/Loop.lean

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,13 @@ theorem Iter.fold_eq_foldM {α β γ : Type w} [Iterator α Id β]
138138
it.fold (init := init) f = (it.foldM (m := Id) (init := init) (pure <| f · ·)).run := by
139139
simp [foldM_eq_forIn, fold_eq_forIn]
140140

141-
theorem Iter.forIn_yield_eq_fold {α β γ : Type w} [Iterator α Id β]
141+
@[simp]
142+
theorem Iter.forIn_pure_yield_eq_fold {α β γ : Type w} [Iterator α Id β]
142143
[Finite α Id] [IteratorLoop α Id Id]
143144
[LawfulIteratorLoop α Id Id] {f : β → γ → γ} {init : γ}
144145
{it : Iter (α := α) β} :
145-
ForIn.forIn (m := Id) it init (fun c b => .yield (f c b)) =
146-
it.fold (fun b c => f c b) init := by
146+
ForIn.forIn (m := Id) it init (fun c b => pure (.yield (f c b))) =
147+
pure (it.fold (fun b c => f c b) init) := by
147148
simp only [fold_eq_forIn]
148149
rfl
149150

src/Std/Data/Iterators/Lemmas/Consumers/Monadic/Loop.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ theorem IterM.fold_eq_foldM {α β γ : Type w} {m : Type w → Type w'} [Iterat
105105
it.fold (init := init) f = it.foldM (init := init) (pure <| f · ·) := by
106106
simp [foldM_eq_forIn, fold_eq_forIn]
107107

108+
@[simp]
108109
theorem IterM.forIn_pure_yield_eq_fold {α β γ : Type w} {m : Type w → Type w'} [Iterator α m β]
109110
[Finite α m] [Monad m] [LawfulMonad m] [IteratorLoop α m m]
110111
[LawfulIteratorLoop α m m] {f : β → γ → γ} {init : γ}

src/Std/Data/Iterators/Lemmas/Producers/List.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ theorem _root_.List.step_iter_cons {x : β} {xs : List β} :
3030
simp only [List.iter, List.iterM, IterM.step, Iterator.step]; rfl
3131

3232
@[simp]
33-
theorem _root_.List.toArray_iter {m : Type w → Type w'} [Monad m] [LawfulMonad m] {l : List β} :
33+
theorem _root_.List.toArray_iter {l : List β} :
3434
l.iter.toArray = l.toArray := by
3535
simp [List.iter, List.toArray_iterM, Iter.toArray_eq_toArray_toIterM]
3636

3737
@[simp]
38-
theorem _root_.List.toList_iter {m : Type w → Type w'} [Monad m] [LawfulMonad m] {l : List β} :
38+
theorem _root_.List.toList_iter {l : List β} :
3939
l.iter.toList = l := by
4040
simp [List.iter, List.toList_iterM]
4141

4242
@[simp]
43-
theorem _root_.List.toListRev_iter {m : Type w → Type w'} [Monad m] [LawfulMonad m] {l : List β} :
43+
theorem _root_.List.toListRev_iter {l : List β} :
4444
l.iter.toListRev = l.reverse := by
4545
simp [List.iter, Iter.toListRev_eq_toListRev_toIterM, List.toListRev_iterM]
4646

tests/lean/run/iterators.lean

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,32 @@ section ListIteratorBasic
3434
#guard_msgs in
3535
#eval [1, 2, 3].iterM IO |>.toArray
3636

37+
example : [1, 2, 3].iter.toList = [1, 2, 3] := by
38+
simp
39+
40+
example : [1, 2, 3].iter.toArray = #[1, 2, 3] := by
41+
simp
42+
43+
example : [1, 2, 3].iter.toListRev = [3, 2, 1] := by
44+
simp
45+
46+
-- This does not work for `IO` because `LawfulMonad IO` is in Batteries
47+
example : ([1, 2, 3].iterM (StateM Nat)).toList = pure [1, 2, 3] := by
48+
simp
49+
50+
example : ([1, 2, 3].iterM (StateM Nat)).toArray = pure #[1, 2, 3] := by
51+
simp
52+
53+
example : ([1, 2, 3].iterM (StateM Nat)).toListRev = pure [3, 2, 1] := by
54+
simp
55+
3756
end ListIteratorBasic
3857

3958
section WellFoundedRecursion
4059

4160
def sum (l : List Nat) : Nat :=
4261
go l.iter 0
4362
where
44-
@[specialize] -- The old code generator seems to need this.
4563
go it acc :=
4664
match it.step with
4765
| .yield it' out _ => go it' (acc + out)
@@ -64,4 +82,27 @@ def sumFold (l : List Nat) : Nat :=
6482
#guard_msgs in
6583
#eval [1, 2, 3].iter.fold (init := 0) (· + · )
6684

85+
example (l : List Nat) : l.iter.fold (init := 0) (· + ·) = l.sum := by
86+
simp [← Std.Iterators.Iter.foldl_toList, List.sum]
87+
-- It remains to show that List.foldl (· + ·) = List.foldr (· + ·)
88+
generalize 0 = init
89+
induction l generalizing init
90+
· rfl
91+
· rename_i x l ih
92+
simp only [List.foldl_cons, ih, List.foldr_cons]
93+
clear ih
94+
induction l generalizing x
95+
· simp only [List.foldr_nil]
96+
omega
97+
· simp only [List.foldr_cons, *]
98+
omega
99+
100+
example (l : List Nat) :
101+
(Id.run do
102+
let mut s := 0
103+
for x in l.iter do
104+
s := s + x
105+
return s) = l.iter.fold (init := 0) (· + ·) := by
106+
simp
107+
67108
end Loop

0 commit comments

Comments
 (0)