leanprover / leanprover/lean4

Invalid type class cache after a change in `MetavarContext`

Open
#4,409 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Prerequisites

Please put an X between the brackets as you perform the following steps:

Description

It is possible for the type class synthesis cache to become invalid when the MetavarContext changes. This is usually no problem because the function instantiateMVars is run on the goal, so if the MetavarContext changes, then the goal also changes. However, if the metavariable is hidden inside a let variable, this becomes a problem.

Context

I was trying to better understand type class synthesis cache, and came up with this example.

Steps to Reproduce

Create a metavariable ?m, and define a let variable a := ?m. First let ?m := Nat, and synthesize Inhabited a, and then let ?m := Int and synthesize Inhabited a again. Lean then finds the instInhabitedNat instance in the cache, and so it fails to find Int.instInhabited. Here's the code:

import Lean

open Lean Meta

run_meta do
  let m ← mkFreshExprMVar (Expr.sort levelOne)
  withLetDecl `a (Expr.sort levelOne) m fun a => do
    withMCtx (← getMCtx) do
      logInfo m! "{← isDefEq m (.const ``Nat [])}; {← trySynthInstance (.app (.const ``Inhabited [levelOne]) a)}"
    withMCtx (← getMCtx) do
      logInfo m! "{← isDefEq m (.const ``Int [])}; {← trySynthInstance (.app (.const ``Inhabited [levelOne]) a)}"

Expected behavior: [Clear and concise description of what you expect to happen]

The logInfo should give

true; (some instInhabitedNat)
true; (some Int.instInhabited)

Actual behavior: [Clear and concise description of what actually happens]

The logInfo gives

true; (some instInhabitedNat)
true; none
Versions

4.9.0-rc1

Additional Information
Impact

I doubt this ever comes up in practice, so there is not much impact.

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

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

Start by running the minimal run_meta example using withMCtx, isDefEq, and trySynthInstance to reproduce the stale cache result. Read the MetavarContext and type class synthesis cache paths, including where instantiateMVars is applied. Done means the second synthesis returns Int.instInhabited after the metavariable changes from Nat to Int.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.