doc-next-gen

Mathlib.Data.Set.Card

Module docstring

{"# Noncomputable Set Cardinality

We define the cardinality of set s as a term Set.encard s : ℕ∞ and a term Set.ncard s : ℕ. The latter takes the junk value of zero if s is infinite. Both functions are noncomputable, and are defined in terms of ENat.card (which takes a type as its argument); this file can be seen as an API for the same function in the special case where the type is a coercion of a Set, allowing for smoother interactions with the Set API.

Set.encard never takes junk values, so is more mathematically natural than Set.ncard, even though it takes values in a less convenient type. It is probably the right choice in settings where one is concerned with the cardinalities of sets that may or may not be infinite.

Set.ncard has a nicer codomain, but when using it, Set.Finite hypotheses are normally needed to make sure its values are meaningful. More generally, Set.ncard is intended to be used over the obvious alternative Finset.card when finiteness is 'propositional' rather than 'structural'. When working with sets that are finite by virtue of their definition, then Finset.card probably makes more sense. One setting where Set.ncard works nicely is in a type α with [Finite α], where every set is automatically finite. In this setting, we use default arguments and a simple tactic so that finiteness goals are discharged automatically in Set.ncard theorems.

Main Definitions

  • Set.encard s is the cardinality of the set s as an extended natural number, with value if s is infinite.
  • Set.ncard s is the cardinality of the set s as a natural number, provided s is Finite. If s is Infinite, then Set.ncard s = 0.
  • toFinite_tac is a tactic that tries to synthesize a Set.Finite s argument with Set.toFinite. This will work for s : Set α where there is a Finite α instance.

Implementation Notes

The theorems in this file are very similar to those in Data.Finset.Card, but with Set operations instead of Finset. We first prove all the theorems for Set.encard, and then derive most of the Set.ncard results as a consequence. Things are done this way to avoid reliance on the Finset API for theorems about infinite sets, and to allow for a refactor that removes or modifies Set.ncard in the future.

Nearly all the theorems for Set.ncard require finiteness of one or more of their arguments. We provide this assumption with a default argument of the form (hs : s.Finite := by toFinite_tac), where toFinite_tac will find an s.Finite term in the cases where s is a set in a Finite type.

Often, where there are two set arguments s and t, the finiteness of one follows from the other in the context of the theorem, in which case we only include the ones that are needed, and derive the other inside the proof. A few of the theorems, such as ncard_union_le do not require finiteness arguments; they are true by coincidence due to junk values. ","### Explicit description of a set from its cardinality "}

Set.encard definition
(s : Set α) : ℕ∞
Full source
/-- The cardinality of a set as a term in `ℕ∞` -/
noncomputable def encard (s : Set α) : ℕ∞ := ENat.card s
Extended cardinality of a set
Informal description
The extended cardinality of a set $s$ over a type $\alpha$ is defined as the cardinality of $s$ as an extended natural number (an element of $\mathbb{N}_\infty$), where $\infty$ represents an infinite set. This is computed as the cardinality of the type of elements of $s$.
Set.encard_univ_coe theorem
(s : Set α) : encard (univ : Set s) = encard s
Full source
@[simp] theorem encard_univ_coe (s : Set α) : encard (univ : Set s) = encard s := by
  rw [encard, encard, ENat.card_congr (Equiv.Set.univ ↑s)]
Extended Cardinality of Universal Set Equals Extended Cardinality of Base Set
Informal description
For any set $s$ over a type $\alpha$, the extended cardinality of the universal set of the subtype corresponding to $s$ is equal to the extended cardinality of $s$ itself. In other words, $\mathrm{encard}(\mathrm{univ} : \mathrm{Set}\, s) = \mathrm{encard}\, s$.
Set.encard_univ theorem
(α : Type*) : encard (univ : Set α) = ENat.card α
Full source
theorem encard_univ (α : Type*) :
    encard (univ : Set α) = ENat.card α := by
  rw [encard, ENat.card_congr (Equiv.Set.univ α)]
Extended Cardinality of Universal Set Equals Type Cardinality
Informal description
For any type $\alpha$, the extended cardinality of the universal set (the set of all elements of $\alpha$) is equal to the extended cardinality of $\alpha$ itself, i.e., $\mathrm{encard}(\mathrm{univ} : \mathrm{Set}\,\alpha) = \mathrm{ENat.card}\,\alpha$.
Set.Finite.encard_eq_coe_toFinset_card theorem
(h : s.Finite) : s.encard = h.toFinset.card
Full source
theorem Finite.encard_eq_coe_toFinset_card (h : s.Finite) : s.encard = h.toFinset.card := by
  have := h.fintype
  rw [encard, ENat.card_eq_coe_fintype_card, toFinite_toFinset, toFinset_card]
Extended Cardinality of Finite Set Equals Finset Cardinality
Informal description
For any finite set $s$ in a type $\alpha$, the extended cardinality of $s$ (as an element of $\mathbb{N}_\infty$) is equal to the cardinality of the finset representation of $s$, i.e., $\mathrm{encard}(s) = |\mathrm{toFinset}(s)|$.
Set.encard_eq_coe_toFinset_card theorem
(s : Set α) [Fintype s] : encard s = s.toFinset.card
Full source
theorem encard_eq_coe_toFinset_card (s : Set α) [Fintype s] : encard s = s.toFinset.card := by
  have h := toFinite s
  rw [h.encard_eq_coe_toFinset_card, toFinite_toFinset]
Extended Cardinality Equals Finset Cardinality for Finite Types
Informal description
For any set $s$ in a type $\alpha$ with a finite type instance, the extended cardinality of $s$ is equal to the cardinality of its finset representation, i.e., $\mathrm{encard}(s) = |\mathrm{toFinset}(s)|$.
Set.toENat_cardinalMk theorem
(s : Set α) : (Cardinal.mk s).toENat = s.encard
Full source
@[simp] theorem toENat_cardinalMk (s : Set α) : (Cardinal.mk s).toENat = s.encard := rfl
Equality of Cardinality Conversion and Extended Cardinality for Sets
Informal description
For any set $s$ of elements of type $\alpha$, the extended natural number obtained by converting the cardinality of $s$ (as a cardinal number) is equal to the extended cardinality of $s$, i.e., $(\mathrm{Cardinal.mk}\, s).\mathrm{toENat} = s.\mathrm{encard}$.
Set.toENat_cardinalMk_subtype theorem
(P : α → Prop) : (Cardinal.mk { x // P x }).toENat = {x | P x}.encard
Full source
theorem toENat_cardinalMk_subtype (P : α → Prop) :
    (Cardinal.mk {x // P x}).toENat = {x | P x}.encard :=
  rfl
Equality of Extended Cardinality for Subtypes and Sets: $|\{x \mid P x\}|_{\mathbb{N}_\infty} = \text{encard}(\{x \mid P x\})$
Informal description
For any predicate $P$ on a type $\alpha$, the extended natural number cardinality of the subtype $\{x \mid P x\}$ is equal to the extended cardinality of the set $\{x \mid P x\}$. In other words, $|\{x \mid P x\}|_{\mathbb{N}_\infty} = \text{encard}(\{x \mid P x\})$, where $|\cdot|_{\mathbb{N}_\infty}$ denotes the extended natural number cardinality and $\text{encard}$ is the extended cardinality function for sets.
Set.coe_fintypeCard theorem
(s : Set α) [Fintype s] : Fintype.card s = s.encard
Full source
@[simp] theorem coe_fintypeCard (s : Set α) [Fintype s] : Fintype.card s = s.encard := by
  simp [encard_eq_coe_toFinset_card]
Finite Type Cardinality Equals Extended Cardinality for Sets
Informal description
For any set $s$ in a type $\alpha$ with a finite type instance, the cardinality of $s$ as a finite type is equal to the extended cardinality of $s$, i.e., $|s| = \mathrm{encard}(s)$.
Set.encard_coe_eq_coe_finsetCard theorem
(s : Finset α) : encard (s : Set α) = s.card
Full source
@[simp, norm_cast] theorem encard_coe_eq_coe_finsetCard (s : Finset α) :
    encard (s : Set α) = s.card := by
  rw [Finite.encard_eq_coe_toFinset_card (Finset.finite_toSet s)]; simp
Extended Cardinality of Finset as Set Equals Finset Cardinality
Informal description
For any finset $s$ of type $\alpha$, the extended cardinality of the underlying set of $s$ is equal to the cardinality of $s$ as a finset, i.e., $\mathrm{encard}(s) = |s|$.
Set.Infinite.encard_eq theorem
{s : Set α} (h : s.Infinite) : s.encard = ⊤
Full source
@[simp] theorem Infinite.encard_eq {s : Set α} (h : s.Infinite) : s.encard =  := by
  have := h.to_subtype
  rw [encard, ENat.card_eq_top_of_infinite]
Extended Cardinality of an Infinite Set is Infinity
Informal description
For any infinite set $s$ in a type $\alpha$, the extended cardinality of $s$ is equal to $\infty$ (denoted as $\top$ in the extended natural numbers $\mathbb{N}_\infty$).
Set.encard_empty theorem
: (∅ : Set α).encard = 0
Full source
@[simp] theorem encard_empty : ( : Set α).encard = 0 := by
  rw [encard_eq_zero]
Extended Cardinality of Empty Set: $\mathrm{encard}(\emptyset) = 0$
Informal description
The extended cardinality of the empty set $\emptyset$ in any type $\alpha$ is zero, i.e., $\mathrm{encard}(\emptyset) = 0$.
Set.nonempty_of_encard_ne_zero theorem
(h : s.encard ≠ 0) : s.Nonempty
Full source
theorem nonempty_of_encard_ne_zero (h : s.encard ≠ 0) : s.Nonempty := by
  rwa [nonempty_iff_ne_empty, Ne, ← encard_eq_zero]
Nonempty Set from Nonzero Extended Cardinality: $\mathrm{encard}(s) \neq 0 \to s \neq \emptyset$
Informal description
For any set $s$, if the extended cardinality of $s$ is not zero, then $s$ is nonempty, i.e., if $\mathrm{encard}(s) \neq 0$, then $s$ is nonempty.
Set.encard_pos theorem
: 0 < s.encard ↔ s.Nonempty
Full source
@[simp] theorem encard_pos : 0 < s.encard ↔ s.Nonempty := by
  rw [pos_iff_ne_zero, encard_ne_zero]
Positivity of Extended Cardinality Characterizes Nonempty Sets
Informal description
For any set $s$, the extended cardinality of $s$ is positive if and only if $s$ is nonempty, i.e., $0 < \mathrm{encard}(s) \leftrightarrow s \neq \emptyset$.
Set.encard_union_eq theorem
(h : Disjoint s t) : (s ∪ t).encard = s.encard + t.encard
Full source
theorem encard_union_eq (h : Disjoint s t) : (s ∪ t).encard = s.encard + t.encard := by
  classical
  simp [encard, ENat.card_congr (Equiv.Set.union h)]
Extended Cardinality of Union of Disjoint Sets: $\mathrm{encard}(s \cup t) = \mathrm{encard}(s) + \mathrm{encard}(t)$
Informal description
For any two disjoint sets $s$ and $t$ in a type $\alpha$, the extended cardinality of their union equals the sum of their extended cardinalities, i.e., $\mathrm{encard}(s \cup t) = \mathrm{encard}(s) + \mathrm{encard}(t)$.
Set.encard_insert_of_not_mem theorem
{a : α} (has : a ∉ s) : (insert a s).encard = s.encard + 1
Full source
theorem encard_insert_of_not_mem {a : α} (has : a ∉ s) : (insert a s).encard = s.encard + 1 := by
  rw [← union_singleton, encard_union_eq (by simpa), encard_singleton]
Extended Cardinality of Insertion: $\mathrm{encard}(s \cup \{a\}) = \mathrm{encard}(s) + 1$ for $a \notin s$
Informal description
For any element $a$ of type $\alpha$ and any set $s$ over $\alpha$ such that $a \notin s$, the extended cardinality of the set obtained by inserting $a$ into $s$ is equal to the extended cardinality of $s$ plus one, i.e., $\mathrm{encard}(\{a\} \cup s) = \mathrm{encard}(s) + 1$.
Set.Finite.encard_lt_top theorem
(h : s.Finite) : s.encard < ⊤
Full source
theorem Finite.encard_lt_top (h : s.Finite) : s.encard <  := by
  induction s, h using Set.Finite.induction_on with
  | empty => simp
  | insert hat _ ht' =>
    rw [encard_insert_of_not_mem hat]
    exact lt_tsub_iff_right.1 ht'
Extended Cardinality of Finite Sets is Finite: $\mathrm{encard}(s) < \infty$ for finite $s$
Informal description
For any finite set $s$ over a type $\alpha$, the extended cardinality of $s$ is strictly less than infinity, i.e., $\mathrm{encard}(s) < \infty$.
Set.Finite.encard_eq_coe theorem
(h : s.Finite) : s.encard = ENat.toNat s.encard
Full source
theorem Finite.encard_eq_coe (h : s.Finite) : s.encard = ENat.toNat s.encard :=
  (ENat.coe_toNat h.encard_lt_top.ne).symm
Extended Cardinality of Finite Sets as Natural Numbers: $\mathrm{encard}(s) = \mathrm{toNat}(\mathrm{encard}(s))$ for finite $s$
Informal description
For any finite set $s$ over a type $\alpha$, the extended cardinality of $s$ is equal to the natural number obtained by converting the extended cardinality to a natural number (where $\infty$ maps to $0$), i.e., $\mathrm{encard}(s) = \mathrm{toNat}(\mathrm{encard}(s))$.
Set.Finite.exists_encard_eq_coe theorem
(h : s.Finite) : ∃ (n : ℕ), s.encard = n
Full source
theorem Finite.exists_encard_eq_coe (h : s.Finite) : ∃ (n : ℕ), s.encard = n :=
  ⟨_, h.encard_eq_coe⟩
Extended Cardinality of Finite Sets is a Natural Number
Informal description
For any finite set $s$, there exists a natural number $n$ such that the extended cardinality of $s$ equals $n$, i.e., $\mathrm{encard}(s) = n$.
Set.encard_ne_top_iff theorem
: s.encard ≠ ⊤ ↔ s.Finite
Full source
theorem encard_ne_top_iff : s.encard ≠ ⊤s.encard ≠ ⊤ ↔ s.Finite := by
  simp
Extended Cardinality is Finite if and only if Set is Finite
Informal description
For any set $s$, the extended cardinality of $s$ is not equal to $\infty$ if and only if $s$ is finite.
Set.finite_of_encard_le_coe theorem
{k : ℕ} (h : s.encard ≤ k) : s.Finite
Full source
theorem finite_of_encard_le_coe {k : } (h : s.encard ≤ k) : s.Finite := by
  rw [← encard_lt_top_iff]; exact h.trans_lt (WithTop.coe_lt_top _)
Finite Set from Bounded Extended Cardinality
Informal description
For any natural number $k$ and any set $s$, if the extended cardinality of $s$ satisfies $\mathrm{encard}(s) \leq k$, then $s$ is finite.
Set.finite_of_encard_eq_coe theorem
{k : ℕ} (h : s.encard = k) : s.Finite
Full source
theorem finite_of_encard_eq_coe {k : } (h : s.encard = k) : s.Finite :=
  finite_of_encard_le_coe h.le
Finite Set from Exact Extended Cardinality
Informal description
For any natural number $k$ and any set $s$, if the extended cardinality of $s$ equals $k$, then $s$ is finite.
Set.encard_le_coe_iff theorem
{k : ℕ} : s.encard ≤ k ↔ s.Finite ∧ ∃ (n₀ : ℕ), s.encard = n₀ ∧ n₀ ≤ k
Full source
theorem encard_le_coe_iff {k : } : s.encard ≤ k ↔ s.Finite ∧ ∃ (n₀ : ℕ), s.encard = n₀ ∧ n₀ ≤ k :=
  ⟨fun h ↦ ⟨finite_of_encard_le_coe h, by rwa [ENat.le_coe_iff] at h⟩,
    fun ⟨_,⟨n₀,hs, hle⟩⟩ ↦ by rwa [hs, Nat.cast_le]⟩
Characterization of Extended Cardinality Bound: $\mathrm{encard}(s) \leq k \leftrightarrow s$ is finite and $\exists n_0 \in \mathbb{N}, \mathrm{encard}(s) = n_0 \leq k$
Informal description
For any set $s$ and natural number $k$, the extended cardinality $\mathrm{encard}(s)$ satisfies $\mathrm{encard}(s) \leq k$ if and only if $s$ is finite and there exists a natural number $n_0$ such that $\mathrm{encard}(s) = n_0$ and $n_0 \leq k$.
Set.encard_prod theorem
: (s ×ˢ t).encard = s.encard * t.encard
Full source
@[simp]
theorem encard_prod : (s ×ˢ t).encard = s.encard * t.encard := by
  simp [Set.encard, ENat.card_congr (Equiv.Set.prod ..)]
Extended Cardinality of Cartesian Product of Sets
Informal description
For any sets $s \subseteq \alpha$ and $t \subseteq \beta$, the extended cardinality of their Cartesian product $s \times t$ is equal to the product of their extended cardinalities, i.e., $\mathrm{encard}(s \times t) = \mathrm{encard}(s) \cdot \mathrm{encard}(t)$.
Set.encard_le_encard theorem
(h : s ⊆ t) : s.encard ≤ t.encard
Full source
theorem encard_le_encard (h : s ⊆ t) : s.encard ≤ t.encard := by
  rw [← union_diff_cancel h, encard_union_eq disjoint_sdiff_right]; exact le_self_add
Monotonicity of Extended Cardinality under Subset Inclusion
Informal description
For any two sets $s$ and $t$ in a type $\alpha$, if $s$ is a subset of $t$, then the extended cardinality of $s$ is less than or equal to the extended cardinality of $t$, i.e., \[ \mathrm{encard}(s) \leq \mathrm{encard}(t). \]
Set.encard_mono theorem
{α : Type*} : Monotone (encard : Set α → ℕ∞)
Full source
theorem encard_mono {α : Type*} : Monotone (encard : Set α → ℕ∞) :=
  fun _ _ ↦ encard_le_encard
Monotonicity of Extended Set Cardinality: $\mathrm{encard}(s) \leq \mathrm{encard}(t)$ for $s \subseteq t$
Informal description
For any type $\alpha$, the extended cardinality function $\mathrm{encard} : \mathcal{P}(\alpha) \to \mathbb{N}_\infty$ is monotone with respect to subset inclusion. That is, for any sets $s, t \subseteq \alpha$ with $s \subseteq t$, we have $\mathrm{encard}(s) \leq \mathrm{encard}(t)$.
Set.encard_diff_add_encard_of_subset theorem
(h : s ⊆ t) : (t \ s).encard + s.encard = t.encard
Full source
theorem encard_diff_add_encard_of_subset (h : s ⊆ t) : (t \ s).encard + s.encard = t.encard := by
  rw [← encard_union_eq disjoint_sdiff_left, diff_union_self, union_eq_self_of_subset_right h]
Additivity of Extended Cardinality for Set Difference and Subset: $\mathrm{encard}(t \setminus s) + \mathrm{encard}(s) = \mathrm{encard}(t)$ when $s \subseteq t$
Informal description
For any sets $s$ and $t$ in a type $\alpha$ with $s \subseteq t$, the sum of the extended cardinalities of the set difference $t \setminus s$ and the set $s$ equals the extended cardinality of $t$, i.e., $$\mathrm{encard}(t \setminus s) + \mathrm{encard}(s) = \mathrm{encard}(t).$$
Set.one_le_encard_iff_nonempty theorem
: 1 ≤ s.encard ↔ s.Nonempty
Full source
@[simp] theorem one_le_encard_iff_nonempty : 1 ≤ s.encard ↔ s.Nonempty := by
  rw [nonempty_iff_ne_empty, Ne, ← encard_eq_zero, ENat.one_le_iff_ne_zero]
Characterization of Nonempty Sets via Extended Cardinality: $1 \leq \mathrm{encard}(s) \leftrightarrow s \neq \emptyset$
Informal description
For any set $s$, the extended cardinality $\mathrm{encard}(s)$ satisfies $1 \leq \mathrm{encard}(s)$ if and only if $s$ is nonempty.
Set.encard_diff_add_encard_inter theorem
(s t : Set α) : (s \ t).encard + (s ∩ t).encard = s.encard
Full source
theorem encard_diff_add_encard_inter (s t : Set α) :
    (s \ t).encard + (s ∩ t).encard = s.encard := by
  rw [← encard_union_eq (disjoint_of_subset_right inter_subset_right disjoint_sdiff_left),
    diff_union_inter]
Extended Cardinality Decomposition: $\mathrm{encard}(s \setminus t) + \mathrm{encard}(s \cap t) = \mathrm{encard}(s)$
Informal description
For any two sets $s$ and $t$ over a type $\alpha$, the sum of the extended cardinalities of the set difference $s \setminus t$ and the intersection $s \cap t$ equals the extended cardinality of $s$, i.e., $\mathrm{encard}(s \setminus t) + \mathrm{encard}(s \cap t) = \mathrm{encard}(s)$.
Set.encard_union_add_encard_inter theorem
(s t : Set α) : (s ∪ t).encard + (s ∩ t).encard = s.encard + t.encard
Full source
theorem encard_union_add_encard_inter (s t : Set α) :
    (s ∪ t).encard + (s ∩ t).encard = s.encard + t.encard := by
  rw [← diff_union_self, encard_union_eq disjoint_sdiff_left, add_right_comm,
    encard_diff_add_encard_inter]
Extended Cardinality Union-Intersection Identity: $\mathrm{encard}(s \cup t) + \mathrm{encard}(s \cap t) = \mathrm{encard}(s) + \mathrm{encard}(t)$
Informal description
For any two sets $s$ and $t$ over a type $\alpha$, the sum of the extended cardinalities of their union and intersection equals the sum of their extended cardinalities, i.e., $$\mathrm{encard}(s \cup t) + \mathrm{encard}(s \cap t) = \mathrm{encard}(s) + \mathrm{encard}(t).$$
Set.encard_eq_encard_iff_encard_diff_eq_encard_diff theorem
(h : (s ∩ t).Finite) : s.encard = t.encard ↔ (s \ t).encard = (t \ s).encard
Full source
theorem encard_eq_encard_iff_encard_diff_eq_encard_diff (h : (s ∩ t).Finite) :
    s.encard = t.encard ↔ (s \ t).encard = (t \ s).encard := by
  rw [← encard_diff_add_encard_inter s t, ← encard_diff_add_encard_inter t s, inter_comm t s,
    WithTop.add_right_inj h.encard_lt_top.ne]
Equality of Extended Cardinalities via Set Differences: $\mathrm{encard}(s) = \mathrm{encard}(t) \leftrightarrow \mathrm{encard}(s \setminus t) = \mathrm{encard}(t \setminus s)$
Informal description
For any two sets $s$ and $t$ over a type $\alpha$ such that their intersection $s \cap t$ is finite, the extended cardinalities of $s$ and $t$ are equal if and only if the extended cardinalities of their set differences $s \setminus t$ and $t \setminus s$ are equal, i.e., $$\mathrm{encard}(s) = \mathrm{encard}(t) \leftrightarrow \mathrm{encard}(s \setminus t) = \mathrm{encard}(t \setminus s).$$
Set.encard_le_encard_iff_encard_diff_le_encard_diff theorem
(h : (s ∩ t).Finite) : s.encard ≤ t.encard ↔ (s \ t).encard ≤ (t \ s).encard
Full source
theorem encard_le_encard_iff_encard_diff_le_encard_diff (h : (s ∩ t).Finite) :
    s.encard ≤ t.encard ↔ (s \ t).encard ≤ (t \ s).encard := by
  rw [← encard_diff_add_encard_inter s t, ← encard_diff_add_encard_inter t s, inter_comm t s,
    WithTop.add_le_add_iff_right h.encard_lt_top.ne]
Extended Cardinality Comparison via Set Differences: $\mathrm{encard}(s) \leq \mathrm{encard}(t) \leftrightarrow \mathrm{encard}(s \setminus t) \leq \mathrm{encard}(t \setminus s)$ for finite $s \cap t$
Informal description
For any two sets $s$ and $t$ over a type $\alpha$, if the intersection $s \cap t$ is finite, then the extended cardinality of $s$ is less than or equal to that of $t$ if and only if the extended cardinality of the set difference $s \setminus t$ is less than or equal to that of $t \setminus s$. In other words: $$ \mathrm{encard}(s) \leq \mathrm{encard}(t) \leftrightarrow \mathrm{encard}(s \setminus t) \leq \mathrm{encard}(t \setminus s). $$
Set.encard_lt_encard_iff_encard_diff_lt_encard_diff theorem
(h : (s ∩ t).Finite) : s.encard < t.encard ↔ (s \ t).encard < (t \ s).encard
Full source
theorem encard_lt_encard_iff_encard_diff_lt_encard_diff (h : (s ∩ t).Finite) :
    s.encard < t.encard ↔ (s \ t).encard < (t \ s).encard := by
  rw [← encard_diff_add_encard_inter s t, ← encard_diff_add_encard_inter t s, inter_comm t s,
    WithTop.add_lt_add_iff_right h.encard_lt_top.ne]
Extended Cardinality Comparison via Set Differences: $\mathrm{encard}(s) < \mathrm{encard}(t) \leftrightarrow \mathrm{encard}(s \setminus t) < \mathrm{encard}(t \setminus s)$
Informal description
For any two sets $s$ and $t$ with finite intersection $s \cap t$, the extended cardinality of $s$ is strictly less than that of $t$ if and only if the extended cardinality of the set difference $s \setminus t$ is strictly less than that of $t \setminus s$. In other words: $$\mathrm{encard}(s) < \mathrm{encard}(t) \leftrightarrow \mathrm{encard}(s \setminus t) < \mathrm{encard}(t \setminus s)$$
Set.encard_union_le theorem
(s t : Set α) : (s ∪ t).encard ≤ s.encard + t.encard
Full source
theorem encard_union_le (s t : Set α) : (s ∪ t).encard ≤ s.encard + t.encard := by
  rw [← encard_union_add_encard_inter]; exact le_self_add
Extended Cardinality Subadditivity: $\mathrm{encard}(s \cup t) \leq \mathrm{encard}(s) + \mathrm{encard}(t)$
Informal description
For any two sets $s$ and $t$ over a type $\alpha$, the extended cardinality of their union is less than or equal to the sum of their extended cardinalities, i.e., $$\mathrm{encard}(s \cup t) \leq \mathrm{encard}(s) + \mathrm{encard}(t).$$
Set.finite_iff_finite_of_encard_eq_encard theorem
(h : s.encard = t.encard) : s.Finite ↔ t.Finite
Full source
theorem finite_iff_finite_of_encard_eq_encard (h : s.encard = t.encard) : s.Finite ↔ t.Finite := by
  rw [← encard_lt_top_iff, ← encard_lt_top_iff, h]
Finite Sets Have Equal Extended Cardinality if and only if Both Are Finite
Informal description
For any two sets $s$ and $t$ with equal extended cardinality ($\mathrm{encard}(s) = \mathrm{encard}(t)$), the set $s$ is finite if and only if $t$ is finite.
Set.infinite_iff_infinite_of_encard_eq_encard theorem
(h : s.encard = t.encard) : s.Infinite ↔ t.Infinite
Full source
theorem infinite_iff_infinite_of_encard_eq_encard (h : s.encard = t.encard) :
    s.Infinite ↔ t.Infinite := by rw [← encard_eq_top_iff, h, encard_eq_top_iff]
Infinite Sets Have Equal Extended Cardinality if and only if Both Are Infinite
Informal description
For any two sets $s$ and $t$ with equal extended cardinality ($\mathrm{encard}(s) = \mathrm{encard}(t)$), the set $s$ is infinite if and only if $t$ is infinite.
Set.Finite.finite_of_encard_le theorem
{s : Set α} {t : Set β} (hs : s.Finite) (h : t.encard ≤ s.encard) : t.Finite
Full source
theorem Finite.finite_of_encard_le {s : Set α} {t : Set β} (hs : s.Finite)
    (h : t.encard ≤ s.encard) : t.Finite :=
  encard_lt_top_iff.1 (h.trans_lt hs.encard_lt_top)
Finite Sets Preserved Under Extended Cardinality Comparison
Informal description
For any finite set $s$ in a type $\alpha$ and any set $t$ in a type $\beta$, if the extended cardinality of $t$ is less than or equal to that of $s$, then $t$ is also finite.
Set.Finite.encard_lt_encard theorem
(hs : s.Finite) (h : s ⊂ t) : s.encard < t.encard
Full source
theorem Finite.encard_lt_encard (hs : s.Finite) (h : s ⊂ t) : s.encard < t.encard :=
  (encard_mono h.subset).lt_of_ne fun he ↦ h.ne (hs.eq_of_subset_of_encard_le h.subset he.symm.le)
Strict Monotonicity of Extended Cardinality for Finite Strict Subsets: $\mathrm{encard}(s) < \mathrm{encard}(t)$ when $s \subset t$ and $s$ is finite
Informal description
For any finite set $s$ and any set $t$ such that $s$ is a strict subset of $t$, the extended cardinality of $s$ is strictly less than the extended cardinality of $t$, i.e., $\mathrm{encard}(s) < \mathrm{encard}(t)$.
Set.encard_strictMono theorem
[Finite α] : StrictMono (encard : Set α → ℕ∞)
Full source
theorem encard_strictMono [Finite α] : StrictMono (encard : Set α → ℕ∞) :=
  fun _ _ h ↦ (toFinite _).encard_lt_encard h
Strict Monotonicity of Extended Cardinality on Finite Types
Informal description
For any finite type $\alpha$, the extended cardinality function $\mathrm{encard} : \mathcal{P}(\alpha) \to \mathbb{N}_\infty$ is strictly monotone. That is, for any sets $s, t \subseteq \alpha$, if $s \subset t$, then $\mathrm{encard}(s) < \mathrm{encard}(t)$.
Set.encard_diff_add_encard theorem
(s t : Set α) : (s \ t).encard + t.encard = (s ∪ t).encard
Full source
theorem encard_diff_add_encard (s t : Set α) : (s \ t).encard + t.encard = (s ∪ t).encard := by
  rw [← encard_union_eq disjoint_sdiff_left, diff_union_self]
Extended Cardinality Identity for Set Difference and Union: $\mathrm{encard}(s \setminus t) + \mathrm{encard}(t) = \mathrm{encard}(s \cup t)$
Informal description
For any sets $s$ and $t$ over a type $\alpha$, the sum of the extended cardinality of the set difference $s \setminus t$ and the extended cardinality of $t$ equals the extended cardinality of the union $s \cup t$, i.e., $$\mathrm{encard}(s \setminus t) + \mathrm{encard}(t) = \mathrm{encard}(s \cup t).$$
Set.encard_le_encard_diff_add_encard theorem
(s t : Set α) : s.encard ≤ (s \ t).encard + t.encard
Full source
theorem encard_le_encard_diff_add_encard (s t : Set α) : s.encard ≤ (s \ t).encard + t.encard :=
  (encard_mono subset_union_left).trans_eq (encard_diff_add_encard _ _).symm
Inequality for Extended Cardinality of Set Difference and Union: $\mathrm{encard}(s) \leq \mathrm{encard}(s \setminus t) + \mathrm{encard}(t)$
Informal description
For any sets $s$ and $t$ over a type $\alpha$, the extended cardinality of $s$ is less than or equal to the sum of the extended cardinalities of the set difference $s \setminus t$ and the set $t$, i.e., $$\mathrm{encard}(s) \leq \mathrm{encard}(s \setminus t) + \mathrm{encard}(t).$$
Set.tsub_encard_le_encard_diff theorem
(s t : Set α) : s.encard - t.encard ≤ (s \ t).encard
Full source
theorem tsub_encard_le_encard_diff (s t : Set α) : s.encard - t.encard ≤ (s \ t).encard := by
  rw [tsub_le_iff_left, add_comm]; apply encard_le_encard_diff_add_encard
Extended Cardinality Difference Inequality: $\mathrm{encard}(s) - \mathrm{encard}(t) \leq \mathrm{encard}(s \setminus t)$
Informal description
For any sets $s$ and $t$ over a type $\alpha$, the difference between the extended cardinalities of $s$ and $t$ is less than or equal to the extended cardinality of the set difference $s \setminus t$, i.e., $$\mathrm{encard}(s) - \mathrm{encard}(t) \leq \mathrm{encard}(s \setminus t).$$
Set.encard_add_encard_compl theorem
(s : Set α) : s.encard + sᶜ.encard = (univ : Set α).encard
Full source
theorem encard_add_encard_compl (s : Set α) : s.encard + sᶜ.encard = (univ : Set α).encard := by
  rw [← encard_union_eq disjoint_compl_right, union_compl_self]
Extended Cardinality Sum of Set and Complement: $\mathrm{encard}(s) + \mathrm{encard}(s^c) = \mathrm{encard}(\text{univ})$
Informal description
For any set $s$ in a type $\alpha$, the sum of the extended cardinalities of $s$ and its complement $s^c$ equals the extended cardinality of the universal set, i.e., $\mathrm{encard}(s) + \mathrm{encard}(s^c) = \mathrm{encard}(\text{univ})$.
Set.encard_insert_le theorem
(s : Set α) (x : α) : (insert x s).encard ≤ s.encard + 1
Full source
theorem encard_insert_le (s : Set α) (x : α) : (insert x s).encard ≤ s.encard + 1 := by
  rw [← union_singleton, ← encard_singleton x]; apply encard_union_le
Upper Bound on Extended Cardinality of Insertion: $\mathrm{encard}(\mathrm{insert}\,x\,s) \leq \mathrm{encard}(s) + 1$
Informal description
For any set $s$ over a type $\alpha$ and any element $x \in \alpha$, the extended cardinality of the set obtained by inserting $x$ into $s$ satisfies $\mathrm{encard}(\mathrm{insert}\,x\,s) \leq \mathrm{encard}(s) + 1$.
Set.encard_singleton_inter theorem
(s : Set α) (x : α) : ({ x } ∩ s).encard ≤ 1
Full source
theorem encard_singleton_inter (s : Set α) (x : α) : ({x}{x} ∩ s).encard ≤ 1 := by
  rw [← encard_singleton x]; exact encard_le_encard inter_subset_left
Extended Cardinality Bound for Singleton Intersection: $\mathrm{encard}(\{x\} \cap s) \leq 1$
Informal description
For any set $s$ in a type $\alpha$ and any element $x \in \alpha$, the extended cardinality of the intersection $\{x\} \cap s$ is at most 1, i.e., \[ \mathrm{encard}(\{x\} \cap s) \leq 1. \]
Set.encard_diff_singleton_add_one theorem
(h : a ∈ s) : (s \ { a }).encard + 1 = s.encard
Full source
theorem encard_diff_singleton_add_one (h : a ∈ s) :
    (s \ {a}).encard + 1 = s.encard := by
  rw [← encard_insert_of_not_mem (fun h ↦ h.2 rfl), insert_diff_singleton, insert_eq_of_mem h]
Extended Cardinality of Set Minus Singleton: $\mathrm{encard}(s \setminus \{a\}) + 1 = \mathrm{encard}(s)$ for $a \in s$
Informal description
For any element $a$ in a set $s$ over a type $\alpha$, the extended cardinality of the set difference $s \setminus \{a\}$ plus one equals the extended cardinality of $s$, i.e., $\mathrm{encard}(s \setminus \{a\}) + 1 = \mathrm{encard}(s)$.
Set.encard_diff_singleton_of_mem theorem
(h : a ∈ s) : (s \ { a }).encard = s.encard - 1
Full source
theorem encard_diff_singleton_of_mem (h : a ∈ s) :
    (s \ {a}).encard = s.encard - 1 := by
  rw [← encard_diff_singleton_add_one h, ← WithTop.add_right_inj WithTop.one_ne_top,
    tsub_add_cancel_of_le (self_le_add_left _ _)]
Extended Cardinality of Set Minus Singleton: $\mathrm{encard}(s \setminus \{a\}) = \mathrm{encard}(s) - 1$ for $a \in s$
Informal description
For any element $a$ in a set $s$ over a type $\alpha$, the extended cardinality of the set difference $s \setminus \{a\}$ is equal to the extended cardinality of $s$ minus one, i.e., \[ \mathrm{encard}(s \setminus \{a\}) = \mathrm{encard}(s) - 1. \]
Set.encard_tsub_one_le_encard_diff_singleton theorem
(s : Set α) (x : α) : s.encard - 1 ≤ (s \ { x }).encard
Full source
theorem encard_tsub_one_le_encard_diff_singleton (s : Set α) (x : α) :
    s.encard - 1 ≤ (s \ {x}).encard := by
  rw [← encard_singleton x]; apply tsub_encard_le_encard_diff
Extended Cardinality Inequality for Set Minus Singleton: $\mathrm{encard}(s) - 1 \leq \mathrm{encard}(s \setminus \{x\})$
Informal description
For any set $s$ over a type $\alpha$ and any element $x \in \alpha$, the extended cardinality of $s$ minus one is less than or equal to the extended cardinality of the set difference $s \setminus \{x\}$, i.e., $$\mathrm{encard}(s) - 1 \leq \mathrm{encard}(s \setminus \{x\}).$$
Set.encard_exchange theorem
(ha : a ∉ s) (hb : b ∈ s) : (insert a (s \ { b })).encard = s.encard
Full source
theorem encard_exchange (ha : a ∉ s) (hb : b ∈ s) : (insert a (s \ {b})).encard = s.encard := by
  rw [encard_insert_of_not_mem, encard_diff_singleton_add_one hb]
  simp_all only [not_true, mem_diff, mem_singleton_iff, false_and, not_false_eq_true]
Exchange Property for Extended Cardinality: $\mathrm{encard}(\{a\} \cup (s \setminus \{b\})) = \mathrm{encard}(s)$ when $a \notin s$ and $b \in s$
Informal description
For any set $s$ and elements $a, b$ such that $a \notin s$ and $b \in s$, the extended cardinality of the set obtained by inserting $a$ into $s \setminus \{b\}$ equals the extended cardinality of $s$, i.e., $\mathrm{encard}(\{a\} \cup (s \setminus \{b\})) = \mathrm{encard}(s)$.
Set.encard_exchange' theorem
(ha : a ∉ s) (hb : b ∈ s) : (insert a s \ { b }).encard = s.encard
Full source
theorem encard_exchange' (ha : a ∉ s) (hb : b ∈ s) : (insertinsert a s \ {b}).encard = s.encard := by
  rw [← insert_diff_singleton_comm (by rintro rfl; exact ha hb), encard_exchange ha hb]
Exchange Property for Extended Cardinality: $\mathrm{encard}((\{a\} \cup s) \setminus \{b\}) = \mathrm{encard}(s)$ when $a \notin s$ and $b \in s$
Informal description
For any set $s$ and elements $a, b$ such that $a \notin s$ and $b \in s$, the extended cardinality of the set obtained by inserting $a$ into $s$ and then removing $b$ equals the extended cardinality of $s$, i.e., $\mathrm{encard}((\{a\} \cup s) \setminus \{b\}) = \mathrm{encard}(s)$.
Set.encard_eq_add_one_iff theorem
{k : ℕ∞} : s.encard = k + 1 ↔ (∃ a t, ¬a ∈ t ∧ insert a t = s ∧ t.encard = k)
Full source
theorem encard_eq_add_one_iff {k : ℕ∞} :
    s.encard = k + 1 ↔ (∃ a t, ¬a ∈ t ∧ insert a t = s ∧ t.encard = k) := by
  refine ⟨fun h ↦ ?_, ?_⟩
  · obtain ⟨a, ha⟩ := nonempty_of_encard_ne_zero (s := s) (by simp [h])
    refine ⟨a, s \ {a}, fun h ↦ h.2 rfl, by rwa [insert_diff_singleton, insert_eq_of_mem], ?_⟩
    rw [← WithTop.add_right_inj WithTop.one_ne_top, ← h,
      encard_diff_singleton_add_one ha]
  rintro ⟨a, t, h, rfl, rfl⟩
  rw [encard_insert_of_not_mem h]
Extended Cardinality Increment Characterization: $\mathrm{encard}(s) = k + 1$ iff $s$ is an insertion of a new element into a set of cardinality $k$
Informal description
For any extended natural number $k \in \mathbb{N}_\infty$, the extended cardinality of a set $s$ satisfies $\mathrm{encard}(s) = k + 1$ if and only if there exists an element $a \notin t$ and a subset $t \subseteq s$ such that $s = \{a\} \cup t$ and $\mathrm{encard}(t) = k$.
Set.encard_pair theorem
{x y : α} (hne : x ≠ y) : ({ x, y } : Set α).encard = 2
Full source
theorem encard_pair {x y : α} (hne : x ≠ y) : ({x, y} : Set α).encard = 2 := by
  rw [encard_insert_of_not_mem (by simpa), ← one_add_one_eq_two,
    WithTop.add_right_inj WithTop.one_ne_top, encard_singleton]
Extended Cardinality of a Pair: $\mathrm{encard}(\{x, y\}) = 2$ for $x \neq y$
Informal description
For any two distinct elements $x$ and $y$ of a type $\alpha$, the extended cardinality of the set $\{x, y\}$ is equal to $2$, i.e., $\mathrm{encard}(\{x, y\}) = 2$.
Set.encard_eq_one theorem
: s.encard = 1 ↔ ∃ x, s = { x }
Full source
theorem encard_eq_one : s.encard = 1 ↔ ∃ x, s = {x} := by
  refine ⟨fun h ↦ ?_, fun ⟨x, hx⟩ ↦ by rw [hx, encard_singleton]⟩
  obtain ⟨x, hx⟩ := nonempty_of_encard_ne_zero (s := s) (by rw [h]; simp)
  exact ⟨x, ((finite_singleton x).eq_of_subset_of_encard_le (by simpa) (by simp [h])).symm⟩
Characterization of Sets with Extended Cardinality One: $\mathrm{encard}(s) = 1 \leftrightarrow s \text{ is a singleton}$
Informal description
For any set $s$, the extended cardinality $\mathrm{encard}(s)$ equals $1$ if and only if $s$ is a singleton set, i.e., there exists an element $x$ such that $s = \{x\}$.
Set.encard_le_one_iff_eq theorem
: s.encard ≤ 1 ↔ s = ∅ ∨ ∃ x, s = { x }
Full source
theorem encard_le_one_iff_eq : s.encard ≤ 1 ↔ s = ∅ ∨ ∃ x, s = {x} := by
  rw [le_iff_lt_or_eq, lt_iff_not_le, ENat.one_le_iff_ne_zero, not_not, encard_eq_zero,
    encard_eq_one]
Extended Cardinality at Most One Characterizes Empty or Singleton Sets: $\mathrm{encard}(s) \leq 1 \leftrightarrow (s = \emptyset \lor \exists x, s = \{x\})$
Informal description
For any set $s$, the extended cardinality $\mathrm{encard}(s)$ is less than or equal to $1$ if and only if $s$ is either empty or a singleton set, i.e., $s = \emptyset$ or there exists an element $x$ such that $s = \{x\}$.
Set.encard_le_one_iff theorem
: s.encard ≤ 1 ↔ ∀ a b, a ∈ s → b ∈ s → a = b
Full source
theorem encard_le_one_iff : s.encard ≤ 1 ↔ ∀ a b, a ∈ s → b ∈ s → a = b := by
  rw [encard_le_one_iff_eq, or_iff_not_imp_left, ← Ne, ← nonempty_iff_ne_empty]
  refine ⟨fun h a b has hbs ↦ ?_,
    fun h ⟨x, hx⟩ ↦ ⟨x, ((singleton_subset_iff.2 hx).antisymm' (fun y hy ↦ h _ _ hy hx))⟩⟩
  obtain ⟨x, rfl⟩ := h ⟨_, has⟩
  rw [(has : a = x), (hbs : b = x)]
Extended Cardinality at Most One Characterizes Equal Elements: $\mathrm{encard}(s) \leq 1 \leftrightarrow \forall a, b \in s, a = b$
Informal description
For any set $s$, the extended cardinality $\mathrm{encard}(s)$ is less than or equal to $1$ if and only if all elements of $s$ are equal, i.e., for any $a, b \in s$, we have $a = b$.
Set.encard_le_one_iff_subsingleton theorem
: s.encard ≤ 1 ↔ s.Subsingleton
Full source
theorem encard_le_one_iff_subsingleton : s.encard ≤ 1 ↔ s.Subsingleton := by
  rw [encard_le_one_iff, Set.Subsingleton]
  tauto
Extended Cardinality at Most One Characterizes Subsingleton Sets: $\mathrm{encard}(s) \leq 1 \leftrightarrow s \text{ is a subsingleton}$
Informal description
For any set $s$, the extended cardinality $\mathrm{encard}(s)$ is at most 1 if and only if $s$ is a subsingleton (i.e., contains at most one distinct element).
Set.one_lt_encard_iff_nontrivial theorem
: 1 < s.encard ↔ s.Nontrivial
Full source
theorem one_lt_encard_iff_nontrivial : 1 < s.encard ↔ s.Nontrivial := by
  rw [← not_iff_not, not_lt, Set.not_nontrivial_iff, ← encard_le_one_iff_subsingleton]
Extended Cardinality Greater Than One Characterizes Nontrivial Sets: $1 < \mathrm{encard}(s) \leftrightarrow s \text{ is nontrivial}$
Informal description
For any set $s$, the extended cardinality $\mathrm{encard}(s)$ is greater than $1$ if and only if $s$ is nontrivial (i.e., contains at least two distinct elements).
Set.one_lt_encard_iff theorem
: 1 < s.encard ↔ ∃ a b, a ∈ s ∧ b ∈ s ∧ a ≠ b
Full source
theorem one_lt_encard_iff : 1 < s.encard ↔ ∃ a b, a ∈ s ∧ b ∈ s ∧ a ≠ b := by
  rw [← not_iff_not, not_exists, not_lt, encard_le_one_iff]; aesop
Extended Cardinality Greater Than One Characterizes Distinct Elements: $1 < \mathrm{encard}(s) \leftrightarrow \exists a, b \in s, a \neq b$
Informal description
For any set $s$, the extended cardinality $\mathrm{encard}(s)$ is greater than $1$ if and only if there exist distinct elements $a, b \in s$ (i.e., $a \neq b$).
Set.exists_ne_of_one_lt_encard theorem
(h : 1 < s.encard) (a : α) : ∃ b ∈ s, b ≠ a
Full source
theorem exists_ne_of_one_lt_encard (h : 1 < s.encard) (a : α) : ∃ b ∈ s, b ≠ a := by
  by_contra! h'
  obtain ⟨b, b', hb, hb', hne⟩ := one_lt_encard_iff.1 h
  apply hne
  rw [h' b hb, h' b' hb']
Existence of Distinct Element in Set with Extended Cardinality Greater Than One
Informal description
For any set $s$ with extended cardinality greater than $1$ (i.e., $\mathrm{encard}(s) > 1$) and any element $a \in s$, there exists another element $b \in s$ such that $b \neq a$.
Set.encard_eq_two theorem
: s.encard = 2 ↔ ∃ x y, x ≠ y ∧ s = { x, y }
Full source
theorem encard_eq_two : s.encard = 2 ↔ ∃ x y, x ≠ y ∧ s = {x, y} := by
  refine ⟨fun h ↦ ?_, fun ⟨x, y, hne, hs⟩ ↦ by rw [hs, encard_pair hne]⟩
  obtain ⟨x, hx⟩ := nonempty_of_encard_ne_zero (s := s) (by rw [h]; simp)
  rw [← insert_eq_of_mem hx, ← insert_diff_singleton, encard_insert_of_not_mem (fun h ↦ h.2 rfl),
    ← one_add_one_eq_two, WithTop.add_right_inj (WithTop.one_ne_top), encard_eq_one] at h
  obtain ⟨y, h⟩ := h
  refine ⟨x, y, by rintro rfl; exact (h.symm.subset rfl).2 rfl, ?_⟩
  rw [← h, insert_diff_singleton, insert_eq_of_mem hx]
Characterization of Sets with Extended Cardinality Two: $\mathrm{encard}(s) = 2 \leftrightarrow s$ is a doubleton with distinct elements
Informal description
For any set $s$, the extended cardinality $\mathrm{encard}(s)$ equals $2$ if and only if there exist distinct elements $x$ and $y$ such that $s = \{x, y\}$.
Set.encard_eq_three theorem
{α : Type u_1} {s : Set α} : encard s = 3 ↔ ∃ x y z, x ≠ y ∧ x ≠ z ∧ y ≠ z ∧ s = { x, y, z }
Full source
theorem encard_eq_three {α : Type u_1} {s : Set α} :
    encardencard s = 3 ↔ ∃ x y z, x ≠ y ∧ x ≠ z ∧ y ≠ z ∧ s = {x, y, z} := by
  refine ⟨fun h ↦ ?_, fun ⟨x, y, z, hxy, hyz, hxz, hs⟩ ↦ ?_⟩
  · obtain ⟨x, hx⟩ := nonempty_of_encard_ne_zero (s := s) (by rw [h]; simp)
    rw [← insert_eq_of_mem hx, ← insert_diff_singleton,
      encard_insert_of_not_mem (fun h ↦ h.2 rfl), (by exact rfl : (3 : ℕ∞) = 2 + 1),
      WithTop.add_right_inj WithTop.one_ne_top, encard_eq_two] at h
    obtain ⟨y, z, hne, hs⟩ := h
    refine ⟨x, y, z, ?_, ?_, hne, ?_⟩
    · rintro rfl; exact (hs.symm.subset (Or.inl rfl)).2 rfl
    · rintro rfl; exact (hs.symm.subset (Or.inr rfl)).2 rfl
    rw [← hs, insert_diff_singleton, insert_eq_of_mem hx]
  rw [hs, encard_insert_of_not_mem, encard_insert_of_not_mem, encard_singleton] <;> aesop
Characterization of Sets with Extended Cardinality Three: $\mathrm{encard}(s) = 3 \leftrightarrow s$ is a tripleton with distinct elements
Informal description
For any set $s$ over a type $\alpha$, the extended cardinality $\mathrm{encard}(s)$ equals $3$ if and only if there exist three distinct elements $x, y, z \in \alpha$ such that $s = \{x, y, z\}$.
Set.Nat.encard_range theorem
(k : ℕ) : {i | i < k}.encard = k
Full source
theorem Nat.encard_range (k : ) : {i | i < k}.encard = k := by
  convert encard_coe_eq_coe_finsetCard (Finset.range k) using 1
  · rw [Finset.coe_range, Iio_def]
  rw [Finset.card_range]
Extended Cardinality of Initial Segment of Natural Numbers: $\mathrm{encard}(\{i \mid i < k\}) = k$
Informal description
For any natural number $k$, the extended cardinality of the set $\{i \mid i < k\}$ is equal to $k$, i.e., $\mathrm{encard}(\{i \mid i < k\}) = k$.
Set.exists_subset_encard_eq theorem
{k : ℕ∞} (hk : k ≤ s.encard) : ∃ t, t ⊆ s ∧ t.encard = k
Full source
theorem exists_subset_encard_eq {k : ℕ∞} (hk : k ≤ s.encard) : ∃ t, t ⊆ s ∧ t.encard = k := by
  revert hk
  refine ENat.nat_induction k (fun _ ↦ ⟨∅, empty_subset _, by simp⟩) (fun n IH hle ↦ ?_) ?_
  · obtain ⟨t₀, ht₀s, ht₀⟩ := IH (le_trans (by simp) hle)
    simp only [Nat.cast_succ] at *
    have hne : t₀ ≠ s := by
      rintro rfl; rw [ht₀, ← Nat.cast_one, ← Nat.cast_add, Nat.cast_le] at hle; simp at hle
    obtain ⟨x, hx⟩ := exists_of_ssubset (ht₀s.ssubset_of_ne hne)
    exact ⟨insert x t₀, insert_subset hx.1 ht₀s, by rw [encard_insert_of_not_mem hx.2, ht₀]⟩
  simp only [top_le_iff, encard_eq_top_iff]
  exact fun _ hi ↦ ⟨s, Subset.rfl, hi⟩
Existence of Subset with Prescribed Extended Cardinality
Informal description
For any extended natural number $k \in \mathbb{N}_\infty$ and any set $s$ such that $k \leq \mathrm{encard}(s)$, there exists a subset $t \subseteq s$ with $\mathrm{encard}(t) = k$.
Set.exists_superset_subset_encard_eq theorem
{k : ℕ∞} (hst : s ⊆ t) (hsk : s.encard ≤ k) (hkt : k ≤ t.encard) : ∃ r, s ⊆ r ∧ r ⊆ t ∧ r.encard = k
Full source
theorem exists_superset_subset_encard_eq {k : ℕ∞}
    (hst : s ⊆ t) (hsk : s.encard ≤ k) (hkt : k ≤ t.encard) :
    ∃ r, s ⊆ r ∧ r ⊆ t ∧ r.encard = k := by
  obtain (hs | hs) := eq_or_ne s.encard 
  · rw [hs, top_le_iff] at hsk; subst hsk; exact ⟨s, Subset.rfl, hst, hs⟩
  obtain ⟨k, rfl⟩ := exists_add_of_le hsk
  obtain ⟨k', hk'⟩ := exists_add_of_le hkt
  have hk : k ≤ encard (t \ s) := by
    rw [← encard_diff_add_encard_of_subset hst, add_comm] at hkt
    exact WithTop.le_of_add_le_add_right hs hkt
  obtain ⟨r', hr', rfl⟩ := exists_subset_encard_eq hk
  refine ⟨s ∪ r', subset_union_left, union_subset hst (hr'.trans diff_subset), ?_⟩
  rw [encard_union_eq (disjoint_of_subset_right hr' disjoint_sdiff_right)]
Existence of Intermediate Set with Prescribed Extended Cardinality: $s \subseteq r \subseteq t$ and $\mathrm{encard}(r) = k$
Informal description
For any extended natural number $k \in \mathbb{N}_\infty$ and any sets $s$ and $t$ such that $s \subseteq t$, $s.\mathrm{encard} \leq k$, and $k \leq t.\mathrm{encard}$, there exists a set $r$ satisfying $s \subseteq r \subseteq t$ with $\mathrm{encard}(r) = k$.
Set.InjOn.encard_image theorem
(h : InjOn f s) : (f '' s).encard = s.encard
Full source
theorem InjOn.encard_image (h : InjOn f s) : (f '' s).encard = s.encard := by
  rw [encard, ENat.card_image_of_injOn h, encard]
Preservation of Extended Cardinality under Injective Images
Informal description
For any function $f : \alpha \to \beta$ and set $s \subseteq \alpha$, if $f$ is injective on $s$, then the extended cardinality of the image $f(s)$ equals the extended cardinality of $s$, i.e., $\mathrm{encard}(f(s)) = \mathrm{encard}(s)$.
Set.encard_congr theorem
(e : s ≃ t) : s.encard = t.encard
Full source
theorem encard_congr (e : s ≃ t) : s.encard = t.encard := by
  rw [← encard_univ_coe, ← encard_univ_coe t, encard_univ, encard_univ, ENat.card_congr e]
Equality of Extended Cardinality under Bijection
Informal description
For any two sets $s$ and $t$ in a type $\alpha$, if there exists a bijection $e : s \simeq t$ between them, then their extended cardinalities are equal, i.e., $\mathrm{encard}(s) = \mathrm{encard}(t)$.
Function.Injective.encard_image theorem
(hf : f.Injective) (s : Set α) : (f '' s).encard = s.encard
Full source
theorem _root_.Function.Injective.encard_image (hf : f.Injective) (s : Set α) :
    (f '' s).encard = s.encard :=
  hf.injOn.encard_image
Extended Cardinality Preservation under Injective Functions
Informal description
For any injective function $f : \alpha \to \beta$ and any subset $s \subseteq \alpha$, the extended cardinality of the image $f(s)$ is equal to the extended cardinality of $s$, i.e., $\mathrm{encard}(f(s)) = \mathrm{encard}(s)$.
Function.Embedding.encard_le theorem
(e : s ↪ t) : s.encard ≤ t.encard
Full source
theorem _root_.Function.Embedding.encard_le (e : s ↪ t) : s.encard ≤ t.encard := by
  rw [← encard_univ_coe, ← e.injective.encard_image, ← Subtype.coe_injective.encard_image]
  exact encard_mono (by simp)
Extended Cardinality Inequality under Injective Embedding: $\mathrm{encard}(s) \leq \mathrm{encard}(t)$
Informal description
For any injective function embedding $e \colon s \hookrightarrow t$ between sets $s$ and $t$, the extended cardinality of $s$ is less than or equal to the extended cardinality of $t$, i.e., $\mathrm{encard}(s) \leq \mathrm{encard}(t)$.
Set.encard_image_le theorem
(f : α → β) (s : Set α) : (f '' s).encard ≤ s.encard
Full source
theorem encard_image_le (f : α → β) (s : Set α) : (f '' s).encard ≤ s.encard := by
  obtain (h | h) := isEmpty_or_nonempty α
  · rw [s.eq_empty_of_isEmpty]; simp
  rw [← (f.invFunOn_injOn_image s).encard_image]
  apply encard_le_encard
  exact f.invFunOn_image_image_subset s
Extended Cardinality Inequality for Function Images: $\mathrm{encard}(f(s)) \leq \mathrm{encard}(s)$
Informal description
For any function $f \colon \alpha \to \beta$ and any subset $s \subseteq \alpha$, the extended cardinality of the image $f(s)$ is less than or equal to the extended cardinality of $s$, i.e., \[ \mathrm{encard}(f(s)) \leq \mathrm{encard}(s). \]
Set.Finite.injOn_of_encard_image_eq theorem
(hs : s.Finite) (h : (f '' s).encard = s.encard) : InjOn f s
Full source
theorem Finite.injOn_of_encard_image_eq (hs : s.Finite) (h : (f '' s).encard = s.encard) :
    InjOn f s := by
  obtain (h' | hne) := isEmpty_or_nonempty α
  · rw [s.eq_empty_of_isEmpty]; simp
  rw [← (f.invFunOn_injOn_image s).encard_image] at h
  rw [injOn_iff_invFunOn_image_image_eq_self]
  exact hs.eq_of_subset_of_encard_le' (f.invFunOn_image_image_subset s) h.symm.le
Injectivity from Finite Set and Equal Extended Cardinality of Image
Informal description
Let $s$ be a finite subset of a type $\alpha$ and $f : \alpha \to \beta$ be a function. If the extended cardinality of the image $f(s)$ equals the extended cardinality of $s$, then $f$ is injective on $s$.
Set.encard_preimage_of_injective_subset_range theorem
(hf : f.Injective) (ht : t ⊆ range f) : (f ⁻¹' t).encard = t.encard
Full source
theorem encard_preimage_of_injective_subset_range (hf : f.Injective) (ht : t ⊆ range f) :
    (f ⁻¹' t).encard = t.encard := by
  rw [← hf.encard_image, image_preimage_eq_inter_range, inter_eq_self_of_subset_left ht]
Extended Cardinality Preservation for Preimages under Injective Functions with Restricted Range
Informal description
For any injective function $f : \alpha \to \beta$ and any subset $t \subseteq \beta$ that is contained in the range of $f$, the extended cardinality of the preimage $f^{-1}(t)$ is equal to the extended cardinality of $t$, i.e., $\mathrm{encard}(f^{-1}(t)) = \mathrm{encard}(t)$.
Set.encard_preimage_of_bijective theorem
(hf : f.Bijective) (t : Set β) : (f ⁻¹' t).encard = t.encard
Full source
lemma encard_preimage_of_bijective (hf : f.Bijective) (t : Set β) : (f ⁻¹' t).encard = t.encard :=
  encard_preimage_of_injective_subset_range hf.injective (by simp [hf.surjective.range_eq])
Extended Cardinality Preservation for Preimages under Bijective Functions
Informal description
For any bijective function $f : \alpha \to \beta$ and any subset $t \subseteq \beta$, the extended cardinality of the preimage $f^{-1}(t)$ is equal to the extended cardinality of $t$, i.e., $\mathrm{encard}(f^{-1}(t)) = \mathrm{encard}(t)$.
Set.encard_le_encard_of_injOn theorem
(hf : MapsTo f s t) (f_inj : InjOn f s) : s.encard ≤ t.encard
Full source
theorem encard_le_encard_of_injOn (hf : MapsTo f s t) (f_inj : InjOn f s) :
    s.encard ≤ t.encard := by
  rw [← f_inj.encard_image]; apply encard_le_encard; rintro _ ⟨x, hx, rfl⟩; exact hf hx
Extended Cardinality Inequality under Injective Maps
Informal description
For any function $f : \alpha \to \beta$ and sets $s \subseteq \alpha$, $t \subseteq \beta$, if $f$ maps $s$ into $t$ and is injective on $s$, then the extended cardinality of $s$ is less than or equal to the extended cardinality of $t$, i.e., \[ \mathrm{encard}(s) \leq \mathrm{encard}(t). \]
Set.Finite.exists_injOn_of_encard_le theorem
[Nonempty β] {s : Set α} {t : Set β} (hs : s.Finite) (hle : s.encard ≤ t.encard) : ∃ (f : α → β), s ⊆ f ⁻¹' t ∧ InjOn f s
Full source
theorem Finite.exists_injOn_of_encard_le [Nonempty β] {s : Set α} {t : Set β} (hs : s.Finite)
    (hle : s.encard ≤ t.encard) : ∃ (f : α → β), s ⊆ f ⁻¹' t ∧ InjOn f s := by
  classical
  obtain (rfl | h | ⟨a, has, -⟩) := s.eq_empty_or_encard_eq_top_or_encard_diff_singleton_lt
  · simp
  · exact (encard_ne_top_iff.mpr hs h).elim
  obtain ⟨b, hbt⟩ := encard_pos.1 ((encard_pos.2 ⟨_, has⟩).trans_le hle)
  have hle' : (s \ {a}).encard ≤ (t \ {b}).encard := by
    rwa [← WithTop.add_le_add_iff_right WithTop.one_ne_top,
    encard_diff_singleton_add_one has, encard_diff_singleton_add_one hbt]

  obtain ⟨f₀, hf₀s, hinj⟩ := exists_injOn_of_encard_le hs.diff hle'
  simp only [preimage_diff, subset_def, mem_diff, mem_singleton_iff, mem_preimage, and_imp] at hf₀s

  use Function.update f₀ a b
  rw [← insert_eq_of_mem has, ← insert_diff_singleton, injOn_insert (fun h ↦ h.2 rfl)]
  simp only [mem_diff, mem_singleton_iff, not_true, and_false, insert_diff_singleton, subset_def,
    mem_insert_iff, mem_preimage, ne_eq, Function.update_apply, forall_eq_or_imp, ite_true, and_imp,
    mem_image, ite_eq_left_iff, not_exists, not_and, not_forall, exists_prop, and_iff_right hbt]

  refine ⟨?_, ?_, fun x hxs hxa ↦ ⟨hxa, (hf₀s x hxs hxa).2⟩⟩
  · rintro x hx; split_ifs with h
    · assumption
    · exact (hf₀s x hx h).1
  exact InjOn.congr hinj (fun x ⟨_, hxa⟩ ↦ by rwa [Function.update_of_ne])
termination_by encard s
Existence of Injection from Finite Set with Smaller or Equal Extended Cardinality
Informal description
Let $\alpha$ and $\beta$ be nonempty types, and let $s \subseteq \alpha$ and $t \subseteq \beta$ be sets. If $s$ is finite and the extended cardinality of $s$ is less than or equal to that of $t$, then there exists an injective function $f : \alpha \to \beta$ such that $s$ is contained in the preimage of $t$ under $f$ and $f$ is injective on $s$.
Set.Finite.exists_bijOn_of_encard_eq theorem
[Nonempty β] (hs : s.Finite) (h : s.encard = t.encard) : ∃ (f : α → β), BijOn f s t
Full source
theorem Finite.exists_bijOn_of_encard_eq [Nonempty β] (hs : s.Finite) (h : s.encard = t.encard) :
    ∃ (f : α → β), BijOn f s t := by
  obtain ⟨f, hf, hinj⟩ := hs.exists_injOn_of_encard_le h.le; use f
  convert hinj.bijOn_image
  rw [(hs.image f).eq_of_subset_of_encard_le (image_subset_iff.mpr hf)
    (h.symm.trans hinj.encard_image.symm).le]
Existence of Bijection between Finite Sets with Equal Extended Cardinality
Informal description
Let $\alpha$ and $\beta$ be nonempty types, and let $s \subseteq \alpha$ and $t \subseteq \beta$ be sets. If $s$ is finite and the extended cardinality of $s$ equals that of $t$, then there exists a bijective function $f \colon \alpha \to \beta$ such that $f$ restricts to a bijection between $s$ and $t$.
Set.tacticToFinite_tac definition
: Lean.ParserDescr✝
Full source
/-- A tactic (for use in default params) that applies `Set.toFinite` to synthesize a `Set.Finite`
  term. -/
syntax "toFinite_tac" : tactic
Tactic for synthesizing finiteness proofs
Informal description
A tactic called `toFinite_tac` that synthesizes a proof term `s.Finite` for a set `s` by applying `Set.toFinite`. This tactic is designed to be used in default parameters where finiteness of a set needs to be automatically derived, particularly when working with sets in a type that has a `Finite` instance.
Set.ncard definition
(s : Set α) : ℕ
Full source
/-- The cardinality of `s : Set α` . Has the junk value `0` if `s` is infinite -/
noncomputable def ncard (s : Set α) :  := ENat.toNat s.encard
Natural cardinality of a set (with zero for infinite sets)
Informal description
The cardinality of a set \( s \) as a natural number, defined as the conversion of its extended cardinality (an element of \(\mathbb{N}_\infty\)) to a natural number. If \( s \) is infinite, the value is \( 0 \).
Set.ncard_def theorem
(s : Set α) : s.ncard = ENat.toNat s.encard
Full source
theorem ncard_def (s : Set α) : s.ncard = ENat.toNat s.encard := rfl
Natural Cardinality as Conversion of Extended Cardinality
Informal description
For any set $s$ over a type $\alpha$, the natural cardinality of $s$ (denoted as $\text{ncard}(s)$) is equal to the conversion of its extended cardinality (denoted as $\text{encard}(s)$) to a natural number, where $\text{encard}(s)$ is an element of the extended natural numbers $\mathbb{N}_\infty$ and $\text{ENat.toNat}$ maps $\infty$ to $0$.
Set.ncard_le_encard theorem
(s : Set α) : s.ncard ≤ s.encard
Full source
lemma ncard_le_encard (s : Set α) : s.ncard ≤ s.encard := ENat.coe_toNat_le_self _
Natural Cardinality Bound: $\text{ncard}(s) \leq \text{encard}(s)$
Informal description
For any set $s$ of type $\alpha$, the natural cardinality of $s$ (denoted by $\text{ncard}(s)$) is less than or equal to its extended cardinality (denoted by $\text{encard}(s)$). In other words, $\text{ncard}(s) \leq \text{encard}(s)$.
Set.Nat.card_coe_set_eq theorem
(s : Set α) : Nat.card s = s.ncard
Full source
theorem Nat.card_coe_set_eq (s : Set α) : Nat.card s = s.ncard := by
  obtain (h | h) := s.finite_or_infinite
  · have := h.fintype
    rw [ncard, h.encard_eq_coe_toFinset_card, Nat.card_eq_fintype_card,
      toFinite_toFinset, toFinset_card, ENat.toNat_coe]
  have := infinite_coe_iff.2 h
  rw [ncard, h.encard_eq, Nat.card_eq_zero_of_infinite, ENat.toNat_top]
Equality of Subtype Cardinality and Natural Set Cardinality
Informal description
For any set $s$ over a type $\alpha$, the cardinality of the subtype corresponding to $s$ (as a natural number) is equal to the natural cardinality of $s$ (where infinite sets are assigned the value $0$), i.e., $\mathrm{Nat.card}(s) = \mathrm{ncard}(s)$.
Set.ncard_eq_toFinset_card' theorem
(s : Set α) [Fintype s] : s.ncard = s.toFinset.card
Full source
theorem ncard_eq_toFinset_card' (s : Set α) [Fintype s] :
    s.ncard = s.toFinset.card := by
  simp [← Nat.card_coe_set_eq, Nat.card_eq_fintype_card]
Equality of Set Cardinality and Finset Cardinality for Finite Sets
Informal description
For any set $s$ of type $\alpha$ that is finite (i.e., has a `Fintype` instance), the natural cardinality of $s$ (as defined by `Set.ncard`) is equal to the cardinality of the finite set representation of $s$ (as defined by `Finset.card` on `s.toFinset`).
Set.cast_ncard theorem
{s : Set α} (hs : s.Finite) : (s.ncard : Cardinal) = Cardinal.mk s
Full source
lemma cast_ncard {s : Set α} (hs : s.Finite) :
    (s.ncard : Cardinal) = Cardinal.mk s := @Nat.cast_card _ hs
Equality of Natural and Cardinal Number Cardinality for Finite Sets
Informal description
For any finite set $s$ of type $\alpha$, the cardinality of $s$ as a natural number (denoted by $\mathrm{ncard}(s)$) is equal to the cardinality of $s$ as a cardinal number (denoted by $\mathrm{Cardinal.mk}(s)$). In other words, the natural number cardinality and the cardinal number cardinality coincide for finite sets.
Set.encard_le_coe_iff_finite_ncard_le theorem
{k : ℕ} : s.encard ≤ k ↔ s.Finite ∧ s.ncard ≤ k
Full source
theorem encard_le_coe_iff_finite_ncard_le {k : } : s.encard ≤ k ↔ s.Finite ∧ s.ncard ≤ k := by
  rw [encard_le_coe_iff, and_congr_right_iff]
  exact fun hfin ↦ ⟨fun ⟨n₀, hn₀, hle⟩ ↦ by rwa [ncard_def, hn₀, ENat.toNat_coe],
    fun h ↦ ⟨s.ncard, by rw [hfin.cast_ncard_eq], h⟩⟩
Extended Cardinality Bound Characterization via Finite Sets and Natural Cardinality
Informal description
For any set $s$ and natural number $k$, the extended cardinality $\mathrm{encard}(s) \leq k$ if and only if $s$ is finite and its natural cardinality $\mathrm{ncard}(s) \leq k$.
Set.ncard_mono theorem
[Finite α] : @Monotone (Set α) _ _ _ ncard
Full source
theorem ncard_mono [Finite α] : @Monotone (Set α) _ _ _ ncard := fun _ _ ↦ ncard_le_ncard
Monotonicity of Natural Cardinality for Finite Types
Informal description
For any finite type $\alpha$, the function $\mathrm{ncard}$ is monotone with respect to the subset relation on sets. That is, if $s \subseteq t$ for sets $s, t \subseteq \alpha$, then $\mathrm{ncard}(s) \leq \mathrm{ncard}(t)$.
Set.ncard_coe_Finset theorem
(s : Finset α) : (s : Set α).ncard = s.card
Full source
@[simp, norm_cast] theorem ncard_coe_Finset (s : Finset α) : (s : Set α).ncard = s.card := by
  rw [ncard_eq_toFinset_card _, Finset.finite_toSet_toFinset]
Equality of Set Cardinality and Finset Cardinality for Coerced Finsets
Informal description
For any finset $s$ of type $\alpha$, the natural number cardinality of the underlying set of $s$ is equal to the cardinality of $s$ as a finset, i.e., $\mathrm{ncard}(s) = \mathrm{card}(s)$.
Set.ncard_univ theorem
(α : Type*) : (univ : Set α).ncard = Nat.card α
Full source
theorem ncard_univ (α : Type*) : (univ : Set α).ncard = Nat.card α := by
  rcases finite_or_infinite α with h | h
  · have hft := Fintype.ofFinite α
    rw [ncard_eq_toFinset_card, Finite.toFinset_univ, Finset.card_univ, Nat.card_eq_fintype_card]
  rw [Nat.card_eq_zero_of_infinite, Infinite.ncard]
  exact infinite_univ
Cardinality of Universal Set Equals Type Cardinality
Informal description
For any type $\alpha$, the natural number cardinality of the universal set $\text{univ} \subseteq \alpha$ equals the cardinality of $\alpha$ as a natural number, i.e., $\mathrm{ncard}(\text{univ}) = \mathrm{Nat.card}(\alpha)$.
Set.ncard_empty theorem
(α : Type*) : (∅ : Set α).ncard = 0
Full source
@[simp] theorem ncard_empty (α : Type*) : ( : Set α).ncard = 0 := by
  rw [ncard_eq_zero]
Cardinality of the Empty Set is Zero
Informal description
For any type $\alpha$, the natural number cardinality of the empty set $\emptyset$ in $\alpha$ is $0$, i.e., $\mathrm{ncard}(\emptyset) = 0$.
Set.finite_of_ncard_ne_zero theorem
(hs : s.ncard ≠ 0) : s.Finite
Full source
theorem finite_of_ncard_ne_zero (hs : s.ncard ≠ 0) : s.Finite :=
  s.finite_or_infinite.elim id fun h ↦ (hs h.ncard).elim
Finite Set from Nonzero Natural Cardinality
Informal description
For any set $s$, if the natural number cardinality of $s$ is nonzero (i.e., $\mathrm{ncard}(s) \neq 0$), then $s$ is finite.
Set.finite_of_ncard_pos theorem
(hs : 0 < s.ncard) : s.Finite
Full source
theorem finite_of_ncard_pos (hs : 0 < s.ncard) : s.Finite :=
  finite_of_ncard_ne_zero hs.ne.symm
Finite Set from Positive Natural Cardinality
Informal description
For any set $s$, if the natural number cardinality of $s$ is positive (i.e., $0 < \mathrm{ncard}(s)$), then $s$ is finite.
Set.nonempty_of_ncard_ne_zero theorem
(hs : s.ncard ≠ 0) : s.Nonempty
Full source
theorem nonempty_of_ncard_ne_zero (hs : s.ncard ≠ 0) : s.Nonempty := by
  rw [nonempty_iff_ne_empty]; rintro rfl; simp at hs
Nonempty Set from Nonzero Cardinality
Informal description
For any set $s$, if the natural number cardinality of $s$ is nonzero (i.e., $\mathrm{ncard}(s) \neq 0$), then $s$ is nonempty.
Set.ncard_singleton theorem
(a : α) : ({ a } : Set α).ncard = 1
Full source
@[simp] theorem ncard_singleton (a : α) : ({a} : Set α).ncard = 1 := by
  simp [ncard]
Cardinality of Singleton Set: $|\{a\}| = 1$
Informal description
For any element $a$ of type $\alpha$, the natural cardinality of the singleton set $\{a\}$ is equal to $1$, i.e., $|\{a\}| = 1$.
Set.ncard_singleton_inter theorem
(a : α) (s : Set α) : ({ a } ∩ s).ncard ≤ 1
Full source
theorem ncard_singleton_inter (a : α) (s : Set α) : ({a}{a} ∩ s).ncard ≤ 1 := by
  rw [← Nat.cast_le (α := ℕ∞), (toFinite _).cast_ncard_eq, Nat.cast_one]
  apply encard_singleton_inter
Cardinality Bound for Singleton Intersection: $|\{a\} \cap s| \leq 1$
Informal description
For any element $a$ of type $\alpha$ and any set $s \subseteq \alpha$, the natural cardinality of the intersection $\{a\} \cap s$ is at most 1, i.e., \[ |\{a\} \cap s| \leq 1. \]
Set.ncard_prod theorem
: (s ×ˢ t).ncard = s.ncard * t.ncard
Full source
@[simp]
theorem ncard_prod : (s ×ˢ t).ncard = s.ncard * t.ncard := by
  simp [ncard, ENat.toNat_mul]
Cardinality of Cartesian Product: $|s \times t| = |s| \cdot |t|$
Informal description
For any sets $s \subseteq \alpha$ and $t \subseteq \beta$, the natural cardinality of their Cartesian product $s \times t$ is equal to the product of their natural cardinalities, i.e., $|s \times t| = |s| \cdot |t|$. Here, $| \cdot |$ denotes the natural cardinality function, which returns $0$ for infinite sets.
Set.ncard_insert_of_mem theorem
{a : α} (h : a ∈ s) : ncard (insert a s) = s.ncard
Full source
theorem ncard_insert_of_mem {a : α} (h : a ∈ s) : ncard (insert a s) = s.ncard := by
  rw [insert_eq_of_mem h]
Cardinality of Set Unchanged by Insertion of Existing Element
Informal description
For any element $a$ in a set $s$ over a type $\alpha$, the cardinality of the set obtained by inserting $a$ into $s$ is equal to the cardinality of $s$, i.e., $|\{a\} \cup s| = |s|$.
Set.ncard_insert_le theorem
(a : α) (s : Set α) : (insert a s).ncard ≤ s.ncard + 1
Full source
theorem ncard_insert_le (a : α) (s : Set α) : (insert a s).ncard ≤ s.ncard + 1 := by
  obtain hs | hs := s.finite_or_infinite
  · to_encard_tac; rw [hs.cast_ncard_eq, (hs.insert _).cast_ncard_eq]; apply encard_insert_le
  rw [(hs.mono (subset_insert a s)).ncard]
  exact Nat.zero_le _
Upper bound on cardinality of insertion: $|\{a\} \cup s| \leq |s| + 1$
Informal description
For any element $a$ of a type $\alpha$ and any set $s \subseteq \alpha$, the natural cardinality of the set obtained by inserting $a$ into $s$ satisfies $|\{a\} \cup s| \leq |s| + 1$, where $|\cdot|$ denotes the natural cardinality function (which returns 0 for infinite sets).
Set.ncard_le_ncard_insert theorem
(a : α) (s : Set α) : s.ncard ≤ (insert a s).ncard
Full source
theorem ncard_le_ncard_insert (a : α) (s : Set α) : s.ncard ≤ (insert a s).ncard := by
  classical
  refine
    s.finite_or_infinite.elim (fun h ↦ ?_) (fun h ↦ by (rw [h.ncard]; exact Nat.zero_le _))
  rw [ncard_insert_eq_ite h]; split_ifs <;> simp
Lower bound on cardinality of insertion: $|s| \leq |\{a\} \cup s|$
Informal description
For any element $a$ of a type $\alpha$ and any set $s \subseteq \alpha$, the natural cardinality of $s$ is less than or equal to the natural cardinality of the set obtained by inserting $a$ into $s$, i.e., $|s| \leq |\{a\} \cup s|$.
Set.ncard_pair theorem
{a b : α} (h : a ≠ b) : ({ a, b } : Set α).ncard = 2
Full source
@[simp] theorem ncard_pair {a b : α} (h : a ≠ b) : ({a, b} : Set α).ncard = 2 := by
  rw [ncard_insert_of_not_mem, ncard_singleton]; simpa
Cardinality of Two-Element Set: $|\{a, b\}| = 2$ when $a \neq b$
Informal description
For any two distinct elements $a$ and $b$ of a type $\alpha$, the natural cardinality of the set $\{a, b\}$ is equal to $2$.
Set.ncard_diff_singleton_le theorem
(s : Set α) (a : α) : (s \ { a }).ncard ≤ s.ncard
Full source
theorem ncard_diff_singleton_le (s : Set α) (a : α) : (s \ {a}).ncard ≤ s.ncard := by
  obtain hs | hs := s.finite_or_infinite
  · apply ncard_le_ncard diff_subset hs
  convert zero_le (α := ) _
  exact (hs.diff (by simp : Set.Finite {a})).ncard
Cardinality Inequality for Set Difference with Singleton: $|s \setminus \{a\}| \leq |s|$
Informal description
For any set $s$ and element $a$ of type $\alpha$, the natural number cardinality of the set difference $s \setminus \{a\}$ is less than or equal to the natural number cardinality of $s$, i.e., \[ |s \setminus \{a\}| \leq |s|. \]
Set.pred_ncard_le_ncard_diff_singleton theorem
(s : Set α) (a : α) : s.ncard - 1 ≤ (s \ { a }).ncard
Full source
theorem pred_ncard_le_ncard_diff_singleton (s : Set α) (a : α) : s.ncard - 1 ≤ (s \ {a}).ncard := by
  rcases s.finite_or_infinite with hs | hs
  · by_cases h : a ∈ s
    · rw [ncard_diff_singleton_of_mem h hs]
    rw [diff_singleton_eq_self h]
    apply Nat.pred_le
  convert Nat.zero_le _
  rw [hs.ncard]
Lower Bound on Cardinality of Set Difference: $|s| - 1 \leq |s \setminus \{a\}|$
Informal description
For any set $s$ over a type $\alpha$ and any element $a \in \alpha$, the predecessor of the natural cardinality of $s$ is less than or equal to the natural cardinality of the set difference $s \setminus \{a\}$, i.e., \[ |s| - 1 \leq |s \setminus \{a\}|. \]
Set.ncard_exchange theorem
{a b : α} (ha : a ∉ s) (hb : b ∈ s) : (insert a (s \ { b })).ncard = s.ncard
Full source
theorem ncard_exchange {a b : α} (ha : a ∉ s) (hb : b ∈ s) : (insert a (s \ {b})).ncard = s.ncard :=
  congr_arg ENat.toNat <| encard_exchange ha hb
Exchange Property for Natural Cardinality: $|\{a\} \cup (s \setminus \{b\})| = |s|$ when $a \notin s$ and $b \in s$
Informal description
For any set $s$ and elements $a, b$ such that $a \notin s$ and $b \in s$, the natural cardinality of the set obtained by inserting $a$ into $s \setminus \{b\}$ equals the natural cardinality of $s$, i.e., $|\{a\} \cup (s \setminus \{b\})| = |s|$.
Set.ncard_exchange' theorem
{a b : α} (ha : a ∉ s) (hb : b ∈ s) : (insert a s \ { b }).ncard = s.ncard
Full source
theorem ncard_exchange' {a b : α} (ha : a ∉ s) (hb : b ∈ s) :
    (insertinsert a s \ {b}).ncard = s.ncard := by
  rw [← ncard_exchange ha hb, ← singleton_union, ← singleton_union, union_diff_distrib,
    @diff_singleton_eq_self _ b {a} fun h ↦ ha (by rwa [← mem_singleton_iff.mp h])]
Exchange Property for Natural Cardinality (Variant): $|(\{a\} \cup s) \setminus \{b\}| = |s|$ when $a \notin s$ and $b \in s$
Informal description
For any set $s$ and elements $a, b$ such that $a \notin s$ and $b \in s$, the natural cardinality of the set obtained by inserting $a$ into $s$ and then removing $b$ equals the natural cardinality of $s$, i.e., $|(\{a\} \cup s) \setminus \{b\}| = |s|$.
Set.ncard_image_of_injOn theorem
(H : Set.InjOn f s) : (f '' s).ncard = s.ncard
Full source
theorem ncard_image_of_injOn (H : Set.InjOn f s) : (f '' s).ncard = s.ncard :=
  congr_arg ENat.toNat <| H.encard_image
Preservation of Natural Cardinality under Injective Images: $|f(s)| = |s|$
Informal description
For any function $f : \alpha \to \beta$ and set $s \subseteq \alpha$, if $f$ is injective on $s$, then the natural cardinality of the image $f(s)$ equals the natural cardinality of $s$, i.e., $|f(s)| = |s|$.
Set.ncard_image_of_injective theorem
(s : Set α) (H : f.Injective) : (f '' s).ncard = s.ncard
Full source
theorem ncard_image_of_injective (s : Set α) (H : f.Injective) : (f '' s).ncard = s.ncard :=
  ncard_image_of_injOn fun _ _ _ _ h ↦ H h
Natural Cardinality Preservation under Injective Functions: $|f(s)| = |s|$
Informal description
For any set $s \subseteq \alpha$ and any injective function $f : \alpha \to \beta$, the natural cardinality of the image $f(s)$ equals the natural cardinality of $s$, i.e., $|f(s)| = |s|$.
Set.ncard_preimage_of_injective_subset_range theorem
{s : Set β} (H : f.Injective) (hs : s ⊆ Set.range f) : (f ⁻¹' s).ncard = s.ncard
Full source
theorem ncard_preimage_of_injective_subset_range {s : Set β} (H : f.Injective)
    (hs : s ⊆ Set.range f) :
    (f ⁻¹' s).ncard = s.ncard := by
  rw [← ncard_image_of_injective _ H, image_preimage_eq_iff.mpr hs]
Natural Cardinality Preservation under Injective Preimages: $|f^{-1}(s)| = |s|$ when $s \subseteq \mathrm{range}(f)$
Informal description
For any injective function $f : \alpha \to \beta$ and any subset $s \subseteq \beta$ that is contained in the range of $f$, the natural cardinality of the preimage $f^{-1}(s)$ equals the natural cardinality of $s$, i.e., $|f^{-1}(s)| = |s|$.
Set.ncard_map theorem
(f : α ↪ β) : (f '' s).ncard = s.ncard
Full source
@[simp] theorem ncard_map (f : α ↪ β) : (f '' s).ncard = s.ncard :=
  ncard_image_of_injective _ f.inj'
Natural Cardinality Preservation under Injective Embeddings: $|f(s)| = |s|$
Informal description
For any injective function embedding $f : \alpha \hookrightarrow \beta$ and any subset $s \subseteq \alpha$, the natural cardinality of the image $f(s)$ equals the natural cardinality of $s$, i.e., $|f(s)| = |s|$.
Set.ncard_subtype theorem
(P : α → Prop) (s : Set α) : {x : Subtype P | (x : α) ∈ s}.ncard = (s ∩ setOf P).ncard
Full source
@[simp] theorem ncard_subtype (P : α → Prop) (s : Set α) :
    { x : Subtype P | (x : α) ∈ s }.ncard = (s ∩ setOf P).ncard := by
  convert (ncard_image_of_injective _ (@Subtype.coe_injective _ P)).symm
  ext x
  simp [← and_assoc, exists_eq_right]
Natural Cardinality of Subtype Equals Intersection Cardinality: $|\{x \in s \mid P(x)\}| = |s \cap \{x \mid P(x)\}|$
Informal description
For any predicate $P : \alpha \to \text{Prop}$ and any set $s \subseteq \alpha$, the natural cardinality of the subtype $\{x : \text{Subtype } P \mid x \in s\}$ is equal to the natural cardinality of the intersection $s \cap \{x \mid P x\}$.
Set.ncard_strictMono theorem
[Finite α] : @StrictMono (Set α) _ _ _ ncard
Full source
theorem ncard_strictMono [Finite α] : @StrictMono (Set α) _ _ _ ncard :=
  fun _ _ h ↦ ncard_lt_ncard h
Strict Monotonicity of Natural Cardinality on Finite Types: $s \subsetneq t \Rightarrow |s| < |t|$
Informal description
For a finite type $\alpha$, the function that maps a subset $s \subseteq \alpha$ to its natural cardinality $|s|$ is strictly monotone with respect to the subset relation. That is, for any subsets $s, t \subseteq \alpha$, if $s \subsetneq t$, then $|s| < |t|$.
Set.ncard_eq_of_bijective theorem
{n : ℕ} (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.ncard = n
Full source
theorem ncard_eq_of_bijective {n : } (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.ncard = n := by
  let f' : Fin n → α := fun i ↦ f i.val i.is_lt
  suffices himage : s = f' '' Set.univ by
    rw [← Fintype.card_fin n, ← Nat.card_eq_fintype_card, ← Set.ncard_univ, himage]
    exact ncard_image_of_injOn <| fun i _hi j _hj h ↦ Fin.ext <| f_inj i.val j.val i.is_lt j.is_lt h
  ext x
  simp only [image_univ, mem_range]
  refine ⟨fun hx ↦ ?_, fun ⟨⟨i, hi⟩, hx⟩ ↦ hx ▸ hf' i hi⟩
  obtain ⟨i, hi, rfl⟩ := hf x hx
  use ⟨i, hi⟩
Bijective Correspondence Implies Cardinality Equality: $|s| = n$
Informal description
Let $s$ be a set in type $\alpha$ and $n$ a natural number. Suppose there exists a function $f$ defined for all $i < n$ with values in $\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$ (values land in $s$), 3. For any $i,j < n$, if $f(i) = f(j)$ then $i = j$ (injectivity). Then the natural cardinality of $s$ equals $n$, i.e., $|s| = n$.
Set.ncard_congr theorem
{t : Set β} (f : ∀ a ∈ s, β) (h₁ : ∀ a ha, f a ha ∈ t) (h₂ : ∀ a b ha hb, f a ha = f b hb → a = b) (h₃ : ∀ b ∈ t, ∃ a ha, f a ha = b) : s.ncard = t.ncard
Full source
theorem ncard_congr {t : Set β} (f : ∀ a ∈ s, β) (h₁ : ∀ a ha, f a ha ∈ t)
    (h₂ : ∀ a b ha hb, f a ha = f b hb → a = b) (h₃ : ∀ b ∈ t, ∃ a ha, f a ha = b) :
    s.ncard = t.ncard := by
  set f' : s → t := fun x ↦ ⟨f x.1 x.2, h₁ _ _⟩
  have hbij : f'.Bijective := by
    constructor
    · rintro ⟨x, hx⟩ ⟨y, hy⟩ hxy
      simp only [f', Subtype.mk.injEq] at hxy ⊢
      exact h₂ _ _ hx hy hxy
    rintro ⟨y, hy⟩
    obtain ⟨a, ha, rfl⟩ := h₃ y hy
    simp only [Subtype.mk.injEq, Subtype.exists]
    exact ⟨_, ha, rfl⟩
  simp_rw [← Nat.card_coe_set_eq]
  exact Nat.card_congr (Equiv.ofBijective f' hbij)
Cardinality Equality via Bijection Between Sets
Informal description
Let $s$ be a set in type $\alpha$ and $t$ a set in type $\beta$. Given a function $f$ defined on elements of $s$ (i.e., for each $a \in s$, $f(a) \in \beta$) satisfying: 1. $f$ maps into $t$ (i.e., for all $a \in s$, $f(a) \in t$), 2. $f$ is injective (i.e., for any $a, b \in s$, if $f(a) = f(b)$ then $a = b$), 3. $f$ is surjective onto $t$ (i.e., for every $b \in t$, there exists $a \in s$ with $f(a) = b$), then the cardinalities of $s$ and $t$ are equal, i.e., $|s| = |t|$.
Set.ncard_coe theorem
{α : Type*} (s : Set α) : Set.ncard (Set.univ : Set (Set.Elem s)) = s.ncard
Full source
@[simp] theorem ncard_coe {α : Type*} (s : Set α) :
    Set.ncard (Set.univ : Set (Set.Elem s)) = s.ncard :=
  Set.ncard_congr (fun a ha ↦ ↑a) (fun a ha ↦ a.prop) (by simp) (by simp)
Cardinality Equality Between Universal Set of Elements and Original Set
Informal description
For any set $s$ of type $\alpha$, the cardinality of the universal set over the elements of $s$ (i.e., the set of all elements of $s$) is equal to the cardinality of $s$ itself. In other words, $|\mathrm{univ} : \mathrm{Set}(\mathrm{Elem}\, s)| = |s|$.
Set.ncard_graphOn theorem
(s : Set α) (f : α → β) : (s.graphOn f).ncard = s.ncard
Full source
@[simp] lemma ncard_graphOn (s : Set α) (f : α → β) : (s.graphOn f).ncard = s.ncard := by
  rw [← ncard_image_of_injOn fst_injOn_graph, image_fst_graphOn]
Cardinality of Graph Equals Cardinality of Domain
Informal description
For any set $s \subseteq \alpha$ and any function $f : \alpha \to \beta$, the natural cardinality of the graph of $f$ on $s$ (i.e., $\{(x, f(x)) \mid x \in s\}$) equals the natural cardinality of $s$ itself. In other words, $|\mathrm{graph}(f)| = |s|$.
Set.ncard_union_le theorem
(s t : Set α) : (s ∪ t).ncard ≤ s.ncard + t.ncard
Full source
theorem ncard_union_le (s t : Set α) : (s ∪ t).ncard ≤ s.ncard + t.ncard := by
  obtain (h | h) := (s ∪ t).finite_or_infinite
  · to_encard_tac
    rw [h.cast_ncard_eq, (h.subset subset_union_left).cast_ncard_eq,
      (h.subset subset_union_right).cast_ncard_eq]
    apply encard_union_le
  rw [h.ncard]
  apply zero_le
Subadditivity of natural cardinality for set union: $|s \cup t| \leq |s| + |t|$
Informal description
For any two sets $s$ and $t$ over a type $\alpha$, the natural cardinality of their union satisfies the inequality: $$|s \cup t| \leq |s| + |t|,$$ where $|\cdot|$ denotes the natural cardinality (with $|s|=0$ if $s$ is infinite).
Set.cast_ncard_sdiff theorem
{R : Type*} [AddGroupWithOne R] (hst : s ⊆ t) (ht : t.Finite) : ((t \ s).ncard : R) = t.ncard - s.ncard
Full source
lemma cast_ncard_sdiff {R : Type*} [AddGroupWithOne R] (hst : s ⊆ t) (ht : t.Finite) :
    ((t \ s).ncard : R) = t.ncard - s.ncard := by
  rw [ncard_diff hst (ht.subset hst), Nat.cast_sub (ncard_le_ncard hst ht)]
Cardinality of Set Difference under Cast: $|t \setminus s|_R = |t| - |s|$
Informal description
Let $R$ be a type with an additive group structure with one, and let $s$ and $t$ be sets in a type $\alpha$ such that $s \subseteq t$ and $t$ is finite. Then, the natural cardinality of the set difference $t \setminus s$, when cast to $R$, satisfies: $$|t \setminus s|_R = |t| - |s|,$$ where $|\cdot|$ denotes the natural cardinality (with $|s|=0$ if $s$ is infinite).
Set.even_ncard_compl_iff theorem
[Finite α] (heven : Even (Nat.card α)) (s : Set α) : Even sᶜ.ncard ↔ Even s.ncard
Full source
lemma even_ncard_compl_iff [Finite α] (heven : Even (Nat.card α)) (s : Set α) :
    EvenEven sᶜ.ncard ↔ Even s.ncard := by
  simp [compl_eq_univ_diff, ncard_diff (subset_univ _ : s ⊆ Set.univ),
    Nat.even_sub (ncard_le_ncard (subset_univ _ : s ⊆ Set.univ)),
    (ncard_univ _).symm ▸ heven]
Parity of Set Complement Cardinality: $\text{Even}(|s^c|) \leftrightarrow \text{Even}(|s|)$ for Finite Type with Even Cardinality
Informal description
Let $\alpha$ be a finite type with an even number of elements, and let $s$ be a subset of $\alpha$. Then the complement of $s$ has an even cardinality if and only if $s$ itself has an even cardinality, i.e., $\text{Even}(|s^c|) \leftrightarrow \text{Even}(|s|)$.
Set.odd_ncard_compl_iff theorem
[Finite α] (heven : Even (Nat.card α)) (s : Set α) : Odd sᶜ.ncard ↔ Odd s.ncard
Full source
lemma odd_ncard_compl_iff [Finite α] (heven : Even (Nat.card α)) (s : Set α) :
    OddOdd sᶜ.ncard ↔ Odd s.ncard := by
  rw [← Nat.not_even_iff_odd, even_ncard_compl_iff heven, Nat.not_even_iff_odd]
Parity of Set Complement Cardinality: $\text{Odd}(|s^c|) \leftrightarrow \text{Odd}(|s|)$ for Finite Type with Even Cardinality
Informal description
Let $\alpha$ be a finite type with an even number of elements, and let $s$ be a subset of $\alpha$. Then the complement of $s$ has an odd cardinality if and only if $s$ itself has an odd cardinality, i.e., $\text{Odd}(|s^c|) \leftrightarrow \text{Odd}(|s|)$.
Set.exists_subsuperset_card_eq theorem
{n : ℕ} (hst : s ⊆ t) (hsn : s.ncard ≤ n) (hnt : n ≤ t.ncard) : ∃ u, s ⊆ u ∧ u ⊆ t ∧ u.ncard = 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 {n : } (hst : s ⊆ t) (hsn : s.ncard ≤ n) (hnt : n ≤ t.ncard) :
    ∃ u, s ⊆ u ∧ u ⊆ t ∧ u.ncard = n := by
  obtain ht | ht := t.infinite_or_finite
  · rw [ht.ncard, Nat.le_zero, ← ht.ncard] at hnt
    exact ⟨t, hst, Subset.rfl, hnt.symm⟩
  lift s to Finset α using ht.subset hst
  lift t to Finset α using ht
  obtain ⟨u, hsu, hut, hu⟩ := Finset.exists_subsuperset_card_eq (mod_cast hst) (by simpa using hsn)
    (mod_cast hnt)
  exact ⟨u, mod_cast hsu, mod_cast hut, mod_cast hu⟩
Existence of Intermediate Set with Given Cardinality
Informal description
Let $s$ and $t$ be sets with $s \subseteq t$, and let $n$ be a natural number such that $\mathrm{ncard}(s) \leq n \leq \mathrm{ncard}(t)$. Then there exists a set $u$ satisfying $s \subseteq u \subseteq t$ with $\mathrm{ncard}(u) = n$.
Set.exists_subset_card_eq theorem
{n : ℕ} (hns : n ≤ s.ncard) : ∃ t ⊆ s, t.ncard = n
Full source
/-- We can shrink a set to any smaller size. -/
lemma exists_subset_card_eq {n : } (hns : n ≤ s.ncard) : ∃ t ⊆ s, t.ncard = n := by
  simpa using exists_subsuperset_card_eq s.empty_subset (by simp) hns
Existence of Subset with Specified Cardinality: $t \subseteq s$ with $|t| = n$
Informal description
For any set $s$ and any natural number $n$ such that $n \leq \mathrm{ncard}(s)$, there exists a subset $t \subseteq s$ with $\mathrm{ncard}(t) = n$.
Set.Infinite.exists_subset_ncard_eq theorem
{s : Set α} (hs : s.Infinite) (k : ℕ) : ∃ t, t ⊆ s ∧ t.Finite ∧ t.ncard = k
Full source
theorem Infinite.exists_subset_ncard_eq {s : Set α} (hs : s.Infinite) (k : ) :
    ∃ t, t ⊆ s ∧ t.Finite ∧ t.ncard = k := by
  have := hs.to_subtype
  obtain ⟨t', -, rfl⟩ := @Infinite.exists_subset_card_eq s univ infinite_univ k
  refine ⟨Subtype.val '' (t' : Set s), by simp, Finite.image _ (by simp), ?_⟩
  rw [ncard_image_of_injective _ Subtype.coe_injective]
  simp
Existence of Finite Subsets with Arbitrary Cardinality in Infinite Sets
Informal description
For any infinite set $s$ of type $\alpha$ and any natural number $k$, there exists a subset $t \subseteq s$ that is finite and has cardinality $k$, i.e., $|t| = k$.
Set.Infinite.exists_superset_ncard_eq theorem
{s t : Set α} (ht : t.Infinite) (hst : s ⊆ t) (hs : s.Finite) {k : ℕ} (hsk : s.ncard ≤ k) : ∃ s', s ⊆ s' ∧ s' ⊆ t ∧ s'.ncard = k
Full source
theorem Infinite.exists_superset_ncard_eq {s t : Set α} (ht : t.Infinite) (hst : s ⊆ t)
    (hs : s.Finite) {k : } (hsk : s.ncard ≤ k) : ∃ s', s ⊆ s' ∧ s' ⊆ t ∧ s'.ncard = k := by
  obtain ⟨s₁, hs₁, hs₁fin, hs₁card⟩ := (ht.diff hs).exists_subset_ncard_eq (k - s.ncard)
  refine ⟨s ∪ s₁, subset_union_left, union_subset hst (hs₁.trans diff_subset), ?_⟩
  rwa [ncard_union_eq (disjoint_of_subset_right hs₁ disjoint_sdiff_right) hs hs₁fin, hs₁card,
    add_tsub_cancel_of_le]
Existence of Superset with Specified Cardinality in Infinite Set: $s \subseteq s' \subseteq t$ with $|s'| = k$
Informal description
Let $s$ and $t$ be sets in a type $\alpha$, where $t$ is infinite, $s$ is a finite subset of $t$, and $k$ is a natural number such that the cardinality of $s$ is at most $k$. Then there exists a set $s'$ such that $s \subseteq s' \subseteq t$ and the cardinality of $s'$ is exactly $k$.
Set.exists_subset_or_subset_of_two_mul_lt_ncard theorem
{n : ℕ} (hst : 2 * n < (s ∪ t).ncard) : ∃ r : Set α, n < r.ncard ∧ (r ⊆ s ∨ r ⊆ t)
Full source
theorem exists_subset_or_subset_of_two_mul_lt_ncard {n : } (hst : 2 * n < (s ∪ t).ncard) :
    ∃ r : Set α, n < r.ncard ∧ (r ⊆ s ∨ r ⊆ t) := by
  classical
  have hu := finite_of_ncard_ne_zero ((Nat.zero_le _).trans_lt hst).ne.symm
  rw [ncard_eq_toFinset_card _ hu,
    Finite.toFinset_union (hu.subset subset_union_left)
      (hu.subset subset_union_right)] at hst
  obtain ⟨r', hnr', hr'⟩ := Finset.exists_subset_or_subset_of_two_mul_lt_card hst
  exact ⟨r', by simpa, by simpa using hr'⟩
Subset Existence from Union Cardinality Condition: $2n < \mathrm{ncard}(s \cup t) \implies \exists r \subseteq s \text{ or } r \subseteq t, n < \mathrm{ncard}(r)$
Informal description
For any natural number $n$ and any sets $s$ and $t$ in a type $\alpha$, if $2n$ is strictly less than the natural number cardinality of $s \cup t$, then there exists a subset $r$ of either $s$ or $t$ whose cardinality is strictly greater than $n$. In other words, if $2n < \mathrm{ncard}(s \cup t)$, then there exists $r \subseteq s$ or $r \subseteq t$ such that $n < \mathrm{ncard}(r)$.
Set.ncard_eq_one theorem
: s.ncard = 1 ↔ ∃ a, s = { a }
Full source
@[simp] theorem ncard_eq_one : s.ncard = 1 ↔ ∃ a, s = {a} := by
  refine ⟨fun h ↦ ?_, by rintro ⟨a, rfl⟩; rw [ncard_singleton]⟩
  have hft := (finite_of_ncard_ne_zero (ne_zero_of_eq_one h)).fintype
  simp_rw [ncard_eq_toFinset_card', @Finset.card_eq_one _ (toFinset s)] at h
  refine h.imp fun a ha ↦ ?_
  simp_rw [Set.ext_iff, mem_singleton_iff]
  simp only [Finset.ext_iff, mem_toFinset, Finset.mem_singleton] at ha
  exact ha
Characterization of Singleton Sets via Cardinality: $|s| = 1 \iff \exists a, s = \{a\}$
Informal description
For any set $s$, the natural number cardinality of $s$ equals 1 if and only if $s$ is a singleton set, i.e., there exists an element $a$ such that $s = \{a\}$.
Set.ncard_le_one_iff_subsingleton theorem
[Finite s] : s.ncard ≤ 1 ↔ s.Subsingleton
Full source
@[simp] theorem ncard_le_one_iff_subsingleton [Finite s] :
    s.ncard ≤ 1 ↔ s.Subsingleton :=
  ncard_le_one <| inferInstanceAs (Finite s)
Cardinality Bound Characterizes Subsingleton Sets: $|s| \leq 1 \leftrightarrow s \text{ is a subsingleton}$
Informal description
For a finite set $s$, the natural cardinality of $s$ is at most one if and only if $s$ is a subsingleton (i.e., contains at most one element). In other words, $|s| \leq 1 \leftrightarrow \forall x y \in s, x = y$.
Set.ncard_le_one_of_subsingleton theorem
[Subsingleton α] (s : Set α) : s.ncard ≤ 1
Full source
/-- A `Set` of a subsingleton type has cardinality at most one. -/
theorem ncard_le_one_of_subsingleton [Subsingleton α] (s : Set α) : s.ncard ≤ 1 := by
  rw [ncard_eq_toFinset_card]
  exact Finset.card_le_one_of_subsingleton _
Cardinality Bound for Sets in a Subsingleton Type: $\mathrm{ncard}(s) \leq 1$
Informal description
For any set $s$ in a subsingleton type $\alpha$, the natural cardinality of $s$ is at most one, i.e., $\mathrm{ncard}(s) \leq 1$.
Set.exists_ne_of_one_lt_ncard theorem
(hs : 1 < s.ncard) (a : α) : ∃ b, b ∈ s ∧ b ≠ a
Full source
theorem exists_ne_of_one_lt_ncard (hs : 1 < s.ncard) (a : α) : ∃ b, b ∈ s ∧ b ≠ a := by
  have hsf := finite_of_ncard_ne_zero (zero_lt_one.trans hs).ne.symm
  rw [ncard_eq_toFinset_card _ hsf] at hs
  simpa only [Finite.mem_toFinset] using Finset.exists_ne_of_one_lt_card hs a
Existence of Distinct Element in Set with Cardinality Greater Than One
Informal description
For any set $s$ with natural number cardinality greater than 1 (i.e., $\mathrm{ncard}(s) > 1$) and any element $a \in \alpha$, there exists an element $b \in s$ such that $b \neq a$.
Set.ncard_eq_two theorem
: s.ncard = 2 ↔ ∃ x y, x ≠ y ∧ s = { x, y }
Full source
theorem ncard_eq_two : s.ncard = 2 ↔ ∃ x y, x ≠ y ∧ s = {x, y} := by
  rw [← encard_eq_two, ncard_def]
  simp
Characterization of Sets with Cardinality Two: $\mathrm{ncard}(s) = 2 \leftrightarrow s$ is a doubleton with distinct elements
Informal description
For any set $s$, the natural number cardinality $\mathrm{ncard}(s)$ equals $2$ if and only if there exist distinct elements $x$ and $y$ such that $s = \{x, y\}$.
Set.ncard_eq_three theorem
: s.ncard = 3 ↔ ∃ x y z, x ≠ y ∧ x ≠ z ∧ y ≠ z ∧ s = { x, y, z }
Full source
theorem ncard_eq_three : s.ncard = 3 ↔ ∃ x y z, x ≠ y ∧ x ≠ z ∧ y ≠ z ∧ s = {x, y, z} := by
  rw [← encard_eq_three, ncard_def]
  simp
Characterization of Sets with Cardinality Three: $\mathrm{ncard}(s) = 3 \leftrightarrow s$ is a tripleton with distinct elements
Informal description
For any set $s$, the natural number cardinality $\mathrm{ncard}(s)$ equals $3$ if and only if there exist distinct elements $x, y, z$ such that $s = \{x, y, z\}$.