leanprover / leanprover/lean4

RFC: custom recursors

Open
#2,716 12 comments 17 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Proposal

In a language that compiles pattern matching down to recursor theroems, it seems natural to be able to define custom recursors and have pattern matching and induction work just as before. Here are some use-cases (and I invite interested parties to expand):

  • Arrays as stacks.

    I switched some code that was maintaining a stack from using Lists to arrays for performance reasons, and going from pattern matching to if … then … else and isEmpty and pop and back made it more ugly. It would be great if I could somehow use patterns .empty and .snoc xs s (possibly with nicer syntax) in match statements.o

  • Fin n.

    This is defined as a subtype of Nat, which is a nice definitions. But some programs and proofs would benefit from being able to pattern match on the Fin n directly.

    In fact, there are two possible recursions that might be useful useful here (going from 0 up, or going from the last one down, changing n along the way). It would be good to have both.

  • Refactoring.

    The List.Forall₂ predicate is defined inductively. It could have been defined recursively over the input lists, which would provide us with some (presumably) nice defeqs. But such a change would affect a lot of code – unless one could prove the existing constructors and induction principle with the new definition and set it up so that by induction and match work as before.

TODO (help welcome): Propose concrete syntax/commands/semantics.

Community Feedback
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

Start with the proposal and its linked community feedback on inductive Fin n and refactoring List.Forall₂. Define concrete syntax, commands, and semantics for custom recursors, with pattern matching and induction continuing to work as described; the issue does not name implementation files or tests.

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
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.