Dependent `match` fails with `unknown metavariable` using custom `DoOps`

Aperta
#16,561 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
47/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Ambito
compilers

Direzione di ricerca

Start with src/Lean/Elab/BuiltinDo/Match.lean, especially elabDoSeqWithRefinedType, and compare its metavariable scope handling with the custom DoOps example in tests/elab/doNotationPluggableOps.lean. Reproduce the failure with the linked Lean Web example and trace how Term.mkInstMVar requests survive withNewMCtxDepth. Done means the dependent match elaborates for the constrained custom monad.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug
Prerequisites
  • Searched existing issues and pull requests; no matching report found.
  • Reduced to a standalone example importing only Lean (no Mathlib or Batteries).
  • Reproduced on the latest nightly, 4.37.0-nightly-2026-11-18, using Lean Web.
Description

A custom do notation using elabDoWith fails on match (dependent := true) when its mkMonadApp creates a typeclass metavariable with Term.mkInstMVar.

The failure appears to come from elabDoSeqWithRefinedType. It calls mkMonadApp on a fresh result-type metavariable inside withNewMCtxDepth. The temporary metavariables are discarded afterwards, but pending instance requests registered by Term.mkInstMVar remain in the term-elaboration state. Retrying such a request then refers to a discarded metavariable.

Context

The setup follows the custom DoOps and elabDoWith pattern in core's pluggable-operations test,
but uses a constrained monad with an Inhabited instance argument.

Steps to Reproduce

Open this example in Lean Web (nightly).

import Lean

open Lean Lean.Parser Lean.Meta Lean.Elab Lean.Elab.Do

def InhabitedId (α : Type) [Inhabited α] := α

def inhabitedOps : DoOps := { DoOps.default with
  mkPureApp _ e := pure e
  mkBindApp _ _ e k := pure (mkApp k e)
  isPureApp? _ := none
  splitMonadApp? type := do
    let .app (.app m α) _ := type.consumeMData | return none
    unless m.isConstOf ``InhabitedId do return none
    return some ({ m, u := .zero, v := .zero }, α)
  mkMonadApp α := do
    let inst ← Term.mkInstMVar (mkApp (mkConst ``Inhabited [.succ .zero]) α)
    return mkApp2 (← read).monadInfo.m α inst
}

syntax (name := idoKind) "ido " doSeq : term

@[term_elab idoKind] def elabIDo : Term.TermElab := fun stx expectedType? => do
  let `(ido $seq) := stx | throwUnsupportedSyntax
  elabDoWith inhabitedOps seq expectedType?

example (n : Nat) : InhabitedId (Fin (n + 1)) := ido
  match (dependent := true) n with
  | 0 => return (0 : Fin 1)
  | k + 1 => return Fin.last (k + 1)

Expected behavior: The example elaborates. The corresponding example with regular do and Id succeeds:

example (n : Nat) : Id (Fin (n + 1)) := do
  match (dependent := true) n with
  | 0 => return (0 : Fin 1)
  | k + 1 => return Fin.last (k + 1)

Actual behavior: The zero branch produces an unknown metavariable error.

Versions
  • Lean Web, lean-nightly: #eval Lean.versionString reports "4.37.0-nightly-2026-11-18".
  • Also reproduced locally on Lean 4.36.0 and 4.35.0, on macOS 26.6.2 (arm64).
Impact

This prevents dependent matching in custom do notation for constrained monads with a typeclass argument on the result type.

Lingua principale
Lean
Stelle
10.3k
Fork
1.1k
Merge medio
19h 8m
PR unite (30g)
421

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Compilers

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.