Skip to content

Commit 09610fd

Browse files
committed
feat: add a positivity extension for Real.toNNReal (#31956)
1 parent 5eaea33 commit 09610fd

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Mathlib/Data/NNReal/Defs.lean

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,9 @@ theorem cast_natAbs_eq_nnabs_cast (n : ℤ) : (n.natAbs : ℝ≥0) = nnabs n :=
908908
ext
909909
rw [NNReal.coe_natCast, Nat.cast_natAbs, Real.coe_nnabs, Int.cast_abs]
910910

911+
@[simp]
912+
theorem nnabs_pos {x : ℝ} : 0 < x.nnabs ↔ x ≠ 0 := by simp [← NNReal.coe_pos]
913+
911914
/-- Every real number nonnegative or nonpositive, phrased using `ℝ≥0`. -/
912915
lemma nnreal_dichotomy (r : ℝ) : ∃ x : ℝ≥0, r = x ∨ r = -x := by
913916
obtain (hr | hr) : 0 ≤ r ∨ 0 ≤ -r := by simpa using le_total ..
@@ -995,4 +998,28 @@ meta def evalNNRealtoReal : PositivityExt where eval {u α} _zα _pα e := do
995998
| _ => pure (.nonnegative q(NNReal.coe_nonneg $a))
996999
| _, _, _ => throwError "not NNReal.toReal"
9971000

1001+
/-- Extension for the `positivity` tactic: `Real.toNNReal. -/
1002+
@[positivity Real.toNNReal _]
1003+
meta def evalRealToNNReal : PositivityExt where eval {u α} _zα _pα e := do
1004+
match u, α, e with
1005+
| 0, ~q(ℝ≥0), ~q(Real.toNNReal $a) =>
1006+
assertInstancesCommute
1007+
match (← core q(inferInstance) q(inferInstance) a) with
1008+
| .positive pa => pure (.positive q(toNNReal_pos.mpr $pa))
1009+
| _ => failure
1010+
| _, _, _ => throwError "not Real.toNNReal"
1011+
1012+
private alias ⟨_, nnabs_pos_of_pos⟩ := Real.nnabs_pos
1013+
1014+
/-- Extension for the `positivity` tactic: `Real.nnabs. -/
1015+
@[positivity Real.nnabs _]
1016+
meta def evalRealNNAbs : PositivityExt where eval {u α} _zα _pα e := do
1017+
match u, α, e with
1018+
| 0, ~q(ℝ≥0), ~q(Real.nnabs $a) =>
1019+
assertInstancesCommute
1020+
match (← core q(inferInstance) q(inferInstance) a).toNonzero with
1021+
| some pa => pure (.positive q(nnabs_pos_of_pos $pa))
1022+
| _ => failure
1023+
| _, _, _ => throwError "not Real.nnabs"
1024+
9981025
end Mathlib.Meta.Positivity

MathlibTest/positivity.lean

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,19 @@ example (a b : ℤ) (ha : a ≠ 0) (hb : b ≠ 0) : 0 < a.lcm b := by positivity
252252
example (a : ℕ) (ha : a ≠ 0) : 0 < a.sqrt := by positivity
253253
example (a : ℕ) (ha : a ≠ 0) : 0 < a.totient := by positivity
254254

255+
section NNReal
256+
257+
example (a : ℝ) (ha : 0 < a) : 0 < a.toNNReal := by positivity
258+
example (a : ℝ) : 0 ≤ a.toNNReal := by positivity
259+
example (a : ℝ) : 0 ≤ a.nnabs := by positivity
260+
example (a : ℝ) (ha : 0 < a) : 0 < a.nnabs := by positivity
261+
example (a : ℝ) (ha : a ≠ 0) : 0 < a.nnabs := by positivity
262+
example (a : ℝ≥0) (ha : 0 < a) : 0 < (a : ℝ) := by positivity
263+
example (a : ℝ≥0) (ha : a ≠ 0) : 0 < (a : ℝ) := by positivity
264+
example (a : ℝ≥0) : 0 ≤ (a : ℝ) := by positivity
265+
266+
end NNReal
267+
255268
section ENNReal
256269

257270
variable {a b : ℝ≥0∞}

0 commit comments

Comments
 (0)