leanprover / leanprover/lean4

Non-Linear Growth In Elaboration Time With A Number Of Local Vars Declared With Let

Open
#5,324 4 comments 1 reaction 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

The elaboration time seems to incease in a non-linear manner as more local vars are declared with let, where the let body involves synthesis of some proof for e.g. bounds checking (I know that in this mwe the proof is unused). Changing the lets to haves elaborates immediately. This also elaborates normally if it is put in tactic mode, or if the h argument of the subtypeSet is removed.

Apparently it looks similar to issue lean#5108 but it is not the same underlying issue.

This is the example code causing a problem:

-- Elaborates normally if the `h` argument is removed.
set_option linter.unusedVariables false in
def subtypeSet (xs : {val : Array UInt64 // val.size = 25}) (i : Nat) (h : i < 25 := by decide) : {val : Array UInt64 // val.size = 25} :=
  xs

set_option linter.unusedVariables false in
def theta (x : {val : Array UInt64 // val.size = 25}) : {val : Array UInt64 // val.size = 25} :=  
  let A := subtypeSet x 0
  let A := subtypeSet x 1
  let A := subtypeSet x 2
  let A := subtypeSet x 3
  let A := subtypeSet x 4
  let A := subtypeSet x 5
  let A := subtypeSet x 6
  let A := subtypeSet x 7
  let A := subtypeSet x 8
  let A := subtypeSet x 9
  let A := subtypeSet x 0
  let A := subtypeSet x 1
  let A := subtypeSet x 2
  let A := subtypeSet x 3
  let A := subtypeSet x 4
  let A := subtypeSet x 5
  let A := subtypeSet x 6
  let A := subtypeSet x 7
  -- Progressively more slow-down as these are uncommented
  let A := subtypeSet x 8
  let A := subtypeSet x 9
  --let A := subtypeSet x 0
  --let A := subtypeSet x 1
  --let A := subtypeSet x 2
  --let A := subtypeSet x 3
  --let A := subtypeSet x 4
  A

Example of the code elaborating normally, when put in tactic mode:

-- Elaborates normally if the `h` argument is removed.
set_option linter.unusedVariables false in
def subtypeSet (xs : {val : Array UInt64 // val.size = 25}) (i : Nat) (h : i < 25 := by decide) : {val : Array UInt64 // val.size = 25} :=
  xs

set_option linter.unusedVariables false in
def theta (x : {val : Array UInt64 // val.size = 25}) : {val : Array UInt64 // val.size = 25} := by 
  let A := subtypeSet x 0
  let A := subtypeSet x 1
  let A := subtypeSet x 2
  let A := subtypeSet x 3
  let A := subtypeSet x 4
  let A := subtypeSet x 5
  let A := subtypeSet x 6
  let A := subtypeSet x 7
  let A := subtypeSet x 8
  let A := subtypeSet x 9
  let A := subtypeSet x 0
  let A := subtypeSet x 1
  let A := subtypeSet x 2
  let A := subtypeSet x 3
  let A := subtypeSet x 4
  let A := subtypeSet x 5
  let A := subtypeSet x 6
  let A := subtypeSet x 7
  -- Progressively more slow-down as these are uncommented
  let A := subtypeSet x 8
  let A := subtypeSet x 9
  --let A := subtypeSet x 0
  --let A := subtypeSet x 1
  --let A := subtypeSet x 2
  --let A := subtypeSet x 3
  --let A := subtypeSet x 4
  exact A
Context

https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Slow.20elaboration.20of.20lets/near/468282213

Steps to Reproduce

The code snippets above are enough to reproduce the issue.

Versions

"4.12.0-nightly-2024-09-12"

Additional Information

N/A

Impact

The use case is the implementation of the SHA3 primitive, where there is a lot array accesses. I cannot remove the checks that array accesses are in-bound.

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 provided subtypeSet and theta snippets against the stated Lean nightly version, then compare the term-mode behavior with the tactic-mode and have variants described in the issue. The issue is resolved when repeated let bindings with default bound proofs no longer show non-linear elaboration slowdown without removing the checks.

Written by the indexing model from the issue text.

Assessment

Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.