LCNF repeats closure setup on every call to a returned function

Open
#16,910 1 comment 0 reactions 0 assignees View on GitHub

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
  1. 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
    
  2. Run lean -Dtrace.Compiler.result=true -c Repro.c Repro.lean.

  3. Inspect makeProbe and the specialized List.mapTR.loop in 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

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.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.