Skip to content

Commit 262b886

Browse files
committed
chore: replace omega with lia where possible (#32666)
As previously discussed, we'd like to move users off `omega` and on to `lia` (which is the linear integer arithmetic module from `grind`, plus a controlled set of instantiated lemmas). This does not yet remove all uses of `omega` in Mathlib.
1 parent 798365a commit 262b886

File tree

229 files changed

+741
-737
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

229 files changed

+741
-737
lines changed

Archive/Imo/Imo1960Q1.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ theorem ge_100 {n : ℕ} (h1 : ProblemPredicate n) : 100 ≤ n := by
4545
rw [← h1.left]
4646
refine Nat.base_pow_length_digits_le 10 n ?_ (not_zero h1)
4747
simp
48-
omega
48+
lia
4949

5050
theorem lt_1000 {n : ℕ} (h1 : ProblemPredicate n) : n < 1000 := by
5151
have h2 : n < 10 ^ 3 := by
5252
rw [← h1.left]
5353
refine Nat.lt_base_pow_length_digits ?_
5454
simp
55-
omega
55+
lia
5656

5757
/-
5858
We do an exhaustive search to show that all results are covered by `SolutionPredicate`.
@@ -70,7 +70,7 @@ theorem searchUpTo_step {c n} (H : SearchUpTo c n) {c' n'} (ec : c + 1 = c') (en
7070
refine ⟨by ring, fun m l p => ?_⟩
7171
obtain ⟨h₁, ⟨m, rfl⟩, h₂⟩ := id p
7272
by_cases h : 11 * m < c * 11; · exact H _ h p
73-
obtain rfl : m = c := by omega
73+
obtain rfl : m = c := by lia
7474
rw [Nat.mul_div_cancel_left _ (by simp : 11 > 0), mul_comm] at h₂
7575
refine (H' h₂).imp ?_ ?_ <;> · rintro rfl; norm_num
7676

Archive/Imo/Imo1962Q1.lean

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,31 @@ Now we can eliminate possibilities for `(digits 10 c).length` until we get to th
5353
lemma case_0_digits {c n : ℕ} (hc : (digits 10 c).length = 0) : ¬ProblemPredicate' c n := by
5454
intro hpp
5555
have hpow : 6 * 10 ^ 0 = 6 * 10 ^ (digits 10 c).length := by rw [hc]
56-
omega
56+
lia
5757

5858
lemma case_1_digits {c n : ℕ} (hc : (digits 10 c).length = 1) : ¬ProblemPredicate' c n := by
5959
intro hpp
6060
have hpow : 6 * 10 ^ 1 = 6 * 10 ^ (digits 10 c).length := by rw [hc]
61-
omega
61+
lia
6262

6363
lemma case_2_digits {c n : ℕ} (hc : (digits 10 c).length = 2) : ¬ProblemPredicate' c n := by
6464
intro hpp
6565
have hpow : 6 * 10 ^ 2 = 6 * 10 ^ (digits 10 c).length := by rw [hc]
66-
omega
66+
lia
6767

6868
lemma case_3_digits {c n : ℕ} (hc : (digits 10 c).length = 3) : ¬ProblemPredicate' c n := by
6969
intro hpp
7070
have hpow : 6 * 10 ^ 3 = 6 * 10 ^ (digits 10 c).length := by rw [hc]
71-
omega
71+
lia
7272

7373
lemma case_4_digits {c n : ℕ} (hc : (digits 10 c).length = 4) : ¬ProblemPredicate' c n := by
7474
intro hpp
7575
have hpow : 6 * 10 ^ 4 = 6 * 10 ^ (digits 10 c).length := by rw [hc]
76-
omega
76+
lia
7777

7878
/-- Putting this inline causes a deep recursion error, so we separate it out. -/
7979
private lemma helper_5_digits {c : ℤ} (hc : 6 * 10 ^ 5 + c = 4 * (10 * c + 6)) : c = 15384 := by
80-
omega
80+
lia
8181

8282
lemma case_5_digits {c n : ℕ} (hc : (digits 10 c).length = 5) (hpp : ProblemPredicate' c n) :
8383
c = 15384 := by

Archive/Imo/Imo1985Q2.lean

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,22 @@ lemma C_mul_mod {n j : ℕ} (hn : 3 ≤ n) (hj : j ∈ Set.Ico 1 n) (cpj : Nat.C
5151
have nej : (k + 1) * j % n ≠ j := by
5252
by_contra! h; nth_rw 2 [← Nat.mod_eq_of_lt hj.2, ← one_mul j] at h
5353
replace h : (k + 1) % n = 1 % n := Nat.ModEq.cancel_right_of_coprime cpj h
54-
rw [Nat.mod_eq_of_lt hk.2, Nat.mod_eq_of_lt (by omega)] at h
55-
omega
54+
rw [Nat.mod_eq_of_lt hk.2, Nat.mod_eq_of_lt (by lia)] at h
55+
lia
5656
have b₁ : (k + 1) * j % n ∈ Set.Ico 1 n := by
57-
refine ⟨?_, Nat.mod_lt _ (by omega)⟩
57+
refine ⟨?_, Nat.mod_lt _ (by lia)⟩
5858
by_contra! h; rw [Nat.lt_one_iff, ← Nat.dvd_iff_mod_eq_zero] at h
5959
have ek := Nat.eq_zero_of_dvd_of_lt (cpj.dvd_of_dvd_mul_right h) hk.2
60-
omega
60+
lia
6161
rw [← ih ⟨hk₁, Nat.lt_of_succ_lt hk.2⟩, hC.2 _ b₁ nej]
6262
rcases nej.lt_or_gt with h | h
6363
· rw [Int.natAbs_natCast_sub_natCast_of_ge h.le]
6464
have b₂ : j - (k + 1) * j % n ∈ Set.Ico 1 n :=
6565
⟨Nat.sub_pos_iff_lt.mpr h, (Nat.sub_le ..).trans_lt hj.2
6666
have q : n - (j - (k + 1) * j % n) = (k + 1) * j % n + (n - j) % n := by
6767
rw [tsub_tsub_eq_add_tsub_of_le h.le, add_comm, Nat.add_sub_assoc hj.2.le,
68-
Nat.mod_eq_of_lt (show n - j < n by omega)]
69-
rw [hC.1 _ b₂, q, ← Nat.add_mod_of_add_mod_lt (by omega), ← Nat.add_sub_assoc hj.2.le,
68+
Nat.mod_eq_of_lt (show n - j < n by lia)]
69+
rw [hC.1 _ b₂, q, ← Nat.add_mod_of_add_mod_lt (by lia), ← Nat.add_sub_assoc hj.2.le,
7070
add_comm, Nat.add_sub_assoc (Nat.le_mul_of_pos_left _ hk.1), ← tsub_one_mul,
7171
Nat.add_mod_left, add_tsub_cancel_right]
7272
· rw [Int.natAbs_natCast_sub_natCast_of_le h.le, Nat.mod_sub_of_le h.le]
@@ -75,13 +75,13 @@ lemma C_mul_mod {n j : ℕ} (hn : 3 ≤ n) (hj : j ∈ Set.Ico 1 n) (cpj : Nat.C
7575
theorem result {n j : ℕ} (hn : 3 ≤ n) (hj : j ∈ Set.Ico 1 n) (cpj : Coprime n j)
7676
{C : ℕ → Fin 2} (hC : Condition n j C) {i : ℕ} (hi : i ∈ Set.Ico 1 n) :
7777
C i = C j := by
78-
obtain ⟨v, -, hv⟩ := exists_mul_mod_eq_one_of_coprime cpj.symm (by omega)
78+
obtain ⟨v, -, hv⟩ := exists_mul_mod_eq_one_of_coprime cpj.symm (by lia)
7979
have hvi : i = (v * i % n) * j % n := by
8080
rw [mod_mul_mod, ← mul_rotate, ← mod_mul_mod, hv, one_mul, mod_eq_of_lt hi.2]
8181
have vib : v * i % n ∈ Set.Ico 1 n := by
82-
refine ⟨(?_ : 0 < _), mod_lt _ (by omega)⟩
82+
refine ⟨(?_ : 0 < _), mod_lt _ (by lia)⟩
8383
by_contra! h; rw [le_zero, ← dvd_iff_mod_eq_zero] at h
84-
rw [mul_comm, ← mod_eq_of_lt (show 1 < n by omega)] at hv
84+
rw [mul_comm, ← mod_eq_of_lt (show 1 < n by lia)] at hv
8585
have i0 := eq_zero_of_dvd_of_lt
8686
((coprime_of_mul_modEq_one _ hv).symm.dvd_of_dvd_mul_left h) hi.2
8787
subst i; simp at hi

Archive/Imo/Imo1988Q6.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ theorem imo1988_q6 {a b : ℕ} (h : a * b + 1 ∣ a ^ 2 + b ^ 2) :
239239
· contrapose! hV₀ with x_lt_z
240240
apply ne_of_gt
241241
calc
242-
z * y > x * x := by apply mul_lt_mul' <;> omega
242+
z * y > x * x := by apply mul_lt_mul' <;> lia
243243
_ ≥ x * x - k := sub_le_self _ (Int.natCast_nonneg k)
244244
· -- There is no base case in this application of Vieta jumping.
245245
simp
@@ -276,16 +276,16 @@ example {a b : ℕ} (h : a * b ∣ a ^ 2 + b ^ 2 + 1) : 3 * a * b = a ^ 2 + b ^
276276
constructor
277277
· have zy_pos : z * y ≥ 0 := by rw [hV₀]; exact mod_cast Nat.zero_le _
278278
apply nonneg_of_mul_nonneg_left zy_pos
279-
omega
279+
lia
280280
· contrapose! hV₀ with x_lt_z
281281
apply ne_of_gt
282282
push_neg at h_base
283283
calc
284-
z * y > x * y := by apply mul_lt_mul_of_pos_right <;> omega
285-
_ ≥ x * (x + 1) := by apply mul_le_mul <;> omega
284+
z * y > x * y := by apply mul_lt_mul_of_pos_right <;> lia
285+
_ ≥ x * (x + 1) := by apply mul_le_mul <;> lia
286286
_ > x * x + 1 := by
287287
rw [mul_add]
288-
omega
288+
lia
289289
· -- Show the base case.
290290
intro x y h h_base
291291
obtain rfl | rfl : x = 0 ∨ x = 1 := by rwa [Nat.le_add_one_iff, Nat.le_zero] at h_base

Archive/Imo/Imo1994Q1.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ theorem tedious (m : ℕ) (k : Fin (m + 1)) : m - ((m + 1 - ↑k) + m) % (m + 1)
3737
rcases hk with ⟨c, rfl⟩
3838
have : (k + c + 1 - k) + (k + c) = c + (k + c + 1) := by lia
3939
rw [Fin.val_mk, this, Nat.add_mod_right, Nat.mod_eq_of_lt, Nat.add_sub_cancel]
40-
omega
40+
lia
4141

4242
end Imo1994Q1
4343

Archive/Imo/Imo1997Q3.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ lemma sign_eq_of_contra
7474
| one => rfl
7575
| mul_right p s sform ih =>
7676
suffices |S x p - S x (p * s)| ≤ (n + 1 : ℕ) + 1 by
77-
rw [ih]; exact sign_eq_of_abs_sub_le (h _) (h _) (by norm_cast; omega) this
77+
rw [ih]; exact sign_eq_of_abs_sub_le (h _) (h _) (by norm_cast; lia) this
7878
rw [Set.mem_range] at sform; obtain ⟨i, hi⟩ := sform
7979
iterate 2 rw [S, ← sum_add_sum_compl {i.castSucc, i.succ}]
8080
have cg : ∑ j ∈ {i.castSucc, i.succ}ᶜ, (j + 1) * x ((p * s) j) =
@@ -99,16 +99,16 @@ lemma S_one_add_S_revPerm
9999
nth_rw 2 [S]; rw [← revPerm.sum_comp _ _ (by simp)]
100100
simp_rw [revPerm_apply, val_rev, rev_rev, S, Perm.one_apply, ← sum_add_distrib, ← add_mul]
101101
have cg : ∑ i : Fin n, (i + 1 + ((n - (i + 1) : ℕ) + 1)) * x i = ∑ i, (n + 1) * x i := by
102-
congr! 2 with i; norm_cast; omega
103-
rw [cg, ← mul_sum, abs_mul, hx₁, mul_one]; exact abs_of_nonneg (by norm_cast; omega)
102+
congr! 2 with i; norm_cast; lia
103+
rw [cg, ← mul_sum, abs_mul, hx₁, mul_one]; exact abs_of_nonneg (by norm_cast; lia)
104104

105105
theorem result {x : Fin n → ℝ} (hx₁ : |∑ i, x i| = 1) (hx₂ : ∀ i, |x i| ≤ (n + 1) / 2) :
106106
∃ p, |S x p| ≤ (n + 1) / 2 := by
107107
match n with
108108
| 0 => simp [S]
109109
| n + 1 =>
110110
by_contra! h
111-
exact (lt_abs_add_of_sign_eq (h _) (h _) (by norm_cast; omega)
111+
exact (lt_abs_add_of_sign_eq (h _) (h _) (by norm_cast; lia)
112112
(sign_eq_of_contra hx₂ h _)).ne' (S_one_add_S_revPerm hx₁)
113113

114114
end Imo1997Q3

Archive/Imo/Imo2001Q3.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ lemma card_not_easy_le_five {i : Fin 21} (hG : #(G i) ≤ 6) (hB : ∀ j, ¬Disj
6666
_ = #((G i).biUnion fun p ↦ {j | p ∈ B j}) := by congr 1; ext j; simp [not_disjoint_iff]
6767
_ ≤ ∑ p ∈ G i, #{j | p ∈ B j} := card_biUnion_le
6868
_ ≤ ∑ p ∈ G i, 2 := sum_le_sum fun p mp ↦ Nat.le_of_lt_succ (h p mp)
69-
_ ≤ _ := by rw [sum_const, smul_eq_mul]; omega
69+
_ ≤ _ := by rw [sum_const, smul_eq_mul]; lia
7070

7171
open Classical in
7272
/-- There are at most 210 girl-boy pairs who solved some problem in common that was not easy for

Archive/Imo/Imo2001Q6.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ variable {a b c d : ℤ}
2222
theorem imo2001_q6 (hd : 0 < d) (hdc : d < c) (hcb : c < b) (hba : b < a)
2323
(h : a * c + b * d = (a + b - c + d) * (-a + b + c + d)) : ¬Prime (a * b + c * d) := by
2424
intro (h0 : Prime (a * b + c * d))
25-
have ha : 0 < a := by omega
26-
have hb : 0 < b := by omega
27-
have hc : 0 < c := by omega
25+
have ha : 0 < a := by lia
26+
have hb : 0 < b := by lia
27+
have hc : 0 < c := by lia
2828
-- the key step is to show that `a*c + b*d` divides the product `(a*b + c*d) * (a*d + b*c)`
2929
have dvd_mul : a * c + b * d ∣ (a * b + c * d) * (a * d + b * c) := by
3030
use b ^ 2 + b * d + d ^ 2

Archive/Imo/Imo2008Q3.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ theorem p_lemma (p : ℕ) (hpp : Nat.Prime p) (hp_mod_4_eq_1 : p ≡ 1 [MOD 4])
4545
simp only [m, Int.cast_pow, Int.cast_add, Int.cast_one, ZMod.coe_valMinAbs]
4646
rw [pow_two, ← hy]; exact neg_add_cancel 1
4747
have hnat₂ : n ≤ p / 2 := ZMod.natAbs_valMinAbs_le y
48-
have hnat₃ : p ≥ 2 * n := by omega
48+
have hnat₃ : p ≥ 2 * n := by lia
4949
set k : ℕ := p - 2 * n with hnat₄
5050
have hnat₅ : p ∣ k ^ 2 + 4 := by
5151
obtain ⟨x, hx⟩ := hnat₁

Archive/Imo/Imo2015Q6.lean

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ lemma exists_add_eq_of_mem_pool {z : ℤ} (hz : z ∈ pool a t) : ∃ u < t, u +
7373
simp_rw [pool, mem_map, Equiv.coe_toEmbedding, Equiv.subRight_apply] at hz
7474
obtain ⟨y, my, ey⟩ := hz
7575
rw [mem_insert, mem_erase] at my; rcases my with h | h
76-
· use t; omega
76+
· use t; lia
7777
· obtain ⟨u, lu, hu⟩ := ih h.2
78-
use u; omega
78+
use u; lia
7979

8080
include ha
8181

@@ -86,10 +86,10 @@ lemma pool_subset_Icc : ∀ {t}, pool a t ⊆ Icc 0 2014
8686
intro x hx
8787
simp_rw [pool, mem_map, Equiv.coe_toEmbedding, Equiv.subRight_apply] at hx
8888
obtain ⟨y, my, ey⟩ := hx
89-
suffices y ∈ Icc 1 2015 by rw [mem_Icc] at this ⊢; omega
89+
suffices y ∈ Icc 1 2015 by rw [mem_Icc] at this ⊢; lia
9090
rw [mem_insert, mem_erase] at my; rcases my with h | ⟨h₁, h₂⟩
9191
· exact h ▸ ha.1 t
92-
· have := pool_subset_Icc h₂; rw [mem_Icc] at this ⊢; omega
92+
· have := pool_subset_Icc h₂; rw [mem_Icc] at this ⊢; lia
9393

9494
lemma notMem_pool_self : a t ∉ pool a t := by
9595
by_contra h
@@ -105,12 +105,12 @@ lemma card_pool_succ : #(pool a (t + 1)) = #(pool a t) + if 0 ∈ pool a t then
105105
rw [mem_erase, not_and_or]; exact .inr (notMem_pool_self ha)
106106
rw [pool, card_map, card_insert_of_notMem nms, card_erase_eq_ite]
107107
split_ifs with h
108-
· have := card_pos.mpr ⟨0, h⟩; omega
108+
· have := card_pos.mpr ⟨0, h⟩; lia
109109
· rfl
110110

111111
lemma monotone_card_pool : Monotone fun t ↦ #(pool a t) := by
112112
refine monotone_nat_of_le_succ fun t ↦ ?_
113-
have := card_pool_succ (t := t) ha; omega
113+
have := card_pool_succ (t := t) ha; lia
114114

115115
/-- There exists a point where the number of balls reaches a maximum (which follows from its
116116
monotonicity and boundedness). We take its coordinates for the problem's `b` and `N`. -/
@@ -135,12 +135,12 @@ lemma b_pos : 0 < b := by
135135
· exact hbN _ h.le
136136
have cp1 : #(pool a 1) = 1 := by
137137
simp_rw [card_pool_succ ha, pool, card_empty, notMem_empty, ite_false]
138-
apply absurd (hbN 1); omega
138+
apply absurd (hbN 1); lia
139139

140140
include ht in
141141
lemma zero_mem_pool : 0 ∈ pool a t := by
142142
have := card_pool_succ (t := t) ha
143-
have := hbN (t + 1) (by omega)
143+
have := hbN (t + 1) (by lia)
144144
simp_all
145145

146146
include ht in
@@ -151,14 +151,14 @@ lemma sum_sub_sum_eq_sub : ∑ x ∈ pool a (t + 1), x - ∑ x ∈ pool a t, x =
151151
rw [mem_erase, not_and_or]; exact .inr (notMem_pool_self ha)
152152
rw [sum_insert nms, sum_erase_eq_sub (h := zero_mem_pool ha hbN ht), sum_sub_distrib, sum_const,
153153
nsmul_one, hbN _ ht]
154-
omega
154+
lia
155155

156156
/-- The telescoping sum giving the part of the problem's expression within the modulus signs. -/
157157
lemma sum_telescope {m n : ℕ} (hm : N ≤ m) (hn : m < n) :
158158
∑ j ∈ Ico m n, (a j - b) = ∑ x ∈ pool a n, x - ∑ x ∈ pool a m, x :=
159159
calc
160160
_ = ∑ j ∈ Ico m n, (∑ x ∈ pool a (j + 1), x - ∑ x ∈ pool a j, x) := by
161-
congr! 1 with j hj; rw [sum_sub_sum_eq_sub ha hbN]; simp at hj; omega
161+
congr! 1 with j hj; rw [sum_sub_sum_eq_sub ha hbN]; simp at hj; lia
162162
_ = _ := sum_Ico_sub (∑ x ∈ pool a ·, x) hn.le
163163

164164
include ht in
@@ -190,7 +190,7 @@ theorem result (ha : Condition a) :
190190
nth_rw 2 [← Nat.sub_one_add_one_eq_of_pos bp]
191191
rw [← sum_flip, sum_range_succ, tsub_self, Nat.cast_zero, add_zero, ← sum_sub_distrib]
192192
have sc : ∀ x ∈ range (b - 1), (2014 - x - (b - 1 - x : ℕ)) = (2015 - b : ℤ) := fun x mx ↦ by
193-
rw [mem_range] at mx; omega
193+
rw [mem_range] at mx; lia
194194
rw [sum_congr rfl sc, sum_const, card_range, nsmul_eq_mul, Nat.cast_pred bp]
195195
_ ≤ _ := by
196196
rw [← mul_le_mul_iff_right₀ zero_lt_four, ← mul_assoc,

0 commit comments

Comments
 (0)