Documentation

Mathlib.Lean.Expr.Basic

Additional operations on Expr and related types #

This file defines basic operations on the types expr, name, declaration, level, environment.

This file is mostly for non-tactics.

Declarations about BinderInfo #

def Lean.BinderInfo.brackets :
BinderInfo โ†’ String ร— String

The brackets corresponding to a given BinderInfo.

Instances For

    Declarations about name #

    @[specialize #[]]
    def Lean.Name.mapPrefix (f : Name โ†’ Option Name) (n : Name) :
    Name

    Find the largest prefix n of a Name such that f n != none, then replace this prefix with the value of f n.

    Instances For
      def Lean.Name.fromComponents :
      List Name โ†’ Name

      Build a name from components. For example, from_components [`foo, `bar] becomes `foo.bar. It is the inverse of Name.components on list of names that have single components.

      Instances For
        def Lean.Name.updateLast (f : String โ†’ String) :
        Name โ†’ Name

        Update the last component of a name.

        Instances For
          def Lean.Name.lastComponentAsString :
          Name โ†’ String

          Get the last field of a name as a string. Doesn't raise an error when the last component is a numeric field.

          Instances For
            def Lean.Name.splitAt (nm : Name) (n : โ„•) :
            Name ร— Name

            nm.splitAt n splits a name nm in two parts, such that the second part has depth n, i.e. (nm.splitAt n).2.getNumParts = n (assuming nm.getNumParts โ‰ฅ n). Example: splitAt `foo.bar.baz.back.bat 1 = (`foo.bar.baz.back, `bat).

            Instances For
              def Lean.Name.isPrefixOf? (pre nm : Name) :
              Option Name

              isPrefixOf? pre nm returns some post if nm = pre ++ post. Note that this includes the case where nm has multiple more namespaces. If pre is not a prefix of nm, it returns none.

              Instances For
                def Lean.Name.isBlackListed {m : Type โ†’ Type} [Monad m] [MonadEnv m] (declName : Name) :
                m Bool
                Instances For
                  def Lean.ConstantInfo.isDef :
                  ConstantInfo โ†’ Bool

                  Checks whether this ConstantInfo is a definition.

                  Instances For
                    def Lean.ConstantInfo.isThm :
                    ConstantInfo โ†’ Bool

                    Checks whether this ConstantInfo is a theorem.

                    Instances For
                      def Lean.ConstantInfo.updateConstantVal :
                      ConstantInfo โ†’ ConstantVal โ†’ ConstantInfo

                      Update ConstantVal (the data common to all constructors of ConstantInfo) in a ConstantInfo.

                      Instances For
                        def Lean.ConstantInfo.updateName (c : ConstantInfo) (name : Name) :
                        ConstantInfo

                        Update the name of a ConstantInfo.

                        Instances For
                          def Lean.ConstantInfo.updateType (c : ConstantInfo) (type : Expr) :
                          ConstantInfo

                          Update the type of a ConstantInfo.

                          Instances For
                            def Lean.ConstantInfo.updateLevelParams (c : ConstantInfo) (levelParams : List Name) :
                            ConstantInfo

                            Update the level parameters of a ConstantInfo.

                            Instances For
                              def Lean.ConstantInfo.updateAll :
                              ConstantInfo โ†’ List Name โ†’ ConstantInfo

                              Update the mutual-block all field of a ConstantInfo.

                              This applies to declaration kinds where ConstantInfo.all is stored directly.

                              Instances For
                                def Lean.ConstantInfo.updateValue :
                                ConstantInfo โ†’ Expr โ†’ ConstantInfo

                                Update the value of a ConstantInfo, if it has one.

                                Instances For
                                  def Lean.ConstantInfo.toDeclaration! :
                                  ConstantInfo โ†’ Declaration

                                  Turn a ConstantInfo into a declaration.

                                  Instances For
                                    def Lean.mkConst' (constName : Name) :
                                    MetaM Expr

                                    Same as mkConst, but with fresh level metavariables.

                                    Instances For

                                      Declarations about Expr #

                                      def Lean.Expr.bvarIdx? :
                                      Expr โ†’ Option โ„•
                                      Instances For
                                        @[inline]
                                        def Lean.Expr.getAppApps (e : Expr) :
                                        Array Expr

                                        Given f a b c, return #[f a, f a b, f a b c]. Each entry in the array is an Expr.app, and this array has the same length as the one returned by Lean.Expr.getAppArgs.

                                        Instances For
                                          def Lean.Expr.eraseProofs (e : Expr) :
                                          MetaM Expr

                                          Erase proofs in an expression by replacing them with sorrys.

                                          This function replaces all proofs in the expression and in the types that appear in the expression by sorryAxs. The resulting expression has the same type as the old one.

                                          It is useful, e.g., to verify if the proof-irrelevant part of a definition depends on a variable.

                                          Instances For
                                            def Lean.Expr.type? :
                                            Expr โ†’ Option Level

                                            If an Expr has the form Type u, then return some u, otherwise none.

                                            Instances For
                                              @[deprecated "This function was implemented incorrectly" (since := "2026-02-13")]
                                              def Lean.Expr.isConstantApplication (e : Expr) :
                                              Bool

                                              isConstantApplication e checks whether e is syntactically an application of the form (fun xโ‚ โ‹ฏ xโ‚™ => H) yโ‚ โ‹ฏ yโ‚™ where H does not contain the variable xโ‚™. In other words, it does a syntactic check that the expression does not depend on yโ‚™.

                                              Instances For
                                                @[inline]
                                                partial def Lean.Expr.isAppOrForallOfConstP (p : Name โ†’ Bool) (type : Expr) :
                                                Bool

                                                Returns true if type is an application of a constant decl for which p decl is true, or a forall with return type of the same form (i.e. of the form โˆ€ (xโ‚€ : Xโ‚€) (xโ‚ : Xโ‚) โ‹ฏ, decl .. where p decl).

                                                Runs cleanupAnnotations on type and forallE bodies, and ignores metadata in applications.

                                                @[inline]
                                                def Lean.Expr.isAppOrForallOfConst (declName : Name) (type : Expr) :
                                                Bool

                                                Returns true if type is an application of a constant declName, or a forall with return type of the same form (i.e. of the form โˆ€ (xโ‚€ : Xโ‚€) (xโ‚ : Xโ‚) โ‹ฏ, declName ..).

                                                Runs cleanupAnnotations on type and forallE bodies, and ignores metadata in applications.

                                                Instances For
                                                  def Lean.Expr.getUnusedForallInstanceBinderIdxsWhere (p : Expr โ†’ Bool) (e : Expr) :
                                                  Array โ„•

                                                  Gets the indices i (in ascending order) of the binders of a nested .forallE, (xโ‚€ : Aโ‚€) โ†’ (xโ‚ : Aโ‚) โ†’ โ‹ฏ โ†’ X, such that

                                                  • the binder [xแตข : Aแตข] has instImplicit binderInfo
                                                  • p Aแตข is true
                                                  • The rest of the type (xแตขโ‚Šโ‚ : Aแตขโ‚Šโ‚) โ†’ โ‹ฏ โ†’ X does not depend on xแตข. (It's in this sense that xแตข : Aแตข is "unused".)

                                                  Note that the argument to p may have loose bvars. This is a performance optimization.

                                                  This function runs cleanupAnnotations on each expression before examining it.

                                                  We see through lets, and do not increment the index when doing so. This behavior is compatible with forallBoundedTelescope.

                                                  Instances For
                                                    partial def Lean.Expr.hasInstanceBinderOf (p : Expr โ†’ Bool) (e : Expr) :
                                                    Bool

                                                    Returns true if e includes a forallE instance binder that satisfies p.

                                                    Cleans up annotations before traversing nested forallEs, and sees through lets.

                                                    def Lean.Expr.letDepth :
                                                    Expr โ†’ โ„•

                                                    Counts the immediate depth of a nested let expression.

                                                    Instances For
                                                      def Lean.Expr.ensureHasNoMVars (e : Expr) :
                                                      MetaM Unit

                                                      Check that an expression contains no metavariables (after instantiation).

                                                      Instances For
                                                        def Lean.Expr.ofNat (ฮฑ : Expr) (n : โ„•) :
                                                        MetaM Expr

                                                        Construct the term of type ฮฑ for a given natural number (doing typeclass search for the OfNat instance required).

                                                        Instances For
                                                          def Lean.Expr.ofInt (ฮฑ : Expr) :
                                                          โ„ค โ†’ MetaM Expr

                                                          Construct the term of type ฮฑ for a given integer (doing typeclass search for the OfNat and Neg instances required).

                                                          Instances For
                                                            partial def Lean.Expr.numeral? (e : Expr) :
                                                            Option โ„•

                                                            Return some n if e is one of the following

                                                            • a nat literal (numeral)
                                                            • Nat.zero
                                                            • Nat.succ x where isNumeral x
                                                            • OfNat.ofNat _ x _ where isNumeral x
                                                            def Lean.Expr.zero? (e : Expr) :
                                                            Bool

                                                            Test if an expression is either Nat.zero, or OfNat.ofNat 0.

                                                            Instances For
                                                              def Lean.Expr.ne?' (e : Expr) :
                                                              Option (Expr ร— Expr ร— Expr)

                                                              Tests is if an expression matches either x โ‰  y or ยฌ (x = y). If it matches, returns some (type, x, y).

                                                              Instances For
                                                                @[inline]
                                                                def Lean.Expr.le? (p : Expr) :
                                                                Option (Expr ร— Expr ร— Expr)

                                                                Lean.Expr.le? e takes e : Expr as input. If e represents a โ‰ค b, then it returns some (t, a, b), where t is the Type of a, otherwise, it returns none.

                                                                Instances For
                                                                  @[inline]
                                                                  def Lean.Expr.lt? (p : Expr) :
                                                                  Option (Expr ร— Expr ร— Expr)

                                                                  Lean.Expr.lt? e takes e : Expr as input. If e represents a < b, then it returns some (t, a, b), where t is the Type of a, otherwise, it returns none.

                                                                  Instances For
                                                                    def Lean.Expr.sides? (ty : Expr) :
                                                                    Option (Expr ร— Expr ร— Expr ร— Expr)

                                                                    Given a proposition ty that is an Eq, Iff, or HEq, returns (tyLhs, lhs, tyRhs, rhs), where lhs : tyLhs and rhs : tyRhs, and where lhs is related to rhs by the respective relation.

                                                                    See also Lean.Expr.iff?, Lean.Expr.eq?, and Lean.Expr.heq?.

                                                                    Instances For
                                                                      def Lean.Expr.isSorryAx :
                                                                      Expr โ†’ Bool

                                                                      Returns true if the provided Expr is exactly of the form sorryAx _ _. This is the form produced by the sorry term/tactic.

                                                                      Contrast with Lean.Expr.isSorry, which additionally returns true for any function application of sorry/sorryAx (including e.g. sorryAx ฮฑ true x y z).

                                                                      Instances For
                                                                        def Lean.Expr.modifyAppArgM {M : Type โ†’ Type u} [Functor M] [Pure M] (modifier : Expr โ†’ M Expr) :
                                                                        Expr โ†’ M Expr
                                                                        Instances For
                                                                          def Lean.Expr.modifyRevArg (modifier : Expr โ†’ Expr) :
                                                                          โ„• โ†’ Expr โ†’ Expr
                                                                          Instances For
                                                                            def Lean.Expr.modifyArg (modifier : Expr โ†’ Expr) (e : Expr) (i : โ„•) (n : โ„• := e.getAppNumArgs) :
                                                                            Expr

                                                                            Given f aโ‚€ aโ‚ ... aโ‚™โ‚‹โ‚, runs modifier on the ith argument or returns the original expression if out of bounds.

                                                                            Instances For
                                                                              def Lean.Expr.setArg (e : Expr) (i : โ„•) (x : Expr) (n : โ„• := e.getAppNumArgs) :
                                                                              Expr

                                                                              Given f aโ‚€ aโ‚ ... aโ‚™โ‚‹โ‚, sets the argument on the ith argument to x or returns the original expression if out of bounds.

                                                                              Instances For
                                                                                def Lean.Expr.getRevArg? :
                                                                                Expr โ†’ โ„• โ†’ Option Expr
                                                                                Instances For
                                                                                  def Lean.Expr.getArg? (e : Expr) (i : โ„•) (n : โ„• := e.getAppNumArgs) :
                                                                                  Option Expr

                                                                                  Given f aโ‚€ aโ‚ ... aโ‚™โ‚‹โ‚, returns the ith argument or none if out of bounds.

                                                                                  Instances For
                                                                                    def Lean.Expr.modifyArgM {M : Type โ†’ Type u} [Monad M] (modifier : Expr โ†’ M Expr) (e : Expr) (i : โ„•) (n : โ„• := e.getAppNumArgs) :
                                                                                    M Expr

                                                                                    Given f aโ‚€ aโ‚ ... aโ‚™โ‚‹โ‚, runs modifier on the ith argument. An argument n may be provided which says how many arguments we are expecting e to have.

                                                                                    Instances For
                                                                                      def Lean.Expr.renameBVar (e : Expr) (old new : Name) :
                                                                                      Expr

                                                                                      Traverses an expression e and renames bound variables named old to new.

                                                                                      Instances For
                                                                                        def Lean.Expr.getBinderName (e : Expr) :
                                                                                        MetaM (Option Name)

                                                                                        getBinderName e returns some n if e is an expression of the form โˆ€ n, ... and none otherwise.

                                                                                        Instances For
                                                                                          def Lean.Expr.mapForallBinderNames :
                                                                                          Expr โ†’ (Name โ†’ Name) โ†’ Expr

                                                                                          Map binder names in a nested forall (aโ‚ : ฮฑโ‚) โ†’ ... โ†’ (aโ‚™ : ฮฑโ‚™) โ†’ _

                                                                                          Instances For
                                                                                            def Lean.Expr.mkDirectProjection (e : Expr) (fieldName : Name) :
                                                                                            MetaM Expr

                                                                                            If e has a structure as type with field fieldName, mkDirectProjection e fieldName creates the projection expression e.fieldName

                                                                                            Instances For
                                                                                              def Lean.Expr.mkProjection (e : Expr) (fieldName : Name) :
                                                                                              MetaM Expr

                                                                                              If e has a structure as type with field fieldName (either directly or in a parent structure), mkProjection e fieldName creates the projection expression e.fieldName

                                                                                              Instances For
                                                                                                def Lean.Expr.reduceProjStruct? (e : Expr) :
                                                                                                MetaM (Option Expr)

                                                                                                If e is a projection of the structure constructor, reduce the projection. Otherwise returns none. If this function detects that expression is ill-typed, throws an error. For example, given Prod.fst (x, y), returns some x.

                                                                                                Instances For
                                                                                                  @[specialize #[]]
                                                                                                  def Lean.Expr.containsConst (e : Expr) (p : Name โ†’ Bool) :
                                                                                                  Bool

                                                                                                  Returns true if e contains a name n where p n is true.

                                                                                                  Instances For
                                                                                                    def Lean.Expr.forallNot_of_notExists (ex hNotEx : Expr) :
                                                                                                    MetaM (Expr ร— Expr)

                                                                                                    Given (hNotEx : Not ex) where ex is of the form Exists x, p x, return a forall x, Not (p x) and a proof for it.

                                                                                                    This function handles nested existentials.

                                                                                                    Instances For
                                                                                                      def Lean.getFieldsToParents (env : Environment) (structName : Name) :
                                                                                                      Array Name

                                                                                                      Get the projections that are projections to parent structures. Similar to getParentStructures, except that this returns the (last component of the) projection names instead of the parent names.

                                                                                                      Instances For