Skip to content

Commit 5db9169

Browse files
committed
chore(Order/Defs/LinearOrder): use @[to_dual] (#31709)
This PR tags the content of `Mathlib.Order.Defs.LinearOrder` with `@[to_dual]`. For this to work, we also tag some lemmas used by `grind` with the `@[to_dual]` tag.
1 parent 6ff93aa commit 5db9169

File tree

3 files changed

+56
-41
lines changed

3 files changed

+56
-41
lines changed

Mathlib/Order/Basic.lean

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,6 @@ lemma gt_or_lt (h : a < b) (c : α) : a < c ∨ c < b := (le_or_gt b c).imp h.tr
372372

373373
end LT.lt
374374

375-
-- Variant of `min_def` with the branches reversed.
376-
theorem min_def' (a b : α) : min a b = if b ≤ a then b else a := by
377-
grind
378-
379-
-- Variant of `min_def` with the branches reversed.
380-
-- This is sometimes useful as it used to be the default.
381-
theorem max_def' (a b : α) : max a b = if b ≤ a then a else b := by
382-
grind
383-
384375
@[deprecated (since := "2025-05-11")] alias lt_of_not_le := lt_of_not_ge
385376
@[deprecated (since := "2025-05-11")] alias lt_iff_not_le := lt_iff_not_ge
386377

Mathlib/Order/Defs/LinearOrder.lean

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,20 @@ class LinearOrder (α : Type*) extends PartialOrder α, Min α, Max α, Ord α w
7575
compare_eq_compareOfLessAndEq : ∀ a b, compare a b = compareOfLessAndEq a b := by
7676
compareOfLessAndEq_rfl
7777

78+
attribute [to_dual existing] LinearOrder.toMax
79+
7880
variable [LinearOrder α] {a b c : α}
7981

8082
attribute [instance 900] LinearOrder.toDecidableLT
8183
attribute [instance 900] LinearOrder.toDecidableLE
8284
attribute [instance 900] LinearOrder.toDecidableEq
8385

86+
@[to_dual existing toDecidableLT, inherit_doc toDecidableLT]
87+
def LinearOrder.toDecidableLT' : DecidableLT' α := fun a b => toDecidableLT b a
88+
89+
@[to_dual existing toDecidableLE, inherit_doc toDecidableLE]
90+
def LinearOrder.toDecidableLE' : DecidableLE' α := fun a b => toDecidableLE b a
91+
8492
instance : Std.IsLinearOrder α where
8593
le_total := LinearOrder.le_total
8694

@@ -91,6 +99,7 @@ lemma lt_of_not_ge (h : ¬b ≤ a) : a < b := lt_of_le_not_ge (le_of_not_ge h) h
9199

92100
@[deprecated (since := "2025-05-11")] alias le_of_not_le := le_of_not_ge
93101

102+
@[to_dual gt_trichotomy]
94103
lemma lt_trichotomy (a b : α) : a < b ∨ a = b ∨ b < a := by grind
95104

96105
lemma le_of_not_gt (h : ¬b < a) : a ≤ b :=
@@ -110,21 +119,27 @@ lemma le_or_gt (a b : α) : a ≤ b ∨ b < a := (lt_or_ge b a).symm
110119

111120
@[deprecated (since := "2025-05-11")] alias le_or_lt := le_or_gt
112121

122+
@[to_dual gt_or_lt_of_ne]
113123
lemma lt_or_gt_of_ne (h : a ≠ b) : a < b ∨ b < a := by grind
114124

125+
@[to_dual ne_iff_gt_or_lt]
115126
lemma ne_iff_lt_or_gt : a ≠ b ↔ a < b ∨ b < a := ⟨lt_or_gt_of_ne, (Or.elim · ne_of_lt ne_of_gt)⟩
116127

117128
lemma lt_iff_not_ge : a < b ↔ ¬b ≤ a := ⟨not_le_of_gt, lt_of_not_ge⟩
118129

119130
@[simp, push] lemma not_lt : ¬a < b ↔ b ≤ a := ⟨le_of_not_gt, not_lt_of_ge⟩
120131
@[simp, push] lemma not_le : ¬a ≤ b ↔ b < a := lt_iff_not_ge.symm
121132

133+
attribute [to_dual self (reorder := a b)]
134+
le_total le_of_not_ge lt_of_not_ge le_of_not_gt lt_or_ge le_or_gt lt_iff_not_ge not_lt not_le
135+
136+
@[to_dual eq_or_lt_of_not_gt]
122137
lemma eq_or_gt_of_not_lt (h : ¬a < b) : a = b ∨ b < a :=
123138
if h₁ : a = b then Or.inl h₁ else Or.inr (lt_of_not_ge fun hge => h (lt_of_le_of_ne hge h₁))
124139

125-
@[deprecated (since := "2025-07-27")] alias eq_or_lt_of_not_gt := eq_or_gt_of_not_lt
126140
@[deprecated (since := "2025-05-11")] alias eq_or_lt_of_not_lt := eq_or_gt_of_not_lt
127141

142+
@[to_dual self (reorder := a b, c d)]
128143
theorem le_imp_le_of_lt_imp_lt {α β} [Preorder α] [LinearOrder β] {a b : α} {c d : β}
129144
(H : d < c → b < a) (h : a ≤ b) : c ≤ d :=
130145
le_of_not_gt fun h' => not_le_of_gt (H h') h
@@ -134,62 +149,50 @@ lemma min_def (a b : α) : min a b = if a ≤ b then a else b := LinearOrder.min
134149
@[grind =]
135150
lemma max_def (a b : α) : max a b = if a ≤ b then b else a := LinearOrder.max_def a b
136151

152+
@[to_dual existing max_def]
153+
theorem min_def' (a b : α) : min a b = if b ≤ a then b else a := by grind
154+
155+
@[to_dual existing min_def]
156+
theorem max_def' (a b : α) : max a b = if b ≤ a then a else b := by grind
157+
158+
@[to_dual le_max_left]
137159
lemma min_le_left (a b : α) : min a b ≤ a := by grind
138160

161+
@[to_dual le_max_right]
139162
lemma min_le_right (a b : α) : min a b ≤ b := by grind
140163

164+
@[to_dual max_le]
141165
lemma le_min (h₁ : c ≤ a) (h₂ : c ≤ b) : c ≤ min a b := by grind
142166

143-
lemma le_max_left (a b : α) : a ≤ max a b := by grind
144-
145-
lemma le_max_right (a b : α) : b ≤ max a b := by grind
146-
147-
lemma max_le (h₁ : a ≤ c) (h₂ : b ≤ c) : max a b ≤ c := by grind
148-
167+
@[to_dual]
149168
lemma eq_min (h₁ : c ≤ a) (h₂ : c ≤ b) (h₃ : ∀ {d}, d ≤ a → d ≤ b → d ≤ c) : c = min a b :=
150169
le_antisymm (le_min h₁ h₂) (h₃ (min_le_left a b) (min_le_right a b))
151170

171+
@[to_dual]
152172
lemma min_comm (a b : α) : min a b = min b a :=
153173
eq_min (min_le_right a b) (min_le_left a b) fun h₁ h₂ => le_min h₂ h₁
154174

175+
@[to_dual]
155176
lemma min_assoc (a b c : α) : min (min a b) c = min a (min b c) := by grind
156177

178+
@[to_dual]
157179
lemma min_left_comm (a b c : α) : min a (min b c) = min b (min a c) := by grind
158180

159-
@[simp] lemma min_self (a : α) : min a a = a := by grind
181+
@[to_dual (attr := simp)] lemma min_self (a : α) : min a a = a := by grind
160182

183+
@[to_dual]
161184
lemma min_eq_left (h : a ≤ b) : min a b = a := by grind
162185

186+
@[to_dual]
163187
lemma min_eq_right (h : b ≤ a) : min a b = b := min_comm b a ▸ min_eq_left h
164188

165-
lemma eq_max (h₁ : a ≤ c) (h₂ : b ≤ c) (h₃ : ∀ {d}, a ≤ d → b ≤ d → c ≤ d) :
166-
c = max a b :=
167-
le_antisymm (h₃ (le_max_left a b) (le_max_right a b)) (max_le h₁ h₂)
168-
169-
lemma max_comm (a b : α) : max a b = max b a :=
170-
eq_max (le_max_right a b) (le_max_left a b) fun h₁ h₂ => max_le h₂ h₁
171-
172-
lemma max_assoc (a b c : α) : max (max a b) c = max a (max b c) := by grind
173-
174-
lemma max_left_comm (a b c : α) : max a (max b c) = max b (max a c) := by grind
175-
176-
@[simp] lemma max_self (a : α) : max a a = a := by grind
177-
178-
lemma max_eq_left (h : b ≤ a) : max a b = a := by grind
179-
180-
lemma max_eq_right (h : a ≤ b) : max a b = b := max_comm b a ▸ max_eq_left h
181-
182-
lemma min_eq_left_of_lt (h : a < b) : min a b = a := min_eq_left (le_of_lt h)
183-
lemma min_eq_right_of_lt (h : b < a) : min a b = b := min_eq_right (le_of_lt h)
184-
lemma max_eq_left_of_lt (h : b < a) : max a b = a := max_eq_left (le_of_lt h)
185-
lemma max_eq_right_of_lt (h : a < b) : max a b = b := max_eq_right (le_of_lt h)
189+
@[to_dual] lemma min_eq_left_of_lt (h : a < b) : min a b = a := min_eq_left (le_of_lt h)
190+
@[to_dual] lemma min_eq_right_of_lt (h : b < a) : min a b = b := min_eq_right (le_of_lt h)
186191

192+
@[to_dual max_lt]
187193
lemma lt_min (h₁ : a < b) (h₂ : a < c) : a < min b c := by
188194
cases le_total b c <;> simp [min_eq_left, min_eq_right, *]
189195

190-
lemma max_lt (h₁ : a < c) (h₂ : b < c) : max a b < c := by
191-
cases le_total a b <;> simp [max_eq_left, max_eq_right, *]
192-
193196
section Ord
194197

195198
lemma compare_lt_iff_lt : compare a b = .lt ↔ a < b := by

Mathlib/Tactic/ToDual.lean

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,24 @@ attribute [to_dual lt_of_eq_of_lt''] lt_of_eq_of_lt
4141
attribute [to_dual lt_of_lt_of_eq''] lt_of_lt_of_eq
4242

4343
attribute [to_dual] Max
44+
45+
-- We need to tag the lemmas used by `grind` in order to translate `grind` proofs.
46+
namespace Lean.Grind.Order
47+
48+
attribute [to_dual existing le_of_eq_1] le_of_eq_2
49+
attribute [to_dual self (reorder := a b)] le_of_not_le lt_of_not_le le_of_not_lt
50+
attribute [to_dual self (reorder := 6 7)] eq_of_le_of_le
51+
attribute [to_dual self (reorder := a c, h₁ h₂)] le_trans lt_trans
52+
attribute [to_dual existing (reorder := a c, h₁ h₂) le_lt_trans] lt_le_trans
53+
attribute [to_dual self (reorder := a b)]
54+
le_eq_true_of_lt le_eq_false_of_lt lt_eq_false_of_lt lt_eq_false_of_le
55+
56+
/- For now, we don't tag any `grind` lemmas involving offsets, but this may be done in the future.
57+
These offset lemmas are:
58+
59+
le_of_eq_1_k, le_of_eq_2_k, le_of_not_lt_k, lt_of_not_le_k, le_trans_k, lt_trans_k, le_lt_trans_k,
60+
lt_le_trans_k, le_unsat_k, lt_unsat_k, le_eq_true_of_le_k, le_eq_true_of_lt_k, lt_eq_true_of_lt_k,
61+
lt_eq_true_of_le_k, le_eq_false_of_le_k, lt_eq_false_of_le_k, lt_eq_false_of_lt_k,
62+
le_eq_false_of_lt_k -/
63+
64+
end Lean.Grind.Order

0 commit comments

Comments
 (0)