doc-next-gen

Mathlib.Data.Finset.Card

Module docstring

{"# Cardinality of a finite set

This defines the cardinality of a Finset and provides induction principles for finsets.

Main declarations

  • Finset.card: #s : ℕ returns the cardinality of s : Finset α.

Induction principles

  • Finset.strongInduction: Strong induction
  • Finset.strongInductionOn
  • Finset.strongDownwardInduction
  • Finset.strongDownwardInductionOn
  • Finset.case_strong_induction_on
  • Finset.Nonempty.strong_induction ","### Lattice structure ","### Explicit description of a finset from its card ","### Inductions "}
Finset.card definition
(s : Finset α) : ℕ
Full source
/-- `s.card` is the number of elements of `s`, aka its cardinality.

The notation `#s` can be accessed in the `Finset` locale. -/
def card (s : Finset α) :  :=
  Multiset.card s.1
Cardinality of a finite set
Informal description
For a finite set \( s \) of type `Finset α`, the cardinality function \( \#s \) returns the number of distinct elements in \( s \) as a natural number. This is implemented by taking the underlying multiset of \( s \) and computing its cardinality (which counts distinct elements since `Finset` ensures no duplicates).
Finset.term#_ definition
: Lean.ParserDescr✝
Full source
@[inherit_doc] scoped prefix:arg "#" => Finset.card
Cardinality of a finite set
Informal description
For a finite set `s` of type `Finset α`, the cardinality function `#s` returns the number of elements in `s` as a natural number.
Finset.card_def theorem
(s : Finset α) : #s = Multiset.card s.1
Full source
theorem card_def (s : Finset α) : #s = Multiset.card s.1 :=
  rfl
Finite Set Cardinality Equals Underlying Multiset Cardinality
Informal description
For any finite set $s$ of type `Finset α`, the cardinality of $s$ (denoted $\#s$) is equal to the cardinality of its underlying multiset (denoted $\text{Multiset.card}(s.1)$).
Finset.card_val theorem
(s : Finset α) : Multiset.card s.1 = #s
Full source
@[simp] lemma card_val (s : Finset α) : Multiset.card s.1 = #s := rfl
Equality of Multiset Cardinality and Finite Set Cardinality
Informal description
For any finite set $s$ of type `Finset α`, the cardinality of the underlying multiset of $s$ is equal to the cardinality of $s$, i.e., $\text{Multiset.card}(s.1) = \#s$.
Finset.card_mk theorem
{m nodup} : #(⟨m, nodup⟩ : Finset α) = Multiset.card m
Full source
@[simp]
theorem card_mk {m nodup} : #(⟨m, nodup⟩ : Finset α) = Multiset.card m :=
  rfl
Cardinality of Finite Set Constructed from Nodup Multiset Equals Multiset Cardinality
Informal description
For any multiset $m$ of type $\alpha$ with no duplicates (i.e., $\text{nodup}(m)$ holds), the cardinality of the finite set $\langle m, \text{nodup}(m) \rangle$ is equal to the cardinality of the multiset $m$, i.e., $\#\langle m, \text{nodup}(m) \rangle = \text{Multiset.card}(m)$.
Finset.card_empty theorem
: #(∅ : Finset α) = 0
Full source
@[simp]
theorem card_empty : #(∅ : Finset α) = 0 :=
  rfl
Cardinality of the Empty Finite Set is Zero
Informal description
The cardinality of the empty finite set is zero, i.e., $\#\emptyset = 0$.
Finset.card_le_card theorem
: s ⊆ t → #s ≤ #t
Full source
@[gcongr]
theorem card_le_card : s ⊆ t#s#t :=
  Multiset.card_le_cardMultiset.card_le_card ∘ val_le_iff.mpr
Subset Implies Cardinality Inequality: $s \subseteq t \implies \#s \leq \#t$
Informal description
For any two finite sets $s$ and $t$ of type $\alpha$, if $s$ is a subset of $t$ (i.e., $s \subseteq t$), then the cardinality of $s$ is less than or equal to the cardinality of $t$ (i.e., $\#s \leq \#t$).
Finset.card_mono theorem
: Monotone (@card α)
Full source
@[mono]
theorem card_mono : Monotone (@card α) := by apply card_le_card
Monotonicity of Finite Set Cardinality: $s \subseteq t \implies \#s \leq \#t$
Informal description
The cardinality function $\# : \text{Finset } \alpha \to \mathbb{N}$ is monotone with respect to the subset relation $\subseteq$ on finite sets and the usual ordering $\leq$ on natural numbers. That is, for any two finite sets $s$ and $t$ of type $\alpha$, if $s \subseteq t$, then $\#s \leq \#t$.
Finset.card_eq_zero theorem
: #s = 0 ↔ s = ∅
Full source
@[simp] lemma card_eq_zero : #s#s = 0 ↔ s = ∅ := Multiset.card_eq_zero.trans val_eq_zero
Empty Set Characterization via Cardinality: $\#s = 0 \leftrightarrow s = \emptyset$
Informal description
For any finite set $s$, the cardinality of $s$ is zero if and only if $s$ is the empty set, i.e., $\#s = 0 \leftrightarrow s = \emptyset$.
Finset.card_ne_zero theorem
: #s ≠ 0 ↔ s.Nonempty
Full source
lemma card_ne_zero : #s#s ≠ 0#s ≠ 0 ↔ s.Nonempty := card_eq_zero.ne.trans nonempty_iff_ne_empty.symm
Nonzero Cardinality Characterization for Finite Sets: $\#s \neq 0 \leftrightarrow s \text{ nonempty}$
Informal description
For any finite set $s$, the cardinality of $s$ is nonzero if and only if $s$ is nonempty, i.e., $\#s \neq 0 \leftrightarrow s \text{ is nonempty}$.
Finset.card_pos theorem
: 0 < #s ↔ s.Nonempty
Full source
@[simp] lemma card_pos : 0 < #s ↔ s.Nonempty := Nat.pos_iff_ne_zero.trans card_ne_zero
Positive Cardinality Characterization for Finite Sets: $0 < \#s \leftrightarrow s \text{ nonempty}$
Informal description
For any finite set $s$, the cardinality of $s$ is positive if and only if $s$ is nonempty, i.e., $0 < \#s \leftrightarrow s \text{ is nonempty}$.
Finset.one_le_card theorem
: 1 ≤ #s ↔ s.Nonempty
Full source
@[simp] lemma one_le_card : 1 ≤ #s ↔ s.Nonempty := card_pos
Nonempty Finite Sets Have Cardinality At Least One: $1 \leq \#s \leftrightarrow s \text{ nonempty}$
Informal description
For any finite set $s$, the cardinality of $s$ is at least 1 if and only if $s$ is nonempty, i.e., $1 \leq \#s \leftrightarrow s \text{ is nonempty}$.
Finset.card_ne_zero_of_mem theorem
(h : a ∈ s) : #s ≠ 0
Full source
theorem card_ne_zero_of_mem (h : a ∈ s) : #s#s ≠ 0 :=
  (not_congr card_eq_zero).2 <| ne_empty_of_mem h
Nonzero Cardinality of Finite Sets with Membership: $a \in s \implies \#s \neq 0$
Informal description
For any element $a$ in a finite set $s$, the cardinality of $s$ is not zero, i.e., $a \in s \implies \#s \neq 0$.
Finset.card_singleton theorem
(a : α) : #{ a } = 1
Full source
@[simp]
theorem card_singleton (a : α) : #{a} = 1 :=
  Multiset.card_singleton _
Cardinality of Singleton Finite Set: $\#\{a\} = 1$
Informal description
For any element $a$ of type $\alpha$, the cardinality of the singleton finite set $\{a\}$ is equal to $1$, i.e., $\#\{a\} = 1$.
Finset.card_singleton_inter theorem
[DecidableEq α] : #({ a } ∩ s) ≤ 1
Full source
theorem card_singleton_inter [DecidableEq α] : #({a} ∩ s) ≤ 1 := by
  obtain h | h := Finset.decidableMem a s
  · simp [Finset.singleton_inter_of_not_mem h]
  · simp [Finset.singleton_inter_of_mem h]
Cardinality Bound for Singleton Intersection: $\#(\{a\} \cap s) \leq 1$
Informal description
For any type $\alpha$ with decidable equality, any element $a \in \alpha$, and any finite set $s \subseteq \alpha$, the cardinality of the intersection $\{a\} \cap s$ is at most 1, i.e., $\#(\{a\} \cap s) \leq 1$.
Finset.card_cons theorem
(h : a ∉ s) : #(s.cons a h) = #s + 1
Full source
@[simp]
theorem card_cons (h : a ∉ s) : #(s.cons a h) = #s + 1 :=
  Multiset.card_cons _ _
Cardinality of Finite Set with Added Element: $\#(s.\text{cons}(a, h)) = \#s + 1$ when $a \notin s$
Informal description
For any element $a$ of type $\alpha$ and any finite set $s$ of type $\text{Finset }\alpha$, if $a$ is not in $s$ (i.e., $a \notin s$), then the cardinality of the finite set obtained by adding $a$ to $s$ (denoted $s.\text{cons}(a, h)$) is equal to the cardinality of $s$ plus one, i.e., $\#(s.\text{cons}(a, h)) = \#s + 1$.
Finset.card_insert_of_not_mem theorem
(h : a ∉ s) : #(insert a s) = #s + 1
Full source
@[simp]
theorem card_insert_of_not_mem (h : a ∉ s) : #(insert a s) = #s + 1 := by
  rw [← cons_eq_insert _ _ h, card_cons]
Cardinality of Insertion of New Element: $\#(\{a\} \cup s) = \#s + 1$ when $a \notin s$
Informal description
For any element $a$ of type $\alpha$ and any finite set $s$ of type $\text{Finset }\alpha$, if $a$ is not in $s$ (i.e., $a \notin s$), then the cardinality of the finite set obtained by inserting $a$ into $s$ is equal to the cardinality of $s$ plus one, i.e., $\#(\{a\} \cup s) = \#s + 1$.
Finset.card_insert_of_mem theorem
(h : a ∈ s) : #(insert a s) = #s
Full source
theorem card_insert_of_mem (h : a ∈ s) : #(insert a s) = #s := by rw [insert_eq_of_mem h]
Cardinality of Insertion of Existing Element
Informal description
For any element $a$ in a finite set $s$, the cardinality of the set obtained by inserting $a$ into $s$ equals the cardinality of $s$, i.e., $\#(\{a\} \cup s) = \#s$.
Finset.card_insert_le theorem
(a : α) (s : Finset α) : #(insert a s) ≤ #s + 1
Full source
theorem card_insert_le (a : α) (s : Finset α) : #(insert a s)#s + 1 := by
  by_cases h : a ∈ s
  · rw [insert_eq_of_mem h]
    exact Nat.le_succ _
  · rw [card_insert_of_not_mem h]
Upper Bound on Cardinality After Insertion: $\#(\{a\} \cup s) \leq \#s + 1$
Informal description
For any element $a$ of type $\alpha$ and any finite set $s$ of type $\text{Finset }\alpha$, the cardinality of the set obtained by inserting $a$ into $s$ is less than or equal to the cardinality of $s$ plus one, i.e., $\#(\{a\} \cup s) \leq \#s + 1$.
Finset.card_le_two theorem
: #{ a, b } ≤ 2
Full source
theorem card_le_two : #{a, b} ≤ 2 := card_insert_le _ _
Cardinality Bound for Two-Element Finite Set: $\#\{a, b\} \leq 2$
Informal description
For any two distinct elements $a$ and $b$ in a finite set, the cardinality of the set $\{a, b\}$ is at most 2.
Finset.card_le_three theorem
: #{ a, b, c } ≤ 3
Full source
theorem card_le_three : #{a, b, c} ≤ 3 :=
  (card_insert_le _ _).trans (Nat.succ_le_succ card_le_two)
Cardinality Bound for Three-Element Finite Set: $\#\{a, b, c\} \leq 3$
Informal description
For any three distinct elements $a$, $b$, and $c$ in a finite set, the cardinality of the set $\{a, b, c\}$ is at most 3.
Finset.card_le_four theorem
: #{ a, b, c, d } ≤ 4
Full source
theorem card_le_four : #{a, b, c, d} ≤ 4 :=
  (card_insert_le _ _).trans (Nat.succ_le_succ card_le_three)
Cardinality Bound for Four-Element Finite Set: $\#\{a, b, c, d\} \leq 4$
Informal description
For any four distinct elements $a$, $b$, $c$, and $d$ in a finite set, the cardinality of the set $\{a, b, c, d\}$ is at most 4.
Finset.card_le_five theorem
: #{ a, b, c, d, e } ≤ 5
Full source
theorem card_le_five : #{a, b, c, d, e} ≤ 5 :=
  (card_insert_le _ _).trans (Nat.succ_le_succ card_le_four)
Cardinality Bound for Five-Element Finite Set: $\#\{a, b, c, d, e\} \leq 5$
Informal description
For any five distinct elements $a$, $b$, $c$, $d$, and $e$ in a finite set, the cardinality of the set $\{a, b, c, d, e\}$ is at most 5.
Finset.card_le_six theorem
: #{ a, b, c, d, e, f } ≤ 6
Full source
theorem card_le_six : #{a, b, c, d, e, f} ≤ 6 :=
  (card_insert_le _ _).trans (Nat.succ_le_succ card_le_five)
Cardinality Bound for Six-Element Finite Set: $\#\{a, b, c, d, e, f\} \leq 6$
Informal description
For any six distinct elements $a$, $b$, $c$, $d$, $e$, and $f$ in a finite set, the cardinality of the set $\{a, b, c, d, e, f\}$ is at most 6.
Finset.card_insert_eq_ite theorem
: #(insert a s) = if a ∈ s then #s else #s + 1
Full source
/-- If `a ∈ s` is known, see also `Finset.card_insert_of_mem` and `Finset.card_insert_of_not_mem`.
-/
theorem card_insert_eq_ite : #(insert a s) = if a ∈ s then #s else #s + 1 := by
  by_cases h : a ∈ s
  · rw [card_insert_of_mem h, if_pos h]
  · rw [card_insert_of_not_mem h, if_neg h]
Cardinality of Insertion via Membership Condition: $\#(\{a\} \cup s) = \text{if } a \in s \text{ then } \#s \text{ else } \#s + 1$
Informal description
For any element $a$ of type $\alpha$ and any finite set $s$ of type $\text{Finset }\alpha$, the cardinality of the set obtained by inserting $a$ into $s$ is equal to $\#s$ if $a \in s$, and $\#s + 1$ otherwise. In other words, \[ \#(\{a\} \cup s) = \begin{cases} \#s & \text{if } a \in s, \\ \#s + 1 & \text{if } a \notin s. \end{cases} \]
Finset.card_pair_eq_one_or_two theorem
: #{ a, b } = 1 ∨ #{ a, b } = 2
Full source
@[simp]
theorem card_pair_eq_one_or_two : #{a, b}#{a, b} = 1 ∨ #{a, b} = 2 := by
  simp [card_insert_eq_ite]
  tauto
Cardinality of Two-Element Finite Set: $\#\{a, b\} \in \{1, 2\}$
Informal description
For any two elements $a$ and $b$ of type $\alpha$, the cardinality of the finite set $\{a, b\}$ is either $1$ or $2$.
Finset.card_pair theorem
(h : a ≠ b) : #{ a, b } = 2
Full source
@[simp]
theorem card_pair (h : a ≠ b) : #{a, b} = 2 := by
  rw [card_insert_of_not_mem (not_mem_singleton.2 h), card_singleton]
Cardinality of Two-Element Finite Set: $\#\{a, b\} = 2$ when $a \neq b$
Informal description
For any two distinct elements $a$ and $b$ of type $\alpha$, the cardinality of the finite set $\{a, b\}$ is equal to $2$.
Finset.card_erase_of_mem theorem
: a ∈ s → #(s.erase a) = #s - 1
Full source
/-- $\#(s \setminus \{a\}) = \#s - 1$ if $a \in s$. -/
@[simp]
theorem card_erase_of_mem : a ∈ s#(s.erase a) = #s - 1 :=
  Multiset.card_erase_of_mem
Cardinality of Finite Set After Element Removal: $|s \setminus \{a\}| = |s| - 1$ when $a \in s$
Informal description
For any element $a$ in a finite set $s$, the cardinality of the set obtained by removing $a$ from $s$ equals the cardinality of $s$ minus one, i.e., $|s \setminus \{a\}| = |s| - 1$.
Finset.card_erase_add_one theorem
: a ∈ s → #(s.erase a) + 1 = #s
Full source
@[simp]
theorem card_erase_add_one : a ∈ s#(s.erase a) + 1 = #s :=
  Multiset.card_erase_add_one
Cardinality of Finite Set After Erasure Plus One Equals Original Cardinality
Informal description
For any element $a$ in a finite set $s$, the cardinality of the set obtained by removing $a$ from $s$ plus one equals the cardinality of $s$, i.e., $|s \setminus \{a\}| + 1 = |s|$.
Finset.card_erase_lt_of_mem theorem
: a ∈ s → #(s.erase a) < #s
Full source
theorem card_erase_lt_of_mem : a ∈ s#(s.erase a) < #s :=
  Multiset.card_erase_lt_of_mem
Strict Cardinality Decrease Under Finite Set Erasure: $|s \setminus \{a\}| < |s|$ when $a \in s$
Informal description
For any element $a$ in a finite set $s$, the cardinality of the set obtained by removing $a$ from $s$ is strictly less than the cardinality of $s$, i.e., $|s \setminus \{a\}| < |s|$.
Finset.card_erase_le theorem
: #(s.erase a) ≤ #s
Full source
theorem card_erase_le : #(s.erase a)#s :=
  Multiset.card_erase_le
Non-Increasing Cardinality Under Element Removal
Informal description
For any finite set $s$ and any element $a$, the cardinality of the set obtained by removing $a$ from $s$ is less than or equal to the cardinality of $s$, i.e., $|s \setminus \{a\}| \leq |s|$.
Finset.pred_card_le_card_erase theorem
: #s - 1 ≤ #(s.erase a)
Full source
theorem pred_card_le_card_erase : #s - 1 ≤ #(s.erase a) := by
  by_cases h : a ∈ s
  · exact (card_erase_of_mem h).ge
  · rw [erase_eq_of_not_mem h]
    exact Nat.sub_le _ _
Lower Bound on Cardinality After Element Removal: $|s| - 1 \leq |s \setminus \{a\}|$
Informal description
For any finite set $s$ and any element $a$, the cardinality of $s$ minus one is less than or equal to the cardinality of the set obtained by removing $a$ from $s$, i.e., $|s| - 1 \leq |s \setminus \{a\}|$.
Finset.card_erase_eq_ite theorem
: #(s.erase a) = if a ∈ s then #s - 1 else #s
Full source
/-- If `a ∈ s` is known, see also `Finset.card_erase_of_mem` and `Finset.erase_eq_of_not_mem`. -/
theorem card_erase_eq_ite : #(s.erase a) = if a ∈ s then #s - 1 else #s :=
  Multiset.card_erase_eq_ite
Cardinality of Finite Set After Conditional Erasure: $|s \setminus \{a\}| = |s| - 1$ if $a \in s$, else $|s|$
Informal description
For any finite set $s$ and any element $a$, the cardinality of the set obtained by removing $a$ from $s$ is given by: $$|s \setminus \{a\}| = \begin{cases} |s| - 1 & \text{if } a \in s \\ |s| & \text{otherwise} \end{cases}$$
Finset.card_range theorem
(n : ℕ) : #(range n) = n
Full source
@[simp]
theorem card_range (n : ) : #(range n) = n :=
  Multiset.card_range n
Cardinality of the Finite Set $\{0, 1, \ldots, n-1\}$ is $n$
Informal description
For any natural number $n$, the cardinality of the finite set $\{0, 1, \ldots, n-1\}$ is equal to $n$, i.e., $\#(\text{range } n) = n$.
Finset.card_attach theorem
: #s.attach = #s
Full source
@[simp]
theorem card_attach : #s.attach = #s :=
  Multiset.card_attach
Cardinality Preservation under Finite Set Attachment
Informal description
For any finite set $s$, the cardinality of the set $\{x \mid x \in s\}$ (where each element is paired with a proof of its membership in $s$) is equal to the cardinality of $s$ itself, i.e., $\#s.\text{attach} = \#s$.
Multiset.card_toFinset theorem
: #m.toFinset = Multiset.card m.dedup
Full source
theorem Multiset.card_toFinset : #m.toFinset = Multiset.card m.dedup :=
  rfl
Cardinality of Deduplicated Multiset Equals Cardinality of Corresponding Finite Set
Informal description
For any multiset $m$ over a type $\alpha$, the cardinality of the finite set obtained by removing duplicates from $m$ equals the cardinality of the deduplicated multiset, i.e., $\#m.\operatorname{toFinset} = \operatorname{card}(m.\operatorname{dedup})$.
Multiset.toFinset_card_le theorem
: #m.toFinset ≤ Multiset.card m
Full source
theorem Multiset.toFinset_card_le : #m.toFinsetMultiset.card m :=
  card_le_card <| dedup_le _
Cardinality Inequality: $\#m.\operatorname{toFinset} \leq \operatorname{card}(m)$
Informal description
For any multiset $m$ over a type $\alpha$, the cardinality of the finite set obtained by removing duplicates from $m$ is less than or equal to the cardinality of $m$ itself, i.e., $\#m.\operatorname{toFinset} \leq \operatorname{card}(m)$.
Multiset.toFinset_card_of_nodup theorem
{m : Multiset α} (h : m.Nodup) : #m.toFinset = Multiset.card m
Full source
theorem Multiset.toFinset_card_of_nodup {m : Multiset α} (h : m.Nodup) :
    #m.toFinset = Multiset.card m :=
  congr_arg card <| Multiset.dedup_eq_self.mpr h
Cardinality Preservation in Deduplication of Nodup Multiset
Informal description
For any multiset $m$ over a type $\alpha$ with no duplicate elements (i.e., $\operatorname{Nodup}(m)$ holds), the cardinality of the finite set obtained by removing duplicates from $m$ equals the cardinality of $m$ itself, i.e., $\#m.\operatorname{toFinset} = \operatorname{card}(m)$.
Multiset.dedup_card_eq_card_iff_nodup theorem
{m : Multiset α} : card m.dedup = card m ↔ m.Nodup
Full source
theorem Multiset.dedup_card_eq_card_iff_nodup {m : Multiset α} :
    cardcard m.dedup = card m ↔ m.Nodup :=
  .trans ⟨fun h ↦ eq_of_le_of_card_le (dedup_le m) h.ge, congr_arg _⟩ dedup_eq_self
Cardinality Equality in Deduplication if and only if No Duplicates
Informal description
For any multiset $m$ over a type $\alpha$, the cardinality of the deduplicated multiset $\operatorname{dedup}(m)$ equals the cardinality of $m$ if and only if $m$ has no duplicate elements, i.e., $\operatorname{card}(\operatorname{dedup}(m)) = \operatorname{card}(m) \leftrightarrow \operatorname{Nodup}(m)$.
Multiset.toFinset_card_eq_card_iff_nodup theorem
{m : Multiset α} : #m.toFinset = card m ↔ m.Nodup
Full source
theorem Multiset.toFinset_card_eq_card_iff_nodup {m : Multiset α} :
    #m.toFinset#m.toFinset = card m ↔ m.Nodup := dedup_card_eq_card_iff_nodup
Cardinality Equality in Deduplication if and only if No Duplicates
Informal description
For any multiset $m$ over a type $\alpha$, the cardinality of the finite set obtained by removing duplicates from $m$ equals the cardinality of $m$ if and only if $m$ has no duplicate elements, i.e., $\#m.\operatorname{toFinset} = \operatorname{card}(m) \leftrightarrow \operatorname{Nodup}(m)$.
List.card_toFinset theorem
: #l.toFinset = l.dedup.length
Full source
theorem List.card_toFinset : #l.toFinset = l.dedup.length :=
  rfl
Cardinality of Finset from List Equals Length of Deduplicated List
Informal description
For any list $l$ with elements of type $\alpha$, the cardinality of the finite set obtained by converting $l$ to a `Finset` is equal to the length of the deduplicated version of $l$, i.e., $\#l.\text{toFinset} = \text{length}(\text{dedup}(l))$.
List.toFinset_card_le theorem
: #l.toFinset ≤ l.length
Full source
theorem List.toFinset_card_le : #l.toFinset ≤ l.length :=
  Multiset.toFinset_card_le ⟦l⟧
Cardinality Bound: $\#l.\text{toFinset} \leq \text{length}(l)$
Informal description
For any list $l$ with elements of type $\alpha$, the cardinality of the finite set obtained by converting $l$ to a `Finset` is less than or equal to the length of $l$, i.e., $\#l.\text{toFinset} \leq \text{length}(l)$.
List.toFinset_card_of_nodup theorem
{l : List α} (h : l.Nodup) : #l.toFinset = l.length
Full source
theorem List.toFinset_card_of_nodup {l : List α} (h : l.Nodup) : #l.toFinset = l.length :=
  Multiset.toFinset_card_of_nodup h
Cardinality Preservation in List-to-Finset Conversion for Nodup Lists
Informal description
For any list $l$ of type `List α` with no duplicate elements (i.e., $l$ satisfies `Nodup`), the cardinality of the finite set obtained by converting $l$ to a `Finset` equals the length of $l$, i.e., $\#l.\text{toFinset} = \text{length}(l)$.
Finset.length_toList theorem
(s : Finset α) : s.toList.length = #s
Full source
@[simp]
theorem length_toList (s : Finset α) : s.toList.length = #s := by
  rw [toList, ← Multiset.coe_card, Multiset.coe_toList, card_def]
List Length Equals Cardinality for Finite Sets
Informal description
For any finite set $s$ of type `Finset α`, the length of the list obtained by converting $s$ to a list equals the cardinality of $s$, i.e., $\text{length}(s.\text{toList}) = \#s$.
Finset.card_image_le theorem
[DecidableEq β] : #(s.image f) ≤ #s
Full source
theorem card_image_le [DecidableEq β] : #(s.image f)#s := by
  simpa only [card_map] using (s.1.map f).toFinset_card_le
Cardinality Inequality for Finite Set Images: $\#(f(s)) \leq \#s$
Informal description
For any finite set $s$ of type $\alpha$ and any function $f \colon \alpha \to \beta$, the cardinality of the image of $s$ under $f$ is less than or equal to the cardinality of $s$, i.e., $\#(f(s)) \leq \#s$.
Finset.card_image_of_injOn theorem
[DecidableEq β] (H : Set.InjOn f s) : #(s.image f) = #s
Full source
theorem card_image_of_injOn [DecidableEq β] (H : Set.InjOn f s) : #(s.image f) = #s := by
  simp only [card, image_val_of_injOn H, card_map]
Cardinality Preservation under Injective Image: $|f(s)| = |s|$ when $f$ is injective on $s$
Informal description
For any function $f \colon \alpha \to \beta$ that is injective on a finite set $s \subseteq \alpha$, the cardinality of the image $f(s)$ equals the cardinality of $s$, i.e., $|f(s)| = |s|$.
Finset.injOn_of_card_image_eq theorem
[DecidableEq β] (H : #(s.image f) = #s) : Set.InjOn f s
Full source
theorem injOn_of_card_image_eq [DecidableEq β] (H : #(s.image f) = #s) : Set.InjOn f s := by
  rw [card_def, card_def, image, toFinset] at H
  dsimp only at H
  have : (s.1.map f).dedup = s.1.map f := by
    refine Multiset.eq_of_le_of_card_le (Multiset.dedup_le _) ?_
    simp only [H, Multiset.card_map, le_rfl]
  rw [Multiset.dedup_eq_self] at this
  exact inj_on_of_nodup_map this
Injectivity from Cardinality Preservation of Finite Set Image
Informal description
For any finite set $s$ of type $\alpha$ and function $f : \alpha \to \beta$, if the cardinality of the image of $s$ under $f$ equals the cardinality of $s$ (i.e., $\#(f(s)) = \#s$), then $f$ is injective on $s$.
Finset.card_image_iff theorem
[DecidableEq β] : #(s.image f) = #s ↔ Set.InjOn f s
Full source
theorem card_image_iff [DecidableEq β] : #(s.image f)#(s.image f) = #s ↔ Set.InjOn f s :=
  ⟨injOn_of_card_image_eq, card_image_of_injOn⟩
Cardinality Equality of Finite Set Image iff Function is Injective on Set
Informal description
For any finite set $s \subseteq \alpha$ and function $f \colon \alpha \to \beta$, the cardinality of the image $f(s)$ equals the cardinality of $s$ if and only if $f$ is injective on $s$. In symbols: $$|f(s)| = |s| \leftrightarrow \text{InjOn}(f, s)$$ where $\text{InjOn}(f, s)$ means that $f$ is injective when restricted to $s$.
Finset.card_image_of_injective theorem
[DecidableEq β] (s : Finset α) (H : Injective f) : #(s.image f) = #s
Full source
theorem card_image_of_injective [DecidableEq β] (s : Finset α) (H : Injective f) :
    #(s.image f) = #s :=
  card_image_of_injOn fun _ _ _ _ h => H h
Cardinality Preservation under Injective Functions: $|f(s)| = |s|$ for injective $f$
Informal description
For any injective function $f \colon \alpha \to \beta$ and finite set $s \subseteq \alpha$, the cardinality of the image $f(s)$ equals the cardinality of $s$, i.e., $|f(s)| = |s|$.
Finset.fiber_card_ne_zero_iff_mem_image theorem
(s : Finset α) (f : α → β) [DecidableEq β] (y : β) : #(s.filter fun x ↦ f x = y) ≠ 0 ↔ y ∈ s.image f
Full source
theorem fiber_card_ne_zero_iff_mem_image (s : Finset α) (f : α → β) [DecidableEq β] (y : β) :
    #(s.filter fun x ↦ f x = y)#(s.filter fun x ↦ f x = y) ≠ 0#(s.filter fun x ↦ f x = y) ≠ 0 ↔ y ∈ s.image f := by
  rw [← Nat.pos_iff_ne_zero, card_pos, fiber_nonempty_iff_mem_image]
Nonzero Fiber Cardinality Characterization for Membership in Finite Set Image
Informal description
For any finite set $s \subseteq \alpha$, function $f \colon \alpha \to \beta$, and element $y \in \beta$, the fiber $\{x \in s \mid f(x) = y\}$ has nonzero cardinality if and only if $y$ belongs to the image of $s$ under $f$. In symbols: $$\#\{x \in s \mid f(x) = y\} \neq 0 \leftrightarrow y \in f(s)$$
Finset.card_filter_le_iff theorem
(s : Finset α) (P : α → Prop) [DecidablePred P] (n : ℕ) : #(s.filter P) ≤ n ↔ ∀ s' ⊆ s, n < #s' → ∃ a ∈ s', ¬P a
Full source
lemma card_filter_le_iff (s : Finset α) (P : α → Prop) [DecidablePred P] (n : ) :
    #(s.filter P)#(s.filter P) ≤ n ↔ ∀ s' ⊆ s, n < #s' → ∃ a ∈ s', ¬ P a :=
  (s.1.card_filter_le_iff P n).trans ⟨fun H s' hs' h ↦ H s'.1 (by aesop) h,
    fun H s' hs' h ↦ H ⟨s', nodup_of_le hs' s.2⟩ (fun _ hx ↦ Multiset.subset_of_le hs' hx) h⟩
Cardinality Bound of Filtered Finite Set via Subsets: $\#\{x \in s \mid P(x)\} \leq n \leftrightarrow \forall s' \subseteq s, n < \#s' \rightarrow \exists a \in s', \neg P(a)$
Informal description
For a finite set $s$ of type $\alpha$, a predicate $P$ on $\alpha$, and a natural number $n$, the cardinality of the filtered subset $\{x \in s \mid P(x)\}$ is at most $n$ if and only if for every subset $s' \subseteq s$ with cardinality greater than $n$, there exists an element $a \in s'$ such that $\neg P(a)$.
Finset.card_map theorem
(f : α ↪ β) : #(s.map f) = #s
Full source
@[simp]
theorem card_map (f : α ↪ β) : #(s.map f) = #s :=
  Multiset.card_map _ _
Cardinality Preservation under Injective Mapping of Finite Sets
Informal description
For any injective function embedding $f \colon \alpha \hookrightarrow \beta$ and any finite set $s$ of type $\alpha$, the cardinality of the image set $s.map\, f$ is equal to the cardinality of $s$, i.e., $\#(s.map\, f) = \#s$.
Finset.card_subtype theorem
(p : α → Prop) [DecidablePred p] (s : Finset α) : #(s.subtype p) = #(s.filter p)
Full source
@[simp]
theorem card_subtype (p : α → Prop) [DecidablePred p] (s : Finset α) :
    #(s.subtype p) = #(s.filter p) := by simp [Finset.subtype]
Cardinality Equality between Subtype and Filtered Finite Sets: $\#(s.\text{subtype}\, p) = \#(s.\text{filter}\, p)$
Informal description
For any finite set $s$ of type $\alpha$ and any decidable predicate $p$ on $\alpha$, the cardinality of the subtype $\{x \in s \mid p(x)\}$ is equal to the cardinality of the filtered set $\{x \in s \mid p(x)\}$, i.e., $\#(s.\text{subtype}\, p) = \#(s.\text{filter}\, p)$.
Finset.card_filter_le theorem
(s : Finset α) (p : α → Prop) [DecidablePred p] : #(s.filter p) ≤ #s
Full source
theorem card_filter_le (s : Finset α) (p : α → Prop) [DecidablePred p] :
    #(s.filter p)#s :=
  card_le_card <| filter_subset _ _
Cardinality Inequality for Filtered Finite Sets
Informal description
For any finite set $s$ of type $\alpha$ and any decidable predicate $p$ on $\alpha$, the cardinality of the filtered subset $s.\mathrm{filter}\,p$ is less than or equal to the cardinality of $s$, i.e., $\#(s.\mathrm{filter}\,p) \leq \#s$.
Finset.subset_iff_eq_of_card_le theorem
(h : #t ≤ #s) : s ⊆ t ↔ s = t
Full source
theorem subset_iff_eq_of_card_le (h : #t#s) : s ⊆ ts ⊆ t ↔ s = t :=
  ⟨fun hst => eq_of_subset_of_card_le hst h, Eq.subset'⟩
Subset Equals Equality for Finite Sets of Equal or Smaller Cardinality
Informal description
For any finite sets $s$ and $t$ of a type $\alpha$, if the cardinality of $t$ is less than or equal to the cardinality of $s$, then $s$ is a subset of $t$ if and only if $s$ equals $t$.
Finset.map_eq_of_subset theorem
{f : α ↪ α} (hs : s.map f ⊆ s) : s.map f = s
Full source
theorem map_eq_of_subset {f : α ↪ α} (hs : s.map f ⊆ s) : s.map f = s :=
  eq_of_subset_of_card_le hs (card_map _).ge
Fixed Point Property for Finite Sets under Injective Endomorphisms
Informal description
For any injective function $f \colon \alpha \hookrightarrow \alpha$ and any finite set $s \subseteq \alpha$, if the image of $s$ under $f$ is a subset of $s$, then the image equals $s$, i.e., $f(s) = s$.
Finset.card_filter_eq_iff theorem
{p : α → Prop} [DecidablePred p] : #(s.filter p) = #s ↔ ∀ x ∈ s, p x
Full source
theorem card_filter_eq_iff {p : α → Prop} [DecidablePred p] :
    #(s.filter p)#(s.filter p) = #s ↔ ∀ x ∈ s, p x := by
  rw [(card_filter_le s p).eq_iff_not_lt, not_lt, eq_iff_card_le_of_subset (filter_subset p s),
    filter_eq_self]
Characterization of Predicate Satisfaction via Filtered Set Cardinality: $\#(s \text{.filter } p) = \#s \leftrightarrow \forall x \in s, p(x)$
Informal description
For a finite set $s$ and a decidable predicate $p$, the cardinality of the filtered subset $\{x \in s \mid p(x)\}$ equals the cardinality of $s$ if and only if every element $x$ in $s$ satisfies $p(x)$, i.e., $\#\{x \in s \mid p(x)\} = \#s \leftrightarrow \forall x \in s, p(x)$.
Finset.card_filter_eq_zero_iff theorem
{p : α → Prop} [DecidablePred p] : #(s.filter p) = 0 ↔ ∀ x ∈ s, ¬p x
Full source
theorem card_filter_eq_zero_iff {p : α → Prop} [DecidablePred p] :
    #(s.filter p)#(s.filter p) = 0 ↔ ∀ x ∈ s, ¬ p x := by
  rw [card_eq_zero, filter_eq_empty_iff]
Empty Filtered Set Characterization via Cardinality: $\#\{x \in s \mid p(x)\} = 0 \leftrightarrow \forall x \in s, \neg p(x)$
Informal description
For a finite set $s$ and a decidable predicate $p$, the cardinality of the filtered set $\{x \in s \mid p(x)\}$ is zero if and only if no element of $s$ satisfies $p$, i.e., $\#\{x \in s \mid p(x)\} = 0 \leftrightarrow \forall x \in s, \neg p(x)$.
Finset.card_lt_card theorem
(h : s ⊂ t) : #s < #t
Full source
nonrec lemma card_lt_card (h : s ⊂ t) : #s < #t := card_lt_card <| val_lt_iff.2 h
Strict Subset Implies Strictly Smaller Cardinality: \( s \subset t \implies \#s < \#t \)
Informal description
For any two finite sets \( s \) and \( t \) of type \( \alpha \), if \( s \) is a strict subset of \( t \) (i.e., \( s \subset t \)), then the cardinality of \( s \) is strictly less than the cardinality of \( t \), i.e., \( \#s < \#t \).
Finset.card_strictMono theorem
: StrictMono (card : Finset α → ℕ)
Full source
lemma card_strictMono : StrictMono (card : Finset α → ) := fun _ _ ↦ card_lt_card
Strict Monotonicity of Finite Set Cardinality: $s \subset t \implies \#s < \#t$
Informal description
The cardinality function $\# : \text{Finset } \alpha \to \mathbb{N}$ is strictly monotone with respect to the subset partial order on finite sets and the usual order on natural numbers. That is, for any two finite sets $s, t \subseteq \alpha$, if $s \subset t$, then $\#s < \#t$.
Finset.card_eq_of_bijective theorem
(f : ∀ i, i < n → α) (hf : ∀ a ∈ s, ∃ i, ∃ h : i < n, f i h = a) (hf' : ∀ i (h : i < n), f i h ∈ s) (f_inj : ∀ i j (hi : i < n) (hj : j < n), f i hi = f j hj → i = j) : #s = n
Full source
theorem card_eq_of_bijective (f : ∀ i, i < n → α) (hf : ∀ a ∈ s, ∃ i, ∃ h : i < n, f i h = a)
    (hf' : ∀ i (h : i < n), f i h ∈ s)
    (f_inj : ∀ i j (hi : i < n) (hj : j < n), f i hi = f j hj → i = j) : #s = n := by
  classical
  have : s = (range n).attach.image fun i => f i.1 (mem_range.1 i.2) := by
    ext a
    suffices _ : a ∈ sa ∈ s ↔ ∃ (i : _) (hi : i ∈ range n), f i (mem_range.1 hi) = a by
      simpa only [mem_image, mem_attach, true_and, Subtype.exists]
    constructor
    · intro ha; obtain ⟨i, hi, rfl⟩ := hf a ha; use i, mem_range.2 hi
    · rintro ⟨i, hi, rfl⟩; apply hf'
  calc
    #s = #((range n).attach.image fun i => f i.1 (mem_range.1 i.2)) := by rw [this]
    _ = #(range n).attach := ?_
    _ = #(range n) := card_attach
    _ = n := card_range n
  apply card_image_of_injective
  intro ⟨i, hi⟩ ⟨j, hj⟩ eq
  exact Subtype.eq <| f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq
Cardinality via Bijective Correspondence: $\#s = n$ for Finite Set $s$ with Indexed Injection
Informal description
Let $s$ be a finite set of type $\alpha$ and $n$ a natural number. Suppose there exists a function $f \colon \{i \mid i < n\} \to \alpha$ such that: 1. For every $a \in s$, there exists $i < n$ with $f(i) = a$ (surjectivity onto $s$), 2. For every $i < n$, $f(i) \in s$ (image containment in $s$), 3. $f$ is injective on its domain. Then the cardinality of $s$ equals $n$, i.e., $\#s = n$.
Finset.card_bij theorem
(i : ∀ a ∈ s, β) (hi : ∀ a ha, i a ha ∈ t) (i_inj : ∀ a₁ ha₁ a₂ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂) (i_surj : ∀ b ∈ t, ∃ a ha, i a ha = b) : #s = #t
Full source
/-- Reorder a finset.

The difference with `Finset.card_bij'` is that the bijection is specified as a surjective injection,
rather than by an inverse function.

The difference with `Finset.card_nbij` is that the bijection is allowed to use membership of the
domain, rather than being a non-dependent function. -/
lemma card_bij (i : ∀ a ∈ s, β) (hi : ∀ a ha, i a ha ∈ t)
    (i_inj : ∀ a₁ ha₁ a₂ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂)
    (i_surj : ∀ b ∈ t, ∃ a ha, i a ha = b) : #s = #t := by
  classical
  calc
    #s = #s.attach := card_attach.symm
    _ = #(s.attach.image fun a ↦ i a.1 a.2) := Eq.symm ?_
    _ = #t := ?_
  · apply card_image_of_injective
    intro ⟨_, _⟩ ⟨_, _⟩ h
    simpa using i_inj _ _ _ _ h
  · congr 1
    ext b
    constructor <;> intro h
    · obtain ⟨_, _, rfl⟩ := mem_image.1 h; apply hi
    · obtain ⟨a, ha, rfl⟩ := i_surj b h; exact mem_image.2 ⟨⟨a, ha⟩, by simp⟩
Cardinality Equality via Bijection on Finite Sets
Informal description
Let $s$ and $t$ be finite sets of types $\alpha$ and $\beta$ respectively. Suppose there exists a function $i$ defined on elements of $s$ (with $i(a, h_a) \in \beta$ for each $a \in s$ and proof $h_a$ of membership) such that: 1. $i$ maps into $t$ (i.e., $i(a, h_a) \in t$ for all $a \in s$), 2. $i$ is injective (i.e., $i(a_1, h_{a_1}) = i(a_2, h_{a_2})$ implies $a_1 = a_2$), and 3. $i$ is surjective onto $t$ (i.e., for every $b \in t$, there exists $a \in s$ with $i(a, h_a) = b$). Then the cardinalities of $s$ and $t$ are equal, i.e., $|s| = |t|$.
Finset.card_bij' theorem
(i : ∀ a ∈ s, β) (j : ∀ a ∈ t, α) (hi : ∀ a ha, i a ha ∈ t) (hj : ∀ a ha, j a ha ∈ s) (left_inv : ∀ a ha, j (i a ha) (hi a ha) = a) (right_inv : ∀ a ha, i (j a ha) (hj a ha) = a) : #s = #t
Full source
/-- Reorder a finset.

The difference with `Finset.card_bij` is that the bijection is specified with an inverse, rather
than as a surjective injection.

The difference with `Finset.card_nbij'` is that the bijection and its inverse are allowed to use
membership of the domains, rather than being non-dependent functions. -/
lemma card_bij' (i : ∀ a ∈ s, β) (j : ∀ a ∈ t, α) (hi : ∀ a ha, i a ha ∈ t)
    (hj : ∀ a ha, j a ha ∈ s) (left_inv : ∀ a ha, j (i a ha) (hi a ha) = a)
    (right_inv : ∀ a ha, i (j a ha) (hj a ha) = a) : #s = #t := by
  refine card_bij i hi (fun a1 h1 a2 h2 eq ↦ ?_) (fun b hb ↦ ⟨_, hj b hb, right_inv b hb⟩)
  rw [← left_inv a1 h1, ← left_inv a2 h2]
  simp only [eq]
Cardinality Equality via Bijection with Explicit Inverse on Finite Sets
Informal description
Let $s$ and $t$ be finite sets of types $\alpha$ and $\beta$ respectively. Suppose there exist functions: - $i \colon \{(a, h_a) \mid a \in s\} \to \beta$ with $i(a, h_a) \in t$ for all $a \in s$, - $j \colon \{(b, h_b) \mid b \in t\} \to \alpha$ with $j(b, h_b) \in s$ for all $b \in t$, such that: 1. $j$ is a left inverse of $i$ (i.e., $j(i(a, h_a), h_{i(a, h_a)}) = a$ for all $a \in s$), 2. $i$ is a right inverse of $j$ (i.e., $i(j(b, h_b), h_{j(b, h_b)}) = b$ for all $b \in t$). Then the cardinalities of $s$ and $t$ are equal, i.e., $|s| = |t|$.
Finset.card_nbij theorem
(i : α → β) (hi : ∀ a ∈ s, i a ∈ t) (i_inj : (s : Set α).InjOn i) (i_surj : (s : Set α).SurjOn i t) : #s = #t
Full source
/-- Reorder a finset.

The difference with `Finset.card_nbij'` is that the bijection is specified as a surjective
injection, rather than by an inverse function.

The difference with `Finset.card_bij` is that the bijection is a non-dependent function, rather than
being allowed to use membership of the domain. -/
lemma card_nbij (i : α → β) (hi : ∀ a ∈ s, i a ∈ t) (i_inj : (s : Set α).InjOn i)
    (i_surj : (s : Set α).SurjOn i t) : #s = #t :=
  card_bij (fun a _ ↦ i a) hi i_inj (by simpa using i_surj)
Cardinality Equality via Non-Dependent Bijection on Finite Sets
Informal description
Let $s$ and $t$ be finite sets of types $\alpha$ and $\beta$ respectively. Given a function $i : \alpha \to \beta$ such that: 1. $i$ maps every element of $s$ into $t$ (i.e., for all $a \in s$, $i(a) \in t$), 2. $i$ is injective on $s$ (i.e., for any $x_1, x_2 \in s$, $i(x_1) = i(x_2)$ implies $x_1 = x_2$), and 3. $i$ is surjective from $s$ onto $t$ (i.e., for every $b \in t$, there exists $a \in s$ such that $i(a) = b$). Then the cardinalities of $s$ and $t$ are equal, i.e., $|s| = |t|$.
Finset.card_nbij' theorem
(i : α → β) (j : β → α) (hi : ∀ a ∈ s, i a ∈ t) (hj : ∀ a ∈ t, j a ∈ s) (left_inv : ∀ a ∈ s, j (i a) = a) (right_inv : ∀ a ∈ t, i (j a) = a) : #s = #t
Full source
/-- Reorder a finset.

The difference with `Finset.card_nbij` is that the bijection is specified with an inverse, rather
than as a surjective injection.

The difference with `Finset.card_bij'` is that the bijection and its inverse are non-dependent
functions, rather than being allowed to use membership of the domains.

The difference with `Finset.card_equiv` is that bijectivity is only required to hold on the domains,
rather than on the entire types. -/
lemma card_nbij' (i : α → β) (j : β → α) (hi : ∀ a ∈ s, i a ∈ t) (hj : ∀ a ∈ t, j a ∈ s)
    (left_inv : ∀ a ∈ s, j (i a) = a) (right_inv : ∀ a ∈ t, i (j a) = a) : #s = #t :=
  card_bij' (fun a _ ↦ i a) (fun b _ ↦ j b) hi hj left_inv right_inv
Cardinality Equality via Non-Dependent Bijection with Explicit Inverse on Finite Sets
Informal description
Let $s$ and $t$ be finite sets of types $\alpha$ and $\beta$ respectively. Given functions $i \colon \alpha \to \beta$ and $j \colon \beta \to \alpha$ such that: 1. $i$ maps every element of $s$ into $t$ (i.e., for all $a \in s$, $i(a) \in t$), 2. $j$ maps every element of $t$ into $s$ (i.e., for all $b \in t$, $j(b) \in s$), 3. $j$ is a left inverse of $i$ on $s$ (i.e., for all $a \in s$, $j(i(a)) = a$), 4. $i$ is a right inverse of $j$ on $t$ (i.e., for all $b \in t$, $i(j(b)) = b$). Then the cardinalities of $s$ and $t$ are equal, i.e., $|s| = |t|$.
Finset.card_equiv theorem
(e : α ≃ β) (hst : ∀ i, i ∈ s ↔ e i ∈ t) : #s = #t
Full source
/-- Specialization of `Finset.card_nbij'` that automatically fills in most arguments.

See `Fintype.card_equiv` for the version where `s` and `t` are `univ`. -/
lemma card_equiv (e : α ≃ β) (hst : ∀ i, i ∈ si ∈ s ↔ e i ∈ t) : #s = #t := by
  refine card_nbij' e e.symm ?_ ?_ ?_ ?_ <;> simp [hst]
Cardinality Preservation under Bijection: $|s| = |t|$ when $s$ and $t$ correspond via a bijection
Informal description
Let $s$ be a finite subset of $\alpha$ and $t$ a finite subset of $\beta$. Given a bijection $e \colon \alpha \simeq \beta$ such that for every element $i \in \alpha$, $i$ belongs to $s$ if and only if $e(i)$ belongs to $t$, then the cardinalities of $s$ and $t$ are equal, i.e., $|s| = |t|$.
Finset.card_bijective theorem
(e : α → β) (he : e.Bijective) (hst : ∀ i, i ∈ s ↔ e i ∈ t) : #s = #t
Full source
/-- Specialization of `Finset.card_nbij` that automatically fills in most arguments.

See `Fintype.card_bijective` for the version where `s` and `t` are `univ`. -/
lemma card_bijective (e : α → β) (he : e.Bijective) (hst : ∀ i, i ∈ si ∈ s ↔ e i ∈ t) :
    #s = #t := card_equiv (.ofBijective e he) hst
Cardinality Equality via Bijection: $|s| = |t|$ when $s$ and $t$ correspond via a bijection
Informal description
Let $s$ be a finite subset of $\alpha$ and $t$ a finite subset of $\beta$. Given a bijective function $e \colon \alpha \to \beta$ such that for every element $i \in \alpha$, $i$ belongs to $s$ if and only if $e(i)$ belongs to $t$, then the cardinalities of $s$ and $t$ are equal, i.e., $|s| = |t|$.
Finset.card_le_card_of_injOn theorem
(f : α → β) (hf : ∀ a ∈ s, f a ∈ t) (f_inj : (s : Set α).InjOn f) : #s ≤ #t
Full source
lemma card_le_card_of_injOn (f : α → β) (hf : ∀ a ∈ s, f a ∈ t) (f_inj : (s : Set α).InjOn f) :
    #s#t := by
  classical
  calc
    #s = #(s.image f) := (card_image_of_injOn f_inj).symm
    _  ≤ #t           := card_le_card <| image_subset_iff.2 hf
Cardinality Inequality under Injective Mapping: $\#s \leq \#t$ when $f$ is injective on $s$ and $f(s) \subseteq t$
Informal description
For any function $f \colon \alpha \to \beta$ such that $f$ maps every element of a finite set $s \subseteq \alpha$ into a finite set $t \subseteq \beta$ and $f$ is injective on $s$, the cardinality of $s$ is less than or equal to the cardinality of $t$, i.e., $\#s \leq \#t$.
Finset.card_le_card_of_injective theorem
{f : s → t} (hf : f.Injective) : #s ≤ #t
Full source
lemma card_le_card_of_injective {f : s → t} (hf : f.Injective) : #s#t := by
  rcases s.eq_empty_or_nonempty with rfl | ⟨a₀, ha₀⟩
  · simp
  · classical
    let f' : α → β := fun a => f (if ha : a ∈ s then ⟨a, ha⟩ else ⟨a₀, ha₀⟩)
    apply card_le_card_of_injOn f'
    · aesop
    · intro a₁ ha₁ a₂ ha₂ haa
      rw [mem_coe] at ha₁ ha₂
      simp only [f', ha₁, ha₂, ← Subtype.ext_iff] at haa
      exact Subtype.ext_iff.mp (hf haa)
Cardinality Inequality for Injective Functions on Finite Sets: $\#s \leq \#t$
Informal description
For any injective function $f$ from a finite set $s$ to a finite set $t$, the cardinality of $s$ is less than or equal to the cardinality of $t$, i.e., $\#s \leq \#t$.
Finset.card_le_card_of_surjOn theorem
(f : α → β) (hf : Set.SurjOn f s t) : #t ≤ #s
Full source
lemma card_le_card_of_surjOn (f : α → β) (hf : Set.SurjOn f s t) : #t#s := by
  classical unfold Set.SurjOn at hf; exact (card_le_card (mod_cast hf)).trans card_image_le
Cardinality Inequality for Surjective Functions on Finite Sets: $\#t \leq \#s$
Informal description
For any function $f \colon \alpha \to \beta$ and finite sets $s \subseteq \alpha$ and $t \subseteq \beta$, if $f$ is surjective from $s$ to $t$ (i.e., for every $y \in t$ there exists $x \in s$ such that $f(x) = y$), then the cardinality of $t$ is less than or equal to the cardinality of $s$, i.e., $\#t \leq \#s$.
Finset.exists_ne_map_eq_of_card_lt_of_maps_to theorem
{t : Finset β} (hc : #t < #s) {f : α → β} (hf : ∀ a ∈ s, f a ∈ t) : ∃ x ∈ s, ∃ y ∈ s, x ≠ y ∧ f x = f y
Full source
/-- If there are more pigeons than pigeonholes, then there are two pigeons in the same pigeonhole.
-/
theorem exists_ne_map_eq_of_card_lt_of_maps_to {t : Finset β} (hc : #t < #s) {f : α → β}
    (hf : ∀ a ∈ s, f a ∈ t) : ∃ x ∈ s, ∃ y ∈ s, x ≠ y ∧ f x = f y := by
  classical
  by_contra! hz
  refine hc.not_le (card_le_card_of_injOn f hf ?_)
  intro x hx y hy
  contrapose
  exact hz x hx y hy
Pigeonhole Principle for Finite Sets: $\#t < \#s$ implies $f$ is not injective on $s$
Informal description
Let $s$ and $t$ be finite sets of types $\alpha$ and $\beta$ respectively, and let $f \colon \alpha \to \beta$ be a function such that $f$ maps every element of $s$ into $t$. If the cardinality of $t$ is strictly less than the cardinality of $s$ (i.e., $\#t < \#s$), then there exist distinct elements $x, y \in s$ such that $f(x) = f(y)$.
Finset.le_card_of_inj_on_range theorem
(f : ℕ → α) (hf : ∀ i < n, f i ∈ s) (f_inj : ∀ i < n, ∀ j < n, f i = f j → i = j) : n ≤ #s
Full source
lemma le_card_of_inj_on_range (f :  → α) (hf : ∀ i < n, f i ∈ s)
    (f_inj : ∀ i < n, ∀ j < n, f i = f j → i = j) : n ≤ #s :=
  calc
    n = #(range n) := (card_range n).symm
    _ ≤ #s := card_le_card_of_injOn f (by simpa only [mem_range]) (by simpa)
Lower Bound on Cardinality via Injective Mapping: $n \leq \#s$ when $f$ injectively maps $\{0, \ldots, n-1\}$ into $s$
Informal description
For any function $f \colon \mathbb{N} \to \alpha$ and finite set $s \subseteq \alpha$, if $f$ maps the first $n$ natural numbers (i.e., $\{0, \ldots, n-1\}$) into $s$ and $f$ is injective on this set, then $n$ is less than or equal to the cardinality of $s$, i.e., $n \leq \#s$.
Finset.surjOn_of_injOn_of_card_le theorem
(f : α → β) (hf : Set.MapsTo f s t) (hinj : Set.InjOn f s) (hst : #t ≤ #s) : Set.SurjOn f s t
Full source
lemma surjOn_of_injOn_of_card_le (f : α → β) (hf : Set.MapsTo f s t) (hinj : Set.InjOn f s)
    (hst : #t#s) : Set.SurjOn f s t := by
  classical
  suffices s.image f = t by simp [← this, Set.SurjOn]
  have : s.image f ⊆ t := by aesop (add simp Finset.subset_iff)
  exact eq_of_subset_of_card_le this (hst.trans_eq (card_image_of_injOn hinj).symm)
Surjectivity from Injectivity and Cardinality Condition: $|t| \leq |s|$ implies $f$ is surjective when $f$ is injective on $s$ and $f(s) \subseteq t$
Informal description
Let $f : \alpha \to \beta$ be a function, $s$ be a finite subset of $\alpha$, and $t$ be a finite subset of $\beta$. If $f$ maps every element of $s$ into $t$ (i.e., $f(s) \subseteq t$), $f$ is injective on $s$, and the cardinality of $t$ is less than or equal to the cardinality of $s$ (i.e., $|t| \leq |s|$), then $f$ is surjective from $s$ to $t$ (i.e., $t \subseteq f(s)$).
Finset.surj_on_of_inj_on_of_card_le theorem
(f : ∀ a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t) (hinj : ∀ a₁ a₂ ha₁ ha₂, f a₁ ha₁ = f a₂ ha₂ → a₁ = a₂) (hst : #t ≤ #s) : ∀ b ∈ t, ∃ a ha, b = f a ha
Full source
lemma surj_on_of_inj_on_of_card_le (f : ∀ a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t)
    (hinj : ∀ a₁ a₂ ha₁ ha₂, f a₁ ha₁ = f a₂ ha₂ → a₁ = a₂) (hst : #t#s) :
    ∀ b ∈ t, ∃ a ha, b = f a ha := by
  let f' : s → β := fun a ↦ f a a.2
  have hinj' : Set.InjOn f' s.attach := fun x hx y hy hxy ↦ Subtype.ext (hinj _ _ x.2 y.2 hxy)
  have hmapsto' : Set.MapsTo f' s.attach t := fun x hx ↦ hf _ _
  intro b hb
  obtain ⟨a, ha, rfl⟩ := surjOn_of_injOn_of_card_le _ hmapsto' hinj' (by rwa [card_attach]) hb
  exact ⟨a, a.2, rfl⟩
Surjectivity from Injectivity and Cardinality Condition: $|t| \leq |s|$ implies $f$ is surjective when $f$ is injective on $s$ and $f(s) \subseteq t$
Informal description
Let $s$ be a finite subset of a type $\alpha$ and $t$ a finite subset of a type $\beta$. Given a function $f$ defined on elements of $s$ (i.e., for each $a \in s$, $f(a)$ is defined and belongs to $\beta$), suppose that: 1. $f$ maps every element of $s$ into $t$ (i.e., for all $a \in s$, $f(a) \in t$), 2. $f$ is injective on $s$ (i.e., for any $a_1, a_2 \in s$, if $f(a_1) = f(a_2)$ then $a_1 = a_2$), and 3. The cardinality of $t$ is less than or equal to that of $s$ (i.e., $|t| \leq |s|$). Then $f$ is surjective from $s$ to $t$, meaning that for every $b \in t$, there exists some $a \in s$ such that $b = f(a)$.
Finset.injOn_of_surjOn_of_card_le theorem
(f : α → β) (hf : Set.MapsTo f s t) (hsurj : Set.SurjOn f s t) (hst : #s ≤ #t) : Set.InjOn f s
Full source
lemma injOn_of_surjOn_of_card_le (f : α → β) (hf : Set.MapsTo f s t) (hsurj : Set.SurjOn f s t)
    (hst : #s#t) : Set.InjOn f s := by
  classical
  have : s.image f = t := Finset.coe_injective <| by simp [hsurj.image_eq_of_mapsTo hf]
  have : #(s.image f) = #t := by rw [this]
  have : #(s.image f)#s := card_image_le
  rw [← card_image_iff]
  omega
Injectivity from Surjectivity and Cardinality Condition: $|s| \leq |t|$ implies $f$ is injective when $f$ is surjective from $s$ to $t$ and $f(s) \subseteq t$
Informal description
Let $f : \alpha \to \beta$ be a function, $s$ be a finite subset of $\alpha$, and $t$ be a finite subset of $\beta$. If $f$ maps every element of $s$ into $t$ (i.e., $f(s) \subseteq t$), $f$ is surjective from $s$ to $t$ (i.e., $t \subseteq f(s)$), and the cardinality of $s$ is less than or equal to the cardinality of $t$ (i.e., $|s| \leq |t|$), then $f$ is injective on $s$.
Finset.inj_on_of_surj_on_of_card_le theorem
(f : ∀ a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t) (hsurj : ∀ b ∈ t, ∃ a ha, f a ha = b) (hst : #s ≤ #t) ⦃a₁⦄ (ha₁ : a₁ ∈ s) ⦃a₂⦄ (ha₂ : a₂ ∈ s) (ha₁a₂ : f a₁ ha₁ = f a₂ ha₂) : a₁ = a₂
Full source
theorem inj_on_of_surj_on_of_card_le (f : ∀ a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t)
    (hsurj : ∀ b ∈ t, ∃ a ha, f a ha = b) (hst : #s#t) ⦃a₁⦄ (ha₁ : a₁ ∈ s) ⦃a₂⦄
    (ha₂ : a₂ ∈ s) (ha₁a₂ : f a₁ ha₁ = f a₂ ha₂) : a₁ = a₂ := by
  let f' : s → β := fun a ↦ f a a.2
  have hsurj' : Set.SurjOn f' s.attach t := fun x hx ↦ by simpa [f'] using hsurj x hx
  have hinj' := injOn_of_surjOn_of_card_le f' (fun x hx ↦ hf _ _) hsurj' (by simpa)
  exact congrArg Subtype.val (@hinj' ⟨a₁, ha₁⟩ (by simp) ⟨a₂, ha₂⟩ (by simp) ha₁a₂)
Injectivity from Surjectivity and Cardinality Inequality: $|s| \leq |t|$ implies $f$ is injective when $f$ is surjective from $s$ to $t$
Informal description
Let $s$ be a finite subset of a type $\alpha$, $t$ a finite subset of a type $\beta$, and $f$ a function defined on $s$ such that $f(a) \in t$ for all $a \in s$. If $f$ is surjective from $s$ to $t$ (i.e., for every $b \in t$ there exists $a \in s$ with $f(a) = b$) and the cardinality of $s$ is less than or equal to the cardinality of $t$ (i.e., $|s| \leq |t|$), then $f$ is injective on $s$ (i.e., for any $a_1, a_2 \in s$, if $f(a_1) = f(a_2)$ then $a_1 = a_2$).
Finset.card_disjUnion theorem
(s t : Finset α) (h) : #(s.disjUnion t h) = #s + #t
Full source
@[simp]
theorem card_disjUnion (s t : Finset α) (h) : #(s.disjUnion t h) = #s + #t :=
  Multiset.card_add _ _
Cardinality of Disjoint Union: $\#(s \sqcup t) = \#s + \#t$
Informal description
For any two finite sets $s$ and $t$ of type $\alpha$ that are disjoint (as witnessed by $h$), the cardinality of their disjoint union $s \sqcup t$ equals the sum of their individual cardinalities, i.e., $\#(s \sqcup t) = \#s + \#t$.
Finset.card_union_add_card_inter theorem
(s t : Finset α) : #(s ∪ t) + #(s ∩ t) = #s + #t
Full source
theorem card_union_add_card_inter (s t : Finset α) :
    #(s ∪ t) + #(s ∩ t) = #s + #t :=
  Finset.induction_on t (by simp) fun a r har h => by by_cases a ∈ s <;>
    simp [*, ← Nat.add_assoc, Nat.add_right_comm _ 1]
Cardinality Sum Identity for Union and Intersection of Finite Sets
Informal description
For any two finite sets $s$ and $t$ of elements of type $\alpha$, the sum of the cardinalities of their union and intersection equals the sum of their individual cardinalities, i.e., $$ \#(s \cup t) + \#(s \cap t) = \#s + \#t. $$
Finset.card_inter_add_card_union theorem
(s t : Finset α) : #(s ∩ t) + #(s ∪ t) = #s + #t
Full source
theorem card_inter_add_card_union (s t : Finset α) :
    #(s ∩ t) + #(s ∪ t) = #s + #t := by rw [Nat.add_comm, card_union_add_card_inter]
Cardinality Sum Identity for Intersection and Union of Finite Sets
Informal description
For any two finite sets $s$ and $t$ of elements of type $\alpha$, the sum of the cardinalities of their intersection and union equals the sum of their individual cardinalities, i.e., $$ \#(s \cap t) + \#(s \cup t) = \#s + \#t. $$
Finset.card_union theorem
(s t : Finset α) : #(s ∪ t) = #s + #t - #(s ∩ t)
Full source
lemma card_union (s t : Finset α) : #(s ∪ t) = #s + #t - #(s ∩ t) := by
  rw [← card_union_add_card_inter, Nat.add_sub_cancel]
Cardinality of Union of Finite Sets: $\#(s \cup t) = \#s + \#t - \#(s \cap t)$
Informal description
For any two finite sets $s$ and $t$ of elements of type $\alpha$, the cardinality of their union is given by: $$ \#(s \cup t) = \#s + \#t - \#(s \cap t). $$
Finset.card_inter theorem
(s t : Finset α) : #(s ∩ t) = #s + #t - #(s ∪ t)
Full source
lemma card_inter (s t : Finset α) : #(s ∩ t) = #s + #t - #(s ∪ t) := by
  rw [← card_inter_add_card_union, Nat.add_sub_cancel]
Cardinality of Intersection of Finite Sets: $\#(s \cap t) = \#s + \#t - \#(s \cup t)$
Informal description
For any two finite sets $s$ and $t$ of elements of type $\alpha$, the cardinality of their intersection is given by: $$ \#(s \cap t) = \#s + \#t - \#(s \cup t). $$
Finset.card_union_le theorem
(s t : Finset α) : #(s ∪ t) ≤ #s + #t
Full source
theorem card_union_le (s t : Finset α) : #(s ∪ t)#s + #t :=
  card_union_add_card_inter s t ▸ Nat.le_add_right _ _
Cardinality Bound for Union of Finite Sets
Informal description
For any two finite sets $s$ and $t$ of elements of type $\alpha$, the cardinality of their union satisfies the inequality $$ \#(s \cup t) \leq \#s + \#t. $$
Finset.card_union_eq_card_add_card theorem
: #(s ∪ t) = #s + #t ↔ Disjoint s t
Full source
lemma card_union_eq_card_add_card : #(s ∪ t)#(s ∪ t) = #s + #t ↔ Disjoint s t := by
  rw [← card_union_add_card_inter]; simp [disjoint_iff_inter_eq_empty]
Cardinality of Union Equals Sum of Cardinalities if and only if Sets are Disjoint
Informal description
For any two finite sets $s$ and $t$ of elements of type $\alpha$, the cardinality of their union equals the sum of their individual cardinalities if and only if $s$ and $t$ are disjoint, i.e., $$ \#(s \cup t) = \#s + \#t \leftrightarrow s \cap t = \emptyset. $$
Finset.card_sdiff theorem
(h : s ⊆ t) : #(t \ s) = #t - #s
Full source
theorem card_sdiff (h : s ⊆ t) : #(t \ s) = #t - #s := by
  suffices #(t \ s) = #(t \ s ∪ s) - #s by rwa [sdiff_union_of_subset h] at this
  rw [card_union_of_disjoint sdiff_disjoint, Nat.add_sub_cancel_right]
Cardinality of Set Difference: $\#(t \setminus s) = \#t - \#s$ when $s \subseteq t$
Informal description
For any finite sets $s$ and $t$ of elements of type $\alpha$, if $s$ is a subset of $t$, then the cardinality of the set difference $t \setminus s$ equals the difference of the cardinalities of $t$ and $s$, i.e., $$ \#(t \setminus s) = \#t - \#s. $$
Finset.card_sdiff_add_card_eq_card theorem
{s t : Finset α} (h : s ⊆ t) : #(t \ s) + #s = #t
Full source
theorem card_sdiff_add_card_eq_card {s t : Finset α} (h : s ⊆ t) : #(t \ s) + #s = #t :=
  ((Nat.sub_eq_iff_eq_add (card_le_card h)).mp (card_sdiff h).symm).symm
Cardinality Decomposition of Set Difference: $\#(t \setminus s) + \#s = \#t$ when $s \subseteq t$
Informal description
For any finite sets $s$ and $t$ of elements of type $\alpha$, if $s$ is a subset of $t$ (i.e., $s \subseteq t$), then the sum of the cardinality of the set difference $t \setminus s$ and the cardinality of $s$ equals the cardinality of $t$, i.e., $$ \#(t \setminus s) + \#s = \#t. $$
Finset.le_card_sdiff theorem
(s t : Finset α) : #t - #s ≤ #(t \ s)
Full source
theorem le_card_sdiff (s t : Finset α) : #t - #s#(t \ s) :=
  calc
    #t - #s#t - #(s ∩ t) :=
      Nat.sub_le_sub_left (card_le_card inter_subset_left) _
    _ = #(t \ (s ∩ t)) := (card_sdiff inter_subset_right).symm
    _ ≤ #(t \ s) := by rw [sdiff_inter_self_right t s]
Cardinality Difference Inequality: $\#t - \#s \leq \#(t \setminus s)$
Informal description
For any two finite sets $s$ and $t$ of elements of type $\alpha$, the difference between the cardinality of $t$ and the cardinality of $s$ is less than or equal to the cardinality of the set difference $t \setminus s$, i.e., $$ \#t - \#s \leq \#(t \setminus s). $$
Finset.card_le_card_sdiff_add_card theorem
: #s ≤ #(s \ t) + #t
Full source
theorem card_le_card_sdiff_add_card : #s#(s \ t) + #t :=
  Nat.sub_le_iff_le_add.1 <| le_card_sdiff _ _
Cardinality Inequality for Set Difference and Union: $\#s \leq \#(s \setminus t) + \#t$
Informal description
For any two finite sets $s$ and $t$ of elements of type $\alpha$, the cardinality of $s$ is less than or equal to the sum of the cardinality of the set difference $s \setminus t$ and the cardinality of $t$, i.e., $$ \#s \leq \#(s \setminus t) + \#t. $$
Finset.card_sdiff_add_card theorem
(s t : Finset α) : #(s \ t) + #t = #(s ∪ t)
Full source
theorem card_sdiff_add_card (s t : Finset α) : #(s \ t) + #t = #(s ∪ t) := by
  rw [← card_union_of_disjoint sdiff_disjoint, sdiff_union_self_eq_union]
Cardinality Identity for Union and Difference: $\#(s \setminus t) + \#t = \#(s \cup t)$
Informal description
For any two finite sets $s$ and $t$ of type $\alpha$, the sum of the cardinality of the set difference $s \setminus t$ and the cardinality of $t$ equals the cardinality of the union $s \cup t$, i.e., $\#(s \setminus t) + \#t = \#(s \cup t)$.
Finset.card_sdiff_comm theorem
(h : #s = #t) : #(s \ t) = #(t \ s)
Full source
lemma card_sdiff_comm (h : #s = #t) : #(s \ t) = #(t \ s) :=
  Nat.add_right_cancel (m := #t) <| by
    simp_rw [card_sdiff_add_card, ← h, card_sdiff_add_card, union_comm]
Symmetric Cardinality of Set Differences: $\#(s \setminus t) = \#(t \setminus s)$ when $\#s = \#t$
Informal description
For any two finite sets $s$ and $t$ of type $\alpha$ with equal cardinality (i.e., $\#s = \#t$), the cardinality of the set difference $s \setminus t$ is equal to the cardinality of $t \setminus s$, i.e., $\#(s \setminus t) = \#(t \setminus s)$.
Finset.sdiff_nonempty_of_card_lt_card theorem
(h : #s < #t) : (t \ s).Nonempty
Full source
theorem sdiff_nonempty_of_card_lt_card (h : #s < #t) : (t \ s).Nonempty := by
  rw [nonempty_iff_ne_empty, Ne, sdiff_eq_empty_iff_subset]
  exact fun h' ↦ h.not_le (card_le_card h')
Nonempty Difference from Cardinality Inequality: $\#s < \#t \implies t \setminus s \neq \emptyset$
Informal description
For any two finite sets $s$ and $t$ of type $\alpha$, if the cardinality of $s$ is strictly less than the cardinality of $t$ (i.e., $\#s < \#t$), then the set difference $t \setminus s$ is nonempty.
Finset.exists_mem_not_mem_of_card_lt_card theorem
(h : #s < #t) : ∃ e, e ∈ t ∧ e ∉ s
Full source
theorem exists_mem_not_mem_of_card_lt_card (h : #s < #t) : ∃ e, e ∈ t ∧ e ∉ s := by
  classical simpa [Finset.Nonempty] using sdiff_nonempty_of_card_lt_card h
Existence of Element in Larger Finite Set Not in Smaller Set: $\#s < \#t \implies \exists e \in t \setminus s$
Informal description
For any two finite sets $s$ and $t$ of type $\alpha$, if the cardinality of $s$ is strictly less than the cardinality of $t$ (i.e., $\#s < \#t$), then there exists an element $e$ such that $e \in t$ and $e \notin s$.
Finset.card_sdiff_add_card_inter theorem
(s t : Finset α) : #(s \ t) + #(s ∩ t) = #s
Full source
@[simp]
lemma card_sdiff_add_card_inter (s t : Finset α) :
    #(s \ t) + #(s ∩ t) = #s := by
  rw [← card_union_of_disjoint (disjoint_sdiff_inter _ _), sdiff_union_inter]
Cardinality Decomposition: $\#(s \setminus t) + \#(s \cap t) = \#s$
Informal description
For any two finite sets $s$ and $t$ of a type $\alpha$, the sum of the cardinalities of the set difference $s \setminus t$ and the intersection $s \cap t$ equals the cardinality of $s$, i.e., $\#(s \setminus t) + \#(s \cap t) = \#s$.
Finset.card_inter_add_card_sdiff theorem
(s t : Finset α) : #(s ∩ t) + #(s \ t) = #s
Full source
@[simp]
lemma card_inter_add_card_sdiff (s t : Finset α) :
    #(s ∩ t) + #(s \ t) = #s := by
  rw [Nat.add_comm, card_sdiff_add_card_inter]
Cardinality Decomposition: $\#(s \cap t) + \#(s \setminus t) = \#s$
Informal description
For any two finite sets $s$ and $t$ of a type $\alpha$, the sum of the cardinalities of the intersection $s \cap t$ and the set difference $s \setminus t$ equals the cardinality of $s$, i.e., $\#(s \cap t) + \#(s \setminus t) = \#s$.
Finset.inter_nonempty_of_card_lt_card_add_card theorem
(hts : t ⊆ s) (hus : u ⊆ s) (hstu : #s < #t + #u) : (t ∩ u).Nonempty
Full source
/-- **Pigeonhole principle** for two finsets inside an ambient finset. -/
theorem inter_nonempty_of_card_lt_card_add_card (hts : t ⊆ s) (hus : u ⊆ s)
    (hstu : #s < #t + #u) : (t ∩ u).Nonempty := by
  contrapose! hstu
  calc
    _ = #(t ∪ u) := by simp [← card_union_add_card_inter, not_nonempty_iff_eq_empty.1 hstu]
    _ ≤ #s := by gcongr; exact union_subset hts hus
Pigeonhole Principle for Finite Sets: $\#s < \#t + \#u \implies t \cap u \neq \emptyset$
Informal description
Let $s$, $t$, and $u$ be finite subsets of a type $\alpha$ such that $t \subseteq s$ and $u \subseteq s$. If the cardinality of $s$ is strictly less than the sum of the cardinalities of $t$ and $u$, then the intersection $t \cap u$ is nonempty.
Finset.filter_card_add_filter_neg_card_eq_card theorem
(p : α → Prop) [DecidablePred p] [∀ x, Decidable (¬p x)] : #(s.filter p) + #(s.filter fun a ↦ ¬p a) = #s
Full source
theorem filter_card_add_filter_neg_card_eq_card
    (p : α → Prop) [DecidablePred p] [∀ x, Decidable (¬p x)] :
    #(s.filter p) + #(s.filter fun a ↦ ¬ p a) = #s := by
  classical
  rw [← card_union_of_disjoint (disjoint_filter_filter_neg _ _ _), filter_union_filter_neg_eq]
Cardinality Sum of Filtered Set and its Complement
Informal description
For any finite set $s$ of type $\alpha$ and any decidable predicate $p : \alpha \to \text{Prop}$, the sum of the cardinalities of the subsets $\{a \in s \mid p(a)\}$ and $\{a \in s \mid \neg p(a)\}$ equals the cardinality of $s$. That is, \[ \#\{a \in s \mid p(a)\} + \#\{a \in s \mid \neg p(a)\} = \#s. \]
Finset.exists_subsuperset_card_eq theorem
(hst : s ⊆ t) (hsn : #s ≤ n) (hnt : n ≤ #t) : ∃ u, s ⊆ u ∧ u ⊆ t ∧ #u = n
Full source
/-- Given a subset `s` of a set `t`, of sizes at most and at least `n` respectively, there exists a
set `u` of size `n` which is both a superset of `s` and a subset of `t`. -/
lemma exists_subsuperset_card_eq (hst : s ⊆ t) (hsn : #s ≤ n) (hnt : n ≤ #t) :
    ∃ u, s ⊆ u ∧ u ⊆ t ∧ #u = n := by
  classical
  refine Nat.decreasingInduction' ?_ hnt ⟨t, by simp [hst]⟩
  intro k _ hnk ⟨u, hu₁, hu₂, hu₃⟩
  obtain ⟨a, ha⟩ : (u \ s).Nonempty := by rw [← card_pos, card_sdiff hu₁]; omega
  simp only [mem_sdiff] at ha
  exact ⟨u.erase a, by simp [subset_erase, erase_subset_iff_of_mem (hu₂ _), *]⟩
Existence of Intermediate-Sized Superset: $s \subseteq u \subseteq t$ with $|u|=n$ when $|s| \leq n \leq |t|$
Informal description
Given finite sets $s$ and $t$ with $s \subseteq t$, and a natural number $n$ satisfying $\#s \leq n \leq \#t$, there exists a finite set $u$ such that $s \subseteq u \subseteq t$ and $\#u = n$.
Finset.exists_subset_card_eq theorem
(hns : n ≤ #s) : ∃ t ⊆ s, #t = n
Full source
/-- We can shrink a set to any smaller size. -/
lemma exists_subset_card_eq (hns : n ≤ #s) : ∃ t ⊆ s, #t = n := by
  simpa using exists_subsuperset_card_eq s.empty_subset (by simp) hns
Existence of Subset with Given Cardinality: $n \leq |s|$ implies $\exists t \subseteq s, |t| = n$
Informal description
For any finite set $s$ and any natural number $n$ such that $n \leq \#s$, there exists a subset $t \subseteq s$ with cardinality $\#t = n$.
Finset.le_card_iff_exists_subset_card theorem
: n ≤ #s ↔ ∃ t ⊆ s, #t = n
Full source
theorem le_card_iff_exists_subset_card : n ≤ #s ↔ ∃ t ⊆ s, #t = n := by
  refine ⟨fun h => ?_, fun ⟨t, hst, ht⟩ => ht ▸ card_le_card hst⟩
  exact exists_subset_card_eq h
Cardinality Inequality Characterization: $n \leq |s|$ iff $\exists t \subseteq s, |t| = n$
Informal description
For any finite set $s$ and natural number $n$, the inequality $n \leq \#s$ holds if and only if there exists a subset $t \subseteq s$ with cardinality $\#t = n$.
Finset.exists_subset_or_subset_of_two_mul_lt_card theorem
[DecidableEq α] {X Y : Finset α} {n : ℕ} (hXY : 2 * n < #(X ∪ Y)) : ∃ C : Finset α, n < #C ∧ (C ⊆ X ∨ C ⊆ Y)
Full source
theorem exists_subset_or_subset_of_two_mul_lt_card [DecidableEq α] {X Y : Finset α} {n : }
    (hXY : 2 * n < #(X ∪ Y)) : ∃ C : Finset α, n < #C ∧ (C ⊆ X ∨ C ⊆ Y) := by
  have h₁ : #(X ∩ (Y \ X)) = 0 := Finset.card_eq_zero.mpr (Finset.inter_sdiff_self X Y)
  have h₂ : #(X ∪ Y) = #X + #(Y \ X) := by
    rw [← card_union_add_card_inter X (Y \ X), Finset.union_sdiff_self_eq_union, h₁, Nat.add_zero]
  rw [h₂, Nat.two_mul] at hXY
  obtain h | h : n < #X ∨ n < #(Y \ X) := by contrapose! hXY; omega
  · exact ⟨X, h, Or.inl (Finset.Subset.refl X)⟩
  · exact ⟨Y \ X, h, Or.inr sdiff_subset⟩
Pigeonhole Principle for Finite Sets: Existence of Large Subset in Union
Informal description
Let $X$ and $Y$ be finite sets of type $\alpha$ with a decidable equality, and let $n$ be a natural number. If $2n$ is strictly less than the cardinality of $X \cup Y$, then there exists a subset $C \subseteq X \cup Y$ such that $n < \#C$ and either $C \subseteq X$ or $C \subseteq Y$.
Finset.exists_eq_insert_iff theorem
[DecidableEq α] {s t : Finset α} : (∃ a ∉ s, insert a s = t) ↔ s ⊆ t ∧ #s + 1 = #t
Full source
theorem exists_eq_insert_iff [DecidableEq α] {s t : Finset α} :
    (∃ a ∉ s, insert a s = t) ↔ s ⊆ t ∧ #s + 1 = #t := by
  constructor
  · rintro ⟨a, ha, rfl⟩
    exact ⟨subset_insert _ _, (card_insert_of_not_mem ha).symm⟩
  · rintro ⟨hst, h⟩
    obtain ⟨a, ha⟩ : ∃ a, t \ s = {a} :=
      card_eq_one.1 (by rw [card_sdiff hst, ← h, Nat.add_sub_cancel_left])
    refine
      ⟨a, fun hs => (?_ : a ∉ {a}) <| mem_singleton_self _, by
        rw [insert_eq, ← ha, sdiff_union_of_subset hst]⟩
    rw [← ha]
    exact not_mem_sdiff_of_mem_right hs
Characterization of Insertion via Subset and Cardinality: $t = s \cup \{a\}$ iff $s \subseteq t$ and $\#t = \#s + 1$
Informal description
For any finite sets $s$ and $t$ of elements of type $\alpha$ with decidable equality, there exists an element $a \notin s$ such that the insertion of $a$ into $s$ equals $t$ if and only if $s$ is a subset of $t$ and the cardinality of $t$ is one more than the cardinality of $s$, i.e., $$ (\exists a \notin s,\ \text{insert } a s = t) \leftrightarrow (s \subseteq t \land \#s + 1 = \#t). $$
Finset.card_le_one theorem
: #s ≤ 1 ↔ ∀ a ∈ s, ∀ b ∈ s, a = b
Full source
theorem card_le_one : #s#s ≤ 1 ↔ ∀ a ∈ s, ∀ b ∈ s, a = b := by
  obtain rfl | ⟨x, hx⟩ := s.eq_empty_or_nonempty
  · simp
  refine (Nat.succ_le_of_lt (card_pos.2 ⟨x, hx⟩)).le_iff_eq.trans (card_eq_one.trans ⟨?_, ?_⟩)
  · rintro ⟨y, rfl⟩
    simp
  · exact fun h => ⟨x, eq_singleton_iff_unique_mem.2 ⟨hx, fun y hy => h _ hy _ hx⟩⟩
Finite Set Cardinality at Most One Characterizes Equality of Elements
Informal description
For any finite set $s$, the cardinality of $s$ is at most 1 if and only if all elements in $s$ are equal, i.e., $\#s \leq 1 \leftrightarrow \forall a \in s, \forall b \in s, a = b$.
Finset.card_le_one_iff theorem
: #s ≤ 1 ↔ ∀ {a b}, a ∈ s → b ∈ s → a = b
Full source
theorem card_le_one_iff : #s#s ≤ 1 ↔ ∀ {a b}, a ∈ s → b ∈ s → a = b := by
  rw [card_le_one]
  tauto
Finite Set Has Cardinality ≤1 iff All Elements Are Equal
Informal description
For any finite set $s$, the cardinality of $s$ is at most 1 if and only if any two elements $a, b \in s$ are equal, i.e., $\#s \leq 1 \leftrightarrow \forall a b \in s, a = b$.
Finset.card_le_one_iff_subset_singleton theorem
[Nonempty α] : #s ≤ 1 ↔ ∃ x : α, s ⊆ { x }
Full source
theorem card_le_one_iff_subset_singleton [Nonempty α] : #s#s ≤ 1 ↔ ∃ x : α, s ⊆ {x} := by
  refine ⟨fun H => ?_, ?_⟩
  · obtain rfl | ⟨x, hx⟩ := s.eq_empty_or_nonempty
    · exact ⟨Classical.arbitrary α, empty_subset _⟩
    · exact ⟨x, fun y hy => by rw [card_le_one.1 H y hy x hx, mem_singleton]⟩
  · rintro ⟨x, hx⟩
    rw [← card_singleton x]
    exact card_le_card hx
Finite Set Cardinality at Most One Characterizes Subset of Singleton: $\#s \leq 1 \leftrightarrow \exists x, s \subseteq \{x\}$
Informal description
For a nonempty type $\alpha$ and a finite set $s$ of elements in $\alpha$, the cardinality of $s$ is at most 1 if and only if there exists an element $x \in \alpha$ such that $s$ is a subset of the singleton set $\{x\}$. In other words, $\#s \leq 1 \leftrightarrow \exists x \in \alpha, s \subseteq \{x\}$.
Finset.exists_mem_ne theorem
(hs : 1 < #s) (a : α) : ∃ b ∈ s, b ≠ a
Full source
lemma exists_mem_ne (hs : 1 < #s) (a : α) : ∃ b ∈ s, b ≠ a := by
  have : Nonempty α := ⟨a⟩
  by_contra!
  exact hs.not_le (card_le_one_iff_subset_singleton.2 ⟨a, subset_singleton_iff'.2 this⟩)
Existence of Distinct Element in Finite Set with Cardinality Greater Than One
Informal description
For any finite set $s$ with cardinality greater than 1 (i.e., $\#s > 1$) and any element $a \in s$, there exists an element $b \in s$ such that $b \neq a$.
Finset.card_le_one_of_subsingleton theorem
[Subsingleton α] (s : Finset α) : #s ≤ 1
Full source
/-- A `Finset` of a subsingleton type has cardinality at most one. -/
theorem card_le_one_of_subsingleton [Subsingleton α] (s : Finset α) : #s ≤ 1 :=
  Finset.card_le_one_iff.2 fun {_ _ _ _} => Subsingleton.elim _ _
Finite Set Cardinality Bound for Subsingleton Types
Informal description
For any finite set $s$ of a subsingleton type $\alpha$, the cardinality of $s$ is at most 1, i.e., $\#s \leq 1$.
Finset.one_lt_card theorem
: 1 < #s ↔ ∃ a ∈ s, ∃ b ∈ s, a ≠ b
Full source
theorem one_lt_card : 1 < #s ↔ ∃ a ∈ s, ∃ b ∈ s, a ≠ b := by
  rw [← not_iff_not]
  push_neg
  exact card_le_one
Finite Set Cardinality Greater Than One Characterizes Existence of Distinct Elements
Informal description
For a finite set $s$, the cardinality of $s$ is greater than 1 if and only if there exist distinct elements $a$ and $b$ in $s$, i.e., $1 < \#s \leftrightarrow \exists a \in s, \exists b \in s, a \neq b$.
Finset.one_lt_card_iff theorem
: 1 < #s ↔ ∃ a b, a ∈ s ∧ b ∈ s ∧ a ≠ b
Full source
theorem one_lt_card_iff : 1 < #s ↔ ∃ a b, a ∈ s ∧ b ∈ s ∧ a ≠ b := by
  rw [one_lt_card]
  simp only [exists_prop, exists_and_left]
Characterization of finite sets with cardinality greater than one
Informal description
For any finite set $s$, the cardinality of $s$ is greater than 1 if and only if there exist two distinct elements $a$ and $b$ in $s$, i.e., $1 < \#s \leftrightarrow \exists a, b \in s, a \neq b$.
Finset.one_lt_card_iff_nontrivial theorem
: 1 < #s ↔ s.Nontrivial
Full source
theorem one_lt_card_iff_nontrivial : 1 < #s ↔ s.Nontrivial := by
  rw [← not_iff_not, not_lt, Finset.Nontrivial, ← Set.nontrivial_coe_sort,
    not_nontrivial_iff_subsingleton, card_le_one_iff_subsingleton_coe, coe_sort_coe]
Finite Set Nontriviality Criterion: $1 < \#s \leftrightarrow s$ is nontrivial
Informal description
For any finite set $s$, the cardinality of $s$ is greater than 1 if and only if $s$ is nontrivial (i.e., contains at least two distinct elements). In symbols: $$1 < \#s \leftrightarrow \text{$s$ is nontrivial}.$$
Finset.exists_ne_of_one_lt_card theorem
(hs : 1 < #s) (a : α) : ∃ b, b ∈ s ∧ b ≠ a
Full source
theorem exists_ne_of_one_lt_card (hs : 1 < #s) (a : α) : ∃ b, b ∈ s ∧ b ≠ a := by
  obtain ⟨x, hx, y, hy, hxy⟩ := Finset.one_lt_card.mp hs
  by_cases ha : y = a
  · exact ⟨x, hx, ne_of_ne_of_eq hxy ha⟩
  · exact ⟨y, hy, ha⟩
Existence of Distinct Element in Finite Set of Cardinality Greater Than One
Informal description
For any finite set $s$ with cardinality greater than 1 (i.e., $1 < \#s$) and any element $a \in s$, there exists another element $b \in s$ distinct from $a$.
Finset.exists_of_one_lt_card_pi theorem
{ι : Type*} {α : ι → Type*} [∀ i, DecidableEq (α i)] {s : Finset (∀ i, α i)} (h : 1 < #s) : ∃ i, 1 < #(s.image (· i)) ∧ ∀ ai, s.filter (· i = ai) ⊂ s
Full source
/-- If a Finset in a Pi type is nontrivial (has at least two elements), then
  its projection to some factor is nontrivial, and the fibers of the projection
  are proper subsets. -/
lemma exists_of_one_lt_card_pi {ι : Type*} {α : ι → Type*} [∀ i, DecidableEq (α i)]
    {s : Finset (∀ i, α i)} (h : 1 < #s) :
    ∃ i, 1 < #(s.image (· i)) ∧ ∀ ai, s.filter (· i = ai) ⊂ s := by
  simp_rw [one_lt_card_iff, Function.ne_iff] at h ⊢
  obtain ⟨a1, a2, h1, h2, i, hne⟩ := h
  refine ⟨i, ⟨_, _, mem_image_of_mem _ h1, mem_image_of_mem _ h2, hne⟩, fun ai => ?_⟩
  rw [filter_ssubset]
  obtain rfl | hne := eq_or_ne (a2 i) ai
  exacts [⟨a1, h1, hne⟩, ⟨a2, h2, hne⟩]
Projection of Nontrivial Finite Product Set Has Nontrivial Fiber
Informal description
Let $ι$ be a type and $α : ι \to \text{Type}$ be a family of types with decidable equality for each $α_i$. For any finite set $s$ of functions in the product type $\prod_{i \in ι} α_i$ with cardinality greater than 1 (i.e., $1 < \#s$), there exists an index $i \in ι$ such that: 1. The projection of $s$ to the $i$-th coordinate has cardinality greater than 1 (i.e., $1 < \#(s.\text{image} (\cdot\ i))$), and 2. For every value $a_i \in α_i$, the subset of functions in $s$ whose $i$-th coordinate equals $a_i$ is a proper subset of $s$ (i.e., $s.\text{filter} (\cdot\ i = a_i) \subset s$).
Finset.card_eq_succ_iff_cons theorem
: #s = n + 1 ↔ ∃ a t, ∃ (h : a ∉ t), cons a t h = s ∧ #t = n
Full source
theorem card_eq_succ_iff_cons :
    #s#s = n + 1 ↔ ∃ a t, ∃ (h : a ∉ t), cons a t h = s ∧ #t = n :=
  ⟨cons_induction_on s (by simp) fun a s _ _ _ => ⟨a, s, by simp_all⟩,
   fun ⟨a, t, _, hs, _⟩ => by simpa [← hs]⟩
Cardinality of Finite Set as Successor Condition via Disjoint Union
Informal description
For a finite set $s$ of type `Finset α` and a natural number $n$, the cardinality of $s$ is equal to $n + 1$ if and only if there exists an element $a \in \alpha$ and a finite set $t$ of type `Finset α` such that $a \notin t$, the set $s$ is equal to the disjoint union $\{a\} \cup t$ (constructed via `cons a t h`), and the cardinality of $t$ is $n$.
Finset.card_eq_succ theorem
: #s = n + 1 ↔ ∃ a t, a ∉ t ∧ insert a t = s ∧ #t = n
Full source
theorem card_eq_succ : #s#s = n + 1 ↔ ∃ a t, a ∉ t ∧ insert a t = s ∧ #t = n :=
  ⟨fun h =>
    let ⟨a, has⟩ := card_pos.mp (h.symm ▸ Nat.zero_lt_succ _ : 0 < #s)
    ⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by
      simp only [h, card_erase_of_mem has, Nat.add_sub_cancel_right]⟩,
    fun ⟨_, _, hat, s_eq, n_eq⟩ => s_eq ▸ n_eq ▸ card_insert_of_not_mem hat⟩
Cardinality Successor Condition for Finite Sets: $\#s = n + 1 \leftrightarrow \exists a \notin t, s = \{a\} \cup t \land \#t = n$
Informal description
For a finite set $s$ and a natural number $n$, the cardinality of $s$ is $n + 1$ if and only if there exists an element $a$ and a finite set $t$ such that $a \notin t$, $s = \{a\} \cup t$, and the cardinality of $t$ is $n$.
Finset.card_eq_two theorem
: #s = 2 ↔ ∃ x y, x ≠ y ∧ s = { x, y }
Full source
theorem card_eq_two : #s#s = 2 ↔ ∃ x y, x ≠ y ∧ s = {x, y} := by
  constructor
  · rw [card_eq_succ]
    simp_rw [card_eq_one]
    rintro ⟨a, _, hab, rfl, b, rfl⟩
    exact ⟨a, b, not_mem_singleton.1 hab, rfl⟩
  · rintro ⟨x, y, h, rfl⟩
    exact card_pair h
Characterization of Two-Element Finite Sets: $\#s = 2 \leftrightarrow \exists x \neq y, s = \{x, y\}$
Informal description
For any finite set $s$, the cardinality of $s$ is equal to 2 if and only if there exist distinct elements $x$ and $y$ such that $s = \{x, y\}$.
Finset.card_eq_three theorem
: #s = 3 ↔ ∃ x y z, x ≠ y ∧ x ≠ z ∧ y ≠ z ∧ s = { x, y, z }
Full source
theorem card_eq_three : #s#s = 3 ↔ ∃ x y z, x ≠ y ∧ x ≠ z ∧ y ≠ z ∧ s = {x, y, z} := by
  constructor
  · rw [card_eq_succ]
    simp_rw [card_eq_two]
    rintro ⟨a, _, abc, rfl, b, c, bc, rfl⟩
    rw [mem_insert, mem_singleton, not_or] at abc
    exact ⟨a, b, c, abc.1, abc.2, bc, rfl⟩
  · rintro ⟨x, y, z, xy, xz, yz, rfl⟩
    simp only [xy, xz, yz, mem_insert, card_insert_of_not_mem, not_false_iff, mem_singleton,
      or_self_iff, card_singleton]
Characterization of Finite Sets with Cardinality Three: $\#s = 3 \leftrightarrow \exists$ three distinct elements forming $s$
Informal description
For any finite set $s$, the cardinality of $s$ is equal to 3 if and only if there exist three distinct elements $x, y, z$ in $s$ such that $s = \{x, y, z\}$.
Finset.two_lt_card_iff theorem
: 2 < #s ↔ ∃ a b c, a ∈ s ∧ b ∈ s ∧ c ∈ s ∧ a ≠ b ∧ a ≠ c ∧ b ≠ c
Full source
theorem two_lt_card_iff : 2 < #s ↔ ∃ a b c, a ∈ s ∧ b ∈ s ∧ c ∈ s ∧ a ≠ b ∧ a ≠ c ∧ b ≠ c := by
  classical
    simp_rw [lt_iff_add_one_le, le_card_iff_exists_subset_card, reduceAdd, card_eq_three,
      ← exists_and_left, exists_comm (α := Finset α)]
    constructor
    · rintro ⟨a, b, c, t, hsub, hab, hac, hbc, rfl⟩
      exact ⟨a, b, c, by simp_all [insert_subset_iff]⟩
    · rintro ⟨a, b, c, ha, hb, hc, hab, hac, hbc⟩
      exact ⟨a, b, c, {a, b, c}, by simp_all [insert_subset_iff]⟩
Characterization of Finite Sets with Cardinality Greater Than Two
Informal description
For any finite set $s$, the cardinality of $s$ is greater than 2 if and only if there exist three distinct elements $a, b, c$ in $s$ such that $a \neq b$, $a \neq c$, and $b \neq c$.
Finset.strongInduction definition
{p : Finset α → Sort*} (H : ∀ s, (∀ t ⊂ s, p t) → p s) : ∀ s : Finset α, p s
Full source
/-- Suppose that, given objects defined on all strict subsets of any finset `s`, one knows how to
define an object on `s`. Then one can inductively define an object on all finsets, starting from
the empty set and iterating. This can be used either to define data, or to prove properties. -/
def strongInduction {p : Finset α → Sort*} (H : ∀ s, (∀ t ⊂ s, p t) → p s) :
    ∀ s : Finset α, p s
  | s =>
    H s fun t h =>
      have : #t < #s := card_lt_card h
      strongInduction H t
  termination_by s => #s
Strong induction principle for finite sets
Informal description
Given a predicate $p$ on finite sets of type $\alpha$, if for any finite set $s$, the truth of $p(t)$ for all strict subsets $t \subset s$ implies the truth of $p(s)$, then $p(s)$ holds for all finite sets $s$. This principle allows for strong induction over finite sets, where the induction step assumes the property holds for all smaller sets.
Finset.strongInduction_eq theorem
{p : Finset α → Sort*} (H : ∀ s, (∀ t ⊂ s, p t) → p s) (s : Finset α) : strongInduction H s = H s fun t _ => strongInduction H t
Full source
@[nolint unusedHavesSuffices] -- Porting note: false positive
theorem strongInduction_eq {p : Finset α → Sort*} (H : ∀ s, (∀ t ⊂ s, p t) → p s)
    (s : Finset α) : strongInduction H s = H s fun t _ => strongInduction H t := by
  rw [strongInduction]
Strong Induction Principle for Finite Sets: Computation Rule
Informal description
For any predicate $p$ on finite sets of type $\alpha$ and any finite set $s$, the strong induction principle applied to $p$ and $s$ is equal to the application of the induction hypothesis $H$ to $s$ and the function that applies strong induction to all strict subsets of $s$.
Finset.strongInductionOn definition
{p : Finset α → Sort*} (s : Finset α) : (∀ s, (∀ t ⊂ s, p t) → p s) → p s
Full source
/-- Analogue of `strongInduction` with order of arguments swapped. -/
@[elab_as_elim]
def strongInductionOn {p : Finset α → Sort*} (s : Finset α) :
    (∀ s, (∀ t ⊂ s, p t) → p s) → p s := fun H => strongInduction H s
Strong induction on a finite set
Informal description
Given a finite set $s$ of type $\alpha$ and a predicate $p$ on finite sets, if for any finite set $s'$, the truth of $p(t)$ for all strict subsets $t \subset s'$ implies the truth of $p(s')$, then $p(s)$ holds. This is a variant of the strong induction principle for finite sets where the induction hypothesis is applied to the given set $s$.
Finset.strongInductionOn_eq theorem
{p : Finset α → Sort*} (s : Finset α) (H : ∀ s, (∀ t ⊂ s, p t) → p s) : s.strongInductionOn H = H s fun t _ => t.strongInductionOn H
Full source
@[nolint unusedHavesSuffices] -- Porting note: false positive
theorem strongInductionOn_eq {p : Finset α → Sort*} (s : Finset α)
    (H : ∀ s, (∀ t ⊂ s, p t) → p s) :
    s.strongInductionOn H = H s fun t _ => t.strongInductionOn H := by
  dsimp only [strongInductionOn]
  rw [strongInduction]
Computation Rule for Strong Induction on Finite Sets
Informal description
For any finite set $s$ of type $\alpha$ and any predicate $p$ on finite sets, the strong induction principle applied to $s$ and $H$ is equal to the application of $H$ to $s$ and the function that applies strong induction to all strict subsets of $s$. Here, $H$ is the induction hypothesis stating that for any finite set $s'$, if $p(t)$ holds for all strict subsets $t \subset s'$, then $p(s')$ holds.
Finset.case_strong_induction_on theorem
[DecidableEq α] {p : Finset α → Prop} (s : Finset α) (h₀ : p ∅) (h₁ : ∀ a s, a ∉ s → (∀ t ⊆ s, p t) → p (insert a s)) : p s
Full source
@[elab_as_elim]
theorem case_strong_induction_on [DecidableEq α] {p : Finset α → Prop} (s : Finset α) (h₀ : p )
    (h₁ : ∀ a s, a ∉ s → (∀ t ⊆ s, p t) → p (insert a s)) : p s :=
  Finset.strongInductionOn s fun s =>
    Finset.induction_on s (fun _ => h₀) fun a s n _ ih =>
      (h₁ a s n) fun t ss => ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _)
Strong Induction Principle for Finite Sets via Insertion and Subset Inclusion
Informal description
Let $\alpha$ be a type with decidable equality and $p$ be a predicate on finite subsets of $\alpha$. Given a finite subset $s \subseteq \alpha$, to prove $p(s)$ it suffices to: 1. Prove the base case: $p(\emptyset)$ holds for the empty set. 2. Prove the inductive step: For any element $a \in \alpha$ and any finite subset $s' \subseteq \alpha$ such that $a \notin s'$, if $p(t)$ holds for all subsets $t \subseteq s'$, then $p(s' \cup \{a\})$ holds.
Finset.Nonempty.strong_induction theorem
{p : ∀ s, s.Nonempty → Prop} (h₀ : ∀ a, p { a } (singleton_nonempty _)) (h₁ : ∀ ⦃s⦄ (hs : s.Nontrivial), (∀ t ht, t ⊂ s → p t ht) → p s hs.nonempty) : ∀ ⦃s : Finset α⦄ (hs), p s hs
Full source
/-- Suppose that, given objects defined on all nonempty strict subsets of any nontrivial finset `s`,
one knows how to define an object on `s`. Then one can inductively define an object on all finsets,
starting from singletons and iterating.

TODO: Currently this can only be used to prove properties.
Replace `Finset.Nonempty.exists_eq_singleton_or_nontrivial` with computational content
in order to let `p` be `Sort`-valued. -/
@[elab_as_elim]
protected lemma Nonempty.strong_induction {p : ∀ s, s.Nonempty → Prop}
    (h₀ : ∀ a, p {a} (singleton_nonempty _))
    (h₁ : ∀ ⦃s⦄ (hs : s.Nontrivial), (∀ t ht, t ⊂ s → p t ht) → p s hs.nonempty) :
    ∀ ⦃s : Finset α⦄ (hs), p s hs
  | s, hs => by
    obtain ⟨a, rfl⟩ | hs := hs.exists_eq_singleton_or_nontrivial
    · exact h₀ _
    · refine h₁ hs fun t ht hts ↦ ?_
      have := card_lt_card hts
      exact ht.strong_induction h₀ h₁
termination_by s => #s
Strong Induction Principle for Nonempty Finite Sets
Informal description
Let $p$ be a property defined for all nonempty finite subsets of a type $\alpha$. Suppose that: 1. For every element $a \in \alpha$, the property $p$ holds for the singleton set $\{a\}$. 2. For any nontrivial finite subset $s \subset \alpha$ (i.e., $s$ contains at least two distinct elements), if $p$ holds for all nonempty strict subsets $t \subset s$, then $p$ holds for $s$. Then, the property $p$ holds for every nonempty finite subset of $\alpha$.
Finset.strongDownwardInduction definition
{p : Finset α → Sort*} {n : ℕ} (H : ∀ t₁, (∀ {t₂ : Finset α}, #t₂ ≤ n → t₁ ⊂ t₂ → p t₂) → #t₁ ≤ n → p t₁) : ∀ s : Finset α, #s ≤ n → p s
Full source
/-- Suppose that, given that `p t` can be defined on all supersets of `s` of cardinality less than
`n`, one knows how to define `p s`. Then one can inductively define `p s` for all finsets `s` of
cardinality less than `n`, starting from finsets of card `n` and iterating. This
can be used either to define data, or to prove properties. -/
def strongDownwardInduction {p : Finset α → Sort*} {n : }
    (H : ∀ t₁, (∀ {t₂ : Finset α}, #t₂ ≤ n → t₁ ⊂ t₂ → p t₂) → #t₁ ≤ n → p t₁) :
    ∀ s : Finset α, #s ≤ n → p s
  | s =>
    H s fun {t} ht h =>
      have := Finset.card_lt_card h
      have : n - #t < n - #s := by omega
      strongDownwardInduction H t ht
  termination_by s => n - #s
Strong downward induction for finite sets
Informal description
Given a property \( p \) defined on finite subsets of a type \( \alpha \), and a natural number \( n \), suppose that for any finite subset \( t_1 \), if \( p \) holds for all finite subsets \( t_2 \) with cardinality at most \( n \) that strictly contain \( t_1 \), and \( t_1 \) itself has cardinality at most \( n \), then \( p \) holds for \( t_1 \). Then, \( p \) holds for all finite subsets \( s \) of \( \alpha \) with cardinality at most \( n \). This principle allows defining or proving properties of finite subsets by descending induction on their cardinality, starting from subsets with cardinality \( n \) and working downwards.
Finset.strongDownwardInduction_eq theorem
{p : Finset α → Sort*} (H : ∀ t₁, (∀ {t₂ : Finset α}, #t₂ ≤ n → t₁ ⊂ t₂ → p t₂) → #t₁ ≤ n → p t₁) (s : Finset α) : strongDownwardInduction H s = H s fun {t} ht _ => strongDownwardInduction H t ht
Full source
@[nolint unusedHavesSuffices] -- Porting note: false positive
theorem strongDownwardInduction_eq {p : Finset α → Sort*}
    (H : ∀ t₁, (∀ {t₂ : Finset α}, #t₂ ≤ n → t₁ ⊂ t₂ → p t₂) → #t₁ ≤ n → p t₁)
    (s : Finset α) :
    strongDownwardInduction H s = H s fun {t} ht _ => strongDownwardInduction H t ht := by
  rw [strongDownwardInduction]
Recursive Equation for Strong Downward Induction on Finite Sets
Informal description
Let $p$ be a property defined on finite subsets of a type $\alpha$, and let $n$ be a natural number. Suppose that for any finite subset $t_1 \subseteq \alpha$, if $p$ holds for all finite subsets $t_2$ with $\#t_2 \leq n$ and $t_1 \subset t_2$, and $\#t_1 \leq n$, then $p$ holds for $t_1$. Then, for any finite subset $s \subseteq \alpha$ with $\#s \leq n$, the strong downward induction principle satisfies the equation: \[ \text{strongDownwardInduction}\, H\, s = H\, s\, \left( \lambda t\, ht\, \_, \text{strongDownwardInduction}\, H\, t\, ht \right). \]
Finset.strongDownwardInductionOn definition
{p : Finset α → Sort*} (s : Finset α) (H : ∀ t₁, (∀ {t₂ : Finset α}, #t₂ ≤ n → t₁ ⊂ t₂ → p t₂) → #t₁ ≤ n → p t₁) : #s ≤ n → p s
Full source
/-- Analogue of `strongDownwardInduction` with order of arguments swapped. -/
@[elab_as_elim]
def strongDownwardInductionOn {p : Finset α → Sort*} (s : Finset α)
    (H : ∀ t₁, (∀ {t₂ : Finset α}, #t₂ ≤ n → t₁ ⊂ t₂ → p t₂) → #t₁ ≤ n → p t₁) :
    #s ≤ n → p s :=
  strongDownwardInduction H s
Strong downward induction on a finite set
Informal description
Given a property \( p \) defined on finite subsets of a type \( \alpha \), a finite subset \( s \), and a natural number \( n \), suppose that for any finite subset \( t_1 \), if \( p \) holds for all finite subsets \( t_2 \) with cardinality at most \( n \) that strictly contain \( t_1 \), and \( t_1 \) itself has cardinality at most \( n \), then \( p \) holds for \( t_1 \). Then, if \( s \) has cardinality at most \( n \), \( p \) holds for \( s \). This is a variant of strong downward induction where the property \( p \) is applied directly to the given finite subset \( s \).
Finset.strongDownwardInductionOn_eq theorem
{p : Finset α → Sort*} (s : Finset α) (H : ∀ t₁, (∀ {t₂ : Finset α}, #t₂ ≤ n → t₁ ⊂ t₂ → p t₂) → #t₁ ≤ n → p t₁) : s.strongDownwardInductionOn H = H s fun {t} ht _ => t.strongDownwardInductionOn H ht
Full source
@[nolint unusedHavesSuffices] -- Porting note: false positive
theorem strongDownwardInductionOn_eq {p : Finset α → Sort*} (s : Finset α)
    (H : ∀ t₁, (∀ {t₂ : Finset α}, #t₂ ≤ n → t₁ ⊂ t₂ → p t₂) → #t₁ ≤ n → p t₁) :
    s.strongDownwardInductionOn H = H s fun {t} ht _ => t.strongDownwardInductionOn H ht := by
  dsimp only [strongDownwardInductionOn]
  rw [strongDownwardInduction]
Recursive Equation for Strong Downward Induction on Finite Sets
Informal description
Let $p$ be a property defined on finite subsets of a type $\alpha$, and let $s$ be a finite subset of $\alpha$. Suppose that for any finite subset $t_1 \subseteq \alpha$, if $p$ holds for all finite subsets $t_2$ with $\#t_2 \leq n$ and $t_1 \subset t_2$, and $\#t_1 \leq n$, then $p$ holds for $t_1$. Then the strong downward induction principle applied to $s$ satisfies: \[ s.\text{strongDownwardInductionOn}\, H = H\, s\, \left( \lambda t\, ht\, \_, t.\text{strongDownwardInductionOn}\, H\, ht \right). \]
Finset.lt_wf theorem
{α} : WellFounded (@LT.lt (Finset α) _)
Full source
theorem lt_wf {α} : WellFounded (@LT.lt (Finset α) _) :=
  have H : Subrelation (@LT.lt (Finset α) _) (InvImage (· < ·) card) := fun {_ _} hxy =>
    card_lt_card hxy
  Subrelation.wf H <| InvImage.wf _ <| (Nat.lt_wfRel).2
Well-foundedness of Strict Subset Relation on Finite Sets
Informal description
The strict subset relation `<` on finite sets of type $\alpha$ is well-founded, meaning there are no infinite descending chains of finite sets under this relation.