Balanced folds of lists #
List.balancedFold op e f l combines the images under f of the elements of l by the binary
operation op along a balanced binary tree: the empty list gives e, a singleton [b] gives
f b, and a list of length k ≥ 2 gives op of the folds of its first ⌊k / 2⌋ elements and of
the others. The tree has depth ⌈log₂ k⌉, so a measure that grows by at most c with every
application of op grows by at most c ⌈log₂ k⌉ along the fold (List.apply_balancedFold_le).
For an associative operation with identity e, the fold is foldr (List.balancedFold_eq_foldr),
whatever the shape of the tree.
Balanced folds give formulas of logarithmic depth: the disjunction of k formulas, and the
formula saying that a branching program of length k maps one state to another.
Main definitions #
List.balancedFold: the fold of a list along a balanced binary tree.
Main results #
List.balancedFold_induction: a property preserved by concatenation holds for balanced folds.List.balancedFold_hom: maps commuting with the operations commute with balanced folds.List.balancedFold_eq_foldr: for an associative operation with an identity, the balanced fold isfoldr.List.apply_balancedFold_le: a measure growing by at mostcwith each operation grows by at mostc ⌈log₂ k⌉along a balanced fold ofkelements.
The fold of l along a balanced binary tree: e for the empty list, f b for [b], and for
a list of length k ≥ 2, op applied to the folds of its first ⌊k / 2⌋ elements and of the
others.
Instances For
The balanced fold of the empty list is e.
The balanced fold of a singleton [b] is f b.
A list of length at least two folds into op of the folds of its two halves.
Induction for balanced folds. A property of lists and values that holds for the empty list
and e, for every singleton [b] and f b, and for l₁ ++ l₂ and op x y whenever it holds for
nonempty l₁ and x and for nonempty l₂ and y, holds for every list and its balanced fold.
For an associative operation with identity e, the balanced fold is foldr.
Depth of a balanced fold. If applying op raises a measure d by at most c above its
values on the arguments, and d is at most D on the leaves, then d is at most
D + c ⌈log₂ k⌉ on the balanced fold of a list of k elements; e only matters for the empty
list.