Loop-based consumers #
This module provides consumers that iterate over a given iterator, applying a certain user-supplied function in every iteration. Concretely, the following operations are provided:
ForIninstancesIterM.fold, the analogue ofList.foldlIterM.foldM, the analogue ofList.foldlMIterM.drain, which iterates over the whole iterator and discards all emitted values. It can be used to apply the monadic effects of the iterator.
Some producers and combinators provide specialized implementations. These are captured by the
IteratorLoop type class. They should be implemented by all
types of iterators. A default implementation is provided. The typeclass LawfulIteratorLoop
asserts that an IteratorLoop instance equals the default implementation.
Relation that needs to be well-formed in order for a loop over an iterator to terminate.
It is assumed that the plausible_forInStep predicate relates the input and output of the
stepper function.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Asserts that IteratorLoop.rel is well-founded.
Equations
- Std.Iterators.IteratorLoop.WellFounded α m plausible_forInStep = WellFounded (Std.Iterators.IteratorLoop.rel α m plausible_forInStep)
Instances For
IteratorLoop α m provides efficient implementations of loop-based consumers for α-based
iterators. The basis is a ForIn-style loop construct.
Its behavior for well-founded loops is fully characterized by the LawfulIteratorLoop type class.
This class is experimental and users of the iterator API should not explicitly depend on it. They can, however, assume that consumers that require an instance will work for all iterators provided by the standard library.
Instances
Internal implementation detail of the iterator library.
- it : IterM m β
- acc : γ
Instances For
Equations
- One or more equations did not get rendered due to their size.
This is the loop implementation of the default instance IteratorLoop.defaultImplementation.
Equations
- One or more equations did not get rendered due to their size.
Instances For
This is the loop implementation of the default instance IteratorLoop.defaultImplementation.
Equations
- One or more equations did not get rendered due to their size.
Instances For
This is the default implementation of the IteratorLoop class.
It simply iterates through the iterator using IterM.step. For certain iterators, more efficient
implementations are possible and should be used instead.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Asserts that a given IteratorLoop instance is equal to IteratorLoop.defaultImplementation.
(Even though equal, the given instance might be vastly more efficient.)
- lawful (lift : (γ : Type w) → (δ : Type x) → (γ → n δ) → m γ → n δ) [Internal.LawfulMonadLiftBindFunction lift] (γ : Type x) (it : IterM m β) (init : γ) (Pl : β → γ → ForInStep γ → Prop) (wf : IteratorLoop.WellFounded α m Pl) (f : (b : β) → it.IsPlausibleIndirectOutput b → (c : γ) → n (Subtype (Pl b c))) : IteratorLoop.forIn lift γ Pl it init f = IteratorLoop.forIn lift γ Pl it init f
Instances
This ForIn'-style loop construct traverses a finite iterator using an IteratorLoop instance.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A ForIn' instance for iterators. Its generic membership relation is not easy to use,
so this is not marked as instance. This way, more convenient instances can be built on top of it
or future library improvements will make it more comfortable.
Equations
- Std.Iterators.IterM.instForIn' = Std.Iterators.IteratorLoop.finiteForIn' fun (x x_1 : Type ?u.68) (f : x → n x_1) (x_2 : m x) => monadLift x_2 >>= f
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Equations
- One or more equations did not get rendered due to their size.
Folds a monadic function over an iterator from the left, accumulating a value starting with init.
The accumulated value is combined with the each element of the list in order, using f.
The monadic effects of f are interleaved with potential effects caused by the iterator's step
function. Therefore, it may not be equivalent to (← it.toList).foldlM.
Equations
- Std.Iterators.IterM.foldM f init it = forIn it init fun (x : β) (acc : γ) => ForInStep.yield <$> f acc x
Instances For
Folds a monadic function over an iterator from the left, accumulating a value starting with init.
The accumulated value is combined with the each element of the list in order, using f.
The monadic effects of f are interleaved with potential effects caused by the iterator's step
function. Therefore, it may not be equivalent to it.toList.foldlM.
This function is deprecated. Instead of it.allowNontermination.foldM, use it.foldM.
Equations
- Std.Iterators.IterM.Partial.foldM f init it = forIn it init fun (x : β) (acc : γ) => ForInStep.yield <$> f acc x
Instances For
Folds a monadic function over an iterator from the left, accumulating a value starting with init.
The accumulated value is combined with the each element of the list in order, using f.
The monadic effects of f are interleaved with potential effects caused by the iterator's step
function. Therefore, it may not be equivalent to it.toList.foldlM.
This variant terminates after finitely many steps and requires a proof that the iterator is
finite. If such a proof is not available, consider using IterM.foldM.
Equations
- Std.Iterators.IterM.Total.foldM f init it = Std.Iterators.IterM.foldM f init it.it
Instances For
Folds a function over an iterator from the left, accumulating a value starting with init.
The accumulated value is combined with the each element of the list in order, using f.
It is equivalent to it.toList.foldl.
Equations
- Std.Iterators.IterM.fold f init it = forIn it init fun (x : β) (acc : γ) => pure (ForInStep.yield (f acc x))
Instances For
Folds a function over an iterator from the left, accumulating a value starting with init.
The accumulated value is combined with the each element of the list in order, using f.
It is equivalent to it.toList.foldl.
This function is deprecated. Instead of it.allowNontermination.fold, use it.fold.
Equations
- Std.Iterators.IterM.Partial.fold f init it = forIn it init fun (x : β) (acc : γ) => pure (ForInStep.yield (f acc x))
Instances For
Folds a function over an iterator from the left, accumulating a value starting with init.
The accumulated value is combined with the each element of the list in order, using f.
It is equivalent to it.toList.foldl.
This variant terminates after finitely many steps and requires a proof that the iterator is
finite. If such a proof is not available, consider using IterM.fold.
Equations
- Std.Iterators.IterM.Total.fold f init it = Std.Iterators.IterM.fold f init it.it
Instances For
Iterates over the whole iterator, applying the monadic effects of each step, discarding all emitted values.
Equations
- it.drain = Std.Iterators.IterM.fold (fun (x : PUnit) (x_1 : β) => PUnit.unit) PUnit.unit it
Instances For
Iterates over the whole iterator, applying the monadic effects of each step, discarding all emitted values.
This function is deprecated. Instead of it.allowNontermination.drain, use it.drain.
Equations
- it.drain = Std.Iterators.IterM.fold (fun (x : PUnit) (x_1 : β) => PUnit.unit) PUnit.unit it.it
Instances For
Iterates over the whole iterator, applying the monadic effects of each step, discarding all emitted values.
This variant terminates after finitely many steps and requires a proof that the iterator is
finite. If such a proof is not available, consider using IterM.drain.
Instances For
Returns ULift.up true if the monadic predicate p returns ULift.up true for
any element emitted by the iterator it.
O(|it|). Short-circuits upon encountering the first match. The outputs of it are
examined in order of iteration.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Returns ULift.up true if the monadic predicate p returns ULift.up true for
any element emitted by the iterator it.
O(|it|). Short-circuits upon encountering the first match. The outputs of it are
examined in order of iteration.
This function is deprecated. Instead of it.allowNontermination.anyM, use it.anyM.
Equations
Instances For
Returns ULift.up true if the monadic predicate p returns ULift.up true for
any element emitted by the iterator it.
O(|it|). Short-circuits upon encountering the first match. The outputs of it are
examined in order of iteration.
This variant terminates after finitely many steps and requires a proof that the iterator is
finite. If such a proof is not available, consider using IterM.anyM.
Equations
Instances For
Returns ULift.up true if the pure predicate p returns true for
any element emitted by the iterator it.
O(|it|). Short-circuits upon encountering the first match. The outputs of it are
examined in order of iteration.
Equations
- Std.Iterators.IterM.any p it = Std.Iterators.IterM.anyM (fun (x : β) => pure { down := p x }) it
Instances For
Returns ULift.up true if the pure predicate p returns true for
any element emitted by the iterator it.
O(|it|). Short-circuits upon encountering the first match. The outputs of it are
examined in order of iteration.
This function is deprecated. Instead of it.allowNontermination.any, use it.any.
Equations
Instances For
Returns ULift.up true if the pure predicate p returns true for
any element emitted by the iterator it.
O(|it|). Short-circuits upon encountering the first match. The outputs of it are
examined in order of iteration.
This variant terminates after finitely many steps and requires a proof that the iterator is
finite. If such a proof is not available, consider using IterM.any.
Equations
Instances For
Returns ULift.up true if the monadic predicate p returns ULift.up true for
all elements emitted by the iterator it.
O(|it|). Short-circuits upon encountering the first mismatch. The outputs of it are
examined in order of iteration.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Returns ULift.up true if the monadic predicate p returns ULift.up true for
all elements emitted by the iterator it.
O(|it|). Short-circuits upon encountering the first mismatch. The outputs of it are
examined in order of iteration.
This function is deprecated. Instead of it.allowNontermination.allM, use it.allM.
Equations
Instances For
Returns ULift.up true if the monadic predicate p returns ULift.up true for
all elements emitted by the iterator it.
O(|it|). Short-circuits upon encountering the first mismatch. The outputs of it are
examined in order of iteration.
This variant terminates after finitely many steps and requires a proof that the iterator is
finite. If such a proof is not available, consider using IterM.allM.
Equations
Instances For
Returns ULift.up true if the pure predicate p returns true for
all elements emitted by the iterator it.
O(|it|). Short-circuits upon encountering the first mismatch. The outputs of it are
examined in order of iteration.
If the iterator is not finite, this function might run forever. The variant
it.ensureTermination.toListRev always terminates after finitely many steps.
Equations
- Std.Iterators.IterM.all p it = Std.Iterators.IterM.allM (fun (x : β) => pure { down := p x }) it
Instances For
Returns ULift.up true if the pure predicate p returns true for
all elements emitted by the iterator it.
O(|it|). Short-circuits upon encountering the first mismatch. The outputs of it are
examined in order of iteration.
This function is deprecated. Instead of it.allowNontermination.allM, use it.allM.
Equations
Instances For
Returns ULift.up true if the pure predicate p returns true for
all elements emitted by the iterator it.
O(|it|). Short-circuits upon encountering the first mismatch. The outputs of it are
examined in order of iteration.
This variant terminates after finitely many steps and requires a proof that the iterator is
finite. If such a proof is not available, consider using IterM.all.
Equations
Instances For
Returns the first non-none result of applying the monadic function f to each output
of the iterator, in order. Returns none if f returns none for all outputs.
O(|it|). Short-circuits when f returns some _. The outputs of it are
examined in order of iteration.
If the iterator is not finite, this function might run forever. The variant
it.ensureTermination.findSomeM? always terminates after finitely many steps.
Example:
#eval ([7, 6, 5, 8, 1, 2, 6].iterM IO).findSomeM? fun i => do
if i < 5 then
return some (i * 10)
if i ≤ 6 then
IO.println s!"Almost! {i}"
return none
Almost! 6
Almost! 5
some 10
Equations
- One or more equations did not get rendered due to their size.
Instances For
Returns the first non-none result of applying the monadic function f to each output
of the iterator, in order. Returns none if f returns none for all outputs.
O(|it|). Short-circuits when f returns some _. The outputs of it are
examined in order of iteration.
This function is deprecated. Instead of it.allowNontermination.findSomeM?, use it.findSomeM?.
Example:
#eval ([7, 6, 5, 8, 1, 2, 6].iterM IO).findSomeM? fun i => do
if i < 5 then
return some (i * 10)
if i ≤ 6 then
IO.println s!"Almost! {i}"
return none
Almost! 6
Almost! 5
some 10
Equations
- it.findSomeM? f = it.it.findSomeM? f
Instances For
Returns the first non-none result of applying the monadic function f to each output
of the iterator, in order. Returns none if f returns none for all outputs.
O(|it|). Short-circuits when f returns some _. The outputs of it are
examined in order of iteration.
This variant terminates after finitely many steps and requires a proof that the iterator is
finite. If such a proof is not available, consider using IterM.findSomeM?.
Example:
#eval ([7, 6, 5, 8, 1, 2, 6].iterM IO).findSomeM? fun i => do
if i < 5 then
return some (i * 10)
if i ≤ 6 then
IO.println s!"Almost! {i}"
return none
Almost! 6
Almost! 5
some 10
Equations
- it.findSomeM? f = it.it.findSomeM? f
Instances For
Returns the first non-none result of applying f to each output of the iterator, in order.
Returns none if f returns none for all outputs.
O(|it|). Short-circuits when f returns some _.The outputs of it are examined in order of
iteration.
If the iterator is not finite, this function might run forever. The variant
it.ensureTermination.findSome? always terminates after finitely many steps.
Examples:
([7, 6, 5, 8, 1, 2, 6].iterM Id).findSome? (fun x => if x < 5 then some (10 * x) else none) = pure (some 10)([7, 6, 5, 8, 1, 2, 6].iterM Id).findSome? (fun x => if x < 1 then some (10 * x) else none) = pure none
Equations
- it.findSome? f = it.findSomeM? fun (x : β) => pure (f x)
Instances For
Returns the first non-none result of applying f to each output of the iterator, in order.
Returns none if f returns none for all outputs.
O(|it|). Short-circuits when f returns some _.The outputs of it are examined in order of
iteration.
This function is deprecated. Instead of it.allowNontermination.findSome?, use it.findSome?.
Examples:
([7, 6, 5, 8, 1, 2, 6].iterM Id).allowNontermination.findSome? (fun x => if x < 5 then some (10 * x) else none) = pure (some 10)([7, 6, 5, 8, 1, 2, 6].iterM Id).allowNontermination.findSome? (fun x => if x < 1 then some (10 * x) else none) = pure none
Instances For
Returns the first non-none result of applying f to each output of the iterator, in order.
Returns none if f returns none for all outputs.
O(|it|). Short-circuits when f returns some _.The outputs of it are examined in order of
iteration.
This variant terminates after finitely many steps and requires a proof that the iterator is
finite. If such a proof is not available, consider using IterM.findSome?.
Examples:
([7, 6, 5, 8, 1, 2, 6].iterM Id).ensureTermination.findSome? (fun x => if x < 5 then some (10 * x) else none) = pure (some 10)([7, 6, 5, 8, 1, 2, 6].iterM Id).ensureTermination.findSome? (fun x => if x < 1 then some (10 * x) else none) = pure none
Equations
- Std.Iterators.IterM.Total.findSome? it f = it.it.findSome? f
Instances For
Returns the first output of the iterator for which the monadic predicate p returns true, or
none if no such element is found.
O(|it|). Short-circuits when f returns true. The outputs of it are examined in order of
iteration.
If the iterator is not finite, this function might run forever. The variant
it.ensureTermination.findM? always terminates after finitely many steps.
Example:
#eval ([7, 6, 5, 8, 1, 2, 6].iterM IO).findM? fun i => do
if i < 5 then
return true
if i ≤ 6 then
IO.println s!"Almost! {i}"
return false
Almost! 6
Almost! 5
some 1
Equations
Instances For
Returns the first output of the iterator for which the monadic predicate p returns true, or
none if no such element is found.
O(|it|). Short-circuits when f returns true. The outputs of it are examined in order of
iteration.
This function is deprecated. Instead of it.allowNontermination.findM?, use it.findM?.
Example:
#eval ([7, 6, 5, 8, 1, 2, 6].iterM IO).findM? fun i => do
if i < 5 then
return true
if i ≤ 6 then
IO.println s!"Almost! {i}"
return false
Almost! 6
Almost! 5
some 1
Instances For
Returns the first output of the iterator for which the monadic predicate p returns true, or
none if no such element is found.
O(|it|). Short-circuits when f returns true. The outputs of it are examined in order of
iteration.
This variant requires terminates after finitely many steps and requires a proof that the iterator is
finite. If such a proof is not available, consider using IterM.findM?.
Example:
#eval ([7, 6, 5, 8, 1, 2, 6].iterM IO).findM? fun i => do
if i < 5 then
return true
if i ≤ 6 then
IO.println s!"Almost! {i}"
return false
Almost! 6
Almost! 5
some 1
Instances For
Returns the first output of the iterator for which the predicate p returns true, or none if
no such output is found.
O(|it|). Short-circuits upon encountering the first match. The elements in it are examined in
order of iteration.
If the iterator is not finite, this function might run forever. The variant
it.ensureTermination.find? always terminates after finitely many steps.
Examples:
([7, 6, 5, 8, 1, 2, 6].iterM Id).find? (· < 5) = pure (some 1)([7, 6, 5, 8, 1, 2, 6].iterM Id).find? (· < 1) = pure none
Instances For
Returns the first output of the iterator for which the predicate p returns true, or none if
no such output is found.
O(|it|). Short-circuits upon encountering the first match. The elements in it are examined in
order of iteration.
This function is deprecated. Instead of it.allowNontermination.find?, use it.find?.
Examples:
([7, 6, 5, 8, 1, 2, 6].iterM Id).allowNontermination.find? (· < 5) = pure (some 1)([7, 6, 5, 8, 1, 2, 6].iterM Id).allowNontermination.find? (· < 1) = pure none
Instances For
Returns the first output of the iterator for which the predicate p returns true, or none if
no such output is found.
O(|it|). Short-circuits upon encountering the first match. The elements in it are examined in
order of iteration.
This variant terminates after finitely many steps and requires a proof that the iterator is
finite. If such a proof is not available, consider using IterM.find?.
Examples:
([7, 6, 5, 8, 1, 2, 6].iterM Id).find? (· < 5) = pure (some 1)([7, 6, 5, 8, 1, 2, 6].iterM Id).find? (· < 1) = pure none
Instances For
Steps through the whole iterator, counting the number of outputs emitted.
Performance:
This function's runtime is linear in the number of steps taken by the iterator.
Equations
Instances For
Steps through the whole iterator, counting the number of outputs emitted.
Performance:
This function's runtime is linear in the number of steps taken by the iterator.
Instances For
Steps through the whole iterator, counting the number of outputs emitted.
Performance:
This function's runtime is linear in the number of steps taken by the iterator.
Equations
Instances For
Steps through the whole iterator, counting the number of outputs emitted.
Performance:
This function's runtime is linear in the number of steps taken by the iterator.