Skip to content

Commit 95b694b

Browse files
committed
chore(NumberTheory/Zsqrtd): inline duplicated order lemmas into instances (leanprover-community#35481)
This PR follows reviewer feedback by removing duplicated order/additive wrapper theorems in `Zsqrtd` and inlining the proofs into the corresponding typeclass instances. Changes include: - remove redundant wrappers such as `Zsqrtd.le_total`, `Zsqrtd.add_le_add_left`, `Zsqrtd.le_of_add_le_add_left`, `Zsqrtd.add_lt_add_left`, and `Zsqrtd.le_antisymm` - keep the canonical API through `LinearOrder` / ordered additive structure fields - update downstream use in `PellMatiyasevic` to the canonical theorem (`_root_.le_antisymm`) This reduces namespace-level duplicate APIs and keeps proofs aligned with standard typeclass lemmas. Co-authored-by: Frankie Wang <git@frankie.wang>
1 parent 9cc6a7c commit 95b694b

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

Mathlib/NumberTheory/PellMatiyasevic.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ set_option backward.privateInPublic.warn false in
267267
set_option linter.flexible false in
268268
theorem eq_pell_lem : ∀ (n) (b : ℤ√(d a1)), 1 ≤ b → IsPell b →
269269
b ≤ pellZd a1 n → ∃ n, b = pellZd a1 n
270-
| 0, _ => fun h1 _ hl => ⟨0, @Zsqrtd.le_antisymm _ (dnsq a1) _ _ hl h1⟩
270+
| 0, _ => fun h1 _ hl => ⟨0, le_antisymm hl h1⟩
271271
| n + 1, b => fun h1 hp h =>
272272
have a1p : (0 : ℤ√(d a1)) ≤ ⟨a, 1⟩ := trivial
273273
have am1p : (0 : ℤ√(d a1)) ≤ ⟨a, -1⟩ := show (_ : Nat) ≤ _ by simp; exact Nat.pred_le _

Mathlib/NumberTheory/Zsqrtd/Basic.lean

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -670,16 +670,22 @@ protected theorem nonneg_total : ∀ a : ℤ√d, Nonneg a ∨ Nonneg (-a)
670670
| ⟨(_ + 1 : ℕ), -[_+1]⟩ => Nat.le_total _ _
671671
| ⟨-[_+1], (_ + 1 : ℕ)⟩ => Nat.le_total _ _
672672

673+
@[deprecated _root_.le_total (since := "2026-02-19")]
673674
protected theorem le_total (a b : ℤ√d) : a ≤ b ∨ b ≤ a := by
674675
have t := (b - a).nonneg_total
675676
rwa [neg_sub] at t
676677

677678
instance preorder : Preorder (ℤ√d) where
678679
le_refl a := show Nonneg (a - a) by simp only [sub_self]; trivial
679680
le_trans a b c hab hbc := by simpa [sub_add_sub_cancel'] using hab.add hbc
680-
lt_iff_le_not_ge _ _ := (and_iff_right_of_imp (Zsqrtd.le_total _ _).resolve_left).symm
681+
lt_iff_le_not_ge a b := by
682+
have ht : b ≤ a ∨ a ≤ b := by
683+
have t := (a - b).nonneg_total
684+
rwa [neg_sub] at t
685+
exact (and_iff_right_of_imp ht.resolve_left).symm
681686

682687
open Int in
688+
-- TODO add an `Archimedean (ℤ√d)` instance and drop this lemma
683689
theorem le_arch (a : ℤ√d) : ∃ n : ℕ, a ≤ n := by
684690
obtain ⟨x, y, (h : a ≤ ⟨x, y⟩)⟩ : ∃ x y : ℕ, Nonneg (⟨x, y⟩ + -a) :=
685691
match -a with
@@ -697,14 +703,11 @@ theorem le_arch (a : ℤ√d) : ∃ n : ℕ, a ≤ n := by
697703
rw [show (x : ℤ) + d * Nat.succ y - x = d * Nat.succ y by simp]
698704
exact h (y + 1)
699705

706+
@[deprecated _root_.add_le_add_left (since := "2026-02-19")]
700707
protected theorem add_le_add_left (a b : ℤ√d) (ab : a ≤ b) (c : ℤ√d) : a + c ≤ b + c :=
701708
show Nonneg _ by rwa [add_sub_add_right_eq_sub]
702709

703-
protected theorem le_of_add_le_add_left (a b c : ℤ√d) (h : c + a ≤ c + b) : a ≤ b := by
704-
simpa using Zsqrtd.add_le_add_left _ _ h (-c)
705710

706-
protected theorem add_lt_add_left (a b : ℤ√d) (h : a < b) (c) : c + a < c + b := fun h' =>
707-
h (Zsqrtd.le_of_add_le_add_left _ _ _ h')
708711

709712
theorem nonneg_smul {a : ℤ√d} {n : ℕ} (ha : Nonneg a) : Nonneg ((n : ℤ√d) * a) := by
710713
rw [← Int.cast_natCast n]
@@ -833,13 +836,16 @@ theorem nonneg_antisymm : ∀ {a : ℤ√d}, Nonneg a → Nonneg (-a) → a = 0
833836
rw [one_mul] at t
834837
exact absurd t (not_divides_sq _ _)
835838

839+
@[deprecated _root_.le_antisymm (since := "2026-02-19")]
836840
theorem le_antisymm {a b : ℤ√d} (ab : a ≤ b) (ba : b ≤ a) : a = b :=
837841
eq_of_sub_eq_zero <| nonneg_antisymm ba (by rwa [neg_sub])
838842

839843
instance linearOrder : LinearOrder (ℤ√d) :=
840844
{ Zsqrtd.preorder with
841-
le_antisymm := fun _ _ => Zsqrtd.le_antisymm
842-
le_total := Zsqrtd.le_total
845+
le_antisymm := fun _ _ ab ba => eq_of_sub_eq_zero <| nonneg_antisymm ba (by rwa [neg_sub])
846+
le_total := fun a b => by
847+
have t := (b - a).nonneg_total
848+
rwa [neg_sub] at t
843849
toDecidableLE := Zsqrtd.decidableLE
844850
toDecidableEq := inferInstance }
845851

@@ -881,14 +887,22 @@ instance : IsDomain (ℤ√d) :=
881887
protected theorem mul_pos (a b : ℤ√d) (a0 : 0 < a) (b0 : 0 < b) : 0 < a * b := fun ab =>
882888
Or.elim
883889
(eq_zero_or_eq_zero_of_mul_eq_zero
884-
(le_antisymm ab (Zsqrtd.mul_nonneg _ _ (le_of_lt a0) (le_of_lt b0))))
890+
(_root_.le_antisymm ab (Zsqrtd.mul_nonneg _ _ (le_of_lt a0) (le_of_lt b0))))
885891
(fun e => ne_of_gt a0 e) fun e => ne_of_gt b0 e
886892

887893
instance : ZeroLEOneClass (ℤ√d) :=
888894
{ zero_le_one := by trivial }
889895

890896
instance : IsOrderedAddMonoid (ℤ√d) :=
891-
{ add_le_add_left := Zsqrtd.add_le_add_left }
897+
{ add_le_add_left := fun a b ab c => show Nonneg _ by rwa [add_sub_add_right_eq_sub] }
898+
899+
@[deprecated _root_.le_of_add_le_add_left (since := "2026-02-19")]
900+
protected theorem le_of_add_le_add_left (a b c : ℤ√d) (h : c + a ≤ c + b) : a ≤ b := by
901+
exact _root_.le_of_add_le_add_left h
902+
903+
@[deprecated _root_.add_lt_add_left (since := "2026-02-19")]
904+
protected theorem add_lt_add_left (a b : ℤ√d) (h : a < b) (c) : c + a < c + b := fun h' =>
905+
h (_root_.le_of_add_le_add_left h')
892906

893907
instance : IsStrictOrderedRing (ℤ√d) :=
894908
.of_mul_pos Zsqrtd.mul_pos

0 commit comments

Comments
 (0)