Skip to content

Commit

Permalink
feat(data/{multiset,finset}/basic): card_erase_eq_ite (#9185)
Browse files Browse the repository at this point in the history
A generic theorem about the cardinality of a `finset` or `multiset` with an element erased.



Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
  • Loading branch information
BoltonBailey and eric-wieser committed Sep 20, 2021
1 parent f37f57d commit 976b261
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/data/finset/basic.lean
Expand Up @@ -977,7 +977,8 @@ mem_erase_iff_of_nodup s.2

theorem not_mem_erase (a : α) (s : finset α) : a ∉ erase s a := mem_erase_of_nodup s.2

@[simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl
-- While this can be solved by `simp`, this lemma is eligible for `dsimp`
@[nolint simp_nf, simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl

theorem ne_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ≠ a :=
by simp only [mem_erase]; exact and.left
Expand Down Expand Up @@ -1016,6 +1017,7 @@ lemma erase_ssubset {a : α} {s : finset α} (h : a ∈ s) : s.erase a ⊂ s :=
calc s.erase a ⊂ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _
... = _ : insert_erase h

@[simp]
theorem erase_eq_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : erase s a = s :=
eq_of_veq $ erase_of_not_mem h

Expand Down Expand Up @@ -2242,6 +2244,7 @@ begin
{ simp [finset.singleton_inter_of_mem h] },
end

@[simp]
theorem card_erase_of_mem [decidable_eq α] {a : α} {s : finset α} :
a ∈ s → card (erase s a) = pred (card s) := card_erase_of_mem

Expand All @@ -2259,6 +2262,10 @@ begin
{ rw [erase_eq_of_not_mem h], apply nat.sub_le }
end

/-- If `a ∈ s` is known, see also `finset.card_erase_of_mem` and `finset.erase_eq_of_not_mem`. -/
theorem card_erase_eq_ite [decidable_eq α] {a : α} {s : finset α} :
card (erase s a) = if a ∈ s then pred (card s) else card s := card_erase_eq_ite

@[simp] theorem card_range (n : ℕ) : card (range n) = n := card_range n

@[simp] theorem card_attach {s : finset α} : card (attach s) = card s := multiset.card_attach
Expand Down
8 changes: 8 additions & 0 deletions src/data/multiset/basic.lean
Expand Up @@ -682,6 +682,14 @@ theorem card_erase_lt_of_mem {a : α} {s : multiset α} : a ∈ s → card (s.er
theorem card_erase_le {a : α} {s : multiset α} : card (s.erase a) ≤ card s :=
card_le_of_le (erase_le a s)

theorem card_erase_eq_ite {a : α} {s : multiset α} :
card (s.erase a) = if a ∈ s then pred (card s) else card s :=
begin
by_cases h : a ∈ s,
{ rwa [card_erase_of_mem h, if_pos] },
{ rwa [erase_of_not_mem h, if_neg] }
end

end erase

@[simp] theorem coe_reverse (l : list α) : (reverse l : multiset α) = l :=
Expand Down

0 comments on commit 976b261

Please sign in to comment.