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

Commit 1a47cfc

Browse files
committed
feat(data/finset/basic): A finset has card two iff it's {x, y} for some x ≠ y (#10252)
and the similar result for card three. Dumb but surprisingly annoying!
1 parent 9fe0cbc commit 1a47cfc

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/data/finset/basic.lean

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,6 +2427,31 @@ iff.intro
24272427
by simp only [eq, card_erase_of_mem has, pred_succ]⟩)
24282428
(assume ⟨a, t, hat, s_eq, n_eq⟩, s_eq ▸ n_eq ▸ card_insert_of_not_mem hat)
24292429

2430+
lemma card_eq_two [decidable_eq α] {s : finset α} : s.card = 2 ↔ ∃ x y, x ≠ y ∧ s = {x, y} :=
2431+
begin
2432+
split,
2433+
{ rw card_eq_succ,
2434+
simp_rw [card_eq_one],
2435+
rintro ⟨a, _, hab, rfl, b, rfl⟩,
2436+
exact ⟨a, b, not_mem_singleton.1 hab, rfl⟩ },
2437+
{ rintro ⟨x, y, hxy, rfl⟩,
2438+
simp only [hxy, card_insert_of_not_mem, not_false_iff, mem_singleton, card_singleton] }
2439+
end
2440+
2441+
lemma card_eq_three [decidable_eq α] {s : finset α} :
2442+
s.card = 3 ↔ ∃ x y z, x ≠ y ∧ x ≠ z ∧ y ≠ z ∧ s = {x, y, z} :=
2443+
begin
2444+
split,
2445+
{ rw card_eq_succ,
2446+
simp_rw [card_eq_two],
2447+
rintro ⟨a, _, abc, rfl, b, c, bc, rfl⟩,
2448+
rw [mem_insert, mem_singleton, not_or_distrib] at abc,
2449+
exact ⟨a, b, c, abc.1, abc.2, bc, rfl⟩ },
2450+
{ rintro ⟨x, y, z, xy, xz, yz, rfl⟩,
2451+
simp only [xy, xz, yz, mem_insert, card_insert_of_not_mem, not_false_iff, mem_singleton,
2452+
or_self, card_singleton] }
2453+
end
2454+
24302455
theorem card_filter_le (s : finset α) (p : α → Prop) [decidable_pred p] :
24312456
card (s.filter p) ≤ card s :=
24322457
card_le_of_subset $ filter_subset _ _

src/data/list/basic.lean

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ end
236236
@[simp] lemma length_injective [subsingleton α] : injective (length : list α → ℕ) :=
237237
length_injective_iff.mpr $ by apply_instance
238238

239+
lemma length_eq_two {l : list α} : l.length = 2 ↔ ∃ a b, l = [a, b] :=
240+
match l with [a, b], _ := ⟨a, b, rfl⟩ end, λ ⟨a, b, e⟩, e.symm ▸ rfl⟩
241+
242+
lemma length_eq_three {l : list α} : l.length = 3 ↔ ∃ a b c, l = [a, b, c] :=
243+
match l with [a, b, c], _ := ⟨a, b, c, rfl⟩ end, λ ⟨a, b, c, e⟩, e.symm ▸ rfl⟩
244+
239245
/-! ### set-theoretic notation of lists -/
240246

241247
lemma empty_eq : (∅ : list α) = [] := by refl

src/data/multiset/basic.lean

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,16 @@ pos_iff_ne_zero.trans $ not_congr card_eq_zero
504504
theorem card_pos_iff_exists_mem {s : multiset α} : 0 < card s ↔ ∃ a, a ∈ s :=
505505
quot.induction_on s $ λ l, length_pos_iff_exists_mem
506506

507+
lemma card_eq_two {s : multiset α} : s.card = 2 ↔ ∃ x y, s = {x, y} :=
508+
⟨quot.induction_on s (λ l h, (list.length_eq_two.mp h).imp
509+
(λ a, Exists.imp (λ b, congr_arg coe))), λ ⟨a, b, e⟩, e.symm ▸ rfl⟩
510+
511+
lemma card_eq_three {s : multiset α} : s.card = 3 ↔ ∃ x y z, s = {x, y, z} :=
512+
⟨quot.induction_on s (λ l h, (list.length_eq_three.mp h).imp
513+
(λ a, Exists.imp (λ b, Exists.imp (λ c, congr_arg coe)))), λ ⟨a, b, c, e⟩, e.symm ▸ rfl⟩
514+
515+
/-! ### Induction principles -/
516+
507517
/-- A strong induction principle for multisets:
508518
If you construct a value for a particular multiset given values for all strictly smaller multisets,
509519
you can construct a value for any multiset.

0 commit comments

Comments
 (0)