lean4-htt/tests/elab_fail/namedHoles.lean
Joachim Breitner 06fb4bec52
feat: require indentation in commands, allow empty tactic sequences (#13229)
This PR wraps the top-level command parser with `withPosition` to
enforce indentation in `by` blocks, combined with an empty-by fallback
for better error messages.

This subsumes #3215 (which introduced `withPosition commandParser` but
without the empty-by fallback). It is also related to #9524, which
explores elaboration with empty tactic sequences — this PR reuses that
idea for the empty-by fallback, so that a `by` not followed by an
indented tactic produces an elaboration error (unsolved goals) rather
than a parse error.

**Changes:**
- `topLevelCommandParserFn` now uses `(withPosition commandParser).fn`,
setting the saved position at the start of each top-level command
- `tacticSeqIndentGt` gains an empty tactic sequence fallback
(`pushNone`) so that missing indentation produces an elaboration error
(unsolved goals) instead of a parse error
- `isEmptyBy` in `goalsAt?` removed: with strict `by` indentation, empty
`by` blocks parse successfully via `pushNone` (producing empty nodes)
rather than producing `.missing` syntax, making the `isEmptyBy` check
dead code. The `isEmpty` helper in `isSyntheticTacticCompletion`
continues to work correctly because it handles both `.missing` and empty
nodes from `pushNone` (via the vacuously-true `args.all isEmpty` on
`#[]`)
- Test files updated to indent `by` blocks and expression continuations
that were previously at column 0

**Behavior:**
- Top-level `by` blocks now require indentation (column > 0 for commands
at column 0)
- Commands indented inside `section` require proofs to be indented past
the command's column
- `#guard_msgs in example : True := by` works because tactic indentation
is checked against the outermost command's column
- Expression continuations (not just `by`) must also be indented past
the command, which is slightly more strict but more consistent
- `have : True := by` followed by a dedent now correctly puts `this` in
scope in the outer tactic block (the `have` is structurally complete
with an unsolved-goal error, rather than a parse error)

**Code changes observed in practice (lean4 test suite + Mathlib):**

- `by` blocks: top-level `theorem ... := by` / `decreasing_by` followed
by tactics at column 0 must be indented
- `variable` continuations: `variable {A : Type*} [Foo A]\n{B : Type*}`
where the second line starts at column 0 must be indented (most common
category in Mathlib)
- Expression continuations: `def f : T :=\nexpr` or `#synth Foo\n[args]`
where the body/arguments start at column 0
- Structure literals: `.symm\n{ toFun := ...` where the struct literal
starts at column 0

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-08 14:05:47 +00:00

58 lines
1.4 KiB
Text

def f (x : Nat) (y : Bool) :=
x + if y then 1 else 0
def g (x y : Nat) :=
x + y
#check f ?x ?x -- error the first occurrence (?x : Nat) and the second (?x : Bool)
#check g ?x ?x -- ok
def h1 (x : Nat) : Nat := by
refine g ?hole ?hole; -- it is the same hole
case hole => exact x
#eval h1 10
theorem ex1 : h1 10 = 20 :=
rfl
def h2 (x : Nat) : Nat := by
refine g ?hole ?hole;
exact x+x
theorem ex2 : h2 10 = 40 :=
rfl
def foo (f : Nat → Nat) (x : Nat) := f x
def bla (x : Nat) (f : Nat → Nat) := f x
def boo (f : Nat → Nat) (g : Bool → Nat) := f (g true)
#check foo (fun x => ?hole) ?hole
#check bla ?hole (fun x => ?hole)
#check boo (fun x => ?hole) (fun y => ?hole) -- error the local contexts of the two holes are incompatible
def h3 (x : Nat) : Nat := by
apply boo;
case f => refine fun y => ?hole + 1; exact x; -- `fun y => ?hole + 1` and assigned `?hole := x`
case g => refine fun b => ?hole -- `fun b => ?hole` it works because assignment is compatible
#eval h3 10
theorem ex3 : h3 10 = 11 := rfl
def h4 (x : Nat) : Nat := by
refine foo (fun y => ?hole + 2) ?hole;
-- note that the local context of ?hole has be shrunk by the second occurrence
exact x
#eval h4 10
theorem ex4 : h4 10 = 12 := rfl
def h5 (x : Nat) : Nat := by
apply boo;
case f => intro y; refine ?hole + 1; exact y; -- `fun y => ?hole + 1` and assigned `?hole := y`
case g => refine fun b => ?hole -- error