LCNF repeats closure setup on every call to a returned function
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 44/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- compilers
Research direction
Start with the standalone Repro.lean example and run lean -Dtrace.Compiler.result=true -c Repro.c Repro.lean; inspect makeProbe and the specialized List.mapTR.loop in the trace. Compare the generated calls with the expected one-time setup, including the empty-map case. The issue says candidate patch #16908 reduces setup calls from three to one for three keys and keeps them at zero for an empty map.
Written by the indexing model from the issue text.
Description
Prerequisites
- Checked for an open duplicate.
- Reduced the issue to a standalone reproducer with no imports.
- Reproduced on the latest nightly,
nightly-2026-11-30.
Description
LCNF can move setup from a function that returns a closure into the closure's body. In this example, each probe recalculates the same index.
Context
#5908 reported the same pattern under the old compiler. The reproducer below shows it in the current LCNF pipeline.
Steps to Reproduce
-
Save this as
Repro.lean.@[noinline] def build (rows : List Nat) : Nat := rows.foldl (· + ·) 0 @[noinline] def makeProbe (rows : List Nat) : Nat → Nat := let index := build rows fun key => index + key def probeAll (rows keys : List Nat) : List Nat := let probe := makeProbe rows keys.map probe -
Run
lean -Dtrace.Compiler.result=true -c Repro.c Repro.lean. -
Inspect
makeProbeand the specializedList.mapTR.loopin the trace.
Expected behavior. The first probe calls build once. Later probes reuse the index. An empty map skips setup.
Actual behavior. makeProbe takes both rows and key in the generated code. The specialized loop calls makeProbe rows key for each key, so build folds over rows each time.
Versions
Lean 4.35.0, 4.36.0, 4.38.0-nightly-2026-11-30 (fd149e0), and master at 6079107. Tested on macOS 27.2 (Apple silicon).
Additional Information
Adding -Dtrace.Compiler.init=true shows the two-argument makeProbe in the initial LCNF. Removing @[noinline] from the factory also reproduces the issue.
The candidate patch in #16908 reduces the setup count from three to one for a three-key map and keeps it at zero for an empty map.
Impact
With n rows and m keys, the repeated folds make the setup work grow with n × m. Add a 👍 reaction if this affects your code.
- Dominant language
- Lean
- Stars
- 10.5k
- Forks
- 1.2k
- Avg merge
- 19h 36m
- Merged PRs (30d)
- 323
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from leanprover/lean4
-
P-medium
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
leanprover/lean4#16958 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
leanprover/lean4#16897 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
leanprover/lean4#16892 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 86/100
leanprover/lean4#16886 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 78/100
leanprover/lean4#16820 ·
Maintainers usually reply within 1 day
All issues in leanprover/lean4
Similar issues
-
area-dart-model gardening
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 87/100
Maintainers usually reply within 1 day
-
`shift(x, y)` with a uint256 shift amount >= 2**255 shifts in the wrong directionPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 3 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 71/100
facebookexperimental/semcode#195 ·
Maintainers usually reply within 2 days
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
stjude-rust-labs/sprocket#1476 · 3 comments ·
Maintainers usually reply within 1 day