Documentation

Hale.Base.Data.Foldable

class Data.Foldable (F : Type u → Type v) :
Type (max (u + 1) v)

Foldable captures the pattern of folding a structure into a single value.

For a Foldable container $F$: $$\text{foldr}(f, z, [x_1, \ldots, x_n]) = f(x_1, f(x_2, \ldots f(x_n, z)))$$ $$\text{foldl}(f, z, [x_1, \ldots, x_n]) = f(\ldots f(f(z, x_1), x_2) \ldots, x_n)$$

  • foldr {α β : Type u} : (αββ)βF αβ

    Right fold: $\text{foldr}(f, z, t) = f(x_1, f(x_2, \ldots f(x_n, z)))$.

  • foldl {β α : Type u} : (βαβ)βF αβ

    Left fold: $\text{foldl}(f, z, t) = f(\ldots f(f(z, x_1), x_2) \ldots, x_n)$.

  • toList {α : Type u} : F αList α

    Convert to a list, preserving order.

Instances
    @[inline]
    def Data.Foldable.foldMap {F : Type u_1 → Type u_2} {β α : Type u_1} [Foldable F] [Append β] [Inhabited β] (f : αβ) (t : F α) :
    β

    Map each element to a monoid and combine. $$\text{foldMap}(f, [x_1, \ldots, x_n]) = f(x_1) \mathbin{++} f(x_2) \mathbin{++} \cdots \mathbin{++} f(x_n)$$

    Uses Append and starts from mempty.

    Equations
    Instances For
      @[inline]
      def Data.Foldable.null {F : TypeType u_1} {α : Type} [Foldable F] (t : F α) :

      Is the structure empty? $$\text{null}(t) \iff |t| = 0$$

      Equations
      Instances For
        @[inline]
        def Data.Foldable.length {F : TypeType u_1} {α : Type} [Foldable F] (t : F α) :

        Count of elements. $$\text{length}(t) = |t|$$

        Equations
        Instances For
          @[inline]
          def Data.Foldable.any {F : TypeType u_1} {α : Type} [Foldable F] (p : αBool) (t : F α) :

          Does any element satisfy the predicate? $$\text{any}(p, t) \iff \exists x \in t,\; p(x) = \text{true}$$

          Equations
          Instances For
            @[inline]
            def Data.Foldable.all {F : TypeType u_1} {α : Type} [Foldable F] (p : αBool) (t : F α) :

            Do all elements satisfy the predicate? $$\text{all}(p, t) \iff \forall x \in t,\; p(x) = \text{true}$$

            Equations
            Instances For
              @[inline]
              def Data.Foldable.find? {F : Type u_1 → Type u_2} {α : Type u_1} [Foldable F] (p : αBool) (t : F α) :

              Find the first element satisfying a predicate.

              Equations
              Instances For
                @[inline]
                def Data.Foldable.elem {F : TypeType u_1} {α : Type} [Foldable F] [BEq α] (a : α) (t : F α) :

                Is the element in the structure? $$\text{elem}(x, t) \iff \exists y \in t,\; x = y$$

                Equations
                Instances For
                  @[inline]
                  def Data.Foldable.minimum? {F : Type u_1 → Type u_2} {α : Type u_1} [Foldable F] [Min α] (t : F α) :

                  The minimum element, if the structure is non-empty.

                  Equations
                  Instances For
                    @[inline]
                    def Data.Foldable.maximum? {F : Type u_1 → Type u_2} {α : Type u_1} [Foldable F] [Max α] (t : F α) :

                    The maximum element, if the structure is non-empty.

                    Equations
                    Instances For
                      @[inline]
                      def Data.Foldable.sum {F : Type u_1 → Type u_2} {α : Type u_1} [Foldable F] [Add α] [OfNat α 0] (t : F α) :
                      α

                      Sum of all elements. $$\text{sum}(t) = \sum_{x \in t} x$$

                      Equations
                      Instances For
                        @[inline]
                        def Data.Foldable.product {F : Type u_1 → Type u_2} {α : Type u_1} [Foldable F] [Mul α] [OfNat α 1] (t : F α) :
                        α

                        Product of all elements. $$\text{product}(t) = \prod_{x \in t} x$$

                        Equations
                        Instances For
                          @[inline]
                          def Data.Foldable.minimum1 {α : Type u_1} [Ord α] (ne : List.NonEmpty α) :
                          α

                          Total minimum on a non-empty structure. No Option needed. $$\text{minimum1}([x_1, \ldots, x_n]) = \min(x_1, \ldots, x_n)$$

                          Equations
                          Instances For
                            @[inline]
                            def Data.Foldable.maximum1 {α : Type u_1} [Ord α] (ne : List.NonEmpty α) :
                            α

                            Total maximum on a non-empty structure. No Option needed. $$\text{maximum1}([x_1, \ldots, x_n]) = \max(x_1, \ldots, x_n)$$

                            Equations
                            Instances For
                              @[implicit_reducible]
                              Equations
                              @[implicit_reducible]
                              Equations
                              • One or more equations did not get rendered due to their size.
                              @[implicit_reducible]
                              Equations
                              • One or more equations did not get rendered due to their size.
                              @[implicit_reducible]
                              instance Data.instFoldableEither {α : Type u_1} :
                              Equations
                              • One or more equations did not get rendered due to their size.
                              theorem Data.foldr_nil {α β : Type u_1} {f : αββ} {z : β} :

                              foldr on an empty list is the initial accumulator.

                              theorem Data.foldl_nil {β α : Type u_1} {f : βαβ} {z : β} :

                              foldl on an empty list is the initial accumulator.