Skip to content

feat: enforce indentation of command contents - #3215

Closed
Kha wants to merge 5 commits into
leanprover:masterfrom
Kha:body-indent
Closed

Kha wants to merge 5 commits into
leanprover:masterfrom
Kha:body-indent

Conversation

@Kha

@Kha Kha commented Jan 24, 2024

Copy link
Copy Markdown
Member

Only affects parts using colGt/Ge, e.g. arguments to functions

See test case for motivation

Only affects parts using `colGt/Ge`, e.g. arguments to functions
@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Jan 24, 2024
@Kha

Kha commented Jan 24, 2024

Copy link
Copy Markdown
Member Author

Sadly this breaks completely unindented tactic blocks as evidenced by the tests, which we might not be ready to do

theorem tst0' {p q : Prop } (h : p ∨ q) : q ∨ p := by
induction h

The two non-tactic cases that I would have less qualms about breaking:

def run1 (i : Nat) (n : Nat) (xs : List Nat) : Nat :=
n.repeat (fun r =>
  dbg_trace ">> [{i}] {r}";
  xs.foldl (fun a b => a + b) r)
0
#synth AppendList
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211]

@ghost

ghost commented Jan 25, 2024 •

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ❗ Std/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. (2024-01-25 15:29:59)
  • 💥 Mathlib branch lean-pr-testing-3215 build failed against this PR. (2024-02-01 09:46:57) View Log
  • 💥 Mathlib branch lean-pr-testing-3215 build failed against this PR. (2024-02-01 10:29:40) View Log

@kim-em

kim-em commented Jan 31, 2024

Copy link
Copy Markdown
Collaborator

I would be happy to make those examples break, and the error message "expected indented tactic sequence" is clear and actionable.

@Kha

Kha commented Feb 1, 2024

Copy link
Copy Markdown
Member Author

@kmill @digama0 Could I get your input on potential interactions of this PR with the empty tactic sequences proposal? The issue I see is that unindented input like above yields an actionable error message with this PR as mentioned by @semorrison but would presumably result in unexpected token "induction", expected command with the combination of the two as we would successfully parse an empty by instead. I believe this would be terrible UX for beginners who often don't consider proper indentation yet and nothing in the error message would point them to it anymore.

ghost pushed a commit to leanprover-community/batteries that referenced this pull request Feb 1, 2024
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Feb 1, 2024
@ghost ghost added breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan full-ci labels Feb 1, 2024
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Feb 1, 2024
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Feb 1, 2024
@digama0

digama0 commented Feb 1, 2024 •

Copy link
Copy Markdown
Collaborator

My view is that if we go forward with this then we should recognize that the style of no-indent after := is basically no longer legal. This isn't entirely true, but the examples you have shown above mean that we can't really recommend it at all.

The issue of terrible parser error messages is a hard one and probably needs significant additional work to deal with. This wouldn't be the first time a slight mis-indentation results in a completely crazy error message. Strictly speaking though, shouldn't the correct (generic) parser error message be "expected indented tactic sequence or command"? (Or infix operator and probably many other things.)

One possible mechanism we could introduce to help with the terrible error messages is to have a parser combinator which says "parse nothing, but if there is an error before the next token add this to the 'expected' list". Then by could have the form "by" ((tacticSeq expectHint("tactic")) <|> expectHint("indented tactic")).

@nomeata

nomeata commented Apr 1, 2026 •

Copy link
Copy Markdown
Collaborator

Looking at this PR again, I think the possible benefits – in particular better error messages – are high. Any syntax that uses colGt probably intents some form of indentation, so it seems reasonable to always have a something.

I’ve revived this in #13229.

nomeata added a commit that referenced this pull request Apr 1, 2026
…approach)

This uses `withPosition commandParser` at the top level to enforce
indentation in `by` blocks, combined with an empty-by fallback for
better error messages. Compare with checkColPosGt approach in #13217.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@Kha Kha closed this Apr 1, 2026
nomeata added a commit that referenced this pull request Apr 2, 2026
…approach)

This uses `withPosition commandParser` at the top level to enforce
indentation in `by` blocks, combined with an empty-by fallback for
better error messages. Compare with checkColPosGt approach in #13217.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
github-merge-queue Bot pushed a commit that referenced this pull request Apr 8, 2026
…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 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
wkrozowski pushed a commit to wkrozowski/lean4 that referenced this pull request Apr 10, 2026
…eanprover#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 leanprover#3215 (which introduced `withPosition commandParser` but
without the empty-by fallback). It is also related to leanprover#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 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
volodeyka pushed a commit that referenced this pull request Apr 16, 2026
…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 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
nomeata added a commit that referenced this pull request May 13, 2026
…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 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants