lean4-htt/tests/lean/run/doElemControlInfoAttribute.lean
Sebastian Graf 4278038940
feat: new, extensible do elaborator (#12459)
This PR adds a new, extensible `do` elaborator. Users can opt into the
new elaborator by unsetting the option `backward.do.legacy`.

New elaborators for the builtin `doElem` syntax category can be
registered with attribute `doElem_elab`. For new syntax, additionally a
control info handler must be registered with attribute
`doElem_control_info` that specifies whether the new syntax `return`s
early, `break`s, `continue`s and which `mut` vars it reassigns.

Do elaborators have type ``TSyntax `doElem → DoElemCont → DoElabM
Expr``, where `DoElabM` is essentially `TermElabM` and the `DoElemCont`
represents how the rest of the `do` block is to be elaborated. Consult
the docstrings for more details.

Breaking Changes:
* The syntax for `let pat := rhs | otherwise` and similar now scope over
the `doSeq` that follows. Furthermore, `otherwise` and the sequence that
follows are now `doSeqIndented` in order not to steal syntax from record
syntax.
 
Breaking Changes when opting into the new `do` elaborator by unsetting
`backward.do.legacy`:
* `do` notation now always requires `Pure`.
* `do match` is now always non-dependent. There is `do match (dependent
:= true)` that expands to a
  term match as a workaround for some dependent uses.
2026-02-21 17:17:29 +00:00

64 lines
1.8 KiB
Text

import Lean
open Lean Parser Meta Elab Do
set_option backward.do.legacy false
syntax (name := myReturn) "myreturn" : doElem
@[doElem_elab myReturn] def elabMyReturn : DoElab := fun _stx dec => do
elabDoElem (← `(doElem| return 42)) dec
/--
error: No `ControlInfo` inference handler found for `myReturn` in syntax ⏎
myreturn
Register a handler with `@[doElem_control_info myReturn]`.
-/
#guard_msgs (error) in
#eval Id.run do
for i in [1, 2, 3] do
myreturn
return 0
@[doElem_control_info myReturn] def controlInfoMyReturn : ControlInfoHandler := fun _stx => do
return { numRegularExits := 0, returnsEarly := true }
/-- info: 42 -/
#guard_msgs in
#eval Id.run do
for i in [1, 2, 3] do
myreturn
return 0
syntax (name := myIf) "myif" term " then" doSeq " else" doSeq : doElem
@[doElem_elab myIf] def elabMyIf : DoElab := fun stx dec => do
let `(doElem| myif $cond:term then $thenSeq else $elseSeq) := stx | throwUnsupportedSyntax
elabDoElem (← `(doElem| if $cond then $thenSeq else $elseSeq)) dec
/--
error: No `ControlInfo` inference handler found for `myIf` in syntax ⏎
myif i > 1 then
return 23 else
continue
Register a handler with `@[doElem_control_info myIf]`.
-/
#guard_msgs (error) in
#eval Id.run do
for i in [1, 2, 3] do
myif i > 1 then return 23 else continue
return 0
@[doElem_control_info myIf] def controlInfoMyIf : ControlInfoHandler := fun stx => do
let `(doElem| myif $_:term then $thenSeq else $elseSeq) := stx | throwUnsupportedSyntax
let thenInfo ← inferControlInfoSeq thenSeq
let elseInfo ← inferControlInfoSeq elseSeq
return thenInfo.alternative elseInfo
/-- info: 26 -/
#guard_msgs in
#eval Id.run do
let mut x := 0
for i in [1, 2, 3] do
myif i > 2 then return x + 23 else x := x + i; continue
return 0