Documentation

CslibFrontier.Foundations.Data.List.Countdown

Counting down to a mapped list element #

List.foldl_countdown describes a left fold that decrements a counter and captures a mapped element when the counter reaches one. A zero counter preserves the captured value. This lets streaming input readers share the same selection argument across symbol representations.

theorem List.foldl_countdown {α : Type u_1} {β : Type u_2} (f : α → β) (l : List α) (k : ℕ) (cur : β) (d : α) :
foldl (fun (p : ℕ × β) (a : α) => (p.1 - 1, if p.1 = 1 then f a else p.2)) (k, cur) l = (k - l.length, if 0 < k ∧ k ≤ l.length then f (l.getD (k - 1) d) else cur)

Counting down from k captures the mapped element at position k - 1 if k is positive and at most the list length; otherwise it preserves cur. The counter saturates at zero.