Skip to content

perf: experiment: abstract recursors in recursor's RHSs - #10548

Closed
nomeata wants to merge 9 commits into
nightly-with-mathlibfrom
joachim/abs-rec-rule
Closed

nomeata wants to merge 9 commits into
nightly-with-mathlibfrom
joachim/abs-rec-rule

Conversation

@nomeata

@nomeata nomeata commented Sep 24, 2025 •

Copy link
Copy Markdown
Collaborator

This PR experiments with a slightly different representation of a
recursor's rules. Instead of having a rules

fun α motive rec cons {n} x xs => cons x xs (Vec.rec α motive rec cons n xs)

we leave a hole for Vec.rec α motive rec and abstract over this

fun α motive rec nil cons {n} x xs => cons x xs (rec n xs)

This could be beneficial if the recursor has many arguments (e.g. many
minor premises from an inductive with many constructors). Note that the
subexpression Vec.rec α motive rec is exactly a subterm of the
expression we are reducing. So instead of substituting every single
argument, we can substitute this subexpression as a whole, increasing
sharing and reducing the cost.

It also simplifies the kernel code for nested inductives a bit, as
recursor names don't have to be changed in the rules rhs, only in the
list of mutual recursors.

This refactoring is also in anticipation of maybe treating more
functions as primitively recursive, instead of always going all the way
to .rec.

This PR lets `#print T.rec` show more information about a recursor, in
particular it's reduction rules.
This PR experiments with a slightly different representation of a
recursor's rules. Instead of having a rules
```
fun α motive rec cons {n} x xs => cons x xs (Vec.rec α motive rec cons n xs)
```
we leave a hole for `Vec.rec α motive rec` and abstract over this
```
fun α motive rec nil cons {n} x xs => cons x xs (rec n xs)
```

This could be beneficial if the recursor has many arguments (e.g. many
minor premises from an inductive with many constructors). Note that the
subexpression `Vec.rec α motive rec` is exactly a subterm of the
expression we are reducing. So instead of substituting every single
argument, we can substitute this subexpression as a whole, increasing
sharing and reducing the cost.

It also simplifies the kernel code for nested inductives a bit, as
recursor names don't have to be changed in the rules `rhs`, only in the
list of mutual recursors.

This refactoring is also in anticipation of maybe treating more
functions as primitively recursive, instead of always going all the way
to `.rec`.
@nomeata nomeata added the changelog-language Language features and metaprograms label Sep 24, 2025
@nomeata

nomeata commented Sep 24, 2025

Copy link
Copy Markdown
Collaborator Author

!bench

@nomeata nomeata changed the title joachim/abs rec rule perf: experiment: abstract recursors in recursor's RHSs Sep 24, 2025
@nomeata
nomeata changed the base branch from master to nightly-with-mathlib September 24, 2025 14:46
@nomeata

nomeata commented Sep 24, 2025

Copy link
Copy Markdown
Collaborator Author

!bench

@leanprover-bot

Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit 39bdfc6.
There were significant changes against commit 9fc18b8:

  Benchmark                 Metric          Change
  ===========================================================
+ big_beq_rec               branch-misses    -3.4%  (-57.5 σ)
+ big_beq_rec               branches         -3.1% (-205.4 σ)
+ big_beq_rec               instructions     -3.3% (-235.0 σ)
+ bv_decide_rewriter.lean   branch-misses    -1.9%  (-23.0 σ)
+ channel.lean              unbounded_seq    -1.4%
+ grind_ring_5.lean         branches        -10.6% (-824.8 σ)
+ grind_ring_5.lean         instructions    -11.2% (-831.8 σ)
+ grind_ring_5.lean         maxrss           -4.7%  (-64.0 σ)
+ mut_rec_wf                branches         -2.8% (-283.8 σ)
+ mut_rec_wf                instructions     -2.9% (-287.2 σ)
+ stdlib                    grind ac        -18.6%  (-26.8 σ)
+ stdlib                    grind dsimp     -20.3%  (-20.6 σ)
- stdlib                    grind ematch      2.3%   (40.3 σ)

@leanprover-bot

Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit a99a153.
There were significant changes against commit 9fc18b8:

  Benchmark                            Metric          Change
  =======================================================================
+ Std.Data.Internal.List.Associative   branch-misses    -2.5%   (-32.4 σ)
+ big_beq_rec                          branch-misses    -4.0%   (-38.9 σ)
+ big_beq_rec                          branches         -3.2%  (-306.1 σ)
+ big_beq_rec                          instructions     -3.4%  (-327.5 σ)
- big_do                               task-clock        1.6%    (66.2 σ)
- big_do                               wall-clock        1.7%    (20.1 σ)
+ grind_ring_5.lean                    branch-misses    -5.7%   (-23.1 σ)
+ grind_ring_5.lean                    branches        -10.6% (-1279.4 σ)
+ grind_ring_5.lean                    instructions    -11.2% (-1424.2 σ)
+ mut_rec_wf                           branches         -2.8%  (-185.0 σ)
+ mut_rec_wf                           instructions     -2.9%  (-199.4 σ)
+ reduceMatch                          instructions     -1.1%   (-30.9 σ)

nomeata added a commit that referenced this pull request Sep 24, 2025
This PR is an experiment: What if the kernel would recognize primitiely
recursive functions and treat them as recursors themselves. In paticular
reduce them only when applied to a construtor, and then to the RHS
directly.

Builds on top of #10548
@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Sep 24, 2025
@leanprover-bot

leanprover-bot commented Sep 24, 2025 •

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2025-09-22 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2025-09-24 22:14:16)
  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-01-10 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-01-14 00:21:48)
  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-01-14 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-01-19 23:00:31)
  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-02-01 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-02-02 11:27:04)

ghost pushed a commit to leanprover-community/batteries that referenced this pull request Sep 24, 2025
ghost pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Sep 24, 2025
@ghost

ghost commented Sep 24, 2025 •

Copy link
Copy Markdown

Mathlib CI status (docs):

@ghost ghost added the breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan label Sep 24, 2025
@nomeata

nomeata commented Jan 12, 2026

Copy link
Copy Markdown
Collaborator Author

!radar

@leanprover-radar

leanprover-radar commented Jan 12, 2026 •

Copy link
Copy Markdown

Benchmark results for 58dcd82 against 1403994 are in! @nomeata

Large changes (3✅)
  • ✅ big_beq_rec//instructions: -773.0M (-3.5%)
  • ✅ grind_ring_5.lean//instructions: -1.2G (-12.0%)
  • ✅ mut_rec_wf//instructions: -1.0G (-3.2%)
Medium changes (4✅, 1🟥)
  • ✅ big_deceq_rec//instructions: -86.7M (-1.2%)
  • 🟥 build/module/Lean.Meta.Tactic.Grind.Arith.Cutsat.Types//instructions: +1.1G (+3.0%) (reduced significance based on absolute threshold)
  • ✅ build/module/Std.Data.HashMap.RawLemmas//instructions: -1.0G (-0.7%)
  • ✅ grind_bitvec2.lean//instructions: -1.6G (-0.7%)
  • ✅ reduceMatch//instructions: -190.6M (-1.0%)
Small changes (19✅, 8🟥)

Too many entries to display here. View the full report on radar instead.

ghost pushed a commit to leanprover-community/batteries that referenced this pull request Jan 14, 2026
@github-actions github-actions Bot added the mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN label Jan 14, 2026
ghost pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Jan 14, 2026
nomeata added a commit to leanprover/lean4export that referenced this pull request Jan 14, 2026
nomeata added a commit to leanprover/lean-kernel-arena that referenced this pull request Jan 14, 2026
@ghost ghost added builds-mathlib CI has verified that Mathlib builds against this PR and removed breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan labels Jan 14, 2026
@nomeata

nomeata commented Jan 14, 2026

Copy link
Copy Markdown
Collaborator Author

!bench mathlib

@leanprover-radar

leanprover-radar commented Jan 14, 2026 •

Copy link
Copy Markdown

Benchmark results for leanprover-community/mathlib4-nightly-testing@03e5dd3 against leanprover-community/mathlib4-nightly-testing@25ef31e are in! @nomeata

Warnings (2)

These warnings may indicate that the benchmark results are not directly comparable, for example due to changes in the runner configuration or hardware.

  • Runner for run main has different system configurations between commits.
  • Runners for run main differ between commits.

No significant changes detected.

nomeata added a commit to leanprover/lean4checker that referenced this pull request Jan 14, 2026
nomeata added a commit to leanprover/lean-kernel-arena that referenced this pull request Jan 14, 2026
@leanprover leanprover deleted a comment from leanprover-radar Jan 19, 2026
@leanprover leanprover deleted a comment from Garmelon Jan 19, 2026
@leanprover leanprover deleted a comment from leanprover-radar Jan 19, 2026
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Jan 19, 2026
ghost pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Jan 19, 2026
@nomeata

nomeata commented Jan 20, 2026

Copy link
Copy Markdown
Collaborator Author

!bench mathlib

@leanprover-radar

leanprover-radar commented Jan 20, 2026 •

Copy link
Copy Markdown

Benchmark results for leanprover-community/mathlib4-nightly-testing@2ca8b22 against leanprover-community/mathlib4-nightly-testing@b4b270b are in! @nomeata

  • build//instructions: -11.2G (-0.0%)

No significant changes detected.

@leanprover leanprover deleted a comment from leanprover-radar Jan 20, 2026
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Feb 2, 2026
ghost pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Feb 2, 2026
@Garmelon
Garmelon deleted the branch nightly-with-mathlib September 16, 2026 17:20
@Garmelon Garmelon closed this Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-mathlib CI has verified that Mathlib builds against this PR changelog-language Language features and metaprograms mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants