Skip to content

Commit 75745ff

Browse files
authored
feat: add DoElem and DoSeq syntax abbreviations (#14025)
This PR adds `Lean.DoElem` and `Lean.DoSeq` as abbreviations for ``TSyntax `doElem`` and ``TSyntax `Lean.Parser.Term.doSeq``, mirroring `Lean.Term`, and uses them throughout the `do` elaborator.
1 parent f9f071a commit 75745ff

9 files changed

Lines changed: 33 additions & 25 deletions

File tree

src/Init/Meta/Defs.lean

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,14 @@ Syntax that represents a tactic.
407407
-/
408408
protected abbrev Tactic := TSyntax `tactic
409409
/--
410+
Syntax that represents an element of a `do` sequence.
411+
-/
412+
abbrev DoElem := TSyntax `doElem
413+
/--
414+
Syntax that represents a sequence of `do` elements.
415+
-/
416+
abbrev DoSeq := TSyntax `Lean.Parser.Term.doSeq
417+
/--
410418
Syntax that represents a precedence (e.g. for an operator).
411419
-/
412420
abbrev Prec := TSyntax `prec
@@ -449,7 +457,7 @@ abbrev HexNum := TSyntax hexnumKind
449457

450458
end Syntax
451459

452-
export Syntax (Term Command Prec Prio Ident StrLit CharLit NameLit ScientificLit NumLit HygieneInfo)
460+
export Syntax (Term Command DoElem DoSeq Prec Prio Ident StrLit CharLit NameLit ScientificLit NumLit HygieneInfo)
453461

454462
namespace TSyntax
455463

src/Lean/Elab/BuiltinDo/Basic.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Lean.Elab.Do
1616
open Lean.Parser.Term
1717
open Lean.Meta
1818

19-
def elabDoIdDecl (x : Ident) (xType? : Option Term) (rhs : TSyntax `doElem) (k : DoElabM Expr)
19+
def elabDoIdDecl (x : Ident) (xType? : Option Term) (rhs : DoElem) (k : DoElabM Expr)
2020
(kind : DoElemContKind := .nonDuplicable) : DoElabM Expr := do
2121
let xType ← Term.elabType (xType?.getD (mkHole x))
2222
let lctx ← getLCtx

src/Lean/Elab/BuiltinDo/Match.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ open Lean.Parser.Term
2323
open Lean.Meta
2424
open Lean.Meta.Match
2525

26-
private def elabDoSeqWithRefinedType (type : Expr) (doSeq : TSyntax ``doSeq) (dec : DoElemCont) : DoElabM Expr := do
26+
private def elabDoSeqWithRefinedType (type : Expr) (doSeq : DoSeq) (dec : DoElemCont) : DoElabM Expr := do
2727
let newDoBlockResultType ← withNewMCtxDepth do
2828
let γ ← mkFreshExprMVar (mkSort (← read).monadInfo.u.succ)
2929
unless ← isDefEqGuarded type (← mkMonadApp γ) do

src/Lean/Elab/Do/Basic.lean

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ It is ``elabTerm `(do $e; $rest) = elabDoElem e dec``, where `elabDoElem e ·` i
217217
`do` element `e`, and `dec` is the `DoElemCont` describing the elaboration of the rest of the block
218218
`rest`.
219219
-/
220-
abbrev DoElab := TSyntax `doElem → DoElemCont → DoElabM Expr
220+
abbrev DoElab := DoElem → DoElemCont → DoElabM Expr
221221

222222
structure ReturnCont where
223223
resultType : Expr
@@ -830,7 +830,7 @@ private partial def hasNestedActionsToLift : Syntax → Bool
830830
else args.any hasNestedActionsToLift
831831
| _ => false
832832

833-
private partial def expandNestedActionsAux (baseId : Name) (inQuot : Bool) (inBinder : Bool) : Syntax → StateT (Array (TSyntax `doElem)) DoElabM Syntax
833+
private partial def expandNestedActionsAux (baseId : Name) (inQuot : Bool) (inBinder : Bool) : Syntax → StateT (Array DoElem) DoElabM Syntax
834834
| stx@(Syntax.node i k args) =>
835835
if k == choiceKind then do
836836
-- choice node: check that lifts are consistent
@@ -856,7 +856,7 @@ private partial def expandNestedActionsAux (baseId : Name) (inQuot : Bool) (inBi
856856
-- Wrap raw terms in `doExpr` so the subsequent `let _ ← _` quotation parses correctly.
857857
let isDoElem :=
858858
(Parser.getParserCategory? (← getEnv) `doElem).any (·.kinds.contains arg.getKind)
859-
let act : TSyntax `doElem
859+
let act : DoElem
860860
if isDoElem then pure ⟨arg⟩
861861
else let t : Term := ⟨arg⟩; `(doElem| $t:term)
862862
-- keep name deterministic across choice branches
@@ -871,7 +871,7 @@ private partial def expandNestedActionsAux (baseId : Name) (inQuot : Bool) (inBi
871871
return Syntax.node i k args
872872
| stx => return stx
873873

874-
def expandNestedActions (stx : TSyntax kind) : DoElabM (Array (TSyntax `doElem) × TSyntax kind) := do
874+
def expandNestedActions (stx : TSyntax kind) : DoElabM (Array DoElem × TSyntax kind) := do
875875
if !hasNestedActionsToLift stx then
876876
return (#[], stx)
877877
else
@@ -907,7 +907,7 @@ private def withTermInfoContext' (elaborator : Name) (stx : Syntax) (expectedTyp
907907
controlAtTermElabM fun runInBase =>
908908
Term.withTermInfoContext' elaborator stx (expectedType? := expectedType) (runInBase x)
909909

910-
private def elabDoElemFns (stx : TSyntax `doElem) (cont : DoElemCont)
910+
private def elabDoElemFns (stx : DoElem) (cont : DoElemCont)
911911
(fns : List (KeyedDeclsAttribute.AttributeEntry DoElab)) (catchExPostpone : Bool := true) : DoElabM Expr := do
912912
let s ← saveState
913913
match fns with
@@ -935,7 +935,7 @@ private def DoElemCont.mkUnit (k : DoElabM Expr) : DoElabM DoElemCont := do
935935
return DoElemCont.mk r unit k .nonDuplicable
936936

937937
mutual
938-
partial def elabDoElem (stx : TSyntax `doElem) (cont : DoElemCont) (catchExPostpone : Bool := true) : DoElabM Expr := do
938+
partial def elabDoElem (stx : DoElem) (cont : DoElemCont) (catchExPostpone : Bool := true) : DoElabM Expr := do
939939
let k := stx.raw.getKind
940940
trace[Elab.do.step] "do element: {stx}"
941941
checkSystem "do element elaborator"
@@ -963,7 +963,7 @@ partial def elabDoElem (stx : TSyntax `doElem) (cont : DoElemCont) (catchExPostp
963963
| [] => throwError "elaboration function for `{k}` has not been implemented{indentD stx}"
964964
| elabFns => elabDoElemFns stx cont elabFns catchExPostpone
965965

966-
partial def elabDoElems1 (doElems : Array (TSyntax `doElem)) (cont : DoElemCont) (catchExPostpone : Bool := true) : DoElabM Expr := do
966+
partial def elabDoElems1 (doElems : Array DoElem) (cont : DoElemCont) (catchExPostpone : Bool := true) : DoElabM Expr := do
967967
if h : doElems.size = 0 then
968968
throwError "Empty array of `do` elements passed to `elabDoElems1`."
969969
else
@@ -976,7 +976,7 @@ partial def elabDoElems1 (doElems : Array (TSyntax `doElem)) (cont : DoElemCont)
976976
res
977977
end
978978

979-
partial def elabDoSeq (doSeq : TSyntax ``doSeq) (cont : DoElemCont) (catchExPostpone : Bool := true) : DoElabM Expr := do
979+
partial def elabDoSeq (doSeq : DoSeq) (cont : DoElemCont) (catchExPostpone : Bool := true) : DoElabM Expr := do
980980
let s ← saveState
981981
try
982982
elabDoElems1 (getDoElems doSeq) cont catchExPostpone
@@ -996,7 +996,7 @@ def elabNestedAction : Term.TermElab := fun stx _ty? => do
996996
throwErrorAt stx "Nested action `{stx}` must be nested inside a `do` expression."
997997

998998
/-- Elaborate `doSeq` using `ops` for pure/bind construction. -/
999-
def elabDoWith (ops : DoOps) (doSeq : TSyntax ``doSeq)
999+
def elabDoWith (ops : DoOps) (doSeq : DoSeq)
10001000
(expectedType? : Option Expr) : TermElabM Expr := do
10011001
Term.tryPostponeIfNoneOrMVar expectedType?
10021002
let ctx ← mkContext expectedType? (ops := ops)

src/Lean/Elab/Do/ForwardSyntax.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ form.
2121
-/
2222
public structure ForwardArg where
2323
binders : Array (TSyntax ``funBinder)
24-
body : TSyntax ``doSeq
24+
body : DoSeq
2525

2626
/--
2727
If `e` is a function application whose last argument effect-forwards into the enclosing `do`

src/Lean/Elab/Do/InferControlInfo.lean

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ instance : ToMessageData ControlInfo where
9797
noFallthrough: {info.noFallthrough}, reassigns: {info.reassigns.toList}"
9898

9999
/-- A handler for inferring `ControlInfo` from a `doElem` syntax. Register with `@[doElem_control_info parserName]`. -/
100-
abbrev ControlInfoHandler := TSyntax `doElem → TermElabM ControlInfo
100+
abbrev ControlInfoHandler := DoElem → TermElabM ControlInfo
101101

102102
unsafe def mkControlInfoElemAttributeUnsafe (ref : Name) : IO (KeyedDeclsAttribute ControlInfoHandler) :=
103103
mkElabAttribute ControlInfoHandler `builtin_doElem_control_info `doElem_control_info
@@ -109,7 +109,7 @@ opaque mkControlInfoElemAttribute (ref : Name) : IO (KeyedDeclsAttribute Control
109109
/--
110110
Registers a `ControlInfo` inference handler for the given `doElem` syntax node kind.
111111
112-
A handler should have type `ControlInfoHandler` (i.e. `TSyntax \`doElem → TermElabM ControlInfo`).
112+
A handler should have type `ControlInfoHandler` (i.e. `DoElem → TermElabM ControlInfo`).
113113
For pure handlers, use `fun stx => return ControlInfo.pure`.
114114
-/
115115
@[builtin_doc]
@@ -121,7 +121,7 @@ namespace InferControlInfo
121121
open InternalSyntax in
122122
mutual
123123

124-
partial def ofElem (stx : TSyntax `doElem) : TermElabM ControlInfo := do
124+
partial def ofElem (stx : DoElem) : TermElabM ControlInfo := do
125125
let env ← getEnv
126126
if let some (_decl, stxNew?) ← liftMacroM (expandMacroImpl? env stx) then
127127
let stxNew ← liftMacroM <| liftExcept stxNew?
@@ -251,7 +251,7 @@ partial def ofLetOrReassignArrow (reassignment : Bool) (decl : TSyntax [``doIdDe
251251
ofLetOrReassign reassigns rhs otherwise? body??.join
252252
| _ => throwError "Not a let or reassignment declaration: {toString decl}"
253253

254-
partial def ofLetOrReassign (reassigned : Array Ident) (rhs? : Option (TSyntax `doElem))
254+
partial def ofLetOrReassign (reassigned : Array Ident) (rhs? : Option DoElem)
255255
(otherwise? : Option (TSyntax ``doSeqIndent)) (body? : Option (TSyntax ``doSeqIndent))
256256
: TermElabM ControlInfo := do
257257
let rhs ← match rhs? with | none => pure { : ControlInfo } | some rhs => ofElem rhs
@@ -260,23 +260,23 @@ partial def ofLetOrReassign (reassigned : Array Ident) (rhs? : Option (TSyntax `
260260
let info := rhs.sequence (body.alternative otherwise)
261261
return { info with reassigns := (reassigned.map TSyntax.getId).foldl NameSet.insert info.reassigns }
262262

263-
partial def ofSeq (stx : TSyntax ``doSeq) : TermElabM ControlInfo := do
263+
partial def ofSeq (stx : DoSeq) : TermElabM ControlInfo := do
264264
let mut info : ControlInfo := {}
265265
for elem in getDoElems stx do
266266
info := info.sequence (← ofElem elem)
267267
return info
268268

269-
partial def ofOptionSeq (stx? : Option (TSyntax ``doSeq)) : TermElabM ControlInfo := do
269+
partial def ofOptionSeq (stx? : Option DoSeq) : TermElabM ControlInfo := do
270270
match stx? with | none => pure { : ControlInfo } | some stx => ofSeq stx
271271

272272
end
273273

274274
end InferControlInfo
275275

276276
/-- Infer the `ControlInfo` of `doSeq`. -/
277-
def inferControlInfoSeq (doSeq : TSyntax ``doSeq) : TermElabM ControlInfo :=
277+
def inferControlInfoSeq (doSeq : DoSeq) : TermElabM ControlInfo :=
278278
InferControlInfo.ofSeq doSeq
279279

280280
/-- Infer the `ControlInfo` of a single doElem. -/
281-
def inferControlInfoElem (doElem : TSyntax `doElem) : TermElabM ControlInfo :=
281+
def inferControlInfoElem (doElem : DoElem) : TermElabM ControlInfo :=
282282
InferControlInfo.ofElem doElem

src/Lean/Meta/Sym/SymM.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def reportIssue (msg : MessageData) : SymM Unit := do
301301
if (← getConfig).verbose then
302302
reportIssue msg
303303

304-
private meta def expandReportIssueMacro (s : Syntax) : MacroM (TSyntax `doElem) := do
304+
private meta def expandReportIssueMacro (s : Syntax) : MacroM DoElem := do
305305
let msg ← if s.getKind == interpolatedStrKind then `(m! $(⟨s⟩)) else `(($(⟨s⟩) : MessageData))
306306
`(doElem| Sym.reportIssueIfVerbose $msg)
307307

@@ -315,7 +315,7 @@ macro "reportIssue!" s:(interpolatedStr(term) <|> term) : doElem => do
315315
if sym.debug.get (← getOptions) then
316316
reportIssue msg
317317

318-
meta def expandReportDbgIssueMacro (s : Syntax) : MacroM (TSyntax `doElem) := do
318+
meta def expandReportDbgIssueMacro (s : Syntax) : MacroM DoElem := do
319319
let msg ← if s.getKind == interpolatedStrKind then `(m! $(⟨s⟩)) else `(($(⟨s⟩) : MessageData))
320320
`(doElem| Sym.reportDbgIssue $msg)
321321

src/Lean/Parser/Do.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ builtin_initialize
4949
register_parser_alias doSeq
5050
register_parser_alias termBeforeDo
5151

52-
def getDoElems (doSeq : TSyntax ``doSeq) : Array (TSyntax `doElem) :=
52+
def getDoElems (doSeq : DoSeq) : Array DoElem :=
5353
if doSeq.raw.getKind == ``Parser.Term.doSeqBracketed then
5454
doSeq.raw[1].getArgs.map fun arg => ⟨arg[0]⟩
5555
else if doSeq.raw.getKind == ``Parser.Term.doSeqIndent then

src/Lean/Util/Trace.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def registerTraceClass (traceClassName : Name) (inherited := false) (ref : Name
387387
if inherited then
388388
inheritedTraceOptions.modify (·.insert optionName)
389389

390-
private meta def expandTraceMacro (id : Syntax) (s : Syntax) : MacroM (TSyntax `doElem) := do
390+
private meta def expandTraceMacro (id : Syntax) (s : Syntax) : MacroM DoElem := do
391391
let msg ← if s.getKind == interpolatedStrKind then `(m! $(⟨s⟩)) else `(($(⟨s⟩) : MessageData))
392392
`(doElem| do
393393
let cls := $(quote id.getId.eraseMacroScopes)

0 commit comments

Comments
 (0)