leanprover / leanprover/lean4

Structural recursion over `Acc` fails

Open
#12,739 1 comment 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
Description

Structurally recursive functions over Acc (and any other recursive subsingleton predicate) that produce non-Props fail. The main reason behind this is that auxiliary helpers (below and brecOn) for Acc have motives that only eliminate to Prop, like any other predicate, despite being able to be strengthened to eliminate to arbitrary sorts. A solution to this would be to generate the aforementioned helpers on subsingleton the same way they're generated for non-Prop types instead of the IndPredBelow machinery.

Context

This issue was discovered when trying to reconstruct some Rocq code for constructing a computable indefinite description choice over decidable predicates on natural numbers Rocq reference

Steps to Reproduce
variable (P : Nat → Prop)
def R (x y : Nat) : Prop := x = y + 1 ∧ ¬ P y
def witness_of_Acc [h : DecidablePred P] (a : Acc (R P) n) : {n : Nat // P n} :=
  match a with
  | .intro k ak =>
    match h k with
      | isTrue h => ⟨k,h⟩
      | isFalse h =>
        witness_of_Acc (ak (k+1) ⟨rfl, h⟩)
termination_by structural a

Expected behavior: No error

Actual behavior:

failed to infer structural recursion:
Cannot use parameter a:
  brecOn is type incorrect
    Application type mismatch: The argument
      fun {n} a => funType_1 a
    has type
      {n : Nat} → Acc (R P) n → Type
    of sort `Type 1` but is expected to have type
      (a : Nat) → Acc (R P) a → Prop
    of sort `Type` in the application
      @Acc.brecOn Nat (R P) fun {n} a => funType_1 a
Versions

Lean 4.30.0-nightly-2026-02-28 Target: x86_64-unknown-linux-gnu

Additional Information

[Additional information, configuration or data that might be necessary to reproduce the issue]

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 by reproducing the minimal Lean example for witness_of_Acc and inspect the Acc.brecOn and below helpers involved in structural recursion. Compare their IndPredBelow generation with helper generation for non-Prop types. Done means the example compiles while preserving the intended recursive subsingleton behavior.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.