leanprover / leanprover/lean4

Conflict with deeply nested rec function declarations

Open
#4,510 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

Description

When declaring nested rec functions within inner functions with the same name, an Error <Outer function name>.<Nested function name> has already been declared is encountered.

Context

I was trying to encapsulate all my code in one definition, which lead to me deciding to nest a helper function under an already nested inner function which is where i encountered the error.

Steps to Reproduce

def  outerFunction (x : Nat) : Nat :=
let innerFunction (x : Nat) :=
  let rec helper (x : Nat) :=
    helper x
  helper x

let otherInnerFunction (x : Nat) :=
  let rec helper (x : Nat) :=
    x
  helper x

funcA x

Expected behavior: Since the first helper is nested under innerFunction and the second helper is nested under otherInnerFunction, i expected this to compile normally.

Actual behavior: In reality this gave an error under the second helper outerFunction.helper has already been declared even though this is nested under otherInnerFunction and not directly under outerFunction

Versions

Lean 4.7.0
Windows 11 Pro, 23H2

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

Run the provided nested rec reproduction on Lean 4.7.0 and confirm the duplicate-declaration error. Trace how the compiler handles the two nested helper declarations; done means the example compiles without the erroneous outerFunction.helper has already been declared error.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.