Skip to content

fix: determine the ~q discriminant's universe level from its unquoted type - #175

Open
josaum wants to merge 5 commits into
leanprover-community:masterfrom
josaum:fix/match-eq-level-mvar
Open

josaum wants to merge 5 commits into
leanprover-community:masterfrom
josaum:fix/match-eq-level-mvar

Conversation

@josaum

@josaum josaum commented Nov 5, 2026

Copy link
Copy Markdown

Fixes #30.

Problem

A ~q pattern that is not in the last discriminant position of a multi-discriminant match — or whose branch contains another match — failed with

Invalid match expression: The type of pattern variable 'match_eq✝' contains metavariables:
  $x✝² =Q («$A» ∧ «$B»)

e.g. 0art0's example from the issue discussion:

def test : (P : Q(Prop)) → (args : List Nat) → MetaM String
  | ~q($A ∧ $B), [] => return "and"
  | _, _ => return "other"

Root cause

_qq_match creates two metavariables: argLvlExpr : Q(Level) and argTyExpr : Q(Quoted (.sort $argLvlExpr)), and elaborates the discriminant with expected type Quoted $argTyExpr. Since Quoted is implicit_reducible (def Quoted (α : Expr) := Expr), unifying the expected and inferred types assigns argTyExpr (e.g. to Expr.sort 0 for a Q(Prop) discriminant) but never constrains argLvlExpr — checking the assignment only requires unfolding Quoted to Expr.

With pp.all, the surviving metavariable is visible as the universe argument of the match_eq hypothesis: @Qq.QuotedDefEq (?m.56 x✝² ...) (Lean.Expr.sort Lean.Level.zero) x✝² .... It originates from argLvlExpr, which flows into the QuotedDefEq type elaborated in makeMatchCode. As soon as match generalization needs to abstract that hypothesis (a nested match, or the ~q pattern in a non-final discriminant position), core rejects the unassigned metavariable.

Fix

After unquoting the discriminant's type in _qq_match, if argLvlExpr is still unassigned, determine the level from the unquoted type (getLevel) and quote it back (quoteLevel), mirroring what unquoteLCtx already does for let-bound local declarations (Qq/Macro.lean). If the level still contains metavariables or cannot be quoted in the current context, the previous behavior is kept.

Tests

New QqTest/matchLevelMVar.lean (picked up by the QqTest.+ glob):

  • the issue's original MWE (nested match inside a ~q branch);
  • 0art0's variant with the ~q pattern in a non-final discriminant position (failed with the issue's exact error before the fix);
  • the argument order that already worked, as a control;
  • #eval checks that the matched functions still compute correctly.

Verification

On the pinned toolchain (leanprover/lean4:v4.36.0-rc2, master b025662):

  • Reproduced the failure on unmodified master (both the pp.all diagnosis above and the issue's error message).
  • With the fix: lake build --wfail passes (30 jobs, no warnings); lake test passes (exit 0; full QqTest suite, 59 jobs, including the new file).
  • Teeth check: reverting Qq/Match.lean makes the new regression file fail with the issue's exact error.
  • No sorry/admit introduced; the change is meta-only in Qq/Match.lean plus tests.
  • Both pushed files verified byte-identical (git hash-object) to the locally built and tested sources.

Note: a ~q match under a section Level variable (e.g. {u : Level} {α : Q(Type u)} discriminants) hits a separate pre-existing maximum recursion depth failure that is unchanged by this fix (reproduces identically on unmodified master); I did not investigate it here.

Branch note: the branch is based on my fork's master (859a214), whose tree is identical to upstream b025662; the diff is exactly Qq/Match.lean and the new QqTest/matchLevelMVar.lean. (Three commits because the file upload API is per-file and one transcription slip in the first commit was fully corrected in the second; the final tree is hash-verified. Feel free to squash.)

…ed type

Elaborating the discriminant never constrains the level metavariable
(`Quoted` is `implicit_reducible`), so it survived into the generated
`match_eq` hypothesis and made `match` generalization fail with
"type of pattern variable 'match_eq✝' contains metavariables".

Fixes leanprover-community#30.
The previous commit's transcription dropped the `isEscapedAntiquot`
guard; this restores the file to exactly the tested content.
Covers the issue MWE (nested `match` in a `~q` branch) and the
multi-discriminant variant where the `~q` pattern is not in the last
position; both failed with "type of pattern variable 'match_eq✝'
contains metavariables" before the fix.
…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.matchLevelMVar).
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.

invalid match-expression, type of pattern variable 'match_eq✝' contains metavariables

1 participant