Documentation

CslibFrontier.Foundations.Data.List.FoldlIdx

Indexed left folds #

l.foldlIdx f init start folds f over the list l from the left, passing the index of each element plus start. Read as an automaton, it is the run of the time-dependent transition function f : ℕ → S → X → S on the letters of l, started at time start in the state init. This file adds the lemmas for splitting such runs to the basic List.foldlIdx API of Batteries.

Main results #

theorem List.foldlIdx_append {α : Type u_1} {β : Type u_2} (f : ℕ → α → β → α) (init : α) (l₁ l₂ : List β) (start : ℕ) :
foldlIdx f init (l₁ ++ l₂) start = foldlIdx f (foldlIdx f init l₁ start) l₂ (start + l₁.length)

Splitting an indexed fold: the fold over l₁ ++ l₂ is the fold over l₂, started at the index and value where the fold over l₁ ends.

theorem List.foldlIdx_map {α : Type u_1} {β : Type u_2} {γ : Type u_3} (f : ℕ → α → β → α) (g : γ → β) (init : α) (l : List γ) (start : ℕ) :
foldlIdx f init (map g l) start = foldlIdx (fun (i : ℕ) (a : α) (c : γ) => f i a (g c)) init l start

An indexed fold over a mapped list is the fold of the function precomposed with the map.

theorem List.foldlIdx_flatten {α : Type u_1} {β : Type u_2} (f : ℕ → α → β → α) {n : ℕ} (L : List (List β)) (hL : ∀ l ∈ L, l.length = n) (init : α) (start : ℕ) :
foldlIdx f init L.flatten (n * start) = foldlIdx (fun (i : ℕ) (a : α) (l : List β) => foldlIdx f a l (n * i)) init L start

Indexed folds over blocks: if every block of L has length n, the fold over the concatenated blocks is the fold over the list of blocks that folds a whole block at each index, the i-th block from index n * i on.

theorem List.foldlIdx_ofFn {α : Type u_1} {β : Type u_2} (f : ℕ → α → β → α) (init : α) {m : ℕ} (x : Fin m → β) (start : ℕ) :
foldlIdx f init (ofFn x) start = Fin.foldl m (fun (a : α) (i : Fin m) => f (start + ↑i) a (x i)) init

An indexed fold over the list of a tuple is a fold over its positions, with the indices shifted by start.

theorem List.foldl_ofFn {α : Type u_1} {β : Type u_2} (f : α → β → α) (init : α) {m : ℕ} (x : Fin m → β) :
foldl f init (ofFn x) = Fin.foldl m (fun (a : α) (i : Fin m) => f a (x i)) init

Folding over the list of a tuple is the same ordinary fold over its finite positions.