Documentation

Init.Data.Iterators.Consumers.Loop

Loop 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:

These operations are implemented using the IteratorLoop and IteratorLoopPartial typeclasses.

@[inline]
def Std.Iterators.Iter.instForIn' {α β : Type w} {n : Type w → Type w'} [Monad n] [Iterator α Id β] [Finite α Id] [IteratorLoop α Id n] :
ForIn' n (Iter β) β { mem := fun (it : Iter β) (out : β) => it.IsPlausibleIndirectOutput out }

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
  • 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.
    instance Std.Iterators.instForMIterOfFiniteOfIteratorLoopId {m : Type w → Type w'} {α β : Type w} [Iterator α Id β] [Finite α Id] [IteratorLoop α Id m] :
    ForM m (Iter β) β
    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.
    @[inline]
    def Std.Iterators.Iter.foldM {m : Type w → Type w'} [Monad m] {α β γ : Type w} [Iterator α Id β] [Finite α Id] [IteratorLoop α Id m] (f : γβm γ) (init : γ) (it : Iter β) :
    m γ

    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.

    It is equivalent to it.toList.foldlM.

    This function requires a Finite instance proving that the iterator will finish after a finite number of steps. If the iterator is not finite or such an instance is not available, consider using it.allowNontermination.foldM instead of it.foldM. However, it is not possible to formally verify the behavior of the partial variant.

    Equations
    Instances For
      @[inline]
      def Std.Iterators.Iter.Partial.foldM {m : Type w → Type w'} [Monad m] {α β γ : Type w} [Iterator α Id β] [IteratorLoopPartial α Id m] (f : γβm γ) (init : γ) (it : Partial β) :
      m γ

      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.

      It is equivalent to it.toList.foldlM.

      This is a partial, potentially nonterminating, function. It is not possible to formally verify its behavior. If the iterator has a Finite instance, consider using IterM.foldM instead.

      Equations
      Instances For
        @[inline]
        def Std.Iterators.Iter.fold {α β γ : Type w} [Iterator α Id β] [Finite α Id] [IteratorLoop α Id Id] (f : γβγ) (init : γ) (it : Iter β) :
        γ

        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 requires a Finite instance proving that the iterator will finish after a finite number of steps. If the iterator is not finite or such an instance is not available, consider using it.allowNontermination.fold instead of it.fold. However, it is not possible to formally verify the behavior of the partial variant.

        Equations
        Instances For
          @[inline]
          def Std.Iterators.Iter.Partial.fold {α β γ : Type w} [Iterator α Id β] [IteratorLoopPartial α Id Id] (f : γβγ) (init : γ) (it : Partial β) :
          γ

          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 is a partial, potentially nonterminating, function. It is not possible to formally verify its behavior. If the iterator has a Finite instance, consider using IterM.fold instead.

          Equations
          Instances For
            @[inline]
            def Std.Iterators.Iter.size {α β : Type w} [Iterator α Id β] [IteratorSize α Id] (it : Iter β) :

            Computes how many elements the iterator returns. In monadic situations, it is unclear which effects are caused by calling size, and if the monad is nondeterministic, it is also unclear what the returned value should be. The reference implementation, IteratorSize.defaultImplementation, simply iterates over the whole iterator monadically, counting the number of emitted values. An IteratorSize instance is considered lawful if it is equal to the reference implementation.

            Performance:

            Default performance is linear in the number of steps taken by the iterator.

            Equations
            Instances For
              @[inline]

              Computes how many elements the iterator emits.

              With monadic iterators (IterM), it is unclear which effects are caused by calling size, and if the monad is nondeterministic, it is also unclear what the returned value should be. The reference implementation, IteratorSize.defaultImplementation, simply iterates over the whole iterator monadically, counting the number of emitted values. An IteratorSize instance is considered lawful if it is equal to the reference implementation.

              This is the partial version of size. It does not require a proof of finiteness and might loop forever. It is not possible to verify the behavior in Lean because it uses partial.

              Performance:

              Default performance is linear in the number of steps taken by the iterator.

              Equations
              Instances For
                class Std.Iterators.LawfulIteratorSize (α : Type w) {β : Type w} [Iterator α Id β] [Finite α Id] [IteratorSize α Id] :

                LawfulIteratorSize α m ensures that the size function of an iterator behaves as if it iterated over the whole iterator, counting its elements and causing all the monadic side effects of the iterations. This is a fairly strong condition for monadic iterators and it will be false for many efficient implementations of size that compute the size without actually iterating.

                This class is experimental and users of the iterator API should not explicitly depend on it.

                Instances