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

Commit 2b35fc7

Browse files
committed
refactor(data/set/finite): reorganize and put emphasis on fintype instances (#14136)
I went through `data/set/finite` and reorganized it by rough topic (and moved some lemmas to their proper homes; closes #11177). Two important parts of this module are (1) `fintype` instances for various set constructions and (2) ways to create `set.finite` terms. This change puts the module closer to following a design where the `set.finite` terms are created in a formulaic way from the `fintype` instances. One tool for this is a `set.finite_of_fintype` constructor, which lets typeclass inference do most of the work. Included in this commit is changing `set.infinite` to be protected so that it does not conflict with `infinite`.
1 parent 34e450b commit 2b35fc7

File tree

15 files changed

+726
-636
lines changed

15 files changed

+726
-636
lines changed

archive/100-theorems-list/83_friendship_graphs.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ theorem adj_matrix_sq_of_ne {v w : V} (hvw : v ≠ w) :
7676
((G.adj_matrix R) ^ 2) v w = 1 :=
7777
begin
7878
rw [sq, ← nat.cast_one, ← hG hvw],
79-
simp [common_neighbors, neighbor_finset_eq_filter, finset.filter_filter, finset.filter_inter,
79+
simp [common_neighbors, neighbor_finset_eq_filter, finset.filter_filter,
8080
and_comm, ← neighbor_finset_def],
8181
end
8282

src/analysis/locally_convex/weak_dual.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ begin
100100
simp only [id.def],
101101
let U' := hU₁.to_finset,
102102
by_cases hU₃ : U.fst.nonempty,
103-
{ have hU₃' : U'.nonempty := (set.finite.to_finset.nonempty hU₁).mpr hU₃,
103+
{ have hU₃' : U'.nonempty := hU₁.nonempty_to_finset.mpr hU₃,
104104
refine ⟨(U'.sup p).ball 0 $ U'.inf' hU₃' U.snd, p.basis_sets_mem _ $
105105
(finset.lt_inf'_iff _).2 $ λ y hy, hU₂ y $ (hU₁.mem_to_finset).mp hy, λ x hx y hy, _⟩,
106106
simp only [set.mem_preimage, set.mem_pi, mem_ball_zero_iff],

src/combinatorics/configuration.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,11 +435,11 @@ begin
435435
classical,
436436
have h1 : fintype.card {q // q ≠ p} + 1 = fintype.card P,
437437
{ apply (eq_tsub_iff_add_eq_of_le (nat.succ_le_of_lt (fintype.card_pos_iff.mpr ⟨p⟩))).mp,
438-
convert (fintype.card_subtype_compl).trans (congr_arg _ (fintype.card_subtype_eq p)) },
438+
convert (fintype.card_subtype_compl _).trans (congr_arg _ (fintype.card_subtype_eq p)) },
439439
have h2 : ∀ l : {l : L // p ∈ l}, fintype.card {q // q ∈ l.1 ∧ q ≠ p} = order P L,
440440
{ intro l,
441441
rw [←fintype.card_congr (equiv.subtype_subtype_equiv_subtype_inter _ _),
442-
fintype.card_subtype_compl, ←nat.card_eq_fintype_card],
442+
fintype.card_subtype_compl (λ (x : subtype (∈ l.val)), x.val = p), ←nat.card_eq_fintype_card],
443443
refine tsub_eq_of_eq_add ((point_count_eq P l.1).trans _),
444444
rw ← fintype.card_subtype_eq (⟨p, l.2⟩ : {q : P // q ∈ l.1}),
445445
simp_rw subtype.ext_iff_val },

src/combinatorics/simple_graph/basic.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ by { ext, simp }
755755

756756
lemma neighbor_finset_compl [decidable_eq V] [decidable_rel G.adj] (v : V) :
757757
Gᶜ.neighbor_finset v = (G.neighbor_finset v)ᶜ \ {v} :=
758-
by simp only [neighbor_finset, neighbor_set_compl, set.to_finset_sdiff, set.to_finset_compl,
758+
by simp only [neighbor_finset, neighbor_set_compl, set.to_finset_diff, set.to_finset_compl,
759759
set.to_finset_singleton]
760760

761761
@[simp]
@@ -917,17 +917,17 @@ begin
917917
{ rw finset.insert_subset,
918918
split,
919919
{ simpa, },
920-
{ rw [neighbor_finset, set.subset_iff_to_finset_subset],
920+
{ rw [neighbor_finset, set.to_finset_mono],
921921
exact G.common_neighbors_subset_neighbor_set_left _ _ } }
922922
end
923923

924924
lemma card_common_neighbors_top [decidable_eq V] {v w : V} (h : v ≠ w) :
925925
fintype.card ((⊤ : simple_graph V).common_neighbors v w) = fintype.card V - 2 :=
926926
begin
927-
simp only [common_neighbors_top_eq, ← set.to_finset_card, set.to_finset_sdiff],
927+
simp only [common_neighbors_top_eq, ← set.to_finset_card, set.to_finset_diff],
928928
rw finset.card_sdiff,
929929
{ simp [finset.card_univ, h], },
930-
{ simp only [set.subset_iff_to_finset_subset, set.subset_univ] },
930+
{ simp only [set.to_finset_mono, set.subset_univ] },
931931
end
932932

933933
end finite

src/combinatorics/simple_graph/strongly_regular.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ lemma is_SRG_with.card_common_neighbors_eq_of_adj_compl (h : G.is_SRG_with n k
134134
fintype.card ↥(Gᶜ.common_neighbors v w) = n - (2 * k - μ) - 2 :=
135135
begin
136136
simp only [←set.to_finset_card, common_neighbors, set.to_finset_inter, neighbor_set_compl,
137-
set.to_finset_sdiff, set.to_finset_singleton, set.to_finset_compl, ←neighbor_finset_def],
137+
set.to_finset_diff, set.to_finset_singleton, set.to_finset_compl, ←neighbor_finset_def],
138138
simp_rw compl_neighbor_finset_sdiff_inter_eq,
139139
have hne : v ≠ w := ne_of_adj _ ha,
140140
rw compl_adj at ha,
@@ -153,7 +153,7 @@ lemma is_SRG_with.card_common_neighbors_eq_of_not_adj_compl (h : G.is_SRG_with n
153153
fintype.card ↥(Gᶜ.common_neighbors v w) = n - (2 * k - ℓ) :=
154154
begin
155155
simp only [←set.to_finset_card, common_neighbors, set.to_finset_inter, neighbor_set_compl,
156-
set.to_finset_sdiff, set.to_finset_singleton, set.to_finset_compl, ←neighbor_finset_def],
156+
set.to_finset_diff, set.to_finset_singleton, set.to_finset_compl, ←neighbor_finset_def],
157157
simp only [not_and, not_not, compl_adj] at hna,
158158
have h2' := hna hn,
159159
simp_rw [compl_neighbor_finset_sdiff_inter_eq, sdiff_compl_neighbor_finset_inter_eq h2'],

src/data/fintype/basic.lean

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,13 @@ by simp [to_finset]
641641
@[simp] theorem mem_to_finset_val {s : set α} [fintype s] {a : α} : a ∈ s.to_finset.1 ↔ a ∈ s :=
642642
mem_to_finset
643643

644+
/-- Membership of a set with a `fintype` instance is decidable.
645+
646+
Using this as an instance leads to potential loops with `subtype.fintype` under certain decidability
647+
assumptions, so it should only be declared a local instance. -/
648+
def decidable_mem_of_fintype [decidable_eq α] (s : set α) [fintype s] (a) : decidable (a ∈ s) :=
649+
decidable_of_iff _ mem_to_finset
650+
644651
-- We use an arbitrary `[fintype s]` instance here,
645652
-- not necessarily coming from a `[fintype α]`.
646653
@[simp]
@@ -667,9 +674,46 @@ by simp only [finset.ssubset_def, to_finset_mono, ssubset_def]
667674
disjoint s.to_finset t.to_finset ↔ disjoint s t :=
668675
by simp only [disjoint_iff_disjoint_coe, coe_to_finset]
669676

677+
lemma to_finset_inter {α : Type*} [decidable_eq α] (s t : set α) [fintype (s ∩ t : set α)]
678+
[fintype s] [fintype t] : (s ∩ t).to_finset = s.to_finset ∩ t.to_finset :=
679+
by { ext, simp }
680+
681+
lemma to_finset_union {α : Type*} [decidable_eq α] (s t : set α) [fintype (s ∪ t : set α)]
682+
[fintype s] [fintype t] : (s ∪ t).to_finset = s.to_finset ∪ t.to_finset :=
683+
by { ext, simp }
684+
685+
lemma to_finset_diff {α : Type*} [decidable_eq α] (s t : set α) [fintype s] [fintype t]
686+
[fintype (s \ t : set α)] : (s \ t).to_finset = s.to_finset \ t.to_finset :=
687+
by { ext, simp }
688+
689+
lemma to_finset_ne_eq_erase {α : Type*} [decidable_eq α] [fintype α] (a : α)
690+
[fintype {x : α | x ≠ a}] : {x : α | x ≠ a}.to_finset = finset.univ.erase a :=
691+
by { ext, simp }
692+
670693
theorem to_finset_compl [decidable_eq α] [fintype α] (s : set α) [fintype s] [fintype ↥sᶜ] :
671694
(sᶜ).to_finset = s.to_finsetᶜ :=
672-
by { ext a, simp }
695+
by { ext, simp }
696+
697+
/- TODO Without the coercion arrow (`↥`) there is an elaboration bug;
698+
it essentially infers `fintype.{v} (set.univ.{u} : set α)` with `v` and `u` distinct.
699+
Reported in leanprover-community/lean#672 -/
700+
@[simp] lemma to_finset_univ [fintype ↥(set.univ : set α)] [fintype α] :
701+
(set.univ : set α).to_finset = finset.univ :=
702+
by { ext, simp }
703+
704+
@[simp] lemma to_finset_range [decidable_eq α] [fintype β] (f : β → α) [fintype (set.range f)] :
705+
(set.range f).to_finset = finset.univ.image f :=
706+
by { ext, simp }
707+
708+
/- TODO The `↥` circumvents an elaboration bug. See comment on `set.to_finset_univ`. -/
709+
lemma to_finset_singleton (a : α) [fintype ↥({a} : set α)] : ({a} : set α).to_finset = {a} :=
710+
by { ext, simp }
711+
712+
/- TODO The `↥` circumvents an elaboration bug. See comment on `set.to_finset_univ`. -/
713+
@[simp] lemma to_finset_insert [decidable_eq α] {a : α} {s : set α}
714+
[fintype ↥(insert a s : set α)] [fintype s] :
715+
(insert a s).to_finset = insert a s.to_finset :=
716+
by { ext, simp }
673717

674718
lemma filter_mem_univ_eq_to_finset [fintype α] (s : set α) [fintype s] [decidable_pred (∈ s)] :
675719
finset.univ.filter (∈ s) = s.to_finset :=
@@ -1168,23 +1212,12 @@ instance Prop.fintype : fintype Prop :=
11681212
instance subtype.fintype (p : α → Prop) [decidable_pred p] [fintype α] : fintype {x // p x} :=
11691213
fintype.subtype (univ.filter p) (by simp)
11701214

1171-
/- TODO Without the coercion arrow (`↥`) there is an elaboration bug;
1172-
it essentially infers `fintype.{v} (set.univ.{u} : set α)` with `v` and `u` distinct.
1173-
Reported in leanprover-community/lean#672 -/
1174-
@[simp] lemma set.to_finset_univ [fintype ↥(set.univ : set α)] [fintype α] :
1175-
(set.univ : set α).to_finset = finset.univ :=
1176-
by { ext, simp only [set.mem_univ, mem_univ, set.mem_to_finset] }
1177-
11781215
@[simp] lemma set.to_finset_eq_empty_iff {s : set α} [fintype s] : s.to_finset = ∅ ↔ s = ∅ :=
1179-
by simp [ext_iff, set.ext_iff]
1216+
by simp only [ext_iff, set.ext_iff, set.mem_to_finset, not_mem_empty, set.mem_empty_eq]
11801217

11811218
@[simp] lemma set.to_finset_empty : (∅ : set α).to_finset = ∅ :=
11821219
set.to_finset_eq_empty_iff.mpr rfl
11831220

1184-
@[simp] lemma set.to_finset_range [decidable_eq α] [fintype β] (f : β → α) [fintype (set.range f)] :
1185-
(set.range f).to_finset = finset.univ.image f :=
1186-
by simp [ext_iff]
1187-
11881221
/-- A set on a fintype, when coerced to a type, is a fintype. -/
11891222
def set_fintype [fintype α] (s : set α) [decidable_pred (∈ s)] : fintype s :=
11901223
subtype.fintype (λ x, x ∈ s)
@@ -1412,6 +1445,26 @@ begin
14121445
simp
14131446
end
14141447

1448+
@[simp]
1449+
lemma fintype.card_subtype_compl [fintype α]
1450+
(p : α → Prop) [fintype {x // p x}] [fintype {x // ¬ p x}] :
1451+
fintype.card {x // ¬ p x} = fintype.card α - fintype.card {x // p x} :=
1452+
begin
1453+
classical,
1454+
rw [fintype.card_of_subtype (set.to_finset pᶜ), set.to_finset_compl p, finset.card_compl,
1455+
fintype.card_of_subtype (set.to_finset p)];
1456+
intro; simp only [set.mem_to_finset, set.mem_compl_eq]; refl,
1457+
end
1458+
1459+
/-- If two subtypes of a fintype have equal cardinality, so do their complements. -/
1460+
lemma fintype.card_compl_eq_card_compl [fintype α]
1461+
(p q : α → Prop)
1462+
[fintype {x // p x}] [fintype {x // ¬ p x}]
1463+
[fintype {x // q x}] [fintype {x // ¬ q x}]
1464+
(h : fintype.card {x // p x} = fintype.card {x // q x}) :
1465+
fintype.card {x // ¬ p x} = fintype.card {x // ¬ q x} :=
1466+
by simp only [fintype.card_subtype_compl, h]
1467+
14151468
theorem fintype.card_quotient_le [fintype α] (s : setoid α) [decidable_rel ((≈) : α → α → Prop)] :
14161469
fintype.card (quotient s) ≤ fintype.card α :=
14171470
fintype.card_le_of_surjective _ (surjective_quotient_mk _)

src/data/nat/nth.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ begin
8383
apply finset.card_erase_of_mem,
8484
rw [nth, set.finite.mem_to_finset],
8585
apply Inf_mem,
86-
rwa [←set.finite.to_finset.nonempty hp'', ←finset.card_pos, hk],
86+
rwa [←hp''.nonempty_to_finset, ←finset.card_pos, hk],
8787
end
8888

8989
lemma nth_set_card {n : ℕ} (hp : (set_of p).finite)
@@ -108,7 +108,7 @@ lemma nth_set_nonempty_of_lt_card {n : ℕ} (hp : (set_of p).finite) (hlt: n < h
108108
begin
109109
have hp': {i : ℕ | p i ∧ ∀ (k : ℕ), k < n → nth p k < i}.finite,
110110
{ exact hp.subset (λ x hx, hx.1) },
111-
rw [←hp'^.to_finset.nonempty, ←finset.card_pos, nth_set_card p hp],
111+
rw [←hp'.nonempty_to_finset, ←finset.card_pos, nth_set_card p hp],
112112
exact nat.sub_pos_of_lt hlt,
113113
end
114114

0 commit comments

Comments
 (0)