lean4-htt/tests/lean/run/11337.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

53 lines
1.2 KiB
Text

import Lean
open Lean
set_option backward.do.legacy false -- only fixed in the new do elaborator
structure Foo (n : Nat) where
(l : List Nat)
(h : n = n)
def foo (n : Nat) : MetaM Unit := do
let mut result : Foo n := ⟨[7], rfl⟩
trace[Meta.Tactic.simp] "{result.l}"
result := ⟨List.range n, rfl⟩
trace[Meta.Tactic.simp] "{result.l}"
match n with
| _ => trace[Meta.Tactic.simp] "{result.l}"
set_option trace.Meta.Tactic.simp true
/--
trace: [Meta.Tactic.simp] [7]
[Meta.Tactic.simp] [0, 1, 2, 3, 4]
[Meta.Tactic.simp] [0, 1, 2, 3, 4]
-/
#guard_msgs in
run_meta do
foo 5
def bar (n : Nat) : MetaM (List Nat) := do
let mut result : Foo n := ⟨[7], rfl⟩
trace[Meta.Tactic.simp] "{result.l}"
result := ⟨List.range n, rfl⟩
trace[Meta.Tactic.simp] "{result.l}"
have : Foo n := ⟨[7], rfl⟩
match n with
| 0 => pure (); result := ⟨[10], rfl⟩
| _+1 => result := ⟨[6], rfl⟩
trace[Meta.Tactic.simp] "{result.l}"
return result.l
set_option trace.Meta.Tactic.simp true
/--
trace: [Meta.Tactic.simp] [7]
[Meta.Tactic.simp] []
[Meta.Tactic.simp] [10]
[Meta.Tactic.simp] [7]
[Meta.Tactic.simp] [0, 1, 2, 3, 4]
[Meta.Tactic.simp] [6]
-/
#guard_msgs in
run_meta do
discard <| bar 0
discard <| bar 5