Skip to content

Recursive definition causes infinite loop/memory leak #12404

@TwoFX

Description

@TwoFX

Prerequisites

Description

Put the following function into a file

def foo (pat : ByteArray) (stackPos : Nat) (hst : stackPos < pat.size) : { n : Nat // n ≤ stackPos } :=
  if h : stackPos = 0 then0, by simp⟩
  else
    go (stackPos - 1) (by omega)
where
  go (guess : Nat) (hg : guess < stackPos) : { n : Nat // n ≤ stackPos } :=
    if pat[guess] = pat[stackPos] then
      ⟨guess + 1, by omega⟩
    else if h : guess = 0 then0, by simp⟩
    else
      have : (foo pat (guess - 1) (by omega)) < guess := by
        have := (foo pat (guess - 1) (by omega)).property
        omega
      go (foo pat (guess - 1) (by omega)) (by omega)

Running the file in VS Code (or live.lean-lang.org) or building it via lake build will cause Lean to indefinitely use a CPU core while continuously eating memory.

This seems to be related to termination checking, since turning the function into a partial function makes it go through instantly.

Expected behavior: Function definition is accepted or rejected

Actual behavior: Hang

Versions

4.27.0 and nightly-2026-02-09

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions