Skip to content

Commit 2bfd40c

Browse files
committed
Formalize reduce semantics and incremental correctness
1 parent 4f3045d commit 2bfd40c

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

reduce.pdf

1.89 KB
Binary file not shown.

reduce.tex

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ \section{Introduction}
6868
\section{Preliminaries}\label{sec:preliminaries}
6969

7070
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$.
7272

7373
\begin{definition}[Collection]
7474
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}
127127
If an initial element $\iota \in A$ is fixed, we abbreviate $\mathsf{fold}_\star(M) := \mathsf{fold}_\star(\iota, M)$.
128128
\end{definition}
129129

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+
130143
\section{The Reduce Combinator}\label{sec:reduce}
131144

132145
The \texttt{reduce} combinator produces a \emph{view} of a collection by summarizing the values for each key.
@@ -168,9 +181,9 @@ \subsection{Reducers}
168181
For a reducer $R = (\iota, \oplus, \ominus)$, we write $\mathsf{reduce}_R$ for $\mathsf{reduce}_{\iota,\oplus}$.
169182

170183
\begin{definition}[Well-Formed Reducer]\label{def:well-formed-reducer}
171-
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$:
172185
\[
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)
174187
\]
175188
\end{definition}
176189

@@ -272,7 +285,7 @@ \section{Correctness}\label{sec:correctness}
272285
Let $R = (\iota, \oplus, \ominus)$ be a reducer (with pairwise commutative $\oplus$ and $\ominus$).
273286
The following are equivalent:
274287
\begin{enumerate}
275-
\item $R$ is well-formed (i.e., $(a \oplus v) \ominus v = a$ for all $a \in A$, $v \in V$).
288+
\item $R$ is well-formed in the sense of Definition~\ref{def:well-formed-reducer}.
276289
\item $R$ satisfies the incremental correctness property.
277290
\end{enumerate}
278291
\end{theorem}
@@ -350,14 +363,11 @@ \section{Correctness}\label{sec:correctness}
350363

351364
Therefore $(a \oplus v) \ominus v = a$.
352365

353-
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}.
355367
\end{proof}
356368

357369
\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$.
361371
\end{remark}
362372

363373
\section{Examples}\label{sec:examples}
@@ -401,7 +411,7 @@ \subsection{Min Reducer (Partial)}
401411
R_{\mathsf{min}} = (+\infty, \lambda(a,v).\, \min(a,v), \bot)
402412
\]
403413

404-
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}.
405415
Consider $C(k) = \{3, 5\}$ with $a_k = 3$.
406416
If we remove $5$, we need $a'_k = 3$, which is correct.
407417
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.
@@ -415,7 +425,8 @@ \subsection{Min Reducer (Partial)}
415425
\section{Complexity}
416426

417427
\begin{theorem}[Time Complexity]
418-
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:
419430
\begin{itemize}
420431
\item Adding a value: $O(1)$ to update the accumulator
421432
\item Removing a value: $O(1)$ to update the accumulator

0 commit comments

Comments
 (0)