Documentation

CslibFrontier.Foundations.Data.List.EmitChain

Indexed expansion of block-emitting list recursions #

A recursion consumes a prefix, emitting a block at each step and replacing its carry by a value determined by the step index. The final suffix is passed to a terminal operation. The expansion retains the order and multiplicity of every emitted block.

Main results #

theorem List.eq_flatMap_range_of_emit {α : Type u_1} {β : Type u_2} (f : ℕ → α → List α → List β) (next : ℕ → α) (emit : ℕ → α → α → List β) (finish : α → List α → List β) (reserve : ℕ) (hshort : ∀ (j : ℕ) (carry : α) (rest : List α), rest.length ≤ reserve → f j carry rest = finish carry rest) (hstep : ∀ (j : ℕ) (carry b : α) (rest : List α), reserve < (b :: rest).length → f j carry (b :: rest) = emit j carry b ++ f (j + 1) (next j) rest) (j : ℕ) (carry : α) (rest : List α) :
f j carry rest = flatMap (fun (i : ℕ) => emit (j + i) (if i = 0 then carry else next (j + (i - 1))) (rest.getD i carry)) (range (rest.length - reserve)) ++ finish (if rest.length - reserve = 0 then carry else next (j + (rest.length - reserve - 1))) (drop (rest.length - reserve) rest)

Expand a block-emitting recursion that leaves reserve entries for its terminal operation. The new carry depends only on the absolute index; it need not equal the emitted value.