lean4-htt/tests/lean/run/iterators.lean
Paul Reichert f4ee72b18c
feat: minimal iterator library (#8358)
This PR introduces a very minimal version of the new iterator library.
It comes with list iterators and various consumers, namely `toArray`,
`toList`, `toListRev`, `ForIn`, `fold`, `foldM` and `drain`. All
consumers also come in a partial variant that can be used without any
proofs. This limited version of the iterator library generates decent
code, even with the old code generator.
2025-05-20 14:53:57 +00:00

67 lines
1.3 KiB
Text

import Std.Data.Iterators
section ListIteratorBasic
/-- info: [1, 2, 3].iter : Std.Iter Nat -/
#guard_msgs in
#check [1, 2, 3].iter
/-- info: [1, 2, 3].iterM Id : Std.IterM Id Nat -/
#guard_msgs in
#check [1, 2, 3].iterM Id
/-- info: [1, 2, 3] -/
#guard_msgs in
#eval [1, 2, 3].iter.toList
/-- info: #[1, 2, 3] -/
#guard_msgs in
#eval [1, 2, 3].iter.toArray
/-- info: [1, 2, 3] -/
#guard_msgs in
#eval [1, 2, 3].iter |>.allowNontermination.toList
/-- info: ([1, 2, 3].iterM IO).toList : IO (List Nat) -/
#guard_msgs in
#check [1, 2, 3].iterM IO |>.toList
/-- info: [1, 2, 3] -/
#guard_msgs in
#eval [1, 2, 3].iterM IO |>.toList
/-- info: #[1, 2, 3] -/
#guard_msgs in
#eval [1, 2, 3].iterM IO |>.toArray
end ListIteratorBasic
section WellFoundedRecursion
def sum (l : List Nat) : Nat :=
go l.iter 0
where
@[specialize] -- The old code generator seems to need this.
go it acc :=
match it.step with
| .yield it' out _ => go it' (acc + out)
| .skip it' _ => go it' acc
| .done _ => acc
termination_by it.finitelyManySteps
/-- info: 6 -/
#guard_msgs in
#eval sum [1, 2, 3]
end WellFoundedRecursion
section Loop
def sumFold (l : List Nat) : Nat :=
l.iter.fold (init := 0) (· + ·)
/-- info: 6 -/
#guard_msgs in
#eval [1, 2, 3].iter.fold (init := 0) (· + · )
end Loop