lean4-htt/tests/lean/notNaturalNumbersGame.lean
Robert J. Simmons 75d79819c3
feat: catch and provide context for misuse of NNG-style induction pattern (#11347)
This PR adds a focused error explanation aimed at the case where someone
tries to use Natural-Numbers-Game-style `induction` proofs directly in
Lean, where such proofs are not syntactically valid.

## Discussion

The natural numbers game uses a syntax that overlaps with Lean's
`induction` syntax despite having more structural similarity to
`induction'`. This means that fully correct proofs in the natural
numbers game, like this...

```lean4
import Mathlib
theorem zero_mul (m : ℕ) : 0 * m = 0 := by
  induction m with n n_ih
  rw [mul_zero]
  rfl
  rw [mul_succ]
  rw [add_zero]
  rw [n_ih]
  rfl
```

...have completely baffling error messages from a newcomers'
perspective:

```
notNaturalNumbersGame.lean:3:20: error: unknown tactic
notNaturalNumbersGame.lean:3:2: error: Alternative `zero` has not been provided
notNaturalNumbersGame.lean:3:2: error: Alternative `succ` has not been provided
```

(the Mathlib import here only provides the `ℕ` syntax here; equivalently
`ℕ` could be renamed to `Nat` and the import could be removed, [like
this](https://live.lean-lang.org/#codez=C4Cwpg9gTmC2AEAvMUIH1YFcA28AUCAXPAHICGwAlPMQAzwBU8CAvPPYWwEYCeAUPHgBLAHYATTAGNgQiCObwA7kNDx5ItEJAD4URfADaWbGmSoAujqgAzbFf1GcaAM5TJlwXsNkxY0yggPXQcNLSCbbCA))

There are many problems with this proof from the perspective of "stock"
Lean, but the error messages in the `induction` case are particularly
unfriendly and provide no guidance from a NNG learner's perspective.

This PR provides more information about what is wrong:

```
notNaturalNumbersGame.lean:3:20: error: unknown tactic
notNaturalNumbersGame.lean:3:14: error(lean.inductionWithNoAlts): Invalid syntax for induction tactic: The `with` keyword must followed by a tactic or by an alternative (e.g. `| zero =>`), but here it is followed by the identifier `n`.
```

The error explanation it links to explicitly flags the transition of
NNG-style proofs to Lean as the likely culprit, and gives an example of
an effective translation.
2025-11-25 18:44:40 +00:00

25 lines
685 B
Text

-- Catch special case of induction pattern that uses Mathlib's alternative `induction'` syntax,
-- which is used in the Natural Numbers game as just `induction`, overlapping with the parser for
-- Lean's default tactic
theorem zero_mul (m : Nat) : 0 * m = 0 := by
induction m with n n_ih
rw [Nat.mul_zero]
rw [Nat.mul_succ]
rw [Nat.add_zero]
rw [n_ih]
theorem zero_mul2 (m : Nat) : 0 * m = 0 := by
cases m with n
rw [Nat.mul_zero]
rw [Nat.mul_succ]
rw [Nat.add_zero]
rw [zero_mul]
-- Special case not triggered by empty case analysis
theorem zero_mul3 (m : Nat) : 0 * m = 0 := by
induction m with
theorem zero_mul4 (m : Nat) : 0 * m = 0 := by
cases m with