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 #
Induction principles #
finZeroElim
: Elimination principle for the empty setFin 0
, generalizesFin.elim0
.Fin.succRec
: DefineC n i
by induction oni : Fin n
interpreted as(0 : Fin (n - i)).succ.succ…
. This function has two arguments:H0 n
defines0
-th elementC (n+1) 0
of an(n+1)
-tuple, andHs n i
defines(i+1)
-st element of(n+1)
-tuple based onn
,i
, andi
-th element ofn
-tuple.Fin.succRecOn
: same asFin.succRec
buti : Fin n
is the first argument;Fin.induction
: DefineC i
by induction oni : Fin (n + 1)
, separating into theNat
-like base cases ofC 0
andC (i.succ)
.Fin.inductionOn
: same asFin.induction
but withi : Fin (n + 1)
as the first argument.Fin.cases
: definef : Π i : Fin n.succ, C i
by separately handling the casesi = 0
andi = Fin.succ j
,j : Fin n
, defined usingFin.induction
.Fin.reverseInduction
: reverse induction oni : Fin (n + 1)
; givenC (Fin.last n)
and∀ i : Fin n, C (Fin.succ i) → C (Fin.castSucc i)
, constructs all valuesC i
by going down;Fin.lastCases
: definef : Π i, Fin (n + 1), C i
by separately handling the casesi = Fin.last n
andi = Fin.castSucc j
, a special case ofFin.reverseInduction
;Fin.addCases
: define a function onFin (m + n)
by separately handling the casesFin.castAdd n i
andFin.natAdd m i
;Fin.succAboveCases
: giveni : Fin (n + 1)
, define a function onFin (n + 1)
by separately handling the casesj = i
andj = Fin.succAbove i k
, same asFin.insertNth
but marked as eliminator and works forSort*
. -- Porting note: this is in another file
Embeddings and isomorphisms #
Fin.valEmbedding
: coercion to natural numbers as anEmbedding
;Fin.succEmb
:Fin.succ
as anEmbedding
;Fin.castLEEmb h
:Fin.castLE
as anEmbedding
, embedFin n
intoFin m
,h : n ≤ m
;finCongr
:Fin.cast
as anEquiv
, equivalence betweenFin n
andFin m
whenn = m
;Fin.castAddEmb m
:Fin.castAdd
as anEmbedding
, embedFin n
intoFin (n+m)
;Fin.castSuccEmb
:Fin.castSucc
as anEmbedding
, embedFin n
intoFin (n+1)
;Fin.addNatEmb m i
:Fin.addNat
as anEmbedding
, addm
oni
on the right, generalizesFin.succ
;Fin.natAddEmb n i
:Fin.natAdd
as anEmbedding
, addsn
oni
on the left;
Other casts #
Fin.ofNat'
: given a positive numbern
(deduced from[NeZero n]
),Fin.ofNat' i
isi % n
interpreted as an element ofFin n
;Fin.divNat i
: dividesi : Fin (m * n)
byn
;Fin.modNat i
: takes the mod ofi : Fin (m * n)
byn
;
Misc definitions #
Fin.revPerm : Equiv.Perm (Fin n)
:Fin.rev
as anEquiv.Perm
, the antitone involution given byi ↦ n-(i+1)
Elimination principle for the empty set Fin 0
, dependent version.
Equations
- finZeroElim x = x.elim0
Instances For
Alias of Fin.eq_of_val_eq
.
Alias of Fin.val_eq_of_eq
.
Alias of Fin.ne_of_val_ne
.
Alias of Fin.val_ne_of_ne
.
coercions and constructions #
order #
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⟩
Given a positive n
, Fin.ofNat' i
is i % n
as an element of Fin n
.
Equations
- Fin.ofNat'' i = ⟨i % n, ⋯⟩
Instances For
Equations
- Fin.instZeroOfNeZeroNat_mathlib = { zero := Fin.ofNat'' 0 }
Equations
- Fin.instOneOfNeZeroNat = { one := Fin.ofNat'' 1 }
The Fin.val_zero
in Lean
only applies in Fin (n+1)
.
This one instead uses a NeZero n
typeclass hypothesis.
The Fin.zero_le
in Lean
only applies in Fin (n+1)
.
This one instead uses a NeZero n
typeclass hypothesis.
Fin.rev
as an Equiv.Perm
, the antitone involution Fin n → Fin n
given by
i ↦ n-(i+1)
.
Equations
- Fin.revPerm = Function.Involutive.toPerm Fin.rev ⋯
Instances For
Coercions to ℤ
and the fin_omega
tactic. #
Preprocessor for omega
to handle inequalities in Fin
.
Note that this involves a lot of case splitting, so may be slow.
Equations
- Fin.tacticFin_omega = Lean.ParserDescr.node `Fin.tacticFin_omega 1024 (Lean.ParserDescr.nonReservedSymbol "fin_omega" false)
Instances For
addition, numerals, and coercion from Nat #
Equations
- Fin.inhabitedFinOneAdd n = inferInstance
Equations
- Fin.instNatCast = { natCast := fun (n_1 : ℕ) => Fin.ofNat'' n_1 }
Alias of Fin.val_natCast
.
Alias of Fin.natCast_self
.
Alias of Fin.natCast_eq_zero
.
Alias of Fin.natCast_eq_last
.
succ and casts into larger Fin types #
The Fin.succ_one_eq_two
in Lean
only applies in Fin (n+2)
.
This one instead uses a NeZero n
typeclass hypothesis.
Fin.castLE
as an Embedding
, castLEEmb h i
embeds i
into a larger Fin
type.
Equations
- Fin.castLEEmb h = { toFun := Fin.castLE h, inj' := ⋯ }
Instances For
While in many cases finCongr
is better than Equiv.cast
/cast
, sometimes we want to apply
a generic theorem about cast
.
Fin.castAdd
as an Embedding
, castAddEmb m i
embeds i : Fin n
in Fin (n+m)
.
See also Fin.natAddEmb
and Fin.addNatEmb
.
Equations
Instances For
Fin.castSucc
as an Embedding
, castSuccEmb i
embeds i : Fin n
in Fin (n+1)
.
Equations
- Fin.castSuccEmb = Fin.castAddEmb 1
Instances For
Alias of Fin.castSucc_lt_or_lt_succ
.
The Fin.castSucc_zero
in Lean
only applies in Fin (n+1)
.
This one instead uses a NeZero n
typeclass hypothesis.
Fin.addNat
as an Embedding
, addNatEmb m i
adds m
to i
, generalizes Fin.succ
.
Equations
- Fin.addNatEmb m = { toFun := fun (x : Fin n) => x.addNat m, inj' := ⋯ }
Instances For
Fin.natAdd
as an Embedding
, natAddEmb n i
adds n
to i
"on the left".
Equations
- Fin.natAddEmb n = { toFun := Fin.natAdd n, inj' := ⋯ }
Instances For
pred #
succAbove
is injective at the pivot
By moving succ
to the outside of this expression, we create opportunities for further
simplification using succAbove_zero
or succ_succAbove_zero
.
recursion and induction principles #
Equations
- Fin.uniqueFinOne = { toInhabited := Fin.instInhabited, uniq := Fin.uniqueFinOne.proof_2 }