You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: reduce.tex
+22-11Lines changed: 22 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -68,7 +68,7 @@ \section{Introduction}
68
68
\section{Preliminaries}\label{sec:preliminaries}
69
69
70
70
Let $K$ be a set of keys, $V$ a set of values, and $A$ a set of accumulator values.
71
-
For a set $V$, we write $\mathcal{M}(V)$ for the set of finite multisets over $V$; we use $\uplus$ and $\setminus$ for multiset union and multiset difference, respectively.
71
+
For a set $V$, we write $\mathcal{M}(V)$ for the set of finite multisets over $V$; we use $\uplus$ and $\setminus$ for multiset union and multiset difference, respectively, and write $M \subseteq N$ for multisets when every element has multiplicity in $M$ less than or equal to its multiplicity in $N$.
72
72
73
73
\begin{definition}[Collection]
74
74
A \emph{collection} is a function $C : K \to\mathcal{M}(V)$. We write $C(k)$ for the multiset of values associated with key $k$.
@@ -127,6 +127,19 @@ \subsection{Folds}
127
127
If an initial element $\iota\in A$ is fixed, we abbreviate $\mathsf{fold}_\star(M) := \mathsf{fold}_\star(\iota, M)$.
128
128
\end{definition}
129
129
130
+
\begin{lemma}[Fold over Union of Multisets]
131
+
Let $\star : A \times V \to A$ be pairwise commutative and let $M, N \in\mathcal{M}(V)$ be finite multisets.
132
+
Then for all $a \in A$:
133
+
\[
134
+
\mathsf{fold}_\star(a, M \uplus N) = \mathsf{fold}_\star(\mathsf{fold}_\star(a, M), N).
135
+
\]
136
+
\end{lemma}
137
+
138
+
\begin{proof}
139
+
Choose an enumeration of $M \uplus N$ in which all elements of $M$ appear first, followed by all elements of $N$.
140
+
The result then follows immediately from the definition of $\mathsf{fold}^{\mathsf{seq}}_\star$ and the fact that $\mathsf{fold}_\star$ is independent of the particular enumeration.
141
+
\end{proof}
142
+
130
143
\section{The Reduce Combinator}\label{sec:reduce}
131
144
132
145
The \texttt{reduce} combinator produces a \emph{view} of a collection by summarizing the values for each key.
@@ -168,9 +181,9 @@ \subsection{Reducers}
168
181
For a reducer $R = (\iota, \oplus, \ominus)$, we write $\mathsf{reduce}_R$ for $\mathsf{reduce}_{\iota,\oplus}$.
A reducer $R = (\iota, \oplus, \ominus)$ is \emph{well-formed} if $\ominus$ is the \textbf{inverse} of $\oplus$:
184
+
A reducer $R = (\iota, \oplus, \ominus)$ is \emph{well-formed} if $\ominus$ is the \textbf{inverse} of $\oplus$ on reachable accumulator values, that is, for all finite multisets $M \in\mathcal{M}(V)$ and all $v \in V$:
172
185
\[
173
-
\forall a \in A, v \in V.\; (a\oplus v) \ominus v = a
186
+
( \mathsf{fold}_\oplus(\iota, M)\oplus v) \ominus v = \mathsf{fold}_\oplus(\iota, M)
This establishes the inverse property for all $a$ in the image of $\mathsf{fold}_\oplus(\iota, -)$.
354
-
Since every accumulator value that arises during execution of $\mathsf{reduce}$ is of this form, this suffices for correctness.
366
+
This establishes the inverse property for all $a$ of the form $\mathsf{fold}_\oplus(\iota, M)$, i.e.\ for all reachable accumulator values, which is exactly the condition in Definition~\ref{def:well-formed-reducer}.
355
367
\end{proof}
356
368
357
369
\begin{remark}
358
-
The proof of (2 $\Rightarrow$ 1) establishes the inverse property for \emph{reachable} accumulator values---those expressible as $\mathsf{fold}_\oplus(\iota, M)$ for some finite multiset $M$.
359
-
If the accumulator type $A$ contains unreachable values, the inverse property need not hold for them.
360
-
In practice, for reducers like sum over integers or product over rationals, all values are reachable, so the distinction is immaterial.
370
+
In many practical reducers (for example sum over integers or product over rationals), every accumulator value is reachable as $\mathsf{fold}_\oplus(\iota, M)$ for some multiset $M$, so the definition of well-formedness above coincides with the simpler global inverse law $(a \oplus v) \ominus v = a$ for all $a \in A$, $v \in V$.
The min reducer does not have a well-defined remove operation $\ominus$ in general.
414
+
The min reducer does not have a well-defined remove operation $\ominus$ in general, so $R_{\mathsf{min}}$ is not a reducer in the strict sense of Definition~\ref{def:well-formed-reducer}.
405
415
Consider $C(k) = \{3, 5\}$ with $a_k = 3$.
406
416
If we remove $5$, we need $a'_k = 3$, which is correct.
407
417
But if we remove $3$, we need $a'_k = 5$---yet from $a_k = 3$ alone, we cannot recover that $5$ was the second-smallest value.
For a well-formed reducer with $O(1)$ add and remove operations:
428
+
For a reducer $R = (\iota, \oplus, \ominus)$ where $\oplus$ and $\ominus$ are $O(1)$ operations, the time to compute $\mathsf{update}_R(a_k, \Delta, k)$ is $O(|\Delta^-(k)| + |\Delta^+(k)|)$.
429
+
In particular, if each logical update induces a delta with $O(1)$ values per key, then:
419
430
\begin{itemize}
420
431
\item Adding a value: $O(1)$ to update the accumulator
421
432
\item Removing a value: $O(1)$ to update the accumulator
0 commit comments