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

Commit 9282f6c

Browse files
feat(finset): two simple lemmas (#5387)
also open function namespace Co-authored-by: Bryan Gin-ge Chen <bryangingechen@gmail.com>
1 parent 39ecd1a commit 9282f6c

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

src/data/finset/basic.lean

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ There's also the typeclass `fintype α`
2626
as well as the predicate `finite` on `s : set α` (which asserts `nonempty (fintype s)`).
2727
-/
2828

29-
open multiset subtype nat
29+
open multiset subtype nat function
3030

3131
variables {α : Type*} {β : Type*} {γ : Type*}
3232

@@ -90,7 +90,7 @@ ext_iff.2
9090
@[simp, norm_cast] theorem coe_inj {s₁ s₂ : finset α} : (s₁ : set α) = s₂ ↔ s₁ = s₂ :=
9191
set.ext_iff.trans ext_iff.symm
9292

93-
lemma coe_injective {α} : function.injective (coe : finset α → set α) :=
93+
lemma coe_injective {α} : injective (coe : finset α → set α) :=
9494
λ s t, coe_inj.1
9595

9696
/-! ### subset -/
@@ -328,6 +328,9 @@ ext $ λ a, by simp
328328
↑(insert a s) = (insert a s : set α) :=
329329
set.ext $ λ x, by simp only [mem_coe, mem_insert, set.mem_insert_iff]
330330

331+
lemma mem_insert_coe {s : finset α} {x y : α} : x ∈ insert y s ↔ x ∈ insert y (s : set α) :=
332+
by simp
333+
331334
instance : is_lawful_singleton α (finset α) := ⟨λ a, by { ext, simp }⟩
332335

333336
@[simp] theorem insert_eq_of_mem {a : α} {s : finset α} (h : a ∈ s) : insert a s = s :=
@@ -923,7 +926,7 @@ lemma piecewise_insert_of_ne [decidable_eq α] {i j : α} [∀i, decidable (i
923926
by simp [piecewise, h]
924927

925928
lemma piecewise_insert [decidable_eq α] (j : α) [∀i, decidable (i ∈ insert j s)] :
926-
(insert j s).piecewise f g = function.update (s.piecewise f g) j (f j) :=
929+
(insert j s).piecewise f g = update (s.piecewise f g) j (f j) :=
927930
begin
928931
classical,
929932
rw [← piecewise_coe, ← piecewise_coe, ← set.piecewise_insert, ← coe_insert j s],
@@ -938,7 +941,7 @@ lemma piecewise_mem_set_pi {δ : α → Type*} {t : set α} {t' : Π i, set (δ
938941
by { classical, rw ← piecewise_coe, exact set.piecewise_mem_pi ↑s hf hg }
939942

940943
lemma piecewise_singleton [decidable_eq α] (i : α) :
941-
piecewise {i} f g = function.update g i (f i) :=
944+
piecewise {i} f g = update g i (f i) :=
942945
by rw [← insert_emptyc_eq, piecewise_insert, piecewise_empty]
943946

944947
lemma piecewise_piecewise_of_subset_left {s t : finset α} [Π i, decidable (i ∈ s)]
@@ -960,30 +963,29 @@ s.piecewise_congr (λ _ _, rfl) (λ i hi, t.piecewise_eq_of_not_mem _ _ (mt (@h
960963
piecewise_piecewise_of_subset_right (subset.refl _) f g₁ g₂
961964

962965
lemma update_eq_piecewise {β : Type*} [decidable_eq α] (f : α → β) (i : α) (v : β) :
963-
function.update f i v = piecewise (singleton i) (λj, v) f :=
966+
update f i v = piecewise (singleton i) (λj, v) f :=
964967
(piecewise_singleton _ _ _).symm
965968

966969
lemma update_piecewise [decidable_eq α] (i : α) (v : δ i) :
967-
function.update (s.piecewise f g) i v =
968-
s.piecewise (function.update f i v) (function.update g i v) :=
970+
update (s.piecewise f g) i v = s.piecewise (update f i v) (update g i v) :=
969971
begin
970972
ext j,
971973
rcases em (j = i) with (rfl|hj); by_cases hs : j ∈ s; simp *
972974
end
973975

974976
lemma update_piecewise_of_mem [decidable_eq α] {i : α} (hi : i ∈ s) (v : δ i) :
975-
function.update (s.piecewise f g) i v = s.piecewise (function.update f i v) g :=
977+
update (s.piecewise f g) i v = s.piecewise (update f i v) g :=
976978
begin
977979
rw update_piecewise,
978-
refine s.piecewise_congr (λ _ _, rfl) (λ j hj, function.update_noteq _ _ _),
980+
refine s.piecewise_congr (λ _ _, rfl) (λ j hj, update_noteq _ _ _),
979981
exact λ h, hj (h.symm ▸ hi)
980982
end
981983

982984
lemma update_piecewise_of_not_mem [decidable_eq α] {i : α} (hi : i ∉ s) (v : δ i) :
983-
function.update (s.piecewise f g) i v = s.piecewise f (function.update g i v) :=
985+
update (s.piecewise f g) i v = s.piecewise f (update g i v) :=
984986
begin
985987
rw update_piecewise,
986-
refine s.piecewise_congr (λ j hj, function.update_noteq _ _ _) (λ _ _, rfl),
988+
refine s.piecewise_congr (λ j hj, update_noteq _ _ _) (λ _ _, rfl),
987989
exact λ h, hi (h ▸ hj)
988990
end
989991

@@ -1373,14 +1375,16 @@ rfl
13731375
@[simp] theorem to_finset_cons {a : α} {l : list α} : to_finset (a :: l) = insert a (to_finset l) :=
13741376
finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.erase_dup_cons, h]
13751377

1376-
theorem to_finset_surjective : function.surjective (to_finset : list α → finset α) :=
1378+
lemma to_finset_surj_on : set.surj_on to_finset {l : list α | l.nodup} set.univ :=
13771379
begin
1378-
refine λ s, ⟨quotient.out' s.val, finset.ext $ λ x, _⟩,
1379-
obtain ⟨l, hl⟩ := quot.exists_rep s.val,
1380-
rw [list.mem_to_finset, finset.mem_def, ←hl],
1381-
exact list.perm.mem_iff (quotient.mk_out l)
1380+
rintro s -,
1381+
cases s with t hl, induction t using quot.ind with l,
1382+
refine ⟨l, hl, (to_finset_eq hl).symm⟩
13821383
end
13831384

1385+
theorem to_finset_surjective : surjective (to_finset : list α → finset α) :=
1386+
by { intro s, rcases to_finset_surj_on (set.mem_univ s) with ⟨l, -, hls⟩, exact ⟨l, hls⟩ }
1387+
13841388
end list
13851389

13861390
namespace finset
@@ -1618,7 +1622,7 @@ by simp [ext_iff, subtype.forall, subtype.coe_mk]; refl
16181622
/-- `s.subtype p` converts back to `s.filter p` with
16191623
`embedding.subtype`. -/
16201624
@[simp] lemma subtype_map (p : α → Prop) [decidable_pred p] :
1621-
(s.subtype p).map (function.embedding.subtype _) = s.filter p :=
1625+
(s.subtype p).map (embedding.subtype _) = s.filter p :=
16221626
begin
16231627
ext x,
16241628
rw mem_map,
@@ -1638,14 +1642,14 @@ end
16381642
/-- If all elements of a `finset` satisfy the predicate `p`,
16391643
`s.subtype p` converts back to `s` with `embedding.subtype`. -/
16401644
lemma subtype_map_of_mem {p : α → Prop} [decidable_pred p] (h : ∀ x ∈ s, p x) :
1641-
(s.subtype p).map (function.embedding.subtype _) = s :=
1645+
(s.subtype p).map (embedding.subtype _) = s :=
16421646
by rw [subtype_map, filter_true_of_mem h]
16431647

16441648
/-- If a `finset` of a subtype is converted to the main type with
16451649
`embedding.subtype`, all elements of the result have the property of
16461650
the subtype. -/
16471651
lemma property_of_mem_map_subtype {p : α → Prop} (s : finset {x // p x}) {a : α}
1648-
(h : a ∈ s.map (function.embedding.subtype _)) : p a :=
1652+
(h : a ∈ s.map (embedding.subtype _)) : p a :=
16491653
begin
16501654
rcases mem_map.1 h with ⟨x, hx, rfl⟩,
16511655
exact x.2
@@ -1655,14 +1659,14 @@ end
16551659
`embedding.subtype`, the result does not contain any value that does
16561660
not satisfy the property of the subtype. -/
16571661
lemma not_mem_map_subtype_of_not_property {p : α → Prop} (s : finset {x // p x})
1658-
{a : α} (h : ¬ p a) : a ∉ (s.map (function.embedding.subtype _)) :=
1662+
{a : α} (h : ¬ p a) : a ∉ (s.map (embedding.subtype _)) :=
16591663
mt s.property_of_mem_map_subtype h
16601664

16611665
/-- If a `finset` of a subtype is converted to the main type with
16621666
`embedding.subtype`, the result is a subset of the set giving the
16631667
subtype. -/
16641668
lemma map_subtype_subset {t : set α} (s : finset t) :
1665-
↑(s.map (function.embedding.subtype _)) ⊆ t :=
1669+
↑(s.map (embedding.subtype _)) ⊆ t :=
16661670
begin
16671671
intros a ha,
16681672
rw mem_coe at ha,
@@ -1782,7 +1786,7 @@ theorem card_image_of_inj_on [decidable_eq β] {f : α → β} {s : finset α}
17821786
by simp only [card, image_val_of_inj_on H, card_map]
17831787

17841788
theorem card_image_of_injective [decidable_eq β] {f : α → β} (s : finset α)
1785-
(H : function.injective f) : card (image f s) = card s :=
1789+
(H : injective f) : card (image f s) = card s :=
17861790
card_image_of_inj_on $ λ x _ y _ h, H h
17871791

17881792
lemma fiber_card_ne_zero_iff_mem_image (s : finset α) (f : α → β) [decidable_eq β] (y : β) :
@@ -1934,7 +1938,7 @@ let f' : {x // x ∈ s} → {x // x ∈ t} := λ x, ⟨f x.1 x.2, hf x.1 x.2⟩
19341938
let g : {x // x ∈ t} → {x // x ∈ s} :=
19351939
@surj_inv _ _ f'
19361940
(λ x, let ⟨y, hy₁, hy₂⟩ := hsurj x.1 x.2 in ⟨⟨y, hy₁⟩, subtype.eq hy₂.symm⟩) in
1937-
have hg : injective g, from function.injective_surj_inv _,
1941+
have hg : injective g, from injective_surj_inv _,
19381942
have hsg : surjective g, from λ x,
19391943
let ⟨y, hy⟩ := surj_on_of_inj_on_of_card_le (λ (x : {x // x ∈ t}) (hx : x ∈ t.attach), g x)
19401944
(λ x _, show (g x) ∈ s.attach, from mem_attach _ _)
@@ -2094,7 +2098,7 @@ theorem sigma_mono {s₁ s₂ : finset α} {t₁ t₂ : Πa, finset (σ a)}
20942098

20952099
theorem sigma_eq_bind [decidable_eq (Σ a, σ a)] (s : finset α)
20962100
(t : Πa, finset (σ a)) :
2097-
s.sigma t = s.bind (λa, (t a).map $ function.embedding.sigma_mk a) :=
2101+
s.sigma t = s.bind (λa, (t a).map $ embedding.sigma_mk a) :=
20982102
by { ext ⟨x, y⟩, simp [and.left_comm] }
20992103

21002104
end sigma

0 commit comments

Comments
 (0)