| /- | |
| Copyright (c) 2025 Lean FRO LLC. All rights reserved. | |
| Released under Apache 2.0 license as described in the file LICENSE. | |
| Authors: Sebastian Graf | |
| -/ | |
| module | |
| prelude | |
| meta import Init.Data.Erased | |
| public import Lean.Elab.Do.InferControlInfo | |
| public import Lean.Elab.Binders | |
| import Lean.Meta.ProdN | |
| import Lean.Elab.DeprecatedSyntax.Basic | |
| public import Lean.Parser | |
| meta import Lean.Parser.Do | |
| import Init.Omega | |
| public section | |
| namespace Lean.Elab.Do | |
| open Lean Meta Parser.Term | |
| builtin_initialize registerTraceClass `Elab.do | |
| builtin_initialize registerTraceClass `Elab.do.match | |
| builtin_initialize registerTraceClass `Elab.do.step | |
| register_builtin_option experimental.intrinsic : Bool := { | |
| defValue := false | |
| descr := "acknowledge that the intrinsic verification syntax (the contract clauses of a `def`, \ | |
| the `assert` element, and the `invariant` and `decreasing` clauses of a loop) is experimental and \ | |
| subject to change; `true` silences the warning that each of these forms reports" | |
| } | |
| /-- Report that an intrinsic verification form is experimental, at the keyword `kw` that introduces | |
| it. `what` names the form, as in ``m!"`assert` element"``. -/ | |
| def warnIntrinsicExperimental [Monad m] [MonadLog m] [AddMessageContext m] [MonadOptions m] | |
| (kw : Syntax) (what : MessageData) : m Unit := do | |
| unless experimental.intrinsic.get (← getOptions) do | |
| logWarningAt kw m!"The {what} is part of the experimental intrinsic verification syntax; \ | |
| `set_option experimental.intrinsic true` acknowledges its experimental status and silences \ | |
| this warning." | |
| structure MonadInfo where | |
| /-- The inferred type of the monad of type `Type u → Type v`. -/ | |
| m : Expr | |
| /-- The `u` in `m : Type u → Type v`. -/ | |
| u : Level | |
| /-- The `v` in `m : Type u → Type v`. -/ | |
| v : Level | |
| /-- The cached `PUnit` expression. -/ | |
| cachedPUnit : Expr := | |
| if u matches .zero then mkConst ``Unit else mkConst ``PUnit [mkLevelSucc u] | |
| /-- The cached `PUnit.unit` expression. -/ | |
| cachedPUnitUnit : Expr := | |
| if u matches .zero then mkConst ``Unit.unit else mkConst ``PUnit.unit [mkLevelSucc u] | |
| -- Same pattern as for `Methods`/`MethodsRef` in `SimpM`. | |
| private opaque ContInfoRefPointed : NonemptyType.{0} | |
| def ContInfoRef : Type := ContInfoRefPointed.type | |
| instance : Nonempty ContInfoRef := | |
| by exact ContInfoRefPointed.property | |
| -- Same pattern as `ContInfoRef` above; used so `Context` can carry `DoOps` without | |
| -- depending on `DoElabM`. | |
| private opaque DoOpsRefPointed : NonemptyType.{0} | |
| def DoOpsRef : Type := DoOpsRefPointed.type | |
| instance : Nonempty DoOpsRef := | |
| by exact DoOpsRefPointed.property | |
| /-- Whether a code block is alive or dead. -/ | |
| inductive CodeLiveness where | |
| /-- We inferred the code is syntactically dead and don't need to elaborate it at all. -/ | |
| | deadSyntactically | |
| /-- We inferred the code is semantically dead, but we need to elaborate it to produce a program. -/ | |
| | deadSemantically | |
| /-- The code is alive. (Or it is dead, but we failed to prove it so.) -/ | |
| | alive | |
| instance : ToString CodeLiveness where | |
| toString | |
| | .alive => "alive" | |
| | .deadSyntactically => "deadSyntactically" | |
| | .deadSemantically => "deadSemantically" | |
| instance : ToMessageData CodeLiveness where | |
| toMessageData l := toString l | |
| /-- The least upper bound of two code livenesses. -/ | |
| def CodeLiveness.lub (a b : CodeLiveness) : CodeLiveness := | |
| match a, b with | |
| | .alive, _ => .alive | |
| | _, .alive => .alive | |
| | .deadSyntactically, _ => b | |
| | _, .deadSyntactically => a | |
| | _, _ => a | |
| /-- A mutable variable declared by `let mut` in a `do` block. -/ | |
| structure MutVar where | |
| /-- The identifier of the `let mut` or `erased mut` declaration. -/ | |
| ident : Ident | |
| /-- The `FVarId` of the initial binding produced by the declaration. -/ | |
| baseId : FVarId | |
| /-- Whether the variable comes from `erased mut`. -/ | |
| erased : Bool | |
| deriving Inhabited | |
| /-- The raw `Name` of a `mut` variable, as found in the local context. -/ | |
| def MutVar.getId (mutVar : MutVar) : Name := mutVar.ident.getId | |
| /-- | |
| Build an `FVarAliasInfo` recording that the reassignment binding `id` aliases the original | |
| `let mut` binding represented by `mutVar`. | |
| -/ | |
| def MutVar.mkAliasInfo (mutVar : MutVar) (id : FVarId) : FVarAliasInfo := | |
| { userName := mutVar.getId, id, baseId := mutVar.baseId } | |
| instance : ToMessageData MutVar where | |
| toMessageData mutVar := | |
| .ofLazyM <| MessageData.withExprHoverM (format mutVar.getId.simpMacroScopes) (.fvar mutVar.baseId) | |
| structure Context where | |
| /-- Inferred and cached information about the monad. -/ | |
| monadInfo : MonadInfo | |
| /-- | |
| The mutable variables in declaration order. Kept in sync with `mutVarDefs`; insertions go | |
| through `declareMutVar` / `declareMutVars` only. | |
| -/ | |
| mutVars : Array MutVar := #[] | |
| /-- Maps mutable variable names to their `MutVar` record. Kept in sync with `mutVars`. -/ | |
| mutVarDefs : Std.HashMap Name MutVar := {} | |
| /-- | |
| The expected type of the current `do` block. | |
| This can be different from `ReturnCont.resultType` in `for` loop `do` blocks, for example. | |
| -/ | |
| doBlockResultType : Expr | |
| /-- Information about `return`, `break` and `continue` continuations. -/ | |
| contInfo : ContInfoRef | |
| /-- | |
| Whether the current `do` element is dead code. `elabDoElem` will emit a warning if not `.alive`. | |
| -/ | |
| deadCode : CodeLiveness := .alive | |
| /-- Pluggable builders for `pure` and `bind` applications. -/ | |
| ops : DoOpsRef | |
| abbrev DoElabM := ReaderT Context Term.TermElabM | |
| /-- Pluggable builders for the `pure` / `bind` applications emitted by the `do` elaborator. -/ | |
| structure DoOps where | |
| /-- Build `pure (α:=α) e : m α`. -/ | |
| mkPureApp : (α e : Expr) → DoElabM Expr | |
| /-- Build `bind (α:=α) (β:=β) e k : m β`. -/ | |
| mkBindApp : (α β e k : Expr) → DoElabM Expr | |
| /-- | |
| If `e` is syntactically a `pure …` application, return the pure value; otherwise `none`. | |
| Used by `DoElemCont.mkBindUnlessPure` to contract `e >>= pure` to `e` and | |
| `pure e >>= k` to `let x := e; k x`. | |
| -/ | |
| isPureApp? : Expr → Option Expr | |
| /-- Match a monad application `m α`, returning `MonadInfo` for `m` and `α`. -/ | |
| splitMonadApp? : Expr → Term.TermElabM (Option (MonadInfo × Expr)) | |
| /-- Construct `m α` from `α`. -/ | |
| mkMonadApp : Expr → DoElabM Expr | |
| deriving Inhabited | |
| unsafe def DoOps.toDoOpsRefImpl (o : DoOps) : DoOpsRef := | |
| unsafeCast o | |
| @[implemented_by DoOps.toDoOpsRefImpl] | |
| opaque DoOps.toDoOpsRef (o : DoOps) : DoOpsRef | |
| unsafe def DoOpsRef.toDoOpsImpl (r : DoOpsRef) : DoOps := | |
| unsafeCast r | |
| @[implemented_by DoOpsRef.toDoOpsImpl] | |
| opaque DoOpsRef.toDoOps (r : DoOpsRef) : DoOps | |
| /-- | |
| Whether the continuation of a `do` element is duplicable. Saying `nonDuplicable` is always safe; | |
| `duplicable` allows for more optimizations. | |
| -/ | |
| inductive DoElemContKind | |
| | nonDuplicable | |
| | duplicable | |
| deriving Inhabited | |
| /-- | |
| Elaboration of a `do` block `do $e; $rest`, results in a call | |
| ``elabTerm `(do $e; $rest) = elabDoElem e dec``, where `elabDoElem 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`. | |
| -/ | |
| structure DoElemCont where | |
| mk :: | |
| /-- The name of the monadic result variable. -/ | |
| resultName : Name | |
| /-- The type of the monadic result. -/ | |
| resultType : Expr | |
| /-- | |
| The continuation to elaborate the `rest` of the block. It assumes that the result of the `do` | |
| block is bound to `resultName` with the correct type (that is, `resultType`, potentially refined | |
| by a dependent `match`). | |
| -/ | |
| k : DoElabM Expr | |
| /-- | |
| Whether we are OK with generating the code of the continuation multiple times, e.g. in different | |
| branches of a `match` or `if`. | |
| -/ | |
| kind : DoElemContKind := .nonDuplicable | |
| deriving Inhabited | |
| /-- | |
| The type of elaborators for `do` block elements. | |
| It is ``elabTerm `(do $e; $rest) = elabDoElem e dec``, where `elabDoElem e ·` is the elaborator for | |
| `do` element `e`, and `dec` is the `DoElemCont` describing the elaboration of the rest of the block | |
| `rest`. | |
| -/ | |
| abbrev DoElab := DoElem → DoElemCont → DoElabM Expr | |
| structure ReturnCont where | |
| resultType : Expr | |
| /-- | |
| The elaborator constructing a jump site to the return continuation, | |
| given some return value. The type of this return value determines the type of the jump expression; | |
| this could very well be different than the `resultType` in case an intervening `match` has refined | |
| `resultType`. So `k` must *not* hardcode the type `resultType` into its definition; rather it | |
| should infer the type of the return value argument. | |
| -/ | |
| k : Expr → DoElabM Expr | |
| deriving Inhabited | |
| /-- | |
| Information about a success, `return`, `break` or `continue` continuation that will be filled in | |
| after the code using it has been elaborated. | |
| -/ | |
| structure ContInfo where | |
| returnCont : ReturnCont | |
| breakCont : Option (DoElabM Expr) := none | |
| continueCont : Option (DoElabM Expr) := none | |
| deriving Inhabited | |
| unsafe def ContInfo.toContInfoRefImpl (m : ContInfo) : ContInfoRef := | |
| unsafeCast m | |
| @[implemented_by ContInfo.toContInfoRefImpl] | |
| opaque ContInfo.toContInfoRef (m : ContInfo) : ContInfoRef | |
| unsafe def ContInfoRef.toContInfoImpl (m : ContInfoRef) : ContInfo := | |
| unsafeCast m | |
| @[implemented_by ContInfoRef.toContInfoImpl] | |
| opaque ContInfoRef.toContInfo (m : ContInfoRef) : ContInfo | |
| /-- Constructs `m α` from `α`. -/ | |
| def mkMonadApp (resultType : Expr) : DoElabM Expr := do | |
| (← read).ops.toDoOps.mkMonadApp resultType | |
| /-- The cached `PUnit` expression. -/ | |
| def mkPUnit : DoElabM Expr := do | |
| return (← read).monadInfo.cachedPUnit | |
| /-- The cached ``PUnit.unit`` expression. -/ | |
| def mkPUnitUnit : DoElabM Expr := do | |
| return (← read).monadInfo.cachedPUnitUnit | |
| /-- The expression ``pure (α:=α) e``. -/ | |
| def mkPureApp (α e : Expr) : DoElabM Expr := do | |
| (← read).ops.toDoOps.mkPureApp α e | |
| /-- Create a `DoElemCont` returning the result using `pure`. -/ | |
| def DoElemCont.mkPure (resultType : Expr) : TermElabM DoElemCont := do | |
| let r ← mkFreshUserName `r | |
| return { | |
| resultName := r, | |
| resultType, | |
| k := do let decl ← getLocalDeclFromUserName r; mkPureApp decl.type decl.toExpr, | |
| kind := .duplicable | |
| } | |
| /-- Create a `ReturnCont` returning the result using `pure`. -/ | |
| def ReturnCont.mkPure (resultType : Expr) : TermElabM ReturnCont := do | |
| return { resultType, k x := do | |
| mkPureApp (← inferType x) x } | |
| /-- The expression ``Bind.bind (α:=α) (β:=β) e k``. -/ | |
| def mkBindApp (α β e k : Expr) : DoElabM Expr := do | |
| (← read).ops.toDoOps.mkBindApp α β e k | |
| /-- `DoOps` emitting `Pure.pure` / `Bind.bind`. -/ | |
| def DoOps.default : DoOps where | |
| mkPureApp α e := do | |
| let info := (← read).monadInfo | |
| if (← read).deadCode matches .deadSyntactically then | |
| -- There is no dead syntax here. Just return a fresh metavariable so that we don't | |
| -- do the `Term.ensureHasType` check below. | |
| return ← mkFreshExprMVar (mkApp info.m α) | |
| let α ← Term.ensureHasType (mkSort (mkLevelSucc info.u)) α | |
| let e ← Term.ensureHasType α e | |
| let instPure ← Term.mkInstMVar (mkApp (mkConst ``Pure [info.u, info.v]) info.m) | |
| let instPure ← instantiateMVars instPure | |
| return mkApp4 (mkConst ``Pure.pure [info.u, info.v]) info.m instPure α e | |
| mkBindApp α β e k := do | |
| let info := (← read).monadInfo | |
| let α ← Term.ensureHasType (mkSort (mkLevelSucc info.u)) α | |
| let mα := mkApp info.m α | |
| let e ← Term.ensureHasType mα e | |
| let k ← Term.ensureHasType (← mkArrow α (mkApp info.m β)) k | |
| let instBind ← Term.mkInstMVar (mkApp (mkConst ``Bind [info.u, info.v]) info.m) | |
| return mkApp6 (mkConst ``Bind.bind [info.u, info.v]) info.m instBind α β e k | |
| isPureApp? e := | |
| if e.isAppOfArity ``Pure.pure 4 then some (e.getArg! 3) else none | |
| splitMonadApp? type := do | |
| let .app m resultType := type.consumeMData | return none | |
| unless ← isType resultType do return none | |
| let u ← getDecLevel resultType | |
| let v ← getDecLevel type | |
| return some ({ m, u := u.normalize, v := v.normalize }, resultType) | |
| mkMonadApp α := do | |
| return mkApp (← read).monadInfo.m α | |
| /-- Register the given name as that of a `mut` variable. -/ | |
| def declareMutVar (x : Ident) (erased : Bool) (k : DoElabM α) : DoElabM α := do | |
| let fvar ← getFVarFromUserName x.getId | |
| let mutVar : MutVar := { ident := x, baseId := fvar.fvarId!, erased } | |
| withReader (fun ctx => { ctx with | |
| mutVars := ctx.mutVars.push mutVar, | |
| mutVarDefs := ctx.mutVarDefs.insert x.getId mutVar, | |
| }) k | |
| /-- Register the given names as that of `mut` variables. -/ | |
| def declareMutVars (xs : Array Ident) (erased : Bool) (k : DoElabM α) : DoElabM α := do | |
| let fvars ← xs.mapM (getFVarFromUserName ·.getId) | |
| let newMutVars : Array MutVar := xs.zipWith (fun x fvar => { ident := x, baseId := fvar.fvarId!, erased }) fvars | |
| withReader (fun ctx => { ctx with | |
| mutVars := ctx.mutVars ++ newMutVars, | |
| mutVarDefs := ctx.mutVarDefs.insertMany (newMutVars.map fun mutVar => (mutVar.getId, mutVar)), | |
| }) k | |
| /-- Register the given name as that of a `mut` variable if the syntax token `mut` is present. -/ | |
| def declareMutVar? (mutTk? : Option Syntax) (x : Ident) (erased : Bool) (k : DoElabM α) : DoElabM α := | |
| if mutTk?.isSome then declareMutVar x erased k else k | |
| /-- Register the given names as that of `mut` variables if the syntax token `mut` is present. -/ | |
| def declareMutVars? (mutTk? : Option Syntax) (xs : Array Ident) (erased : Bool) (k : DoElabM α) : DoElabM α := | |
| if mutTk?.isSome then declareMutVars xs erased k else k | |
| /-- Look up a declared `mut` variable by its raw `Name`. -/ | |
| def findMutVar? (n : Name) : DoElabM (Option MutVar) := do | |
| return (← read).mutVarDefs[n]? | |
| /-- Throw an error if the given name is not a declared `mut` variable. -/ | |
| def throwUnlessMutVarDeclared (x : Ident) : DoElabM Unit := do | |
| if (← findMutVar? x.getId).isNone then | |
| let xMsg ← MessageData.ofUserName x.getId | |
| throwErrorAt x "Variable `{xMsg}` cannot be mutated. Only variables declared using `let mut` can be mutated. | |
| If you did not intend to mutate but define `{xMsg}`, consider using `let {xMsg}` instead" | |
| /-- Throw an error if the given names are not declared `mut` variables. -/ | |
| def throwUnlessMutVarsDeclared (xs : Array Ident) : DoElabM Unit := do | |
| for x in xs do throwUnlessMutVarDeclared x | |
| /-- Throw an error if a declaration of the given name would shadow a `mut` variable. -/ | |
| def checkMutVarsForShadowingOne (x : Ident) : DoElabM Unit := do | |
| if let some mutVar ← findMutVar? x.getId then | |
| throwErrorAt x "mutable variable `{mutVar}` cannot be shadowed" | |
| /-- Throw an error if a declaration of the given name would shadow a `mut` variable. -/ | |
| def checkMutVarsForShadowing (xs : Array Ident) : DoElabM Unit := do | |
| for x in xs do checkMutVarsForShadowingOne x | |
| /-- Create a fresh `α` that would fit in `m α`. -/ | |
| def mkFreshResultType (userName := `α) (kind := MetavarKind.natural) : DoElabM Expr := do | |
| mkFreshExprMVar (mkSort (mkLevelSucc (← read).monadInfo.u)) (userName := userName) (kind := kind) | |
| /-- | |
| Like `controlAt TermElabM`, but it maintains the state using the `DoElabM`'s ref cell instead of returning it | |
| in the `TermElabM` result. This makes it possible to run multiple `DoElabM` computations in a row. | |
| -/ | |
| @[inline] | |
| def controlAtTermElabM (k : (runInBase : ∀ {β}, DoElabM β → TermElabM β) → TermElabM α) : DoElabM α := fun ctx => do | |
| k (· ctx) | |
| @[inline] | |
| def mapTermElabM (f : ∀{α}, TermElabM α → TermElabM α) {α} (k : DoElabM α) : DoElabM α := | |
| controlAtTermElabM fun runInBase => f (runInBase <| k) | |
| @[inline] | |
| def map1TermElabM (f : ∀{α}, (β → TermElabM α) → TermElabM α) {α} (k : β → DoElabM α) : DoElabM α := | |
| controlAtTermElabM fun runInBase => f fun b => runInBase <| k b | |
| def withDeadCode (deadCode : CodeLiveness) (k : DoElabM α) : DoElabM α := do | |
| withReader (fun ctx => { ctx with deadCode }) k | |
| /-- | |
| The subset of `mutVars` that were reassigned in any of the `childCtxs` relative to the given | |
| `rootCtx`. | |
| -/ | |
| def getReassignedMutVars (rootCtx : LocalContext) (mutVars : Std.HashSet Name) (childCtxs : Array LocalContext) : Std.HashSet Name := Id.run do | |
| let mut reassignedMutVars := Std.HashSet.emptyWithCapacity mutVars.size | |
| for childCtx in childCtxs do | |
| let newDefs := childCtx.findFromUserNames mutVars.inner (start := rootCtx.numIndices) | |
| reassignedMutVars := reassignedMutVars.insertMany (newDefs.map (·.userName)) | |
| return reassignedMutVars | |
| /-- | |
| Adds the new reaching definitions of the given `tunneledVars` in `childCtx` relative to `rootCtx` as | |
| non-dependent decls. | |
| -/ | |
| private def addReachingDefsAsNonDep (rootCtx childCtx : LocalContext) (tunneledVars : Std.HashMap Name α) : MetaM LocalContext := do | |
| let mut tunnelDecls := childCtx.findFromUserNames tunneledVars (start := rootCtx.numIndices) | |
| -- We must also tunnel any variables that the tunneled vars depend on; hence compute the closure. | |
| let fvars ← (·.2.fvarIds) <$> (tunnelDecls.mapM (Expr.collectFVars ·.type) |>.run {}) | |
| let fvarDecls ← fvars.mapM (·.getDecl) | |
| let fvarDecls := fvarDecls.insertionSort (·.index > ·.index) | |
| |>.takeWhile (·.index >= rootCtx.numIndices) | |
| |>.reverse | |
| tunnelDecls := tunnelDecls.filter fun tun => !fvarDecls.any (·.index == tun.index) | |
| tunnelDecls := fvarDecls ++ tunnelDecls | |
| let mut rootCtx := rootCtx | |
| for decl in tunnelDecls do | |
| rootCtx := rootCtx.addDecl (decl.setNondep true) | |
| return rootCtx | |
| /-- | |
| 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 be elaborated in a local context that contains the reassignment of `x`, but not the shadowing | |
| mut var definition of `y`. | |
| -/ | |
| def withLCtxKeepingMutVarDefs (oldLCtx : LocalContext) (oldCtx : Context) (resultName : Name) (k : DoElabM α) : DoElabM α := do | |
| let oldMutVars := oldCtx.mutVars | |
| let oldMutVarDefs := oldCtx.mutVarDefs | |
| let tunneledDefs := oldMutVarDefs.insert resultName default -- tunneledDefs is used as a set, so the value doesn't matter | |
| let newCtx ← addReachingDefsAsNonDep oldLCtx (← getLCtx) tunneledDefs | |
| withLCtx' newCtx <| withReader (fun ctx => { ctx with | |
| mutVars := oldMutVars, | |
| mutVarDefs := oldMutVarDefs | |
| }) k | |
| def mkMonadicResultTypeMismatchError (contType : Expr) (elementType : Expr) : MessageData := | |
| m!"Type mismatch. The `do` element has monadic result type{indentExpr elementType}\n\ | |
| but the rest of the `do` block has monadic result type{indentExpr contType}" | |
| /-- | |
| Given a continuation `dec`, a reference `ref`, and an element result type `elementType`, returns a | |
| continuation derived from `dec` with result type `elementType`. | |
| If `dec` already has result type `elementType`, simply returns `dec`. | |
| Otherwise, an error is logged and a new continuation is returned that calls `dec` with `sorry` as a | |
| result. The error is reported at `ref`. | |
| -/ | |
| def DoElemCont.ensureHasTypeAt (dec : DoElemCont) (ref : Syntax) (elementType : Expr) : DoElabM DoElemCont := do | |
| if ← isDefEqGuarded dec.resultType elementType then | |
| return dec | |
| let errMessage := mkMonadicResultTypeMismatchError dec.resultType elementType | |
| unless (← readThe Term.Context).errToSorry do | |
| throwErrorAt ref errMessage | |
| logErrorAt ref errMessage | |
| return { | |
| resultName := ← mkFreshUserName `__r | |
| resultType := elementType | |
| k := do | |
| mapLetDecl dec.resultName dec.resultType (← mkSorry dec.resultType true) | |
| (nondep := true) (kind := .implDetail) fun _ => dec.k | |
| kind := dec.kind | |
| } | |
| /-- | |
| Given a continuation `dec` and a reference `ref`, returns a continuation derived from `dec` with result type `PUnit`. | |
| If `dec` already has result type `PUnit`, simply returns `dec`. Otherwise, an error is logged and a | |
| new continuation is returned that calls `dec` with `sorry` as a result. The error is reported at `ref`. | |
| -/ | |
| def DoElemCont.ensureUnitAt (dec : DoElemCont) (ref : Syntax) : DoElabM DoElemCont := do | |
| dec.ensureHasTypeAt ref (← mkPUnit) | |
| /-- | |
| Given a continuation `dec`, returns a continuation derived from `dec` with result type `PUnit`. | |
| If `dec` already has result type `PUnit`, simply returns `dec`. Otherwise, an error is logged and a | |
| new continuation is returned that calls `dec` with `sorry` as a result. | |
| -/ | |
| def DoElemCont.ensureUnit (dec : DoElemCont) : DoElabM DoElemCont := do | |
| dec.ensureUnitAt (← getRef) | |
| /-- | |
| Return `$e >>= fun ($dec.resultName : $dec.resultType) => $(← dec.k)`, cancelling | |
| the bind if `$(← dec.k)` is `pure $dec.resultName` or `e` is some `pure` computation. | |
| -/ | |
| def DoElemCont.mkBindUnlessPure (dec : DoElemCont) (e : Expr) : DoElabM Expr := do | |
| -- let eResultTy ← mkFreshResultType | |
| -- let e ← Term.ensureHasType (← mkMonadApp eResultTy) e | |
| -- let dec ← dec.ensureHasType eResultTy | |
| let x := dec.resultName | |
| let k := dec.k | |
| let eResultTy := dec.resultType | |
| -- The .ofBinderName below is mainly to interpret `__do_lift` binders as implementation details. | |
| let declKind := .ofBinderName x | |
| let kResultTy ← mkFreshResultType `kResultTy | |
| withLocalDecl x .default eResultTy (kind := declKind) fun xFVar => do | |
| let body ← k | |
| let body' := body.consumeMData | |
| let ops := (← read).ops.toDoOps | |
| -- First try to contract `e >>= pure` into `e`. | |
| -- Reason: for `pure e >>= pure`, we want to get `pure e` and not `have xFVar := e; pure xFVar`. | |
| if let some pureArg := ops.isPureApp? body' then | |
| if pureArg == xFVar then | |
| let body'' ← mkPureApp eResultTy xFVar | |
| if ← withNewMCtxDepth do isDefEq body' body'' then | |
| return e | |
| -- Now test whether we can contract `pure e >>= k` into `have xFVar := e; k xFVar`. We zeta `xFVar` when | |
| -- `e` is duplicable; we don't look at `k` to see whether it is used at most once. | |
| let e' := e.consumeMData | |
| if let some eRes := ops.isPureApp? e' then | |
| let e' ← mkPureApp eResultTy eRes | |
| let (isPure, isDuplicable) ← withNewMCtxDepth do | |
| let isPure ← isDefEq e e' | |
| let isDuplicable ← isDefEq eResultTy (← mkPUnit) | |
| -- <||> pure eRes.isFVar -- this is too aggressive; users expect to see "useless binds" after elaboration | |
| return (isPure, isDuplicable) | |
| if isPure then | |
| if isDuplicable then | |
| return ← body.replaceFVarsM #[xFVar] #[eRes] | |
| let body ← Term.ensureHasType (← mkMonadApp kResultTy) body | |
| let k ← mkLambdaFVars #[xFVar] body | |
| mkBindApp eResultTy kResultTy e k | |
| /-- | |
| 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`. | |
| -/ | |
| def DoElemCont.continueWithUnit (dec : DoElemCont) : DoElabM Expr := do | |
| let dec ← dec.ensureUnit | |
| mapLetDeclZeta dec.resultName (← mkPUnit) (← mkPUnitUnit) (nondep := true) (kind := .ofBinderName dec.resultName) fun _ => | |
| dec.k | |
| /-- Elaborate the `DoElemCont` with the `deadCode` flag set to `deadSyntactically` to emit warnings. -/ | |
| def DoElemCont.elabAsSyntacticallyDeadCode (dec : DoElemCont) : DoElabM Unit := | |
| withNewMCtxDepth do | |
| withDeadCode .deadSyntactically do | |
| withLocalDecl dec.resultName .default (← mkFreshResultType) (kind := .implDetail) fun _ => do | |
| let s ← Term.saveState | |
| let log ← Core.getAndEmptyMessageLog | |
| try discard <| dec.k catch _ => pure () | |
| let warnings := MessageLog.getWarningMessages (← Core.getMessageLog) | |
| s.restore | |
| Core.setMessageLog (log ++ warnings) | |
| /-- Wrap `dec.k` so it elaborates as dead iff `info.noFallthrough`. -/ | |
| def DoElemCont.withDeadCodeFromInfo (dec : DoElemCont) (info : ControlInfo) : DoElemCont := | |
| { dec with k := withDeadCode (if info.noFallthrough then .deadSemantically else .alive) dec.k } | |
| /-- | |
| If `x` is a `mut` variable, record that its current binding aliases the original `let mut` binding, | |
| so that go-to-definition and find-references treat both as the same variable. | |
| -/ | |
| def registerMutVarAlias (x : Name) : DoElabM Unit := do | |
| if let some baseMutVar ← findMutVar? x then | |
| let id := (← getFVarFromUserName x).fvarId! | |
| if id != baseMutVar.baseId then | |
| pushInfoLeaf <| .ofFVarAliasInfo (baseMutVar.mkAliasInfo id) | |
| /-- Bind `x` to `carried.out` at the underlying type while `k` runs, and zeta-substitute the | |
| binding away, so the source name reaches proofs and never compiled code. The newest binding of | |
| `x` must be the carried `Erased` binding. -/ | |
| def withErasedProj (x : Ident) (k : DoElabM Expr) (info : Bool := true) : DoElabM Expr := do | |
| let carried ← getLocalDeclFromUserName x.getId | |
| let_expr c@Erased t ← carried.type | |
| | throwError "the carried binding of erased variable `{x.getId}` has type{indentExpr carried.type}\ninstead of an `Erased` type" | |
| let outVal := mkApp2 (mkConst ``Erased.out c.constLevels!) t carried.toExpr | |
| withLetDecl x.getId t outVal (nondep := true) fun xv => do | |
| if info then | |
| Term.addLocalVarInfo x xv | |
| -- Uses of `x` resolve to the projection, so alias it to the variable's base binding for | |
| -- find-references and rename. | |
| let baseId := ((← findMutVar? x.getId).map (·.baseId)).getD carried.fvarId | |
| pushInfoLeaf <| .ofFVarAliasInfo { userName := x.getId, id := xv.fvarId!, baseId } | |
| let body ← k | |
| return (← body.abstractM #[xv]).instantiate1 outVal | |
| /-- Bind the `.out` projection of each erased variable among `mutVars` around `k`. -/ | |
| def withErasedProjs (mutVars : Array MutVar) (k : DoElabM Expr) (info : Bool := true) : DoElabM Expr := | |
| (mutVars.filter (·.erased)).foldr (init := k) fun mv k => withErasedProj mv.ident k info | |
| /-- `Erased.mk e`, which erases `e` in compiled code. -/ | |
| def mkErasedMkApp (e : Expr) : MetaM Expr := do | |
| let t ← inferType e | |
| return mkApp2 (mkConst ``Erased.mk [← getLevel t]) t e | |
| /-- The type of `mv`'s slot in runtime state (tuples, join parameters): an erased variable's slot | |
| carries the `Erased` value. -/ | |
| def MutVar.stateType (mv : MutVar) : MetaM Expr := do | |
| let t := (← getLocalDeclFromUserName mv.getId).type | |
| if mv.erased then return mkApp (mkConst ``Erased [← getLevel t]) t else return t | |
| /-- The current value of `mv` as packed into runtime state. -/ | |
| def MutVar.stateValue (mv : MutVar) : MetaM Expr := do | |
| let v := (← getLocalDeclFromUserName mv.getId).toExpr | |
| if mv.erased then mkErasedMkApp v else return v | |
| /-- | |
| 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. | |
| -/ | |
| def bindMutVarsFromTuple (vars : List Name) (tupleVar : FVarId) (k : DoElabM Expr) : DoElabM Expr := do | |
| let erasedVars := (← read).mutVars.filter fun mv => mv.erased && vars.contains mv.getId | |
| -- Like the erased rebindings themselves, the projections contribute only aliases here. | |
| let k := withErasedProjs erasedVars k (info := false) | |
| go vars tupleVar (← tupleVar.getType) #[] k | |
| where | |
| go vars tupleVar tupleTy letFVars k := do | |
| let tuple := mkFVar tupleVar | |
| match vars with | |
| | [] => mkLetFVars letFVars (← k) | |
| | [x] => | |
| withLetDecl x tupleTy tuple fun xf => do | |
| registerMutVarAlias x | |
| mkLetFVars (letFVars.push xf) (← k) | |
| | [x, y] => | |
| let (fst, fstTy, snd, sndTy) ← getProdFields tuple tupleTy | |
| withLetDecl x fstTy fst fun xf => | |
| withLetDecl y sndTy snd fun yf => do | |
| registerMutVarAlias x | |
| registerMutVarAlias y | |
| mkLetFVars (letFVars.push xf |>.push yf) (← k) | |
| | x :: xs => do | |
| let (fst, fstTy, snd, sndTy) ← getProdFields tuple tupleTy | |
| withLetDecl x fstTy fst fun xf => do | |
| registerMutVarAlias x | |
| withLetDecl (← tupleVar.getUserName) sndTy snd fun r => do | |
| go xs r.fvarId! sndTy (letFVars |>.push xf |>.push r) k | |
| /-- | |
| Backtrackable state for the `TermElabM` monad. | |
| -/ | |
| structure SavedState where | |
| «term» : Term.SavedState | |
| deriving Nonempty | |
| def SavedState.restore (s : SavedState) (restoreInfo : Bool := false) : DoElabM Unit := do | |
| s.term.restore (restoreInfo := restoreInfo) | |
| protected def DoElabM.saveState : DoElabM SavedState := | |
| return { «term» := (← Term.saveState) } | |
| instance : MonadBacktrack SavedState DoElabM where | |
| saveState := DoElabM.saveState | |
| restoreState b := b.restore | |
| def observingPostpone (x : DoElabM α) : DoElabM (Option α) := do | |
| let s ← saveState | |
| try | |
| some <$> x | |
| catch ex => match ex with | |
| | .error .. => throw ex | |
| | .internal id _ => | |
| if id == postponeExceptionId then | |
| s.restore | |
| pure none | |
| else | |
| throw ex | |
| def doElabToSyntaxWithExpectedType (hint : MessageData) (doElab : Option Expr → DoElabM Expr) (k : Term → DoElabM α) (ref : Syntax := .missing) : DoElabM α := | |
| controlAtTermElabM fun runInBase => | |
| Term.elabToSyntax (hint? := hint) (ref := ref) | |
| (fun ty? => runInBase (doElab ty?)) (runInBase ∘ k) | |
| def doElabToSyntax (hint : MessageData) (doElab : DoElabM Expr) (k : Term → DoElabM α) (ref : Syntax := .missing) : DoElabM α := | |
| doElabToSyntaxWithExpectedType hint (fun _ty? => doElab) k ref | |
| /-- | |
| 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. | |
| -/ | |
| def DoElemCont.withDuplicableCont (nondupDec : DoElemCont) (callerInfo : ControlInfo) (caller : DoElemCont → DoElabM Expr) : DoElabM Expr := do | |
| if nondupDec.kind matches .duplicable .. then | |
| return ← caller nondupDec | |
| let γ := (← read).doBlockResultType | |
| let mγ ← mkMonadApp γ | |
| let mutVars := (← read).mutVars |>.filter (callerInfo.reassigns.contains ·.getId) | |
| let joinName ← mkFreshUserName `__do_jp | |
| -- σ is the tuple type of the mut vars, or mγ if jumpCount = 0. Hence it is either level mi.u or mi.v. | |
| -- let σ ← mkFreshTypeMVar (userName := `σ) | |
| -- An erased variable's join parameter carries the `Erased` value; its projection rebinds below. | |
| let mutTypes ← mutVars.mapM (·.stateType) | |
| let joinTy ← mkArrow nondupDec.resultType (← mkArrowN mutTypes mγ) | |
| let joinRhsMVar ← mkFreshExprSyntheticOpaqueMVar joinTy | |
| withLetDecl joinName joinTy joinRhsMVar (kind := .implDetail) (nondep := true) fun jp => do | |
| let mkJump : DoElabM Expr := do | |
| let jp' ← getFVarFromUserName joinName | |
| let result ← getFVarFromUserName nondupDec.resultName | |
| let mut e := mkApp jp' result | |
| for x in mutVars do | |
| Term.addTermInfo' x.ident (← getFVarFromUserName x.getId) | |
| e := mkApp e (← x.stateValue) | |
| return e | |
| let elabBody := | |
| caller { nondupDec with k := mkJump, kind := .duplicable } | |
| -- We need observingPostpone to decouple elaboration problems from the RHS and the body. | |
| let body? : Option Expr ← observingPostpone elabBody | |
| let joinRhs ← joinRhsMVar.mvarId!.withContext do | |
| withLocalDeclD nondupDec.resultName nondupDec.resultType fun r => do | |
| withLocalDeclsDND ((mutVars.zip mutTypes).map fun (mv, t) => (mv.getId, t)) fun muts => do | |
| for (x, newX) in mutVars.zip muts do Term.addTermInfo' x.ident newX | |
| let e ← withErasedProjs mutVars (nondupDec.withDeadCodeFromInfo callerInfo).k | |
| mkLambdaFVars (#[r] ++ muts) e | |
| unless ← joinRhsMVar.mvarId!.checkedAssign joinRhs do | |
| joinRhsMVar.mvarId!.withContext do | |
| throwError "Bug in a `do` elaborator. Failed to assign join point RHS{indentExpr joinRhs}\n\ | |
| to metavariable\n{joinRhsMVar.mvarId!}" | |
| let body ← body?.getDM do | |
| -- Here we unconditionally add a pending MVar. | |
| doElabToSyntax "join point RHS" elabBody (Term.postponeElabTerm · mγ) | |
| if callerInfo.numRegularExits > 1 then | |
| mkLetFVars (generalizeNondepLet := false) #[jp] body | |
| else | |
| return (← body.abstractM #[jp]).instantiateBetaRevRange 0 1 #[joinRhs] | |
| def getReturnCont : DoElabM ReturnCont := do | |
| return (← read).contInfo.toContInfo.returnCont | |
| def getBreakCont : DoElabM (Option (DoElabM Expr)) := do | |
| return (← read).contInfo.toContInfo.breakCont | |
| def getContinueCont : DoElabM (Option (DoElabM Expr)) := do | |
| return (← read).contInfo.toContInfo.continueCont | |
| /-- Set the new `do` block result type for the scope of the continuation `k`. -/ | |
| @[inline] | |
| def withDoBlockResultType (doBlockResultType : Expr) (k : DoElabM α) : DoElabM α := do | |
| withReader (fun ctx => { ctx with doBlockResultType }) k | |
| /-- | |
| Refine the result type of the `do` block and its `return` continuation for the scope of `k`, | |
| as needed for a dependent `match` branch where a bare `return` targets the refined branch type. | |
| Relies on the `return` continuation result type coinciding with the `do` block result type, which | |
| holds for the tail-position dependent `match` this serves. | |
| -/ | |
| def withRefinedResultType (doBlockResultType : Expr) (k : DoElabM α) : DoElabM α := do | |
| let contInfo := (← read).contInfo.toContInfo | |
| let contInfo := { contInfo with returnCont.resultType := doBlockResultType } | |
| withReader (fun ctx => { ctx with doBlockResultType, contInfo := contInfo.toContInfoRef }) k | |
| /-- | |
| 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. | |
| -/ | |
| def enterLoopBody (breakCont continueCont : DoElabM Expr) (returnCont : ReturnCont) (body : DoElabM α) : DoElabM α := do | |
| let contInfo := { (← read).contInfo.toContInfo with returnCont, breakCont, continueCont }.toContInfoRef | |
| withReader (fun ctx => { ctx with contInfo }) body | |
| /-- | |
| Prepare the context for elaborating the body of a `do` block that does not support `mut` vars, | |
| `break`, `continue` or `return`. | |
| -/ | |
| def withoutControl (k : DoElabM Expr) : DoElabM Expr := do | |
| let error := throwError "This `do` block does not support `break`, `continue` or `return`." | |
| let rc ← getReturnCont | |
| let contInfo := { breakCont := error, continueCont := error, returnCont := { rc with k _ := error }} | |
| let contInfo := ContInfo.toContInfoRef contInfo | |
| withReader (fun ctx => { ctx with contInfo }) k | |
| /-- | |
| 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. | |
| -/ | |
| def enterFinally (resultType : Expr) (k : DoElabM Expr) : DoElabM Expr := do | |
| withoutControl do | |
| withDoBlockResultType resultType k | |
| /-- Extracts `MonadInfo` and monadic result type `α` from the expected type of a `do` block `m α`. -/ | |
| private partial def extractMonadInfo (ops : DoOps) (expectedType? : Option Expr) : Term.TermElabM (MonadInfo × Expr) := do | |
| let some expectedType := expectedType? | mkUnknownMonadResult | |
| let expectedType ← instantiateMVars expectedType | |
| let rec extract? (type : Expr) : Term.TermElabM (Option (MonadInfo × Expr)) := do | |
| match (← ops.splitMonadApp? type) with | |
| | some r => return r | |
| | none => | |
| let typeNew ← whnfCore type | |
| if typeNew != type then | |
| extract? typeNew | |
| else | |
| if typeNew.getAppFn.isMVar then | |
| mkUnknownMonadResult | |
| else match (← unfoldDefinition? typeNew) with | |
| | some typeNew => extract? typeNew | |
| | none => return none | |
| match (← extract? expectedType) with | |
| | some r => return r | |
| | none => throwError "invalid `do` notation, expected type is not a monad application{indentExpr expectedType}\nYou can use the `do` notation in pure code by writing `Id.run do` instead of `do`, where `Id` is the identity monad." | |
| where | |
| mkUnknownMonadResult : TermElabM (MonadInfo × Expr) := do | |
| let u ← mkFreshLevelMVar | |
| let v ← mkFreshLevelMVar | |
| let m ← mkFreshExprMVar (← mkArrow (mkSort (mkLevelSucc u)) (mkSort (mkLevelSucc v))) (userName := `m) | |
| let resultType ← mkFreshExprMVar (mkSort (mkLevelSucc u)) (userName := `α) | |
| return ({ m, u, v }, resultType) | |
| /-- Create the `Context` for `do` elaboration from the given expected type of a `do` block. -/ | |
| def mkContext (expectedType? : Option Expr) (ops : DoOps := .default) : TermElabM Context := do | |
| let (mi, resultType) ← extractMonadInfo ops expectedType? | |
| let returnCont ← ReturnCont.mkPure resultType | |
| let contInfo := ContInfo.toContInfoRef { returnCont } | |
| return { monadInfo := mi, doBlockResultType := resultType, contInfo, | |
| ops := ops.toDoOpsRef } | |
| section NestedActions | |
| /-- Return true if we should not lift nested action `(← …)` out of syntax nodes with the given kind. -/ | |
| private def liftNestedActionDelimiter (k : SyntaxNodeKind) : Bool := | |
| k == ``Parser.Term.do || | |
| k == ``Parser.Term.doSeqIndent || | |
| k == ``Parser.Term.doSeqBracketed || | |
| k == ``Parser.Term.termReturn || | |
| k == ``Parser.Term.termUnless || | |
| k == ``Parser.Term.termTry || | |
| k == ``Parser.Term.termFor | |
| private def letDeclArgHasBinders (letDeclArg : Syntax) : Bool := | |
| let k := letDeclArg.getKind | |
| if k == ``Parser.Term.letPatDecl then | |
| false | |
| else if k == ``Parser.Term.letEqnsDecl then | |
| true | |
| else if k == ``Parser.Term.letIdDecl then | |
| -- letIdLhs := letId >> many (ppSpace >> letIdBinder) >> optType | |
| let binders := letDeclArg[1] | |
| binders.getNumArgs > 0 | |
| else | |
| false | |
| /-- Return `true` if the given `letDecl` contains binders. -/ | |
| private def letDeclHasBinders (letDecl : Syntax) : Bool := | |
| letDeclArgHasBinders letDecl[0] | |
| /-- Return true if we should generate an error message when lifting a nested action over this kind of syntax. -/ | |
| private def nestedActionForbiddenBinder (stx : Syntax) : Bool := | |
| let k := stx.getKind | |
| -- TODO: make this extensible in the future. | |
| if k == ``Parser.Term.fun || k == ``Parser.Term.matchAlts || | |
| k == ``Parser.Term.doLetRec || k == ``Parser.Term.letrec then | |
| -- It is never ok to lift over this kind of binder | |
| true | |
| -- The following kinds of `let`-expressions require extra checks to decide whether they contain binders or not | |
| else if k == ``Parser.Term.letDecl then | |
| letDeclHasBinders stx | |
| else | |
| false | |
| -- TODO: we must track whether we are inside a quotation or not. | |
| private partial def hasNestedActionsToLift : Syntax → Bool | |
| | Syntax.node _ k args => | |
| if liftNestedActionDelimiter k then false | |
| -- NOTE: We don't check for lifts in quotations here, which doesn't break anything but merely makes this rare case a | |
| -- bit slower | |
| else if k == ``Parser.Term.nestedAction then true | |
| -- For `pure` if-then-else, we only lift `(<- ...)` occurring in the condition. | |
| else if k == ``termDepIfThenElse || k == ``termIfThenElse then args.size >= 2 && hasNestedActionsToLift args[1]! | |
| else args.any hasNestedActionsToLift | |
| | _ => false | |
| private partial def expandNestedActionsAux (baseId : Name) (inQuot : Bool) (inBinder : Bool) : Syntax → StateT (Array DoElem) DoElabM Syntax | |
| | stx@(Syntax.node i k args) => | |
| if k == choiceKind then do | |
| -- choice node: check that lifts are consistent | |
| let alts ← stx.getArgs.mapM (expandNestedActionsAux baseId inQuot inBinder · |>.run #[]) | |
| let (_, lifts) := alts[0]! | |
| unless alts.all (·.2 == lifts) do | |
| throwErrorAt stx "Cannot lift nested action `{stx}` over inconsistent syntax variants.\nConsider lifting out the binding manually." | |
| modify (· ++ lifts) | |
| return .node i k (alts.map (·.1)) | |
| else if liftNestedActionDelimiter k then | |
| return stx | |
| -- For `pure` if-then-else, we only lift `(<- ...)` occurring in the condition. | |
| else if h : args.size >= 2 ∧ (k == ``termDepIfThenElse || k == ``termIfThenElse) then do | |
| let inAntiquot := stx.isAntiquot && !stx.isEscapedAntiquot | |
| let arg1 ← expandNestedActionsAux baseId (inQuot && !inAntiquot || stx.isQuot) inBinder args[1] | |
| let args := args.set! 1 arg1 | |
| return Syntax.node i k args | |
| else if k == ``Parser.Term.nestedAction && !inQuot then withFreshMacroScope do | |
| if inBinder then | |
| throwErrorAt stx "Cannot lift nested action `{stx}` over a binder.\nThis error usually happens when you are trying to lift a method nested in a `fun`, `let`, or `match`-alternative, and it can often be fixed by adding a missing `do`." | |
| let arg ← expandNestedActionsAux baseId inQuot inBinder args[1]! | |
| -- Pre-stage0-update format stored a term in `args[1]`; the new format stores a doElem. | |
| -- Wrap raw terms in `doExpr` so the subsequent `let _ ← _` quotation parses correctly. | |
| let isDoElem := | |
| (Parser.getParserCategory? (← getEnv) `doElem).any (·.kinds.contains arg.getKind) | |
| let act : DoElem ← | |
| if isDoElem then pure ⟨arg⟩ | |
| else let t : Term := ⟨arg⟩; `(doElem| $t:term) | |
| -- keep name deterministic across choice branches | |
| let id ← mkIdentFromRef (.num baseId (← get).size) | |
| let auxDoElem ← `(doElem| let $id:ident ← $act) | |
| modify fun s => s.push auxDoElem | |
| return id | |
| else do | |
| let inAntiquot := stx.isAntiquot && !stx.isEscapedAntiquot | |
| let inBinder := inBinder || (!inQuot && nestedActionForbiddenBinder stx) | |
| let args ← args.mapM (expandNestedActionsAux baseId (inQuot && !inAntiquot || stx.isQuot) inBinder) | |
| return Syntax.node i k args | |
| | stx => return stx | |
| def expandNestedActions (stx : TSyntax kind) : DoElabM (Array DoElem × TSyntax kind) := do | |
| if !hasNestedActionsToLift stx then | |
| return (#[], stx) | |
| else | |
| let baseId ← withFreshMacroScope (MonadQuotation.addMacroScope `__do_lift) | |
| let (stx, doElemsNew) ← (expandNestedActionsAux baseId false false stx).run #[] | |
| return (doElemsNew, ⟨stx⟩) | |
| end NestedActions | |
| unsafe def mkDoElemElabAttributeUnsafe (ref : Name) : IO (KeyedDeclsAttribute DoElab) := | |
| mkElabAttribute DoElab `builtin_doElem_elab `doElem_elab `Lean.Parser.Term.doElem ``Lean.Elab.Do.DoElab "do element" ref | |
| @[implemented_by mkDoElemElabAttributeUnsafe] | |
| opaque mkDoElemElabAttribute (ref : Name) : IO (KeyedDeclsAttribute DoElab) | |
| /-- | |
| Registers a `do` element elaborator for the given syntax node kind. | |
| A `do` element elaborator should have type `DoElab` (which is | |
| `Lean.Syntax → DoElemCont → DoElabM 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. | |
| -/ | |
| @[builtin_doc] | |
| builtin_initialize doElemElabAttribute : KeyedDeclsAttribute DoElab ← mkDoElemElabAttribute decl_name% | |
| private def withTermInfoContext' (elaborator : Name) (stx : Syntax) (expectedType : Expr) (x : DoElabM Expr) : DoElabM Expr := | |
| controlAtTermElabM fun runInBase => | |
| Term.withTermInfoContext' elaborator stx (expectedType? := expectedType) (runInBase x) | |
| private def elabDoElemFns (stx : DoElem) (cont : DoElemCont) | |
| (fns : List (KeyedDeclsAttribute.AttributeEntry DoElab)) (catchExPostpone : Bool := true) : DoElabM Expr := do | |
| let s ← saveState | |
| match fns with | |
| | [] => throwError "unexpected `do` element syntax{indentD stx}" | |
| | elabFn :: elabFns => | |
| let expectedType ← mkMonadApp (← read).doBlockResultType | |
| withTermInfoContext' elabFn.declName stx (expectedType := expectedType) do | |
| try | |
| elabFn.value stx cont | |
| catch ex => match ex with | |
| | .internal id _ => | |
| if id == unsupportedSyntaxExceptionId then | |
| s.restore | |
| elabDoElemFns stx cont elabFns | |
| else if catchExPostpone && id == postponeExceptionId then | |
| s.restore | |
| doElabToSyntax m!"do element {stx}" (elabFn.value stx cont) (Term.postponeElabTerm · expectedType) | |
| else | |
| throw ex | |
| | _ => throw ex | |
| /-- | |
| Runs `x cont`, which elaborates the `do` element `stx` or its macro expansion, followed by `cont`, | |
| like `Term.withDeprecationContextForSyntax stx`. The rest of the block `cont` is not part of `stx`, | |
| so it is elaborated with the current deprecation checks. | |
| -/ | |
| private def withDeprecationContextForDoElem (stx : Syntax) (cont : DoElemCont) | |
| (x : DoElemCont → DoElabM Expr) : DoElabM Expr := do | |
| unless isDeprecatedSyntax (← getEnv) stx.getKind do | |
| return ← x cont | |
| let checkDeprecated := (← readThe Core.Context).checkDeprecated | |
| Term.withoutCheckDeprecatedSynthetic <| | |
| x { cont with k := withTheReader Core.Context ({ · with checkDeprecated }) cont.k } | |
| private def DoElemCont.mkUnit (k : DoElabM Expr) : DoElabM DoElemCont := do | |
| let unit ← mkPUnit | |
| let r ← mkFreshUserName `__r | |
| return DoElemCont.mk r unit k .nonDuplicable | |
| mutual | |
| partial def elabDoElem (stx : DoElem) (cont : DoElemCont) (catchExPostpone : Bool := true) : DoElabM Expr := do | |
| let k := stx.raw.getKind | |
| trace[Elab.do.step] "do element: {stx}" | |
| checkSystem "do element elaborator" | |
| profileitM Exception "do element elaborator" (decl := k) (← getOptions) <| | |
| withRef stx <| withIncRecDepth <| withFreshMacroScope <| do | |
| let mγ ← mkMonadApp (← read).doBlockResultType | |
| if (← read).deadCode matches .deadSyntactically then | |
| logWarningAt stx "This `do` element and its control-flow region are dead code. Consider removing it." | |
| return ← mkFreshExprMVar mγ (userName := `deadCode) | |
| if (← read).deadCode matches .deadSemantically then | |
| logWarningAt stx "This `do` element and its control-flow region are dead code. Consider refactoring your code to remove it." | |
| withDeadCode .alive do -- we have warned for this doElem. No need to warn for every element of the block | |
| if let some (decl, stxNew) ← Term.expandMacroReportingDeprecated? stx then | |
| return ← withTermInfoContext' decl stx mγ <| | |
| Term.withMacroExpansion stx stxNew <| | |
| withRef stxNew <| withDeprecationContextForDoElem stx cont (elabDoElem ⟨stxNew⟩ ·) | |
| let (doElems, stx) ← expandNestedActions stx | |
| if !doElems.isEmpty then | |
| return ← elabDoElems1 (doElems.push stx) cont | |
| -- After `expandNestedActions`, as syntax with nested actions is elaborated again without them | |
| Term.reportDeprecatedSyntax stx | |
| match doElemElabAttribute.getEntries (← getEnv) k with | |
| | [] => throwError "elaboration function for `{k}` has not been implemented{indentD stx}" | |
| | elabFns => | |
| withDeprecationContextForDoElem stx cont (elabDoElemFns stx · elabFns catchExPostpone) | |
| partial def elabDoElems1 (doElems : Array DoElem) (cont : DoElemCont) (catchExPostpone : Bool := true) : DoElabM Expr := do | |
| if h : doElems.size = 0 then | |
| throwError "Empty array of `do` elements passed to `elabDoElems1`." | |
| else | |
| let back := doElems.back | |
| let initCont ← DoElemCont.mkUnit (fun _ => throwError "always replaced") | |
| let mkCont el k := { initCont with k } | |
| let init := (back, elabDoElem back cont catchExPostpone) | |
| let (_, res) := doElems.pop.foldr (init := init) fun el (prev, k) => | |
| (el, elabDoElem el (mkCont prev k) catchExPostpone) | |
| res | |
| end | |
| partial def elabDoSeq (doSeq : DoSeq) (cont : DoElemCont) (catchExPostpone : Bool := true) : DoElabM Expr := do | |
| let s ← saveState | |
| try | |
| elabDoElems1 (getDoElems doSeq) cont catchExPostpone | |
| catch ex => match ex with | |
| | .internal id _ => | |
| if catchExPostpone && id == postponeExceptionId then | |
| s.restore | |
| let expectedType ← mkMonadApp (← read).doBlockResultType | |
| doElabToSyntax m!"do sequence {doSeq}" (elabDoSeq doSeq cont) (Term.postponeElabTerm · expectedType) | |
| else | |
| throw ex | |
| | _ => throw ex | |
| -- @[builtin_term_elab nestedAction] | |
| def elabNestedAction : Term.TermElab := fun stx _ty? => do | |
| let `(← $_rhs) := stx | throwUnsupportedSyntax | |
| throwErrorAt stx "Nested action `{stx}` must be nested inside a `do` expression." | |
| /-- Elaborate `doSeq` using `ops` for pure/bind construction. -/ | |
| def elabDoWith (ops : DoOps) (doSeq : DoSeq) | |
| (expectedType? : Option Expr) : TermElabM Expr := do | |
| Term.tryPostponeIfNoneOrMVar expectedType? | |
| let ctx ← mkContext expectedType? (ops := ops) | |
| let cont ← DoElemCont.mkPure ctx.doBlockResultType | |
| let res ← elabDoSeq doSeq cont |>.run ctx | |
| -- Synthesizing default instances here is harmful for expressions such as | |
| -- ``` | |
| -- withTraceNode `Meta.Tactic.solveByElim (return m!"{exceptEmoji ·} trying to apply: {e}") do | |
| -- ... (g.apply e cfg) ... | |
| -- ``` | |
| -- Doing so will default the type of `e` to `MessageData` as part of elaborating the `return` | |
| -- expression before elaboration can propagate that `e : Expr` in the `apply` call. | |
| -- Term.synthesizeSyntheticMVarsUsingDefault | |
| trace[Elab.do] "{← instantiateMVars res}" | |
| pure res | |
| -- @[builtin_term_elab «do»] -- once the legacy `do` elaborator has been phased out | |
| def elabDo : Term.TermElab := fun e expectedType? => do | |
| let `(do $doSeq) := e | throwError "unexpected `do` block syntax{indentD e}" | |
| elabDoWith .default doSeq expectedType? |