Skip to content
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

fix: modify projection instance binder info #5376

Merged
merged 4 commits into from
Sep 20, 2024
Merged

fix: modify projection instance binder info #5376

merged 4 commits into from
Sep 20, 2024

Conversation

leodemoura
Copy link
Member

closes #5333

This PR tries to address issue #5333.

My conjecture is that the binder annotations for C.toB and Algebra.toSMul are not ideal. Algebra.toSMul is one of declarations where the new command set_synth_order was used. Both classes, C and Algebra, are parametric over instances, and in both cases, the issue arises due to projection instances: C.toB and Algebra.toSMul. Let's focus on the binder annotations for C.toB. They are as follows:

C.toB [inst : A 20000] [self : @C inst] : @B ...

As a projection, it seems odd that inst is an instance-implicit argument instead of an implicit one, given that its value is fixed by self. We observe the same issue in Algebra.toSMul:

Algebra.toSMul {R : Type u} {A : Type v} [inst1 : CommSemiring R] [inst2 : Semiring A]
   [self : @Algebra R A inst1 inst2] : SMul R A

The PR changes the binder annotations as follows:

C.toB {inst : A 20000} [self : @C inst] : @B ...

and

Algebra.toSMul {R : Type u} {A : Type v} {inst1 : CommSemiring R} {inst2 : Semiring A}
    [self : @Algebra R A inst1 inst2] : SMul R A

In both cases, the set_synth_order is used to force self to be processed first.

In the MWE, there is no instance for C ..., and C.toB is quickly discarded. I suspect a similar issue occurs when trying to use Algebra.toSMul, where there is no @Algebra R A ... ..., but Lean spends unnecessary time trying to synthesize CommSemiring R and Semiring A instances. I believe the new binder annotations make sense, as if there is a way to synthesize Algebra R A ... ..., it will tell us how to retrieve the instance-implicit arguments.

TODO:

  • Impact on Mathlib.
  • Document changes.

closes #5333

This PR tries to address issue #5333.

My conjecture is that the binder annotations for `C.toB` and `Algebra.toSMul` are not ideal. `Algebra.toSMul` is one of declarations where the new command `set_synth_order` was used. Both classes, `C` and `Algebra`, are parametric over instances, and in both cases, the issue arises due to projection instances: `C.toB` and `Algebra.toSMul`. Let's focus on the binder annotations for `C.toB`. They are as follows:

```
C.toB [inst : A 20000] [self : @C inst] : @b ...
```

As a projection, it seems odd that `inst` is an instance-implicit argument instead of an implicit one, given that its value is fixed by `self`. We observe the same issue in `Algebra.toSMul`:

```
Algebra.toSMul {R : Type u} {A : Type v} [inst1 : CommSemiring R] [inst2 : Semiring A]
   [self : @algebra R A inst1 inst2] : SMul R A
```

The PR changes the binder annotations as follows:

```
C.toB {inst : A 20000} [self : @C inst] : @b ...
```

and

```
Algebra.toSMul {R : Type u} {A : Type v} {inst1 : CommSemiring R} {inst2 : Semiring A}
    [self : @algebra R A inst1 inst2] : SMul R A
```

In both cases, the `set_synth_order` is used to force `self` to be processed first.

In the MWE, there is no instance for `C ...`, and `C.toB` is quickly discarded. I suspect a similar issue occurs when trying to use `Algebra.toSMul`, where there is no `@Algebra R A ... ...`, but Lean spends unnecessary time trying to synthesize `CommSemiring R` and `Semiring A` instances. I believe the new binder annotations make sense, as if there is a way to synthesize `Algebra R A ... ...`, it will tell us how to retrieve the instance-implicit arguments.

Not sure what the impact will be on Mathlib.
@leodemoura leodemoura marked this pull request as draft September 18, 2024 00:47
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc September 18, 2024 00:59 Inactive
@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 18, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Sep 18, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Sep 18, 2024
@leanprover-community-bot leanprover-community-bot added the breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan label Sep 18, 2024
@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

@leodemoura
Copy link
Member Author

!bench

@leanprover-bot
Copy link
Collaborator

Here are the benchmark results for commit 170166e.
There were significant changes against commit c25d206:

  Benchmark          Metric       Change
  ================================================
- ilean roundtrip    compress       1.0%  (17.6 σ)
+ lake build no-op   task-clock    -2.1% (-23.4 σ)

@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

@leodemoura leodemoura marked this pull request as ready for review September 19, 2024 03:19
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc September 19, 2024 03:31 Inactive
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Sep 19, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Sep 19, 2024
@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

kim-em added a commit to leanprover-community/mathlib4 that referenced this pull request Sep 19, 2024
@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

kim-em added a commit to leanprover-community/mathlib4 that referenced this pull request Sep 19, 2024
@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

@leanprover-community-bot leanprover-community-bot 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 Sep 19, 2024
@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

@leanprover-community-bot leanprover-community-bot added breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan and removed builds-mathlib CI has verified that Mathlib builds against this PR labels Sep 19, 2024
@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

@leanprover-community-bot leanprover-community-bot removed the breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan label Sep 19, 2024
@leanprover-community-bot leanprover-community-bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Sep 19, 2024
@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

@mattrobball
Copy link
Contributor

Yes, this is exactly my understanding of the problem in Mathlib and this fix is far more sensible. Thanks @leodemoura !

@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc September 20, 2024 01:59 Inactive
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Sep 20, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Sep 20, 2024
@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

@kim-em kim-em added this pull request to the merge queue Sep 20, 2024
Merged via the queue into master with commit 0a2d121 Sep 20, 2024
15 checks passed
@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

mathlib-bors bot pushed a commit to leanprover-community/mathlib4 that referenced this pull request Oct 8, 2024
With the change to the construction of the synthesis order algorithm, we want to avoid searches with metavariables by specifying more `outParam`s.
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 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.

modifying TC synth order can give 2% speedup of mathlib
6 participants