Source/Packages

Std.List

packages/foundation/standard/src/Std/List.alpha

428 lines21 declarations14.7 KiBSHA-256 1d350bdfe159

def · lines 179–206

stdListFoldFromHead

Full file
Fold from the head: `step (... (step (step initial x0) x1) ...) xn`. stdListFold visits the LAST element first (its step sees the folded tail); this visits the FIRST element first and threads the accumulator forward, which is the order a floating-point accumulation or a reverse-time pass over a newest-first history must respect (the results differ when the step does not associate, e.g. f32 addition). The list is eliminated into a function of the accumulator, so the recursion is still structural.
179def stdListFoldFromHead =
180  (lambda erased element : Type 0 .
181    erased accumulator : Type 0 .
182    unrestricted step : (pi unrestricted carried : accumulator . unrestricted value : element . accumulator) .
183    unrestricted initial : accumulator .
184    unrestricted values : (family StdList element) .
185    app
186      (eliminate
187        StdList
188        (lambda unrestricted current : (family StdList element) .
189          pi unrestricted carried : accumulator .
190          accumulator)
191        values
192        (branch StdListEmpty . lambda unrestricted carried : accumulator . carried)
193        (branch
194          StdListCons
195          head
196          tail
197          induction
198          .
199          lambda
200          unrestricted
201          carried
202          :
203          accumulator
204          .
205          induction (step carried head)))
206      initial)

The compiler supplied declaration spans and resolved links from this source snapshot. This page does not assert that this file belongs to a checked closure.