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

[Merged by Bors] - refactor(Tactic/Positivity): use stricter Qq matching #10196

Closed
wants to merge 17 commits into from

Conversation

eric-wieser
Copy link
Member

@eric-wieser eric-wieser commented Feb 2, 2024

The previous code often discarded the safety features of Qq by casting quoted terms to Expr and back. This is far from an exhaustive replacement.

This makes use of a bug fix in Lean 4.6.0rc1 that allows us to write things like

match u, α, e with
| 0, ~q(ℤ), ~q(@Int.floor $α' $i $j $a) =>

Previously these matches did not generalize u correctly.

To make Qq happy, we introduce a few more assertInstancesCommute that were not previously here.
Without them, there is a higher risk that positivity produces an ill-typed proof in weird situations.
Like every assertInstancesCommute, this comes at a small performance cost that could be eliminated by using the unsafe assumeInstancesCommute instead.

Another very small performance hit here is from the (possibly unnecessary) defeq check of the types before checking defeq of the values. On the other hand, this might actually increase performance when the match fails due to a type mismatch.

There is probably some boilerplate that can be extracted from the repetition here; but I am declaring that out of scope for this PR: the goal is to establish a canonical spelling for this sort of matching, so that future extensions copy-pasted from these extensions inherit the spelling automatically.


Open in Gitpod

The previous code often discarded the safety features of Qq by casting quoted terms to `Expr` and back.
This is far from an exhaustive replacement.
@eric-wieser eric-wieser added awaiting-CI t-meta Tactics, attributes or user commands labels Feb 2, 2024
@eric-wieser eric-wieser added the WIP Work in progress label Feb 2, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the blocked-by-other-PR This PR depends on another PR to Mathlib label Feb 2, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot removed the blocked-by-other-PR This PR depends on another PR to Mathlib label Feb 3, 2024
@eric-wieser
Copy link
Member Author

!bench

@leanprover-bot
Copy link
Collaborator

Here are the benchmark results for commit 4162ad4.
There were no significant changes against commit 0311bbc.

@eric-wieser eric-wieser added awaiting-review The author would like community review of the PR and removed WIP Work in progress labels Feb 4, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. label Feb 5, 2024
@YaelDillies
Copy link
Collaborator

Sorry, the conflicts are from me moving the extensions around.

@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added blocked-by-other-PR This PR depends on another PR to Mathlib and removed blocked-by-other-PR This PR depends on another PR to Mathlib labels Feb 5, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. label Feb 6, 2024
Copy link
Collaborator

@YaelDillies YaelDillies left a comment

Choose a reason for hiding this comment

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

Those changes alll look like improvements to me, although I haven't checked you caught all possible ones.

Mathlib/Analysis/SpecialFunctions/Gamma/Basic.lean Outdated Show resolved Hide resolved
Mathlib/Analysis/SpecialFunctions/Trigonometric/Basic.lean Outdated Show resolved Hide resolved
Comment on lines +346 to +349
| .nonnegative _pa =>
pure .none
| .none =>
pure .none
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
| .nonnegative _pa =>
pure .none
| .none =>
pure .none
| _ => pure .none

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd prefer to keep this change focused on the match changes and nothing more (these lines vanish from the diff once you ignore whitespace).

@YaelDillies
Copy link
Collaborator

Let's get this done. (and thanks for always being a step ahead, I was just wondering what happened to that universe match issue)

maintainer merge

@eric-wieser
Copy link
Member Author

maintainer merge

Copy link

github-actions bot commented Feb 7, 2024

🚀 Pull request has been placed on the maintainer queue by eric-wieser.

@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. label Feb 8, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added blocked-by-other-PR This PR depends on another PR to Mathlib and removed merge-conflict The PR has a merge conflict with master, and needs manual merging. labels Feb 8, 2024
@@ -350,29 +350,29 @@ open Lean Meta Qq

/-- Extension for the `positivity` tactic: exponentiation by a real number is positive (namely 1)
when the exponent is zero. The other cases are done in `evalRpow`. -/
@[positivity (_ : ℝ) ^ (0 : ℝ), Pow.pow (_ : ℝ) (0 : ℝ), Real.rpow (_ : ℝ) (0 : ℝ)]
Copy link
Member Author

Choose a reason for hiding this comment

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

The changes to this line are why I marked a dependency on #10344

Copy link
Contributor

@Vierkantor Vierkantor left a comment

Choose a reason for hiding this comment

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

Although my confidence in Lean 4 meta code is still low, the previous approvals make me confident enough to approve this.

bors d+

def evalNNRealSqrt : PositivityExt where eval {u α} _zα _pα e := do
match u, α, e with
| 0, ~q(NNReal), ~q(NNReal.sqrt $a) =>
let ra ← core q(inferInstance) q(inferInstance) a
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let ra ← core q(inferInstance) q(inferInstance) a
let ra ← core q(inferInstance) q(inferInstance) a

Mathlib/Tactic/Positivity/Basic.lean Show resolved Hide resolved
Comment on lines +338 to +341
let ra ← core zα' pα' a
match ra with
| .positive pa =>
assertInstancesCommute
Copy link
Contributor

Choose a reason for hiding this comment

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

Sometimes you write assertInstancesCommute before the match, sometimes after. Is there a reason for this (that you can document somewhere easily found)?

Copy link
Collaborator

@YaelDillies YaelDillies Feb 12, 2024

Choose a reason for hiding this comment

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

All that matters is the position of the assertInstancesCommute/assumeInstancesCommute wrt the inferInstances. In fact, all that matters is that there's no inferInstance not followed by assertInstancesCommute/assumeInstancesCommute before a q() quotation. This is something that's automatically checked by the Qq quotations as you write the code.

The only slight difference is that assertInstancesCommute actually checks that the instance diamonds are defeq, while assumeInstancesCommute simply assumes they are (hence has epsilon performance cost). Hence running assertInstancesCommute before a match that could potentially result in no strictness being found is slightly wasteful.

@mathlib-bors
Copy link
Contributor

mathlib-bors bot commented Feb 12, 2024

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

@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added delegated and removed awaiting-review The author would like community review of the PR labels Feb 12, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot removed the blocked-by-other-PR This PR depends on another PR to Mathlib label Feb 12, 2024
@eric-wieser
Copy link
Member Author

bors merge

@github-actions github-actions bot added the ready-to-merge This PR has been sent to bors. label Feb 12, 2024
mathlib-bors bot pushed a commit that referenced this pull request Feb 12, 2024
The previous code often discarded the safety features of Qq by casting quoted terms to `Expr` and back. This is far from an exhaustive replacement.

This makes use of a bug fix in Lean 4.6.0rc1 that allows us to write things like
```lean
match u, α, e with
| 0, ~q(ℤ), ~q(@int.floor $α' $i $j $a) =>
```
Previously these `match`es did not generalize `u` correctly.

To make Qq happy, we introduce a few more `assertInstancesCommute` that were not previously here.
Without them, there is a higher risk that `positivity` produces an ill-typed proof in weird situations.
Like every `assertInstancesCommute`, this comes at a small performance cost that could be eliminated by using the unsafe `assumeInstancesCommute` instead.

Another very small performance hit here is from the (possibly unnecessary) defeq check of the types before checking defeq of the values. On the other hand, this might actually increase performance when the match fails due to a type mismatch.

There is probably some boilerplate that can be extracted from the repetition here; but I am declaring that out of scope for this PR: the goal is to establish a canonical spelling for this sort of matching, so that future extensions copy-pasted from these extensions inherit the spelling automatically.
@mathlib-bors
Copy link
Contributor

mathlib-bors bot commented Feb 12, 2024

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors bot changed the title refactor(Tactic/Positivity): use stricter Qq matching [Merged by Bors] - refactor(Tactic/Positivity): use stricter Qq matching Feb 12, 2024
@mathlib-bors mathlib-bors bot closed this Feb 12, 2024
@mathlib-bors mathlib-bors bot deleted the eric-wieser/norm-num-matching branch February 12, 2024 14:11
dagurtomas pushed a commit that referenced this pull request Mar 22, 2024
The previous code often discarded the safety features of Qq by casting quoted terms to `Expr` and back. This is far from an exhaustive replacement.

This makes use of a bug fix in Lean 4.6.0rc1 that allows us to write things like
```lean
match u, α, e with
| 0, ~q(ℤ), ~q(@int.floor $α' $i $j $a) =>
```
Previously these `match`es did not generalize `u` correctly.

To make Qq happy, we introduce a few more `assertInstancesCommute` that were not previously here.
Without them, there is a higher risk that `positivity` produces an ill-typed proof in weird situations.
Like every `assertInstancesCommute`, this comes at a small performance cost that could be eliminated by using the unsafe `assumeInstancesCommute` instead.

Another very small performance hit here is from the (possibly unnecessary) defeq check of the types before checking defeq of the values. On the other hand, this might actually increase performance when the match fails due to a type mismatch.

There is probably some boilerplate that can be extracted from the repetition here; but I am declaring that out of scope for this PR: the goal is to establish a canonical spelling for this sort of matching, so that future extensions copy-pasted from these extensions inherit the spelling automatically.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
delegated 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.

None yet

5 participants