Documentation

Init.Data.Iterators.Consumers.Monadic.Collect

Collectors #

This module provides consumers that collect the values emitted by an iterator in a data structure. Concretely, the following operations are provided:

Some producers and combinators provide specialized implementations. These are captured by the IteratorCollect type class. They should be implemented by all types of iterators. A default implementation is provided. The typeclass LawfulIteratorCollect asserts that an IteratorCollect instance equals the default implementation.

class Std.Iterators.IteratorCollect (α : Type w) (m : Type w → Type w') (n : Type w → Type w'') {β : Type w} [Iterator α m β] :
Type (max (max (w + 1) w') w'')

IteratorCollect α m provides efficient implementations of collectors for α-based iterators. Right now, it is limited to a potentially optimized toArray implementation.

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.

Note: For this to be compositional enough to be useful, toArrayMapped would need to accept a termination proof for the specific mapping function used instead of the blanket Finite α m instance. Otherwise, most combinators like map cannot implement their own instance relying on the instance of their base iterators. However, fixing this is currently low priority.

  • toArrayMapped (lift : δ : Type w⦄ → m δn δ) {γ : Type w} : (βn γ)IterM m βn (Array γ)

    Maps the emitted values of an iterator using the given function and collects the results in an Array. This is an internal implementation detail. Consider using it.map f |>.toArray instead.

Instances
    def Std.Iterators.IterM.DefaultConsumers.toArrayMapped.RecursionRel {α β : Type w} {m : Type w → Type w'} [Iterator α m β] {γ : Type w} (x' x : (_ : IterM m β) ×' Array γ) :
    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      @[always_inline]
      def Std.Iterators.IterM.DefaultConsumers.toArrayMapped {α β : Type w} {m : Type w → Type w'} {n : Type w → Type w''} [Monad n] [Iterator α m β] (lift : α : Type w⦄ → m αn α) {γ : Type w} (f : βn γ) (it : IterM m β) :
      n (Array γ)

      This is an internal function used in IteratorCollect.defaultImplementation.

      It iterates over an iterator and applies f whenever a value is emitted before inserting the result of f into an array.

      Equations
      Instances For
        @[always_inline]
        def Std.Iterators.IteratorCollect.defaultImplementation {α β : Type w} {m : Type w → Type w'} {n : Type w → Type w''} [Monad n] [Iterator α m β] :

        This is the default implementation of the IteratorCollect class. It simply iterates through the iterator using IterM.step, incrementally building up the desired data structure. For certain iterators, more efficient implementations are possible and should be used instead.

        Equations
        Instances For
          class Std.Iterators.LawfulIteratorCollect (α : Type w) (m : Type w → Type w') (n : Type w → Type w'') {β : Type w} [Monad m] [Monad n] [Iterator α m β] [i : IteratorCollect α m n] :

          Asserts that a given IteratorCollect instance is equal to IteratorCollect.defaultImplementation if the underlying iterator is finite. (Even though equal, the given instance might be vastly more efficient.)

          Instances
            theorem Std.Iterators.LawfulIteratorCollect.toArrayMapped_eq {α β γ : Type w} {m : Type w → Type w'} {n : Type w → Type w''} [Monad m] [Monad n] [Iterator α m β] [Finite α m] [IteratorCollect α m n] [hl : LawfulIteratorCollect α m n] {lift : δ : Type w⦄ → m δn δ} [Internal.LawfulMonadLiftFunction lift] {f : βn γ} {it : IterM m β} :
            instance Std.Iterators.instLawfulIteratorCollectOfIteratorOfFinite (α β : Type w) (m : Type w → Type w') (n : Type w → Type w'') [Monad n] [Iterator α m β] [Monad m] [Iterator α m β] [Finite α m] :
            @[inline]
            def Std.Iterators.IterM.toArray {α β : Type w} {m : Type w → Type w'} [Monad m] [Iterator α m β] [IteratorCollect α m m] (it : IterM m β) :
            m (Array β)

            Traverses the given iterator and stores the emitted values in an array.

            If the iterator is not finite, this function might run forever. The variant it.ensureTermination.toArray always terminates after finitely many steps.

            Equations
            Instances For
              @[inline, deprecated Std.Iterators.IterM.toArray (since := "2025-10-23")]
              def Std.Iterators.IterM.Partial.toArray {α : Type w} {m : Type w → Type w'} {β : Type w} [Monad m] [Iterator α m β] (it : Partial m β) [IteratorCollect α m m] :
              m (Array β)

              Traverses the given iterator and stores the emitted values in an array.

              This function is deprecated. Instead of it.allowNontermination.toArray, use it.toArray.

              Equations
              Instances For
                @[inline]
                def Std.Iterators.IterM.Total.toArray {α : Type w} {m : Type w → Type w'} {β : Type w} [Monad m] [Iterator α m β] [Finite α m] (it : Total m β) [IteratorCollect α m m] :
                m (Array β)

                Traverses the given iterator and stores the emitted values in an array.

                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.toArray.

                Equations
                Instances For
                  @[inline]
                  def Std.Iterators.IterM.toListRev {α : Type w} {m : Type w → Type w'} [Monad m] {β : Type w} [Iterator α m β] (it : IterM m β) :
                  m (List β)

                  Traverses the given iterator and stores the emitted values in reverse order in a list. Because lists are prepend-only, this toListRev is usually more efficient that toList.

                  If the iterator is not finite, this function might run forever. The variant it.ensureTermination.toListRev always terminates after finitely many steps.

                  Equations
                  Instances For
                    @[inline]
                    def Std.Iterators.IterM.toListRev.go {α : Type w} {m : Type w → Type w'} [Monad m] {β : Type w} [Iterator α m β] (it : IterM m β) (acc : List β) :
                    m (List β)
                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For
                      @[inline, deprecated Std.Iterators.IterM.toListRev (since := "2025-10-23")]
                      def Std.Iterators.IterM.Partial.toListRev {α : Type w} {m : Type w → Type w'} [Monad m] {β : Type w} [Iterator α m β] (it : Partial m β) :
                      m (List β)

                      Traverses the given iterator and stores the emitted values in reverse order in a list. Because lists are prepend-only, this toListRev is usually more efficient that toList.

                      This function is deprecated. Instead of it.allowNontermination.toListRev, use it.toListRev.

                      Equations
                      Instances For
                        @[inline]
                        def Std.Iterators.IterM.Total.toListRev {α : Type w} {m : Type w → Type w'} {β : Type w} [Monad m] [Iterator α m β] [Finite α m] (it : Total m β) :
                        m (List β)

                        Traverses the given iterator and stores the emitted values in reverse order in a list. Because lists are prepend-only, this toListRev is usually more efficient that toList.

                        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.toListRev.

                        Equations
                        Instances For
                          @[inline]
                          def Std.Iterators.IterM.toList {α : Type w} {m : Type w → Type w'} [Monad m] {β : Type w} [Iterator α m β] [IteratorCollect α m m] (it : IterM m β) :
                          m (List β)

                          Traverses the given iterator and stores the emitted values in a list. Because lists are prepend-only, toListRev is usually more efficient that toList.

                          If the iterator is not finite, this function might run forever. The variant it.ensureTermination.toList always terminates after finitely many steps.

                          Equations
                          Instances For
                            @[inline, deprecated Std.Iterators.IterM.toList (since := "2025-10-23")]
                            def Std.Iterators.IterM.Partial.toList {α : Type w} {m : Type w → Type w'} [Monad m] {β : Type w} [Iterator α m β] (it : Partial m β) [IteratorCollect α m m] :
                            m (List β)

                            Traverses the given iterator and stores the emitted values in a list. Because lists are prepend-only, toListRev is usually more efficient that toList.

                            This function is deprecated. Instead of it.allowNontermination.toList, use it.toList.

                            Equations
                            Instances For
                              @[inline]
                              def Std.Iterators.IterM.Total.toList {α : Type w} {m : Type w → Type w'} {β : Type w} [Monad m] [Iterator α m β] [Finite α m] (it : Total m β) [IteratorCollect α m m] :
                              m (List β)

                              Traverses the given iterator and stores the emitted values in a list. Because lists are prepend-only, toListRev is usually more efficient that toList.

                              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.toList.

                              Equations
                              Instances For