leanprover / leanprover/lean4

Match case elimination fails on `n : Nat` with `h : n ≤ 16`

Open
#9,826 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P-low
Dominant language
Lean
Stars
9.2k
Forks
990
Avg merge
1d 17h
Merged PRs (30d)
175

Description

Prerequisites

Please put an X between the brackets as you perform the following steps:

Description

match will fail to eliminate remaining cases for a discriminant n : Nat when the known upper bound is at least n ≤ 16.

Steps to Reproduce
-- works
def test15 (n : Nat) (h : n ≤ 15) :=
  match n with
  | 0 => 0
  | 1 => 1
  | 2 => 2
  | 3 => 3
  | 4 => 4
  | 5 => 5
  | 6 => 6
  | 7 => 7
  | 8 => 8
  | 9 => 9
  | 10 => 10
  | 11 => 11
  | 12 => 12
  | 13 => 13
  | 14 => 14
  | 15 => 15

/--
error: Missing cases:
(Nat.succ (Nat.succ (Nat.succ (Nat.succ (Nat.succ (Nat.succ (Nat.succ (Nat.succ (Nat.succ (Nat.succ (Nat.succ (Nat.succ (Nat.succ (Nat.succ (Nat.succ (Nat.succ (Nat.succ _))))))))))))))))), _
-/
#guard_msgs in
def test16 (n : Nat) (h : n ≤ 16) :=
  match n with
  | 0 => 0
  | 1 => 1
  | 2 => 2
  | 3 => 3
  | 4 => 4
  | 5 => 5
  | 6 => 6
  | 7 => 7
  | 8 => 8
  | 9 => 9
  | 10 => 10
  | 11 => 11
  | 12 => 12
  | 13 => 13
  | 14 => 14
  | 15 => 15
  | 16 => 16

Expected behavior:

Match eliminations succeeds, or, if necessary, there is an option that can be set to get Lean to try harder and then succeed.

Actual behavior:

Match elimination fails even with increases to standard "try harder" options like maxHeartbeats and maxRecDepth.

Versions

eanprover/lean4:v4.22.0-rc4
leanprover/lean4:nightly-2025-08-10

Additional Information

This is approximately the same as #9292, but for Nat.

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal reproducer and comparing test15, which succeeds, with test16, which reports a missing case. Trace the match-elimination path for Nat bounds and make the test16 case succeed without relying on increased maxHeartbeats or maxRecDepth; the reproducer's expected behavior defines done.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.