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.Fin.Basic

90 declarations and 1 deprecated aliases. Mathlib docs · Source

The finite type with n elements

Fin n is the type whose elements are natural numbers smaller than n. This file expands on the development in the core library.

Main definitions

  • finZeroElim : Elimination principle for the empty set Fin 0, generalizes Fin.elim0. Further definitions and eliminators can be found in Init.Data.Fin.Lemmas
  • Fin.equivSubtype : Equivalence between Fin n and { i // i < n }.

coercions and constructions

order

Coercions to ℤ and the fin_omega tactic.

addition, numerals, and coercion from Nat

recursion and induction principles

theorem Nat.forall_lt_iff_fin E1 #

∀ {n : ℕ} {p : (k : ℕ) → k < n → Prop}, (∀ (k : ℕ) (hk : k < n), p k hk) ↔ ∀ (k : Fin n), p ↑k ⋯

theorem Nat.exists_lt_iff_fin E1 #

∀ {n : ℕ} {p : (k : ℕ) → k < n → Prop}, (∃ k, ∃ (hk : k < n), p k hk) ↔ ∃ k, p ↑k ⋯

def finZeroElim E3 #

{α : Fin 0 → Sort u_1} → (x : Fin 0) → α x

Elimination principle for the empty set Fin 0, dependent version.

theorem Fin.mk_eq_one E0 #

∀ {n a : ℕ} {ha : a < n + 2}, ⟨a, ha⟩ = 1 ↔ a = 1

theorem Fin.one_eq_mk E0 #

∀ {n a : ℕ} {ha : a < n + 2}, 1 = ⟨a, ha⟩ ↔ a = 1

instance Fin.instCanLiftNatValLt E0 #

∀ {n : ℕ}, CanLift ℕ (Fin n) Fin.val fun x ↦ x < n

def Fin.rec0 E1 #

{α : Fin 0 → Sort u_1} → (i : Fin 0) → α i

A dependent variant of Fin.elim0.

theorem Fin.val_injective E0 #

∀ {n : ℕ}, Function.Injective Fin.val

theorem Fin.size_positive E1 #

∀ {n : ℕ} (a : Fin n), 0 < n

If you actually have an element of Fin n, then the n is always positive

theorem Fin.size_positive' E1 #

∀ {n : ℕ} [Nonempty (Fin n)], 0 < n

theorem Fin.prop E1 #

∀ {n : ℕ} (a : Fin n), ↑a < n

theorem Fin.lt_last_iff_ne_last E1 #

∀ {n : ℕ} {a : Fin (n + 1)}, a < Fin.last n ↔ a ≠ Fin.last n

theorem Fin.ne_zero_of_lt E2 #

∀ {n : ℕ} {a b : Fin (n + 1)}, a < b → b ≠ 0

theorem Fin.ne_last_of_lt E2 #

∀ {n : ℕ} {a b : Fin (n + 1)}, a < b → a ≠ Fin.last n

theorem Fin.ne_last_of_ne_last_of_le E1 #

∀ {n : ℕ} {a b : Fin (n + 1)}, b ≠ Fin.last n → a ≤ b → a ≠ Fin.last n

theorem Fin.val_sub_lt_of_lt_of_le E0 #

∀ {n m : ℕ} {a b : Fin n}, ↑a < m → b ≤ a → ↑(a - b) < m

theorem Fin.sub_ne_last_of_ne_last_of_le E1 #

∀ {n : ℕ} {a b : Fin (n + 1)}, a ≠ Fin.last n → b ≤ a → a - b ≠ Fin.last n

def Fin.equivSubtype E3 #

{n : ℕ} → Fin n ≃ { i // i < n }

Equivalence between Fin n and { i // i < n }.

theorem Fin.equivSubtype_symm_apply E0 #

∀ {n : ℕ} (a : { i // i < n }), Fin.equivSubtype.symm a = ⟨↑a, ⋯⟩

theorem Fin.equivSubtype_apply E0 #

∀ {n : ℕ} (a : Fin n), Fin.equivSubtype a = ⟨↑a, ⋯⟩

theorem Fin.neZero E2 #

∀ {n : ℕ} (i : Fin n), NeZero n

theorem Fin.val_eq_val E0 #

∀ {n : ℕ} (a b : Fin n), ↑a = ↑b ↔ a = b

theorem Fin.ne_iff_vne E1 #

∀ {n : ℕ} (a b : Fin n), a ≠ b ↔ ↑a ≠ ↑b

theorem Fin.mk_eq_mk E0 #

∀ {n a : ℕ} {h : a < n} {a' : ℕ} {h' : a' < n}, ⟨a, h⟩ = ⟨a', h'⟩ ↔ a = a'

theorem Fin.heq_fun_iff E0 #

∀ {α : Sort u_1} {k l : ℕ} (h : k = l) {f : Fin k → α} {g : Fin l → α}, f ≍ g ↔ ∀ (i : Fin k), f i = g ⟨↑i, ⋯⟩

Assume k = l. If two functions defined on Fin k and Fin l are equal on each element, then they coincide (in the heq sense).

theorem Fin.heq_fun₂_iff E0 #

∀ {α : Sort u_1} {k l k' l' : ℕ} (h : k = l) (h' : k' = l') {f : Fin k → Fin k' → α} {g : Fin l → Fin l' → α},
  f ≍ g ↔ ∀ (i : Fin k) (j : Fin k'), f i j = g ⟨↑i, ⋯⟩ ⟨↑j, ⋯⟩

Assume k = l and k' = l'. If two functions Fin k → Fin k' → α and Fin l → Fin l' → α are equal on each pair, then they coincide (in the heq sense).

theorem Fin.heq_ext_iff E0 #

∀ {k l : ℕ}, k = l → ∀ {i : Fin k} {j : Fin l}, i ≍ j ↔ ↑i = ↑j

Two elements of Fin k and Fin l are heq iff their values in ℕ coincide. This requires k = l. For the left implication without this assumption, see val_eq_val_of_heq.

theorem Fin.lt_or_ge E1 #

∀ {n : ℕ} (a b : Fin n), a < b ∨ b ≤ a

Fin.lt_or_ge is an alias of Fin.lt_or_le. It is preferred since it follows the mathlib naming convention.

theorem Fin.le_or_gt E1 #

∀ {n : ℕ} (a b : Fin n), a ≤ b ∨ b < a

Fin.le_or_gt is an alias of Fin.le_or_lt. It is preferred since it follows the mathlib naming convention.

theorem Fin.le_iff_val_le_val E0 #

∀ {n : ℕ} {a b : Fin n}, a ≤ b ↔ ↑a ≤ ↑b

theorem Fin.val_fin_lt E0 #

∀ {n : ℕ} {a b : Fin n}, ↑a < ↑b ↔ a < b

a < b as natural numbers if and only if a < b in Fin n.

theorem Fin.val_fin_le E0 #

∀ {n : ℕ} {a b : Fin n}, ↑a ≤ ↑b ↔ a ≤ b

a ≤ b as natural numbers if and only if a ≤ b in Fin n.

theorem Fin.min_val E0 #

∀ {n : ℕ} {a : Fin n}, min (↑a) n = ↑a

theorem Fin.max_val E0 #

∀ {n : ℕ} {a : Fin n}, max (↑a) n = n

instance Fin.instWellFoundedRelation_mathlib E0 #

{n : ℕ} → WellFoundedRelation (Fin n)

Use the ordering on Fin n for checking recursive definitions.

For example, the following definition is not accepted by the termination checker, unless we declare the WellFoundedRelation instance:

def factorial {n : ℕ} : Fin n → ℕ
  | ⟨0, _⟩ := 1
  | ⟨i + 1, hi⟩ := (i + 1) * factorial ⟨i, i.lt_succ_self.trans hi⟩

theorem Fin.mk_zero' E0 #

∀ (n : ℕ) [inst : NeZero n], ⟨0, ⋯⟩ = 0

Fin.mk_zero in Lean only applies in Fin (n + 1). This one instead uses a NeZero n typeclass hypothesis.

theorem Fin.val_pos_iff E0 #

∀ {n : ℕ} [inst : NeZero n] {a : Fin n}, 0 < ↑a ↔ 0 < a

theorem Fin.cast_eq_self E0 #

∀ {n : ℕ} (a : Fin n), Fin.cast ⋯ a = a

theorem Fin.cast_eq_zero E0 #

∀ {k l : ℕ} [inst : NeZero k] [inst_1 : NeZero l] (h : k = l) (x : Fin k), Fin.cast h x = 0 ↔ x = 0

theorem Fin.cast_injective E1 #

∀ {k l : ℕ} (h : k = l), Function.Injective (Fin.cast h)

theorem Fin.last_pos' E1 #

∀ {n : ℕ} [NeZero n], 0 < Fin.last n

theorem Fin.one_lt_last E1 #

∀ {n : ℕ} [NeZero n], 1 < Fin.last (n + 1)

theorem Fin.coe_int_sub_eq_ite E0 #

∀ {n : ℕ} (u v : Fin n), ↑↑(u - v) = if v ≤ u then ↑↑u - ↑↑v else ↑↑u - ↑↑v + ↑n

theorem Fin.coe_int_sub_eq_mod E0 #

∀ {n : ℕ} (u v : Fin n), ↑↑(u - v) = (↑↑u - ↑↑v) % ↑n

theorem Fin.coe_int_add_eq_ite E0 #

∀ {n : ℕ} (u v : Fin n), ↑↑(u + v) = if ↑u + ↑v < n then ↑↑u + ↑↑v else ↑↑u + ↑↑v - ↑n

theorem Fin.coe_int_add_eq_mod E0 #

∀ {n : ℕ} (u v : Fin n), ↑↑(u + v) = (↑↑u + ↑↑v) % ↑n

def Fin.tacticFin_omega E1 #

Lean.ParserDescr

fin_omega is a preprocessor for omega to handle inequalities in Fin. It rewrites all hypotheses and the goal, turning statements about addition, subtraction and inequalities in Fin n into statements that omega can use/solve. Note that this involves a lot of case splitting, so may be slow.

theorem Fin.val_one' E0 #

∀ (n : ℕ) [inst : NeZero n], ↑1 = 1 % n

theorem Fin.nontrivial_iff_two_le E1 #

∀ {n : ℕ}, Nontrivial (Fin n) ↔ 2 ≤ n

instance Fin.instNontrivial E0 #

∀ {n : ℕ} [n.AtLeastTwo], Nontrivial (Fin n)

theorem Fin.exists_ne_and_ne_of_two_lt E1 #

∀ {n : ℕ} (i j : Fin n), 2 < n → ∃ k, k ≠ i ∧ k ≠ j

If working with more than two elements, we can always pick a third distinct from two existing elements.

instance Fin.inhabitedFinOneAdd E0 #

(n : ℕ) → Inhabited (Fin (1 + n))

theorem Fin.default_eq_zero E0 #

∀ (n : ℕ) [inst : NeZero n], default = 0

theorem Fin.val_add_eq_ite E0 #

∀ {n : ℕ} (a b : Fin n), ↑(a + b) = if n ≤ ↑a + ↑b then ↑a + ↑b - n else ↑a + ↑b

theorem Fin.val_add_eq_of_add_lt E0 #

∀ {n : ℕ} {a b : Fin n}, ↑a + ↑b < n → ↑(a + b) = ↑a + ↑b

theorem Fin.intCast_val_sub_eq_sub_add_ite E1 #

∀ {n : ℕ} (a b : Fin n), ↑↑(a - b) = ↑↑a - ↑↑b + ↑(if b ≤ a then 0 else n)

theorem Fin.sub_val_lt_sub E1 #

∀ {n : ℕ} {i j : Fin n}, i ≤ j → ↑(j - i) < n - ↑i

theorem Fin.castLT_sub_nezero E1 #

∀ {n : ℕ} {i j : Fin n} (hij : i < j), (j - i).castLT ⋯ ≠ 0

theorem Fin.one_le_of_ne_zero E1 #

∀ {n : ℕ} {k : Fin n}, k ≠ 0 → 1 ≤ k

theorem Fin.val_sub_one_of_ne_zero E0 #

∀ {n : ℕ} {i : Fin n}, i ≠ 0 → ↑(i - 1) = ↑i - 1

theorem Fin.ofNat_eq_cast E0 #

∀ (n : ℕ) [inst : NeZero n] (a : ℕ), Fin.ofNat n a = ↑a

theorem Fin.val_natCast E0 #

∀ (a n : ℕ) [inst : NeZero n], ↑↑a = a % n

theorem Fin.val_cast_of_lt E0 #

∀ {n : ℕ} [inst : NeZero n] {a : ℕ}, a < n → ↑↑a = a

Converting an in-range number to Fin (n + 1) produces a result whose value is the original number.

theorem Fin.cast_val_eq_self E0 #

∀ {n : ℕ} (a : Fin n), ↑↑a = a

Converting the value of a Fin n to Fin n results in the same value.

theorem Fin.natCast_self E0 #

∀ (n : ℕ) [inst : NeZero n], ↑n = 0

theorem Fin.natCast_eq_zero E0 #

∀ {a n : ℕ} [inst : NeZero n], ↑a = 0 ↔ n ∣ a

theorem Fin.natCast_zero E0 #

∀ {n : ℕ} [inst : NeZero n], ↑0 = 0

theorem Fin.natCast_eq_last E0 #

∀ (n : ℕ), ↑n = Fin.last n

theorem Fin.natCast_eq_mk E0 #

∀ {m n : ℕ} (h : m < n),
  have this := ⋯;
  ↑m = ⟨m, h⟩

theorem Fin.one_eq_mk_of_lt E1 #

∀ {n : ℕ} (h : 1 < n),
  have this := ⋯;
  1 = ⟨1, h⟩

theorem Fin.le_val_last E1 #

∀ {n : ℕ} (i : Fin (n + 1)), i ≤ ↑n

theorem Fin.natCast_le_natCast E1 #

∀ {n a b : ℕ}, a ≤ n → b ≤ n → (↑a ≤ ↑b ↔ a ≤ b)

theorem Fin.natCast_lt_natCast E1 #

∀ {n a b : ℕ}, a ≤ n → b ≤ n → (↑a < ↑b ↔ a < b)

theorem Fin.natCast_mono E1 #

∀ {n a b : ℕ}, b ≤ n → a ≤ b → ↑a ≤ ↑b

theorem Fin.natCast_strictMono E1 #

∀ {n a b : ℕ}, b ≤ n → a < b → ↑a < ↑b

theorem Fin.castLE_natCast E0 #

∀ {m n : ℕ} [inst : NeZero m] (h : m ≤ n) (a : ℕ), Fin.castLE h ↑a = ↑(a % m)

theorem Fin.modNat_rev E1 #

∀ {n m : ℕ} (i : Fin (m * n)), i.rev.modNat = i.modNat.rev

theorem Fin.strong_induction_on E1 #

∀ {n : ℕ} {motive : Fin n → Prop}, (∀ (j : Fin n), (∀ k < j, motive k) → motive j) → ∀ (i : Fin n), motive i

theorem Fin.liftFun_iff_succ E1 #

∀ {n : ℕ} {α : Type u_1} (r : α → α → Prop) [IsTrans α r] {f : Fin (n + 1) → α},
  Relator.LiftFun (fun x1 x2 ↦ x1 < x2) r f f ↔ ∀ (i : Fin n), r (f i.castSucc) (f i.succ)

theorem Fin.coe_neg_one E0 #

∀ {n : ℕ}, ↑(-1) = n

theorem Fin.last_sub E1 #

∀ {n : ℕ} (i : Fin (n + 1)), Fin.last n - i = i.rev

theorem Fin.add_one_le_of_lt E1 #

∀ {n : ℕ} {a b : Fin (n + 1)}, a < b → a + 1 ≤ b

theorem Fin.exists_eq_add_of_le E1 #

∀ {n : ℕ} {a b : Fin n}, a ≤ b → ∃ k ≤ b, b = a + k

theorem Fin.exists_eq_add_of_lt E1 #

∀ {n : ℕ} {a b : Fin (n + 1)}, a < b → ∃ k < b, k + 1 ≤ b ∧ b = a + k + 1

theorem Fin.pos_of_ne_zero E1 #

∀ {n : ℕ} {a : Fin (n + 1)}, a ≠ 0 → 0 < a

theorem Fin.sub_succ_le_sub_of_le E1 #

∀ {n : ℕ} {u v : Fin (n + 2)}, u < v → v - (u + 1) < v - u

theorem Fin.coe_natCast_eq_mod E0 #

∀ (m n : ℕ) [inst : NeZero m], ↑↑n = n % m

theorem Fin.coe_ofNat_eq_mod E0 #

∀ (m n : ℕ) [inst : NeZero m], ↑(OfNat.ofNat n) = OfNat.ofNat n % m

theorem Fin.val_add_one_of_lt' E0 #

∀ {n : ℕ} {i : Fin n}, ↑i + 1 < n → ↑(i + 1) = ↑i + 1

instance Fin.instNeZeroHAddNatOfNat_mathlib E0 #

∀ {n m : ℕ} [NeZero n] [inst : NeZero (OfNat.ofNat m)], NeZero (OfNat.ofNat m)

Deprecated aliases

NameNew nameSince
Fin.pos_iff_ne_zero'Fin.pos_iff_ne_zero2026-11-16