Documentation

Lean.Elab.Do.Basic

  • m : Expr

    The inferred type of the monad of type Type u → Type v.

  • u : Level

    The u in m : Type u → Type v.

  • v : Level

    The v in m : Type u → Type v.

  • cachedPUnit : Expr

    The cached PUnit expression.

  • cachedPUnitUnit : Expr

    The cached PUnit.unit expression.

Instances For
    • monadInfo : MonadInfo

      Inferred and cached information about the monad.

    • mutVars : Array Name

      The mutable variables in declaration order.

    • doBlockResultType : Expr

      The expected type of the current do block. This can be different from earlyReturnType in for loop do blocks, for example.

    • contInfo : ContInfoRef

      Information about return, break and continue continuations.

    Instances For
      Instances For

        A continuation metavariable.

        When generating jumps to join points or filling in expressions for break or continue, it is still unclear what mutable variables need to be passed, because it depends on which mutable variables were reassigned in the control flow path to any of the jumps.

        The mechanism of ContVarId allows to delay the assignment of the jump expressions until the local contexts of all the jumps are known.

        Instances For

          Information about a jump site associated to ContVarId. There will be one instance per jump site to a join point, or for each break or continue element.

          • mvar : Expr

            The metavariable to be assigned with the jump to the join point. Conveniently, its captured local context is that of the jump, in which the new mutable variable definitions and result variable are in scope.

          • ref : Syntax

            A reference for error reporting.

          Instances For

            Information about a ContVarId.

            • type : Expr

              The monadic type of the continuation.

            • tunneledVars : Std.HashSet Name

              A superset of the local variable names that the jumps will refer to. Often the mut variables. Any let-bound FV will be turned into a have-bound FV by setting their nondep flag in the local context of the metavariable for the jump site. This is a technicality to ensure that isDefEq will not inline the lets.

            • Local context at the time the continuation variable was created.

            • The tracked jumps to the continuation. Each contains a metavariable to be assigned later.

            Instances For
              Instances For

                Elaboration of a do block do $e; $rest, results in a call elabTerm `(do $e; $rest) = elabElem e dec, where elabElem e · is the elaborator for do element e, and dec is the DoElemCont describing the elaboration of the rest of the block rest.

                If the semantics of e resumes its continuation rest, its elaborator must bind its result to resultName, ensure that it has type resultType and then elaborate rest using dec.

                Clearly, for term elements e : m α, the result has type α. More subtly, for binding elements let x := e or let x ← e, the result has type PUnit and is unrelated to the type of the bound variable x.

                Examples:

                • return drops the continuation; return x; pure () elaborates to pure x.
                • let x ← e; rest x elaborates to e >>= fun x => rest x.
                • let x := 3; let y ← (let x ← e); rest x elaborates to let x := 3; e >>= fun x_1 => let y := (); rest x, which is immediately zeta-reduced to let x := 3; e >>= fun x_1 => rest x.
                • one; two elaborates to one >>= fun (_ : PUnit) => two; it is an error if one does not have type PUnit.
                • resultName : Name

                  The name of the monadic result variable.

                • resultType : Expr

                  The type of the monadic result.

                • The continuation to elaborate the rest of the block.

                Instances For
                  @[reducible, inline]

                  The type of elaborators for do block elements.

                  It is elabTerm `(do $e; $rest) = elabElem e dec, where elabElem e · is the elaborator for do element e, and dec is the DoElemCont describing the elaboration of the rest of the block rest.

                  Equations
                  Instances For

                    Information about a success, return, break or continue continuation that will be filled in after the code using it has been elaborated.

                    Instances For
                      @[implemented_by Lean.Elab.Do.ContInfo.toContInfoRefImpl]
                      @[implemented_by Lean.Elab.Do.ContInfoRef.toContInfoImpl]

                      Constructs m α from α.

                      Equations
                      Instances For

                        The cached PUnit expression.

                        Equations
                        Instances For

                          The cached PUnit.unit expression.

                          Equations
                          Instances For

                            The expression pure (α:=α) e.

                            Equations
                            Instances For

                              Create a DoElemCont returning the result using pure.

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

                                The expression Bind.bind (α:=α) (β:=β) e k.

                                Equations
                                • One or more equations did not get rendered due to their size.
                                Instances For
                                  def Lean.Elab.Do.declareMutVar {α : Type} (x : Name) :
                                  DoElabM αDoElabM α

                                  Register the given name as that of a mut variable.

                                  Equations
                                  • One or more equations did not get rendered due to their size.
                                  Instances For
                                    def Lean.Elab.Do.declareMutVar? {α : Type} (mutTk? : Option Syntax) (x : Name) (k : DoElabM α) :

                                    Register the given name as that of a mut variable if the syntax token mut is present.

                                    Equations
                                    Instances For

                                      Throw an error if the given name is not a declared mut variable.

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

                                        Throw an error if a declaration of the given name would shadow a mut variable.

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

                                          Create a fresh α that would fit in m α.

                                          Equations
                                          • One or more equations did not get rendered due to their size.
                                          Instances For
                                            def Lean.Elab.Do.synthUsingDefEq (msg : String) (expected actual : Expr) :
                                            Equations
                                            • One or more equations did not get rendered due to their size.
                                            Instances For

                                              Has the effect of e >>= fun (x : eResultTy) => $(← k `(x)). Ensures that e has type m eResultTy.

                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For
                                                def Lean.Elab.Do.elabType (ty? : Option (TSyntax `term)) (freshLevel : Bool := false) :

                                                A variant of Term.elabType that takes the universe of the monad into account, unless freshLevel is set.

                                                Equations
                                                • One or more equations did not get rendered due to their size.
                                                Instances For
                                                  def Lean.Elab.Do.elabTerm (stx : Syntax) (expectedType? : Option Expr) :
                                                  Equations
                                                  Instances For
                                                    Equations
                                                    Instances For
                                                      def Lean.Elab.Do.elabBinder {α : Type} (binder : Syntax) (x : ExprDoElabM α) :
                                                      Equations
                                                      • One or more equations did not get rendered due to their size.
                                                      Instances For

                                                        The subset of mutVars that were reassigned in any of the childCtxs relative to the given rootCtx.

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

                                                          Adds the new reaching definitions of the given tunneledVars in childCtx relative to rootCtx as non-dependent decls.

                                                          Equations
                                                          • One or more equations did not get rendered due to their size.
                                                          Instances For
                                                            def Lean.Elab.Do.mkFreshContVar (resultType : Expr) (tunneledVars : Array Name) :

                                                            Creates a new continuation variable of type m α given the result type α. The tunneledVars is a superset of the let-bound variable names that the jumps will refer to. Often it will be the mut variables. Leaving it empty inlines let-bound variables at jump sites.

                                                            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

                                                                Creates a new jump site for the continuation variable, to be synthesized later.

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

                                                                  The number of jump sites allocated for the continuation variable.

                                                                  Equations
                                                                  Instances For

                                                                    Synthesize the jump sites for the continuation variable. k is run once for each jump site, in the LocalContext of the jump site. The result of k is used to fill in the jump site.

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

                                                                        The subset of (← read).mutVars that were reassigned at any of the jump sites of the continuation variable. The result array has the same order as (← read).mutVars.

                                                                        Equations
                                                                        • One or more equations did not get rendered due to their size.
                                                                        Instances For
                                                                          def Lean.Elab.Do.withLCtxKeepingMutVarDefs {α : Type} (oldCtx : LocalContext) (oldMutVars : Array Name) (resultName : Name) (k : DoElabM α) :

                                                                          Restores the local context to oldCtx and adds the new reaching definitions of the mut vars and result. Then resume the continuation k with the mutVars restored to the given oldMutVars.

                                                                          This function is useful to de-nest

                                                                          let mut x := 0
                                                                          let y := 3
                                                                          let z ← do
                                                                            let mut y ← e
                                                                            x := y + 1
                                                                            pure y
                                                                          let y := y + 3
                                                                          pure (x + y + z)
                                                                          

                                                                          into

                                                                          let mut x := 0
                                                                          let y := 3
                                                                          let mut y† ← e
                                                                          x := y† + 1
                                                                          let z ← pure y†
                                                                          let y := y + 3
                                                                          pure (x + y + z)
                                                                          

                                                                          Note that the continuation of the let z ← ... bind, roughly k := .cont `z _ `(let y := y + 3; pure (x + y + z)), needs to elaborated in a local context that contains the reassignment of x, but not the shadowing mut var definition of y.

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

                                                                            Return $e >>= fun ($dec.resultName : $dec.resultType) => $(← dec.k), cancelling the bind if $(← dec.k) is pure $dec.resultName.

                                                                            Equations
                                                                            Instances For

                                                                              Return let $k.resultName : PUnit := PUnit.unit; $(← k.k), ensuring that the result type of k.k is PUnit and then immediately zeta-reduce the let.

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

                                                                                Call caller with a duplicable proxy of dec. When the proxy is elaborated more than once, a join point is introduced so that dec is only elaborated once to fill in the RHS of this join point.

                                                                                This is useful for control-flow constructs like if and match, where multiple tail-called branches share the continuation.

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

                                                                                  Given a list of mut vars vars and an FVar tupleVar binding a tuple, bind the mut vars to the fields of the tuple and call k in the resulting local context.

                                                                                  Equations
                                                                                  Instances For
                                                                                    @[inline]

                                                                                    Introduce proxy redefinitions for all mut vars and call the continuation k with a function elimProxyDefs : Expr → MetaM Expr similar to mkLetFVars that will replace the proxy defs with the actual reassigned or original definitions.

                                                                                    Equations
                                                                                    • One or more equations did not get rendered due to their size.
                                                                                    Instances For
                                                                                      def Lean.Elab.Do.enterLoopBody {α : Type} (resultType : Expr) (returnCont : DoElemCont) (breakCont continueCont : DoElabM Expr) (body : DoElabM α) :

                                                                                      Prepare the context for elaborating the body of a loop. This includes setting the return continuation, break continuation, continue continuation, as well as the changed result type of the do block in the loop body.

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

                                                                                        Prepare the context for elaborating the body of a do block that does not support mut vars, break, continue or return.

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

                                                                                          Prepare the context for elaborating the body of a finally block. There is no support for mut vars, break, continue or return in a finally block.

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

                                                                                            Create the Context for do elaboration from the given expected type of a do block.

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

                                                                                              Backtrackable state for the TermElabM monad.

                                                                                              Instances For
                                                                                                Equations
                                                                                                Instances For
                                                                                                  Equations
                                                                                                  Instances For
                                                                                                    @[implemented_by Lean.Elab.Do.mkDoElemElabAttributeUnsafe]

                                                                                                    Registers a do element elaborator for the given syntax node kind.

                                                                                                    A do element elaborator should have type DoElab (which is Lean.SyntaxDoElemContDoElabM Expr), i.e. should take syntax of the given syntax node kind and a DoElemCont as parameters and produce an expression.

                                                                                                    When elaborating a do block do e; rest, the elaborator for e is invoked with the syntax of e and the DoElemCont representing rest.

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

                                                                                                    partial def Lean.Elab.Do.elabDoElem (stx : TSyntax `doElem) (cont : DoElemCont) :
                                                                                                    def Lean.Elab.Do.elabDoElems1 (doElems : Array (TSyntax `doElem)) (cont : DoElemCont) :
                                                                                                    Equations
                                                                                                    • One or more equations did not get rendered due to their size.
                                                                                                    Instances For
                                                                                                      def Lean.Elab.Do.elabDoSeq (doSeq : TSyntax `Lean.Parser.Term.doSeq) (cont : DoElemCont) :
                                                                                                      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