Recall the (simplified) definition of reduce:
T reduce(T id, BinaryOp<T> acc);
U reduce(U id, BiFn<U, T, U> acc, BinaryOp<U> cmb);
This has 2 cases:
- Sequential execution. This is equivalent to
foldl. - Parallel execution. Things start to get weird:
- The stream is first broken up into multiple substreams, which
foldlis applied on individually, each yielding typeU(originally typeT). - The subresults are then combined, not necessarily via
foldl.
- The stream is first broken up into multiple substreams, which
The notes then provide a few “conditions” for the triple (id, acc, cmb) — hereafter denoted (
- Purity of A and C. (Obvious)
- Identity.
- Associativity of
. - Compatibility of
with .
The motivation of this document is to resolve my own confusion with regard to which conditions are necessary, and which sets of conditions are sufficient — the notes are oddly vague and handwavy on this.
First, definitions:
- We shall be applying
reduceon a stream of elements from a set, which returns a particular output in set . ; ; - Notice that the two argument
reduceis simply a special case of the three argument version, whereand . Thus it shall not be elaborated on.
- We shall say that a condition is necessary or sufficient for some predicate
, where is true if and only if for any stream of elements in , any possible parallel reduction output equals that of the sequential reduction.
We shall initially hold the assumption that repeated application of
then we shall assume that
First, consider any
Which is our identity condition (1).
Next, we append any element
which needs to hold
Finally, consider any
such that
We break the stream into 3 substreams
or
both of which should yield the same result (as per supposition), we arrive at the associativity condition (2).
From here, for any sequentially reduced stream we have
And similarly any other “grouping” of substreams (i.e. parallely reduced) can be broken down into the above form (some of which invoke condition (1)), thus they are equivalent.
Note: The explanation for (0) is obvious and thus omitted.
As mentioned, above hinges upon the assumption that there exists a stream that evaluates to
Notice that the three (four including (0)) conditions above act primarily on
Suppose
, for all
Simply attempting to restrict the domain of the universal statement to
where
with a reminder that every
, for all
Trivial, same reasoning as above. The domain is restricted to
, for all
Trivial, same reasoning as above. Following the original explanation for (2), we “break”
Again, the explanation for (0) is (also) trivial and thus omitted.
TL;DR
The combiner
, for all , for all , for all
Where
Update 2025-11-13
Upon closer inspection, I realise that technically, the domain for (3) should use
I then attempted to find a counterexample where (3) still holds for
Let
- Since
, there exist both a sequence of substreams over and some parallelization (of ) on that sequence which eventually yield . - Since
, there does not exist a (full) stream over where sequential application of yields . - Notably, consider the flattened sequence of substreams from [1] into a “full” stream; its sequential application of
does not in fact yield (from [2]).
This contradicts either
- the assumption that (1), (2), and (3) (and trivially (0)) are universally true under
; or - if not, then our supposition that there exists an element in
.
Hence the updated TL;DR shall be that, the combiner
, for allidentity , for allassociativity , for allcompatibility
Where
And since it wasn’t made directly obvious above (unchanged from previous TL;DR, just included for clarity sake):
- The associativity of the accumulator doesn’t matter. (It is not really well-defined anyway, for
) - The above conditions are necessary and sufficient for a parallelization to be “correct”.
- If the input stream is fixed and predetermined, then
effectively reduces to the set of elements in that stream only.- Proving properties over this restricted
yields sufficiency but of course not necessity.
- Proving properties over this restricted
Additional proof of sufficiency (since necessity is directly implied through
Suppose the above conditions are true for all
More specifically by supposition,
However, (
Thus arriving at a contradiction.