Skip to content

Commit 4cacb7e

Browse files
YaelDilliesJun2M
authored andcommitted
refactor(Rat): Streamline basic theory (#11504)
`Rat` has a long history in (and before) mathlib and as such its development is full of cruft. Now that `NNRat` is a thing, there is a need for the theory of `Rat` to be mimickable to yield the theory of `NNRat`, which is not currently the case. Broadly, this PR aims at mirroring the `Rat` and `NNRat` declarations. It achieves this by: * Relying more on `Rat.num` and `Rat.den`, and less on the structure representation of `Rat` * Abandoning the vestigial `Rat.Nonneg` (which was replaced in Std by a new development of the order on `Rat`) * Renaming many `Rat` lemmas with dubious names. This creates quite a lot of conflicts with Std lemmas, whose names are themselves dubious. I am priming the relevant new mathlib names and leaving TODOs to fix the Std names. * Handling most of the `Rat` porting notes Reduce the diff of #11203
1 parent 63d9bf0 commit 4cacb7e

File tree

20 files changed

+419
-511
lines changed

20 files changed

+419
-511
lines changed

Mathlib/Algebra/Order/Archimedean.lean

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,6 @@ theorem exists_rat_btwn {x y : α} (h : x < y) : ∃ q : ℚ, x < q ∧ (q : α)
302302
rw [Rat.num_natCast, Int.cast_natCast, Nat.cast_eq_zero] at H
303303
subst H
304304
cases n0
305-
· rw [Rat.den_natCast, Nat.cast_one]
306-
exact one_ne_zero
307305
#align exists_rat_btwn exists_rat_btwn
308306

309307
theorem le_of_forall_rat_lt_imp_le (h : ∀ q : ℚ, (q : α) < x → (q : α) ≤ y) : x ≤ y :=

Mathlib/Algebra/Order/Nonneg/Field.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This is used to derive algebraic structures on `ℝ≥0` and `ℚ≥0` automatic
2222
* `{x : α // 0 ≤ x}` is a `CanonicallyLinearOrderedSemifield` if `α` is a `LinearOrderedField`.
2323
-/
2424

25+
assert_not_exists abs_inv
2526

2627
open Set
2728

Mathlib/Combinatorics/Additive/PluenneckeRuzsa.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ Copyright (c) 2022 Yaël Dillies, George Shakan. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Yaël Dillies, George Shakan
55
-/
6+
import Mathlib.Algebra.GroupPower.Order
67
import Mathlib.Combinatorics.Enumerative.DoubleCounting
78
import Mathlib.Data.Finset.Pointwise
8-
import Mathlib.Data.NNRat.Lemmas
99
import Mathlib.Tactic.GCongr
10-
import Mathlib.Algebra.GroupPower.Order
1110

1211
#align_import combinatorics.additive.pluennecke_ruzsa from "leanprover-community/mathlib"@"4aab2abced69a9e579b1e6dc2856ed3db48e2cbd"
1312

Mathlib/Combinatorics/SimpleGraph/Density.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Mathlib.Combinatorics.SimpleGraph.Basic
88
import Mathlib.Data.Rat.Cast.Order
99
import Mathlib.Order.Partition.Finpartition
1010
import Mathlib.Tactic.GCongr
11+
import Mathlib.Tactic.NormNum
1112
import Mathlib.Tactic.Positivity
1213
import Mathlib.Tactic.Ring
1314

Mathlib/Data/NNRat/Defs.lean

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,18 @@ of `x` with `↑x`. This tactic also works for a function `f : α → ℚ` with
3030

3131
open Function
3232

33-
/-- Nonnegative rational numbers. -/
34-
def NNRat := { q : ℚ // 0 ≤ q } deriving
35-
CanonicallyOrderedCommSemiring, CanonicallyLinearOrderedAddCommMonoid, Sub, Inhabited
36-
#align nnrat NNRat
33+
deriving instance CanonicallyOrderedCommSemiring for NNRat
34+
deriving instance CanonicallyLinearOrderedAddCommMonoid for NNRat
35+
deriving instance Sub for NNRat
36+
deriving instance Inhabited for NNRat
3737

38-
-- Porting note (#10754): Added these instances to get `OrderedSub, DenselyOrdered, Archimedean`
39-
-- instead of `deriving` them
40-
instance : OrderedSub NNRat := Nonneg.orderedSub
41-
42-
scoped[NNRat] notation "ℚ≥0" => NNRat
38+
-- TODO: `deriving instance OrderedSub for NNRat` doesn't work yet, so we add the instance manually
39+
instance NNRat.instOrderedSub : OrderedSub ℚ≥0 := Nonneg.orderedSub
4340

4441
namespace NNRat
4542

4643
variable {α : Type*} {p q : ℚ≥0}
4744

48-
instance instCoe : Coe ℚ≥0 ℚ := ⟨Subtype.val⟩
49-
50-
/-
51-
-- Simp lemma to put back `n.val` into the normal form given by the coercion.
52-
@[simp]
53-
theorem val_eq_coe (q : ℚ≥0) : q.val = q :=
54-
rfl
55-
-/
56-
-- Porting note: `val_eq_coe` is no longer needed.
5745
#noalign nnrat.val_eq_coe
5846

5947
instance canLift : CanLift ℚ ℚ≥0 (↑) fun q ↦ 0 ≤ q where
@@ -358,26 +346,21 @@ namespace NNRat
358346

359347
variable {p q : ℚ≥0}
360348

361-
/-- The numerator of a nonnegative rational. -/
362-
@[pp_dot] def num (q : ℚ≥0) : ℕ := (q : ℚ).num.natAbs
363-
#align nnrat.num NNRat.num
364-
365-
/-- The denominator of a nonnegative rational. -/
366-
@[pp_dot] def den (q : ℚ≥0) : ℕ := (q : ℚ).den
367-
#align nnrat.denom NNRat.den
368-
369349
@[norm_cast] lemma num_coe (q : ℚ≥0) : (q : ℚ).num = q.num := by
370350
simp [num, abs_of_nonneg, Rat.num_nonneg, q.2]
371351

372352
theorem natAbs_num_coe : (q : ℚ).num.natAbs = q.num := rfl
373353
#align nnrat.nat_abs_num_coe NNRat.natAbs_num_coe
374354

375-
@[simp, norm_cast] lemma den_coe : (q : ℚ).den = q.den := rfl
355+
@[norm_cast] lemma den_coe : (q : ℚ).den = q.den := rfl
376356
#align nnrat.denom_coe NNRat.den_coe
377357

378358
@[simp] lemma num_ne_zero : q.num ≠ 0 ↔ q ≠ 0 := by simp [num]
379359
@[simp] lemma num_pos : 0 < q.num ↔ 0 < q := by simp [pos_iff_ne_zero]
380360
@[simp] lemma den_pos (q : ℚ≥0) : 0 < q.den := Rat.den_pos _
361+
@[simp] lemma den_ne_zero (q : ℚ≥0) : q.den ≠ 0 := Rat.den_ne_zero _
362+
363+
lemma coprime_num_den (q : ℚ≥0) : q.num.Coprime q.den := by simpa [num, den] using Rat.reduced _
381364

382365
-- TODO: Rename `Rat.coe_nat_num`, `Rat.intCast_den`, `Rat.ofNat_num`, `Rat.ofNat_den`
383366
@[simp, norm_cast] lemma num_natCast (n : ℕ) : num n = n := rfl
@@ -395,7 +378,34 @@ theorem ext_num_den_iff : p = q ↔ p.num = q.num ∧ p.den = q.den :=
395378
by rintro rfl; exact ⟨rfl, rfl⟩, fun h ↦ ext_num_den h.1 h.2
396379
#align nnrat.ext_num_denom_iff NNRat.ext_num_den_iff
397380

398-
end NNRat
381+
/-- Form the quotient `n / d` where `n d : ℕ`.
399382
400-
-- `NNRat` needs to be available in the definition of `Field`
401-
assert_not_exists Field
383+
See also `Rat.divInt` and `mkRat`. -/
384+
def divNat (n d : ℕ) : ℚ≥0 := ⟨.divInt n d, Rat.divInt_nonneg n.cast_nonneg d.cast_nonneg⟩
385+
386+
variable {n₁ n₂ d₁ d₂ d : ℕ}
387+
388+
@[simp, norm_cast] lemma coe_divNat (n d : ℕ) : (divNat n d : ℚ) = .divInt n d := rfl
389+
390+
lemma mk_divInt (n d : ℕ) :
391+
⟨.divInt n d, Rat.divInt_nonneg n.cast_nonneg d.cast_nonneg⟩ = divNat n d := rfl
392+
393+
lemma divNat_inj (h₁ : d₁ ≠ 0) (h₂ : d₂ ≠ 0) : divNat n₁ d₁ = divNat n₂ d₂ ↔ n₁ * d₂ = n₂ * d₁ := by
394+
rw [← coe_inj]; simp [Rat.mkRat_eq_iff, h₁, h₂]; norm_cast
395+
396+
@[simp] lemma num_divNat_den (q : ℚ≥0) : divNat q.num q.den = q :=
397+
ext $ by rw [← (q : ℚ).mkRat_num_den']; simp [num_coe, den_coe]
398+
399+
/-- Define a (dependent) function or prove `∀ r : ℚ, p r` by dealing with nonnegative rational
400+
numbers of the form `n / d` with `d ≠ 0` and `n`, `d` coprime. -/
401+
@[elab_as_elim]
402+
def numDenCasesOn.{u} {C : ℚ≥0Sort u} (q) (H : ∀ n d, d ≠ 0 → n.Coprime d → C (divNat n d)) :
403+
C q := by rw [← q.num_divNat_den]; exact H _ _ q.den_ne_zero q.coprime_num_den
404+
405+
lemma add_def (q r : ℚ≥0) : q + r = divNat (q.num * r.den + r.num * q.den) (q.den * r.den) := by
406+
ext; simp [Rat.add_def', Rat.mkRat_eq, num_coe, den_coe]
407+
408+
lemma mul_def (q r : ℚ≥0) : q * r = divNat (q.num * r.num) (q.den * r.den) := by
409+
ext; simp [Rat.mul_def', Rat.mkRat_eq, num_coe, den_coe]
410+
411+
end NNRat

Mathlib/Data/NNRat/Lemmas.lean

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Yaël Dillies, Bhavik Mehta
55
-/
66
import Mathlib.Algebra.Function.Indicator
7-
import Mathlib.Algebra.Order.Field.Basic
8-
import Mathlib.Algebra.Order.Nonneg.Field
9-
import Mathlib.Data.NNRat.Defs
107
import Mathlib.Data.Rat.Field
118

129
#align_import data.rat.nnrat from "leanprover-community/mathlib"@"b3f4f007a962e3787aa0f3b5c7942a1317f7d88e"
@@ -21,20 +18,9 @@ cycles.
2118
open Function
2219
open scoped NNRat
2320

24-
-- The `LinearOrderedCommGroupWithZero` instance is a shortcut instance for performance
25-
deriving instance CanonicallyLinearOrderedSemifield, LinearOrderedCommGroupWithZero for NNRat
26-
2721
namespace NNRat
2822
variable {α : Type*} {p q : ℚ≥0}
2923

30-
instance instDenselyOrdered : DenselyOrdered ℚ≥0 := Nonneg.instDenselyOrdered
31-
32-
@[simp, norm_cast] lemma coe_inv (q : ℚ≥0) : ((q⁻¹ : ℚ≥0) : ℚ) = (q : ℚ)⁻¹ := rfl
33-
#align nnrat.coe_inv NNRat.coe_inv
34-
35-
@[simp, norm_cast] lemma coe_div (p q : ℚ≥0) : ((p / q : ℚ≥0) : ℚ) = p / q := rfl
36-
#align nnrat.coe_div NNRat.coe_div
37-
3824
/-- A `MulAction` over `ℚ` restricts to a `MulAction` over `ℚ≥0`. -/
3925
instance [MulAction ℚ α] : MulAction ℚ≥0 α :=
4026
MulAction.compHom α coeHom.toMonoidHom

Mathlib/Data/Nat/Digits.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ theorem digits_two_eq_bits (n : ℕ) : digits 2 n = n.bits.map fun b => cond b 1
598598
cases b
599599
· rw [digits_def' one_lt_two]
600600
· simpa [Nat.bit, Nat.bit0_val n]
601-
· simpa [pos_iff_ne_zero, bit_eq_zero_iff]
601+
· simpa [pos_iff_ne_zero, Nat.bit0_eq_zero]
602602
· simpa [Nat.bit, Nat.bit1_val n, add_comm, digits_add 2 one_lt_two 1 n, Nat.add_mul_div_left]
603603
#align nat.digits_two_eq_bits Nat.digits_two_eq_bits
604604

Mathlib/Data/Rat/Cast/CharZero.lean

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ theorem cast_inj [CharZero α] : ∀ {m n : ℚ}, (m : α) = n ↔ m = n
3131
refine' ⟨fun h => _, congr_arg _⟩
3232
have d₁a : (d₁ : α) ≠ 0 := Nat.cast_ne_zero.2 d₁0
3333
have d₂a : (d₂ : α) ≠ 0 := Nat.cast_ne_zero.2 d₂0
34-
rw [num_den', num_den'] at h ⊢
35-
rw [cast_mk_of_ne_zero, cast_mk_of_ne_zero] at h <;> simp [d₁0, d₂0] at h ⊢
36-
rwa [eq_div_iff_mul_eq d₂a, division_def, mul_assoc, (d₁.cast_commute (d₂ : α)).inv_left₀.eq,
37-
mul_assoc, ← division_def, eq_comm, eq_div_iff_mul_eq d₁a, eq_comm, ← Int.cast_natCast d₁,
38-
Int.cast_mul, ← Int.cast_natCast d₂, ← Int.cast_mul, Int.cast_inj,
39-
← mkRat_eq_iff d₁0 d₂0] at h
34+
rw [mk'_eq_divInt, mk'_eq_divInt] at h ⊢
35+
rw [cast_divInt_of_ne_zero, cast_divInt_of_ne_zero] at h <;> simp [d₁0, d₂0] at h ⊢
36+
rwa [eq_div_iff_mul_eq d₂a, division_def, mul_assoc, (d₁.cast_commute (d₂ : α)).inv_left₀.eq,
37+
mul_assoc, ← division_def, eq_comm, eq_div_iff_mul_eq d₁a, eq_comm, ← Int.cast_natCast d₁,
38+
Int.cast_mul, ← Int.cast_natCast d₂, ← Int.cast_mul, Int.cast_inj, ← mkRat_eq_iff d₁0 d₂0]
39+
at h
4040
#align rat.cast_inj Rat.cast_inj
4141

4242
theorem cast_injective [CharZero α] : Function.Injective ((↑) : ℚ → α)

0 commit comments

Comments
 (0)