\documentclass{article}
\input{../../../lib.sty}
\usepackage{bbm}

\title{\texttt{fn make\_expr\_count}}
\author{Michael Shoemate}

\begin{document}
\maketitle

This document proves that the implementation of \rustdoc{transformations/make_stable_expr/expr_count/fn}{make\_expr\_count} in \asOfCommit{mod.rs}{f5bb719} 
satisfies its proof definition.

\section{Hoare Triple}
\subsection*{Preconditions}
\subsubsection*{Compiler-verified}
\begin{itemize}
    \item Argument \texttt{input\_domain} of type \texttt{ExprDomain}
    \item Argument \texttt{input\_metric} of type \texttt{PartitionDistance<MI>}
    \item Argument \texttt{expr} of type \texttt{Expr}
    \item Generic \texttt{MI} must implement \texttt{UnboundedMetric}
    \item Const generic \texttt{P} must be of type \texttt{usize}
    \item \texttt{(ExprDomain, PartitionDistance<MI>)} implements \texttt{MetricSpace}
    \item \texttt{(ExprDomain, LpDistance<P, f64>)} implements \texttt{MetricSpace}
    \item \texttt{Expr} implements \texttt{StableExpr<PartitionDistance<MI>, PartitionDistance<MI>>}
\end{itemize}

\subsubsection*{Caller-verified}
None

\subsubsection*{Pseudocode}

\lstinputlisting[language=Python,firstline=2,escapechar=`]{./pseudocode/make_expr_count.py}

\subsubsection*{Postcondition}
\validTransformation{(input\_domain, input\_metric, expr, MI, P)}{make\_expr\_count}

\section{Proof}
Starting from line \ref{match-strategy}, 
\texttt{expr} is analyzed to determine the type of counting query \texttt{strategy} and input to the counting query \texttt{input}.

All preconditions for \texttt{make\_stable} on line \ref{t-prior} are compiler-verified, 
therefore by the postcondition \texttt{t\_prior} is a valid transformation.

To prove that the output is a valid transformation, 
we must first prove that the transformation on line \ref{count-trans} is valid.

\begin{proof}
    \textbf{Appropriate Output Domain}
    Since the count transformation is not row-by-row, 
    line \ref{alignment} disallows this count transformation from being constructed in a row-by-row context,
    satisfying the requirements of ExprDomain.

    By the definition of each of the allowed counting expressions in \ref{match-strategy},
    the resulting output will always contain one series whose name matches the active series' name,
    and the data type of elements in the series is u32,
    which is consistent with \ref{output-series}.
    This series is then part of a dataframe and and operations are continued to be applied with the same context,
    on line \ref{output-domain}.    
    This domain is then used on line \ref{output-domain-usage} to construct the count transformation.
\end{proof}

\begin{proof}
    \textbf{Stability Guarantee}

    To determine if \texttt{input} is row-by-row,
    line \ref{is-row-by-row} checks if \texttt{input} can be parsed into a row-by-row transformation.

    Line \ref{will-count-all} uses domain descriptors to determine if the counting query will count all rows.
    This is the case if the counting query is \texttt{Len}, 
    or if the counting query counts all non-null values in data that doesn't contain nulls.

    If all rows are to be counted, 
    then the \texttt{public\_info} domain descriptor about the partitioning applies to this count query strategy, 
    on line \ref{public-info}.

    The function defined on line \ref{function} applies the counting query to the input expression with the given query strategy.
    The stability of this function is governed by the stability map returned by \rustdoc{transformations/make_stable_expr/expr_count/fn}{counting\_query\_stability\_map},
    as the \texttt{Len}, \texttt{Count} and \texttt{NullCount} strategies can be expressed as arbitrary predicates,
    and the \texttt{NUnique} strategy can be considered as a 1-stable (non-row-by-row) "unique" transformation chained with a counting query with the \texttt{Len} strategy.
\end{proof}

Since it has been shown that both \texttt{t\_prior} and the count transformation are valid transformations,
then the preconditions for \texttt{make\_chain\_tt} are met (invoked via the right-shift operator shorthand).

\end{document} 