@@ -23,9 +23,9 @@ Concretely, the following operations are provided:
2323* `IterM.toArray`, collecting the values in an array
2424
2525 Some producers and combinators provide specialized implementations. These are captured by the
26- `IteratorCollect` and `IteratorCollectPartial` typeclasses . They should be implemented by all
27- types of iterators. A default implementation is provided. The typeclass `LawfulIteratorCollect`
28- asserts that an `IteratorCollect` instance equals the default implementation.
26+ `IteratorCollect` type class . They should be implemented by all types of iterators. A default
27+ implementation is provided. The typeclass `LawfulIteratorCollect` asserts that an `IteratorCollect `
28+ instance equals the default implementation.
2929-/
3030
3131namespace Std.Iterators
@@ -65,7 +65,7 @@ This is an internal function used in `IteratorCollect.defaultImplementation`.
6565It iterates over an iterator and applies `f` whenever a value is emitted before inserting the result
6666of `f` into an array.
6767-/
68- @[always_inline, inline, no_expose]
68+ @[always_inline, no_expose]
6969def IterM.DefaultConsumers.toArrayMapped {α β : Type w} {m : Type w → Type w'}
7070 {n : Type w → Type w''} [Monad n] [Iterator α m β]
7171 (lift : ⦃α : Type w⦄ → m α → n α) {γ : Type w} (f : β → n γ)
@@ -88,7 +88,7 @@ It simply iterates through the iterator using `IterM.step`, incrementally buildi
8888data structure. For certain iterators, more efficient implementations are possible and should be
8989used instead.
9090-/
91- @[always_inline, inline ]
91+ @[always_inline]
9292def IteratorCollect.defaultImplementation {α β : Type w} {m : Type w → Type w'}
9393 {n : Type w → Type w''} [Monad n] [Iterator α m β] :
9494 IteratorCollect α m n where
@@ -124,7 +124,7 @@ instance (α β : Type w) (m : Type w → Type w') (n : Type w → Type w'') [Mo
124124/--
125125Traverses the given iterator and stores the emitted values in an array.
126126-/
127- @[always_inline, inline ]
127+ @[always_inline]
128128def IterM.toArray {α β : Type w} {m : Type w → Type w'} [Monad m] [Iterator α m β]
129129 [IteratorCollect α m m] (it : IterM (α := α) m β) : m (Array β) :=
130130 IteratorCollect.toArrayMapped (fun ⦃_⦄ => id) pure it
@@ -134,59 +134,46 @@ Traverses the given iterator and stores the emitted values in an array.
134134
135135This function is deprecated. Instead of `it.allowNontermination.toArray`, use `it.toArray`.
136136-/
137- @[always_inline, inline, deprecated IterM.toArray (since := "2025-10-15")]
137+ @[always_inline, deprecated IterM.toArray (since := "2025-10-15")]
138138def IterM.Partial.toArray {α : Type w} {m : Type w → Type w'} {β : Type w} [Monad m]
139139 [Iterator α m β] (it : IterM.Partial (α := α) m β) [IteratorCollect α m m] : m (Array β) :=
140140 it.it.toArray
141141
142142end ToArray
143143
144- -- TODO:
145144/--
146145Traverses the given iterator and stores the emitted values in reverse order in a list. Because
147146lists are prepend-only, this `toListRev` is usually more efficient that `toList`.
148-
149- This function requires a `Finite` instance proving that the iterator will finish after a finite
150- number of steps. If the iterator is not finite or such an instance is not available, consider using
151- `it.allowNontermination.toListRev` instead of `it.toListRev`. However, it is not possible to
152- formally verify the behavior of the partial variant.
153147-/
154- @[inline ]
148+ @[always_inline ]
155149def IterM.toListRev {α : Type w} {m : Type w → Type w'} [Monad m] {β : Type w}
156- [Iterator α m β] [Finite α m] (it : IterM (α := α) m β) : m (List β) :=
150+ [Iterator α m β] (it : IterM (α := α) m β) : m (List β) :=
157151 go it []
158152where
159- go [Finite α m] it bs := do
160- match (← it.step).inflate with
161- | .yield it' b _ => go it' (b :: bs)
162- | .skip it' _ => go it' bs
163- | .done _ => return bs
164- termination_by it.finitelyManySteps
153+ @[always_inline]
154+ go (it : IterM m β) acc :=
155+ extrinsicFix₂ (fun it acc recur => do
156+ match (← it.step).inflate with
157+ | .yield it' out _ => recur it' (out :: acc)
158+ | .skip it' _ => recur it' acc
159+ | .done _ => return acc) it acc
165160
166161/--
167162Traverses the given iterator and stores the emitted values in reverse order in a list. Because
168163lists are prepend-only, this `toListRev` is usually more efficient that `toList`.
169164
170- This is a partial, potentially nonterminating, function. It is not possible to formally verify
171- its behavior. If the iterator has a `Finite` instance, consider using `IterM.toListRev` instead.
165+ This function is deprecated. Instead of `it.allowNontermination.toListRev`, use `it.toListRev`.
172166-/
173- @[always_inline, inline ]
167+ @[always_inline, deprecated IterM.toListRev (since := "2025-10-16") ]
174168partial def IterM.Partial.toListRev {α : Type w} {m : Type w → Type w'} [Monad m] {β : Type w}
175169 [Iterator α m β] (it : IterM.Partial (α := α) m β) : m (List β) :=
176- go it.it []
177- where
178- @[specialize]
179- go it bs := do
180- match (← it.step).inflate with
181- | .yield it' b _ => go it' (b :: bs)
182- | .skip it' _ => go it' bs
183- | .done _ => return bs
170+ it.it.toListRev
184171
185172/--
186173Traverses the given iterator and stores the emitted values in a list. Because
187174lists are prepend-only, `toListRev` is usually more efficient that `toList`.
188175-/
189- @[always_inline, inline ]
176+ @[always_inline]
190177def IterM.toList {α : Type w} {m : Type w → Type w'} [Monad m] {β : Type w}
191178 [Iterator α m β] [IteratorCollect α m m] (it : IterM (α := α) m β) : m (List β) :=
192179 Array.toList <$> IterM.toArray it
@@ -197,7 +184,7 @@ lists are prepend-only, `toListRev` is usually more efficient that `toList`.
197184
198185This function is deprecated. Instead of `it.allowNontermination.toList`, use `it.toList`.
199186-/
200- @[always_inline, inline, deprecated IterM.toList (since := "2025-10-15")]
187+ @[always_inline, deprecated IterM.toList (since := "2025-10-15")]
201188def IterM.Partial.toList {α : Type w} {m : Type w → Type w'} [Monad m] {β : Type w}
202189 [Iterator α m β] (it : IterM.Partial (α := α) m β) [IteratorCollect α m m] :
203190 m (List β) :=
0 commit comments