leanprover / leanprover/lean4

mut at binders and pattern matches

Open
#2,203 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Prerequisites
  • Put an X between the brackets on this line if you have done all of the following:
    • Checked that your issue isn't already filed.
    • Reduced the issue to a self-contained, reproducible test case.
Description

Currently, mut can only occur in restricted locations. This leads one to introduce
spurious names, that are later copied into the actual mutable variable:

opaque generator : Nat → Float × Nat
def generateN (seed: Nat) (n: Nat) : Array Float × Nat := Id.run do
  -- (1) this is annoying, please let me write '(mut seed : Nat)' in the declaration
  let mut seed := seed
  let mut out := #[]
  for _ in List.range n do
    -- (2) This is annoying, please let me write 'let (val, mut seed) := generator seed'
    let (val, seed') := generator seed
    seed := seed'

    out := out.push val
  return (out, seed)

A quality of life feature request is to be able to annotate binders such as function arguments and subterms of
pattern matches with mut. This would enable one to clean up the above function as:

opaque generator : Nat → Float × Nat
def generateN (mut seed: Nat) (n: Nat) : Array Float × Nat := Id.run do
  let mut out := #[]
  for _ in List.range n do
    let (val, mut seed) := generator seed'
    out := out.push val
  return (out, seed)

@digama0 mentioned that this might be complex, because lean does not have a dedicated grammar for patterns, it just reuses the expr grammar. A proposal was to add a expr macro mut that fails for normal exprs, and elaborates to the expected code when used at appropriate binding locations.

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 examining Lean's expression grammar and elaboration for binders and pattern matches; the issue notes that patterns reuse the expr grammar. Evaluate the proposed expr macro for mut, including its failure on ordinary expressions. Done means function arguments and pattern-match subterms accept mut while existing restricted locations continue to work.

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.