-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[Merged by Bors] - feat(Tactic): linarith and rify for NNReal
#35155
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
Closed
DavidLedvinka
wants to merge
50
commits into
leanprover-community:master
from
DavidLedvinka:linarith_for_nnreal
Closed
Changes from all commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
00f4eb5
test
6165595
Revert "test"
38e69bf
Merge branch 'master' of https://github.com/DavidLedvinka/mathlib4
97b18e8
Merge branch 'master' of https://github.com/DavidLedvinka/mathlib4
72423dd
Merge branch 'master' of https://github.com/DavidLedvinka/mathlib4
0ba4995
erge branch 'master' of https://github.com/DavidLedvinka/mathlib4
606e9d8
Merge branch 'master' of https://github.com/DavidLedvinka/mathlib4
f753b22
Merge branch 'master' of https://github.com/DavidLedvinka/mathlib4
3adde15
Merge branch 'master' of https://github.com/DavidLedvinka/mathlib4
c56ec98
Merge branch 'master' of https://github.com/DavidLedvinka/mathlib4
261cdd6
draft
35566bb
fix
acbb85a
test
de7c035
typo
fb67fc0
fix again
3e648c0
small updates
63d15b4
documentation add
9cc29c7
mk_all
f663d26
test
5a3665c
tests
d86ec02
metaMify
72f61fd
fix division bug
10d53e4
fix year
9a858c0
fix unintended addition
DavidLedvinka 911ea8b
fix typo
DavidLedvinka ac76289
Update Mathlib/Tactic/Linarith/NNRealPreprocessor.lean
DavidLedvinka c05eaa3
Update Mathlib/Tactic/Linarith/Preprocessing.lean
DavidLedvinka 867d3aa
impliment suggestions
706f1a7
Update Rify.lean
DavidLedvinka af275a7
fix
ba6377a
Merge branch 'linarith_for_nnreal' of https://github.com/DavidLedvink…
4f23e02
re-add Qify
2f99e41
update
6d4c105
delete accidental test code
4d8dc87
Merge branch 'master' into linarith_for_nnreal
6977b75
Update Mathlib/Tactic/Rify.lean
DavidLedvinka c61b056
Update Mathlib/Tactic/Linarith/Preprocessing.lean
DavidLedvinka a3989c0
adjustments
37efab3
ci: rerun
e6520a4
Update Mathlib/Tactic/Rify.lean
DavidLedvinka aaee6c3
Merge branch 'master' into linarith_for_nnreal
3ab28e1
Merge branch 'linarith_for_nnreal' of https://github.com/DavidLedvink…
76b275e
Merge branch 'master' into linarith_for_nnreal
DavidLedvinka 6bd751e
Update Mathlib/Tactic/Rify.lean
DavidLedvinka 9249463
Update Mathlib/Tactic/Rify.lean
DavidLedvinka 028b601
Update Mathlib/Tactic/Linarith/NNRealPreprocessor.lean
DavidLedvinka 25b74ce
Update Mathlib/Tactic/Linarith/NNRealPreprocessor.lean
DavidLedvinka c2036ba
Update Mathlib/Tactic/Linarith/NNRealPreprocessor.lean
DavidLedvinka c5b10eb
apply suggestions
669cbcb
add comment
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /- | ||
| Copyright (c) 2026 David Ledvinka. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: David Ledvinka | ||
| -/ | ||
| module | ||
|
|
||
| public meta import Mathlib.Tactic.Linarith | ||
| public meta import Mathlib.Tactic.Rify | ||
| public import Mathlib.Data.NNReal.Basic | ||
|
|
||
| /-! | ||
| # NNReal linarith preprocessing | ||
|
|
||
| This file contains a `linarith` preprocessor for converting (in)equalities in `ℝ≥0` to `ℝ`. | ||
|
|
||
| By overriding the behaviour of the placeholder preprocessor `nnrealToReal` (which does nothing | ||
| unless this file is imported) `linarith` can still be used without importing `NNReal`. | ||
| -/ | ||
|
|
||
| public meta section | ||
|
|
||
| namespace Mathlib.Tactic.Linarith | ||
|
|
||
| open Lean Meta | ||
|
|
||
| /-- | ||
| `isNNRealProp tp` is true iff `tp` is an inequality or equality between nonnegative real numbers | ||
| or the negation thereof. | ||
| -/ | ||
| partial def isNNRealProp (e : Expr) : MetaM Bool := succeeds do | ||
| let (_, _, .const ``NNReal _, _, _) ← e.ineqOrNotIneq? | failure | ||
|
|
||
| /-- If `e` is of the form `((x : ℝ≥0) : ℝ)`, `NNReal.toReal e` returns `x`. -/ | ||
| def isNNRealtoReal (e : Expr) : Option Expr := | ||
| match e with | ||
| | .app (.const ``NNReal.toReal _) n => some n | ||
| | _ => none | ||
|
|
||
| /-- | ||
| `getNNRealComparisons e` returns a list of all subexpressions of `e` of the form `(x : ℝ)`. | ||
| -/ | ||
| partial def getNNRealCoes (e : Expr) : List Expr := | ||
| match isNNRealtoReal e with | ||
| | some x => [x] | ||
| | none => match e.getAppFnArgs with | ||
| | (``HAdd.hAdd, #[_, _, _, _, a, b]) => getNNRealCoes a ++ getNNRealCoes b | ||
| | (``HMul.hMul, #[_, _, _, _, a, b]) => getNNRealCoes a ++ getNNRealCoes b | ||
| | (``HSub.hSub, #[_, _, _, _, a, b]) => getNNRealCoes a ++ getNNRealCoes b | ||
| | (``HDiv.hDiv, #[_, _, _, _, a, _]) => getNNRealCoes a | ||
| | (``Neg.neg, #[_, _, a]) => getNNRealCoes a | ||
| | _ => [] | ||
|
|
||
| /-- If `e : ℝ≥0`, returns a proof of `0 ≤ (e : ℝ)`. -/ | ||
| def mk_toReal_nonneg_prf (e : Expr) : MetaM (Option Expr) := | ||
| try commitIfNoEx (mkAppM ``NNReal.coe_nonneg #[e]) | ||
| catch e => do | ||
| trace[linarith] "Got exception when using `coe_nonneg` {e.toMessageData}" | ||
| return none | ||
|
|
||
| initialize nnrealToRealTransform.set fun l => do | ||
| let l ← l.mapM fun e => do | ||
| let t ← whnfR (← instantiateMVars (← inferType e)) | ||
| if ← isNNRealProp t then | ||
| return (← Rify.rifyProof e t).1 | ||
| else | ||
| return e | ||
| let atoms : List Expr ← withNewMCtxDepth <| AtomM.run .reducible do | ||
| for e in l do | ||
| let (_, _, a, b) ← (← inferType e).ineq? | ||
| discard <| (getNNRealCoes a).mapM AtomM.addAtom | ||
| discard <| (getNNRealCoes b).mapM AtomM.addAtom | ||
| return (← get).atoms.toList | ||
| let nonneg_pfs : List Expr ← atoms.filterMapM mk_toReal_nonneg_prf | ||
| return nonneg_pfs ++ l | ||
|
|
||
| end Mathlib.Tactic.Linarith |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
DavidLedvinka marked this conversation as resolved.
|
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import Mathlib.Tactic.Linarith.NNRealPreprocessor | ||
| import Mathlib.Data.ENNReal.Operations | ||
| import Mathlib.Data.ENNReal.Inv | ||
|
|
||
| open NNReal | ||
|
|
||
| example {a b c : ℝ≥0} (h1 : 2 * a < b + 1) (h2 : b ≤ c) : a < (c + 1) / 2 := by | ||
| linarith | ||
|
|
||
| example {a b c d : ℝ≥0} (h1 : 3 * a + 2 * b ≤ 5 * c + 7) (h2 : c ≤ d) : a ≤ (5 * d + 7) / 3 := by | ||
| linarith | ||
|
|
||
| example {a b c d e : ℝ≥0} (h1 : (a + b) / 2 + c ≤ d) (h2 : d ≤ e) : a ≤ 2 * e := by | ||
| linarith | ||
|
|
||
| example {a b c d : ℝ≥0} (h1 : (a + 3 * b) / 4 < c + 1) (h2 : c ≤ d) : a < 4 * (d + 1) := by | ||
| linarith | ||
|
|
||
| example {a b c d : ℝ≥0} (h1 : 2 * a + b ≤ 3 * c) (h2 : c < d + 5) : a < (3 * (d + 5)) / 2 := by | ||
| linarith | ||
|
|
||
| example {a b c d : ℝ≥0} (h1 : a + b ≤ c) (h2 : c ≤ d / 2) : a ≤ d / 2 := by | ||
| linarith | ||
|
|
||
| example {a b c d : ℝ≥0} (h1 : a + b ≤ 2 * c + 3) (h2 : c ≤ d) : a ≤ 2 * d + 3 := by | ||
| linarith |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.