Operation on tuples #
We interpret maps ∀ i : Fin n, α i
as n
-tuples of elements of possibly varying type α i
,
(α 0, …, α (n-1))
. A particular case is Fin n → α
of elements with all the same type.
In this case when α i
is a constant map, then tuples are isomorphic (but not definitionally equal)
to Vector
s.
We define the following operations:
Fin.tail
: the tail of ann+1
tuple, i.e., its lastn
entries;Fin.cons
: adding an element at the beginning of ann
-tuple, to get ann+1
-tuple;Fin.init
: the beginning of ann+1
tuple, i.e., its firstn
entries;Fin.snoc
: adding an element at the end of ann
-tuple, to get ann+1
-tuple. The namesnoc
comes fromcons
(i.e., adding an element to the left of a tuple) read in reverse order.Fin.insertNth
: insert an element to a tuple at a given position.Fin.find p
: returns the first indexn
wherep n
is satisfied, andnone
if it is never satisfied.Fin.append a b
: append two tuples.Fin.repeat n a
: repeat a tuplen
times.
As a binary function, Fin.cons
is injective.
Recurse on an n+1
-tuple by splitting it into a single element and an n
-tuple.
Equations
Instances For
Recurse on a tuple by splitting into Fin.elim0
and Fin.cons
.
Equations
- Fin.consInduction h0 h x_2 = Eq.mpr (_ : P 0 x_2 = P 0 Fin.elim0) (id h0)
- Fin.consInduction h0 h x_2 = Fin.consCases (fun x₀ x => h n x₀ x (Fin.consInduction h0 (fun {n} => h n) x)) x_2
Instances For
Append a tuple of length m
to a tuple of length n
to get a tuple of length m + n
.
This is a non-dependent version of Fin.add_cases
.
Equations
- Fin.append a b = Fin.addCases a b
Instances For
Repeat a
m
times. For example Fin.repeat 2 ![0, 3, 7] = ![0, 3, 7, 0, 3, 7]
.
Equations
- Fin.repeat m a x = let i := x; a (Fin.modNat i)
Instances For
In the previous section, we have discussed inserting or removing elements on the left of a
tuple. In this section, we do the same on the right. A difference is that Fin (n+1)
is constructed
inductively from Fin n
starting from the left, not from the right. This implies that Lean needs
more help to realize that elements belong to the right types, i.e., we need to insert casts at
several places.
Adding an element at the end of an n
-tuple, to get an n+1
-tuple. The name snoc
comes from
cons
(i.e., adding an element to the left of a tuple) read in reverse order.
Equations
- Fin.snoc p x i = if h : ↑i < n then cast (_ : α (Fin.castSucc (Fin.castLT i h)) = α i) (p (Fin.castLT i h)) else cast (_ : α (Fin.last n) = α i) x
Instances For
Updating a tuple and adding an element at the end commute.
Adding an element at the beginning of a tuple and then updating it amounts to adding it directly.
Updating an element and taking the beginning commute.
Appending a one-tuple to the right is the same as Fin.snoc
.
Define a function on Fin (n + 1)
from a value on i : Fin (n + 1)
and values on each
Fin.succAbove i j
, j : Fin n
. This version is elaborated as eliminator and works for
propositions, see also Fin.insertNth
for a version without an @[elab_as_elim]
attribute.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Insert an element into a tuple at a given position. For i = 0
see Fin.cons
,
for i = Fin.last n
see Fin.snoc
. See also Fin.succAboveCases
for a version elaborated
as an eliminator.
Equations
- Fin.insertNth i x p j = Fin.succAboveCases i x p j
Instances For
If find p = some i
, then p i
holds
find p
does not return none
if and only if p i
holds at some index i
.
Sends (g₀, ..., gₙ)
to (g₀, ..., op gⱼ gⱼ₊₁, ..., gₙ)
.
Equations
- Fin.contractNth j op g k = if ↑k < ↑j then g (Fin.castSucc k) else if ↑k = ↑j then op (g (Fin.castSucc k)) (g (Fin.succ k)) else g (Fin.succ k)