Skip to content

Commit 2357b64

Browse files
committed
chore(NNRat): Rearrange imports (#10392)
The goal is to separate the field material on `Rat`/`NNRat` from everything before to make way for `NNRat.cast`. We achieve this by * splitting `Data.Rat.NNRat` into * `Data.NNRat.Defs` for the foundationl stuff that will be needed in the definition of `Field` * `Data.NNRat.Lemmas` for the field and big operators material * moving the field material from `Data.Rat.Order` to `Data.Rat.Basic` * proving a few lemmas by `rfl` rather than `coeHom.some_now_unavailable_lemma` * renaming `Data.Rat.NNRat.BigOperators` to `Data.NNRat.BigOperators`
1 parent 0796b06 commit 2357b64

File tree

15 files changed

+136
-93
lines changed

15 files changed

+136
-93
lines changed

Mathlib.lean

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,9 @@ import Mathlib.Data.MvPolynomial.Polynomial
17941794
import Mathlib.Data.MvPolynomial.Rename
17951795
import Mathlib.Data.MvPolynomial.Supported
17961796
import Mathlib.Data.MvPolynomial.Variables
1797+
import Mathlib.Data.NNRat.BigOperators
1798+
import Mathlib.Data.NNRat.Defs
1799+
import Mathlib.Data.NNRat.Lemmas
17971800
import Mathlib.Data.Nat.Basic
17981801
import Mathlib.Data.Nat.Bits
17991802
import Mathlib.Data.Nat.Bitwise
@@ -1942,7 +1945,6 @@ import Mathlib.Data.QPF.Multivariate.Constructions.Quot
19421945
import Mathlib.Data.QPF.Multivariate.Constructions.Sigma
19431946
import Mathlib.Data.QPF.Univariate.Basic
19441947
import Mathlib.Data.Quot
1945-
import Mathlib.Data.Rat.Basic
19461948
import Mathlib.Data.Rat.BigOperators
19471949
import Mathlib.Data.Rat.Cast.CharZero
19481950
import Mathlib.Data.Rat.Cast.Defs
@@ -1951,11 +1953,10 @@ import Mathlib.Data.Rat.Cast.Order
19511953
import Mathlib.Data.Rat.Defs
19521954
import Mathlib.Data.Rat.Denumerable
19531955
import Mathlib.Data.Rat.Encodable
1956+
import Mathlib.Data.Rat.Field
19541957
import Mathlib.Data.Rat.Floor
19551958
import Mathlib.Data.Rat.Init
19561959
import Mathlib.Data.Rat.Lemmas
1957-
import Mathlib.Data.Rat.NNRat
1958-
import Mathlib.Data.Rat.NNRat.BigOperators
19591960
import Mathlib.Data.Rat.Order
19601961
import Mathlib.Data.Rat.Sqrt
19611962
import Mathlib.Data.Rat.Star

Mathlib/Algebra/Module/Basic.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro
66
import Mathlib.Algebra.Function.Indicator
77
import Mathlib.Algebra.SMulWithZero
88
import Mathlib.Data.Int.Basic
9-
import Mathlib.Data.Rat.NNRat
9+
import Mathlib.Data.NNRat.Defs
1010
import Mathlib.GroupTheory.GroupAction.Group
1111
import Mathlib.GroupTheory.GroupAction.Pi
1212
import Mathlib.Logic.Basic

Mathlib/Algebra/Order/Archimedean.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Mathlib.Algebra.GroupPower.Order
77
import Mathlib.Algebra.Order.Field.Power
88
import Mathlib.Data.Int.LeastGreatest
99
import Mathlib.Data.Rat.Floor
10-
import Mathlib.Data.Rat.NNRat
10+
import Mathlib.Data.NNRat.Defs
1111

1212
#align_import algebra.order.archimedean from "leanprover-community/mathlib"@"6f413f3f7330b94c92a5a27488fdc74e6d483a78"
1313

Mathlib/Algebra/Order/Nonneg/Ring.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ instance distribLattice [DistribLattice α] {a : α} : DistribLattice { x : α /
7070
Set.Ici.distribLattice
7171
#align nonneg.distrib_lattice Nonneg.distribLattice
7272

73-
instance densely_ordered [Preorder α] [DenselyOrdered α] {a : α} :
73+
instance instDenselyOrdered [Preorder α] [DenselyOrdered α] {a : α} :
7474
DenselyOrdered { x : α // a ≤ x } :=
7575
show DenselyOrdered (Ici a) from Set.instDenselyOrdered
76-
#align nonneg.densely_ordered Nonneg.densely_ordered
76+
#align nonneg.densely_ordered Nonneg.instDenselyOrdered
7777

7878
/-- If `sSup ∅ ≤ a` then `{x : α // a ≤ x}` is a `ConditionallyCompleteLinearOrder`. -/
7979
@[reducible]

Mathlib/Combinatorics/Additive/PluenneckeRuzsa.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Authors: Yaël Dillies, George Shakan
55
-/
66
import Mathlib.Combinatorics.DoubleCounting
77
import Mathlib.Data.Finset.Pointwise
8-
import Mathlib.Data.Rat.NNRat
8+
import Mathlib.Data.NNRat.Lemmas
99
import Mathlib.Tactic.GCongr
1010
import Mathlib.Algebra.GroupPower.Order
1111

Mathlib/Data/Rat/NNRat/BigOperators.lean renamed to Mathlib/Data/NNRat/BigOperators.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Yaël Dillies, Bhavik Mehta
55
-/
66
import Mathlib.Algebra.BigOperators.Order
7-
import Mathlib.Data.Rat.NNRat
7+
import Mathlib.Data.NNRat.Defs
88

99
/-! # Casting lemmas for non-negative rational numbers involving sums and products
1010
-/

Mathlib/Data/Rat/NNRat.lean renamed to Mathlib/Data/NNRat/Defs.lean

Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ Copyright (c) 2022 Yaël Dillies, Bhavik Mehta. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Yaël Dillies, Bhavik Mehta
55
-/
6-
import Mathlib.Algebra.Function.Indicator
7-
import Mathlib.Algebra.Order.Nonneg.Field
6+
import Mathlib.Algebra.Order.Nonneg.Ring
87
import Mathlib.Data.Int.Lemmas
98
import Mathlib.Data.Rat.Order
109

@@ -13,8 +12,10 @@ import Mathlib.Data.Rat.Order
1312
/-!
1413
# Nonnegative rationals
1514
16-
This file defines the nonnegative rationals as a subtype of `Rat` and provides its algebraic order
17-
structure.
15+
This file defines the nonnegative rationals as a subtype of `Rat` and provides its basic algebraic
16+
order structure.
17+
18+
Note that `NNRat` is not declared as a `Field` here. See `Data.NNRat.Lemmas` for that instance.
1819
1920
We also define an instance `CanLift ℚ ℚ≥0`. This instance can be used by the `lift` tactic to
2021
replace `x : ℚ` and `hx : 0 ≤ x` in the proof context with `x : ℚ≥0` while replacing all occurrences
@@ -31,14 +32,12 @@ open Function
3132

3233
/-- Nonnegative rational numbers. -/
3334
def NNRat := { q : ℚ // 0 ≤ q } deriving
34-
CanonicallyOrderedCommSemiring, CanonicallyLinearOrderedSemifield, LinearOrderedCommGroupWithZero,
35-
Sub, Inhabited
35+
CanonicallyOrderedCommSemiring, CanonicallyLinearOrderedAddCommMonoid, Sub, Inhabited
3636
#align nnrat NNRat
3737

3838
-- Porting note: Added these instances to get `OrderedSub, DenselyOrdered, Archimedean`
3939
-- instead of `deriving` them
4040
instance : OrderedSub NNRat := Nonneg.orderedSub
41-
instance : DenselyOrdered NNRat := Nonneg.densely_ordered
4241

4342
-- mathport name: nnrat
4443
scoped[NNRat] notation "ℚ≥0" => NNRat
@@ -47,8 +46,7 @@ namespace NNRat
4746

4847
variable {α : Type*} {p q : ℚ≥0}
4948

50-
instance : Coe ℚ≥0 ℚ :=
51-
⟨Subtype.val⟩
49+
instance instCoe : Coe ℚ≥0 ℚ := ⟨Subtype.val⟩
5250

5351
/-
5452
-- Simp lemma to put back `n.val` into the normal form given by the coercion.
@@ -130,16 +128,6 @@ theorem coe_mul (p q : ℚ≥0) : ((p * q : ℚ≥0) : ℚ) = p * q :=
130128
rfl
131129
#align nnrat.coe_mul NNRat.coe_mul
132130

133-
@[simp, norm_cast]
134-
theorem coe_inv (q : ℚ≥0) : ((q⁻¹ : ℚ≥0) : ℚ) = (q : ℚ)⁻¹ :=
135-
rfl
136-
#align nnrat.coe_inv NNRat.coe_inv
137-
138-
@[simp, norm_cast]
139-
theorem coe_div (p q : ℚ≥0) : ((p / q : ℚ≥0) : ℚ) = p / q :=
140-
rfl
141-
#align nnrat.coe_div NNRat.coe_div
142-
143131
-- Porting note: `bit0` `bit1` are deprecated, so remove these theorems.
144132
#noalign nnrat.coe_bit0
145133
#noalign nnrat.coe_bit1
@@ -214,25 +202,11 @@ theorem mk_coe_nat (n : ℕ) : @Eq ℚ≥0 (⟨(n : ℚ), n.cast_nonneg⟩ : ℚ
214202
ext (coe_natCast n).symm
215203
#align nnrat.mk_coe_nat NNRat.mk_coe_nat
216204

217-
/-- A `MulAction` over `ℚ` restricts to a `MulAction` over `ℚ≥0`. -/
218-
instance [MulAction ℚ α] : MulAction ℚ≥0 α :=
219-
MulAction.compHom α coeHom.toMonoidHom
220-
221-
/-- A `DistribMulAction` over `ℚ` restricts to a `DistribMulAction` over `ℚ≥0`. -/
222-
instance [AddCommMonoid α] [DistribMulAction ℚ α] : DistribMulAction ℚ≥0 α :=
223-
DistribMulAction.compHom α coeHom.toMonoidHom
224-
225205
@[simp]
226206
theorem coe_coeHom : ⇑coeHom = ((↑) : ℚ≥0 → ℚ) :=
227207
rfl
228208
#align nnrat.coe_coe_hom NNRat.coe_coeHom
229209

230-
@[simp, norm_cast]
231-
theorem coe_indicator (s : Set α) (f : α → ℚ≥0) (a : α) :
232-
((s.indicator f a : ℚ≥0) : ℚ) = s.indicator (fun x ↦ ↑(f x)) a :=
233-
(coeHom : ℚ≥0 →+ ℚ).map_indicator _ _ _
234-
#align nnrat.coe_indicator NNRat.coe_indicator
235-
236210
@[simp, norm_cast]
237211
theorem coe_pow (q : ℚ≥0) (n : ℕ) : (↑(q ^ n) : ℚ) = (q : ℚ) ^ n :=
238212
coeHom.map_pow _ _
@@ -359,21 +333,6 @@ theorem toNNRat_mul (hp : 0 ≤ p) : toNNRat (p * q) = toNNRat p * toNNRat q :=
359333
rw [toNNRat_eq_zero.2 hq, toNNRat_eq_zero.2 hpq, mul_zero]
360334
#align rat.to_nnrat_mul Rat.toNNRat_mul
361335

362-
theorem toNNRat_inv (q : ℚ) : toNNRat q⁻¹ = (toNNRat q)⁻¹ := by
363-
obtain hq | hq := le_total q 0
364-
· rw [toNNRat_eq_zero.mpr hq, inv_zero, toNNRat_eq_zero.mpr (inv_nonpos.mpr hq)]
365-
· nth_rw 1 [← Rat.coe_toNNRat q hq]
366-
rw [← coe_inv, toNNRat_coe]
367-
#align rat.to_nnrat_inv Rat.toNNRat_inv
368-
369-
theorem toNNRat_div (hp : 0 ≤ p) : toNNRat (p / q) = toNNRat p / toNNRat q := by
370-
rw [div_eq_mul_inv, div_eq_mul_inv, ← toNNRat_inv, ← toNNRat_mul hp]
371-
#align rat.to_nnrat_div Rat.toNNRat_div
372-
373-
theorem toNNRat_div' (hq : 0 ≤ q) : toNNRat (p / q) = toNNRat p / toNNRat q := by
374-
rw [div_eq_inv_mul, div_eq_inv_mul, toNNRat_mul (inv_nonneg.2 hq), toNNRat_inv]
375-
#align rat.to_nnrat_div' Rat.toNNRat_div'
376-
377336
end Rat
378337

379338
/-- The absolute value on `ℚ` as a map to `ℚ≥0`. -/
@@ -430,18 +389,7 @@ theorem ext_num_den_iff : p = q ↔ p.num = q.num ∧ p.den = q.den :=
430389
by rintro rfl; exact ⟨rfl, rfl⟩, fun h ↦ ext_num_den h.1 h.2
431390
#align nnrat.ext_num_denom_iff NNRat.ext_num_den_iff
432391

433-
@[simp]
434-
theorem num_div_den (q : ℚ≥0) : (q.num : ℚ≥0) / q.den = q := by
435-
ext1
436-
rw [coe_div, coe_natCast, coe_natCast, num, ← Int.cast_ofNat,
437-
Int.natAbs_of_nonneg (Rat.num_nonneg_iff_zero_le.2 q.prop)]
438-
exact Rat.num_div_den q
439-
#align nnrat.num_div_denom NNRat.num_div_den
440-
441-
/-- A recursor for nonnegative rationals in terms of numerators and denominators. -/
442-
protected def rec {α : ℚ≥0Sort*} (h : ∀ m n : ℕ, α (m / n)) (q : ℚ≥0) : α q := by
443-
rw [← num_div_den q]
444-
apply h
445-
#align nnrat.rec NNRat.rec
446-
447392
end NNRat
393+
394+
-- `NNRat` needs to be available in the definition of `Field`
395+
assert_not_exists Field

Mathlib/Data/NNRat/Lemmas.lean

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/-
2+
Copyright (c) 2022 Yaël Dillies, Bhavik Mehta. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Yaël Dillies, Bhavik Mehta
5+
-/
6+
import Mathlib.Algebra.Function.Indicator
7+
import Mathlib.Algebra.Order.Nonneg.Field
8+
import Mathlib.Data.NNRat.Defs
9+
import Mathlib.Data.Rat.Field
10+
11+
#align_import data.rat.nnrat from "leanprover-community/mathlib"@"b3f4f007a962e3787aa0f3b5c7942a1317f7d88e"
12+
13+
/-!
14+
# Field and action structures on the nonnegative rationals
15+
16+
This file provides additional results about `NNRat` that cannot live in earlier files due to import
17+
cycles.
18+
-/
19+
20+
open Function
21+
open scoped NNRat
22+
23+
-- The `LinearOrderedCommGroupWithZero` instance is a shortcut instance for performance
24+
deriving instance CanonicallyLinearOrderedSemifield, LinearOrderedCommGroupWithZero for NNRat
25+
26+
namespace NNRat
27+
variable {α : Type*} {p q : ℚ≥0}
28+
29+
instance instDenselyOrdered : DenselyOrdered ℚ≥0 := Nonneg.instDenselyOrdered
30+
31+
@[simp, norm_cast] lemma coe_inv (q : ℚ≥0) : ((q⁻¹ : ℚ≥0) : ℚ) = (q : ℚ)⁻¹ := rfl
32+
#align nnrat.coe_inv NNRat.coe_inv
33+
34+
@[simp, norm_cast] lemma coe_div (p q : ℚ≥0) : ((p / q : ℚ≥0) : ℚ) = p / q := rfl
35+
#align nnrat.coe_div NNRat.coe_div
36+
37+
/-- A `MulAction` over `ℚ` restricts to a `MulAction` over `ℚ≥0`. -/
38+
instance [MulAction ℚ α] : MulAction ℚ≥0 α :=
39+
MulAction.compHom α coeHom.toMonoidHom
40+
41+
/-- A `DistribMulAction` over `ℚ` restricts to a `DistribMulAction` over `ℚ≥0`. -/
42+
instance [AddCommMonoid α] [DistribMulAction ℚ α] : DistribMulAction ℚ≥0 α :=
43+
DistribMulAction.compHom α coeHom.toMonoidHom
44+
45+
@[simp, norm_cast]
46+
lemma coe_indicator (s : Set α) (f : α → ℚ≥0) (a : α) :
47+
((s.indicator f a : ℚ≥0) : ℚ) = s.indicator (fun x ↦ ↑(f x)) a :=
48+
(coeHom : ℚ≥0 →+ ℚ).map_indicator _ _ _
49+
#align nnrat.coe_indicator NNRat.coe_indicator
50+
51+
end NNRat
52+
53+
open NNRat
54+
55+
namespace Rat
56+
57+
variable {p q : ℚ}
58+
59+
lemma toNNRat_inv (q : ℚ) : toNNRat q⁻¹ = (toNNRat q)⁻¹ := by
60+
obtain hq | hq := le_total q 0
61+
· rw [toNNRat_eq_zero.mpr hq, inv_zero, toNNRat_eq_zero.mpr (inv_nonpos.mpr hq)]
62+
· nth_rw 1 [← Rat.coe_toNNRat q hq]
63+
rw [← coe_inv, toNNRat_coe]
64+
#align rat.to_nnrat_inv Rat.toNNRat_inv
65+
66+
lemma toNNRat_div (hp : 0 ≤ p) : toNNRat (p / q) = toNNRat p / toNNRat q := by
67+
rw [div_eq_mul_inv, div_eq_mul_inv, ← toNNRat_inv, ← toNNRat_mul hp]
68+
#align rat.to_nnrat_div Rat.toNNRat_div
69+
70+
lemma toNNRat_div' (hq : 0 ≤ q) : toNNRat (p / q) = toNNRat p / toNNRat q := by
71+
rw [div_eq_inv_mul, div_eq_inv_mul, toNNRat_mul (inv_nonneg.2 hq), toNNRat_inv]
72+
#align rat.to_nnrat_div' Rat.toNNRat_div'
73+
74+
end Rat
75+
76+
/-! ### Numerator and denominator -/
77+
78+
namespace NNRat
79+
80+
variable {p q : ℚ≥0}
81+
82+
@[simp]
83+
lemma num_div_den (q : ℚ≥0) : (q.num : ℚ≥0) / q.den = q := by
84+
ext : 1
85+
rw [coe_div, coe_natCast, coe_natCast, num, ← Int.cast_ofNat,
86+
Int.natAbs_of_nonneg (Rat.num_nonneg_iff_zero_le.2 q.prop)]
87+
exact Rat.num_div_den q
88+
#align nnrat.num_div_denom NNRat.num_div_den
89+
90+
/-- A recursor for nonnegative rationals in terms of numerators and denominators. -/
91+
protected def rec {α : ℚ≥0Sort*} (h : ∀ m n : ℕ, α (m / n)) (q : ℚ≥0) : α q := by
92+
rw [← num_div_den q]; apply h
93+
#align nnrat.rec NNRat.rec
94+
95+
end NNRat

Mathlib/Data/Rat/Cast/Defs.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Copyright (c) 2019 Johannes Hölzl. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Johannes Hölzl, Mario Carneiro
55
-/
6-
import Mathlib.Data.Rat.Basic
6+
import Mathlib.Data.Rat.Field
77
import Mathlib.Data.Rat.Lemmas
88

99
#align_import data.rat.cast from "leanprover-community/mathlib"@"acebd8d49928f6ed8920e502a6c90674e75bd441"

Mathlib/Data/Rat/Basic.lean renamed to Mathlib/Data/Rat/Field.lean

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Copyright (c) 2019 Johannes Hölzl. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Johannes Hölzl, Mario Carneiro
55
-/
6-
import Mathlib.Algebra.Field.Defs
7-
import Mathlib.Data.Rat.Defs
6+
import Mathlib.Algebra.Order.Field.Defs
7+
import Mathlib.Data.Rat.Order
88

99
#align_import data.rat.basic from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e"
1010

@@ -45,4 +45,6 @@ instance field : Field ℚ :=
4545
-- Extra instances to short-circuit type class resolution
4646
instance divisionRing : DivisionRing ℚ := by infer_instance
4747

48+
instance instLinearOrderedField : LinearOrderedField ℚ := { field, instLinearOrderedCommRing with }
49+
4850
end Rat

0 commit comments

Comments
 (0)