Documentation

CslibFrontier.Foundations.Data.Fin.Fold

Resuming a monadic fold over finite indices #

Fin.foldlMFrom runs the suffix of a finite-index fold from a natural-number cursor. Its equations specify initialization, one live step, and completion.

def Fin.foldlMFrom {m : Type → Type} [Monad m] {α : Type} {n : ℕ} (f : α → Fin n → m α) (a : α) (i : ℕ) :
m α

Run the suffix of a monadic fold over the indices from i to n - 1.

@[simp]
theorem Fin.foldlMFrom_zero {m : Type → Type} [Monad m] {α : Type} {n : ℕ} (f : α → Fin n → m α) (a : α) :
foldlMFrom f a 0 = foldlM n f a

Running from zero performs the original fold.

theorem Fin.foldlMFrom_of_le {m : Type → Type} [Monad m] {α : Type} {n : ℕ} (f : α → Fin n → m α) (a : α) {i : ℕ} (hi : n ≤ i) :
foldlMFrom f a i = pure a

Once the cursor reaches the bound, the current accumulator is final.

theorem Fin.foldlMFrom_step {m : Type → Type} [Monad m] {α : Type} {n : ℕ} (f : α → Fin n → m α) (a : α) {i : ℕ} (hi : i < n) :
foldlMFrom f a i = do let a' ← f a ⟨i, hi⟩ foldlMFrom f a' (i + 1)

A live cursor consumes one index and continues at its successor.