doc-next-gen

Mathlib.Data.Finset.Sort

Module docstring

{"# Construct a sorted list from a finset. ","### sort "}

Finset.sort definition
(s : Finset α) : List α
Full source
/-- `sort s` constructs a sorted list from the unordered set `s`.
  (Uses merge sort algorithm.) -/
def sort (s : Finset α) : List α :=
  Multiset.sort r s.1
Sorted list from finite set via merge sort
Informal description
Given a finite set `s` over a type `α` equipped with a total, antisymmetric, and transitive relation `r`, the function `sort` constructs a sorted list from the elements of `s` using the merge sort algorithm. The resulting list is sorted with respect to the relation `r`.
Finset.sort_val theorem
(s : Finset α) : Multiset.sort r s.val = sort r s
Full source
@[simp]
theorem sort_val (s : Finset α) : Multiset.sort r s.val = sort r s :=
  rfl
Equality of Sorted Lists from Multiset and Finset Construction
Informal description
For any finite set $s$ of type $\alpha$, the sorted list obtained by applying the merge sort algorithm to the underlying multiset of $s$ (with respect to a total, antisymmetric, and transitive relation $r$) is equal to the sorted list obtained by applying the merge sort algorithm directly to $s$ (with respect to $r$).
Finset.sort_mk theorem
{s : Multiset α} (h : s.Nodup) : sort r ⟨s, h⟩ = s.sort r
Full source
@[simp]
theorem sort_mk {s : Multiset α} (h : s.Nodup) : sort r ⟨s, h⟩ = s.sort r := rfl
Equality of Sorted Lists from Finset and Multiset Construction
Informal description
Given a multiset `s` over a type `α` with no duplicate elements (as witnessed by `h : s.Nodup`), and given a total, antisymmetric, and transitive relation `r` on `α`, the sorted list constructed from the finset `⟨s, h⟩` (where `s` is the underlying multiset and `h` proves it has no duplicates) is equal to the sorted list constructed directly from the multiset `s` using the relation `r`.
Finset.sort_sorted theorem
(s : Finset α) : List.Sorted r (sort r s)
Full source
@[simp]
theorem sort_sorted (s : Finset α) : List.Sorted r (sort r s) :=
  Multiset.sort_sorted _ _
Sortedness of Merge Sort on Finite Sets
Informal description
For any finite set $s$ over a type $\alpha$ equipped with a total, antisymmetric, and transitive relation $r$, the list obtained by sorting the elements of $s$ with respect to $r$ is sorted with respect to $r$.
Finset.sort_eq theorem
(s : Finset α) : ↑(sort r s) = s.1
Full source
@[simp]
theorem sort_eq (s : Finset α) : ↑(sort r s) = s.1 :=
  Multiset.sort_eq _ _
Sorted List from Finite Set Preserves Elements
Informal description
For any finite set $s$ over a type $\alpha$ equipped with a total, antisymmetric, and transitive relation $r$, the underlying multiset of the sorted list obtained from $s$ via merge sort is equal to the multiset of elements of $s$.
Finset.sort_nodup theorem
(s : Finset α) : (sort r s).Nodup
Full source
@[simp]
theorem sort_nodup (s : Finset α) : (sort r s).Nodup :=
  (by rw [sort_eq]; exact s.2 : @Multiset.Nodup α (sort r s))
Sorted List from Finite Set Has No Duplicates
Informal description
For any finite set $s$ over a type $\alpha$ equipped with a total, antisymmetric, and transitive relation $r$, the list obtained by sorting the elements of $s$ with respect to $r$ has no duplicate elements.
Finset.sort_toFinset theorem
[DecidableEq α] (s : Finset α) : (sort r s).toFinset = s
Full source
@[simp]
theorem sort_toFinset [DecidableEq α] (s : Finset α) : (sort r s).toFinset = s :=
  List.toFinset_eq (sort_nodup r s) ▸ eq_of_veq (sort_eq r s)
Sorted List to Finite Set Conversion Preserves Original Set: $\mathrm{toFinset}(\mathrm{sort}_r(s)) = s$
Informal description
For any finite set $s$ of elements of type $\alpha$ with decidable equality, converting the sorted list of $s$ (with respect to a total, antisymmetric, and transitive relation $r$) back to a finite set yields the original set $s$. In other words, $\mathrm{toFinset}(\mathrm{sort}_r(s)) = s$.
Finset.mem_sort theorem
{s : Finset α} {a : α} : a ∈ sort r s ↔ a ∈ s
Full source
@[simp]
theorem mem_sort {s : Finset α} {a : α} : a ∈ sort r sa ∈ sort r s ↔ a ∈ s :=
  Multiset.mem_sort _
Membership Preservation in Sorted List from Finite Set: $a \in \mathrm{sort}_r(s) \leftrightarrow a \in s$
Informal description
For any finite set $s$ of elements of type $\alpha$ and any element $a \in \alpha$, the element $a$ belongs to the sorted list obtained from $s$ (with respect to a total, antisymmetric, and transitive relation $r$) if and only if $a$ belongs to $s$.
Finset.length_sort theorem
{s : Finset α} : (sort r s).length = s.card
Full source
@[simp]
theorem length_sort {s : Finset α} : (sort r s).length = s.card :=
  Multiset.length_sort _
Length of Sorted List Equals Cardinality of Finite Set
Informal description
For any finite set $s$ of elements of type $\alpha$, the length of the sorted list obtained from $s$ (with respect to a total, antisymmetric, and transitive relation $r$) is equal to the cardinality of $s$.
Finset.sort_empty theorem
: sort r ∅ = []
Full source
@[simp]
theorem sort_empty : sort r  = [] :=
  Multiset.sort_zero r
Empty set sorts to empty list: $\mathrm{sort}_r(\emptyset) = []$
Informal description
For any total, antisymmetric, and transitive relation $r$ on a type $\alpha$, the sorted list of the empty finite set $\emptyset$ is the empty list $[]$.
Finset.sort_singleton theorem
(a : α) : sort r { a } = [a]
Full source
@[simp]
theorem sort_singleton (a : α) : sort r {a} = [a] :=
  Multiset.sort_singleton r a
Sorted List of Singleton Finite Set is Singleton List
Informal description
For any element $a$ of type $\alpha$, the sorted list of the singleton finite set $\{a\}$ with respect to a relation $r$ is the singleton list $[a]$.
Finset.sort_cons theorem
{a : α} {s : Finset α} (h₁ : ∀ b ∈ s, r a b) (h₂ : a ∉ s) : sort r (cons a s h₂) = a :: sort r s
Full source
theorem sort_cons {a : α} {s : Finset α} (h₁ : ∀ b ∈ s, r a b) (h₂ : a ∉ s) :
    sort r (cons a s h₂) = a :: sort r s := by
  rw [sort, cons_val, Multiset.sort_cons r a _ h₁, sort_val]
Consistency of Sorting with Insertion in Finite Sets
Informal description
Let $\alpha$ be a type equipped with a total, antisymmetric, and transitive relation $r$. For any element $a \in \alpha$ and finite set $s \subset \alpha$ such that $a \notin s$, if $r(a, b)$ holds for all $b \in s$, then the sorted list of the finite set $\{a\} \cup s$ with respect to $r$ is equal to $a$ prepended to the sorted list of $s$ with respect to $r$. In other words, $\text{sort}_r(\{a\} \cup s) = a :: \text{sort}_r(s)$ under the given conditions.
Finset.sort_insert theorem
[DecidableEq α] {a : α} {s : Finset α} (h₁ : ∀ b ∈ s, r a b) (h₂ : a ∉ s) : sort r (insert a s) = a :: sort r s
Full source
theorem sort_insert [DecidableEq α] {a : α} {s : Finset α} (h₁ : ∀ b ∈ s, r a b) (h₂ : a ∉ s) :
    sort r (insert a s) = a :: sort r s := by
  rw [← cons_eq_insert _ _ h₂, sort_cons r h₁]
Insertion Preserves Sorted Order in Finite Sets
Informal description
Let $\alpha$ be a type with a decidable equality, equipped with a total, antisymmetric, and transitive relation $r$. For any element $a \in \alpha$ and finite set $s \subset \alpha$ such that $a \notin s$, if $r(a, b)$ holds for all $b \in s$, then the sorted list (with respect to $r$) of the finite set $\{a\} \cup s$ is equal to $a$ prepended to the sorted list of $s$. That is, $\text{sort}_r(\text{insert}(a, s)) = a :: \text{sort}_r(s)$.
Finset.sort_range theorem
(n : ℕ) : sort (· ≤ ·) (range n) = List.range n
Full source
@[simp]
theorem sort_range (n : ) : sort (· ≤ ·) (range n) = List.range n :=
  Multiset.sort_range n
Sorted Range List Equals Range List for Natural Numbers
Informal description
For any natural number $n$, the sorted list (with respect to the standard ordering $\leq$) of the finite set $\{0, 1, \dots, n-1\}$ is equal to the list $[0, 1, \dots, n-1]$.
Finset.sort_perm_toList theorem
(s : Finset α) : sort r s ~ s.toList
Full source
theorem sort_perm_toList (s : Finset α) : sortsort r s ~ s.toList := by
  rw [← Multiset.coe_eq_coe]
  simp only [coe_toList, sort_eq]
Sorted List from Finite Set is Permutation of Original List
Informal description
For any finite set $s$ over a type $\alpha$ equipped with a total, antisymmetric, and transitive relation $r$, the sorted list obtained from $s$ via merge sort is a permutation of the list obtained by converting $s$ to a list.
List.toFinset_sort theorem
[DecidableEq α] {l : List α} (hl : l.Nodup) : sort r l.toFinset = l ↔ l.Sorted r
Full source
theorem _root_.List.toFinset_sort [DecidableEq α] {l : List α} (hl : l.Nodup) :
    sortsort r l.toFinset = l ↔ l.Sorted r := by
  refine ⟨?_, List.eq_of_perm_of_sorted ((sort_perm_toList r _).trans (List.toFinset_toList hl))
    (sort_sorted r _)⟩
  intro h
  rw [← h]
  exact sort_sorted r _
Equivalence of Sorted Finite Set and Original List for Distinct Elements
Informal description
Let $\alpha$ be a type with a decidable equality, and let $r$ be a total, antisymmetric, and transitive relation on $\alpha$. For any list $l$ of distinct elements of $\alpha$, the sorted version of the finite set constructed from $l$ is equal to $l$ if and only if $l$ is already sorted with respect to $r$.
Finset.sort_sorted_lt theorem
(s : Finset α) : List.Sorted (· < ·) (sort (· ≤ ·) s)
Full source
theorem sort_sorted_lt (s : Finset α) : List.Sorted (· < ·) (sort (· ≤ ·) s) :=
  (sort_sorted _ _).lt_of_le (sort_nodup _ _)
Strictly Sorted List from Finite Set via Non-Strict Order
Informal description
For any finite set $s$ of elements in a linearly ordered type $\alpha$, the list obtained by sorting $s$ with respect to the non-strict order $\leq$ is strictly sorted with respect to the relation $<$.
Finset.sort_sorted_gt theorem
(s : Finset α) : List.Sorted (· > ·) (sort (· ≥ ·) s)
Full source
theorem sort_sorted_gt (s : Finset α) : List.Sorted (· > ·) (sort (· ≥ ·) s) :=
  (sort_sorted _ _).gt_of_ge (sort_nodup _ _)
Strictly Decreasing Sort via $\geq$ Relation
Informal description
For any finite set $s$ of elements in a linearly ordered type $\alpha$, the list obtained by sorting $s$ with respect to the relation $\geq$ is strictly decreasing (sorted with respect to $>$).
Finset.sorted_zero_eq_min'_aux theorem
(s : Finset α) (h : 0 < (s.sort (· ≤ ·)).length) (H : s.Nonempty) : (s.sort (· ≤ ·)).get ⟨0, h⟩ = s.min' H
Full source
theorem sorted_zero_eq_min'_aux (s : Finset α) (h : 0 < (s.sort (· ≤ ·)).length) (H : s.Nonempty) :
    (s.sort (· ≤ ·)).get ⟨0, h⟩ = s.min' H := by
  let l := s.sort (· ≤ ·)
  apply le_antisymm
  · have : s.min' H ∈ l := (Finset.mem_sort (α := α) (· ≤ ·)).mpr (s.min'_mem H)
    obtain ⟨i, hi⟩ : ∃ i, l.get i = s.min' H := List.mem_iff_get.1 this
    rw [← hi]
    exact (s.sort_sorted (· ≤ ·)).rel_get_of_le (Nat.zero_le i)
  · have : l.get ⟨0, h⟩ ∈ s := (Finset.mem_sort (α := α) (· ≤ ·)).1 (List.get_mem l _)
    exact s.min'_le _ this
First Element of Sorted List Equals Minimum in Finite Sets
Informal description
For any nonempty finite set $s$ in a linearly ordered type $\alpha$, if the sorted list of $s$ (with respect to $\leq$) has length greater than 0, then the first element of this sorted list is equal to the minimum element of $s$.
Finset.sorted_zero_eq_min' theorem
{s : Finset α} {h : 0 < (s.sort (· ≤ ·)).length} : (s.sort (· ≤ ·))[0] = s.min' (card_pos.1 <| by rwa [length_sort] at h)
Full source
theorem sorted_zero_eq_min' {s : Finset α} {h : 0 < (s.sort (· ≤ ·)).length} :
    (s.sort (· ≤ ·))[0] = s.min' (card_pos.1 <| by rwa [length_sort] at h) :=
  sorted_zero_eq_min'_aux _ _ _
First Element of Sorted List Equals Minimum in Finite Sets
Informal description
For any nonempty finite set $s$ of elements in a linearly ordered type $\alpha$, the first element of the sorted list of $s$ (with respect to $\leq$) is equal to the minimum element of $s$. Here, the condition $0 < \text{length}(\text{sort}_{\leq}(s))$ ensures the sorted list is nonempty, which is equivalent to $s$ being nonempty.
Finset.min'_eq_sorted_zero theorem
{s : Finset α} {h : s.Nonempty} : s.min' h = (s.sort (· ≤ ·))[0]'(by rw [length_sort]; exact card_pos.2 h)
Full source
theorem min'_eq_sorted_zero {s : Finset α} {h : s.Nonempty} :
    s.min' h = (s.sort (· ≤ ·))[0]'(by rw [length_sort]; exact card_pos.2 h) :=
  (sorted_zero_eq_min'_aux _ _ _).symm
Minimum Element Equals First Element of Sorted List
Informal description
For any nonempty finite set $s$ in a linearly ordered type $\alpha$, the minimum element of $s$ is equal to the first element of the list obtained by sorting $s$ with respect to the $\leq$ relation.
Finset.sorted_last_eq_max'_aux theorem
(s : Finset α) (h : (s.sort (· ≤ ·)).length - 1 < (s.sort (· ≤ ·)).length) (H : s.Nonempty) : (s.sort (· ≤ ·))[(s.sort (· ≤ ·)).length - 1] = s.max' H
Full source
theorem sorted_last_eq_max'_aux (s : Finset α)
    (h : (s.sort (· ≤ ·)).length - 1 < (s.sort (· ≤ ·)).length) (H : s.Nonempty) :
    (s.sort (· ≤ ·))[(s.sort (· ≤ ·)).length - 1] = s.max' H := by
  let l := s.sort (· ≤ ·)
  apply le_antisymm
  · have : l.get ⟨(s.sort (· ≤ ·)).length - 1, h⟩ ∈ s :=
      (Finset.mem_sort (α := α) (· ≤ ·)).1 (List.get_mem l _)
    exact s.le_max' _ this
  · have : s.max' H ∈ l := (Finset.mem_sort (α := α) (· ≤ ·)).mpr (s.max'_mem H)
    obtain ⟨i, hi⟩ : ∃ i, l.get i = s.max' H := List.mem_iff_get.1 this
    rw [← hi]
    exact (s.sort_sorted (· ≤ ·)).rel_get_of_le (Nat.le_sub_one_of_lt i.prop)
Last Element of Sorted Finite Set Equals Maximum Element (Auxiliary Version)
Informal description
For any nonempty finite set $s$ of elements in a linearly ordered type $\alpha$, the last element of the list obtained by sorting $s$ with respect to the $\leq$ relation is equal to the maximum element of $s$. More formally, if $l$ is the sorted list of $s$ (i.e., $l = \mathrm{sort}_{\leq}(s)$) and $h$ is a proof that $l.\mathrm{length} - 1$ is a valid index of $l$, then: $$ l[l.\mathrm{length} - 1] = \max'(s, H) $$ where $H$ is a proof that $s$ is nonempty.
Finset.sorted_last_eq_max' theorem
{s : Finset α} {h : (s.sort (· ≤ ·)).length - 1 < (s.sort (· ≤ ·)).length} : (s.sort (· ≤ ·))[(s.sort (· ≤ ·)).length - 1] = s.max' (by rw [length_sort] at h; exact card_pos.1 (lt_of_le_of_lt bot_le h))
Full source
theorem sorted_last_eq_max' {s : Finset α}
    {h : (s.sort (· ≤ ·)).length - 1 < (s.sort (· ≤ ·)).length} :
    (s.sort (· ≤ ·))[(s.sort (· ≤ ·)).length - 1] =
      s.max' (by rw [length_sort] at h; exact card_pos.1 (lt_of_le_of_lt bot_le h)) :=
  sorted_last_eq_max'_aux _ h _
Last Element of Sorted Finite Set Equals Maximum Element
Informal description
For any nonempty finite set $s$ of elements in a linearly ordered type $\alpha$, the last element of the sorted list of $s$ (with respect to the $\leq$ relation) is equal to the maximum element of $s$. More precisely, if $l$ is the sorted list of $s$ (i.e., $l = \mathrm{sort}_{\leq}(s)$) and $h$ is a proof that $l.\mathrm{length} - 1$ is a valid index of $l$, then: $$ l[l.\mathrm{length} - 1] = \max'(s, H) $$ where $H$ is a proof that $s$ is nonempty (derived from $h$ via the equality $\mathrm{length}(l) = \mathrm{card}(s)$ and the fact that $\mathrm{length}(l) - 1 < \mathrm{length}(l)$ implies $\mathrm{card}(s) > 0$).
Finset.max'_eq_sorted_last theorem
{s : Finset α} {h : s.Nonempty} : s.max' h = (s.sort (· ≤ ·))[(s.sort (· ≤ ·)).length - 1]'(by simpa using Nat.sub_lt (card_pos.mpr h) Nat.zero_lt_one)
Full source
theorem max'_eq_sorted_last {s : Finset α} {h : s.Nonempty} :
    s.max' h =
      (s.sort (· ≤ ·))[(s.sort (· ≤ ·)).length - 1]'
        (by simpa using Nat.sub_lt (card_pos.mpr h) Nat.zero_lt_one) :=
  (sorted_last_eq_max'_aux _ (by simpa using Nat.sub_lt (card_pos.mpr h) Nat.zero_lt_one) _).symm
Maximum of Finite Set Equals Last Element of Sorted List
Informal description
For any nonempty finite set $s$ in a linearly ordered type $\alpha$, the maximum element of $s$ (denoted $\max'(s, h)$ where $h$ is a proof that $s$ is nonempty) is equal to the last element of the list obtained by sorting $s$ with respect to the $\leq$ relation. More precisely, if $l$ is the sorted list of $s$ (i.e., $l = \mathrm{sort}_{\leq}(s)$), then: $$ \max'(s, h) = l[l.\mathrm{length} - 1] $$
Finset.orderIsoOfFin definition
(s : Finset α) {k : ℕ} (h : s.card = k) : Fin k ≃o s
Full source
/-- Given a finset `s` of cardinality `k` in a linear order `α`, the map `orderIsoOfFin s h`
is the increasing bijection between `Fin k` and `s` as an `OrderIso`. Here, `h` is a proof that
the cardinality of `s` is `k`. We use this instead of an iso `Fin s.card ≃o s` to avoid
casting issues in further uses of this function. -/
def orderIsoOfFin (s : Finset α) {k : } (h : s.card = k) : FinFin k ≃o s :=
  OrderIso.trans (Fin.castOrderIso ((length_sort (α := α) (· ≤ ·)).trans h).symm) <|
    (s.sort_sorted_lt.getIso _).trans <| OrderIso.setCongr _ _ <| Set.ext fun _ => mem_sort _
Increasing order isomorphism between $\text{Fin }k$ and a finite set of size $k$
Informal description
Given a finite set $s$ of cardinality $k$ in a linearly ordered type $\alpha$, the function `orderIsoOfFin s h` is the increasing bijection between the finite type $\text{Fin }k$ and the set $s$ as an order isomorphism. Here, $h$ is a proof that the cardinality of $s$ is $k$. This avoids casting issues that would arise from using an isomorphism $\text{Fin }s.\text{card} \simeq_o s$ directly.
Finset.orderEmbOfFin definition
(s : Finset α) {k : ℕ} (h : s.card = k) : Fin k ↪o α
Full source
/-- Given a finset `s` of cardinality `k` in a linear order `α`, the map `orderEmbOfFin s h` is
the increasing bijection between `Fin k` and `s` as an order embedding into `α`. Here, `h` is a
proof that the cardinality of `s` is `k`. We use this instead of an embedding `Fin s.card ↪o α` to
avoid casting issues in further uses of this function. -/
def orderEmbOfFin (s : Finset α) {k : } (h : s.card = k) : FinFin k ↪o α :=
  (orderIsoOfFin s h).toOrderEmbedding.trans (OrderEmbedding.subtype _)
Increasing order embedding from $\text{Fin }k$ to a finite set of size $k$ in a linear order
Informal description
Given a finite set $s$ of cardinality $k$ in a linearly ordered type $\alpha$, the function `orderEmbOfFin s h` is the increasing bijection between the finite type $\text{Fin }k$ and the set $s$ as an order embedding into $\alpha$. Here, $h$ is a proof that the cardinality of $s$ is $k$. This avoids casting issues that would arise from using an embedding $\text{Fin }s.\text{card} \hookrightarrow_o \alpha$ directly.
Finset.coe_orderIsoOfFin_apply theorem
(s : Finset α) {k : ℕ} (h : s.card = k) (i : Fin k) : ↑(orderIsoOfFin s h i) = orderEmbOfFin s h i
Full source
@[simp]
theorem coe_orderIsoOfFin_apply (s : Finset α) {k : } (h : s.card = k) (i : Fin k) :
    ↑(orderIsoOfFin s h i) = orderEmbOfFin s h i :=
  rfl
Equality of Coerced Order Isomorphism and Order Embedding for Finite Sets
Informal description
Let $\alpha$ be a linearly ordered type, $s$ be a finite subset of $\alpha$ with cardinality $k$, and $i$ be an element of $\text{Fin }k$. Then the element of $\alpha$ obtained by applying the order isomorphism $\text{orderIsoOfFin } s h$ to $i$ and then coercing to $\alpha$ is equal to the element obtained by applying the order embedding $\text{orderEmbOfFin } s h$ to $i$.
Finset.orderIsoOfFin_symm_apply theorem
(s : Finset α) {k : ℕ} (h : s.card = k) (x : s) : ↑((s.orderIsoOfFin h).symm x) = (s.sort (· ≤ ·)).idxOf ↑x
Full source
theorem orderIsoOfFin_symm_apply (s : Finset α) {k : } (h : s.card = k) (x : s) :
    ↑((s.orderIsoOfFin h).symm x) = (s.sort (· ≤ ·)).idxOf ↑x :=
  rfl
Index Correspondence in Sorted List via Order Isomorphism Inverse
Informal description
Let $\alpha$ be a linearly ordered type, $s$ be a finite subset of $\alpha$ with cardinality $k$, and $x$ be an element of $s$. Then the index of $x$ in the sorted list of $s$ (with respect to the order $\leq$) is equal to the value obtained by applying the inverse of the order isomorphism between $\text{Fin }k$ and $s$ to $x$.
Finset.orderEmbOfFin_apply theorem
(s : Finset α) {k : ℕ} (h : s.card = k) (i : Fin k) : s.orderEmbOfFin h i = (s.sort (· ≤ ·))[i]'(by rw [length_sort, h]; exact i.2)
Full source
theorem orderEmbOfFin_apply (s : Finset α) {k : } (h : s.card = k) (i : Fin k) :
    s.orderEmbOfFin h i = (s.sort (· ≤ ·))[i]'(by rw [length_sort, h]; exact i.2) :=
  rfl
Order Embedding of Finite Set Equals Sorted List Element
Informal description
Let $\alpha$ be a linearly ordered type, $s$ be a finite subset of $\alpha$ with cardinality $k$, and $i$ be an element of $\text{Fin }k$. Then the image of $i$ under the order embedding $\text{orderEmbOfFin } s h$ is equal to the $i$-th element (0-indexed) of the list obtained by sorting $s$ with respect to the order $\leq$.
Finset.orderEmbOfFin_mem theorem
(s : Finset α) {k : ℕ} (h : s.card = k) (i : Fin k) : s.orderEmbOfFin h i ∈ s
Full source
@[simp]
theorem orderEmbOfFin_mem (s : Finset α) {k : } (h : s.card = k) (i : Fin k) :
    s.orderEmbOfFin h i ∈ s :=
  (s.orderIsoOfFin h i).2
Image of Order Embedding from $\text{Fin }k$ to Finite Set Lies in the Set
Informal description
Let $\alpha$ be a linearly ordered type, $s$ be a finite subset of $\alpha$ with cardinality $k$, and $i$ be an element of $\text{Fin }k$. Then the image of $i$ under the order embedding `orderEmbOfFin s h` is an element of $s$.
Finset.range_orderEmbOfFin theorem
(s : Finset α) {k : ℕ} (h : s.card = k) : Set.range (s.orderEmbOfFin h) = s
Full source
@[simp]
theorem range_orderEmbOfFin (s : Finset α) {k : } (h : s.card = k) :
    Set.range (s.orderEmbOfFin h) = s := by
  simp only [orderEmbOfFin, Set.range_comp ((↑) : _ → α) (s.orderIsoOfFin h),
  RelEmbedding.coe_trans, Set.image_univ, Finset.orderEmbOfFin, RelIso.range_eq,
    OrderEmbedding.coe_subtype, OrderIso.coe_toOrderEmbedding, eq_self_iff_true,
    Subtype.range_coe_subtype, Finset.setOf_mem, Finset.coe_inj]
Range of Increasing Order Embedding from Finite Set Equals the Set Itself
Informal description
For any finite set $s$ of cardinality $k$ in a linearly ordered type $\alpha$, the range of the increasing order embedding $\text{orderEmbOfFin}\ s\ h$ is equal to $s$ itself. Here, $h$ is a proof that the cardinality of $s$ is $k$.
Finset.image_orderEmbOfFin_univ theorem
(s : Finset α) {k : ℕ} (h : s.card = k) : Finset.image (s.orderEmbOfFin h) Finset.univ = s
Full source
@[simp]
theorem image_orderEmbOfFin_univ (s : Finset α) {k : } (h : s.card = k) :
    Finset.image (s.orderEmbOfFin h) Finset.univ = s := by
  apply Finset.coe_injective
  simp
Image of Universal Set under Order Embedding Equals Original Finite Set
Informal description
For any finite set $s$ of cardinality $k$ in a linearly ordered type $\alpha$, the image of the universal finite set under the increasing order embedding $\text{orderEmbOfFin}\ s\ h$ is equal to $s$ itself. Here, $h$ is a proof that the cardinality of $s$ is $k$. In symbols: $$\text{image } (s.\text{orderEmbOfFin } h) \text{ univ} = s$$
Finset.map_orderEmbOfFin_univ theorem
(s : Finset α) {k : ℕ} (h : s.card = k) : Finset.map (s.orderEmbOfFin h).toEmbedding Finset.univ = s
Full source
@[simp]
theorem map_orderEmbOfFin_univ (s : Finset α) {k : } (h : s.card = k) :
    Finset.map (s.orderEmbOfFin h).toEmbedding Finset.univ = s := by
  simp [map_eq_image]
Image of Universal Set under Order Embedding Equals Original Finite Set
Informal description
For any finite set $s$ of cardinality $k$ in a linearly ordered type $\alpha$, the image of the universal finite set under the order embedding $\text{orderEmbOfFin}\ s\ h$ is equal to $s$ itself. Here, $h$ is a proof that the cardinality of $s$ is $k$. In symbols: $$\text{map } (s.\text{orderEmbOfFin } h) \text{ univ} = s$$
Finset.listMap_orderEmbOfFin_finRange theorem
(s : Finset α) {k : ℕ} (h : s.card = k) : (List.finRange k).map (s.orderEmbOfFin h) = s.sort (· ≤ ·)
Full source
@[simp]
theorem listMap_orderEmbOfFin_finRange (s : Finset α) {k : } (h : s.card = k) :
    (List.finRange k).map (s.orderEmbOfFin h) = s.sort (· ≤ ·) := by
  obtain rfl : k = (s.sort (· ≤ ·)).length := by simp [h]
  exact List.finRange_map_getElem (s.sort (· ≤ ·))
Order Embedding Maps FinRange to Sorted List of Finite Set
Informal description
Let $s$ be a finite set of elements of type $\alpha$ with a linear order, and let $k$ be a natural number such that the cardinality of $s$ equals $k$. Then the list obtained by mapping the order embedding `orderEmbOfFin s h` (where $h$ is the proof that $s.\text{card} = k$) over the list of indices $\text{finRange}(k)$ is equal to the list obtained by sorting $s$ with respect to the $\leq$ relation. In symbols: $$\text{map } (s.\text{orderEmbOfFin } h) \text{ finRange}(k) = \text{sort } (\leq) s$$
Finset.orderEmbOfFin_zero theorem
{s : Finset α} {k : ℕ} (h : s.card = k) (hz : 0 < k) : orderEmbOfFin s h ⟨0, hz⟩ = s.min' (card_pos.mp (h.symm ▸ hz))
Full source
/-- The bijection `orderEmbOfFin s h` sends `0` to the minimum of `s`. -/
theorem orderEmbOfFin_zero {s : Finset α} {k : } (h : s.card = k) (hz : 0 < k) :
    orderEmbOfFin s h ⟨0, hz⟩ = s.min' (card_pos.mp (h.symm ▸ hz)) := by
  simp only [orderEmbOfFin_apply, Fin.getElem_fin, sorted_zero_eq_min']
Order Embedding Maps First Index to Minimum Element of Finite Set
Informal description
Let $s$ be a nonempty finite subset of a linearly ordered type $\alpha$ with cardinality $k > 0$, and let $h$ be a proof that $s$ has cardinality $k$. Then the image of the first index $\langle 0, hz \rangle$ under the order embedding $\mathrm{orderEmbOfFin}\ s\ h$ is equal to the minimum element of $s$. In symbols: $$\mathrm{orderEmbOfFin}\ s\ h\ \langle 0, hz \rangle = \min'(s, \mathrm{card\_pos.mp}\ (h.\mathrm{symm} ▸ hz))$$
Finset.orderEmbOfFin_last theorem
{s : Finset α} {k : ℕ} (h : s.card = k) (hz : 0 < k) : orderEmbOfFin s h ⟨k - 1, Nat.sub_lt hz (Nat.succ_pos 0)⟩ = s.max' (card_pos.mp (h.symm ▸ hz))
Full source
/-- The bijection `orderEmbOfFin s h` sends `k-1` to the maximum of `s`. -/
theorem orderEmbOfFin_last {s : Finset α} {k : } (h : s.card = k) (hz : 0 < k) :
    orderEmbOfFin s h ⟨k - 1, Nat.sub_lt hz (Nat.succ_pos 0)⟩ =
      s.max' (card_pos.mp (h.symm ▸ hz)) := by
  simp [orderEmbOfFin_apply, max'_eq_sorted_last, h]
Order Embedding Maps Last Index to Maximum Element of Finite Set
Informal description
Let $s$ be a finite subset of a linearly ordered type $\alpha$ with cardinality $k > 0$, and let $h$ be a proof that $s$ has cardinality $k$. Then the image of the last element $\langle k-1, \ldots \rangle$ under the order embedding $\mathrm{orderEmbOfFin}\ s\ h$ is equal to the maximum element of $s$. In symbols: $$\mathrm{orderEmbOfFin}\ s\ h\ \langle k-1, \ldots \rangle = \max'(s, \mathrm{card\_pos.mp}\ (h.\mathrm{symm} ▸ hz))$$
Finset.orderEmbOfFin_singleton theorem
(a : α) (i : Fin 1) : orderEmbOfFin { a } (card_singleton a) i = a
Full source
/-- `orderEmbOfFin {a} h` sends any argument to `a`. -/
@[simp]
theorem orderEmbOfFin_singleton (a : α) (i : Fin 1) :
    orderEmbOfFin {a} (card_singleton a) i = a := by
  rw [Subsingleton.elim i ⟨0, Nat.zero_lt_one⟩, orderEmbOfFin_zero _ Nat.zero_lt_one,
    min'_singleton]
Order Embedding of Singleton Set Maps All Indices to Its Element
Informal description
For any element $a$ in a linearly ordered type $\alpha$ and any index $i$ in the finite type $\mathrm{Fin}(1)$, the order embedding $\mathrm{orderEmbOfFin}$ applied to the singleton set $\{a\}$ with cardinality proof $\mathrm{card\_singleton}\ a$ and index $i$ yields $a$. In symbols: $$\mathrm{orderEmbOfFin}\ \{a\}\ (\mathrm{card\_singleton}\ a)\ i = a$$
Finset.orderEmbOfFin_unique theorem
{s : Finset α} {k : ℕ} (h : s.card = k) {f : Fin k → α} (hfs : ∀ x, f x ∈ s) (hmono : StrictMono f) : f = s.orderEmbOfFin h
Full source
/-- Any increasing map `f` from `Fin k` to a finset of cardinality `k` has to coincide with
the increasing bijection `orderEmbOfFin s h`. -/
theorem orderEmbOfFin_unique {s : Finset α} {k : } (h : s.card = k) {f : Fin k → α}
    (hfs : ∀ x, f x ∈ s) (hmono : StrictMono f) : f = s.orderEmbOfFin h := by
  rw [← hmono.range_inj (s.orderEmbOfFin h).strictMono, range_orderEmbOfFin, ← Set.image_univ,
    ← coe_univ, ← coe_image, coe_inj]
  refine eq_of_subset_of_card_le (fun x hx => ?_) ?_
  · rcases mem_image.1 hx with ⟨x, _, rfl⟩
    exact hfs x
  · rw [h, card_image_of_injective _ hmono.injective, card_univ, Fintype.card_fin]
Uniqueness of Strictly Increasing Maps from $\mathrm{Fin}(k)$ to a Finite Set of Size $k$
Informal description
Let $s$ be a finite subset of a linearly ordered type $\alpha$ with cardinality $k$, and let $f \colon \mathrm{Fin}(k) \to \alpha$ be a strictly increasing function such that $f(x) \in s$ for all $x \in \mathrm{Fin}(k)$. Then $f$ coincides with the canonical increasing bijection $\mathrm{orderEmbOfFin}\ s\ h$ from $\mathrm{Fin}(k)$ to $s$.
Finset.orderEmbOfFin_unique' theorem
{s : Finset α} {k : ℕ} (h : s.card = k) {f : Fin k ↪o α} (hfs : ∀ x, f x ∈ s) : f = s.orderEmbOfFin h
Full source
/-- An order embedding `f` from `Fin k` to a finset of cardinality `k` has to coincide with
the increasing bijection `orderEmbOfFin s h`. -/
theorem orderEmbOfFin_unique' {s : Finset α} {k : } (h : s.card = k) {f : FinFin k ↪o α}
    (hfs : ∀ x, f x ∈ s) : f = s.orderEmbOfFin h :=
  RelEmbedding.ext <| funext_iff.1 <| orderEmbOfFin_unique h hfs f.strictMono
Uniqueness of Order Embeddings from $\mathrm{Fin}(k)$ to a Finite Set of Size $k$
Informal description
Let $\alpha$ be a linearly ordered type, $s$ be a finite subset of $\alpha$ with cardinality $k$, and $f \colon \mathrm{Fin}(k) \hookrightarrow_o \alpha$ be an order embedding such that $f(x) \in s$ for all $x \in \mathrm{Fin}(k)$. Then $f$ coincides with the canonical increasing bijection $\mathrm{orderEmbOfFin}\ s\ h$ from $\mathrm{Fin}(k)$ to $s$.
Finset.orderEmbOfFin_eq_orderEmbOfFin_iff theorem
{k l : ℕ} {s : Finset α} {i : Fin k} {j : Fin l} {h : s.card = k} {h' : s.card = l} : s.orderEmbOfFin h i = s.orderEmbOfFin h' j ↔ (i : ℕ) = (j : ℕ)
Full source
/-- Two parametrizations `orderEmbOfFin` of the same set take the same value on `i` and `j` if
and only if `i = j`. Since they can be defined on a priori not defeq types `Fin k` and `Fin l`
(although necessarily `k = l`), the conclusion is rather written `(i : ℕ) = (j : ℕ)`. -/
@[simp]
theorem orderEmbOfFin_eq_orderEmbOfFin_iff {k l : } {s : Finset α} {i : Fin k} {j : Fin l}
    {h : s.card = k} {h' : s.card = l} :
    s.orderEmbOfFin h i = s.orderEmbOfFin h' j ↔ (i : ℕ) = (j : ℕ) := by
  substs k l
  exact (s.orderEmbOfFin rfl).eq_iff_eq.trans Fin.ext_iff
Equality of Order Embedding Values on a Finite Set Corresponds to Natural Number Equality of Indices
Informal description
Let $s$ be a finite subset of a linearly ordered type $\alpha$, with cardinality $k$ and $l$ respectively (i.e., $|s| = k = l$). For any two order embeddings $f : \text{Fin }k \hookrightarrow_o \alpha$ and $g : \text{Fin }l \hookrightarrow_o \alpha$ constructed from $s$, and for any indices $i \in \text{Fin }k$ and $j \in \text{Fin }l$, we have $f(i) = g(j)$ if and only if the underlying natural numbers of $i$ and $j$ are equal, i.e., $(i : \mathbb{N}) = (j : \mathbb{N})$.
Finset.orderEmbOfCardLe definition
(s : Finset α) {k : ℕ} (h : k ≤ s.card) : Fin k ↪o α
Full source
/-- Given a finset `s` of size at least `k` in a linear order `α`, the map `orderEmbOfCardLe`
is an order embedding from `Fin k` to `α` whose image is contained in `s`. Specifically, it maps
`Fin k` to an initial segment of `s`. -/
def orderEmbOfCardLe (s : Finset α) {k : } (h : k ≤ s.card) : FinFin k ↪o α :=
  (Fin.castLEOrderEmb h).trans (s.orderEmbOfFin rfl)
Order embedding of initial segment from finite type to finite set
Informal description
Given a finite set $s$ in a linearly ordered type $\alpha$ and a natural number $k$ such that $k \leq |s|$, the function `orderEmbOfCardLe s h` is an order embedding from the finite type $\text{Fin }k$ to $\alpha$, whose image is an initial segment of $s$. Specifically, it first casts elements of $\text{Fin }k$ to $\text{Fin }|s|$ via the inequality $h : k \leq |s|$, and then applies the increasing bijection between $\text{Fin }|s|$ and $s$.
Finset.orderEmbOfCardLe_mem theorem
(s : Finset α) {k : ℕ} (h : k ≤ s.card) (a) : orderEmbOfCardLe s h a ∈ s
Full source
theorem orderEmbOfCardLe_mem (s : Finset α) {k : } (h : k ≤ s.card) (a) :
    orderEmbOfCardLeorderEmbOfCardLe s h a ∈ s := by
  simp only [orderEmbOfCardLe, RelEmbedding.coe_trans, Finset.orderEmbOfFin_mem,
    Function.comp_apply]
Membership in Image of Order Embedding from Finite Type to Finite Set
Informal description
For any finite set $s$ in a linearly ordered type $\alpha$ and any natural number $k$ such that $k \leq |s|$, the image of any element $a$ under the order embedding $\text{orderEmbOfCardLe}_s h$ is contained in $s$.
Finset.instRepr instance
[Repr α] : Repr (Finset α)
Full source
unsafe instance [Repr α] : Repr (Finset α) where
  reprPrec s _ :=
    -- multiset uses `0` not `∅` for empty sets
    if s.card = 0 then "∅" else repr s.1
Representation of Finite Sets
Informal description
For any type $\alpha$ with a representation function, the finite set of elements of type $\alpha$ also has a representation function.
Fin.sort_univ theorem
(n : ℕ) : Finset.univ.sort (fun x y : Fin n => x ≤ y) = List.finRange n
Full source
theorem sort_univ (n : ) : Finset.univ.sort (fun x y : Fin n => x ≤ y) = List.finRange n :=
  List.eq_of_perm_of_sorted
    (List.perm_of_nodup_nodup_toFinset_eq
      (Finset.univ.sort_nodup _) (List.nodup_finRange n) (by simp))
    (Finset.univ.sort_sorted LE.le)
    (List.pairwise_le_finRange n)
Sorted Universal Finite Set of $\mathrm{Fin}(n)$ Equals $\mathrm{finRange}(n)$
Informal description
For any natural number $n$, the sorted list of the universal finite set of $\mathrm{Fin}(n)$ with respect to the natural ordering $\leq$ is equal to the list $[0, 1, \dots, n-1]$.
Fintype.orderIsoFinOfCardEq definition
(α : Type*) [LinearOrder α] [Fintype α] {k : ℕ} (h : Fintype.card α = k) : Fin k ≃o α
Full source
/-- Given a `Fintype` `α` of cardinality `k`, the map `orderIsoFinOfCardEq s h` is the increasing
bijection between `Fin k` and `α` as an `OrderIso`. Here, `h` is a proof that the cardinality of `α`
is `k`. We use this instead of an iso `Fin (Fintype.card α) ≃o α` to avoid casting issues in further
uses of this function. -/
def Fintype.orderIsoFinOfCardEq
    (α : Type*) [LinearOrder α] [Fintype α] {k : } (h : Fintype.card α = k) :
    FinFin k ≃o α :=
  (Finset.univ.orderIsoOfFin h).trans
    ((OrderIso.setCongr _ _ Finset.coe_univ).trans OrderIso.Set.univ)
Order isomorphism between $\text{Fin }k$ and a finite linearly ordered type of cardinality $k$
Informal description
Given a finite type $\alpha$ with a linear order and cardinality $k$, the function `orderIsoFinOfCardEq` constructs an order isomorphism between the finite ordinal type $\text{Fin }k$ and $\alpha$. Here, $h$ is a proof that the cardinality of $\alpha$ is $k$. The isomorphism is built by first creating an order isomorphism between $\text{Fin }k$ and the universal finite set of $\alpha$, then composing with the natural order isomorphism between this set and $\alpha$ itself.
nonempty_orderEmbedding_of_finite_infinite theorem
(α : Type*) [LinearOrder α] [hα : Finite α] (β : Type*) [LinearOrder β] [hβ : Infinite β] : Nonempty (α ↪o β)
Full source
/-- Any finite linear order order-embeds into any infinite linear order. -/
lemma nonempty_orderEmbedding_of_finite_infinite
    (α : Type*) [LinearOrder α] [hα : Finite α]
    (β : Type*) [LinearOrder β] [hβ : Infinite β] : Nonempty (α ↪o β) := by
  haveI := Fintype.ofFinite α
  obtain ⟨s, hs⟩ := Infinite.exists_subset_card_eq β (Fintype.card α)
  exact ⟨((Fintype.orderIsoFinOfCardEq α rfl).symm.toOrderEmbedding).trans (s.orderEmbOfFin hs)⟩
Existence of Order Embedding from Finite to Infinite Linear Orders
Informal description
For any finite linearly ordered type $\alpha$ and any infinite linearly ordered type $\beta$, there exists an order embedding from $\alpha$ into $\beta$.