-
Notifications
You must be signed in to change notification settings - Fork 673
perf: experiment: abstract recursors in recursor's RHSs #10548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
nomeata
wants to merge
5
commits into
nightly-with-mathlib
Choose a base branch
from
joachim/abs-rec-rule
base: nightly-with-mathlib
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`.
!bench |
!bench |
Here are the benchmark results for commit 39bdfc6. 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 σ) |
Here are the benchmark results for commit a99a153. 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
Reference manual CI status:
|
leanprover-community-mathlib4-bot
added a commit
to leanprover-community/batteries
that referenced
this pull request
Sep 24, 2025
leanprover-community-mathlib4-bot
added a commit
to leanprover-community/mathlib4-nightly-testing
that referenced
this pull request
Sep 24, 2025
Mathlib CI status (docs):
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
breaks-mathlib
This is not necessarily a blocker for merging: but there needs to be a plan
changelog-language
Language features and metaprograms
toolchain-available
A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR experiments with a slightly different representation of a
recursor's rules. Instead of having a rules
we leave a hole for
Vec.rec α motive rec
and abstract over thisThis 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 theexpression 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 thelist 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
.