Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 4a02fd3

Browse files
committed
refactor(algebra/order/ring,data/complex): redefine ordered_comm_ring and complex order (#9420)
* `ordered_comm_ring` no longer extends `ordered_comm_semiring`. We add an instance `ordered_comm_ring.to_ordered_comm_semiring` instead. * redefine complex order in terms of `re` and `im` instead of existence of a nonnegative real number. * simplify `has_star.star` on `complex` to `conj`; * rename `complex.complex_partial_order` etc to `complex.partial_order` etc, make them protected.
1 parent 06610c7 commit 4a02fd3

File tree

3 files changed

+52
-174
lines changed

3 files changed

+52
-174
lines changed

src/algebra/order/ring.lean

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,13 @@ section ordered_comm_ring
962962
/-- An `ordered_comm_ring α` is a commutative ring `α` with a partial order such that
963963
addition is monotone and multiplication by a positive number is strictly monotone. -/
964964
@[protect_proj]
965-
class ordered_comm_ring (α : Type u) extends ordered_ring α, ordered_comm_semiring α, comm_ring α
965+
class ordered_comm_ring (α : Type u) extends ordered_ring α, comm_ring α
966+
967+
@[priority 100] -- See note [lower instance priority]
968+
instance ordered_comm_ring.to_ordered_comm_semiring {α : Type u} [ordered_comm_ring α] :
969+
ordered_comm_semiring α :=
970+
{ .. (by apply_instance : ordered_semiring α),
971+
.. ‹ordered_comm_ring α› }
966972

967973
/-- Pullback an `ordered_comm_ring` under an injective map.
968974
See note [reducible non-instances]. -/
@@ -973,8 +979,7 @@ def function.injective.ordered_comm_ring [ordered_comm_ring α] {β : Type*}
973979
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
974980
(neg : ∀ x, f (- x) = - f x) (sub : ∀ x y, f (x - y) = f x - f y) :
975981
ordered_comm_ring β :=
976-
{ ..hf.ordered_comm_semiring f zero one add mul,
977-
..hf.ordered_ring f zero one add mul neg sub,
982+
{ ..hf.ordered_ring f zero one add mul neg sub,
978983
..hf.comm_ring f zero one add mul neg sub }
979984

980985
end ordered_comm_ring
@@ -1223,17 +1228,7 @@ class linear_ordered_comm_ring (α : Type u) extends linear_ordered_ring α, com
12231228
@[priority 100] -- see Note [lower instance priority]
12241229
instance linear_ordered_comm_ring.to_ordered_comm_ring [d : linear_ordered_comm_ring α] :
12251230
ordered_comm_ring α :=
1226-
-- One might hope that `{ ..linear_ordered_ring.to_linear_ordered_semiring, ..d }`
1227-
-- achieved the same result here.
1228-
-- Unfortunately with that definition we see mismatched instances in `algebra.star.chsh`.
1229-
let s : linear_ordered_semiring α := @linear_ordered_ring.to_linear_ordered_semiring α _ in
1230-
{ zero_mul := @linear_ordered_semiring.zero_mul α s,
1231-
mul_zero := @linear_ordered_semiring.mul_zero α s,
1232-
add_left_cancel := @linear_ordered_semiring.add_left_cancel α s,
1233-
le_of_add_le_add_left := @linear_ordered_semiring.le_of_add_le_add_left α s,
1234-
mul_lt_mul_of_pos_left := @linear_ordered_semiring.mul_lt_mul_of_pos_left α s,
1235-
mul_lt_mul_of_pos_right := @linear_ordered_semiring.mul_lt_mul_of_pos_right α s,
1236-
..d }
1231+
{ ..d }
12371232

12381233
@[priority 100] -- see Note [lower instance priority]
12391234
instance linear_ordered_comm_ring.to_integral_domain [s : linear_ordered_comm_ring α] :
@@ -1243,18 +1238,7 @@ instance linear_ordered_comm_ring.to_integral_domain [s : linear_ordered_comm_ri
12431238
@[priority 100] -- see Note [lower instance priority]
12441239
instance linear_ordered_comm_ring.to_linear_ordered_semiring [d : linear_ordered_comm_ring α] :
12451240
linear_ordered_semiring α :=
1246-
-- One might hope that `{ ..linear_ordered_ring.to_linear_ordered_semiring, ..d }`
1247-
-- achieved the same result here.
1248-
-- Unfortunately with that definition we see mismatched `preorder ℝ` instances in
1249-
-- `topology.metric_space.basic`.
1250-
let s : linear_ordered_semiring α := @linear_ordered_ring.to_linear_ordered_semiring α _ in
1251-
{ zero_mul := @linear_ordered_semiring.zero_mul α s,
1252-
mul_zero := @linear_ordered_semiring.mul_zero α s,
1253-
add_left_cancel := @linear_ordered_semiring.add_left_cancel α s,
1254-
le_of_add_le_add_left := @linear_ordered_semiring.le_of_add_le_add_left α s,
1255-
mul_lt_mul_of_pos_left := @linear_ordered_semiring.mul_lt_mul_of_pos_left α s,
1256-
mul_lt_mul_of_pos_right := @linear_ordered_semiring.mul_lt_mul_of_pos_right α s,
1257-
..d }
1241+
{ .. d, ..linear_ordered_ring.to_linear_ordered_semiring }
12581242

12591243
section linear_ordered_comm_ring
12601244

src/data/complex/basic.lean

Lines changed: 38 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,12 @@ lemma eq_conj_iff_im {z : ℂ} : conj z = z ↔ z.im = 0 :=
233233
λ h, ext rfl (neg_eq_iff_add_eq_zero.mpr (add_self_eq_zero.mpr h))⟩
234234

235235
instance : star_ring ℂ :=
236-
{ star := λ z, conj z,
237-
star_involutive := λ z, by simp,
238-
star_mul := λ r s, by { ext; simp [mul_comm], },
239-
star_add := by simp, }
236+
{ star := (conj : ℂ → ℂ),
237+
star_involutive := conj_conj,
238+
star_mul := λ a b, (conj.map_mul a b).trans (mul_comm _ _),
239+
star_add := conj.map_add }
240+
241+
@[simp] lemma star_def : (has_star.star : ℂ → ℂ) = conj := rfl
240242

241243
/-! ### Norm squared -/
242244

@@ -533,126 +535,45 @@ by rw [abs, sq, real.mul_self_sqrt (norm_sq_nonneg _)]
533535
We put a partial order on ℂ so that `z ≤ w` exactly if `w - z` is real and nonnegative.
534536
Complex numbers with different imaginary parts are incomparable.
535537
-/
536-
def complex_order : partial_order ℂ :=
537-
{ le := λ z w, ∃ x : ℝ, 0 ≤ x ∧ w = z + x,
538-
le_refl := λ x, ⟨0, by simp⟩,
539-
le_trans := λ x y z h₁ h₂,
540-
begin
541-
obtain ⟨w₁, l₁, rfl⟩ := h₁,
542-
obtain ⟨w₂, l₂, rfl⟩ := h₂,
543-
refine ⟨w₁ + w₂, _, _⟩,
544-
{ linarith, },
545-
{ simp [add_assoc], },
546-
end,
547-
le_antisymm := λ z w h₁ h₂,
548-
begin
549-
obtain ⟨w₁, l₁, rfl⟩ := h₁,
550-
obtain ⟨w₂, l₂, e⟩ := h₂,
551-
have h₃ : w₁ + w₂ = 0,
552-
{ symmetry,
553-
rw add_assoc at e,
554-
apply of_real_inj.mp,
555-
apply add_left_cancel,
556-
convert e; simp, },
557-
have h₄ : w₁ = 0, linarith,
558-
simp [h₄],
559-
end, }
560-
561-
localized "attribute [instance] complex_order" in complex_order
538+
protected def partial_order : partial_order ℂ :=
539+
{ le := λ z w, z.re ≤ w.re ∧ z.im = w.im,
540+
lt := λ z w, z.re < w.re ∧ z.im = w.im,
541+
lt_iff_le_not_le := λ z w, by { dsimp, rw lt_iff_le_not_le, tauto },
542+
le_refl := λ x, ⟨le_rfl, rfl⟩,
543+
le_trans := λ x y z h₁ h₂, ⟨h₁.1.trans h₂.1, h₁.2.trans h₂.2⟩,
544+
le_antisymm := λ z w h₁ h₂, ext (h₁.1.antisymm h₂.1) h₁.2 }
562545

563546
section complex_order
564-
open_locale complex_order
565547

566-
lemma le_def {z w : ℂ} : z ≤ w ↔ ∃ x : ℝ, 0 ≤ x ∧ w = z + x := iff.refl _
567-
lemma lt_def {z w : ℂ} : z < w ↔ ∃ x : ℝ, 0 < x ∧ w = z + x :=
568-
begin
569-
rw [lt_iff_le_not_le],
570-
fsplit,
571-
{ rintro ⟨⟨x, l, rfl⟩, h⟩,
572-
by_cases hx : x = 0,
573-
{ simpa [hx] using h },
574-
{ replace l : 0 < x := l.lt_of_ne (ne.symm hx),
575-
exact ⟨x, l, rfl⟩, } },
576-
{ rintro ⟨x, l, rfl⟩,
577-
fsplit,
578-
{ exact ⟨x, l.le, rfl⟩, },
579-
{ rintro ⟨x', l', e⟩,
580-
rw [add_assoc] at e,
581-
replace e := add_left_cancel (by { convert e, simp }),
582-
norm_cast at e,
583-
linarith, } }
584-
end
548+
localized "attribute [instance] complex.partial_order" in complex_order
549+
550+
lemma le_def {z w : ℂ} : z ≤ w ↔ z.re ≤ w.re ∧ z.im = w.im := iff.rfl
551+
lemma lt_def {z w : ℂ} : z < w ↔ z.re < w.re ∧ z.im = w.im := iff.rfl
552+
553+
@[simp, norm_cast] lemma real_le_real {x y : ℝ} : (x : ℂ) ≤ (y : ℂ) ↔ x ≤ y := by simp [le_def]
554+
555+
@[simp, norm_cast] lemma real_lt_real {x y : ℝ} : (x : ℂ) < (y : ℂ) ↔ x < y := by simp [lt_def]
585556

586-
@[simp, norm_cast] lemma real_le_real {x y : ℝ} : (x : ℂ) ≤ (y : ℂ) ↔ x ≤ y :=
587-
begin
588-
rw [le_def],
589-
fsplit,
590-
{ rintro ⟨r, l, e⟩,
591-
norm_cast at e,
592-
subst e,
593-
exact le_add_of_nonneg_right l, },
594-
{ intro h,
595-
exact ⟨y - x, sub_nonneg.mpr h, (by simp)⟩, },
596-
end
597-
@[simp, norm_cast] lemma real_lt_real {x y : ℝ} : (x : ℂ) < (y : ℂ) ↔ x < y :=
598-
begin
599-
rw [lt_def],
600-
fsplit,
601-
{ rintro ⟨r, l, e⟩,
602-
norm_cast at e,
603-
subst e,
604-
exact lt_add_of_pos_right x l, },
605-
{ intro h,
606-
exact ⟨y - x, sub_pos.mpr h, (by simp)⟩, },
607-
end
608557
@[simp, norm_cast] lemma zero_le_real {x : ℝ} : (0 : ℂ) ≤ (x : ℂ) ↔ 0 ≤ x := real_le_real
609558
@[simp, norm_cast] lemma zero_lt_real {x : ℝ} : (0 : ℂ) < (x : ℂ) ↔ 0 < x := real_lt_real
610559

560+
lemma not_le_iff {z w : ℂ} : ¬(z ≤ w) ↔ w.re < z.re ∨ z.im ≠ w.im :=
561+
by rw [le_def, not_and_distrib, not_le]
562+
563+
lemma not_le_zero_iff {z : ℂ} : ¬z ≤ 00 < z.re ∨ z.im ≠ 0 := not_le_iff
564+
611565
/--
612566
With `z ≤ w` iff `w - z` is real and nonnegative, `ℂ` is an ordered ring.
613567
-/
614-
def complex_ordered_comm_ring : ordered_comm_ring ℂ :=
615-
{ zero_le_one := ⟨1, zero_le_one, by simp⟩,
616-
add_le_add_left := λ w z h y,
617-
begin
618-
obtain ⟨x, l, rfl⟩ := h,
619-
exact ⟨x, l, by simp [add_assoc]⟩,
620-
end,
568+
protected def ordered_comm_ring : ordered_comm_ring ℂ :=
569+
{ zero_le_one := ⟨zero_le_one, rfl⟩,
570+
add_le_add_left := λ w z h y, ⟨add_le_add_left h.1 _, congr_arg2 (+) rfl h.2⟩,
621571
mul_pos := λ z w hz hw,
622-
begin
623-
obtain ⟨zx, lz, rfl⟩ := lt_def.mp hz,
624-
obtain ⟨wx, lw, rfl⟩ := lt_def.mp hw,
625-
norm_cast,
626-
simp only [mul_pos, lz, lw, zero_add],
627-
end,
628-
le_of_add_le_add_left := λ u v z h,
629-
begin
630-
obtain ⟨x, l, e⟩ := h,
631-
rw add_assoc at e,
632-
exact ⟨x, l, add_left_cancel e⟩,
633-
end,
634-
mul_lt_mul_of_pos_left := λ u v z h₁ h₂,
635-
begin
636-
obtain ⟨x₁, l₁, rfl⟩ := lt_def.mp h₁,
637-
obtain ⟨x₂, l₂, rfl⟩ := lt_def.mp h₂,
638-
simp only [mul_add, zero_add],
639-
exact lt_def.mpr ⟨x₂ * x₁, mul_pos l₂ l₁, (by norm_cast)⟩,
640-
end,
641-
mul_lt_mul_of_pos_right := λ u v z h₁ h₂,
642-
begin
643-
obtain ⟨x₁, l₁, rfl⟩ := lt_def.mp h₁,
644-
obtain ⟨x₂, l₂, rfl⟩ := lt_def.mp h₂,
645-
simp only [add_mul, zero_add],
646-
exact lt_def.mpr ⟨x₁ * x₂, mul_pos l₁ l₂, (by norm_cast)⟩,
647-
end,
648-
-- we need more instances here because comm_ring doesn't have zero_add et al as fields,
649-
-- they are derived as lemmas
650-
..(by apply_instance : partial_order ℂ),
651-
..(by apply_instance : comm_ring ℂ),
652-
..(by apply_instance : comm_semiring ℂ),
653-
..(by apply_instance : add_cancel_monoid ℂ) }
654-
655-
localized "attribute [instance] complex_ordered_comm_ring" in complex_order
572+
by simp [lt_def, mul_re, mul_im, ← hz.2, ← hw.2, mul_pos hz.1 hw.1],
573+
.. complex.partial_order,
574+
.. complex.comm_ring }
575+
576+
localized "attribute [instance] complex.ordered_comm_ring" in complex_order
656577

657578
/--
658579
With `z ≤ w` iff `w - z` is real and nonnegative, `ℂ` is a star ordered ring.
@@ -662,16 +583,10 @@ In fact, the nonnegative elements are precisely those of this form.
662583
This hold in any `C^*`-algebra, e.g. `ℂ`,
663584
but we don't yet have `C^*`-algebras in mathlib.
664585
-/
665-
def complex_star_ordered_ring : star_ordered_ring ℂ :=
666-
{ star_mul_self_nonneg := λ z,
667-
begin
668-
refine ⟨z.abs^2, pow_nonneg (abs_nonneg z) 2, _⟩,
669-
simp only [has_star.star, of_real_pow, zero_add],
670-
norm_cast,
671-
rw [←norm_sq_eq_abs, norm_sq_eq_conj_mul_self],
672-
end, }
673-
674-
localized "attribute [instance] complex_star_ordered_ring" in complex_order
586+
protected def star_ordered_ring : star_ordered_ring ℂ :=
587+
{ star_mul_self_nonneg := λ z, ⟨by simp [add_nonneg, mul_self_nonneg], by simp [mul_comm]⟩ }
588+
589+
localized "attribute [instance] complex.star_ordered_ring" in complex_order
675590

676591
end complex_order
677592

src/data/complex/module.lean

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -102,33 +102,12 @@ end
102102
section
103103
open_locale complex_order
104104

105-
lemma complex_ordered_smul : ordered_smul ℝ ℂ :=
106-
{ smul_lt_smul_of_pos := λ z w x h₁ h₂,
107-
begin
108-
obtain ⟨y, l, rfl⟩ := lt_def.mp h₁,
109-
refine lt_def.mpr ⟨x * y, _, _⟩,
110-
exact mul_pos h₂ l,
111-
ext; simp [mul_add],
112-
end,
113-
lt_of_smul_lt_smul_of_pos := λ z w x h₁ h₂,
114-
begin
115-
obtain ⟨y, l, e⟩ := lt_def.mp h₁,
116-
by_cases h : x = 0,
117-
{ subst h, exfalso, apply lt_irrefl 0 h₂, },
118-
{ refine lt_def.mpr ⟨y / x, div_pos l h₂, _⟩,
119-
replace e := congr_arg (λ z, (x⁻¹ : ℂ) * z) e,
120-
simp only [mul_add, ←mul_assoc, h, one_mul, of_real_eq_zero, real_smul, ne.def,
121-
not_false_iff, inv_mul_cancel] at e,
122-
convert e,
123-
simp only [div_eq_iff_mul_eq, h, of_real_eq_zero, of_real_div, ne.def, not_false_iff],
124-
norm_cast,
125-
simp [mul_comm _ y, mul_assoc, h] },
126-
end }
127-
128-
localized "attribute [instance] complex_ordered_smul" in complex_order
105+
protected lemma ordered_smul : ordered_smul ℝ ℂ :=
106+
ordered_smul.mk' $ λ a b r hab hr, ⟨by simp [hr, hab.1.le], by simp [hab.2]⟩
129107

130-
end
108+
localized "attribute [instance] complex.ordered_smul" in complex_order
131109

110+
end
132111

133112
open submodule finite_dimensional
134113

0 commit comments

Comments
 (0)