Skip to content

Commit ee3a540

Browse files
committed
chore: golf using grind (and add four supporting grind annotations) (leanprover-community#34154)
The goal of this golfing PR is to decrease the number of times lemmas are called explicitly (replacing calls to lemmas with calls to tactics). Any decrease in compilation time is a welcome side effect, although it is not a primary objective. Trace profiling results (shown if ≥10 ms before or after): * `Orientation.nonneg_inner_and_areaForm_eq_zero_iff_sameRay`: 311 ms before, 284 ms after 🎉 * `UV.compression_self`: 29 ms before, 19 ms after 🎉 * `IsSepClosed.exists_root_C_mul_X_pow_add_C_mul_X_add_C`: 405 ms before, 291 ms after 🎉 * `InnerProductGeometry.norm_add_eq_add_norm_iff_angle_eq_zero`: 425 ms before, 309 ms after 🎉 This golfing PR is batched under the following guidelines: * Up to ~5 changed files per PR * Up to ~25 changed declarations per PR * Up to ~100 changed lines per PR
1 parent bfc240d commit ee3a540

6 files changed

Lines changed: 13 additions & 35 deletions

File tree

Mathlib/Algebra/Polynomial/Degree/Defs.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ theorem degree_C_mul_X_pow_le (n : ℕ) (a : R) : degree (C a * X ^ n) ≤ n :=
210210
theorem degree_C_mul_X_le (a : R) : degree (C a * X) ≤ 1 := by
211211
simpa only [pow_one] using degree_C_mul_X_pow_le 1 a
212212

213-
@[simp]
213+
@[simp, grind =]
214214
theorem natDegree_C_mul_X_pow (n : ℕ) (a : R) (ha : a ≠ 0) : natDegree (C a * X ^ n) = n :=
215215
natDegree_eq_of_degree_eq_some (degree_C_mul_X_pow n ha)
216216

217-
@[simp]
217+
@[simp, grind =]
218218
theorem natDegree_C_mul_X (a : R) (ha : a ≠ 0) : natDegree (C a * X) = 1 := by
219219
simpa only [pow_one] using natDegree_C_mul_X_pow 1 a ha
220220

Mathlib/Algebra/Polynomial/Degree/Operations.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ theorem natDegree_add_eq_right_of_natDegree_lt (h : natDegree p < natDegree q) :
200200
theorem degree_add_C (hp : 0 < degree p) : degree (p + C a) = degree p :=
201201
add_comm (C a) p ▸ degree_add_eq_right_of_degree_lt <| lt_of_le_of_lt degree_C_le hp
202202

203-
@[simp] theorem natDegree_add_C {a : R} : (p + C a).natDegree = p.natDegree := by
203+
@[simp, grind =] theorem natDegree_add_C {a : R} : (p + C a).natDegree = p.natDegree := by
204204
rcases eq_or_ne p 0 with rfl | hp
205205
· simp
206206
by_cases! hpd : p.degree ≤ 0

Mathlib/Analysis/InnerProductSpace/TwoDim.lean

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,7 @@ theorem nonneg_inner_and_areaForm_eq_zero_iff_sameRay (x y : E) :
392392
mul_zero, add_zero, zero_add, neg_zero, inner_rightAngleRotation_right,
393393
real_inner_self_eq_norm_sq]
394394
exact this
395-
rintro ⟨ha, hb⟩
396-
have hx' : 0 < ‖x‖ := by simpa using hx
397-
have ha' : 0 ≤ a := nonneg_of_mul_nonneg_left ha (by positivity)
398-
have hb' : b = 0 := eq_zero_of_ne_zero_of_mul_right_eq_zero (pow_ne_zero 2 hx'.ne') hb
399-
exact (SameRay.sameRay_nonneg_smul_right x ha').add_right <| by simp [hb']
395+
simp_all
400396
· intro h
401397
obtain ⟨r, hr, rfl⟩ := h.exists_nonneg_left hx
402398
simp only [inner_smul_right, real_inner_self_eq_norm_sq, map_smulₛₗ, areaForm_apply_self,

Mathlib/Combinatorics/SetFamily/Compression/UV.lean

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ theorem compress_of_disjoint_of_le' (hva : Disjoint v a) (hua : u ≤ a) :
9292
(le_sdiff.2 ⟨(le_sup_right : v ≤ a ⊔ v), hva.mono_right hua⟩),
9393
sdiff_sup_cancel (le_sup_of_le_left hua), hva.symm.sup_sdiff_cancel_right]
9494

95-
@[simp]
95+
@[simp, grind =]
9696
theorem compress_self (u a : α) : compress u u a = a := by
9797
unfold compress
9898
split_ifs with h
@@ -153,14 +153,7 @@ protected theorem IsCompressed.eq (h : IsCompressed u v s) : 𝓒 u v s = s := h
153153

154154
@[simp]
155155
theorem compression_self (u : α) (s : Finset α) : 𝓒 u u s = s := by
156-
unfold compression
157-
convert union_empty s
158-
· ext a
159-
rw [mem_filter, compress_self, and_self_iff]
160-
· refine eq_empty_of_forall_notMem fun a ha ↦ ?_
161-
simp_rw [mem_filter, mem_image, compress_self] at ha
162-
obtain ⟨⟨b, hb, rfl⟩, hb'⟩ := ha
163-
exact hb' hb
156+
grind [mem_compression]
164157

165158
/-- Any family is compressed along two identical elements. -/
166159
theorem isCompressed_self (u : α) (s : Finset α) : IsCompressed u u s := compression_self u s

Mathlib/FieldTheory/IsSepClosed.lean

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,12 @@ theorem exists_root_C_mul_X_pow_add_C_mul_X_add_C
104104
[IsSepClosed k] {n : ℕ} (a b c : k) (hn : (n : k) = 0) (hn' : 2 ≤ n) (hb : b ≠ 0) :
105105
∃ x, a * x ^ n + b * x + c = 0 := by
106106
let f : k[X] := C a * X ^ n + C b * X + C c
107-
have hdeg : f.degree ≠ 0 := degree_ne_of_natDegree_ne <| by
107+
-- Specify `n := 0` below, otherwise Lean unfolds `0` to `Zero.zero`.
108+
have hdeg : f.degree ≠ 0 := degree_ne_of_natDegree_ne (n := 0) <| by
109+
have : C 0 * X ^ n + C b * X = 0 * X ^ n + C b * X := by grind
108110
by_cases ha : a = 0
109-
· suffices f.natDegree = 1 from this ▸ one_ne_zero
110-
simp_rw [f, ha, map_zero, zero_mul, zero_add]
111-
compute_degree!
112-
· suffices f.natDegree = n from this ▸ (lt_of_lt_of_le zero_lt_two hn').ne'
113-
simp_rw [f]
114-
have h0 : n ≠ 0 := by linarith only [hn']
115-
have h1 : n ≠ 1 := by linarith only [hn']
116-
have : 1 ≤ n := le_trans one_le_two hn'
117-
compute_degree!
118-
simp [h0, h1, ha]
111+
· grind [zero_add]
112+
· grind [natDegree_add_eq_left_of_natDegree_lt]
119113
have hsep : f.Separable := separable_C_mul_X_pow_add_C_mul_X_add_C a b c hn hb.isUnit
120114
obtain ⟨x, hx⟩ := exists_root f hdeg hsep
121115
exact ⟨x, by simpa [f] using hx⟩

Mathlib/Geometry/Euclidean/Angle/Unoriented/Basic.lean

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,8 @@ theorem norm_sub_eq_add_norm_iff_angle_eq_pi {x y : V} (hx : x ≠ 0) (hy : y
276276
if and only the angle between the two vectors is 0. -/
277277
theorem norm_add_eq_add_norm_iff_angle_eq_zero {x y : V} (hx : x ≠ 0) (hy : y ≠ 0) :
278278
‖x + y‖ = ‖x‖ + ‖y‖ ↔ angle x y = 0 := by
279-
refine ⟨fun h => ?_, norm_add_eq_add_norm_of_angle_eq_zero⟩
280-
rw [← inner_eq_mul_norm_iff_angle_eq_zero hx hy]
281-
obtain ⟨hxy₁, hxy₂⟩ := norm_nonneg (x + y), add_nonneg (norm_nonneg x) (norm_nonneg y)
282-
rw [← sq_eq_sq₀ hxy₁ hxy₂, norm_add_pow_two_real] at h
283-
calc
284-
⟪x, y⟫ = ((‖x‖ + ‖y‖) ^ 2 - ‖x‖ ^ 2 - ‖y‖ ^ 2) / 2 := by linarith
285-
_ = ‖x‖ * ‖y‖ := by ring
279+
refine ⟨?_, norm_add_eq_add_norm_of_angle_eq_zero⟩
280+
grind [inner_eq_mul_norm_iff_angle_eq_zero hx hy, norm_add_pow_two_real]
286281

287282
/-- The norm of the difference of two non-zero vectors equals the absolute value
288283
of the difference of their norms if and only the angle between the two vectors is 0. -/

0 commit comments

Comments
 (0)