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
Order embeddings and an order isomorphism #
Fin.orderIsoSubtype
: coercion to{ i // i < n }
as anOrderIso
;Fin.valEmbedding
: coercion to natural numbers as anEmbedding
;Fin.valOrderEmbedding
: coercion to natural numbers as anOrderEmbedding
;Fin.succEmbedding
:Fin.succ
as anOrderEmbedding
;Fin.castLEEmb h
:Fin.castLE
as anOrderEmbedding
, embedFin n
intoFin m
,h : n ≤ m
;Fin.castIso
:Fin.cast
as anOrderIso
, order isomorphism betweenFin n
andFin m
provided thatn = m
, see alsoEquiv.finCongr
;Fin.castAddEmb m
:Fin.castAdd
as anOrderEmbedding
, embedFin n
intoFin (n+m)
;Fin.castSuccEmb
:Fin.castSucc
as anOrderEmbedding
, embedFin n
intoFin (n+1)
;Fin.succAboveEmb p
:Fin.auccAbove
as anOrderEmbedding
, embedFin n
intoFin (n + 1)
with a hole aroundp
;Fin.addNatEmb m i
:Fin.addNat
as anOrderEmbedding
, addm
oni
on the right, generalizesFin.succ
;Fin.natAddEmb n i
:Fin.natAdd
as anOrderEmbedding
, 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 = Fin.elim0 x
Instances For
coercions and constructions #
order #
Equations
- One or more equations did not get rendered due to their size.
Equations
- Fin.instPartialOrderFin = inferInstance
The inclusion map Fin n → ℕ
is an embedding.
Equations
- Fin.valEmbedding = { toFun := Fin.val, inj' := (_ : Function.Injective Fin.val) }
Instances For
The ordering on Fin n
is a well 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 = { val := i % n, isLt := (_ : i % n < n) }
Instances For
Equations
- Fin.instZeroFin = { zero := Fin.ofNat'' 0 }
Equations
- Fin.instOneFin = { one := Fin.ofNat'' 1 }
The Fin.val_zero
in Std
only applies in Fin (n+1)
.
This one instead uses a NeZero n
typeclass hypothesis.
The Fin.zero_le
in Std
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 (_ : Function.Involutive Fin.rev)
Instances For
Equations
- Fin.instBoundedOrderFinHAddNatInstHAddInstAddNatOfNatInstLEFin = BoundedOrder.mk
Two strictly monotone functions from Fin n
are equal provided that their ranges
are equal.
addition, numerals, and coercion from Nat #
Equations
Equations
- One or more equations did not get rendered due to their size.
Equations
- Fin.instOfNatFin = { ofNat := Fin.ofNat' a (_ : 0 < n) }
Equations
- Fin.addCommMonoid n = let src := Fin.addCommSemigroup n; AddCommMonoid.mk (_ : ∀ (a b : Fin n), a + b = b + a)
Equations
- Fin.instAddMonoidWithOne n = let src := inferInstanceAs (AddCommMonoid (Fin n)); AddMonoidWithOne.mk
succ and casts into larger Fin types #
Fin.succ
as an OrderEmbedding
Equations
- Fin.succEmbedding n = OrderEmbedding.ofStrictMono Fin.succ (_ : ∀ (x x_1 : Fin n), x < x_1 → Nat.succ ↑x < Nat.succ ↑x_1)
Instances For
The Fin.succ_one_eq_two
in Std
only applies in Fin (n+2)
.
This one instead uses a NeZero n
typeclass hypothesis.
Fin.castLE
as an OrderEmbedding
, castLEEmb h i
embeds i
into a larger Fin
type.
Equations
- Fin.castLEEmb h = OrderEmbedding.ofStrictMono (Fin.castLE h) (_ : StrictMono (Fin.castLE h))
Instances For
While in many cases Fin.castIso
is better than Equiv.cast
/cast
, sometimes we want to apply
a generic theorem about cast
.
Fin.castAdd
as an OrderEmbedding
, castAddEmb m i
embeds i : Fin n
in Fin (n+m)
.
See also Fin.natAddEmb
and Fin.addNatEmb
.
Equations
- Fin.castAddEmb m = OrderEmbedding.ofStrictMono (Fin.castAdd m) (_ : StrictMono (Fin.castAdd m))
Instances For
Fin.castSucc
as an OrderEmbedding
, castSuccEmb i
embeds i : Fin n
in Fin (n+1)
.
Equations
- Fin.castSuccEmb = OrderEmbedding.ofStrictMono Fin.castSucc (_ : StrictMono Fin.castSucc)
Instances For
The Fin.castSucc_zero
in Std
only applies in Fin (n+1)
.
This one instead uses a NeZero n
typeclass hypothesis.
castSucc i
is positive when i
is positive.
The Fin.castSucc_pos
in Std
only applies in Fin (n+1)
.
This one instead uses a NeZero n
typeclass hypothesis.
The Fin.castSucc_eq_zero_iff
in Std
only applies in Fin (n+1)
.
This one instead uses a NeZero n
typeclass hypothesis.
The Fin.castSucc_ne_zero_iff
in Std
only applies in Fin (n+1)
.
This one instead uses a NeZero n
typeclass hypothesis.
Fin.addNat
as an OrderEmbedding
, addNatEmb m i
adds m
to i
, generalizes
Fin.succ
.
Equations
- Fin.addNatEmb m = OrderEmbedding.ofStrictMono (fun x => Fin.addNat x m) (_ : StrictMono fun x => Fin.addNat x m)
Instances For
Fin.natAdd
as an OrderEmbedding
, natAddEmb n i
adds n
to i
"on the left".
Equations
- Fin.natAddEmb n = OrderEmbedding.ofStrictMono (Fin.natAdd n) (_ : StrictMono (Fin.natAdd n))
Instances For
pred #
recursion and induction principles #
A function f
on Fin (n + 1)
is strictly monotone if and only if f i < f (i + 1)
for all i
.
A function f
on Fin (n + 1)
is strictly antitone if and only if f (i + 1) < f i
for all i
.
Abelian group structure on Fin n
.
Equations
- Fin.addCommGroup n = let src := Fin.addCommMonoid n; let src_1 := Fin.neg n; AddCommGroup.mk (_ : ∀ (a b : Fin n), a + b = b + a)
Note this is more general than Fin.addCommGroup
as it applies (vacuously) to Fin 0
too.
Equations
- Fin.instInvolutiveNeg n = InvolutiveNeg.mk (_ : ∀ (x : Fin n), - -x = x)
Note this is more general than Fin.addCommGroup
as it applies (vacuously) to Fin 0
too.
Note this is more general than Fin.addCommGroup
as it applies (vacuously) to Fin 0
too.
Equations
- Fin.instAddLeftCancelSemigroup n = let src := Fin.addCommSemigroup n; let src_1 := (_ : IsCancelAdd (Fin n)); AddLeftCancelSemigroup.mk (_ : ∀ (a b c : Fin n), a + b = a + c → b = c)
Note this is more general than Fin.addCommGroup
as it applies (vacuously) to Fin 0
too.
Equations
- Fin.instAddRightCancelSemigroup n = let src := Fin.addCommSemigroup n; let src_1 := (_ : IsCancelAdd (Fin n)); AddRightCancelSemigroup.mk (_ : ∀ (a b c : Fin n), a + b = c + b → a = c)
Fin.auccAbove
as an OrderEmbedding
, succAboveEmb p i
embeds Fin n
into Fin (n + 1)
with a hole around p
.
Equations
- Fin.succAboveEmb p = OrderEmbedding.ofStrictMono (Fin.succAbove p) (_ : StrictMono (Fin.succAbove p))
Instances For
Embedding i : Fin n
into Fin (n + 1)
with a hole around p : Fin (n + 1)
embeds i
by castSucc
when the resulting i.castSucc < p
.
Embedding i : Fin n
into Fin (n + 1)
with a hole around p : Fin (n + 1)
embeds i
by succ
when the resulting p < i.succ
.
Embedding i : Fin n
into Fin (n + 1)
is always about some hole p
.
Embedding i : Fin n
into Fin (n + 1)
using a pivot p
that is greater
results in a value that is less than p
.
Embedding i : Fin n
into Fin (n + 1)
using a pivot p
that is lesser
results in a value that is greater than p
.
The range of p.succAbove
is everything except p
.
Given a fixed pivot x : Fin (n + 1)
, x.succAbove
is injective
succAbove
is injective at the pivot
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
.
predAbove p i
embeds i : Fin (n+1)
into Fin n
by subtracting one if p < i
.
Equations
- Fin.predAbove p i = if h : Fin.castSucc p < i then Fin.pred i (_ : i ≠ 0) else Fin.castLT i (_ : ↑i < n)
Instances For
castPred
embeds i : Fin (n + 2)
into Fin (n + 1)
by lowering just last (n + 1)
to last n
.
Equations
- Fin.castPred i = Fin.predAbove (Fin.last n) i
Instances For
Sending Fin (n+1)
to Fin n
by subtracting one from anything above p
then back to Fin (n+1)
with a gap around p
is the identity away from p
.
Sending Fin n
into Fin (n + 1)
with a gap at p
then back to Fin n
by subtracting one from anything above p
is the identity.
pred
commutes with succAbove
.
succ
commutes with predAbove
.
mul #
Equations
- One or more equations did not get rendered due to their size.