Skip to content

fix: don't treat let-bound aliases as canonical instances in assertInstancesCommute - #171

Open
josaum wants to merge 1 commit into
leanprover-community:masterfrom
josaum:fix/assert-instances-commute-let-alias
Open

josaum wants to merge 1 commit into
leanprover-community:masterfrom
josaum:fix/assert-instances-commute-let-alias

Conversation

@josaum

@josaum josaum commented Oct 31, 2026

Copy link
Copy Markdown

Fixes #38.

Problem

assertInstancesCommute (and assumeInstancesCommute) looped with maximum recursion depth has been reached whenever a let-bound copy of a local instance was in scope — e.g. the issue's MWE:

example {α : Q(Type)} (x : Q(Inhabited $α)) : Lean.Meta.MetaM Unit := do
  let := x -- comment this line out to fix
  assertInstancesCommute -- maximum recursion depth reached
  pure ()

Root cause

findRedundantLocalInst? looks for a local instance whose type is synthesizable from the other local instances, then assertInstancesCommuteImpl records a QuotedDefEq hypothesis and relies on the next unquoting round zeta-reducing the redundant variable away, so the search terminates.

With let := x in scope, the unquoted context contains $x and a value-less copy $this of the anonymous let's type (the unquoting drops the let's value), and synthInstance? finds $this as a witness that $x is redundant. But quoting $this back yields the original let variable, and unquoteExpr starts with whnf, which unfolds it to x. The generated QuotedDefEq hypothesis therefore degenerates to x = x, the zeta-reduction that would eliminate $x never happens, and the next round rediscovers the exact same pair — forever.

Fix

In findRedundantLocalInst?, skip a witness whose quoted counterpart is let-bound: such a variable cannot serve as the canonical instance, since it unfolds to something else during unquoting. With the MWE, no canonical instance is found (correctly — there is only one real instance), and elaboration proceeds.

This is conservative: it only ever makes the redundancy check find fewer candidates, and the intended path (two genuinely distinct instances of the same class, discharged with a QuotedDefEq hypothesis) still works — pinned by a test.

Tests

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

  • the issue's MWE (anonymous let := x before assertInstancesCommute);
  • several unnamed let-copies (the original report's shape);
  • thorimur's MWE from the issue discussion (named let-bound instance via synthInstanceQ);
  • the intended use: two independent instances of the same class still commute;
  • the same let-copy scenario under assumeInstancesCommute.

On unmodified master, the first two tests fail with maximum recursion depth has been reached; with this fix the whole file compiles.

Verification

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

  • lake build --wfail — passes (30 jobs, no warnings)
  • lake test — passes (exit 0; full QqTest suite, 59 jobs, including the new regression file)
  • No sorry/admit introduced; the change is meta-only in Qq/AssertInstancesCommute.lean plus tests.

…nstancesCommute`

When a local instance had a let-bound copy in scope (e.g. from `let := x`
in a `do` block), `findRedundantLocalInst?` picked the let-bound alias as
the canonical instance for the redundant-instance check. Quoting it back
yields a let variable that `whnf` unfolds during the next unquoting round,
so the generated `QuotedDefEq` hypothesis degenerated (to `x = x`), the
zeta-reduction that would eliminate the redundant instance never happened,
and `assertInstancesCommute` rediscovered the same instance until the
recursion depth was exceeded.

Skip let-bound aliases as canonical-instance witnesses, and add regression
tests covering the issue's MWEs.

Fixes leanprover-community#38
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.

assertInstancesCommute loops when there are several unnamed assumptions

1 participant