Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 4b9d94d

Browse files
fpvandoornmergify[bot]
authored andcommitted
feat(data/[fin]set): add some more basic properties of (finite) sets (#948)
* feat(data/[fin]set): add some more basic properties of (finite) sets * update after reviews * fix error, move pairwise_disjoint to lattice as well * fix error
1 parent 7370cbf commit 4b9d94d

File tree

6 files changed

+208
-7
lines changed

6 files changed

+208
-7
lines changed

src/data/equiv/basic.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ protected lemma subset_image {α β} (e : α ≃ β) (s : set α) (t : set β) :
136136
by rw [set.image_subset_iff, e.image_eq_preimage]
137137

138138
lemma symm_image_image {α β} (f : equiv α β) (s : set α) : f.symm '' (f '' s) = s :=
139-
by rw [← set.image_comp]; simpa using set.image_id s
139+
by { rw [← set.image_comp], simp }
140140

141141
protected lemma image_compl {α β} (f : equiv α β) (s : set α) :
142142
f '' -s = -(f '' s) :=

src/data/finset.lean

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ ext.2
6565
@[simp] theorem coe_inj {s₁ s₂ : finset α} : (↑s₁ : set α) = ↑s₂ ↔ s₁ = s₂ :=
6666
(set.ext_iff _ _).trans ext.symm
6767

68+
lemma to_set_injective {α} : function.injective (finset.to_set : finset α → set α) :=
69+
λ s t, coe_inj.1
70+
6871
/- subset -/
6972

7073
instance : has_subset (finset α) := ⟨λ s₁ s₂, ∀ ⦃a⦄, a ∈ s₁ → a ∈ s₂⟩
@@ -140,6 +143,12 @@ exists_mem_of_ne_zero (mt val_eq_zero.1 h)
140143

141144
@[simp] lemma coe_empty : ↑(∅ : finset α) = (∅ : set α) := rfl
142145

146+
lemma nonempty_iff_ne_empty (s : finset α) : nonempty (↑s : set α) ↔ s ≠ ∅ :=
147+
begin
148+
rw [set.coe_nonempty_iff_ne_empty, ←coe_empty],
149+
apply not_congr, apply function.injective.eq_iff, exact to_set_injective
150+
end
151+
143152
/-- `singleton a` is the set `{a}` containing `a` and nothing else. -/
144153
def singleton (a : α) : finset α := ⟨_, nodup_singleton a⟩
145154
local prefix `:90 := singleton
@@ -509,6 +518,9 @@ by simpa only [subset_iff, mem_sdiff, and_imp] using λ a m₁ m₂, and.intro (
509518
@[simp] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (↑s₁ \ ↑s₂ : set α) :=
510519
set.ext $ λ _, mem_sdiff
511520

521+
@[simp] lemma to_set_sdiff (s t : finset α) : (s \ t).to_set = s.to_set \ t.to_set :=
522+
by apply finset.coe_sdiff
523+
512524
end decidable_eq
513525

514526
/- attach -/
@@ -580,6 +592,10 @@ theorem filter_union (s₁ s₂ : finset α) :
580592
(s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p :=
581593
ext.2 $ λ _, by simp only [mem_filter, mem_union, or_and_distrib_right]
582594

595+
theorem filter_union_right (p q : α → Prop) [decidable_pred p] [decidable_pred q] (s : finset α) :
596+
s.filter p ∪ s.filter q = s.filter (λx, p x ∨ q x) :=
597+
ext.2 $ λ x, by simp only [mem_filter, mem_union, and_or_distrib_left.symm]
598+
583599
theorem filter_or (s : finset α) : s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q :=
584600
ext.2 $ λ _, by simp only [mem_filter, mem_union, and_or_distrib_left]
585601

@@ -602,6 +618,15 @@ by simp only [filter_not, inter_sdiff_self]
602618
@[simp] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) :=
603619
set.ext $ λ _, mem_filter
604620

621+
lemma subset_union_elim {s : finset α} {t₁ t₂ : set α} [decidable_pred (∈ t₁)] (h : ↑s ⊆ t₁ ∪ t₂) :
622+
∃s₁ s₂ : finset α, s₁ ∪ s₂ = s ∧ ↑s₁ ⊆ t₁ ∧ ↑s₂ ⊆ t₂ \ t₁ :=
623+
begin
624+
refine ⟨s.filter (∈ t₁), s.filter (∉ t₁), _, _ , _⟩,
625+
{ simp [filter_union_right, classical.or_not] },
626+
{ intro x, simp },
627+
{ intro x, simp, intros hx hx₂, refine ⟨or.resolve_left (h hx) hx₂, hx₂⟩ }
628+
end
629+
605630
end filter
606631

607632
/- range -/
@@ -912,6 +937,21 @@ protected def subtype {α} (p : α → Prop) [decidable_pred p] (s : finset α)
912937
∀{a : subtype p}, a ∈ s.subtype p ↔ a.val ∈ s
913938
| ⟨a, ha⟩ := by simp [finset.subtype, ha]
914939

940+
lemma subset_image_iff [decidable_eq α] [decidable_eq β] {f : α → β}
941+
{s : finset β} {t : set α} : ↑s ⊆ f '' t ↔ ∃s' : finset α, ↑s' ⊆ t ∧ s'.image f = s :=
942+
begin
943+
split, swap,
944+
{ rintro ⟨s, hs, rfl⟩, rw [coe_image], exact set.image_subset f hs },
945+
intro h, induction s using finset.induction with a s has ih h,
946+
{ exact ⟨∅, set.empty_subset _, finset.image_empty _⟩ },
947+
rw [finset.coe_insert, set.insert_subset] at h,
948+
rcases ih h.2 with ⟨s', hst, hsi⟩,
949+
rcases h.1 with ⟨x, hxt, rfl⟩,
950+
refine ⟨insert x s', _, _⟩,
951+
{ rw [finset.coe_insert, set.insert_subset], exact ⟨hxt, hst⟩ },
952+
rw [finset.image_insert, hsi]
953+
end
954+
915955
end image
916956

917957
/- card -/

src/data/set/basic.lean

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,12 @@ union_subset_union h (by refl)
284284
theorem union_subset_union_right (s) {t₁ t₂ : set α} (h : t₁ ⊆ t₂) : s ∪ t₁ ⊆ s ∪ t₂ :=
285285
union_subset_union (by refl) h
286286

287+
lemma subset_union_of_subset_left {s t : set α} (h : s ⊆ t) (u : set α) : s ⊆ t ∪ u :=
288+
subset.trans h (subset_union_left t u)
289+
290+
lemma subset_union_of_subset_right {s u : set α} (h : s ⊆ u) (t : set α) : s ⊆ t ∪ u :=
291+
subset.trans h (subset_union_right t u)
292+
287293
@[simp] theorem union_empty_iff {s t : set α} : s ∪ t = ∅ ↔ s = ∅ ∧ t = ∅ :=
288294
by finish [ext_iff], by finish [ext_iff]⟩
289295

@@ -496,6 +502,9 @@ by simp [eq_empty_iff_forall_not_mem]
496502
theorem inter_singleton_eq_empty : s ∩ {a} = ∅ ↔ a ∉ s :=
497503
by rw [inter_comm, singleton_inter_eq_empty]
498504

505+
lemma nmem_singleton_empty {s : set α} : s ∉ ({∅} : set (set α)) ↔ nonempty s :=
506+
by simp [coe_nonempty_iff_ne_empty]
507+
499508
/- separation -/
500509

501510
theorem mem_sep {s : set α} {p : α → Prop} {x : α} (xs : x ∈ s) (px : p x) : x ∈ {x ∈ s | p x} :=
@@ -553,6 +562,17 @@ by finish [ext_iff]
553562
@[simp] theorem compl_univ : -(univ : set α) = ∅ :=
554563
by finish [ext_iff]
555564

565+
lemma compl_empty_iff {s : set α} : -s = ∅ ↔ s = univ :=
566+
by { split, intro h, rw [←compl_compl s, h, compl_empty], intro h, rw [h, compl_univ] }
567+
568+
lemma compl_univ_iff {s : set α} : -s = univ ↔ s = ∅ :=
569+
by rw [←compl_empty_iff, compl_compl]
570+
571+
lemma nonempty_compl {s : set α} : nonempty (-s : set α) ↔ s ≠ univ :=
572+
by { symmetry, rw [coe_nonempty_iff_ne_empty], apply not_congr,
573+
split, intro h, rw [h, compl_univ],
574+
intro h, rw [←compl_compl s, h, compl_empty] }
575+
556576
theorem union_eq_compl_compl_inter_compl (s t : set α) : s ∪ t = -(-s ∩ -t) :=
557577
by simp [compl_inter, compl_compl]
558578

@@ -667,6 +687,15 @@ lemma diff_subset_iff {s t u : set α} : s \ t ⊆ u ↔ s ⊆ t ∪ u :=
667687
⟨assume h x xs, classical.by_cases or.inl (assume nxt, or.inr (h ⟨xs, nxt⟩)),
668688
assume h x ⟨xs, nxt⟩, or.resolve_left (h xs) nxt⟩
669689

690+
lemma subset_insert_diff (s t : set α) : s ⊆ (s \ t) ∪ t :=
691+
by rw [union_comm, ←diff_subset_iff]
692+
693+
@[simp] lemma diff_singleton_subset_iff {x : α} {s t : set α} : s \ {x} ⊆ t ↔ s ⊆ insert x t :=
694+
by { rw [←union_singleton, union_comm], apply diff_subset_iff }
695+
696+
lemma subset_insert_diff_singleton (x : α) (s : set α) : s ⊆ insert x (s \ {x}) :=
697+
by rw [←diff_singleton_subset_iff]
698+
670699
lemma diff_subset_comm {s t u : set α} : s \ t ⊆ u ↔ s \ u ⊆ t :=
671700
by rw [diff_subset_iff, diff_subset_iff, union_comm]
672701

@@ -694,6 +723,13 @@ by simp [insert_eq, union_diff_self, -union_singleton, -singleton_union]
694723

695724
@[simp] lemma diff_self {s : set α} : s \ s = ∅ := ext $ by simp
696725

726+
lemma mem_diff_singleton {s s' : set α} {t : set (set α)} : s ∈ t \ {s'} ↔ (s ∈ t ∧ s ≠ s') :=
727+
by simp
728+
729+
lemma mem_diff_singleton_empty {s : set α} {t : set (set α)} :
730+
s ∈ t \ {∅} ↔ (s ∈ t ∧ nonempty s) :=
731+
by simp [coe_nonempty_iff_ne_empty]
732+
697733
/- powerset -/
698734

699735
theorem mem_powerset {x s : set α} (h : x ⊆ s) : x ∈ powerset s := h
@@ -799,6 +835,10 @@ mem_image_elim h h_y
799835
(h : ∀a∈s, f a = g a) : f '' s = g '' s :=
800836
by safe [ext_iff, iff_def]
801837

838+
/- A common special case of `image_congr` -/
839+
lemma image_congr' {f g : α → β} {s : set α} (h : ∀ (x : α), f x = g x) : f '' s = g '' s :=
840+
image_congr (λx _, h x)
841+
802842
theorem image_eq_image_of_eq_on {f₁ f₂ : α → β} {s : set α} (heq : eq_on f₁ f₂ s) :
803843
f₁ '' s = f₂ '' s :=
804844
image_congr heq
@@ -815,6 +855,11 @@ begin
815855
finish
816856
end -/
817857

858+
/-- A variant of `image_comp`, useful for rewriting -/
859+
lemma image_image (g : β → γ) (f : α → β) (s : set α) : g '' (f '' s) = (λ x, g (f x)) '' s :=
860+
(image_comp g f s).symm
861+
862+
818863
theorem image_subset {a b : set α} (f : α → β) (h : a ⊆ b) : f '' a ⊆ f '' b :=
819864
by finish [subset_def, mem_image_eq]
820865

@@ -861,6 +906,9 @@ end
861906

862907
@[simp] theorem image_id (s : set α) : id '' s = s := ext $ by simp
863908

909+
/-- A variant of `image_id` -/
910+
@[simp] lemma image_id' (s : set α) : (λx, x) '' s = s := image_id s
911+
864912
theorem compl_compl_image (S : set (set α)) :
865913
compl '' (compl '' S) = S :=
866914
by rw [← image_comp, compl_comp_compl, image_id]
@@ -899,6 +947,9 @@ by rw ← image_union; simp [image_univ_of_surjective H]
899947
theorem image_compl_eq {f : α → β} {s : set α} (H : bijective f) : f '' -s = -(f '' s) :=
900948
subset.antisymm (image_compl_subset H.1) (subset_image_compl H.2)
901949

950+
lemma nonempty_image (f : α → β) {s : set α} : nonempty s → nonempty (f '' s)
951+
| ⟨⟨x, hx⟩⟩ := ⟨⟨f x, mem_image_of_mem f hx⟩⟩
952+
902953
/- image and preimage are a Galois connection -/
903954
theorem image_subset_iff {s : set α} {t : set β} {f : α → β} :
904955
f '' s ⊆ t ↔ s ⊆ f ⁻¹' t :=
@@ -1051,6 +1102,23 @@ lemma image_preimage_eq_of_subset {f : α → β} {s : set β} (hs : s ⊆ range
10511102
f '' (f ⁻¹' s) = s :=
10521103
by rw [image_preimage_eq_inter_range, inter_eq_self_of_subset_left hs]
10531104

1105+
lemma preimage_subset_preimage_iff {s t : set α} {f : β → α} (hs : s ⊆ range f) :
1106+
f ⁻¹' s ⊆ f ⁻¹' t ↔ s ⊆ t :=
1107+
begin
1108+
split,
1109+
{ intros h x hx, rcases hs hx with ⟨y, rfl⟩, exact h hx },
1110+
intros h x, apply h
1111+
end
1112+
1113+
lemma preimage_eq_preimage' {s t : set α} {f : β → α} (hs : s ⊆ range f) (ht : t ⊆ range f) :
1114+
f ⁻¹' s = f ⁻¹' t ↔ s = t :=
1115+
begin
1116+
split,
1117+
{ intro h, apply subset.antisymm, rw [←preimage_subset_preimage_iff hs, h],
1118+
rw [←preimage_subset_preimage_iff ht, h] },
1119+
rintro rfl, refl
1120+
end
1121+
10541122
theorem preimage_inter_range {f : α → β} {s : set β} : f ⁻¹' (s ∩ range f) = f ⁻¹' s :=
10551123
set.ext $ λ x, and_iff_left ⟨x, rfl⟩
10561124

@@ -1082,6 +1150,9 @@ funext $ λ i, rfl
10821150
lemma surjective_onto_range : surjective (range_factorization f) :=
10831151
λ ⟨_, ⟨i, rfl⟩⟩, ⟨i, rfl⟩
10841152

1153+
lemma image_eq_range (f : α → β) (s : set α) : f '' s = range (λ(x : s), f x.1) :=
1154+
by { ext, split, rintro ⟨x, h1, h2⟩, exact ⟨⟨x, h1⟩, h2⟩, rintro ⟨⟨x, h1⟩, h2⟩, exact ⟨x, h1, h2⟩ }
1155+
10851156
end range
10861157

10871158
/-- The set `s` is pairwise `r` if `r x y` for all *distinct* `x y ∈ s`. -/
@@ -1096,6 +1167,7 @@ theorem pairwise_on.mono' {s : set α} {r r' : α → α → Prop}
10961167
λ x xs y ys h, H _ _ (hp x xs y ys h)
10971168

10981169
end set
1170+
open set
10991171

11001172
/- image and preimage on subtypes -/
11011173

@@ -1112,6 +1184,10 @@ set.ext $ assume a,
11121184
@[simp] lemma val_range {p : α → Prop} :
11131185
set.range (@subtype.val _ p) = {x | p x} :=
11141186
by rw ← set.image_univ; simp [-set.image_univ, val_image]
1187+
1188+
@[simp] lemma range_val (s : set α) : range (subtype.val : s → α) = s :=
1189+
val_range
1190+
11151191
theorem val_image_subset (s : set α) (t : set (subtype s)) : t.image val ⊆ s :=
11161192
λ x ⟨y, yt, yvaleq⟩, by rw ←yvaleq; exact y.property
11171193

@@ -1133,6 +1209,16 @@ begin
11331209
split, { intro h, rw h },
11341210
intro h, exact set.injective_image (val_injective) h
11351211
end
1212+
1213+
lemma exists_set_subtype {t : set α} (p : set α → Prop) :
1214+
(∃(s : set t), p (subtype.val '' s)) ↔ ∃(s : set α), s ⊆ t ∧ p s :=
1215+
begin
1216+
split,
1217+
{ rintro ⟨s, hs⟩, refine ⟨subtype.val '' s, _, hs⟩,
1218+
convert image_subset_range _ _, rw [range_val] },
1219+
rintro ⟨s, hs₁, hs₂⟩, refine ⟨subtype.val ⁻¹' s, _⟩,
1220+
rw [image_preimage_eq_of_subset], exact hs₂, rw [range_val], exact hs₁
1221+
end
11361222
end subtype
11371223

11381224
namespace set

src/data/set/finite.lean

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ noncomputable def finite.to_finset {s : set α} (h : finite s) : finset α :=
5555
@[simp] theorem finite.mem_to_finset {s : set α} {h : finite s} {a : α} : a ∈ h.to_finset ↔ a ∈ s :=
5656
@mem_to_finset _ _ (finite.fintype h) _
5757

58+
lemma finite.coe_to_finset {α} {s : set α} (h : finite s) : ↑h.to_finset = s :=
59+
by { ext, apply mem_to_finset }
60+
61+
lemma exists_finset_of_finite {s : set α} (h : finite s) : ∃(s' : finset α), ↑s' = s :=
62+
⟨h.to_finset, h.coe_to_finset⟩
63+
5864
theorem finite.exists_finset {s : set α} : finite s →
5965
∃ s' : finset α, ∀ a : α, a ∈ s' ↔ a ∈ s
6066
| ⟨h⟩ := by exactI ⟨to_finset s, λ _, mem_to_finset⟩
@@ -260,6 +266,10 @@ theorem finite_bUnion {α} {ι : Type*} {s : set ι} {f : ι → set α} :
260266
finite s → (∀i, finite (f i)) → finite (⋃ i∈s, f i)
261267
| ⟨hs⟩ h := by rw [bUnion_eq_Union]; exactI finite_Union (λ i, h _)
262268

269+
theorem finite_bUnion' {α} {ι : Type*} {s : set ι} (f : ι → set α) :
270+
finite s → (∀i ∈ s, finite (f i)) → finite (⋃ i∈s, f i)
271+
| ⟨hs⟩ h := by { rw [bUnion_eq_Union], exactI finite_Union (λ i, h i.1 i.2) }
272+
263273
instance fintype_lt_nat (n : ℕ) : fintype {i | i < n} :=
264274
fintype_of_finset (finset.range n) $ by simp
265275

src/data/set/function.lean

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,23 @@ theorem inj_on_of_eq_on {f1 f2 : α → β} {a : set α} (h₁ : eq_on f1 f2 a)
6262
inj_on f2 a :=
6363
λ _ _ h₁' h₂' heq, by apply h₂ h₁' h₂'; rw [h₁, heq, ←h₁]; repeat {assumption}
6464

65-
theorem inj_on_comp {g : β → γ} {f : α → β} {a : set α} {b : set β}
66-
(h₁ : maps_to f a b) (h₂ : inj_on g b) (h₃: inj_on f a) :
67-
inj_on (g ∘ f) a :=
68-
λ _ _ h₁' h₂' heq,
69-
by apply h₃ h₁' h₂'; apply h₂; repeat {apply h₁, assumption}; assumption
70-
7165
theorem inj_on_of_inj_on_of_subset {f : α → β} {a b : set α} (h₁ : inj_on f b) (h₂ : a ⊆ b) :
7266
inj_on f a :=
7367
λ _ _ h₁' h₂' heq, h₁ (h₂ h₁') (h₂ h₂') heq
7468

7569
lemma injective_iff_inj_on_univ {f : α → β} : injective f ↔ inj_on f univ :=
7670
iff.intro (λ h _ _ _ _ heq, h heq) (λ h _ _ heq, h trivial trivial heq)
7771

72+
theorem inj_on_comp {g : β → γ} {f : α → β} {a : set α} {b : set β}
73+
(h₁ : maps_to f a b) (h₂ : inj_on g b) (h₃: inj_on f a) :
74+
inj_on (g ∘ f) a :=
75+
λ _ _ h₁' h₂' heq,
76+
by apply h₃ h₁' h₂'; apply h₂; repeat {apply h₁, assumption}; assumption
77+
78+
lemma inj_on_comp_of_injective_left {g : β → γ} {f : α → β} {a : set α} (hg : injective g)
79+
(hf : inj_on f a) : inj_on (g ∘ f) a :=
80+
inj_on_comp (maps_to_univ _ _) (injective_iff_inj_on_univ.mp hg) hf
81+
7882
lemma inj_on_iff_injective {f : α → β} {s : set α} : inj_on f s ↔ injective (λ x:s, f x.1) :=
7983
⟨λ H a b h, subtype.eq $ H a.2 b.2 h,
8084
λ H a b as bs h, congr_arg subtype.val $ @H ⟨a, as⟩ ⟨b, bs⟩ h⟩
@@ -88,6 +92,13 @@ begin
8892
simp [A] {contextual := tt}
8993
end
9094

95+
lemma inj_on_preimage {f : α → β} {B : set (set β)} (hB : B ⊆ powerset (range f)) :
96+
inj_on (preimage f) B :=
97+
begin
98+
intros s t hs ht hst,
99+
rw [←image_preimage_eq_of_subset (hB hs), ←image_preimage_eq_of_subset (hB ht), hst]
100+
end
101+
91102
lemma subset_image_iff {s : set α} {t : set β} (f : α → β) :
92103
t ⊆ f '' s ↔ ∃u⊆s, t = f '' u ∧ inj_on f u :=
93104
begin

0 commit comments

Comments
 (0)