Skip to content

Commit 48de726

Browse files
committed
feat: norm_cast lemmas about IsSquare and {Int,Rat}.sqrt (#13788)
1 parent 0e39a68 commit 48de726

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

Mathlib/Algebra/Ring/Int.lean

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,16 @@ lemma two_mul_ediv_two_of_odd (h : Odd n) : 2 * (n / 2) = n - 1 :=
256256
eq_sub_of_add_eq (two_mul_ediv_two_add_one_of_odd h)
257257
#align int.two_mul_div_two_of_odd Int.two_mul_ediv_two_of_odd
258258

259+
@[norm_cast, simp]
260+
theorem isSquare_natCast_iff {n : ℕ} : IsSquare (n : ℤ) ↔ IsSquare n := by
261+
constructor <;> rintro ⟨x, h⟩
262+
· exact ⟨x.natAbs, (natAbs_mul_natAbs_eq h.symm).symm⟩
263+
· exact ⟨x, mod_cast h⟩
264+
265+
-- See note [no_index around OfNat.ofNat]
266+
@[simp]
267+
theorem isSquare_ofNat_iff {n : ℕ} :
268+
IsSquare (no_index (OfNat.ofNat n) : ℤ) ↔ IsSquare (OfNat.ofNat n : ℕ) :=
269+
isSquare_natCast_iff
270+
259271
end Int

Mathlib/Data/Int/Sqrt.lean

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,12 @@ theorem sqrt_nonneg (n : ℤ) : 0 ≤ sqrt n :=
3939
natCast_nonneg _
4040
#align int.sqrt_nonneg Int.sqrt_nonneg
4141

42+
@[simp, norm_cast]
43+
theorem sqrt_natCast (n : ℕ) : Int.sqrt (n : ℤ) = Nat.sqrt n := by rw [sqrt, toNat_ofNat]
44+
45+
-- See note [no_index around OfNat.ofNat]
46+
@[simp]
47+
theorem sqrt_ofNat (n : ℕ) : Int.sqrt (no_index (OfNat.ofNat n) : ℤ) = Nat.sqrt (OfNat.ofNat n) :=
48+
sqrt_natCast _
49+
4250
end Int

Mathlib/Data/Rat/Lemmas.lean

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,30 @@ theorem add_num_den (q r : ℚ) :
119119
rw [mul_comm r.num q.den]
120120
#align rat.add_num_denom Rat.add_num_den
121121

122+
123+
theorem isSquare_iff {q : ℚ} : IsSquare q ↔ IsSquare q.num ∧ IsSquare q.den := by
124+
constructor
125+
· rintro ⟨qr, rfl⟩
126+
rw [Rat.mul_self_num, mul_self_den]
127+
simp only [isSquare_mul_self, and_self]
128+
· rintro ⟨⟨nr, hnr⟩, ⟨dr, hdr⟩⟩
129+
refine ⟨nr / dr, ?_⟩
130+
rw [div_mul_div_comm, ← Int.cast_mul, ← Nat.cast_mul, ← hnr, ← hdr, num_div_den]
131+
132+
@[norm_cast, simp]
133+
theorem isSquare_natCast_iff {n : ℕ} : IsSquare (n : ℚ) ↔ IsSquare n := by
134+
simp_rw [isSquare_iff, num_natCast, den_natCast, isSquare_one, and_true, Int.isSquare_natCast_iff]
135+
136+
@[norm_cast, simp]
137+
theorem isSquare_intCast_iff {z : ℤ} : IsSquare (z : ℚ) ↔ IsSquare z := by
138+
simp_rw [isSquare_iff, intCast_num, intCast_den, isSquare_one, and_true]
139+
140+
-- See note [no_index around OfNat.ofNat]
141+
@[simp]
142+
theorem isSquare_ofNat_iff {n : ℕ} :
143+
IsSquare (no_index (OfNat.ofNat n) : ℚ) ↔ IsSquare (OfNat.ofNat n : ℕ) :=
144+
isSquare_natCast_iff
145+
122146
section Casts
123147

124148
theorem exists_eq_mul_div_num_and_eq_mul_div_den (n : ℤ) {d : ℤ} (d_ne_zero : d ≠ 0) :

Mathlib/Data/Rat/Sqrt.lean

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,17 @@ instance : DecidablePred (IsSquare : ℚ → Prop) :=
4343
fun m => decidable_of_iff' (sqrt m * sqrt m = m) <| by
4444
simp_rw [← exists_mul_self m, IsSquare, eq_comm]
4545

46+
@[simp, norm_cast]
47+
theorem sqrt_intCast (z : ℤ) : Rat.sqrt (z : ℚ) = Int.sqrt z := by
48+
simp only [sqrt, num_intCast, den_intCast, Nat.sqrt_one, mkRat_one]
49+
50+
@[simp, norm_cast]
51+
theorem sqrt_natCast (n : ℕ) : Rat.sqrt (n : ℚ) = Nat.sqrt n := by
52+
rw [← Int.cast_natCast, sqrt_intCast, Int.sqrt_natCast, Int.cast_natCast]
53+
54+
-- See note [no_index around OfNat.ofNat]
55+
@[simp]
56+
theorem sqrt_ofNat (n : ℕ) : Rat.sqrt (no_index (OfNat.ofNat n) : ℚ) = Nat.sqrt (OfNat.ofNat n) :=
57+
sqrt_natCast _
58+
4659
end Rat

0 commit comments

Comments
 (0)