leanprover / leanprover/lean4

out of scope let-variable can appear in `fun` binder type of metavariable assignment

Open
#5,939 2 comments 1 reaction 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

The following declaration raises an error.

-- (kernel) declaration has free variables '_example'
example : Unit :=
  let x : Nat → Unit := _
  let N := Nat;
  (fun a : N =>
    have : x a = () := rfl
    ()) Nat.zero

The unification that results from have : x a = () := rfl asigns the metavariable in x to fun (a : N) => (). However, N is not in the context of x, so the free variable remains in the definition of x.

Context

I ran into an error caused by this isDefEq bug while programming. I reported the problem on Zulip.

Steps to Reproduce

Use the above problematic declaration.

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

The elaboration succeeds, and results in either

let x : Nat → Unit := let N := Nat; fun (a : N) => ()

or the reduced form

let x : Nat → Unit := fun (a : Nat) => ()

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

We get the error (kernel) declaration has free variables '_example'

Versions

4.13.0

Additional Information

The minimized original example that I ran into is this:

-- (kernel) declaration has free variables 'oops'
def oops : IO Unit := do
 pure ()
 match some () with
 | some u => do
  let pair := match () with | _ => ((),())
  let i := ()
  if h : i = pair.1 then
   let k := 0
 | _ => return

The same underlying problem is responsible for #4375 and #5387. One problematic case was fixed by #4410.

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 with the minimal reproducible declaration in the issue and investigate the isDefEq path that assigns the metavariable in x. Compare the behavior with the related issues #4375 and #5387 and the fix in #4410. Done means the declaration elaborates without a kernel free-variable error and the resulting definition keeps all let-bound variables in scope.

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.