leanprover / leanprover/lean4

`unknown free variable` error when inferring a forall argument

Open
#11,692 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Prerequisites
Description

unknown free variable error when trying to infer a forall argument.

Context

I encountered the error when playing around with continuous functions in Mathlib.

Steps to Reproduce
structure IsFoo {X Y : Type} (f : X → Y) : Prop where

structure FooFun (X Y : Type) where
  toFun : X → Y
  isFoo_toFun : IsFoo toFun

theorem isFoo_subtype_mk {X Y : Type} {p : Y → Prop} {f : X → Y} (_ : IsFoo f) (hp : ∀ x, p (f x)) :
    IsFoo (Y := Subtype p) fun x ↦ ⟨f x, hp x⟩ where

theorem isFoo_comp {X Y Z : Type} {f : X → Y} {g : Y → Z} : IsFoo (g ∘ f) where

variable (α : Type)

/-- error: unknown free variable `_fvar.752` -/
#guard_msgs in
def fooFun (x : α) : FooFun α { x : α × α // True } where
  toFun t := ⟨⟨x, t⟩, by trivial⟩
  isFoo_toFun := isFoo_subtype_mk isFoo_comp _

Expected behavior: The coolFun definition should work

Actual behavior: There's an error on def coolFun: unknown free variable `_fvar.794​

Versions
Lean 4.28.0-nightly-2025-12-15
Target: x86_64-unknown-linux-gnu
Additional Information

Doing any of these changes to the repro fixes it:

  • Delete the by before the trivial
  • Add by exact after cool_toFun :=
  • Change the x in toFun to be t (this creates a type mismatch instead, probably because using t twice breaks the function composition inference)

Replacing cool_comp with sorry still errors, which is why I believe the error is related to inferring the last argument of cool_subtype_mk.

btw I'd love to know how to root-cause Lean bugs to get to the offending Lean/C++ code if it's not too much trouble to explain.

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 self-contained reproducer against the Lean nightly release, using the linked live.lean-lang.org setup if useful. Trace the forall-argument inference involved in isFoo_subtype_mk and confirm that the FooFun definition elaborates without an unknown free variable error while preserving the reported working and failing variations.

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.