leanprover / leanprover/lean4

Runtime exponential in number of copies of unused variables

Open
#5,108 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P-medium
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 adding copies of a propositional parameter, which depends on a large proof term of a specific form, to a theorem, the running time of exact sorry seemingly increases exponentially.

Context

See https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/Performance.20issue

Steps to Reproduce
def help (A : Type) : ([] : List A).length % 1 = 0 → Prop := fun _ ↦ True
set_option profiler true

--the running time of "exact sorry" seems exponential in the number of h1
theorem slow {A : Type} (p : Bool)
  (h : ∀ (hp : ([] : List A).length % 1 = 0), help A hp → help A hp)
  (h1 : help A (by cases p <;> omega))
  (h1 : help A (by cases p <;> omega))
  (h1 : help A (by cases p <;> omega))
  (h1 : help A (by cases p <;> omega))
  (h1 : help A (by cases p <;> omega))
  -- (h1 : help A (by cases p <;> omega))
  -- (h1 : help A (by cases p <;> omega))
  (hp : ([] : List A).length % 1 = 0) : help A hp := by
  refine h (by cases p <;> omega) ?_
  exact sorry
--the issue disappers after removing some of the "cases p",
--or replacing "omega" by "simp"
--or replacing A by ℕ
--or replacing "exact sorry" by "exact trivial"

A variant crashing the VS Code infoview is

def help (A : Type) : ([] : List A).length % 1 = 0 → Prop := fun _ ↦ True
set_option profiler true
set_option diagnostics true

example (A : Type) (p : Bool) : ([] : List A).length % 1 = 0 := by
  let h1 : ([] : List A).length % 1 = 0 := by cases p <;> omega
  let h1 : ([] : List A).length % 1 = 0 := by cases p <;> omega
  let h1 : ([] : List A).length % 1 = 0 := by cases p <;> omega
  let h1 : ([] : List A).length % 1 = 0 := by cases p <;> omega
  let h1 : ([] : List A).length % 1 = 0 := by cases p <;> omega
  let h1 : ([] : List A).length % 1 = 0 := by cases p <;> omega
  let h1 : ([] : List A).length % 1 = 0 := by cases p <;> omega
  let h1 : ([] : List A).length % 1 = 0 := by cases p <;> omega
  -- let h1 : ([] : List A).length % 1 = 0 := by cases p <;> omega
  -- let h1 : ([] : List A).length % 1 = 0 := by cases p <;> omega
  -- let h1 : ([] : List A).length % 1 = 0 := by cases p <;> omega
  omega
--also, replacing let by have fixes the issue

Expected behavior: The running time is linear or at least polynomial in the number of copies of h1.

Actual behavior: The running time seems exponential in the number of copies of h1.

Versions

4.11.0-rc2

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 with the minimal slow reproduction in the issue on Lean 4.11.0-rc2, using set_option profiler true and varying the number of repeated h1 parameters. Compare exact sorry with exact trivial, omega with simp, and the let versus have variants; done means the runtime no longer grows exponentially and the VS Code infoview variant does not crash.

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
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.