Skip to content

Commit 6d45391

Browse files
committed
feat(Data/Finset): right elements of a finset in the sum type (#17014)
1 parent 8834646 commit 6d45391

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

Mathlib/Data/Finset/Preimage.lean

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,13 @@ theorem sigma_image_fst_preimage_mk {β : α → Type*} [DecidableEq α] (s : Fi
118118
s :=
119119
s.sigma_preimage_mk_of_subset (Subset.refl _)
120120

121+
@[simp] lemma preimage_inl (s : Finset (α ⊕ β)) :
122+
s.preimage Sum.inl Sum.inl_injective.injOn = s.toLeft := by
123+
ext x; simp
124+
125+
@[simp] lemma preimage_inr (s : Finset (α ⊕ β)) :
126+
s.preimage Sum.inr Sum.inr_injective.injOn = s.toRight := by
127+
ext x; simp
128+
121129
end Preimage
122130
end Finset

Mathlib/Data/Finset/Sum.lean

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/-
22
Copyright (c) 2022 Yaël Dillies. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
4-
Authors: Yaël Dillies
4+
Authors: Yaël Dillies, Bhavik Mehta
55
-/
66
import Mathlib.Data.Multiset.Sum
77
import Mathlib.Data.Finset.Card
@@ -15,6 +15,8 @@ the `Finset.sum` operation which computes the additive sum.
1515
## Main declarations
1616
1717
* `Finset.disjSum`: `s.disjSum t` is the disjoint sum of `s` and `t`.
18+
* `Finset.toLeft`: Given a finset of elements `α ⊕ β`, extracts all the elements of the form `α`.
19+
* `Finset.toRight`: Given a finset of elements `α ⊕ β`, extracts all the elements of the form `β`.
1820
-/
1921

2022

@@ -94,4 +96,109 @@ theorem disj_sum_strictMono_right (s : Finset α) :
9496
StrictMono (s.disjSum : Finset β → Finset (α ⊕ β)) := fun _ _ =>
9597
disjSum_ssubset_disjSum_of_subset_of_ssubset Subset.rfl
9698

99+
@[simp] lemma disjSum_inj {α β : Type*} {s₁ s₂ : Finset α} {t₁ t₂ : Finset β} :
100+
s₁.disjSum t₁ = s₂.disjSum t₂ ↔ s₁ = s₂ ∧ t₁ = t₂ := by
101+
simp [Finset.ext_iff]
102+
103+
lemma Injective2_disjSum {α β : Type*} : Function.Injective2 (@disjSum α β) :=
104+
fun _ _ _ _ => by simp [Finset.ext_iff]
105+
106+
/--
107+
Given a finset of elements `α ⊕ β`, extract all the elements of the form `α`. This
108+
forms a quasi-inverse to `disjSum`, in that it recovers its left input.
109+
110+
See also `List.partitionMap`.
111+
-/
112+
def toLeft (s : Finset (α ⊕ β)) : Finset α :=
113+
s.disjiUnion (Sum.elim singleton (fun _ => ∅)) <| by
114+
simp [Set.PairwiseDisjoint, Set.Pairwise, Function.onFun, eq_comm]
115+
116+
/--
117+
Given a finset of elements `α ⊕ β`, extract all the elements of the form `β`. This
118+
forms a quasi-inverse to `disjSum`, in that it recovers its right input.
119+
120+
See also `List.partitionMap`.
121+
-/
122+
def toRight (s : Finset (α ⊕ β)) : Finset β :=
123+
s.disjiUnion (Sum.elim (fun _ => ∅) singleton) <| by
124+
simp [Set.PairwiseDisjoint, Set.Pairwise, Function.onFun, eq_comm]
125+
126+
variable {u v : Finset (α ⊕ β)}
127+
128+
@[simp] lemma mem_toLeft {x : α} : x ∈ u.toLeft ↔ inl x ∈ u := by
129+
simp [toLeft]
130+
131+
@[simp] lemma mem_toRight {x : β} : x ∈ u.toRight ↔ inr x ∈ u := by
132+
simp [toRight]
133+
134+
@[gcongr]
135+
lemma toLeft_subset_toLeft : u ⊆ v → u.toLeft ⊆ v.toLeft :=
136+
fun h _ => by simpa only [mem_toLeft] using @h _
137+
138+
@[gcongr]
139+
lemma toRight_subset_toRight : u ⊆ v → u.toRight ⊆ v.toRight :=
140+
fun h _ => by simpa only [mem_toRight] using @h _
141+
142+
lemma toLeft_monotone : Monotone (@toLeft α β) := fun _ _ => toLeft_subset_toLeft
143+
lemma toRight_monotone : Monotone (@toRight α β) := fun _ _ => toRight_subset_toRight
144+
145+
lemma toLeft_disjSum_toRight : u.toLeft.disjSum u.toRight = u := by
146+
ext (x | x) <;> simp
147+
148+
lemma card_toLeft_add_card_toRight : u.toLeft.card + u.toRight.card = u.card := by
149+
rw [← card_disjSum, toLeft_disjSum_toRight]
150+
151+
lemma card_toLeft_le : u.toLeft.card ≤ u.card :=
152+
(Nat.le_add_right _ _).trans_eq card_toLeft_add_card_toRight
153+
154+
lemma card_toRight_le : u.toRight.card ≤ u.card :=
155+
(Nat.le_add_left _ _).trans_eq card_toLeft_add_card_toRight
156+
157+
@[simp] lemma toLeft_disjSum : (s.disjSum t).toLeft = s := by ext x; simp
158+
159+
@[simp] lemma toRight_disjSum : (s.disjSum t).toRight = t := by ext x; simp
160+
161+
lemma disjSum_eq_iff : s.disjSum t = u ↔ s = u.toLeft ∧ t = u.toRight :=
162+
fun h => by simp [← h], fun h => by simp [h, toLeft_disjSum_toRight]⟩
163+
164+
lemma eq_disjSum_iff : u = s.disjSum t ↔ u.toLeft = s ∧ u.toRight = t :=
165+
fun h => by simp [h], fun h => by simp [← h, toLeft_disjSum_toRight]⟩
166+
167+
@[simp] lemma toLeft_map_sumComm : (u.map (Equiv.sumComm _ _).toEmbedding).toLeft = u.toRight := by
168+
ext x; simp
169+
170+
@[simp] lemma toRight_map_sumComm : (u.map (Equiv.sumComm _ _).toEmbedding).toRight = u.toLeft := by
171+
ext x; simp
172+
173+
@[simp] lemma toLeft_cons_inl (ha) :
174+
(cons (inl a) u ha).toLeft = cons a u.toLeft (by simpa) := by ext y; simp
175+
@[simp] lemma toLeft_cons_inr (hb) :
176+
(cons (inr b) u hb).toLeft = u.toLeft := by ext y; simp
177+
@[simp] lemma toRight_cons_inl (ha) :
178+
(cons (inl a) u ha).toRight = u.toRight := by ext y; simp
179+
@[simp] lemma toRight_cons_inr (hb) :
180+
(cons (inr b) u hb).toRight = cons b u.toRight (by simpa) := by ext y; simp
181+
182+
variable [DecidableEq α] [DecidableEq β]
183+
184+
lemma toLeft_image_swap : (u.image Sum.swap).toLeft = u.toRight := by
185+
ext x; simp
186+
187+
lemma toRight_image_swap : (u.image Sum.swap).toRight = u.toLeft := by
188+
ext x; simp
189+
190+
@[simp] lemma toLeft_insert_inl : (insert (inl a) u).toLeft = insert a u.toLeft := by ext y; simp
191+
@[simp] lemma toLeft_insert_inr : (insert (inr b) u).toLeft = u.toLeft := by ext y; simp
192+
@[simp] lemma toRight_insert_inl : (insert (inl a) u).toRight = u.toRight := by ext y; simp
193+
@[simp] lemma toRight_insert_inr : (insert (inr b) u).toRight = insert b u.toRight := by ext y; simp
194+
195+
lemma toLeft_inter : (u ∩ v).toLeft = u.toLeft ∩ v.toLeft := by ext x; simp
196+
lemma toRight_inter : (u ∩ v).toRight = u.toRight ∩ v.toRight := by ext x; simp
197+
198+
lemma toLeft_union : (u ∪ v).toLeft = u.toLeft ∪ v.toLeft := by ext x; simp
199+
lemma toRight_union : (u ∪ v).toRight = u.toRight ∪ v.toRight := by ext x; simp
200+
201+
lemma toLeft_sdiff : (u \ v).toLeft = u.toLeft \ v.toLeft := by ext x; simp
202+
lemma toRight_sdiff : (u \ v).toRight = u.toRight \ v.toRight := by ext x; simp
203+
97204
end Finset

0 commit comments

Comments
 (0)