Documentation

Hale.Base.Data.Either

inductive Data.Either (α : Type u) (β : Type v) :
Type (max u v)

Either α β is a sum type: either Left α or Right β.

Right-biased: Functor, Monad act on the Right case. $$\text{Either}(\alpha, \beta) \cong \alpha + \beta$$

  • left {α : Type u} {β : Type v} : αEither α β

    The left case, typically representing an error or alternative.

  • right {α : Type u} {β : Type v} : βEither α β

    The right case, typically representing success.

Instances For
    @[implicit_reducible]
    instance Data.instBEqEither {α✝ : Type u_1} {β✝ : Type u_2} [BEq α✝] [BEq β✝] :
    BEq (Either α✝ β✝)
    Equations
    def Data.instBEqEither.beq {α✝ : Type u_1} {β✝ : Type u_2} [BEq α✝] [BEq β✝] :
    Either α✝ β✝Either α✝ β✝Bool
    Equations
    Instances For
      @[implicit_reducible]
      instance Data.instOrdEither {α✝ : Type u_1} {β✝ : Type u_2} [Ord α✝] [Ord β✝] :
      Ord (Either α✝ β✝)
      Equations
      @[implicit_reducible]
      instance Data.instReprEither {α✝ : Type u_1} {β✝ : Type u_2} [Repr α✝] [Repr β✝] :
      Repr (Either α✝ β✝)
      Equations
      def Data.instReprEither.repr {α✝ : Type u_1} {β✝ : Type u_2} [Repr α✝] [Repr β✝] :
      Either α✝ β✝NatStd.Format
      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        @[implicit_reducible]
        instance Data.instHashableEither {α✝ : Type u_1} {β✝ : Type u_2} [Hashable α✝] [Hashable β✝] :
        Hashable (Either α✝ β✝)
        Equations
        @[inline]
        def Data.Either.isLeft {α : Type u_1} {β : Type u_2} :
        Either α βBool

        Test if a value is Left. $$\text{isLeft}(x) = \begin{cases} \text{true} & \text{if } x = \text{Left}(a) \\ \text{false} & \text{if } x = \text{Right}(b) \end{cases}$$

        Equations
        Instances For
          @[inline]
          def Data.Either.isRight {α : Type u_1} {β : Type u_2} :
          Either α βBool

          Test if a value is Right. $$\text{isRight}(x) = \neg\,\text{isLeft}(x)$$

          Equations
          Instances For
            @[inline]
            def Data.Either.fromLeft {α : Type u_1} {β : Type u_2} (default : α) :
            Either α βα

            Extract from Left, or return a default. $$\text{fromLeft}(d, \text{Left}(a)) = a, \quad \text{fromLeft}(d, \text{Right}(b)) = d$$

            Equations
            Instances For
              @[inline]
              def Data.Either.fromRight {β : Type u_1} {α : Type u_2} (default : β) :
              Either α ββ

              Extract from Right, or return a default. $$\text{fromRight}(d, \text{Right}(b)) = b, \quad \text{fromRight}(d, \text{Left}(a)) = d$$

              Equations
              Instances For
                @[inline]
                def Data.Either.either {α : Type u_1} {γ : Sort u_2} {β : Type u_3} (f : αγ) (g : βγ) :
                Either α βγ

                Case analysis: apply f to Left, g to Right. $$\text{either}(f, g, \text{Left}(a)) = f(a), \quad \text{either}(f, g, \text{Right}(b)) = g(b)$$

                Equations
                Instances For
                  @[inline]
                  def Data.Either.mapLeft {α : Type u_1} {γ : Type u_2} {β : Type u_3} (f : αγ) :
                  Either α βEither γ β

                  Map over the left component. $$\text{mapLeft}(f, \text{Left}(a)) = \text{Left}(f(a))$$

                  Equations
                  Instances For
                    @[inline]
                    def Data.Either.mapRight {β : Type u_1} {γ : Type u_2} {α : Type u_3} (f : βγ) :
                    Either α βEither α γ

                    Map over the right component. $$\text{mapRight}(f, \text{Right}(b)) = \text{Right}(f(b))$$

                    Equations
                    Instances For
                      @[inline]
                      def Data.Either.swap {α : Type u_1} {β : Type u_2} :
                      Either α βEither β α

                      Swap Left and Right. $$\text{swap}(\text{Left}(a)) = \text{Right}(a), \quad \text{swap}(\text{Right}(b)) = \text{Left}(b)$$

                      Equations
                      Instances For
                        def Data.Either.partitionEithers {α : Type u_1} {β : Type u_2} (l : List (Either α β)) :
                        List α × List β

                        Partition a list of Either into lefts and rights.

                        Given $[e_1, \ldots, e_n]$, produces $(ls, rs)$ where $ls$ are the Left values and $rs$ are the Right values.

                        Length preservation: $|ls| + |rs| = n$

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          theorem Data.Either.swap_swap {α : Type u_1} {β : Type u_2} (e : Either α β) :
                          e.swap.swap = e

                          Swapping twice is identity.

                          theorem Data.Either.isLeft_not_isRight {α : Type u_1} {β : Type u_2} (e : Either α β) :

                          isLeft and isRight are complementary.

                          theorem Data.Either.partitionEithers_length {α : Type u_1} {β : Type u_2} (l : List (Either α β)) :
                          match partitionEithers l with | (ls, rs) => ls.length + rs.length = l.length

                          Partition preserves total count: $$|\text{fst}(\text{partitionEithers}(l))| + |\text{snd}(\text{partitionEithers}(l))| = |l|$$

                          theorem Data.Either.map_id {α : Type u_1} {β : Type u_2} (e : Either α β) :

                          Identity law: map id = id for Either.

                          theorem Data.Either.map_comp {γ : Type u_1} {δ : Type u_2} {β : Type u_3} {α : Type u_4} (f : γδ) (g : βγ) (e : Either α β) :
                          mapRight (f g) e = mapRight f (mapRight g e)

                          Composition law: map (f ∘ g) = map f ∘ map g for Either.

                          @[implicit_reducible]
                          instance Data.Either.instFunctor {α : Type u_1} :
                          Equations
                          @[implicit_reducible]
                          instance Data.Either.instPure {α : Type u_1} :
                          Equations
                          @[implicit_reducible]
                          instance Data.Either.instBind {α : Type u_1} :
                          Equations
                          • One or more equations did not get rendered due to their size.
                          @[implicit_reducible]
                          instance Data.Either.instSeq {α : Type u_1} :
                          Seq (Either α)
                          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.Either.instMonad {α : Type u_1} :
                          Equations
                          theorem Data.Either.pure_bind {β : Type u_1} {α : Type u_2} {γ : Type u_1} (a : β) (f : βEither α γ) :
                          right a >>= f = f a

                          Left identity: pure a >>= f = f a.

                          theorem Data.Either.bind_pure {α : Type u_1} {β : Type u_2} (e : Either α β) :
                          e >>= right = e

                          Right identity: m >>= pure = m.

                          theorem Data.Either.bind_assoc {α : Type u_1} {β γ δ : Type u_2} (e : Either α β) (f : βEither α γ) (g : γEither α δ) :
                          e >>= f >>= g = e >>= fun (x : β) => f x >>= g

                          Associativity: (m >>= f) >>= g = m >>= (λ x → f x >>= g).

                          @[implicit_reducible]
                          Equations
                          • One or more equations did not get rendered due to their size.
                          @[implicit_reducible]
                          instance Data.Either.instToString {α : Type u_1} {β : Type u_2} [ToString α] [ToString β] :
                          Equations
                          • One or more equations did not get rendered due to their size.