Documentation

CslibFrontier.Foundations.Data.List.Equiv

Enumerating list occurrences #

List.Enumeration xs f identifies the positions of xs with an index type, with f giving the value at each index. Values need not be distinct: mapping a list retains every occurrence.

Main definitions #

Main results #

def List.productIndexEquiv {α : Type u_1} {β : Type u_2} (left : List α) (right : List β) :
Fin (left ×ˢ right).length ≃ Fin left.length × Fin right.length

The positions in a list product, with the right coordinate varying fastest.

def List.appendIndexEquiv {α : Type u_1} (left right : List α) :
Fin left.length ⊕ Fin right.length ≃ Fin (left ++ right).length

The positions of left ++ right: the positions of left, followed by those of right shifted by left.length.

theorem List.ofFn_appendIndexEquiv {α : Type u_1} {β : Type u_2} (left right : List α) (f : Fin (left ++ right).length → β) :
ofFn f = (ofFn fun (i : Fin left.length) => f ((left.appendIndexEquiv right) (Sum.inl i))) ++ ofFn fun (j : Fin right.length) => f ((left.appendIndexEquiv right) (Sum.inr j))

Enumerating an appended index set concatenates the enumerations of its two parts.

@[simp]
theorem List.val_appendIndexEquiv_inl {α : Type u_1} (left right : List α) (i : Fin left.length) :
↑((left.appendIndexEquiv right) (Sum.inl i)) = ↑i
@[simp]
theorem List.val_appendIndexEquiv_inr {α : Type u_1} (left right : List α) (j : Fin right.length) :
↑((left.appendIndexEquiv right) (Sum.inr j)) = left.length + ↑j
theorem List.getElem_appendIndexEquiv_inl {α : Type u_1} (left right : List α) (i : Fin left.length) :
(left ++ right)[(left.appendIndexEquiv right) (Sum.inl i)] = left[i]
theorem List.getElem_appendIndexEquiv_inr {α : Type u_1} (left right : List α) (j : Fin right.length) :
(left ++ right)[(left.appendIndexEquiv right) (Sum.inr j)] = right[j]
theorem List.appendIndexEquiv_lt_iff {α : Type u_1} (left right : List α) (x y : Fin left.length ⊕ Fin right.length) :
(left.appendIndexEquiv right) x < (left.appendIndexEquiv right) y ↔ Sum.Lex (fun (x1 x2 : Fin left.length) => x1 < x2) (fun (x1 x2 : Fin right.length) => x1 < x2) x y

The order of the positions of left ++ right is the lexicographic order of the sum: positions of left precede positions of right.

structure List.Enumeration {α : Type u_1} {ι : Type u_3} (xs : List α) (f : ι → α) :
Type u_3

An ordered enumeration of occurrences, allowing different indices to have the same value.

  • equiv : Fin xs.length ≃ ι

    The index assigned to each list position.

  • get_eq (i : Fin xs.length) : xs.get i = f (self.equiv i)

    The value at a position is given by its assigned index.

def List.Enumeration.ofNodup {α : Type u_1} [DecidableEq α] (xs : List α) (hxs : xs.Nodup) (hmem : ∀ (x : α), x ∈ xs) :

A complete duplicate-free list enumerates the type of its values.

The numerical range enumerates its bounded indices in order.

def List.Enumeration.singleton {α : Type u_1} (a : α) :
[a].Enumeration fun (x : Unit) => a

A singleton has one occurrence, indexed by Unit.

def List.Enumeration.reindex {α : Type u_1} {ι : Type u_3} {κ : Type u_4} {xs : List α} {f : ι → α} (e : xs.Enumeration f) (number : ι ≃ κ) :
xs.Enumeration (f ∘ ⇑number.symm)

Rename the occurrence indices without changing the list.

def List.Enumeration.map {α : Type u_1} {β : Type u_2} {ι : Type u_3} {xs : List α} {f : ι → α} (e : xs.Enumeration f) (g : α → β) :
(List.map g xs).Enumeration (g ∘ f)

Mapping changes values but retains each occurrence and its index.

def List.Enumeration.prod {α : Type u_1} {β : Type u_2} {ι : Type u_3} {κ : Type u_4} {xs : List α} {ys : List β} {f : ι → α} {g : κ → β} (left : xs.Enumeration f) (right : ys.Enumeration g) :
(xs ×ˢ ys).Enumeration fun (p : ι × κ) => (f p.1, g p.2)

Products enumerate the right list for each successive occurrence of the left list.

def List.Enumeration.append {α : Type u_1} {ι : Type u_3} {κ : Type u_4} {xs ys : List α} {f : ι → α} {g : κ → α} (left : xs.Enumeration f) (right : ys.Enumeration g) :
(xs ++ ys).Enumeration (Sum.elim f g)

Appending keeps every left occurrence before every right occurrence.