Skip to content

feat: allow omission of => ?_ in induction/cases tactics - #7830

Merged
kmill merged 2 commits into
leanprover:masterfrom
kmill:kmill_induction_no_arrow
Apr 15, 2025
Merged

kmill merged 2 commits into
leanprover:masterfrom
kmill:kmill_induction_no_arrow

Conversation

@kmill

@kmill kmill commented Apr 5, 2025 •

Copy link
Copy Markdown
Collaborator

This PR modifies the syntax of induction, cases, and other tactics that use Lean.Parser.Tactic.inductionAlts. If a case omits => ... then it is assumed to be => ?_. Example:

example (p : Nat × Nat) : p.1 = p.1 := by
  cases p with | _ p1 p2
  /-
  case mk
  p1 p2 : Nat
  ⊢ (p1, p2).fst = (p1, p2).fst
  -/

This works with multiple cases as well. Example:

example (n : Nat) : n + 1 = 1 + n := by
  induction n with | zero | succ n ih
  /-
  case zero
  ⊢ 0 + 1 = 1 + 0
  
  case succ
  n : Nat
  ih : n + 1 = 1 + n
  ⊢ n + 1 + 1 = 1 + (n + 1)
  -/

The induction n with | zero | succ n ih is short for induction n with | zero | succ n ih => ?_, which is short for induction n with | zero => ?_ | succ n ih => ?_. Note that a consequence of parsing is that only the last alternative can omit =>. Any =>-free alternatives before an alternative with => will be a part of that alternative.

Rationale:

  • In the future we may require tacticSeq to be indented. For one-constructor types, this lets the rest of the tactic sequence not need indentation.
  • This is a semi-structured alternative to the cases'/induction' tactics in mathlib.

@kmill kmill added the changelog-language Language features and metaprograms label Apr 5, 2025
@kmill
kmill requested a review from kim-em as a code owner April 5, 2025 21:48
@kmill kmill added the changes-stage0 Contains stage0 changes, merge manually using rebase label Apr 5, 2025
@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 Apr 5, 2025
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Apr 5, 2025
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Apr 5, 2025
@ghost ghost added the builds-mathlib CI has verified that Mathlib builds against this PR label Apr 5, 2025
@ghost

ghost commented Apr 5, 2025 •

Copy link
Copy Markdown

Mathlib CI status (docs):

  • ✅ Mathlib branch lean-pr-testing-7830 has successfully built against this PR. (2025-04-05 23:05:14) View Log
  • ✅ Mathlib branch lean-pr-testing-7830 has successfully built against this PR. (2025-04-06 20:51:28) View Log
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 0076ba03d41795b95919c93e68717b21cf8664b5 --onto deef1c2739a331899b0e536312e65e9815059014. You can force Mathlib CI using the force-mathlib-ci label. (2025-04-14 22:01:06)
  • ✅ Mathlib branch lean-pr-testing-7830 has successfully built against this PR. (2025-04-15 19:02:44) View Log

ghost pushed a commit to leanprover-community/batteries that referenced this pull request Apr 6, 2025
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Apr 6, 2025
kmill added a commit to kmill/lean4 that referenced this pull request Apr 14, 2025
This PR adds the tactic implementation for leanprover#7830, before changing the syntax after a stage0 update. It will allow optional RHSs in induction cases.
github-merge-queue Bot pushed a commit that referenced this pull request Apr 14, 2025
This PR adds the tactic implementation for #7830, before changing the
syntax after a stage0 update. It will allow optional RHSs in induction
cases.
@kmill
kmill force-pushed the kmill_induction_no_arrow branch from 5faa9cd to 0a35c43 Compare April 14, 2025 21:29
@kmill kmill removed the changes-stage0 Contains stage0 changes, merge manually using rebase label Apr 14, 2025
kmill added 2 commits April 15, 2025 10:39
feat: allow omission of `=> ?_` in `induction`/`cases`

This PR modifies the syntax of `induction`, `cases`, and other tactics that use `Lean.Parser.Tactic.inductionAlts`. If a case omits `=> ...` then it is assumed to be `=> ?_`. For example,
```
example (p : Nat × Nat) : p.1 = p.1 := by
  cases p with | _ p1 p2
  /-
  case mk
  p1 p2 : Nat
  ⊢ (p1, p2).fst = (p1, p2).fst
  -/
```
This works with multiple cases as well. For example, `induction n with | zero | succ n ih` is short for `induction n with | zero | succ n ih => ?_` is short for `induction n with | zero => ?_ | succ n ih => ?_`. Note that the grammar makes it so that only the last alternative can omit `=>`. Any `=>`-free alternatives before an alternative with `=>` will take that `=>`.

fix macro expansion loop
@kmill
kmill force-pushed the kmill_induction_no_arrow branch from 0a35c43 to d8be4c7 Compare April 15, 2025 17:54
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Apr 15, 2025
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Apr 15, 2025
@kmill
kmill added this pull request to the merge queue Apr 15, 2025
Merged via the queue into leanprover:master with commit dd84829 Apr 15, 2025
kmill added a commit to kmill/lean4 that referenced this pull request Jul 25, 2025
This PR fixes a bug introduced in leanprover#7830 where if the cursor is at the indicated position
```lean
example (as bs : List Nat) : (as.append bs).length = as.length + bs.length := by
  induction as with
  | nil => -- cursor
  | cons b bs ih =>
```
then the Infoview would show "no goals".
github-merge-queue Bot pushed a commit that referenced this pull request Jul 26, 2025
… and other tactic info improvements (#9553)

This PR fixes a bug introduced in #7830 where if the cursor is at the
indicated position
```lean
example (as bs : List Nat) : (as.append bs).length = as.length + bs.length := by
  induction as with
  | nil => -- cursor
  | cons b bs ih =>
```
then the Infoview would show "no goals" rather than the `nil` goal. The
PR also fixes a separate bug where placing the cursor on the next line
after the `induction`/`cases` tactics like in
```lean
  induction as with
  | nil => sorry
  | cons b bs ih => sorry
  I -- < cursor
```
would report the original goal in the goal list. Furthermore, there are
numerous improvements to error recovery (including `allGoals`-type logic
for pre-tactics) and the visible tactic states when there are errors.
Adds `Tactic.throwOrLogErrorAt`/`Tactic.throwOrLogError` for throwing or
logging errors depending on the recovery state.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-mathlib CI has verified that Mathlib builds against this PR changelog-language Language features and metaprograms 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.

1 participant