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

41 lines
1.1 KiB
Text

set_option backward.do.legacy false
-- Refines the free variable `n` and the result type of the `do` block:
example (x : Nat) (n : Fin (x + 1)) : Id (Fin (x + 1)) := do
match (dependent := true) x with
| 42 => pure 40
| _ => pure n
-- Just refines the free variable `n`:
example (x : Nat) (n : Fin x) : Id Bool := do
match (dependent := true) x with
| 0 => n.elim0
| _ => pure true
/--
error: Dependent match is not supported when the result type of the `do` block ⏎
Bool
is different to the result type of the `match` ⏎
Fin (x + 1)
-/
#guard_msgs (error) in
example (x : Nat) (n : Fin (x + 1)) : Id Bool := do
let y : Fin (x + 1) ←
match (dependent := true) x with
| 42 => pure 40
| _ => pure 0
return y % 2 == 0
/--
error: Dependent match is not supported when the result type of the `do` block ⏎
Bool
is different to the result type of the `match` ⏎
?m.7
-/
#guard_msgs (error) in
example (x : Nat) (n : Fin (x + 1)) : Id Bool := do
let y ←
match (dependent := true) x with
| 42 => pure 40
| _ => pure 0
return y % 2 == 0