leanprover / leanprover/lean4

`by_elab` with variables from local context gives cryptic error message

Open
#7,668 2 comments 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

When using by_elab with a term containing free variables, a kernel typechecking error occurs. The error message does not describe the problem in an easy to understand way.

Context

I was trying to access a private declaration while debugging #lean4 > Emulating type ascriptions and used a let-variable to store the name.

Steps to Reproduce
import Lean

def MyNat :=
  let nat := Lean.Expr.const ``Nat []
  -- (kernel) declaration has free variables '_tmp✝'
  by_elab pure nat

-- the solution
def MyNat :=
  by_elab
    let nat := Lean.Expr.const ``Nat []
    pure nat

Expected behavior:
A better error message, like by_elab cannot compile elaboration function which depends on free variable nat. Optionally, also unfold local let variables in by_elab

Actual behavior:
The error message (kernel) declaration has free variables '_tmp✝' which was hard to interpret, since there is no declaration called _tmp✝ visible in the syntax.

Versions
  • Lean 4.19.0-nightly-2025-03-24
    Target: x86_64-unknown-linux-gnu
  • Lean 4.13.0
    Target: x86_64-w64-windows-gnu Windows
Additional Information

[Additional information, configuration or data that might be necessary to reproduce the issue]

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 by_elab examples against the reported Lean versions and compare the kernel error with the requested diagnostic. Trace the by_elab handling for terms containing free variables; done means the reproduction reports that the elaboration function depends on the local variable, with any let-unfolding behavior verified separately.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.