Documentation

Init.Data.Option.Basic

instance Option.instBEq {α✝ : Type u_1} [BEq α✝] :
BEq (Option α✝)
Equations
def Option.getM {m : Type u_1 → Type u_2} {α : Type u_1} [Alternative m] :
Option αm α

Lifts an optional value to any Alternative, sending none to failure.

Equations
Instances For
    @[inline]
    def Option.isSome {α : Type u_1} :
    Option αBool

    Returns true on some x and false on none.

    Equations
    Instances For
      @[simp]
      @[simp]
      theorem Option.isSome_some {α✝ : Type u_1} {a : α✝} :
      @[inline]
      def Option.isNone {α : Type u_1} :
      Option αBool

      Returns true on none and false on some x.

      This function is more flexible than (· == none) because it does not require a BEq α instance.

      Examples:

      Equations
      Instances For
        @[simp]
        theorem Option.isNone_none {α : Type u_1} :
        @[simp]
        theorem Option.isNone_some {α✝ : Type u_1} {a : α✝} :
        @[inline]
        def Option.isEqSome {α : Type u_1} [BEq α] :
        Option ααBool

        Checks whether an optional value is both present and equal to some other value.

        Given x? : Option α and y : α, x?.isEqSome y is equivalent to x? == some y. It is more efficient because it avoids an allocation.

        Equations
        Instances For
          @[inline]
          def Option.bind {α : Type u_1} {β : Type u_2} :
          Option α(αOption β)Option β

          Sequencing of Option computations.

          From the perspective of Option as computations that might fail, this function sequences potentially-failing computations, failing if either fails. From the perspective of Option as a collection with at most one element, the function is applied to the element if present, and the final result is empty if either the initial or the resulting collections are empty.

          This function is often accessed via the >>= operator from the Bind (Option α) instance, or implicitly via do-notation, but it is also idiomatic to call it using generalized field notation.

          Examples:

          Equations
          Instances For
            @[inline]
            def Option.bindM {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_1} [Monad m] (f : αm (Option β)) (o : Option α) :
            m (Option β)

            Runs the monadic action f on o's value, if any, and returns the result, or none if there is no value.

            From the perspective of Option as a collection with at most one element, the monadic the function is applied to the element if present, and the final result is empty if either the initial or the resulting collections are empty.

            Equations
            Instances For
              @[inline]
              def Option.mapM {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_1} [Monad m] (f : αm β) (o : Option α) :
              m (Option β)

              Runs a monadic function f on an optional value, returning the result. If the optional value is none, the function is not called and the result is also none.

              From the perspective of Option as a container with at most one element, this is analogous to List.mapM, returning the result of running the monadic function on all elements of the container.

              Option.mapA is the corresponding operation for applicative functors.

              Equations
              Instances For
                @[inline]
                def Option.filter {α : Type u_1} (p : αBool) :
                Option αOption α

                Keeps an optional value only if it satisfies a Boolean predicate.

                If Option is thought of as a collection that contains at most one element, then Option.filter is analogous to List.filter or Array.filter.

                Examples:

                Equations
                Instances For
                  @[inline]
                  def Option.all {α : Type u_1} (p : αBool) :
                  Option αBool

                  Checks whether an optional value either satisfies a Boolean predicate or is none.

                  Examples:

                  • `(some 33).all (· % 2 == 0) = false
                  • `(some 22).all (· % 2 == 0) = true
                  • `none.all (fun x : Nat => x % 2 == 0) = true
                  Equations
                  Instances For
                    @[inline]
                    def Option.any {α : Type u_1} (p : αBool) :
                    Option αBool

                    Checks whether an optional value is not none and satisfies a Boolean predicate.

                    Examples:

                    • `(some 33).any (· % 2 == 0) = false
                    • `(some 22).any (· % 2 == 0) = true
                    • `none.any (fun x : Nat => true) = false
                    Equations
                    Instances For
                      @[macro_inline]
                      def Option.orElse {α : Type u_1} :
                      Option α(UnitOption α)Option α

                      Implementation of OrElse's <|> syntax for Option. If the first argument is some a, returns some a, otherwise evaluates and returns the second argument.

                      See also or for a version that is strict in the second argument.

                      Equations
                      Instances For
                        instance Option.instOrElse {α : Type u_1} :
                        Equations
                        @[macro_inline]
                        def Option.or {α : Type u_1} :
                        Option αOption αOption α

                        Returns the first of its arguments that is some, or none if neither is some.

                        This is similar to the <|> operator, also known as OrElse.orElse, but both arguments are always evaluated without short-circuiting.

                        Equations
                        Instances For
                          @[inline]
                          def Option.lt {α : Type u_1} {β : Type u_2} (r : αβProp) :
                          Option αOption βProp

                          Lifts an ordering relation to Option, such that none is the least element.

                          It can be understood as adding a distinguished least element, represented by none, to both α and β.

                          This definition is part of the implementation of the LT (Option α) instance. However, because it can be used with heterogeneous relations, it is sometimes useful on its own.

                          Examples:

                          Equations
                          Instances For
                            @[inline]
                            def Option.le {α : Type u_1} {β : Type u_2} (r : αβProp) :
                            Option αOption βProp
                            Equations
                            Instances For
                              def Option.merge {α : Type u_1} (fn : ααα) :
                              Option αOption αOption α

                              Applies a function to a two optional values if both are present. Otherwise, if one value is present, it is returned and the function is not used.

                              The value is some (fn a b) if the inputs are some a and some b. Otherwise, the behavior is equivalent to Option.orElse: if only one input is some x, then the value is some x, and if both are none, then the value is none.

                              Examples:

                              Equations
                              Instances For
                                @[simp]
                                theorem Option.getD_none {α✝ : Type u_1} {a : α✝} :
                                none.getD a = a
                                @[simp]
                                theorem Option.getD_some {α✝ : Type u_1} {a b : α✝} :
                                (some a).getD b = a
                                @[simp]
                                theorem Option.map_none' {α : Type u_1} {β : Type u_2} (f : αβ) :
                                @[simp]
                                theorem Option.map_some' {α : Type u_1} {β : Type u_2} (a : α) (f : αβ) :
                                Option.map f (some a) = some (f a)
                                @[simp]
                                theorem Option.none_bind {α : Type u_1} {β : Type u_2} (f : αOption β) :
                                @[simp]
                                theorem Option.some_bind {α : Type u_1} {β : Type u_2} (a : α) (f : αOption β) :
                                (some a).bind f = f a
                                @[inline]
                                def Option.elim {α : Type u_1} {β : Sort u_2} :
                                Option αβ(αβ)β

                                A case analysis function for Option.

                                Given a value for none and a function to apply to the contents of some, Option.elim checks which constructor a given Option consists of, and uses the appropriate argument.

                                Option.elim is an elimination principle for Option. In particular, it is a non-dependent version of Option.recOn. It can also be seen as a combination of Option.map and Option.getD.

                                Examples:

                                Equations
                                Instances For
                                  @[inline]
                                  def Option.get {α : Type u} (o : Option α) :
                                  o.isSome = trueα

                                  Extracts the value from an option that can be proven to be some.

                                  Equations
                                  Instances For
                                    @[simp]
                                    theorem Option.some_get {α : Type u_1} {x : Option α} (h : x.isSome = true) :
                                    some (x.get h) = x
                                    @[simp]
                                    theorem Option.get_some {α : Type u_1} (x : α) (h : (some x).isSome = true) :
                                    (some x).get h = x
                                    @[inline]
                                    def Option.guard {α : Type u_1} (p : αProp) [DecidablePred p] (a : α) :

                                    Returns none if a value doesn't satisfy a predicate, or the value itself otherwise.

                                    From the perspective of Option as computations that might fail, this function is a run-time assertion operator in the Option monad.

                                    Examples:

                                    Equations
                                    Instances For
                                      @[inline]
                                      def Option.toList {α : Type u_1} :
                                      Option αList α

                                      Converts an optional value to a list with zero or one element.

                                      Examples:

                                      Equations
                                      Instances For
                                        @[inline]
                                        def Option.toArray {α : Type u_1} :
                                        Option αArray α

                                        Converts an optional value to an array with zero or one element.

                                        Examples:

                                        Equations
                                        Instances For
                                          def Option.liftOrGet {α : Type u_1} (f : ααα) :
                                          Option αOption αOption α

                                          Applies a function to a two optional values if both are present. Otherwise, if one value is present, it is returned and the function is not used.

                                          The value is some (f a b) if the inputs are some a and some b. Otherwise, the behavior is equivalent to Option.orElse. If only one input is some x, then the value is some x. If both are none, then the value is none.

                                          Examples:

                                          Equations
                                          Instances For
                                            inductive Option.Rel {α : Type u_1} {β : Type u_2} (r : αβProp) :
                                            Option αOption βProp

                                            Lifts a relation α → β → Prop to a relation Option α → Option β → Prop by just adding none ~ none.

                                            Instances For
                                              @[inline]
                                              def Option.join {α : Type u_1} (x : Option (Option α)) :

                                              Flattens nested optional values, preserving any value found.

                                              This is analogous to List.flatten.

                                              Examples:

                                              Equations
                                              Instances For
                                                @[inline]
                                                def Option.mapA {m : Type u_1 → Type u_2} [Applicative m] {α : Type u_3} {β : Type u_1} (f : αm β) :
                                                Option αm (Option β)

                                                Applies a function in some applicative functor to an optional value, returning none with no effects if the value is missing.

                                                This is analogous to Option.mapM for monads.

                                                Equations
                                                Instances For
                                                  @[inline]
                                                  def Option.sequence {m : Type u → Type u_1} [Monad m] {α : Type u} :
                                                  Option (m α)m (Option α)

                                                  Converts an optional monadic computation into a monadic computation of an optional value.

                                                  Example:

                                                  #eval show IO (Option String) from
                                                    Option.sequence <| some do
                                                      IO.println "hello"
                                                      return "world"
                                                  
                                                  hello
                                                  
                                                  some "world"
                                                  
                                                  Equations
                                                  Instances For
                                                    @[inline]
                                                    def Option.elimM {m : Type u_1 → Type u_2} {α β : Type u_1} [Monad m] (x : m (Option α)) (y : m β) (z : αm β) :
                                                    m β

                                                    A monadic case analysis function for Option.

                                                    Given a fallback computation for none and a monadic operation to apply to the contents of some, Option.elimM checks which constructor a given Option consists of, and uses the appropriate argument.

                                                    Option.elimM can also be seen as a combination of Option.mapM and Option.getDM. It is a monadic analogue of Option.elim.

                                                    Equations
                                                    Instances For
                                                      @[inline]
                                                      def Option.getDM {m : Type u_1 → Type u_2} {α : Type u_1} [Monad m] (x : Option α) (y : m α) :
                                                      m α

                                                      Gets the value in an option, monadically computing a default value on none.

                                                      This is the monadic analogue of Option.getD.

                                                      Equations
                                                      Instances For
                                                        instance Option.instLawfulBEq (α : Type u_1) [BEq α] [LawfulBEq α] :
                                                        @[simp]
                                                        theorem Option.all_none {α✝ : Type u_1} {p : α✝Bool} :
                                                        @[simp]
                                                        theorem Option.all_some {α✝ : Type u_1} {p : α✝Bool} {x : α✝} :
                                                        Option.all p (some x) = p x
                                                        @[simp]
                                                        theorem Option.any_none {α✝ : Type u_1} {p : α✝Bool} :
                                                        @[simp]
                                                        theorem Option.any_some {α✝ : Type u_1} {p : α✝Bool} {x : α✝} :
                                                        Option.any p (some x) = p x
                                                        def Option.min {α : Type u_1} [Min α] :
                                                        Option αOption αOption α

                                                        The minimum of two optional values, with none treated as the least element. This function is usually accessed through the Min (Option α) instance, rather than directly.

                                                        Prior to nightly-2025-02-27, none was treated as the greatest element, so min none (some x) = min (some x) none = some x.

                                                        Examples:

                                                        Equations
                                                        Instances For
                                                          instance Option.instMin {α : Type u_1} [Min α] :
                                                          Min (Option α)
                                                          Equations
                                                          @[simp]
                                                          theorem Option.min_some_some {α : Type u_1} [Min α] {a b : α} :
                                                          min (some a) (some b) = some (min a b)
                                                          @[simp]
                                                          theorem Option.min_some_none {α : Type u_1} [Min α] {a : α} :
                                                          @[simp]
                                                          theorem Option.min_none_some {α : Type u_1} [Min α] {b : α} :
                                                          @[simp]
                                                          theorem Option.min_none_none {α : Type u_1} [Min α] :
                                                          def Option.max {α : Type u_1} [Max α] :
                                                          Option αOption αOption α

                                                          The maximum of two optional values.

                                                          This function is usually accessed through the Max (Option α) instance, rather than directly.

                                                          Examples:

                                                          Equations
                                                          Instances For
                                                            instance Option.instMax {α : Type u_1} [Max α] :
                                                            Max (Option α)
                                                            Equations
                                                            @[simp]
                                                            theorem Option.max_some_some {α : Type u_1} [Max α] {a b : α} :
                                                            max (some a) (some b) = some (max a b)
                                                            @[simp]
                                                            theorem Option.max_some_none {α : Type u_1} [Max α] {a : α} :
                                                            @[simp]
                                                            theorem Option.max_none_some {α : Type u_1} [Max α] {b : α} :
                                                            @[simp]
                                                            theorem Option.max_none_none {α : Type u_1} [Max α] :
                                                            instance instLTOption {α : Type u_1} [LT α] :
                                                            LT (Option α)
                                                            Equations
                                                            instance instLEOption {α : Type u_1} [LE α] :
                                                            LE (Option α)
                                                            Equations
                                                            @[always_inline]
                                                            Equations
                                                            @[always_inline]
                                                            Equations
                                                            • One or more equations did not get rendered due to their size.
                                                            @[always_inline]
                                                            Equations
                                                            def liftOption {m : Type u_1 → Type u_2} {α : Type u_1} [Alternative m] :
                                                            Option αm α
                                                            Equations
                                                            Instances For
                                                              @[inline]
                                                              def Option.tryCatch {α : Type u_1} (x : Option α) (handle : UnitOption α) :

                                                              Recover from failing Option computations with a handler function.

                                                              This function is usually accessed through the MonadExceptOf Unit Option instance.

                                                              Examples:

                                                              Equations
                                                              Instances For
                                                                Equations