Skip to content

Commit 13c82fb

Browse files
committed
add tests
1 parent 27cc2fc commit 13c82fb

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

src/Std/Data/Iterators/Combinators/Monadic/Drop.lean

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ namespace Std.Iterators
1717

1818
variable {α : Type w} {m : Type w → Type w'} {β : Type w}
1919

20+
/--
21+
The internal state of the `IterM.drop` combinator.
22+
-/
2023
@[unbox]
2124
structure Drop (α : Type w) (m : Type w → Type w') (β : Type w) where
25+
/-- Internal implementation detail of the iterator library -/
2226
remaining : Nat
27+
/-- Internal implementation detail of the iterator library -/
2328
inner : IterM (α := α) m β
2429

2530
/--

src/Std/Data/Iterators/Lemmas/Combinators/Drop.lean

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ theorem Iter.atIdxSlow?_drop {α β}
4141
rw [atIdxSlow?.eq_def, atIdxSlow?.eq_def, step_drop]
4242
cases it.step using PlausibleIterStep.casesOn <;> simp [*]
4343

44+
@[simp]
4445
theorem Iter.toList_drop {α β} [Iterator α Id β] {n : Nat}
4546
[Finite α Id] [IteratorCollect α Id] [LawfulIteratorCollect α Id]
4647
{it : Iter (α := α) β} :
@@ -49,6 +50,7 @@ theorem Iter.toList_drop {α β} [Iterator α Id β] {n : Nat}
4950
simp only [getElem?_toList_eq_atIdxSlow?, List.getElem?_drop, atIdxSlow?_drop]
5051
rw [Nat.add_comm]
5152

53+
@[simp]
5254
theorem Iter.toListRev_drop {α β} [Iterator α Id β] {n : Nat}
5355
[Finite α Id] [IteratorCollect α Id] [LawfulIteratorCollect α Id]
5456
{it : Iter (α := α) β} :
@@ -57,18 +59,19 @@ theorem Iter.toListRev_drop {α β} [Iterator α Id β] {n : Nat}
5759

5860
-- TODO: Init.Data.Array.Lemmas
5961
theorem _root_.List.drop_toArray {l : List α} {k : Nat} :
60-
l.toArray.drop k = (l.drop k).toArray := by
62+
(l.drop k).toArray = l.toArray.extract k := by
6163
induction l generalizing k
6264
case nil => simp
6365
case cons l' ih =>
6466
match k with
6567
| 0 => simp
66-
| k' + 1 => simp [List.drop_succ_cons, ih]
68+
| k' + 1 => simp [List.drop_succ_cons, ih]
6769

70+
@[simp]
6871
theorem Iter.toArray_drop {α β} [Iterator α Id β] {n : Nat}
6972
[Finite α Id] [IteratorCollect α Id] [LawfulIteratorCollect α Id]
7073
{it : Iter (α := α) β} :
71-
(it.drop n).toArray = it.toArray.drop n := by
72-
rw [← toArray_toList, ← toArray_toList, List.drop_toArray, toList_drop]
74+
(it.drop n).toArray = it.toArray.extract n := by
75+
rw [← toArray_toList, ← toArray_toList, List.drop_toArray, toList_drop]
7376

7477
end Std.Iterators

tests/lean/run/iterators.lean

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,41 @@ example : ([1, 2, 3].iter.take 2).toListRev = [2, 1] := by
144144
simp
145145

146146
end Take
147+
148+
section Drop
149+
150+
def sumDropRec (l : List Nat) : Nat :=
151+
go (l.iter.drop 1) 0
152+
where
153+
go it acc :=
154+
match it.step with
155+
| .yield it' out _ => go it' (acc + out)
156+
| .skip it' _ => go it' acc
157+
| .done _ => acc
158+
termination_by it.finitelyManySteps
159+
160+
def sumDropFold (l : List Nat) : Nat :=
161+
l.iter.drop 1 |>.fold (init := 0) (· + ·)
162+
163+
/-- info: [2, 3] -/
164+
#guard_msgs in
165+
#eval [1, 2, 3].iter.drop 1 |>.toList
166+
167+
/-- info: 5 -/
168+
#guard_msgs in
169+
#eval sumDropRec [1, 2, 3]
170+
171+
/-- info: 5 -/
172+
#guard_msgs in
173+
#eval sumDropFold [1, 2, 3]
174+
175+
example : ([1, 2, 3].iter.drop 1).toList = [2, 3] := by
176+
simp
177+
178+
example : ([1, 2, 3].iter.drop 1).toArray = #[2, 3] := by
179+
simp
180+
181+
example : ([1, 2, 3].iter.drop 1).toListRev = [3, 2] := by
182+
simp
183+
184+
end Drop

0 commit comments

Comments
 (0)