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

Commit ecaa289

Browse files
committed
feat(data/finset/basic): lemmas about filter, cons, and disj_union (#15385)
The lemma names and statements match the existing multiset versions.
1 parent 6b2ebac commit ecaa289

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/data/finset/basic.lean

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,13 +538,33 @@ end cons
538538
/-- `disj_union s t h` is the set such that `a ∈ disj_union s t h` iff `a ∈ s` or `a ∈ t`.
539539
It is the same as `s ∪ t`, but it does not require decidable equality on the type. The hypothesis
540540
ensures that the sets are disjoint. -/
541-
def disj_union {α} (s t : finset α) (h : ∀ a ∈ s, a ∉ t) : finset α :=
541+
def disj_union (s t : finset α) (h : ∀ a ∈ s, a ∉ t) : finset α :=
542542
⟨s.1 + t.1, multiset.nodup_add.2 ⟨s.2, t.2, h⟩⟩
543543

544544
@[simp] theorem mem_disj_union {α s t h a} :
545545
a ∈ @disj_union α s t h ↔ a ∈ s ∨ a ∈ t :=
546546
by rcases s with ⟨⟨s⟩⟩; rcases t with ⟨⟨t⟩⟩; apply list.mem_append
547547

548+
lemma disj_union_comm (s t : finset α) (h : ∀ a ∈ s, a ∉ t) :
549+
disj_union s t h = disj_union t s (λ a ht hs, h _ hs ht) :=
550+
eq_of_veq $ add_comm _ _
551+
552+
@[simp] lemma empty_disj_union (t : finset α) (h : ∀ a' ∈ ∅, a' ∉ t := λ a h _, not_mem_empty _ h) :
553+
disj_union ∅ t h = t :=
554+
eq_of_veq $ zero_add _
555+
556+
@[simp] lemma disj_union_empty (s : finset α) (h : ∀ a' ∈ s, a' ∉ ∅ := λ a h, not_mem_empty _) :
557+
disj_union s ∅ h = s :=
558+
eq_of_veq $ add_zero _
559+
560+
lemma singleton_disj_union (a : α) (t : finset α) (h : ∀ a' ∈ {a}, a' ∉ t) :
561+
disj_union {a} t h = cons a t (h _ $ mem_singleton_self _) :=
562+
eq_of_veq $ multiset.singleton_add _ _
563+
564+
lemma disj_union_singleton (s : finset α) (a : α) (h : ∀ a' ∈ s, a' ∉ {a}) :
565+
disj_union s {a} h = cons a s (λ ha, h _ ha $ mem_singleton_self _) :=
566+
by rw [disj_union_comm, singleton_disj_union]
567+
548568
/-! ### insert -/
549569

550570
section decidable_eq
@@ -1522,6 +1542,31 @@ lemma subset_coe_filter_of_subset_forall (s : finset α) {t : set α}
15221542
theorem filter_singleton (a : α) : filter p (singleton a) = if p a then singleton a else ∅ :=
15231543
by { classical, ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] }
15241544

1545+
theorem filter_cons_of_pos (a : α) (s : finset α) (ha : a ∉ s) (hp : p a):
1546+
filter p (cons a s ha) = cons a (filter p s) (mem_filter.not.mpr $ mt and.left ha) :=
1547+
eq_of_veq $ multiset.filter_cons_of_pos s.val hp
1548+
1549+
theorem filter_cons_of_neg (a : α) (s : finset α) (ha : a ∉ s) (hp : ¬p a):
1550+
filter p (cons a s ha) = filter p s :=
1551+
eq_of_veq $ multiset.filter_cons_of_neg s.val hp
1552+
1553+
theorem filter_disj_union (s : finset α) (t : finset α) (h : ∀ (a : α), a ∈ s → a ∉ t) :
1554+
filter p (disj_union s t h) = (filter p s).disj_union (filter p t)
1555+
(λ a hs ht, h a (mem_of_mem_filter _ hs) (mem_of_mem_filter _ ht)) :=
1556+
eq_of_veq $ multiset.filter_add _ _ _
1557+
1558+
theorem filter_cons {a : α} (s : finset α) (ha : a ∉ s) :
1559+
filter p (cons a s ha) = (if p a then {a} else ∅ : finset α).disj_union (filter p s) (λ b hb, by
1560+
{ split_ifs at hb,
1561+
{ rw finset.mem_singleton.mp hb,
1562+
exact (mem_filter.not.mpr $ mt and.left ha) },
1563+
{ cases hb } }) :=
1564+
begin
1565+
split_ifs with h,
1566+
{ rw [filter_cons_of_pos _ _ _ ha h, singleton_disj_union] },
1567+
{ rw [filter_cons_of_neg _ _ _ ha h, empty_disj_union] },
1568+
end
1569+
15251570
variable [decidable_eq α]
15261571

15271572
theorem filter_union (s₁ s₂ : finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p :=

0 commit comments

Comments
 (0)