List Utils #
Small list-fold lemmas used throughout TorchLean proof files. They are generic, not NN-specific, so tensor, runtime-approximation, and RL proofs can share the same fold facts instead of carrying local copies.
foldl max upper bound helper.
If the initial accumulator and every f i are ≤ eps, then the folded maximum is also ≤ eps.
Lower bound helper for foldl max.
The folded maximum is always at least as large as the initial accumulator.
Membership helper for foldl max.
If i ∈ l, then f i is ≤ the folded maximum.
A finite left fold of max returns either its initial accumulator or one of the values it
visited. This complements the order bounds above with the attainment fact needed by stable
softmax: the computed maximum is an actual input coordinate, so one max-shifted exponential is
exactly exp 0 = 1.
Pointwise congruence for left folds of the shape acc + f x.
Many tensor and autograd proofs use executable folds for sums; this lemma lets them rewrite the per-coordinate summand without re-proving a list induction locally.
Turn foldl (fun a x => a + f x) acc into acc + foldl (fun a x => a + f x) 0.
This is the standard "peel off the initial accumulator" lemma for left folds over +.
Distribute a fold of g1 x + g2 x into the sum of two folds.
This is the list-level form of “finite sum distributes over addition”.
Length of the second component when a left fold prepends exactly one output per input.
This captures the common "right-to-left scan implemented as reverse.foldl" proof pattern used by
return/advantage computations: the first accumulator evolves by step, while the second accumulator
records one new value with :: at every iteration.
Rewrite the canonical List.finRange addition fold into a Finset.univ sum.
Specs use List.foldl because it computes well; proofs usually want Finset.sum so standard
big-operator lemmas apply.
Accumulator form of finRange_foldl_add_eq_finset_sum.
This avoids re-proving "peel off the initial accumulator, then rewrite the zero fold" in large finite-sum proofs.