leanprover / leanprover/lean4

RFC: `if let` chains

Open
#1,894 19 comments 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Decrease redundancy in cases like

if let c a := x then
  if q a then
    y
  else z
else z
...

by supporting mixed let/condition chains like

if let c a := x && q a then
  y
else z
...

This is a syntactic idea I thought was too wild to even propose, until I learned that Rust will have it, so obviously it must be a good idea.
Probably even more than for Rust though, parsing this is a bit of an issue. The cleanest approach I could come up with so far is to keep basic if syntax intact and introduce new chain syntax for subterms of precedence greater than &&/∧ only.

syntax letChainElem := "let " term " := " term:36  -- "&&"/"∧" have precedence 35
syntax (priority := low) "if " sepBy1(letChainElem <|> term:36, "&&", " && " <|> " ∧ ") " then " term " else " term : term
#check if let x := a && p x then c else d  -- parsed as elements `[let x := a, p x]` (after removing current `if let` syntax)
#check if let x := a && let y := b then c else d  -- parsed as elements `[let x := a, let y := b]`
#check if a || b then c else d  -- parsed by basic syntax
#check if a || b && let x := a then c else d  -- parse error, incomplete term `let`
#check if (a || b) && let x := a then c else d  -- parsed as elements `[a || b, let x := a]`
#check if a then b else c  -- parsed by both, disambiguated by priority

Desugaring: obvious, modulo introducing join points

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, entry points, or tests are named. Start by reviewing the proposed syntax, precedence examples, and desugaring discussion in this issue; done would require an agreed parsing and implementation design plus validation, but the payload does not specify where those live.

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
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.