Documentation

Hale.Base.Data.List.NonEmpty

structure Data.List.NonEmpty (α : Type u) :

A non-empty list. Stores head : α and tail : List α, ensuring at least one element.

Unlike { l : List α // l ≠ [] }, this representation provides $O(1)$ head access without proof elimination. The canonical form is: $$\text{NonEmpty}(\alpha) \cong \alpha \times \text{List}(\alpha)$$

  • head : α

    The first element, accessible in $O(1)$.

  • tail : List α

    The remaining elements (possibly empty).

Instances For
    @[implicit_reducible]
    instance Data.List.instBEqNonEmpty {α✝ : Type u_1} [BEq α✝] :
    BEq (NonEmpty α✝)
    Equations
    def Data.List.instBEqNonEmpty.beq {α✝ : Type u_1} [BEq α✝] :
    NonEmpty α✝NonEmpty α✝Bool
    Equations
    Instances For
      def Data.List.instOrdNonEmpty.ord {α✝ : Type u_1} [Ord α✝] :
      NonEmpty α✝NonEmpty α✝Ordering
      Equations
      Instances For
        @[implicit_reducible]
        instance Data.List.instOrdNonEmpty {α✝ : Type u_1} [Ord α✝] :
        Ord (NonEmpty α✝)
        Equations
        @[implicit_reducible]
        instance Data.List.instReprNonEmpty {α✝ : Type u_1} [Repr α✝] :
        Repr (NonEmpty α✝)
        Equations
        def Data.List.instReprNonEmpty.repr {α✝ : Type u_1} [Repr α✝] :
        NonEmpty α✝NatStd.Format
        Equations
        • One or more equations did not get rendered due to their size.
        Instances For
          def Data.List.instHashableNonEmpty.hash {α✝ : Type u_1} [Hashable α✝] :
          NonEmpty α✝UInt64
          Equations
          Instances For
            @[inline]
            def Data.List.NonEmpty.singleton {α : Type u_1} (x : α) :

            Construct a singleton non-empty list. $$\text{singleton}(x) = [x]$$

            Equations
            Instances For
              @[inline]
              def Data.List.NonEmpty.cons {α : Type u_1} (x : α) (xs : NonEmpty α) :

              Construct from head and tail. $$\text{cons}(x, xs) = x :: xs$$

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

                Convert to a standard List. Always non-empty. $$\text{toList}(x, xs) = x :: xs$$

                Equations
                Instances For
                  def Data.List.NonEmpty.last {α : Type u_1} (ne : NonEmpty α) :
                  α

                  The last element. Total — no Option needed. For a non-empty list $[x_1, \ldots, x_n]$ where $n \geq 1$: $$\text{last}([x_1, \ldots, x_n]) = x_n$$

                  Runs in $O(n)$ time.

                  Equations
                  Instances For
                    def Data.List.NonEmpty.length {α : Type u_1} (ne : NonEmpty α) :
                    { n : Nat // n 1 }

                    The length of a non-empty list, guaranteed $\geq 1$. $$|\text{NonEmpty}| = 1 + |\text{tail}|$$

                    Returns a subtype proving the result is at least 1.

                    Equations
                    Instances For
                      def Data.List.NonEmpty.append {α : Type u_1} (xs ys : NonEmpty α) :

                      Append a non-empty list to another. Result is non-empty. $$[x_1, \ldots, x_m] \mathbin{+\!\!+} [y_1, \ldots, y_n] = [x_1, \ldots, x_m, y_1, \ldots, y_n]$$

                      Equations
                      Instances For
                        @[implicit_reducible]
                        Equations
                        def Data.List.NonEmpty.map {α : Type u_1} {β : Type u_2} (f : αβ) (ne : NonEmpty α) :

                        Map a function over every element. $$\text{map}(f, [x_1, \ldots, x_n]) = [f(x_1), \ldots, f(x_n)]$$

                        Equations
                        Instances For
                          def Data.List.NonEmpty.reverse {α : Type u_1} (ne : NonEmpty α) :

                          Reverse the non-empty list. $$\text{reverse}([x_1, \ldots, x_n]) = [x_n, \ldots, x_1]$$

                          Equations
                          Instances For
                            def Data.List.NonEmpty.foldr {α : Type u_1} {β : Type u_2} (f : αββ) (init : β) (ne : NonEmpty α) :
                            β

                            Right fold over the non-empty list. $$\text{foldr}(f, z, [x_1, \ldots, x_n]) = f(x_1, f(x_2, \ldots f(x_n, z)))$$

                            Equations
                            Instances For
                              def Data.List.NonEmpty.foldr1 {α : Type u_1} (f : ααα) (ne : NonEmpty α) :
                              α

                              Right fold without an initial value (uses the last element). $$\text{foldr1}(f, [x_1, \ldots, x_n]) = f(x_1, f(x_2, \ldots f(x_{n-1}, x_n)))$$

                              Equations
                              Instances For
                                def Data.List.NonEmpty.foldr1.go {α : Type u_1} (f : ααα) (x : α) :
                                List αα
                                Equations
                                Instances For
                                  def Data.List.NonEmpty.foldl1 {α : Type u_1} (f : ααα) (ne : NonEmpty α) :
                                  α

                                  Left fold without an initial value. $$\text{foldl1}(f, [x_1, \ldots, x_n]) = f(\ldots f(f(x_1, x_2), x_3) \ldots, x_n)$$

                                  Equations
                                  Instances For

                                    Construct from a list, if non-empty. Returns some iff the input list is non-nil.

                                    Equations
                                    Instances For
                                      def Data.List.NonEmpty.fromList {α : Type u_1} (l : List α) (h : l []) :

                                      Construct from a list with proof of non-emptiness.

                                      Equations
                                      Instances For
                                        theorem Data.List.NonEmpty.toList_ne_nil {α : Type u_1} (ne : NonEmpty α) :

                                        Converting to a list always yields a non-nil list.

                                        The length of a reversed non-empty list equals the original length.

                                        theorem Data.List.NonEmpty.toList_length {α : Type u_1} (ne : NonEmpty α) :

                                        toList preserves length.

                                        theorem Data.List.NonEmpty.map_length {α : Type u_1} {β : Type u_2} (f : αβ) (ne : NonEmpty α) :

                                        map preserves length.

                                        @[implicit_reducible]
                                        Equations
                                        @[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]
                                        Equations