Skip to content

Commit

Permalink
fix: catch 'none' exception in Real.sqrt positivity extension (#8014)
Browse files Browse the repository at this point in the history
This example currently fails
```lean
example (x : ℝ) : 0 ≤ Real.sqrt x := by positivity
```
due to the exception being thrown here:
https://github.com/leanprover-community/mathlib4/blob/b56efa53d7479fda9740f364170cbaef34699dee/Mathlib/Tactic/Positivity/Core.lean#L304

It should succeed, because the junk value used for `sqrt` of a negative number is non-negative.

This PR fixes it by catching the exception, a pattern used elsewhere:
https://github.com/leanprover-community/mathlib4/blob/b56efa53d7479fda9740f364170cbaef34699dee/Mathlib/Tactic/Positivity/Core.lean#L325
  • Loading branch information
dwrensha committed Oct 29, 2023
1 parent b56efa5 commit a0343c1
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Mathlib/Data/Real/Sqrt.lean
Expand Up @@ -380,7 +380,7 @@ def evalSqrt : PositivityExt where eval {_ _} _zα _pα e := do
let (.app _ (a : Q(Real))) ← whnfR e | throwError "not Real.sqrt"
let zα' ← synthInstanceQ (q(Zero Real) : Q(Type))
let pα' ← synthInstanceQ (q(PartialOrder Real) : Q(Type))
let ra ← core zα' pα' a
let ra ← catchNone <| core zα' pα' a
assertInstancesCommute
match ra with
| .positive pa => pure (.positive (q(Real.sqrt_pos_of_pos $pa) : Expr))
Expand Down
1 change: 1 addition & 0 deletions test/positivity.lean
Expand Up @@ -216,6 +216,7 @@ example {a : ℤ} (ha : 1 < a) : 0 < |(3:ℤ) + a| := by positivity

example : 0 < NNReal.sqrt 5 := by positivity
example : 0 ≤ Real.sqrt (-5) := by positivity
example (x : ℝ) : 0 ≤ Real.sqrt x := by positivity
example : 0 < Real.sqrt 5 := by positivity

example {a : ℝ} (_ha : 0 ≤ a) : 0 ≤ Real.sqrt a := by positivity
Expand Down

0 comments on commit a0343c1

Please sign in to comment.