The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V02. Numbers

Mathlib.Data.Nat.Nth

78 declarations and 1 deprecated aliases. Mathlib docs · Source

The nth Number Satisfying a Predicate

This file defines a function for "what is the nth number that satisfies a given predicate p", and provides lemmas that deal with this function and its connection to Nat.count.

Main definitions

  • Nat.nth p n: The n-th natural k (zero-indexed) such that p k. If there is no such natural (that is, p is true for at most n naturals), then Nat.nth p n = 0.

Main results

  • Nat.nth_eq_orderEmbOfFin: For a finitely-often true p, gives the cardinality of the set of numbers satisfying p above particular values of nth p
  • Nat.gc_count_nth: Establishes a Galois connection between Nat.nth p and Nat.count p.
  • Nat.nth_eq_orderIsoOfNat: For an infinitely-often true predicate, nth agrees with the order-isomorphism of the subtype to the natural numbers.

Implementation notes

Much of the below was written before Set.encard existed and partly for this reason uses the pattern ∀ hf : Set.Finite (Set.ofPred p), n < hf.toFinset.card rather than n < {x | p x}.encard. We should consider changing this.

There has been some discussion on the subject of whether both of nth and Nat.Subtype.orderIsoOfNat should exist. See discussion here. Future work should address how lemmas that use these should be written.

Lemmas about Nat.nth on a finite set

Lemmas about Nat.nth on an infinite set

Lemmas that work for finite and infinite sets

def Nat.nth E3 #

(ℕ → Prop) → ℕ → ℕ

Find the n-th natural number satisfying p (indexed from 0, so nth p 0 is the first natural number satisfying p), or 0 if there is no such number. See also Subtype.orderIsoOfNat for the order isomorphism with ℕ when p is infinitely often true.

theorem Nat.nth_of_card_le E1 #

∀ {p : ℕ → Prop} (hf : (Set.ofPred p).Finite) {n : ℕ}, hf.toFinset.card ≤ n → Nat.nth p n = 0

theorem Nat.nth_eq_getD_sort E1 #

∀ {p : ℕ → Prop} (h : (Set.ofPred p).Finite) (n : ℕ), Nat.nth p n = (h.toFinset.sort fun a b ↦ a ≤ b).getD n 0

theorem Nat.nth_eq_orderEmbOfFin E3 #

∀ {p : ℕ → Prop} (hf : (Set.ofPred p).Finite) {n : ℕ} (hn : n < hf.toFinset.card),
  Nat.nth p n = (hf.toFinset.orderEmbOfFin ⋯) ⟨n, hn⟩

theorem Nat.nth_strictMonoOn E1 #

∀ {p : ℕ → Prop} (hf : (Set.ofPred p).Finite), StrictMonoOn (Nat.nth p) (Set.Iio hf.toFinset.card)

theorem Nat.nth_lt_nth_of_lt_card E1 #

∀ {p : ℕ → Prop} (hf : (Set.ofPred p).Finite) {m n : ℕ}, m < n → n < hf.toFinset.card → Nat.nth p m < Nat.nth p n

theorem Nat.nth_le_nth_of_lt_card E1 #

∀ {p : ℕ → Prop} (hf : (Set.ofPred p).Finite) {m n : ℕ}, m ≤ n → n < hf.toFinset.card → Nat.nth p m ≤ Nat.nth p n

theorem Nat.lt_of_nth_lt_nth_of_lt_card E1 #

∀ {p : ℕ → Prop} (hf : (Set.ofPred p).Finite) {m n : ℕ}, Nat.nth p m < Nat.nth p n → m < hf.toFinset.card → m < n

theorem Nat.le_of_nth_le_nth_of_lt_card E1 #

∀ {p : ℕ → Prop} (hf : (Set.ofPred p).Finite) {m n : ℕ}, Nat.nth p m ≤ Nat.nth p n → m < hf.toFinset.card → m ≤ n

theorem Nat.nth_injOn E1 #

∀ {p : ℕ → Prop} (hf : (Set.ofPred p).Finite), Set.InjOn (Nat.nth p) (Set.Iio hf.toFinset.card)

theorem Nat.range_nth_of_finite E1 #

∀ {p : ℕ → Prop}, (Set.ofPred p).Finite → Set.range (Nat.nth p) = insert 0 (Set.ofPred p)

theorem Nat.image_nth_Iio_card E0 #

∀ {p : ℕ → Prop} (hf : (Set.ofPred p).Finite), Nat.nth p '' Set.Iio hf.toFinset.card = Set.ofPred p

theorem Nat.nth_mem_of_lt_card E1 #

∀ {p : ℕ → Prop} {n : ℕ} (hf : (Set.ofPred p).Finite), n < hf.toFinset.card → p (Nat.nth p n)

theorem Nat.exists_lt_card_finite_nth_eq E0 #

∀ {p : ℕ → Prop} (hf : (Set.ofPred p).Finite) {x : ℕ}, p x → ∃ n < hf.toFinset.card, Nat.nth p n = x

theorem Nat.nth_apply_eq_orderIsoOfNat E1 #

∀ {p : ℕ → Prop} (hf : (Set.ofPred p).Infinite) (n : ℕ), Nat.nth p n = ↑((Nat.Subtype.orderIsoOfNat (Set.ofPred p)) n)

When s is an infinite set, nth agrees with Nat.Subtype.orderIsoOfNat.

theorem Nat.nth_eq_orderIsoOfNat E3 #

∀ {p : ℕ → Prop} (hf : (Set.ofPred p).Infinite), Nat.nth p = Subtype.val ∘ ⇑(Nat.Subtype.orderIsoOfNat (Set.ofPred p))

When s is an infinite set, nth agrees with Nat.Subtype.orderIsoOfNat.

theorem Nat.nth_strictMono E1 #

∀ {p : ℕ → Prop}, (Set.ofPred p).Infinite → StrictMono (Nat.nth p)

theorem Nat.nth_injective E1 #

∀ {p : ℕ → Prop}, (Set.ofPred p).Infinite → Function.Injective (Nat.nth p)

theorem Nat.nth_monotone E1 #

∀ {p : ℕ → Prop}, (Set.ofPred p).Infinite → Monotone (Nat.nth p)

theorem Nat.nth_lt_nth E1 #

∀ {p : ℕ → Prop}, (Set.ofPred p).Infinite → ∀ {k n : ℕ}, Nat.nth p k < Nat.nth p n ↔ k < n

theorem Nat.nth_le_nth E1 #

∀ {p : ℕ → Prop}, (Set.ofPred p).Infinite → ∀ {k n : ℕ}, Nat.nth p k ≤ Nat.nth p n ↔ k ≤ n

theorem Nat.range_nth_of_infinite E1 #

∀ {p : ℕ → Prop}, (Set.ofPred p).Infinite → Set.range (Nat.nth p) = Set.ofPred p

theorem Nat.nth_mem_of_infinite E1 #

∀ {p : ℕ → Prop}, (Set.ofPred p).Infinite → ∀ (n : ℕ), p (Nat.nth p n)

theorem Nat.exists_lt_card_nth_eq E0 #

∀ {p : ℕ → Prop} {x : ℕ}, p x → ∃ n, (∀ (hf : (Set.ofPred p).Finite), n < hf.toFinset.card) ∧ Nat.nth p n = x

theorem Nat.subset_range_nth E1 #

∀ {p : ℕ → Prop}, Set.ofPred p ⊆ Set.range (Nat.nth p)

theorem Nat.range_nth_subset E1 #

∀ {p : ℕ → Prop}, Set.range (Nat.nth p) ⊆ insert 0 (Set.ofPred p)

theorem Nat.nth_mem E1 #

∀ {p : ℕ → Prop} (n : ℕ), (∀ (hf : (Set.ofPred p).Finite), n < hf.toFinset.card) → p (Nat.nth p n)

theorem Nat.nth_lt_nth' E1 #

∀ {p : ℕ → Prop} {m n : ℕ}, m < n → (∀ (hf : (Set.ofPred p).Finite), n < hf.toFinset.card) → Nat.nth p m < Nat.nth p n

theorem Nat.nth_le_nth' E1 #

∀ {p : ℕ → Prop} {m n : ℕ}, m ≤ n → (∀ (hf : (Set.ofPred p).Finite), n < hf.toFinset.card) → Nat.nth p m ≤ Nat.nth p n

theorem Nat.le_nth E1 #

∀ {p : ℕ → Prop} {n : ℕ}, (∀ (hf : (Set.ofPred p).Finite), n < hf.toFinset.card) → n ≤ Nat.nth p n

theorem Nat.isLeast_nth E1 #

∀ {p : ℕ → Prop} {n : ℕ},
  (∀ (hf : (Set.ofPred p).Finite), n < hf.toFinset.card) → IsLeast {i | p i ∧ ∀ k < n, Nat.nth p k < i} (Nat.nth p n)

theorem Nat.isLeast_nth_of_lt_card E1 #

∀ {p : ℕ → Prop} {n : ℕ} (hf : (Set.ofPred p).Finite),
  n < hf.toFinset.card → IsLeast {i | p i ∧ ∀ k < n, Nat.nth p k < i} (Nat.nth p n)

theorem Nat.isLeast_nth_of_infinite E1 #

∀ {p : ℕ → Prop}, (Set.ofPred p).Infinite → ∀ (n : ℕ), IsLeast {i | p i ∧ ∀ k < n, Nat.nth p k < i} (Nat.nth p n)

theorem Nat.nth_eq_sInf E1 #

∀ (p : ℕ → Prop) (n : ℕ), Nat.nth p n = sInf {x | p x ∧ ∀ k < n, Nat.nth p k < x}

An alternative recursive definition of Nat.nth: Nat.nth s n is the infimum of x ∈ s such that Nat.nth s k < x for all k < n, if this set is nonempty. We do not assume that the set is nonempty because we use the same "garbage value" 0 both for sInf on ℕ and for Nat.nth s n for n ≥ #s.

theorem Nat.nth_zero E1 #

∀ {p : ℕ → Prop}, Nat.nth p 0 = sInf (Set.ofPred p)

theorem Nat.nth_zero_of_zero E0 #

∀ {p : ℕ → Prop}, p 0 → Nat.nth p 0 = 0

theorem Nat.nth_zero_of_exists E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p] (h : ∃ n, p n), Nat.nth p 0 = Nat.find h

theorem Nat.nth_eq_zero E1 #

∀ {p : ℕ → Prop} {n : ℕ}, Nat.nth p n = 0 ↔ p 0 ∧ n = 0 ∨ ∃ (hf : (Set.ofPred p).Finite), hf.toFinset.card ≤ n

theorem Nat.lt_card_toFinset_of_nth_ne_zero E1 #

∀ {p : ℕ → Prop} {n : ℕ}, Nat.nth p n ≠ 0 → ∀ (hf : (Set.ofPred p).Finite), n < hf.toFinset.card

theorem Nat.nth_mem_of_ne_zero E1 #

∀ {p : ℕ → Prop} {n : ℕ}, Nat.nth p n ≠ 0 → p (Nat.nth p n)

theorem Nat.nth_eq_zero_mono E1 #

∀ {p : ℕ → Prop}, ¬p 0 → ∀ {a b : ℕ}, a ≤ b → Nat.nth p a = 0 → Nat.nth p b = 0

theorem Nat.nth_ne_zero_anti E1 #

∀ {p : ℕ → Prop}, ¬p 0 → ∀ {a b : ℕ}, a ≤ b → Nat.nth p b ≠ 0 → Nat.nth p a ≠ 0

theorem Nat.le_nth_of_lt_nth_succ E1 #

∀ {p : ℕ → Prop} {k a : ℕ}, a < Nat.nth p (k + 1) → p a → a ≤ Nat.nth p k

theorem Nat.nth_mem_anti E1 #

∀ {p : ℕ → Prop} {a b : ℕ}, a ≤ b → p (Nat.nth p b) → p (Nat.nth p a)

theorem Nat.nth_le_of_strictMonoOn_of_mapsTo E1 #

∀ {p : ℕ → Prop} (f : ℕ → ℕ),
  Set.MapsTo f {n | ∀ (hf : (Set.ofPred p).Finite), n < hf.toFinset.card} (Set.ofPred p) →
    StrictMonoOn f {n | ∀ (hf : (Set.ofPred p).Finite), n < hf.toFinset.card} → ∀ {n : ℕ}, Nat.nth p n ≤ f n

Nat.nth p is the least strictly monotone function whose image is contained in Set.ofPred p

theorem Nat.le_nth_of_monotoneOn_of_surjOn E1 #

∀ {p : ℕ → Prop} (f : ℕ → ℕ),
  Set.SurjOn f {n | ∀ (hf : (Set.ofPred p).Finite), n < hf.toFinset.card} (Set.ofPred p) →
    MonotoneOn f {n | ∀ (hf : (Set.ofPred p).Finite), n < hf.toFinset.card} →
      ∀ {n : ℕ}, (∀ (hf : (Set.ofPred p).Finite), n < hf.toFinset.card) → f n ≤ Nat.nth p n

Nat.nth p is the greatest monotone function whose image contains Set.ofPred p.

theorem Nat.nth_comp_of_strictMono E1 #

∀ {p : ℕ → Prop} {n : ℕ} {f : ℕ → ℕ},
  StrictMono f →
    (∀ (k : ℕ), p k → k ∈ Set.range f) →
      (∀ (hfi : (Set.ofPred p).Finite), n < hfi.toFinset.card) → f (Nat.nth (fun i ↦ p (f i)) n) = Nat.nth p n

theorem Nat.nth_add E1 #

∀ {p : ℕ → Prop} {m n : ℕ}, (∀ k < m, ¬p k) → Nat.nth p n ≠ 0 → Nat.nth (fun i ↦ p (i + m)) n + m = Nat.nth p n

theorem Nat.nth_add_eq_sub E1 #

∀ {p : ℕ → Prop} {m n : ℕ}, (∀ k < m, ¬p k) → Nat.nth p n ≠ 0 → Nat.nth (fun i ↦ p (i + m)) n = Nat.nth p n - m

theorem Nat.nth_add_one E1 #

∀ {p : ℕ → Prop} {n : ℕ}, ¬p 0 → Nat.nth p n ≠ 0 → Nat.nth (fun i ↦ p (i + 1)) n + 1 = Nat.nth p n

theorem Nat.nth_add_one_eq_sub E1 #

∀ {p : ℕ → Prop} {n : ℕ}, ¬p 0 → Nat.nth p n ≠ 0 → Nat.nth (fun i ↦ p (i + 1)) n = Nat.nth p n - 1

theorem Nat.count_nth_zero E0 #

∀ (p : ℕ → Prop) [inst : DecidablePred p], Nat.count p (Nat.nth p 0) = 0

theorem Nat.filter_range_nth_subset_insert E1 #

∀ (p : ℕ → Prop) [inst : DecidablePred p] (k : ℕ),
  {n ∈ Finset.range (Nat.nth p (k + 1)) | p n} ⊆ insert (Nat.nth p k) ({n ∈ Finset.range (Nat.nth p k) | p n})

theorem Nat.filter_range_nth_eq_insert E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p] {k : ℕ},
  (∀ (hf : (Set.ofPred p).Finite), k + 1 < hf.toFinset.card) →
    {n ∈ Finset.range (Nat.nth p (k + 1)) | p n} = insert (Nat.nth p k) ({n ∈ Finset.range (Nat.nth p k) | p n})

theorem Nat.filter_range_nth_eq_insert_of_finite E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p] (hf : (Set.ofPred p).Finite) {k : ℕ},
  k + 1 < hf.toFinset.card →
    {n ∈ Finset.range (Nat.nth p (k + 1)) | p n} = insert (Nat.nth p k) ({n ∈ Finset.range (Nat.nth p k) | p n})

theorem Nat.filter_range_nth_eq_insert_of_infinite E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p],
  (Set.ofPred p).Infinite →
    ∀ (k : ℕ),
      {n ∈ Finset.range (Nat.nth p (k + 1)) | p n} = insert (Nat.nth p k) ({n ∈ Finset.range (Nat.nth p k) | p n})

theorem Nat.count_nth E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p] {n : ℕ},
  (∀ (hf : (Set.ofPred p).Finite), n < hf.toFinset.card) → Nat.count p (Nat.nth p n) = n

theorem Nat.count_nth_of_lt_card_finite E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p] {n : ℕ} (hp : (Set.ofPred p).Finite),
  n < hp.toFinset.card → Nat.count p (Nat.nth p n) = n

theorem Nat.count_nth_of_infinite E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p], (Set.ofPred p).Infinite → ∀ (n : ℕ), Nat.count p (Nat.nth p n) = n

theorem Nat.surjective_count_of_infinite_setOfPred E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p], {n | p n}.Infinite → Function.Surjective (Nat.count p)

theorem Nat.count_nth_succ E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p] {n : ℕ},
  (∀ (hf : (Set.ofPred p).Finite), n < hf.toFinset.card) → Nat.count p (Nat.nth p n + 1) = n + 1

theorem Nat.count_nth_succ_of_infinite E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p], (Set.ofPred p).Infinite → ∀ (n : ℕ), Nat.count p (Nat.nth p n + 1) = n + 1

theorem Nat.nth_count E0 #

∀ {p : ℕ → Prop} [inst : DecidablePred p] {n : ℕ}, p n → Nat.nth p (Nat.count p n) = n

theorem Nat.nth_lt_of_lt_count E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p] {n k : ℕ}, k < Nat.count p n → Nat.nth p k < n

theorem Nat.le_nth_of_count_le E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p] {n k : ℕ}, n ≤ Nat.nth p k → Nat.count p n ≤ k

theorem Nat.count_eq_zero E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p], (∃ n, p n) → ∀ {n : ℕ}, Nat.count p n = 0 ↔ n ≤ Nat.nth p 0

theorem Nat.nth_count_eq_sInf E1 #

∀ (p : ℕ → Prop) [inst : DecidablePred p] (n : ℕ), Nat.nth p (Nat.count p n) = sInf {i | p i ∧ n ≤ i}

theorem Nat.le_nth_count' E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p] {n : ℕ}, (∃ k, p k ∧ n ≤ k) → n ≤ Nat.nth p (Nat.count p n)

theorem Nat.le_nth_count E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p], (Set.ofPred p).Infinite → ∀ (n : ℕ), n ≤ Nat.nth p (Nat.count p n)

def Nat.giCountNth E1 #

{p : ℕ → Prop} → [inst : DecidablePred p] → (Set.ofPred p).Infinite → GaloisInsertion (Nat.count p) (Nat.nth p)

If a predicate p : ℕ → Prop is true for infinitely many numbers, then Nat.count p and Nat.nth p form a Galois insertion.

theorem Nat.gc_count_nth E3 #

∀ {p : ℕ → Prop} [inst : DecidablePred p], (Set.ofPred p).Infinite → GaloisConnection (Nat.count p) (Nat.nth p)

theorem Nat.count_le_iff_le_nth E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p], (Set.ofPred p).Infinite → ∀ {a b : ℕ}, Nat.count p a ≤ b ↔ a ≤ Nat.nth p b

theorem Nat.lt_nth_iff_count_lt E1 #

∀ {p : ℕ → Prop} [inst : DecidablePred p], (Set.ofPred p).Infinite → ∀ {a b : ℕ}, a < Nat.count p b ↔ Nat.nth p a < b

theorem Nat.nth_add_one_le_iff E0 #

∀ {p : ℕ → Prop}, (Set.ofPred p).Infinite → ∀ {n q : ℕ}, p q → (Nat.nth p (n + 1) ≤ q ↔ Nat.nth p n < q)

nth p (n + 1) is the least value of p exceeding nth p n: for any q satisfying p, it is ≤ q exactly when nth p n < q.

theorem Nat.nth_of_forall E1 #

∀ {p : ℕ → Prop} {n : ℕ}, (∀ n' ≤ n, p n') → Nat.nth p n = n

theorem Nat.nth_true E0 #

∀ (n : ℕ), Nat.nth (fun x ↦ True) n = n

theorem Nat.nth_of_forall_not E1 #

∀ {p : ℕ → Prop} {n : ℕ}, (∀ n' ≥ n, ¬p n') → Nat.nth p n = 0

theorem Nat.nth_false E0 #

∀ (n : ℕ), Nat.nth (fun x ↦ False) n = 0

Deprecated aliases

NameNew nameSince
Nat.surjective_count_of_infinite_setOfNat.surjective_count_of_infinite_setOfPred2026-07-09