Documentation

Lean.Elab.Command

Opaque linter state. Similar to EnvExtensionState for environment extensions.

Instances For
    Instances For
      @[inline]

      Runs x without reporting deprecated constants and syntax, like Term.withoutCheckDeprecated.

      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        Instances For

          A handle to a registered stateful linter, returned by registerStatefulLinter. σ is the linter's persistent state, and τ is the pre-phase output type.

          Instances For

            The type-erased registry entry for a stateful linter.

            Instances For
              @[implemented_by _private.Lean.Elab.Command.0.Lean.Elab.Command.StatefulLinter.prevStateImpl]
              @[implemented_by _private.Lean.Elab.Command.0.Lean.Elab.Command.StatefulLinter.preStateImpl]
              @[reducible, inline]

              A typed accessor to a linter's previous state.

              Equations
              Instances For
                @[reducible, inline]

                A typed accessor to a linter's pre-phase output.

                Equations
                Instances For
                  @[instance_reducible, always_inline]
                  Equations
                  • One or more equations did not get rendered due to their size.
                  @[inline]

                  Like Core.tryCatchRuntimeEx; runtime errors are generally used to abort term elaboration, so we do want to catch and process them at the command level.

                  Equations
                  Instances For
                    def Lean.Elab.Command.mkState (env : Environment) (messages : MessageLog := { }) (opts : Options := ∅) :
                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For
                      unsafe def Lean.Elab.Command.registerStatefulLinterImpl {σ τ : Type} (init : σ) (pre : Syntax → σ → PrevStateFn → CommandElabM (Option τ) := fun (x : Syntax) (x_1 : σ) (x_2 : PrevStateFn) => pure none) (post : Syntax → σ → Option τ → PrevStateFn → PreStateFn → CommandElabM σ) :

                      Registers a stateful linter and returns its handle (used by other linters to read its state). Must be called during initialization.

                      Lifecycle Phases #

                      • pre: Reads the previous command's persistant state (via readPrevPostState) and optionally outputs a pre-phase state (type Option τ).
                      • post: Reads the previous command's state and all current pre-phase outputs (via readPrevPostState and readCurrentPreState), then produces the new state (type σ) for the next command.

                      State Access Rules #

                      • Self: Access its own persistent and pre-phase states directly via selfPrevPostState and selfCurrentPreState.
                      • Others: Access other linters' states only via the readPrevPostState and readCurrentPreState closures that take typed handles of other linters.

                      Commands with Parse Errors #

                      Unlike regular linters, stateful linters also run on commands with parse errors. Their messages are discarded there (unless showPartialSyntaxErrors is set), but their info trees are kept, so code actions from MessageData.hint would be offered without the corresponding message. Linters should not produce hints when (← read).suppressElabErrors is set.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        @[implemented_by Lean.Elab.Command.registerStatefulLinterImpl]
                        opaque Lean.Elab.Command.registerStatefulLinter {σ τ : Type} (init : σ) (pre : Syntax → σ → PrevStateFn → CommandElabM (Option τ) := fun (x : Syntax) (x_1 : σ) (x_2 : PrevStateFn) => pure none) (post : Syntax → σ → Option τ → PrevStateFn → PreStateFn → CommandElabM σ) :

                        Registers a stateful linter and returns its handle (used by other linters to read its state). Must be called during initialization.

                        Lifecycle Phases #

                        • pre: Reads the previous command's persistant state (via readPrevPostState) and optionally outputs a pre-phase state (type Option τ).
                        • post: Reads the previous command's state and all current pre-phase outputs (via readPrevPostState and readCurrentPreState), then produces the new state (type σ) for the next command.

                        State Access Rules #

                        • Self: Access its own persistent and pre-phase states directly via selfPrevPostState and selfCurrentPreState.
                        • Others: Access other linters' states only via the readPrevPostState and readCurrentPreState closures that take typed handles of other linters.

                        Commands with Parse Errors #

                        Unlike regular linters, stateful linters also run on commands with parse errors. Their messages are discarded there (unless showPartialSyntaxErrors is set), but their info trees are kept, so code actions from MessageData.hint would be offered without the corresponding message. Linters should not produce hints when (← read).suppressElabErrors is set.

                        @[instance_reducible]
                        Equations
                        • One or more equations did not get rendered due to their size.
                        @[instance_reducible]
                        Equations
                        • One or more equations did not get rendered due to their size.
                        @[instance_reducible, always_inline]
                        Equations
                        Equations
                        Instances For
                          @[instance_reducible]
                          Equations
                          • One or more equations did not get rendered due to their size.
                          @[instance_reducible]
                          Equations
                          • One or more equations did not get rendered due to their size.
                          @[instance_reducible]
                          Equations
                          • One or more equations did not get rendered due to their size.
                          @[instance_reducible]
                          Equations
                          • One or more equations did not get rendered due to their size.
                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            @[instance_reducible]
                            Equations
                            • One or more equations did not get rendered due to their size.
                            Equations
                            Instances For
                              @[inline]
                              Equations
                              Instances For

                                Return the current scope.

                                Equations
                                Instances For
                                  @[instance_reducible]
                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  @[instance_reducible]
                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  def Lean.Elab.Command.runLinters (stx : Syntax) (infoTreePromise? : Option (IO.Promise InfoTree) := none) (codeQualityEntriesPromise? : Option (IO.Promise (Array Linter.CodeQualityLogEntry)) := none) :
                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For
                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    Instances For
                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For
                                        @[inline]

                                        Catches and logs exceptions occurring in x. Unlike try catch in CommandElabM, this function catches interrupt exceptions as well and thus is intended for use at the top level of elaboration. Interrupt and abort exceptions are caught but not logged.

                                        Equations
                                        Instances For
                                          def Lean.Elab.Command.wrapAsync {α β : Type} (act : α → CommandElabM β) (cancelTk? : Option IO.CancelToken) :

                                          Wraps the given action for use in EIO.asTask etc., discarding its final monadic state.

                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For
                                            def Lean.Elab.Command.wrapAsyncAsSnapshot {α : Type} (act : α → CommandElabM Unit) (cancelTk? : Option IO.CancelToken) (desc : String := by exact decl_name%.toString) :

                                            Wraps the given action for use in BaseIO.asTask etc., discarding its final state except for logSnapshotTask tasks, which are reported as part of the returned tree. The given cancellation token, if any, should be stored in a SnapshotTask for the server to trigger it when the result is no longer needed.

                                            Equations
                                            • One or more equations did not get rendered due to their size.
                                            Instances For

                                              Includes a given task (such as from wrapAsyncAsSnapshot) in the overall snapshot tree for this command's elaboration, making its result available to reporting and the language server. The reporter will not know about this snapshot tree node until the main elaboration thread for this command has finished so this function is not useful for incremental reporting within a longer elaboration thread but only for tasks that outlive it such as background kernel checking or proof elaboration.

                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For
                                                Equations
                                                • One or more equations did not get rendered due to their size.
                                                Instances For

                                                  Runs the module linters on cmds, the commands of the module, asynchronously if Elab.async is set.

                                                  Equations
                                                  • One or more equations did not get rendered due to their size.
                                                  Instances For
                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For

                                                      Registers a command elaborator for the given syntax node kind.

                                                      A command elaborator should have type Lean.Elab.Command.CommandElab (which is Lean.Syntax → Lean.Elab.Term.CommandElabM Unit), i.e. should take syntax of the given syntax node kind as a parameter and perform an action.

                                                      The elab_rules and elab commands should usually be preferred over using this attribute directly.

                                                      Disables incremental command reuse and reporting for act if cond is true by setting Context.snap? to none.

                                                      Equations
                                                      • One or more equations did not get rendered due to their size.
                                                      Instances For
                                                        def Lean.Elab.Command.withMacroExpansion {α : Type} (beforeStx afterStx : Syntax) (x : CommandElabM α) :

                                                        Elaborate x with stx on the macro stack

                                                        Equations
                                                        • One or more equations did not get rendered due to their size.
                                                        Instances For
                                                          @[instance_reducible]
                                                          Equations
                                                          • One or more equations did not get rendered due to their size.
                                                          @[instance_reducible]
                                                          Equations
                                                          • One or more equations did not get rendered due to their size.

                                                          Snapshot after macro expansion of a command.

                                                          Instances For
                                                            @[instance_reducible]
                                                            Equations
                                                            • One or more equations did not get rendered due to their size.

                                                            Option for showing elaboration errors from partial syntax errors.

                                                            If hint? is some hint, establishes a new context for macro scope naming and runs act in it, otherwise runs act directly without changes.

                                                            Context names as documented in Note Macro Scope Representation help with avoiding rebuilds and prefer_native lookup misses from macro scopes in declaration names and other exported information. This function establishes a new context with a globally unique name by combining the name of the current module with hint while also checking for previously used hints in the same module. Thus hint does not need to be unique but ensuring it is usually unique helps with keeping the context name stable.

                                                            In the current implementation, we call withInitQuotContext once in elabCommandTopLevel using the source input of the command as the hint. This helps with keeping macro scopes stable on changes to other parts of the file but not on changes to the command itself. Thus in each declaration elaborator we call withInitQuotContext again with the declaration name(s) as a hint so that changes to any other part of the declaration do not change the context name.

                                                            Equations
                                                            Instances For

                                                              elabCommand wrapper that should be used for the initial invocation, not for recursive calls after macro expansion etc.

                                                              Equations
                                                              • One or more equations did not get rendered due to their size.
                                                              Instances For

                                                                Adapt a syntax transformation to a regular, command-producing elaborator.

                                                                Equations
                                                                Instances For

                                                                  The environment linter framework needs to be able to run linters with the same context as liftTermElabM, so we expose that context as a public function here.

                                                                  Equations
                                                                  Instances For

                                                                    Return identifier names in the given bracketed binder.

                                                                    Equations
                                                                    • One or more equations did not get rendered due to their size.
                                                                    Instances For

                                                                      Lift the TermElabM monadic action x into a CommandElabM monadic action.

                                                                      Note that x is executed with an empty message log. Thus, x cannot modify/view messages produced by previous commands.

                                                                      If you need to access the free variables corresponding to the ones declared using the variable command, consider using runTermElabM.

                                                                      Recall that TermElabM actions can automatically lift MetaM and CoreM actions. Example:

                                                                      public import Lean
                                                                      
                                                                      open Lean Elab Command Meta
                                                                      
                                                                      def printExpr (e : Expr) : MetaM Unit := do
                                                                        IO.println s!"{← ppExpr e} : {← ppExpr (← inferType e)}"
                                                                      
                                                                      #eval
                                                                        liftTermElabM do
                                                                          printExpr (mkConst ``Nat)
                                                                      
                                                                      Equations
                                                                      • One or more equations did not get rendered due to their size.
                                                                      Instances For

                                                                        Execute the monadic action elabFn xs as a CommandElabM monadic action, where xs are free variables corresponding to all active scoped variables declared using the variable command.

                                                                        This method is similar to liftTermElabM, but it elaborates all scoped variables declared using the variable command.

                                                                        Example:

                                                                        public import Lean
                                                                        
                                                                        public section
                                                                        
                                                                        open Lean Elab Command Meta
                                                                        
                                                                        variable {α : Type u} {f : α → α}
                                                                        variable (n : Nat)
                                                                        
                                                                        #eval
                                                                          runTermElabM fun xs => do
                                                                            for x in xs do
                                                                              IO.println s!"{← ppExpr x} : {← ppExpr (← inferType x)}"
                                                                        
                                                                        Equations
                                                                        • One or more equations did not get rendered due to their size.
                                                                        Instances For

                                                                          Return the stack of all currently active scopes: the base scope always comes last; new scopes are prepended in the front. In particular, the current scope is always the first element.

                                                                          Equations
                                                                          Instances For
                                                                            Equations
                                                                            • One or more equations did not get rendered due to their size.
                                                                            Instances For
                                                                              Equations
                                                                              • One or more equations did not get rendered due to their size.
                                                                              Instances For
                                                                                Equations
                                                                                • One or more equations did not get rendered due to their size.
                                                                                Instances For
                                                                                  def Lean.liftCommandElabM {α : Type} (cmd : Elab.Command.CommandElabM α) (throwOnError : Bool := true) :

                                                                                  Lifts an action in CommandElabM into CoreM, updating the environment, messages, info trees, traces, the name generator, and macro scopes. The action is run in a context with an empty message log, empty trace state, and empty info trees.

                                                                                  If throwOnError is true, then if the command produces an error message, it is converted into an exception. In this case, info trees and messages are not carried over.

                                                                                  Commands that modify the processing of subsequent commands, such as open and namespace commands, only have an effect for the remainder of the CommandElabM computation passed here, and do not affect subsequent commands.

                                                                                  Warning: when using this from MetaM monads, the caches are not reset. While the modifyEnv function for MetaM clears its caches entirely, liftCommandElabM has no way to reset these caches.

                                                                                  Equations
                                                                                  Instances For
                                                                                    def Lean.Parser.Command.visibility.ofBool (isPublic : Bool) :
                                                                                    TSyntax `Lean.Parser.Command.visibility

                                                                                    Returns syntax for private or public visibility depending on isPublic. This function should be used to generate visibility syntax for declarations that is independent of the presence of public sections.

                                                                                    Equations
                                                                                    • One or more equations did not get rendered due to their size.
                                                                                    Instances For
                                                                                      def Lean.Parser.Command.visibility.ofAttrKind (attrKind : TSyntax `Lean.Parser.Term.attrKind) :
                                                                                      TSyntax `Lean.Parser.Command.visibility

                                                                                      Returns syntax for private if attrKind is local and public otherwise.

                                                                                      Equations
                                                                                      • One or more equations did not get rendered due to their size.
                                                                                      Instances For