Skip to content

fix: support let mut in capture-free if let ~q branches - #173

Open
josaum wants to merge 5 commits into
leanprover-community:masterfrom
josaum:fix/qq-if-let-mut
Open

josaum wants to merge 5 commits into
leanprover-community:masterfrom
josaum:fix/qq-if-let-mut

Conversation

@josaum

@josaum josaum commented Nov 2, 2026

Copy link
Copy Markdown

Addresses #36.

Problem

The issue's MWE failed in both if let branches with Variable x cannot be mutated:

example : MetaM Nat := do
  let mut x := 1
  if let ~q(Prop) := q(Type) then
    x := 2
  else
    x := 3
  return x

_qq_match elaborates the successful branch inside the continuation of its generated Bind.bind. Lean's do elaborator does not permit mutation of an outer let mut variable inside that generated lambda.

Fix

For a whole-pattern, capture-free if let ~q(...), evaluate the quotation match before choosing a branch, then use an ordinary do match on the result:

  • mkIsDefEqOption returns none on failure, or the match result paired with its match_eq proof on success.
  • The new _qq_match_option elaborator reuses the existing pattern elaboration and lifts the result to the enclosing monad.
  • A dedicated doIf expansion uses a monadic discriminant (match ← test with), so branch bodies are handled directly by Lean's ordinary do elaborator rather than by the matcher's continuation lambda. This preserves let mut and branch control flow.

Scope: this intentionally covers capture-free patterns, including the issue's MWE. Capture-bearing patterns continue through the existing continuation-based matcher unchanged; returning their dependent pattern variables and match_eq proof is a larger change and is not attempted here.

Tests

New QqTest/ifLetMut.lean covers:

  • the issue's exact MWE;
  • matching and non-matching branches (2 versus 3);
  • omitted else branches, including the fall-through result;
  • elaboration in TacticM as well as MetaM;
  • a capture-bearing pattern as a fallback check.

Verification

On the pinned leanprover/lean4:v4.36.0-rc2 toolchain:

  • The regression file fails on unmodified master with the issue's mutation errors.
  • lake build --wfail — passes (30 jobs, no warnings).
  • lake test — passes (exit 0; full QqTest suite, 59 jobs).
  • No sorry or admit introduced.
  • Both pushed files were verified byte-identical (git hash-object) to the locally built and tested sources.

Branch note: my fork's sync commit means the PR diff may also show the one-line lean-toolchain change from #170, which upstream already has; merging is content-neutral for that file. The intended change is exactly Qq/Match.lean and the new QqTest/ifLetMut.lean.

…th master

Upstream master bumped the toolchain past this branch (v4.36.0-rc2 ->
v4.36.0 -> v4.37.0-rc1 in leanprover-community#179/leanprover-community#180), conflicting with the rc1->rc2 bump
this branch carried from the fork's merge-base. Setting the branch's
toolchain to master's makes the merge clean; the PR diff relative to the
merge result is unchanged.

Verified locally on leanprover/lean4:v4.37.0-rc1 (upstream master
ad0c9f8 + this branch, toolchain conflict resolved to master's version):
`lake build --wfail` passes (31 jobs, no warnings) and `lake test`
passes (exit 0, incl. QqTest.ifLetMut).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant