Skip to content

Commit 0865637

Browse files
committed
doc: Improve Field's fields docstrings (#11508)
Reduce the diff of #11203
1 parent 0817259 commit 0865637

File tree

1 file changed

+76
-75
lines changed

1 file changed

+76
-75
lines changed

Mathlib/Algebra/Field/Defs.lean

Lines changed: 76 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ Copyright (c) 2014 Robert Lewis. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Robert Lewis, Leonardo de Moura, Johannes Hölzl, Mario Carneiro
55
-/
6-
76
import Mathlib.Algebra.Ring.Defs
8-
import Std.Data.Rat.Basic
97
import Mathlib.Data.Rat.Init
108

119
#align_import algebra.field.defs from "leanprover-community/mathlib"@"2651125b48fc5c170ab1111afd0817c903b1fc6c"
@@ -47,61 +45,84 @@ a `GroupWithZero` lemma instead.
4745
field, division ring, skew field, skew-field, skewfield
4846
-/
4947

48+
-- `NeZero` should not be needed in the basic algebraic hierarchy.
49+
assert_not_exists NeZero
50+
51+
-- Check that we have not imported `Mathlib.Tactic.Common` yet.
52+
assert_not_exists Mathlib.Tactic.LibrarySearch.librarySearch
53+
54+
assert_not_exists MonoidHom
5055

5156
open Function Set
5257

5358
universe u
5459

5560
variable {α β K : Type*}
5661

57-
/-- The default definition of the coercion `(↑(a : ℚ) : K)` for a division ring `K`
58-
is defined as `(a / b : K) = (a : K) * (b : K)⁻¹`.
59-
Use `coe` instead of `Rat.castRec` for better definitional behaviour.
60-
-/
61-
def Rat.castRec [NatCast K] [IntCast K] [Mul K] [Inv K] : ℚ → K
62-
| ⟨a, b, _, _⟩ => ↑a * (↑b)⁻¹
62+
/-- The default definition of the coercion `ℚ → K` for a division ring `K`.
63+
64+
`↑q : K` is defined as `(q.num : K) * (q.den : K)⁻¹`.
65+
66+
Do not use this directly (instances of `DivisionRing` are allowed to override that default for
67+
better definitional properties). Instead, use the coercion. -/
68+
def Rat.castRec [NatCast K] [IntCast K] [Mul K] [Inv K] (q : ℚ) : K := q.num * (q.den : K)⁻¹
6369
#align rat.cast_rec Rat.castRec
6470

65-
/-- The default definition of the scalar multiplication `(a : ℚ) • (x : K)` for a division ring `K`
66-
is given by `a • x = (↑ a) * x`.
67-
Use `(a : ℚ) • (x : K)` instead of `qsmulRec` for better definitional behaviour.
68-
-/
71+
/-- The default definition of the scalar multiplication by `ℚ` on a division ring `K`.
72+
73+
`q • x` is defined as `↑q * x`.
74+
75+
Do not use directly (instances of `DivisionRing` are allowed to override that default for
76+
better definitional properties). Instead use the `•` notation. -/
6977
def qsmulRec (coe : ℚ → K) [Mul K] (a : ℚ) (x : K) : K :=
7078
coe a * x
7179
#align qsmul_rec qsmulRec
7280

73-
/-- A `DivisionSemiring` is a `Semiring` with multiplicative inverses for nonzero elements. -/
74-
class DivisionSemiring (α : Type*) extends Semiring α, GroupWithZero α
81+
/-- A `DivisionSemiring` is a `Semiring` with multiplicative inverses for nonzero elements.
82+
83+
An instance of `DivisionSemiring K` includes maps `nnratCast : ℚ≥0 → K` and `nnqsmul : ℚ≥0 → K → K`.
84+
Those two fields are needed to implement the `DivisionSemiring K → Algebra ℚ≥0 K` instance since we
85+
need to control the specific definitions for some special cases of `K` (in particular `K = ℚ≥0`
86+
itself). See also note [forgetful inheritance].
87+
88+
If the division semiring has positive characteristic `p`, our division by zero convention forces
89+
`nnratCast (1 / p) = 1 / 0 = 0`. -/
90+
class DivisionSemiring (α : Type*) extends Semiring α, GroupWithZero α where
7591
#align division_semiring DivisionSemiring
7692

7793
/-- A `DivisionRing` is a `Ring` with multiplicative inverses for nonzero elements.
7894
7995
An instance of `DivisionRing K` includes maps `ratCast : ℚ → K` and `qsmul : ℚ → K → K`.
80-
If the division ring has positive characteristic p, we define `ratCast (1 / p) = 1 / 0 = 0`
81-
for consistency with our division by zero convention.
82-
The fields `ratCast` and `qsmul` are needed to implement the
83-
`algebraRat [DivisionRing K] : Algebra ℚ K` instance, since we need to control the specific
84-
definitions for some special cases of `K` (in particular `K = ℚ` itself).
85-
See also Note [forgetful inheritance].
86-
-/
87-
class DivisionRing (K : Type u) extends Ring K, DivInvMonoid K, Nontrivial K, RatCast K where
96+
Those two fields are needed to implement the `DivisionRing K → Algebra ℚ K` instance since we need
97+
to control the specific definitions for some special cases of `K` (in particular `K = ℚ` itself).
98+
See also note [forgetful inheritance]. Similarly, there are maps `nnratCast ℚ≥0 → K` and
99+
`nnqsmul : ℚ≥0 → K → K` to implement the `DivisionSemiring K → Algebra ℚ≥0 K` instance.
100+
101+
If the division ring has positive characteristic `p`, our division by zero convention forces
102+
`ratCast (1 / p) = 1 / 0 = 0`. -/
103+
class DivisionRing (α : Type*)
104+
extends Ring α, DivInvMonoid α, Nontrivial α, RatCast α where
88105
/-- For a nonzero `a`, `a⁻¹` is a right multiplicative inverse. -/
89-
protected mul_inv_cancel : ∀ (a : K), a ≠ 0 → a * a⁻¹ = 1
90-
/-- We define the inverse of `0` to be `0`. -/
91-
protected inv_zero : (0 : K)⁻¹ = 0
106+
protected mul_inv_cancel : ∀ (a : α), a ≠ 0 → a * a⁻¹ = 1
107+
/-- The inverse of `0` is `0` by convention. -/
108+
protected inv_zero : (0 : α)⁻¹ = 0
92109
protected ratCast := Rat.castRec
93-
/-- However `ratCast` is defined, propositionally it must be equal to `a * b⁻¹`. -/
94-
protected ratCast_mk : ∀ (a : ℤ) (b : ℕ) (h1 h2), Rat.cast ⟨a, b, h1, h2⟩ = a * (b : K)⁻¹ := by
95-
intros
96-
rfl
97-
/-- Multiplication by a rational number.
98-
Set this to `qsmulRec _` unless there is a risk of a `Module ℚ _` instance diamond. -/
99-
protected qsmul : ℚ → K → K
100-
/-- However `qsmul` is defined,
101-
propositionally it must be equal to multiplication by `ratCast`. -/
102-
protected qsmul_eq_mul' : ∀ (a : ℚ) (x : K), qsmul a x = Rat.cast a * x := by
110+
/-- However `Rat.cast` is defined, it must be propositionally equal to `a * b⁻¹`.
111+
112+
Do not use this lemma directly. Use `Rat.cast_def` instead. -/
113+
protected ratCast_mk : ∀ (a : ℤ) (b : ℕ) (h1 h2), Rat.cast ⟨a, b, h1, h2⟩ = a * (b : α)⁻¹ := by
103114
intros
104115
rfl
116+
/-- Scalar multiplication by a rational number.
117+
118+
Set this to `qsmulRec _` unless there is a risk of a `Module ℚ _` instance diamond.
119+
120+
Do not use directly. Instead use the `•` notation. -/
121+
protected qsmul : ℚ → α → α
122+
/-- However `qsmul` is defined, it must be propositionally equal to multiplication by `Rat.cast`.
123+
124+
Do not use this lemma directly. Use `Rat.cast_def` instead. -/
125+
protected qsmul_eq_mul' (a : ℚ) (x : α) : qsmul a x = Rat.cast a * x := by intros; rfl
105126
#align division_ring DivisionRing
106127
#align division_ring.rat_cast_mk DivisionRing.ratCast_mk
107128

@@ -110,28 +131,36 @@ instance (priority := 100) DivisionRing.toDivisionSemiring [DivisionRing α] : D
110131
{ ‹DivisionRing α› with }
111132
#align division_ring.to_division_semiring DivisionRing.toDivisionSemiring
112133

113-
/-- A `Semifield` is a `CommSemiring` with multiplicative inverses for nonzero elements. -/
134+
/-- A `Semifield` is a `CommSemiring` with multiplicative inverses for nonzero elements.
135+
136+
An instance of `Semifield K` includes maps `nnratCast : ℚ≥0 → K` and `nnqsmul : ℚ≥0 → K → K`.
137+
Those two fields are needed to implement the `DivisionSemiring K → Algebra ℚ≥0 K` instance since we
138+
need to control the specific definitions for some special cases of `K` (in particular `K = ℚ≥0`
139+
itself). See also note [forgetful inheritance].
140+
141+
If the semifield has positive characteristic `p`, our division by zero convention forces
142+
`nnratCast (1 / p) = 1 / 0 = 0`. -/
114143
class Semifield (α : Type*) extends CommSemiring α, DivisionSemiring α, CommGroupWithZero α
115144
#align semifield Semifield
116145

117146
/-- A `Field` is a `CommRing` with multiplicative inverses for nonzero elements.
118147
119148
An instance of `Field K` includes maps `ratCast : ℚ → K` and `qsmul : ℚ → K → K`.
120-
If the field has positive characteristic p, we define `ratCast (1 / p) = 1 / 0 = 0`
121-
for consistency with our division by zero convention.
122-
The fields `ratCast` and `qsmul` are needed to implement the
123-
`algebraRat [DivisionRing K] : Algebra ℚ K` instance, since we need to control the specific
124-
definitions for some special cases of `K` (in particular `K = ℚ` itself).
125-
See also Note [forgetful inheritance].
126-
-/
149+
Those two fields are needed to implement the `DivisionRing K → Algebra ℚ K` instance since we need
150+
to control the specific definitions for some special cases of `K` (in particular `K = ℚ` itself).
151+
See also note [forgetful inheritance].
152+
153+
If the field has positive characteristic `p`, our division by zero convention forces
154+
`ratCast (1 / p) = 1 / 0 = 0`. -/
127155
class Field (K : Type u) extends CommRing K, DivisionRing K
128156
#align field Field
129157

130-
section DivisionRing
131-
132-
variable [DivisionRing K] {a b : K}
158+
-- see Note [lower instance priority]
159+
instance (priority := 100) Field.toSemifield [Field α] : Semifield α := { ‹Field α› with }
160+
#align field.to_semifield Field.toSemifield
133161

134162
namespace Rat
163+
variable [DivisionRing K] {a b : K}
135164

136165
theorem cast_mk' (a b h1 h2) : ((⟨a, b, h1, h2⟩ : ℚ) : K) = a * (b : K)⁻¹ :=
137166
DivisionRing.ratCast_mk _ _ _ _
@@ -145,8 +174,7 @@ instance (priority := 100) smulDivisionRing : SMul ℚ K :=
145174
⟨DivisionRing.qsmul⟩
146175
#align rat.smul_division_ring Rat.smulDivisionRing
147176

148-
theorem smul_def (a : ℚ) (x : K) : a • x = ↑a * x :=
149-
DivisionRing.qsmul_eq_mul' a x
177+
theorem smul_def (a : ℚ) (x : K) : a • x = ↑a * x := DivisionRing.qsmul_eq_mul' a x
150178
#align rat.smul_def Rat.smul_def
151179

152180
@[simp]
@@ -156,32 +184,5 @@ theorem smul_one_eq_coe (A : Type*) [DivisionRing A] (m : ℚ) : m • (1 : A) =
156184

157185
end Rat
158186

159-
end DivisionRing
160-
161-
section OfScientific
162-
163187
instance RatCast.toOfScientific [RatCast K] : OfScientific K where
164188
ofScientific (m : ℕ) (b : Bool) (d : ℕ) := Rat.ofScientific m b d
165-
166-
end OfScientific
167-
168-
section Field
169-
170-
variable [Field K]
171-
172-
-- see Note [lower instance priority]
173-
instance (priority := 100) Field.toSemifield : Semifield K :=
174-
{ ‹Field K› with }
175-
#align field.to_semifield Field.toSemifield
176-
177-
end Field
178-
179-
/-
180-
`NeZero` should not be needed in the basic algebraic hierarchy.
181-
-/
182-
assert_not_exists NeZero
183-
184-
/-
185-
Check that we have not imported `Mathlib.Tactic.Common` yet.
186-
-/
187-
assert_not_exists Mathlib.Tactic.LibrarySearch.librarySearch

0 commit comments

Comments
 (0)