leanprover / leanprover/lean4

RFC: handle well a reused variable after `match` in `decreasing_by`

Open
#10,977 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Current state

Currently, reusing a variable after match often leads to an unsolvable termination goal, for example

def f (a : Nat) := match a with
| 0 => 1
| a1+1 => f (a-1)
termination_by a
decreasing_by

Proof state:

a a1 : Nat
⊢ a - 1 < a1.succ
Proposal

The proof state should be either

a a1 : Nat
⊢ a1.succ - 1 < a1.succ

or

a a1 : Nat
h : a = a1 + 1
⊢ a - 1 < a1.succ
Benefit

match is very common in defining recursive functions, and it is sometimes useful to reuse an argument again. It is true that it can be circumvented with a@(a1+1) which actually include the appropriate equation but without such "hack", it is virtually impossible to reuse a matched variable, which is confusing especially for beginners.

decreasing_by already captures conditions in an if / then / else, so it should not be a significant change to handle match correctly as well.

Community Feedback

I see this as a borderline between a bug, and a feature request, so I haven't discussed on Zulip so far but am happy to do so if it turns out to be difficult / controversial.

Impact

Add 👍 to issues you consider important. If others benefit from the changes in this proposal being added, 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

No implementation files or tests are named. Start by running the reproducer in the issue and inspect the decreasing_by proof state after the reused variable in match; done means the state includes the refined value or an equality such as a = a1 + 1.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.