Skip to content

docs: Library note on performance when defining instances - #7432

Closed
ChrisHughes24 wants to merge 4 commits into
masterfrom
LibraryNoteCH
Closed

ChrisHughes24 wants to merge 4 commits into
masterfrom
LibraryNoteCH

Conversation

@ChrisHughes24

@ChrisHughes24 ChrisHughes24 commented Sep 29, 2023 •

Copy link
Copy Markdown
Member

This method can improve speed, for example #7434, #7435, #7401, #6874, #7408, #7430, #7435, #7436
Sometimes it slows things dow though, e.g #7431

Open in Gitpod

@riccardobrasca riccardobrasca left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes it is better to avoid with, right? Maybe we can explain more here.

Comment thread Mathlib/Algebra/Group/Defs.lean Resolved
@ChrisHughes24

Copy link
Copy Markdown
Member Author

Sometimes it is better to avoid with, right? Maybe we can explain more here.

I'm writing instructions with this PR in mind. So after that PR it won't necessarily be best to avoid with. However I should maybe include information about how Lean automatically fills in fields that can be found by type class inference and prioritises whatever is given by with. I don't know how much detail to go into about how extends works and how something like CommGroup extends both Group and CommMonoid, but only has a toGroup field and not a toCommMonoid field. I also don't want it to sound like thinking about this is some requirement, I think it's best if most contributors don't have to know this stuff.

@mattrobball

Copy link
Copy Markdown
Contributor

My understanding of unification is still mainly anecdotal but this lines up with my expectations.

It might be useful to open this up to a broader discussion on Zulip where we can get some input from core and others.

One point I do know and that is implicit here but might benefit from being made explicit: aligning the inheritance structure for instance construction only matters mainly for data carrying fields.

@mattrobball

Copy link
Copy Markdown
Contributor

Lean also seems to really dislike unifying instances constructed using differing patterns. My guess that it has to unfold everything on both sides to reach a common state.

@mattrobball

Copy link
Copy Markdown
Contributor

I solicited some expert opinion on Zulip for any potential refinements.

@fpvandoorn

Copy link
Copy Markdown
Member

I haven't followed this discussion closely, but a library note like this is good (and we can refine it later with more experience). I presume that this library note is still relevant after lean4#2644?

bors d+

@bors

bors Bot commented Oct 23, 2023

Copy link
Copy Markdown

✌️ ChrisHughes24 can now approve this pull request. To approve and merge a pull request, simply reply with bors r+. More detailed instructions are available here.

@github-actions github-actions Bot added delegated This pull request has been delegated to the PR author (or occasionally another non-maintainer). and removed awaiting-review labels Oct 23, 2023
@ChrisHughes24

Copy link
Copy Markdown
Member Author

I don't think it is still relevant. Or at least the gains are much less. See #7554 . I haven't had time to investigate more closely, but I will probably close my open PRs.

@mattrobball

Copy link
Copy Markdown
Contributor

Some things are still relevant but it's less clear after leanprover/lean4#2644. My guess is that the jumping up the hierarchy by explicitly a data carrying instance at the top might be counterproductive since the improved defeq cache does a much more efficient job of discharging the checks in between and the fact we are forcing unfolded terms is hurting more.

@fpvandoorn

Copy link
Copy Markdown
Member

Ok, then feel free to close it.

@ghost ghost added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jan 17, 2024
@grunweg

grunweg commented May 24, 2024

Copy link
Copy Markdown
Contributor

Coming here from issue triage: what's the current state of this PR?

@ChrisHughes24

Copy link
Copy Markdown
Member Author

The issue about def-eq performance is no longer relevant after the lean3 PR. There were commits made to improve def-eq performance that referenced this note that are probably no longer important and could be reversed.There are still a bunch of TODOs in mathlib referencing this PR. The changes they are next to are probably no longer important and could be reversed.

mathlib-bors Bot pushed a commit that referenced this pull request Jan 23, 2025
Quoting the docstring:

`fast_instance% inst` takes an expression for a typeclass instance `inst`, and unfolds it into
constructor applications that leverage existing instances.
For instance, when used as
```lean
instance instSemiring : Semiring X := sorry
instance instRing : Ring X := fast_instance% Function.Injective.ring ..
```
this will define `instRing` as a nested constructor application that refers to `instSemiring`.
The advantage is then that `instRing.toSemiring` unifies almost immediately with `instSemiring`,
rather than having to break it down into smaller pieces.

Related to #7432



Co-authored-by: Kyle Miller <[email protected]>
Co-authored-by: Matthew Robert Ballard <[email protected]>
@YaelDillies
YaelDillies deleted the LibraryNoteCH branch July 31, 2025 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

delegated This pull request has been delegated to the PR author (or occasionally another non-maintainer). merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants