Skip to content

[Merged by Bors] - feat: add fast_instance% elaborator#11521

Closed
eric-wieser wants to merge 12 commits intomasterfrom
fast_instance
Closed

[Merged by Bors] - feat: add fast_instance% elaborator#11521
eric-wieser wants to merge 12 commits intomasterfrom
fast_instance

Conversation

@eric-wieser
Copy link
Copy Markdown
Member

@eric-wieser eric-wieser commented Mar 20, 2024

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

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


Open in Gitpod

@eric-wieser eric-wieser added awaiting-author A reviewer has asked the author a question or requested changes. t-meta Tactics, attributes or user commands awaiting-CI This PR does not pass CI yet. This label is automatically removed once it does. labels Mar 20, 2024
@eric-wieser
Copy link
Copy Markdown
Member Author

!bench

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit 02e5f99.
The entire run failed.
Found no significant differences.

@eric-wieser
Copy link
Copy Markdown
Member Author

!bench

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit f31997b.
The entire run failed.
Found no significant differences.

@mattrobball
Copy link
Copy Markdown
Contributor

Ping me when ready. I think metaprogramming to hide preferred parent and even intermediate structure internals is the future and am glad this got started.

@eric-wieser
Copy link
Copy Markdown
Member Author

Feel free to push things to this branch to get linting passing, so that we have a benchmark.

@eric-wieser
Copy link
Copy Markdown
Member Author

!bench

@github-actions github-actions bot removed the awaiting-CI This PR does not pass CI yet. This label is automatically removed once it does. label Mar 20, 2024
@leanprover-bot
Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit ce19211.
There were no significant changes against commit 69fd837.

@eric-wieser
Copy link
Copy Markdown
Member Author

!bench

@mattrobball
Copy link
Copy Markdown
Contributor

One comment: I think compact_instance% would be a more fitting name

@eric-wieser
Copy link
Copy Markdown
Member Author

!bench

@eric-wieser
Copy link
Copy Markdown
Member Author

One comment: I think compact_instance% would be a more fitting name

Sure, that sounds reasonable; though I'll refrain from doing so until I have a benchmark that indicates this actually helps.

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit 3e91dd1.
There were significant changes against commit 69fd837:

  Benchmark                                              Metric         Change
  ============================================================================
+ ~Mathlib.AlgebraicGeometry.GammaSpecAdjunction         instructions   -11.0%
+ ~Mathlib.AlgebraicGeometry.Spec                        instructions   -13.0%
+ ~Mathlib.FieldTheory.AbelRuffini                       instructions   -10.3%
+ ~Mathlib.FieldTheory.Adjoin                            instructions    -8.9%
+ ~Mathlib.FieldTheory.PurelyInseparable                 instructions   -13.0%
+ ~Mathlib.FieldTheory.SeparableDegree                   instructions   -20.0%
+ ~Mathlib.FieldTheory.Subfield                          instructions   -14.6%
+ ~Mathlib.FieldTheory.Subfield.Order                    instructions   -76.7%
+ ~Mathlib.NumberTheory.NumberField.CanonicalEmbedding   instructions    -7.6%
+ ~Mathlib.NumberTheory.NumberField.ClassNumber          instructions   -17.6%
+ ~Mathlib.NumberTheory.NumberField.Discriminant         instructions    -8.5%
+ ~Mathlib.NumberTheory.NumberField.FractionalIdeal      instructions   -20.4%
+ ~Mathlib.NumberTheory.NumberField.Units                instructions    -6.1%
+ ~Mathlib.RingTheory.Subring.Order                      instructions   -79.3%
+ ~Mathlib.RingTheory.Subsemiring.Order                  instructions   -64.4%

@mattrobball
Copy link
Copy Markdown
Contributor

That is pretty good

@mattrobball
Copy link
Copy Markdown
Contributor

I think the changes in #11029 should be useful to focus the use of the compactor. There I just make versions of Function.Injective/Surjective instances taking bundled class instance from the target/source instead of just the data.

@eric-wieser
Copy link
Copy Markdown
Member Author

Huh, I guess the benchmark doesn't care about shake output; so we're going to get a repeat.

What this doesn't tell us is which fast_instance%s are carrying the weight; but maybe we don't care.


instance strictOrderedSemiring [StrictOrderedSemiring α] :
StrictOrderedSemiring { x : α // 0 ≤ x } :=
-- fast_instance% -- TODO: fix
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There's something weird happening with a free variable on this one.

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit 27f7d47.
There were significant changes against commit 69fd837:

  Benchmark                                              Metric         Change
  ============================================================================
+ ~Mathlib.AlgebraicGeometry.GammaSpecAdjunction         instructions   -11.0%
+ ~Mathlib.AlgebraicGeometry.Spec                        instructions   -13.0%
+ ~Mathlib.FieldTheory.AbelRuffini                       instructions   -10.3%
+ ~Mathlib.FieldTheory.Adjoin                            instructions    -8.9%
+ ~Mathlib.FieldTheory.PurelyInseparable                 instructions   -13.0%
+ ~Mathlib.FieldTheory.SeparableDegree                   instructions   -20.0%
+ ~Mathlib.FieldTheory.Subfield                          instructions   -14.6%
+ ~Mathlib.FieldTheory.Subfield.Order                    instructions   -76.7%
+ ~Mathlib.NumberTheory.NumberField.CanonicalEmbedding   instructions    -7.6%
+ ~Mathlib.NumberTheory.NumberField.ClassNumber          instructions   -17.6%
+ ~Mathlib.NumberTheory.NumberField.Discriminant         instructions    -8.5%
+ ~Mathlib.NumberTheory.NumberField.FractionalIdeal      instructions   -20.4%
+ ~Mathlib.NumberTheory.NumberField.Units                instructions    -6.1%
+ ~Mathlib.RingTheory.Subring.Order                      instructions   -79.3%
+ ~Mathlib.RingTheory.Subsemiring.Order                  instructions   -64.4%

@eric-wieser
Copy link
Copy Markdown
Member Author

!bench

@mattrobball
Copy link
Copy Markdown
Contributor

RingTheory.NonUnitalSubsemiring.Basic is another target.

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit d633325.
There were significant changes against commit 69fd837:

  Benchmark                                              Metric         Change
  ============================================================================
+ ~Mathlib.AlgebraicGeometry.GammaSpecAdjunction         instructions   -11.0%
+ ~Mathlib.AlgebraicGeometry.Spec                        instructions   -13.0%
+ ~Mathlib.FieldTheory.AbelRuffini                       instructions   -45.4%
+ ~Mathlib.FieldTheory.Adjoin                            instructions    -9.9%
+ ~Mathlib.FieldTheory.PurelyInseparable                 instructions   -13.1%
+ ~Mathlib.FieldTheory.SeparableDegree                   instructions   -20.4%
+ ~Mathlib.FieldTheory.Subfield                          instructions   -14.6%
+ ~Mathlib.FieldTheory.Subfield.Order                    instructions   -76.7%
+ ~Mathlib.NumberTheory.NumberField.CanonicalEmbedding   instructions    -7.7%
+ ~Mathlib.NumberTheory.NumberField.ClassNumber          instructions   -17.6%
+ ~Mathlib.NumberTheory.NumberField.Discriminant         instructions    -8.5%
+ ~Mathlib.NumberTheory.NumberField.FractionalIdeal      instructions   -20.4%
+ ~Mathlib.NumberTheory.NumberField.Units                instructions    -6.1%
+ ~Mathlib.NumberTheory.RamificationInertia              instructions    -3.9%
+ ~Mathlib.RingTheory.Subring.Order                      instructions   -79.3%
+ ~Mathlib.RingTheory.Subsemiring.Order                  instructions   -64.4%

@github-actions
Copy link
Copy Markdown

PR summary 37b82b8ef1

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference
Mathlib.Tactic 1
Mathlib.Tactic.FastInstance (new file) 2

Declarations diff

+ CommMagma
+ CommSemigroup
+ Dec
+ Function.Injective.commMagma
+ Function.Injective.commSemigroup
+ Function.Injective.semigroup
+ Mul
+ Semigroup
+ Wrapped.instField
+ dec1
+ dec2
+ elabFastInstance
+ fastInstance
+ instCommSemigroup
+ instRing
+ instSemigroup
+ instSemiring
+ instance : Add (Wrapped α) where add := fun ⟨m⟩ ⟨n⟩ => ⟨m + n⟩
+ instance : Div (Wrapped α) where div m n := ⟨Div.div m.1 n.1⟩
+ instance : Inf (Wrapped α) where inf s t := ⟨Inf.inf s.1 t.1⟩
+ instance : IntCast (Wrapped α) where intCast n := ⟨IntCast.intCast n⟩
+ instance : Inv (Wrapped α) where inv m := ⟨Inv.inv m.1⟩
+ instance : Mul (Wrapped α) where mul := fun ⟨m⟩ ⟨n⟩ => ⟨m * n⟩
+ instance : Mul Nat := ⟨(· * · )⟩
+ instance : NNRatCast (Wrapped α) where nnratCast n := ⟨NNRatCast.nnratCast n⟩
+ instance : NatCast (Wrapped α) where natCast n := ⟨NatCast.natCast n⟩
+ instance : Neg (Wrapped α) where neg m := ⟨Neg.neg m.1⟩
+ instance : One (Wrapped α) where one := ⟨1⟩
+ instance : Pow (Wrapped α) ℕ where pow m n := ⟨Pow.pow m.1 n⟩
+ instance : Pow (Wrapped α) ℤ where pow m n := ⟨Pow.pow m.1 n⟩
+ instance : RatCast (Wrapped α) where ratCast n := ⟨RatCast.ratCast n⟩
+ instance : SMul ℕ (Wrapped α) where smul n m := ⟨SMul.smul n m.1⟩
+ instance : SMul ℚ (Wrapped α) where smul n m := ⟨SMul.smul n m.1⟩
+ instance : SMul ℚ≥0 (Wrapped α) where smul n m := ⟨SMul.smul n m.1⟩
+ instance : SMul ℤ (Wrapped α) where smul n m := ⟨SMul.smul n m.1⟩
+ instance : Sub (Wrapped α) where sub m n := ⟨Sub.sub m.1 n.1⟩
+ instance : Sup (Wrapped α) where sup s t := ⟨Sup.sup s.1 t.1⟩
+ instance : Zero (Wrapped α) where zero := ⟨0⟩
+ instance [Mul α] : Mul (Wrapped α) where mul m n := ⟨Mul.mul m.1 n.1⟩
+ slowInstance
++ Wrapped
++ val_injective

You can run this locally as follows
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for script/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

Co-authored-by: grunweg <rothgami@math.hu-berlin.de>
@github-actions
Copy link
Copy Markdown

PR summary d0a3b12d12

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference
Mathlib.Tactic 1
Mathlib.Tactic.FastInstance (new file) 20

Declarations diff

+ CommMagma
+ CommSemigroup
+ Dec
+ Function.Injective.commMagma
+ Function.Injective.commSemigroup
+ Function.Injective.semigroup
+ Mul
+ Semigroup
+ Wrapped.instField
+ dec1
+ dec2
+ elabFastInstance
+ fastInstance
+ instCommSemigroup
+ instRing
+ instSemigroup
+ instSemiring
+ instance : Add (Wrapped α) where add := fun ⟨m⟩ ⟨n⟩ => ⟨m + n⟩
+ instance : Div (Wrapped α) where div m n := ⟨Div.div m.1 n.1⟩
+ instance : Inf (Wrapped α) where inf s t := ⟨Inf.inf s.1 t.1⟩
+ instance : IntCast (Wrapped α) where intCast n := ⟨IntCast.intCast n⟩
+ instance : Inv (Wrapped α) where inv m := ⟨Inv.inv m.1⟩
+ instance : Mul (Wrapped α) where mul := fun ⟨m⟩ ⟨n⟩ => ⟨m * n⟩
+ instance : Mul Nat := ⟨(· * · )⟩
+ instance : NNRatCast (Wrapped α) where nnratCast n := ⟨NNRatCast.nnratCast n⟩
+ instance : NatCast (Wrapped α) where natCast n := ⟨NatCast.natCast n⟩
+ instance : Neg (Wrapped α) where neg m := ⟨Neg.neg m.1⟩
+ instance : One (Wrapped α) where one := ⟨1⟩
+ instance : Pow (Wrapped α) ℕ where pow m n := ⟨Pow.pow m.1 n⟩
+ instance : Pow (Wrapped α) ℤ where pow m n := ⟨Pow.pow m.1 n⟩
+ instance : RatCast (Wrapped α) where ratCast n := ⟨RatCast.ratCast n⟩
+ instance : SMul ℕ (Wrapped α) where smul n m := ⟨SMul.smul n m.1⟩
+ instance : SMul ℚ (Wrapped α) where smul n m := ⟨SMul.smul n m.1⟩
+ instance : SMul ℚ≥0 (Wrapped α) where smul n m := ⟨SMul.smul n m.1⟩
+ instance : SMul ℤ (Wrapped α) where smul n m := ⟨SMul.smul n m.1⟩
+ instance : Sub (Wrapped α) where sub m n := ⟨Sub.sub m.1 n.1⟩
+ instance : Sup (Wrapped α) where sup s t := ⟨Sup.sup s.1 t.1⟩
+ instance : Zero (Wrapped α) where zero := ⟨0⟩
+ instance [Mul α] : Mul (Wrapped α) where mul m n := ⟨Mul.mul m.1 n.1⟩
+ slowInstance
++ Wrapped
++ val_injective

You can run this locally as follows
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for script/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@github-actions
Copy link
Copy Markdown

PR summary 8e6f7431b6

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference
Mathlib.Tactic 1
Mathlib.Tactic.FastInstance (new file) 20

Declarations diff

+ CommMagma
+ CommSemigroup
+ Dec
+ Function.Injective.commMagma
+ Function.Injective.commSemigroup
+ Function.Injective.semigroup
+ Mul
+ Semigroup
+ Wrapped
+ dec1
+ dec2
+ elabFastInstance
+ instCommSemigroup
+ instRing
+ instSemigroup
+ instSemiring
+ instance : Mul Nat := ⟨(· * · )⟩
+ instance [Mul α] : Mul (Wrapped α) where mul m n := ⟨Mul.mul m.1 n.1⟩
+ val_injective

You can run this locally as follows
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for script/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@mattrobball
Copy link
Copy Markdown
Contributor

!bench

@adomani
Copy link
Copy Markdown
Contributor

adomani commented Jan 22, 2025

Should there also be a test with a measure of some heartbeat count that is smaller with fast_instance% than without?

Co-authored-by: damiano <adomani@gmail.com>
@github-actions
Copy link
Copy Markdown

PR summary 92e327721c

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference
Mathlib.Tactic 1
Mathlib.Tactic.FastInstance (new file) 20

Declarations diff

+ CommMagma
+ CommSemigroup
+ Dec
+ Function.Injective.commMagma
+ Function.Injective.commSemigroup
+ Function.Injective.semigroup
+ Mul
+ Semigroup
+ Wrapped
+ dec1
+ dec2
+ elabFastInstance
+ instCommSemigroup
+ instRing
+ instSemigroup
+ instSemiring
+ instance : Mul Nat := ⟨(· * · )⟩
+ instance [Mul α] : Mul (Wrapped α) where mul m n := ⟨Mul.mul m.1 n.1⟩
+ val_injective

You can run this locally as follows
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for script/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit 8e6f743.
There were no significant changes against commit 80283e8.

@github-actions
Copy link
Copy Markdown

File Instructions %
build +20.981⬝10⁹ (+0.01%)
CI run

@mattrobball
Copy link
Copy Markdown
Contributor

Of course, I forgot that the applications are split off now.

@adomani
Copy link
Copy Markdown
Contributor

adomani commented Jan 22, 2025

I wonder why the PR summary now posts a new comment, rather than updating the old ones. It seems to be limited to this PR, though.

@github-actions
Copy link
Copy Markdown

PR summary 55bc9b1daa

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference
Mathlib.Tactic 1
Mathlib.Tactic.FastInstance (new file) 20

Declarations diff

+ CommMagma
+ CommSemigroup
+ Dec
+ Function.Injective.commMagma
+ Function.Injective.commSemigroup
+ Function.Injective.semigroup
+ Mul
+ Semigroup
+ Wrapped
+ dec1
+ dec2
+ elabFastInstance
+ instCommSemigroup
+ instRing
+ instSemigroup
+ instSemiring
+ instance : Mul Nat := ⟨(· * · )⟩
+ instance [Mul α] : Mul (Wrapped α) where mul m n := ⟨Mul.mul m.1 n.1⟩
+ val_injective

You can run this locally as follows
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for script/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@mattrobball
Copy link
Copy Markdown
Contributor

mattrobball commented Jan 23, 2025

#20993 has the original changes mostly working on a current master. We has some shenanigans with unknown free variables etc. now though Not on current master

@github-actions
Copy link
Copy Markdown

PR summary 56ec6a243b

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference
Mathlib.Tactic 1
Mathlib.Tactic.FastInstance (new file) 20

Declarations diff

+ CommMagma
+ CommSemigroup
+ Dec
+ Function.Injective.commMagma
+ Function.Injective.commSemigroup
+ Function.Injective.semigroup
+ Mul
+ Semigroup
+ Wrapped
+ dec1
+ dec2
+ elabFastInstance
+ instCommSemigroup
+ instRing
+ instSemigroup
+ instSemiring
+ instance : Mul Nat := ⟨(· * · )⟩
+ instance [Mul α] : Mul (Wrapped α) where mul m n := ⟨Mul.mul m.1 n.1⟩
+ val_injective

You can run this locally as follows
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for script/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@mattrobball
Copy link
Copy Markdown
Contributor

Given the effects of #20933, I will merge this in ~3 hours (after classes) unless anyone objects.

@mattrobball
Copy link
Copy Markdown
Contributor

bors merge

@ghost ghost added the ready-to-merge This PR has been sent to bors. label Jan 23, 2025
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 <kmill31415@gmail.com>
Co-authored-by: Matthew Robert Ballard <100034030+mattrobball@users.noreply.github.com>
@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented Jan 23, 2025

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors bot changed the title feat: add fast_instance% elaborator [Merged by Bors] - feat: add fast_instance% elaborator Jan 23, 2025
@mathlib-bors mathlib-bors bot closed this Jan 23, 2025
@mathlib-bors mathlib-bors bot deleted the fast_instance branch January 23, 2025 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge This PR has been sent to bors. t-meta Tactics, attributes or user commands

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants