Skip to content

Commit aeb8065

Browse files
YaelDilliesMaldooor
authored andcommitted
refactor(Data/Sym/Sym2): curry mk (leanprover-community#34803)
Curry `Sym2.mk` from `Sym2.mk (p : α × α)` to `Sym2.mk (a b : α)`. This matches the `s(a, b)` notation and reduce the uncertainty about simp normal form.
1 parent c33f70f commit aeb8065

File tree

15 files changed

+104
-181
lines changed

15 files changed

+104
-181
lines changed

Mathlib/Combinatorics/SimpleGraph/Acyclic.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ lemma IsTree.card_edgeFinset [Fintype V] [Fintype G.edgeSet] (hG : G.IsTree) :
333333
length_cons, length_nil] at h'
334334
simp at h'
335335
rw [← hf' _ (.cons h.symm (f x)) ((cons_isPath_iff _ _).2 ⟨hf _, fun hy => ?contra⟩)]
336-
· simp only [firstDart_toProd, getVert_cons_succ, getVert_zero, Prod.swap_prod_mk]
336+
· simp
337337
case contra =>
338338
suffices (f x).takeUntil y hy = .cons h .nil by
339339
rw [← take_spec _ hy] at h'

Mathlib/Combinatorics/SimpleGraph/Dart.lean

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ instance Dart.fintype [Fintype V] [DecidableRel G.Adj] : Fintype G.Dart :=
5858
invFun := fun d => ⟨d.fst, d.snd, d.adj⟩ }
5959

6060
/-- The edge associated to the dart. -/
61-
def Dart.edge (d : G.Dart) : Sym2 V :=
62-
Sym2.mk d.toProd
61+
def Dart.edge (d : G.Dart) : Sym2 V := s(d.fst, d.snd)
6362

6463
@[simp]
65-
theorem Dart.edge_mk {p : V × V} (h : G.Adj p.1 p.2) : (Dart.mk p h).edge = Sym2.mk p :=
64+
theorem Dart.edge_mk {p : V × V} (h : G.Adj p.1 p.2) : (Dart.mk p h).edge = s(p.1, p.2) :=
6665
rfl
6766

6867
@[simp]
@@ -80,7 +79,7 @@ theorem Dart.symm_mk {p : V × V} (h : G.Adj p.1 p.2) : (Dart.mk p h).symm = Dar
8079

8180
@[simp]
8281
theorem Dart.edge_symm (d : G.Dart) : d.symm.edge = d.edge :=
83-
Sym2.mk_prod_swap_eq
82+
Sym2.eq_swap
8483

8584
@[simp]
8685
theorem Dart.edge_comp_symm : Dart.edge ∘ Dart.symm = (Dart.edge : G.Dart → Sym2 V) :=
@@ -102,9 +101,9 @@ theorem dart_edge_eq_iff : ∀ d₁ d₂ : G.Dart, d₁.edge = d₂.edge ↔ d
102101
simp
103102

104103
theorem dart_edge_eq_mk'_iff :
105-
∀ {d : G.Dart} {p : V × V}, d.edge = Sym2.mk p ↔ d.toProd = p ∨ d.toProd = p.swap := by
106-
rintro ⟨p, h⟩
107-
apply Sym2.mk_eq_mk_iff
104+
∀ {d : G.Dart} {u v : V}, d.edge = s(u, v) ↔ d.toProd = (u, v) ∨ d.toProd = (v, u) := by
105+
rintro ⟨p, h⟩ _ _
106+
simp
108107

109108
theorem dart_edge_eq_mk'_iff' :
110109
∀ {d : G.Dart} {u v : V},

Mathlib/Combinatorics/SimpleGraph/EdgeLabeling.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def mk (f : ∀ x y : V, G.Adj x y → K)
123123
(f_symm : ∀ (x y : V) (H : G.Adj x y), f y x H.symm = f x y H) : EdgeLabeling G K
124124
| ⟨e, he⟩ => by
125125
revert he
126-
refine Sym2.hrec (fun xy ↦ f xy.1 xy.2) (fun a b ↦ ?_) e
126+
refine Sym2.hrec f (fun a b ↦ ?_) e
127127
apply Function.hfunext (by simp [adj_comm])
128128
grind
129129

Mathlib/Combinatorics/SimpleGraph/Maps.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ theorem edgeSet_map (f : V ↪ W) (G : SimpleGraph V) :
7777
constructor
7878
· intro ⟨a, b, hadj, ha, hb⟩
7979
use s(a, b), hadj
80-
rw [Embedding.sym2Map_apply, Sym2.map_pair_eq, ha, hb]
80+
rw [Embedding.sym2Map_apply, Sym2.map_mk, ha, hb]
8181
· intro ⟨e, hadj, he⟩
8282
induction e
83-
rw [Embedding.sym2Map_apply, Sym2.map_pair_eq, Sym2.eq_iff] at he
83+
rw [Embedding.sym2Map_apply, Sym2.map_mk, Sym2.eq_iff] at he
8484
exact he.elim (fun ⟨h, h'⟩ ↦ ⟨_, _, hadj, h, h'⟩) (fun ⟨h', h⟩ ↦ ⟨_, _, hadj.symm, h, h'⟩)
8585

8686
lemma map_adj_apply {G : SimpleGraph V} {f : V ↪ W} {a b : V} :

Mathlib/Combinatorics/SimpleGraph/Paths.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ theorem map_isTrail_iff_of_injective (hinj : Function.Injective f) :
822822
| cons _ _ ih =>
823823
rw [map_cons, isTrail_cons, ih, isTrail_cons]
824824
apply and_congr_right'
825-
rw [← Sym2.map_pair_eq, edges_map, ← List.mem_map_of_injective (Sym2.map.injective hinj)]
825+
rw [← Sym2.map_mk, edges_map, ← List.mem_map_of_injective (Sym2.map.injective hinj)]
826826

827827
alias ⟨_, map_isTrail_of_injective⟩ := map_isTrail_iff_of_injective
828828

Mathlib/Combinatorics/SimpleGraph/Subgraph.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ lemma image_coe_edgeSet_coe (G' : G.Subgraph) : Sym2.map (↑) '' G'.coe.edgeSet
254254
rintro e he
255255
induction e using Sym2.ind with | h a b =>
256256
rw [Subgraph.mem_edgeSet] at he
257-
exact ⟨s(⟨a, edge_vert _ he⟩, ⟨b, edge_vert _ he.symm⟩), Sym2.map_pair_eq ..⟩
257+
exact ⟨s(⟨a, edge_vert _ he⟩, ⟨b, edge_vert _ he.symm⟩), Sym2.map_mk ..⟩
258258

259259
theorem mem_verts_of_mem_edge {G' : Subgraph G} {e : Sym2 V} {v : V} (he : e ∈ G'.edgeSet)
260260
(hv : v ∈ e) : v ∈ G'.verts := by
@@ -1099,7 +1099,7 @@ theorem deleteEdges_coe_eq (s : Set (Sym2 G'.verts)) :
10991099
· intro hs
11001100
refine Sym2.ind ?_
11011101
rintro ⟨v', hv'⟩ ⟨w', hw'⟩
1102-
simp only [Sym2.map_pair_eq, Sym2.eq]
1102+
simp only [Sym2.map_mk, Sym2.eq]
11031103
contrapose!
11041104
rintro (_ | _) <;> simpa only [Sym2.eq_swap]
11051105
· intro h' hs

Mathlib/Combinatorics/SimpleGraph/Triangle/Basic.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ lemma edgeDisjointTriangles_iff_mem_sym2_subsingleton :
119119
constructor
120120
· rw [Sym2.forall]
121121
rintro hG a b hab
122-
simp only [Sym2.isDiag_iff_proj_eq] at hab
122+
simp only [Sym2.mk_isDiag_iff] at hab
123123
rw [this _ _ (Sym2.mk_isDiag_iff.not.2 hab)]
124124
rintro _ ⟨hab, c, hac, hbc, rfl⟩ _ ⟨-, d, had, hbd, rfl⟩
125125
refine hG.eq ?_ ?_ (Set.Nontrivial.not_subsingleton ⟨a, ?_, b, ?_, hab.ne⟩) <;>

Mathlib/Data/Finset/Sym.lean

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,14 @@ end
130130

131131
variable {s t : Finset α} {a b : α}
132132

133-
theorem sym2_eq_image [DecidableEq α] : s.sym2 = (s ×ˢ s).image Sym2.mk := by
134-
ext z
135-
refine z.ind fun x y ↦ ?_
136-
grind
133+
theorem sym2_eq_image [DecidableEq α] : s.sym2 = (s ×ˢ s).image Sym2.mk.uncurry := by
134+
ext ⟨a, b⟩; simp; grind
137135

138-
theorem isDiag_mk_of_mem_diag {a : α × α} (h : a ∈ s.diag) : (Sym2.mk a).IsDiag :=
139-
(Sym2.isDiag_iff_proj_eq _).2 (mem_diag.1 h).2
136+
theorem isDiag_mk_of_mem_diag {a b : α} (h : (a, b) ∈ s.diag) : s(a, b).IsDiag := by
137+
simp at *; grind
140138

141-
theorem not_isDiag_mk_of_mem_offDiag {a : α × α} (h : a ∈ s.offDiag) :
142-
¬ (Sym2.mk a).IsDiag := by
143-
rw [Sym2.isDiag_iff_proj_eq]
144-
exact (mem_offDiag.1 h).2.2
139+
theorem not_isDiag_mk_of_mem_offDiag {a b : α} (h : (a, b) ∈ s.offDiag) : ¬ s(a, b).IsDiag := by
140+
simp at *; grind
145141

146142
section Sym2
147143

@@ -155,7 +151,7 @@ theorem diag_mem_sym2_mem_iff : (∀ b, b ∈ Sym2.diag a → b ∈ s) ↔ a ∈
155151
theorem diag_mem_sym2_iff : Sym2.diag a ∈ s.sym2 ↔ a ∈ s := by simp [diag_mem_sym2_mem_iff]
156152

157153
theorem image_diag_union_image_offDiag [DecidableEq α] :
158-
s.diag.image Sym2.mk ∪ s.offDiag.image Sym2.mk = s.sym2 := by
154+
s.diag.image Sym2.mk.uncurry ∪ s.offDiag.image Sym2.mk.uncurry = s.sym2 := by
159155
rw [← image_union, diag_union_offDiag, sym2_eq_image]
160156

161157
end Sym2

Mathlib/Data/List/Sym.lean

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -141,30 +141,7 @@ theorem map_mk_disjoint_sym2 (x : α) (xs : List α) (h : x ∉ xs) :
141141
(map (fun y ↦ s(x, y)) xs).Disjoint xs.sym2 := by
142142
induction xs with
143143
| nil => simp
144-
| cons x' xs ih =>
145-
simp only [mem_cons, not_or] at h
146-
rw [List.sym2, map_cons, map_cons, disjoint_cons_left, disjoint_append_right,
147-
disjoint_cons_right]
148-
refine ⟨?_, ⟨?_, ?_⟩, ?_⟩
149-
· refine not_mem_cons_of_ne_of_not_mem ?_ (not_mem_append ?_ ?_)
150-
· simp [h.1]
151-
· simp_rw [mem_map, not_exists, not_and]
152-
intro x'' hx
153-
simp_rw [Sym2.mk_eq_mk_iff, Prod.swap_prod_mk, Prod.mk.injEq, true_and]
154-
rintro (⟨rfl, rfl⟩ | rfl)
155-
· exact h.2 hx
156-
· exact h.2 hx
157-
· simp [mk_mem_sym2_iff, h.2]
158-
· simp [h.1]
159-
· intro z hx hy
160-
rw [List.mem_map] at hx hy
161-
obtain ⟨a, hx, rfl⟩ := hx
162-
obtain ⟨b, hy, hx⟩ := hy
163-
simp only [Sym2.eq, Sym2.rel_iff', Prod.mk.injEq, Ne.symm h.1, false_and, Prod.swap_prod_mk,
164-
false_or] at hx
165-
obtain ⟨rfl, rfl⟩ := hx
166-
exact h.2 hy
167-
· exact ih h.2
144+
| cons x' xs ih => aesop (add simp mk_mem_sym2_iff, unfold safe List.Disjoint)
168145

169146
theorem dedup_sym2 [DecidableEq α] (xs : List α) : xs.sym2.dedup = xs.dedup.sym2 := by
170147
induction xs with

Mathlib/Data/Sym/Card.lean

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -123,37 +123,23 @@ namespace Sym2
123123
variable [DecidableEq α]
124124

125125
/-- The `diag` of `s : Finset α` is sent on a finset of `Sym2 α` of card `#s`. -/
126-
theorem card_image_diag (s : Finset α) : #(s.diag.image Sym2.mk) = #s := by
127-
rw [card_image_of_injOn, diag_card]
128-
rintro ⟨x₀, x₁⟩ hx _ _ h
129-
cases Sym2.eq.1 h
130-
· rfl
131-
· simp only [mem_coe, mem_diag] at hx
132-
rw [hx.2]
133-
134-
lemma two_mul_card_image_offDiag (s : Finset α) : 2 * #(s.offDiag.image Sym2.mk) = #s.offDiag := by
135-
rw [card_eq_sum_card_image (Sym2.mk : α × α → _), sum_const_nat (Sym2.ind _), mul_comm]
136-
rintro x y hxy
137-
simp_rw [mem_image, mem_offDiag] at hxy
138-
obtain ⟨a, ⟨ha₁, ha₂, ha⟩, h⟩ := hxy
139-
replace h := Sym2.eq.1 h
140-
obtain ⟨hx, hy, hxy⟩ : x ∈ s ∧ y ∈ s ∧ x ≠ y := by
141-
cases h <;> refine ⟨‹_›, ‹_›, ?_⟩ <;> [exact ha; exact ha.symm]
142-
have hxy' : y ≠ x := hxy.symm
143-
have : {z ∈ s.offDiag | Sym2.mk z = s(x, y)} = {(x, y), (y, x)} := by
144-
ext ⟨x₁, y₁⟩
145-
rw [mem_filter, mem_insert, mem_singleton, Sym2.eq_iff, Prod.mk_inj, Prod.mk_inj,
146-
and_iff_right_iff_imp]
147-
-- `hxy'` is used in `exact`
148-
rintro (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩) <;> rw [mem_offDiag] <;> exact ⟨‹_›, ‹_›, ‹_›⟩
149-
rw [this, card_insert_of_notMem, card_singleton]
150-
simp only [not_and, Prod.mk_inj, mem_singleton]
151-
exact fun _ => hxy'
126+
theorem card_image_diag (s : Finset α) : #(s.diag.image Sym2.mk.uncurry) = #s := by
127+
simp [card_image_of_injOn]
128+
129+
lemma two_mul_card_image_offDiag (s : Finset α) :
130+
2 * #(s.offDiag.image Sym2.mk.uncurry) = #s.offDiag := by
131+
rw [card_eq_sum_card_image (Sym2.mk.uncurry : α × α → _), sum_const_nat (Sym2.ind _), mul_comm]
132+
-- FIXME: Would be cool for the final `aesop` call not to require this `a ≠ b ∨ b ≠ a` trick.
133+
have (a b : α) (ha : a ∈ s) (hb : b ∈ s) (hab : a ≠ b ∨ b ≠ a) :
134+
{z ∈ s.offDiag | Sym2.mk.uncurry z = s(a, b)} = .cons (a, b) {(b, a)}
135+
(by simpa [eq_comm] using hab) := by aesop
136+
aesop
152137

153138
/-- The `offDiag` of `s : Finset α` is sent on a finset of `Sym2 α` of card `#s.offDiag / 2`.
154139
This is because every element `s(x, y)` of `Sym2 α` not on the diagonal comes from exactly two
155140
pairs: `(x, y)` and `(y, x)`. -/
156-
theorem card_image_offDiag (s : Finset α) : #(s.offDiag.image Sym2.mk) = (#s).choose 2 := by
141+
theorem card_image_offDiag (s : Finset α) :
142+
#(s.offDiag.image Sym2.mk.uncurry) = (#s).choose 2 := by
157143
rw [Nat.choose_two_right, Nat.mul_sub_left_distrib, mul_one, ← offDiag_card,
158144
Nat.div_eq_of_eq_mul_right Nat.zero_lt_two (two_mul_card_image_offDiag s).symm]
159145

0 commit comments

Comments
 (0)