-
Notifications
You must be signed in to change notification settings - Fork 460
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
RFC: swap the order of the arguments to Membership.mem
#4932
Comments
Note: ~2% of the keys in Mathlib are of the form |
And ~3% of the keys are of the form |
Hi @mattrobball, as far as I understand the 2% build speedup is on the |
Given that there is such a large decrease in typeclass synthesis, I would be surprised if it is substantially due to the |
@mattrobball Yes, that would be very helpful, thank you. |
Comparison from v4.10.0 to modified toolchain (which is branched off v.4.10.0). |
@mattrobball, do you have an estimate of how much work is required to fix Mathlib? |
Maybe in the neighborhood of unbundling FunLike would be my guess. To get a sense of the failures, there is one test here in core, which I think you made, about |
Hi @mattrobball, thank you for investigating this issue. The numbers and the reasoning are solid and we would like to see this change happen, so we are accepting the RFC. Getting a corresponding PR merged would be conditional on having a mathlib adaptation PR ready. Adapting mathlib seems to be quite a lot of work, and the FRO does not have enough capacity at the moment to perform the adaptation in a timely manner. We would be very happy about a community-led effort to adapt mathlib and would help where we can. To phrase it in terms of our new priority labels, we would treat a PR for this RFC with priority P-medium. |
@TwoFX, very understandable. I will have to start teaching again next week so also cannot commit to any timeline for a working version of mathlib. I'll bring this up on Zulip to see if others might be willing to assist. PS: you might want to adjust the priority label to avoid confusion. |
We swap the arguments for `Membership.mem` so that when proceeded by a `SetLike` coercion, as is often the case in Mathlib, the resulting expression is recognized as eta expanded and reduce for many computations. The most beneficial outcome is that the discrimination tree keys for instances and simp lemmas concerning subsets become more robust resulting in more efficient searches. Closes `RFC` #4932 --------- Co-authored-by: Kim Morrison <[email protected]> Co-authored-by: Henrik Böving <[email protected]>
This was completed in #5020. |
Proposal
The current definition of
Membership
isWe propose to change it to
Why?
As pointed out by @digama0, when elaborated, instances on subtypes need an insertion of a
.lam
for the coercion. With the current ordering, this lambda does not satisfyetaExpandStrict?.isSome
at reducible transparency since the initial application is not to the bound variable of the lambda in the body.As a result, the discrimination tree keys for any such instance will be weaker than necessary and will match too much. For
The instances below all match.
because the keys are
[Fintype, Subtype, *, ◾]
After swapping the order, we only match
because the keys for the goal are
[Fintype, Subtype, *, Membership.mem, *, Subgroup, *, *, *, Top.top, Subgroup, *, *, *]
and the keys all instances inFintype
instances on subtypes are more robust.Additionally, the reordering is more pleasant for the identification of sets with predicates.
Community Feedback
This was discussed here after multiple cases of slow instance synthesis for subobjects, including explicit workarounds to avoid the coercion.
It speeds up building of mathlib by ~2% and linting by ~5%. See leanprover-community/mathlib4#15457 for a
sorry
ed branch of mathlib with this implemented.Impact
The positive impact was discussed above. The negative impact is that mathlib seems to rely a bit too much on the weak keys at some places to synthesize instances. Indeed, the example fails without
bar
on the new branch. A decent amount of cleanup will be required.Add 👍 to issues you consider important. If others benefit from the changes in this proposal being added, please ask them to add 👍 to it.
The text was updated successfully, but these errors were encountered: