\section{Type-Error Witnesses}
\label{sec:searching-witness}

% Our goal is to find concrete values that demonstrate how a
% program ``goes wrong''.

% \paragraph{Problem: Which inputs are bad?}
% %
% One approach is to randomly generate input values and use
% them to execute the program until we find one that causes
% the program to go wrong. However, to see why this approach
% is naive, consider the following example:
% %
% \begin{lstlisting}
%   let f x =
%     let y = 1 + x in
%       1. +. y
% \end{lstlisting}
% %
% What \emph{types} of inputs should we test \texttt{f} with?
% Values of type \texttt{int} and \texttt{float} are fair game,
% but values of type say, \texttt{string} or \texttt{int list}
% will cause the program to go wrong in an \emph{irrelevant}
% manner.

% \paragraph{Solution:} \RJ{STOP}

% we cannot provide \emph{completely arbitrary} inputs to
% \texttt{f}. Instead, we call \texttt{f} with a \emph{hole}, written
% \ehole{}, which is a placeholder for a value whose type we have not
% yet determined. As we execute the program, we instantiate holes with
% concrete values as demanded by the primitive operations in the
% program. For example, the hole we pass to f will be instantiated to an
% int when we reach the \lstinline{1 + x} term. Thus, y will be an int as
% well, and the program will get stuck at \lstinline{1. +. y}. \ES{this
%   reads more like overview text..}
%

% \begin{itemize}
% \item how do we run ill-typed programs?
% \item for a lang like ocaml, dynamic semantics are independent of static
%   semantics, just lambda calculus. so no problem to run ill-typed
%   program
% \item but what about functions? what type of arguments should we pass? consider
%
% \begin{lstlisting}
% let f x =
%   let y = 1 + x in
%     1. +. y
% \end{lstlisting}
%
% does \texttt{f} take an int, float, string? int and float are both
% somewhat plausible, but string or anything else is ``clearly'' bogus. so
% we cannot provide \emph{completely arbitrary} inputs to
% \texttt{f}. Instead, we call \texttt{f} with a \emph{hole}, written
% \ehole{}, which is a placeholder for a value whose type we have not
% yet determined. As we execute the program, we instantiate holes with
% concrete values as demanded by the primitive operations in the
% program. For example, the hole we pass to f will be instantiated to an
% int when we reach the \lstinline{1 + x} term. Thus, y will be an int as
% well, and the program will get stuck at \lstinline{1. +. y}. \ES{this
%   reads more like overview text..}
%
% % \item values are tagged with their types, just like ``untyped'' langs
% % \item special ``hole'' value whose type is not yet known, used for function args
% % \item on-the-fly unification to determine ``correct'' type for holes
% \end{itemize}

Next, we formalize the notion of type error witnesses as follows.
%
First, we define a core calculus within which we will work~(\S~\ref{sec:syntax}).
%
Second, we develop a (non-deterministic) operational semantics
for ill-typed programs that precisely defines the notion
of a \emph{witness}~(\S~\ref{sec:semantics}).
%
Third, we formalize and prove a notion of \emph{generality} for
witnesses, which states, intuitively, that if we find a
single witness then for \emph{every possible} type
assignment there exist inputs that are guaranteed to make
the program ``go wrong''~(\S~\ref{sec:soundness}).
%
Finally, we refine the operational semantics into a
\emph{search procedure} that returns concrete (general)
witnesses for ill-typed programs~\S~(\ref{sec:search-algorithm}).
%
We have formalized and tested our semantics and generality theorem
in \textsc{PLT-Redex}~\cite{Felleisen2009-ya}.
%
Detailed proofs for the theorems in this section can be found in
%
\ifthenelse{\equal{\isTechReport}{true}}
{Appendix~\ref{sec:proofs}.}
{Appendix A of the accompanying tech report~\cite{Seidel2016Dynamic-TechRep}.}

\subsection{Syntax}
\label{sec:syntax}
\input{syntax.tex}
%
Figure~\ref{fig:syntax} describes the syntax of \lang, a simple lambda
calculus with integers, booleans, pairs, and binary trees.
%
As we are specifically interested in programs that \emph{do} go wrong,
we include an explicit \stuck\ term in our syntax. We write $\estuck$ to
denote terms that may be \stuck, and $e$ to denote terms that may not be
stuck.

\paragraph{Holes}
\label{sec:holes}
%
Recall that a key challenge in our setting is to find witnesses
that are meaningful and do not arise from choosing values from
irrelevant types.
%
We solve this problem by equipping our term language with a
notion of a \emph{hole}, written \vhole{\thole}, which represents
an \emph{unconstrained} value $\ehole$ that may be replaced with
\emph{any} value of an unknown type \thole.
%
Intuitively, the type holes \thole\ can be viewed as type variables
that we will \emph{not} generalize over.
%
A \emph{normalized} value is one that is not a hole,
but which may internally contain holes.
%
For example
$\vnode{\thole}{\vhole{\thole}}{\vleaf{\thole}}{\vleaf{\thole}}$ is a
normalized value.

\paragraph{Substitutions}
%
Our semantics ensure the generality of witnesses by incrementally
\emph{refining} holes, filling in just as much information as is
needed locally to make progress (inspired by the manner in
which SmallCheck uses lazy evaluation~\cite{Runciman2008-ka}).
%
We track how the holes are incrementally filled in, by using
value (resp.\ type) \emph{substitutions} $\vsu$ (resp. $\tsu$)
that map value (resp.\ type) holes to values (resp.\ types).
%
The substitutions let us ensure that we consistently instantiate each
hole with the same (partially defined) value or type, regardless of the
multiple contexts in which the hole appears.
%
This ensures we can report a concrete (and general) witness for any
(dynamically) discovered type errors.

A \emph{normalized} value substitution is one whose co-domain is
comprised of normalized values.
%
In the sequel, we will assume and ensure that all value substitutions
are normalized.
%
We ensure additionally that the co-domain of a substitution does not
refer to any elements of its domain, \ie when we extend a substitution
with a new binding we apply the substitution to itself.

% \paragraph{Resolving Holes}
% We \emph{resolve} a hole with respect to a substitution by
% transitively applying the substitution as long as it contains
% any holes that are defined in the substitution.
% %
% We write \resolve{\thole}{\tsu} to denote the the resolution
% of \thole\ with respect to \tsu.
% %
% Note that by definition, \resolve{\thole}{\tsu} does not contain
% any holes in the domain of \tsu.

\subsection{Semantics}
\label{sec:semantics}
%
Recall that our goal is to synthesize a value that demonstrates
why (and how) a function goes wrong.
%
We accomplish this by combining evaluation with type inference,
giving us a form of dynamic type inference.
% starting with an unconstrained hole and gradually refining it
% as evaluation constrains its type.
%
Each primitive evaluation step tells us more about the types of the
program values. For example, addition tells us that the addends must be
integers, and % case-analysis tells us that the scrutinee must be a tree.
an if-expression tells us the condition must be a boolean.
%
When a hole appears in such a context, we know what type it must have
in order to make progress and can fill it in with a concrete value.

The evaluation relation is parameterized by a pair of functions:
called \emph{narrow} (\forcesym) and \emph{generate} (\gensym),
that ``dynamically'' perform type-checking and hole-filling
respectively.

\paragraph{Narrowing Types} The procedure % $\force{v}{t}{\vsu}{\tsu}$,
$$
\forcesym : v \times t \times \vsu \times \tsu \rightarrow \triple{v \cup \stuck}{\vsu}{\tsu}
$$
defined in Figure~\ref{fig:narrow}, takes as input a value $v$, a type
$t$, and the current value and type substitutions, and refines $v$ to
have type $t$ by yielding a triple of either the same value and
substitutions, or yields the stuck state if no such refinement is
possible. In the case where $v$ is a hole, it first checks in the given
$\vsu$ to see if the hole has already been instantiated and, if so,
returns the existing instantiation.
%
\begin{figure*}[t]
$$
\begin{array}{lcl}
%% \multicolumn{3}{l}{\forcesym \ :: \  (e, t) \rightarrow \pair{e}{\vsu}} \\
\forcesym                  & : & v \times t \times \vsu \times \tsu \rightarrow \triple{v \cup \stuck}{\vsu}{\tsu} \\
% \force{v}{\thole}{\vsu}{\tsu}  & \defeq & \hspace{-1ex}
% \begin{cases}
%   \triple{v}{\vsu}{\tsu'} & \mbox{if } \tsu' = \unify{\{\typeof{v}, \thole\}}{\tsu} \\
%   \triple{\stuck}{\vsu}{\tsu} & \mbox{otherwise} \\

%   % \force{v}{\subst{\tsu}{\thole}}{\vsu}{\tsu} & \mbox{if}\ \thole \in dom(\tsu) \\
%   % \triple{v}{\vsu}{\extendsu{\tsu}{\thole}{\typeof{v}}} & \mbox{otherwise} \\
% \end{cases} \\
\force{\vhole{\thole}}{t}{\vsu}{\tsu} & \defeq & \hspace{-1ex}
\begin{cases}
  \triple{v}{\vsu}{\tsu'}    & \mbox{if } v = \lookupsu{\vsu}{\vhole{\thole}},
                                         \tsu' = \unify{\{\thole, t, \typeof{v}\}}{\tsu}\\
  \triple{\stuck}{\vsu}{\tsu} & \mbox{if } v = \lookupsu{\vsu}{\vhole{\thole}} \\
  \triple{v}{\extendsu{\vsu}{\vhole{\thole}}{v}}{\tsu'} & \mbox{if}\ \tsu' = \unify{\{\thole, t\}}{\tsu}, v = \gen{t}{\tsu'}\\
\end{cases} \\
% \begin{cases}
%   \triple{\lookupsu{\vsu}{\ehole}}{\vsu}{\tsu} & \mbox{if}\ \ehole \in dom(\vsu), \hastype{\lookupsu{\vsu}{\ehole}}{t} \\
%   \triple{\stuck}{\vsu}{\tsu}                  & \mbox{if}\ \ehole \in dom(\vsu) \\
%   \triple{v}{\extendsu{\vsu}{\ehole}{v}}{\tsu}   & v = \gen{t}\\
% \end{cases} \\
 % \begin{array}{l}
    % \mathtt{if}\ i \in \vsu\ \mathtt{then}\ \triple{\vsu(i)}{\vsu}\ \mathtt{else} \\
         % \elet{v}{\gen{t}}{\triple{v}{\ehole{i} \mapsto v}}
  % \end{array} \\
\force{n}{\tint}{\vsu}{\tsu}     & \defeq & \triple{n}{\vsu}{\tsu} \\
\force{b}{\tbool}{\vsu}{\tsu}    & \defeq & \triple{b}{\vsu}{\tsu} \\
\force{\efun{x}{e}}{\tfun}{\vsu}{\tsu} & \defeq & \triple{\efun{x}{e}}{\vsu}{\tsu} \\
\force{\epair{v_1}{v_2}}{\tprod{t_1}{t_2}}{\vsu}{\tsu} & \defeq & \triple{\epair{v_1}{v_2}}{\vsu}{\tsu''}, \mbox{if}\ \tsu' = \unify{\{\typeof{v_1}, t_1\}}{\tsu}, \tsu'' = \unify{\{\typeof{v_2}, t_2\}}{\tsu'}  \\
\force{\vleaf{t_1}}{\ttree{t_2}}{\vsu}{\tsu} & \defeq & \triple{\vleaf{t_1}}{\vsu}{\tsu'}, \mbox{if}\ \tsu' = \unify{\{t_1, t_2\}}{\tsu} \\
\force{\vnode{t_1}{v_1}{v_2}{v_3}}{\ttree{t_2}}{\vsu}{\tsu} & \defeq & \triple{\vnode{t_1}{v_1}{v_2}{v_3}}{\vsu}{\tsu'}, \mbox{if}\ \tsu' = \unify{\{t_1, t_2\}}{\tsu} \\
\force{v}{t}{\vsu}{\tsu} & \defeq & \triple{\stuck}{\vsu}{\tsu}
\end{array}
$$
\caption{Narrowing values}
\label{fig:narrow}
\end{figure*}
%
%While a hole may map to a value that \emph{contains} another hole, \eg a
%lambda or a tree, it may not map \emph{directly} to another hole,
As the value substitution is normalized, in the first case of \forcesym\ we
do not need to \forcesym\ the result of the substitution, the sub-hole
will be narrowed when the context demands it.

\paragraph{Generating Values} The (non-deterministic)
$\gen{t}{\tsu}$ in Figure~\ref{fig:gen} takes
as input a type $t$ and returns a value of that type.
%
For base types the procedure returns an arbitrary value of
that type.
%
For functions it returns a lambda with a \emph{new} hole
denoting the return value.
%
For unconstrained types (denoted
by $\thole$) it yields a fresh hole constrained to have type
\thole (denoted by $\vhole{\thole}$).
%
When generating a $\ttree{t}$ we must take care to ensure
the resulting tree is well-typed.
%
For a polymorphic type $\ttree{\thole}$ %, eg in \recasegoodone,
or $\tprod{\thole_1}{\thole_2}$
we will place holes in the generated value; they will be lazily filled
in later, on demand.

\begin{figure}[t]
$$
\begin{array}{lcll}
\gensym       & :   & t \times \tsu \rightarrow v \\
\gen{\thole}{\tsu}  & \defeq  & \gen{\subst{\tsu}{\thole}}{\tsu} &  \text{if } \thole \in dom(\tsu) \\
\gen{\tint}{\tsu}   & \defeq  & n &  \text{non-det.} \\
\gen{\tbool}{\tsu}  & \defeq  & b &  \text{non-det.} \\
\gen{\tprod{t_1}{t_2}}{\tsu}  & \defeq  & \epair{\gen{t_1}{\tsu}}{\gen{t_2}{\tsu}} & \\ % \begin{subarray}{l} v_1 = \gen{t_1}{\tsu}\\ v_2 = \gen{t_2}{\tsu}\end{subarray} \\
\gen{\ttree{t}}{\tsu}  & \defeq  & tr &  \text{non-det.} \\
\gen{\tfun}{\tsu}   & \defeq & \efun{x}{\vhole{\thole}} &  \text{\ehole, \thole are fresh} \\
\gen{\thole}{\tsu}  & \defeq & \vhole{\thole} & \text{\ehole is fresh} \\
\end{array}
$$
\caption{Generating values}
\label{fig:gen}
\end{figure}

\paragraph{Steps and Traces}
\input{operational.tex}
%
% WRW notes that Figure 4 does not seem to handle recursion (it's not clear
% how the let rule would work for something "let rec"-y, and there's not
% function call rule). I only mention this because I can imagine a reviewer
% wondering about your ability to generate good witnesses for function
% types. This could likely be addressed in text, by a forward reference to
% Section 3.4 where higher-order functions are handled, without changing
% any of the formalisms at the last minute.
Figure~\ref{fig:operational} describes the small-step contextual
reduction semantics for \lang.
%
A configuration is a triple $\triple{\estuck}{\vsu}{\tsu}$ of an
expression $e$ or the stuck term $\stuck$, a value substitution $\vsu$,
and a type substitution $\tsu$.
%
% We write \estuck to
% denote either an expression $e$ or \stuck.
%
We write $\step{\estuck}{\vsu}{\tsu}{\estuck'}{\vsu'}{\tsu'}$ if the state
$\triple{\estuck}{\vsu}{\tsu}$ transitions in a \emph{single step} to
$\triple{\estuck'}{\vsu'}{\tsu'}$.
%
A (finite) \emph{trace} $\trace$ is a sequence of configurations
$\triple{\estuck_0}{\vsu_0}{\tsu_0}, \ldots, \triple{\estuck_n}{\vsu_n}{\tsu_n}$ such that
$\forall 0 \leq i < n$, we have
$\step{\estuck_i}{\vsu_i}{\tsu_i}{\estuck_{i+1}}{\vsu_{i+1}}{\tsu_{i+1}}$.
%
We write \steptr{\trace}{\estuck}{\vsu}{\tsu}{\estuck'}{\vsu'}{\tsu'} if $\trace$ is
a trace of the form $\triple{\estuck}{\vsu}{\tsu},\ldots,\triple{\estuck'}{\vsu'}{\tsu'}$.
%
We write \steps{\estuck}{\vsu}{\tsu}{\estuck'}{\vsu'}{\tsu'} if
\steptr{\trace}{\estuck}{\vsu}{\tsu}{\estuck'}{\vsu'}{\tsu'} for some trace $\trace$.

\paragraph{Primitive Reductions}
%
% \RJ{Put high-level intuition about how "dynamic HM" is
% formalized in op-sem, to set up next few paragraphs}
%
Primitive reduction steps --- addition, if-elimination,
function application, and data construction and case
analysis --- use \forcesym to ensure that values have
the appropriate type (and that holes are instantiated)
before continuing the computation.
%
Importantly, beta-reduction \emph{does not} type-check its
argument, it only ensures that ``the caller'' $v_1$ is indeed
a function.

\paragraph{Recursion}
%
% \RJ{this para appears out of nowhere --- non-sequitur}
Our semantics lacks a built-in $\mathtt{fix}$ construct
for defining recursive functions, which may surprise
the reader.
%
Fixed-point operators often cannot be typed in static type
systems, but our system would simply approximate its type
as $\tfun$, apply it, and move along with evaluation.
%
Thus we can use any of the standard fixed-point operators
and do not need a built-in recursion construct.

%  but we are not concerned with \emph{assigning}
% types to terms, rather with showing that \emph{no type}
% can be assigned.
% %
% We are simply executing the untyped $\lambda$-calculus,
% which has no issue handling recursion.

%% \begin{thm}
%% \label{thm:all-reduce}
%%   Every closed expression $e$ reduces to a value $v$ (which may be \stuck).
%% \ES{do we really need to state this, or is it obvious?}
%% \end{thm}

% \begin{proof}%[Proof of \autoref{thm:all-reduce}]
%   Simple induction on the evaluation relation.
% \end{proof}

\subsection{Generality}\label{sec:soundness}

A key technical challenge in generating witnesses is
that we have no (static) type information to rely upon.
%
Thus, we must avoid the trap of generating \emph{spurious}
witnesses that arise from picking irrelevant values, when
instead there exist perfectly good values of a \emph{different}
type under which the program would not have gone wrong.
%
We now show that our evaluation relation instantiates holes
in a \emph{general} manner. That is, given a lambda-term $f$,
if we have $\steps{\eapp{f}{\vhole{\thole}}}{\emptysu}{\emptysu}{\stuck}{\vsu}{\tsu}$,
then \emph{for every} concrete type $t$, we can find a value
$v$ of type $t$ such that $\eapp{f}{v}$ goes wrong.

\begin{thm}{\textbf{[Witness Generality]}}
\label{thm:soundness}
  For any lambda-term $f$, if
  \hbox{$\steptr{\trace}{\eapp{f}{\vhole{\thole}}}{\emptysu}{\emptysu}{\stuck}{\vsu}{\tsu}$,}
  then for every
  (inhabited\footnote{All types in our formalism are inhabited, but in a larger language like \ocaml this may not be the case.})
  type
  % \footnote{We exclude builtin functions that subvert the type system, \eg \texttt{Obj.magic}, and thus consider the type $\forall a b. \tfun{a}{b}$ to be uninhabitable.}
  $t$ there exists a value $v$ of type $t$ such that
  $\steps{\eapp{f}{v}}{\emptysu}{\emptysu}{\stuck}{\vsu'}{\tsu'}$.
\end{thm}

We need to develop some machinery in order to prove this theorem.
First, we show how our evaluation rules encode a dynamic form of
type inference, and then we show that the witnesses found by
evaluation are indeed maximally general.

\paragraph{The Type of a Value} The \emph{dynamic type}
of a value $v$ is defined as a function $\typeof{v}$ shown
in Figure~\ref{fig:typeof}.
%
The types of primitive values are defined in the natural manner.
%
The types of functions are \emph{approximated}, which is all
that is needed to ensure an application does not get stuck.
%
For example,
$$\typeof{\efun{x}{\eplus{x}{1}}} = \tfun$$
instead of $\tint \rightarrow \tint$.
%
The types of (polymorphic) trees are obtained from the labels on their
values, and the types of tuples directly from their values.

\begin{figure}[t]
\[ \begin{array}{lcll}
    \typeof{n}   & \defeq & \tint & \\
    \typeof{b}   & \defeq & \tbool & \\
    \typeof{\efun{x}{e}} & \defeq & \tfun \\
    \typeof{\epair{v_1}{v_2}} & \defeq & \tprod{\typeof{v_1}}{\typeof{v_2}} \\
    \typeof{\vleaf{t}} & \defeq & \ttree{t} \\
    \typeof{\vnode{t}{v_1}{v_2}{v_3}} & \defeq & \ttree{t} \\
    \typeof{\vhole{\thole}} & \defeq & \thole \\
    % \typeof{\eleaf} & \defeq & \ttree{\thole}, & \quad \text{\thole is fresh} \\
    % \typeof{\enode{v_1}{v_2}{v_3}} & \defeq & \ttree{\thole}, & \quad \text{\thole is fresh} \\
    % \typeof{e} & \defeq & \thole, & \quad \text{\thole is fresh} \\
  \end{array} \]
\caption{The \emph{dynamic type} of a value.}
\label{fig:typeof}
\end{figure}

\paragraph{Dynamic Type Inference}
We can think of the evaluation of \eapp{f}{\vhole{\thole}}
as synthesizing a partial instantiation of \thole, and thus
\emph{dynamically inferring} a (partial) type for $f$'s input.
%
We can extract this type from an evaluation trace by
applying the final type substitution to \thole.
% \emph{resolving} the \thole\ with the final type
% substitution at the end of the trace.
%
Formally, we say that if
$\steptr{\trace}{\eapp{f}{\vhole{\thole}}}{\emptysu}{\emptysu}{\estuck}{\vsu}{\tsu}$,
then the \emph{partial input type} of $f$ up to $\trace$, written
\ptype{\trace}{f}, is $\resolve{\thole}{\tsu}$.

%repeatedly
%applying the final type substitution to \thole until it contains no
%holes in the domain of the substitution. We will call this process of
%repeated substition \emph{resolving} a hole, and will use
%\resolve{\thole}{\tsu} to denote the the resolution of \thole with
%respect to \tsu.

%$\typeof{\subst{\vsu}{\ehole}}$.
% \ES{should we say $\subst{\tsu}{\thole}$ instead? would need a helper function that does the repeated application of \tsu\ until the result has no $\thole \in dom(\tsu)$}
%
%We will omit the subscript when we wish to refer to the final partial
%type, \ie\ at the step where the expression has been reduced to a value
%(or stuck.)

% \begin{lem}
% \label{lem:narrow-tsu}
% If $\trace \defeq \triple{\eapp{f}{\vhole{\thole}}}{\emptysu}{\emptysu},\ldots$
% and $\trace' \defeq \trace, \triple{\estuck'}{\vsu'}{\tsu'}$
%     (\ie $\trace'$ is a single-step extension of $\trace$)
% and $\tsu \neq \tsu'$
% then the final step must have \emph{successfully} invoked \forcesym.
% \end{lem}

% \paragraph{Narrowing}
% %
% Only a successful call to \forcesym can change the partial
% input type of $f$.
% %
% \begin{lem}
% \label{lem:force-inst}
% If
% $\trace \defeq \triple{\eapp{f}{\vhole{\thole}}}{\emptysu}{\emptysu},\ldots,\triple{e}{\vsu}{\tsu}$
% and
% $\trace' \defeq \trace, \step{e}{\vsu}{\tsu}{e'}{\vsu'}{\tsu'}$
% (\ie $\trace'$ is a single-step extension of $\trace$)
% and
% $\ptype{\trace}{f} \neq \ptype{\trace'}{f}$,
% then the final step $\step{e}{\vsu}{\tsu}{e'}{\vsu'}{\tsu'}$
%  successfully invokes \forcesym.
% %
% \ES{not clear we still need this}
% \end{lem}

% \begin{proof}
%   By case analysis on the evaluation rules.
%   %
%   If $\ptype{\trace}{f} \neq \ptype{\trace'}{f}$ then,
%   % one of the holes in $f$'s
%   % argument must have been instantiated with a concrete value at the last step.
%   by the definition of $\ptype{\trace}{f}$, $\tsu \neq \tsu'$, as \thole
%   does not change.
%   %
%   % An examination of the rules shows that only place this happens is
%   % in the second case of \forcesym.
%   An examination of the rules shows that only \forcesym can update \tsu,
%   and furthermore that only the successful cases of \forcesym do update
%   \tsu.
% \end{proof}

\paragraph{Compatibility}
%
A type $s$ is \emph{compatible} with a type $t$, written \tcompat{s}{t},
if $\exists \tsu.\ \subst{\tsu}{s} = \subst{\tsu}{t}$.
%
That is, two types are compatible if there exists a type substitution
that maps both types to the same type.
%
A value $v$ is \emph{compatible} with a type $t$, written \vcompat{v}{t},
if $\tcompat{\typeof{v}}{t}$, that is, if the dynamic type of $v$ is
compatible with $t$.

% \paragraph{Preservation}
% We prove that each evaluation step \emph{refines} the partial input type
% of $f$, \ie\ preserves type compatibility.
% %
% \begin{lem}
% \label{lem:refine-partial}
% If $\trace \defeq \triple{\eapp{f}{\vhole{\thole}}}{\emptysu}{\emptysu},\ldots$ and
% $\trace'$ is a single-step extension of $\trace$, % \defeq \trace, \triple{\estuck'}{\vsu'}{\tsu'}$
% %
% %The partial type of $f$ upto $\trace$ is compatible
% %with the partial type upto $\trace'$, \ie\
% %
% then \tcompat{\ptype{\trace}{f}}{\ptype{\trace'}{f}}.
% %
% \ES{not clear we still need this}
% \end{lem}
% \begin{proof}
%   By case analysis on the evaluation rules.
%   %
%   First note that by Lemma~\ref{lem:force-inst} we can immediately
%   discharge the \rulename{E-*-Bad} rules as they cannot change
%   \ptype{\trace}{f} at all, and are thus trivially
%   compatibility-preserving. For the \rulename{E-*-Good} rules we can
%   show that, by virtue of \forcesym succeeding, all must preseve
%   compatibility.
%   % Note that all rules preserve partial types with the exception of when
%   % \forcesym\ is called on a hole, in which case we may instantiate the hole with
%   % a concrete value.
%   % %
%   % But $\typeof{\vhole{\thole}} = \thole$, which is compatible with any type.
% \end{proof}

\paragraph{Type Refinement}
A type $s$ is a \emph{refinement} of a type $t$, written $\tsub{s}{t}$,
if $\exists \theta. s = \subst{\theta}{t}$.
%
In other words, $s$ is a refinement of $t$ if there exists a type
substitution that maps $t$ directly to $s$.
%
A type $t$ is a \emph{refinement} of a value $v$, written $\tsub{t}{v}$,
if $\tsub{t}{\typeof{v}}$, \ie if $t$ is a refinement of the
dynamic type of $v$.
%
%\ES{calling a type a refinement of a value sounds really weird..}

% We prove that the partial instantiation of a type hole $\thole$
% is always a refinement of the partial instantiation of the
% associated value hole $\vhole{\thole}$.

\paragraph{Preservation}
%
We prove two preservation lemmas. First, we show that each evaluation
step refines the partial input type of $f$, thus preserving type
compatibility.
%
\begin{lem}
\label{lem:vsu-ext}
If  $\trace \defeq \triple{\eapp{f}{\vhole{\thole}}}{\emptysu}{\emptysu},\ldots,\triple{e}{\vsu}{\tsu}$
and $\trace' \defeq \trace, \step{e}{\vsu}{\tsu}{e'}{\vsu'}{\tsu'}$
  (\ie\ $\trace'$ is a single-step extension of $\trace$)
% and $\trace'$ is a single-step extension of $\trace$
and $\ptype{\trace}{f} \neq \ptype{\trace'}{f}$
% and $\resolve{\thole}{\tsu} \neq \resolve{\thole}{\tsu'}$
then $\tsu' = \tsu[\thole_1 \mapsto t_1] \ldots [\thole_n \mapsto t_n]$.
\end{lem}
\begin{proof}
  By case analysis on the evaluation rules.
  %
  $\thole$ does not change, so if the partial input types differ then
  $\tsu \neq \tsu'$.
  %
  Note that only \forcesym\ can change \tsu,
  %, and it can only do so via \unifysym
  via \unifysym, which can only extend \tsu.
  % and only the first case,
  % when we extend \vsu\ with $[\ehole \mapsto v]$,
  % does.
  %
  % \ES{fill in cases in the appendix}
\end{proof}
%
Second, we show that at each step of evaluation, the partial input type of $f$
is a refinement of the instantiation of $\vhole{\thole}$.
%
\begin{lem}
\label{lem:resolve-compat}
For all traces
$\trace \defeq \triple{\eapp{f}{\vhole{\thole}}}{\emptysu}{\emptysu},\ldots,\triple{e}{\vsu}{\tsu}$,
$\vsub{\ptype{\trace}{f}}{\resolve{\vhole{\thole}}{\vsu}}$.
\end{lem}
\begin{proof}
  By induction on $\trace$.
  %
  In the base case $\trace = \triple{\eapp{f}{\vhole{\thole}}}{\emptysu}{\emptysu}$
  and $\thole$ is trivially a refinement of $\vhole{\thole}$.
  %
  In the inductive case, consider the single-step extension of $\trace$,
  $\trace' = \trace,\triple{e'}{\vsu'}{\tsu'}$.
  %
  We show by case analysis on the evaluation rules that if
  $\vsub{\ptype{\trace}{f}}{\resolve{\vhole{\thole}}{\vsu}}$, then
  $\vsub{\ptype{\trace'}{f}}{\resolve{\vhole{\thole}}{\vsu'}}$.
  %
\end{proof}

\paragraph{Incompatible Types Are Wrong}
\emph{For all} types that are \emph{incompatible} with the
partial input type up to $\trace$, there exists a value
that will cause $f$ to get stuck in \emph{at most} $k$ steps,
where $k$ is the length of $\trace$.

\begin{lem}
\label{lem:k-stuck}
For all types $t$,
if $\steptr{\trace}{\eapp{f}{\vhole{\thole}}}{\emptysu}{\emptysu}{e}{\vsu}{\tsu}$ and
   $\tincompat{t}{\ptype{\trace}{f}}$,
   then there exists a $v$ such that $\hastype{v}{t}$ and
   $\steps{\eapp{f}{v}}{\emptysu}{\emptysu}{\stuck}{\vsu'}{\tsu'}$ in at most
   $k$ steps, where $k$ is the length of $\trace$.
\end{lem}
\begin{proof}
  We can construct $v$ from $\trace$ as follows.
  %
  Let
  $$
  \trace_i = \triple{\eapp{f}{\vhole{\thole}}}{\emptysu}{\emptysu},
             \ldots,
             \triple{e_{i-1}}{\vsu_{i-1}}{\tsu_{i-1}},
             \triple{e_{i}}{\vsu_{i}}{\tsu_{i}}
  $$
  be the shortest prefix of $\trace$ such that
  $\tincompat{\ptype{\trace_i}{f}}{t}$.
  %
  We will show that $\ptype{\trace_{i-1}}{f}$ % $\resolve{\thole}{\tsu_{i-1}}$
  must contain some other hole $\thole'$ that is
  instantiated at step $i$.
  %
  Furthermore, $\thole'$ is instantiated in such a way that
  $\tincompat{\ptype{\trace_i}{f}}{t}$.
  % $\tincompat{\resolve{\thole}{\tsu_{i}}}{t}$.
  %
  Finally, we will show that if we had instantiated $\thole'$ such that
  $\tcompat{\ptype{\trace_i}{f}}{t}$,
  % $\tcompat{\resolve{\thole}{\tsu_{i}}}{t}$,
  the current step would have gotten $\stuck$.

  % Next, we introduce a notion of type % and value
  % contexts, defined analogously to evaluation contexts.
  % %
  % $$
  % \begin{array}{lcl}
  %   T &::=& \bullet \spmid \ttree{T} \spmid \tprod{T}{t} \spmid \tprod{t}{T} \\
  %   % V &::=& \bullet \spmid \vnode{t}{V}{v}{v} \spmid \vnode{t}{v}{V}{v} \spmid \vnode{t}{v}{v}{V} \\
  % \end{array}
  % $$

  % By Lemma~\ref{lem:fixme} we know that
  % $\vcompat{\resolve{\vhole{\thole}}{\vsu}}{\resolve{\thole}{\tsu}}$.
  %
  By Lemma~\ref{lem:vsu-ext} we know that
  $\tsu_{i} = \tsu_{i-1}[\thole_1 \mapsto t_1] \ldots [\thole_n \mapsto t_n]$.
  %
  We will assume, without loss of generality, that
  $\tsu_{i} = \tsu_{i-1}[\thole' \mapsto t']$.
  %
  Since $\tsu_{i-1}$ and $\tsu_{i}$ differ only in $\thole'$ but the resolved
  types differ, we have
  $\thole' \in \ptype{\trace_{i-1}}{f}$
  and
  $\ptype{\trace_i}{f} = \ptype{\trace_{i-1}}{f}\sub{\thole'}{t'}$.
  %
  % We prove\includeTechReport{, in Lemma~\ref{lem:context-compat},}
  % that for all types $s$ and $t$, if.
  Let $s$ be a
  concrete type such that $\ptype{\trace_{i-1}}{f}\sub{\thole'}{s} = t$.
  %
  We show by case analysis on the evaluation rules that
  %
  $$\step{e_{i-1}}{\vsu_{i-1}}{\tsu_{i-1}[\thole' \mapsto s]}{\stuck}{\vsu}{\tsu}$$

  Finally, by Lemma~\ref{lem:resolve-compat} we know that
  % $\vsub{\resolve{\thole}{\tsu_{i-1}}}{\resolve{\ehole}{\vsu_{i-1}}}$,
  $\vsub{\ptype{\trace_{i-1}}{f}}{\resolve{\vhole{\thole}}{\vsu_{i-1}}}$
  and thus $\thole' \in \resolve{\vhole{\thole}}{\vsu_{i-1}}$.
  %and thus $\vsub{\intctx{\thole'}}{\resolve{\vhole{\thole}}{\vsu_{i-1}}}$.
  %
  Let
  $$
  \begin{array}{lcl}
  u &=& \gen{s}{\tsu} \\
  v &=& \resolve{\vhole{\thole}}{\vsu_{i-1}}\sub{\ehole'[\thole']}{u}\sub{\thole'}{s} \\
  \end{array}
  $$
  $\steps{\eapp{f}{v}}{\emptysu}{\emptysu}{\stuck}{\vsu}{\tsu}$ in $i$ steps.
  %and
  % $\vsub{\resolve{\thole}{\tsu_{i}}}{\resolve{\ehole}{\vsu_{i}}}$.
  % \vcompat{\resolve{\vhole{\thole}}{\vsu_{i-1}}}{\resolve{\thole}{\tsu_{i-1}}}
  % =
  % \tcompat{\ptype{i-1}{f}}{t}
  % \\
  % \vcompat{\resolve{\vhole{\thole}}{\vsu_{i}}}{\resolve{\thole}{\tsu_{i}}}
  % =
  % \tincompat{\ptype{i}{f}}{t}
  %
  % Finally, by the definition of $\typeofsym$,
  % we know that $\resolve{\vhole{\thole}}{\vsu_{i-1}} = \invctx{v}$
  % such that $\vsub{T'[\thole']}{v}$.
  %such that $\resolve{\typeof{v}}{\tsu_{i-1}} = T'[\thole']$
  %
  %\ES{need to connect \ptype{\trace{i}}{f} and \resolve{\vhole{\thole}}{\vsu_i}}
  %
  % Now, consider any value $u$ such that $\vcompat{\invctx{u}}{t}$,
  % we show by case analysis on the evaluation relation that
  % $\invctx{u}$ could not make progress at this step, and thus
  % $\steps{\eapp{f}{\invctx{u}}}{\emptysu}{\emptysu}{\stuck}{\vsu'}{\tsu'}$.
\end{proof}

% \paragraph{Incompatible Values Are Wrong}
%
% \emph{Any} value that is \emph{incompatible} with
% the partial input type upto trace $\trace$ will
% cause $f$ to get stuck in \emph{at most} $k$
% steps, where $k$ is the length of $\trace$.
% %
% \begin{lem}
% \label{lem:k-stuck}
%   For all $v$,
%   if \steptr{\trace}{\eapp{f}{\vhole{\thole}}}{\emptysu}{\emptysu}{e}{\vsu}{\tsu} and
%      \vincompat{v}{\ptype{\trace}{f}},
%   then
%      \steps{\eapp{f}{v}}{\emptysu}{\emptysu}{\stuck}{\vsu}{\tsu}
%      in at most $k$ steps, where $k$ is the length of $\trace$.
% \end{lem}
% \begin{proof}
% By induction on $k$, the length of $\trace$.
% %
% Suppose {\vincompat{v}{\ptype{\trace}{f}}}.
% %
% We show that \steps{\eapp{f}{v}}{\emptysu}{\emptysu}{\stuck}{\vsu}{\tsu}
% in at most $k$ steps.
% %
% The base case, $k = 0$ is trivial, $\trace$ is empty
% and so $\ptype{\trace}{f}$ is a hole that is compatible
% with \emph{every} value $v$.
% %
% In the inductive case, let
% $\trace' = \triple{\eapp{f}{\vhole{\thole}}}{\emptysu}{\emptysu},\ldots,\triple{e'}{\vsu'}{\tsu'}$
% be the prefix of $\trace$ of length $k-1$.
% %
% Furthermore, let $s_{\trace} = \ptype{\trace}{f}$ and $s_{\trace'} = \ptype{\trace'}{f}$.
% %
% Let us split cases on whether $v$ is compatible with $s_{\trace'}$.
% %
% \begin{description}
% \item [Case \vincompat{v}{s_{\trace'}}:]
%   The inductive hypothesis applies.

% \item [Case $\vcompat{v}{s_{\trace'}}$ but $\vincompat{v}{s_{\trace}}$:]
%   Since $\vcompat{v}{s_{\trace'}}$ but $\vincompat{v}{s_{\trace}}$ we know
%   that $s_{\trace'} \neq s_{\trace}$.
%   By Lemma~\ref{lem:force-inst} we know that we must have
%   invoked \forcesym\ at step $k$.
%   %
%   The Preservation Lemma~\ref{lem:refine-partial} implies that
%   $\tcompat{s_{\trace'}}{s_{\trace}}$, which means we must have
%   specifically narrowed $s_{\trace'}$ to a type incompatible with $v$.
%   %
%   A case analysis of the evaluation rules shows that such an
%   invocation of \forcesym\ at step $k$
%   cannot succeed, \ie\ yields \stuck.
%    \RJ{some intuition needed --- seems like key step.}
% \end{description}
% \end{proof}

\pagebreak
\begin{proof}[\textbf{Proof of Theorem~\ref{thm:soundness}}]
%
Suppose $\trace$ witnesses that $f$ gets stuck,
and let $s = \ptype{\trace}{f}$.
We show that \emph{all} types $t$ have stuck-inducing
values by splitting cases on whether $t$ is
compatible with $s$. %the partial type upto $\trace$.
%
\begin{description}
\item [Case \tcompat{s}{t}:]
  Let $\trace = \triple{\eapp{f}{\vhole{\thole}}}{\emptysu}{\emptysu},\ldots,\triple{\stuck}{\vsu}{\tsu}$.
  %
  The value $v = \resolve{\vhole{\thole}}{\vsu}$ demonstrates that
  $\eapp{f}{v}$ gets stuck.
\item [Case \tincompat{s}{t}:] By Lemma~\ref{lem:k-stuck}, we can derive
  a $v$ from $\trace$ such that $\hastype{v}{t}$ and $\eapp{f}{v}$ gets
  stuck.
% \item [Case \tincompat{s}{t}:] By Lemma~\ref{lem:k-stuck}, every $v$
%   such that \hastype{v}{t} demonstrates that $\eapp{f}{v}$ gets stuck.
%   % \ES{do we need to say anythign else?}
\qedhere
\end{description}
\end{proof}

\subsection{Search Algorithm}
\label{sec:search-algorithm}
%
So far, we have seen how a trace leading to a stuck configuration yields
a general witness demonstrating that the program is ill-typed (\ie\ goes
wrong for at least one input of every type).
In particular, we have shown how to non-deterministically find a witnesses
for a function of a \emph{single} argument.

In order to convert the semantics into a \emph{procedure} for finding
witnesses, we must address two challenges.
%
First, we must resolve the non-determinism introduced by \gensym.
%
Second, in the presence of higher-order functions and currying,
we must determine how many concrete values to generate to make
execution go wrong (as we cannot rely upon static typing to
provide this information.)

The witness generation procedure $\genWitnessN$ is formalized in
Figure~\ref{fig:algo-gen-witness}.
%
Next, we describe its input and output, and how it
addresses the above challenges to search the space of possible
executions for general type error witnesses.

\paragraph{Inputs and Outputs}
%
The problem of generating inputs is undecidable in general.
%
Our witness generation procedure takes two inputs:
%
(1) a search bound $k$ which is used to define the \emph{number} of
traces to explore\footnote{We assume, without loss of generality, that all
traces are finite.} and
%
(2) the target expression $e$ that contains the type error
(which may be a curried function of multiple arguments).
%
The witness generation procedure returns as output a list of (general)
witness expressions, each of which is of the form $e\ v_1 \ldots v_n$.
%
The \emph{empty} list is returned when no witness can be found after
exploring $k$ traces.

\paragraph{Modeling Semantics}
%
We resolve the non-determinism in the operational semantics
(\S~\ref{sec:semantics}) via the procedure
%
$$
\evalN : e \rightarrow \triple{v \cup \stuck}{\vsu}{\tsu}^{*}
$$
%
Due to the non-determinism introduced by \gensym, a call
$\evalfn{e}$ returns a \emph{list}
of possible results of the form $\triple{v \cup \stuck}{\vsu}{\tsu}$
such that $\steps{e}{\emptysu}{\emptysu}{v \cup \stuck}{\vsu}{\tsu}$.

\paragraph{Currying}
We address the issue of currying by defining a procedure \genArgs{e},
defined in Figure~\ref{fig:algo-gen-args}, that takes as input an
expression $e$ and produces a \emph{saturated} expression of the form
$\eapp{e}{\ehole_1[\thole_1] \ldots \ehole_n[\thole_n]}$ that
\emph{does not} evaluate to a lambda.
%
This is achieved with a simple loop that keeps adding holes to the
target application until evaluating the term yields a non-lambda value.
%
% The helper functions @witness@ and @close@ are used to respectively
% apply the parameters to the target and close the result under the top-level
% let-binders, prior to invoking \hbox{@eval@.}
%
% That is, @mkApps@ creates a nested sequence of applications in
% the usual left-associative style, and @mkLets@ takes a list of
% binders and a body expression, and creates a sequence of nested
% let-binders that close the body expression.

\begin{figure}[t]
% $$
% \begin{array}{lcl}
% \genArgs{e} &\defeq& \begin{cases} \genArgs{\addArg{e}},& \mbox{if } \triple{\efun{x}{e'}}{\vsu}{\tsu},\ldots = \doeval{e} \\
%  e,& \mbox{otherwise} \\
% \end{cases}
% \\
% \addArg{e} &\defeq& \eapp{e}{\vhole{\thole}}, \qquad \mbox{where } \ehole, \thole \mbox{ are fresh }
% \end{array}
% $$
$$
\begin{array}{lclr}
\genArgsN   & : & e \rightarrow e \\
\genArgs{e} & = & \mbox{\textbf{case }} \evalfn{e} \mbox{\textbf{ of}} \\
 \quad \triple{\efun{x}{e}}{\vsu}{\tsu},\ldots &\rightarrow& \genArgs{\eapp{e}{\vhole{\thole}}} & (\ehole, \thole \mbox{ are fresh}) \\
 \quad \_ &\rightarrow& e \\
\end{array}
$$
\caption{Generating a saturated application.}
\label{fig:algo-gen-args}

\end{figure}

\paragraph{Generating Witnesses}
%
Finally, Figure~\ref{fig:algo-gen-witness} summarizes the overall
implementation of our search for witnesses with the procedure
$\genWitness{k}{e}$, which takes as input a bound $k$ and the
target expression $e$, and returns a list of witness expressions
$\eapp{e}{v_1 \ldots v_n}$ that demonstrate how the input program
gets stuck.
%
The search proceeds as follows.
%
\begin{enumerate}
  \item We invoke $\genArgs{e}$ to produce a \emph{saturated}
        application $e_{sat}$.

  \item We take the first $k$ traces returned by $\evalN$
        on the target $e_{sat}$, and

  \item We extract the substitutions corresponding to the
        $\stuck$ traces, and use them to return the list
        of witnesses.
\end{enumerate}
%
We obtain the following corollary of Theorem~\ref{thm:soundness}:

\begin{cor}{\textbf{[Witness Generation]}}
\label{thm:generation}
  If $$\genWitness{k}{e} = \triple{\eapp{e}{v_1 \ldots v_n}}{\vsu}{\tsu},\ldots$$
  then for all types $t_1 \ldots t_n$ there exist values $w_1 \ldots w_n$ such that
  $\steps{\eapp{e}{w_1 \ldots w_n}}{\emptysu}{\emptysu}{\stuck}{\vsu'}{\tsu'}$.
\end{cor}
\begin{proof}
  For any function $f$ of multiple arguments, we can define $f'$ to be
  the uncurried version of $f$ that takes all of its arguments as a single
  nested pair, and then apply Theorem~\ref{thm:soundness} to $f'$.
\end{proof}

\begin{figure}[t]
\centering
$$
\begin{array}{lclr}
\genWitnessN       & : & \mathsf{Nat} \times e \rightarrow e^{*} & \\
\genWitness{n}{e}  & = & \{ \resolve{e_{sat}}{\vsu} \mid \vsu \in \Sigma \} & \\
\quad \mbox{\textbf{where}} &    & & \\
\quad \quad e_{sat} & = & \genArgs{e} & (1) \\
\quad \quad res    & = & \takefn{n}{\evalfn{e_{sat}}} & (2) \\
\quad \quad \Sigma & = & \{ \vsu\ \mid \triple{\stuck}{\vsu}{\tsu} \in res\} & (3)
\end{array}
$$
%% \begin{mcode}
%% $\genWitnessN :: (\mathsf{Nat} \times e) \rightarrow 2^{e}$
%% $\genWitness{n}{e} = \{ \resolve{\eapp{e}{vs}}{\vsu} \mid \vsu \in \Sigma \}$
  %% where
   %% $vs     = \genArgs{e}$               -- (1)
   %% $res    = \takefn{n}{\evalfn{\triple{\eapp{e}{vs}}{\emptysu}{\emptysu}}}$  -- (2)
   %% $\Sigma = \{ \vsu\ \mid \triple{\stuck}{\vsu}{\tsu} \in res\}$   -- (3)
%% \end{mcode}
\caption{Generating witnesses.}
\label{fig:algo-gen-witness}
\end{figure}

%% \begin{figure}[t]
  %% \centering
  %% \begin{mcode}
  %% -- transitive small-step reduction, returning a list of results
  %% eval :: ($e$, $\su$) -> [($v$, $\su$)]
%%
  %% -- | is a value stuck?
  %% isStuck :: $v$ -> Bool
%%
  %% mkApps  :: $e$ -> [$e$] -> $e$
  %% mkLets  :: [($x$, $e$)] -> $e$ -> $e$
  %% \end{mcode}
  %% \caption{Expression API}
  %% \label{fig:expression-api}
%% \end{figure}
%% %
%% We also define a few helper functions for manipulating expressions:
%% \begin{itemize}
%% \item @subst@ applies a substitution of holes to a value,
%% \item @mkApps@ creates a nested sequence of applications in the usual
  %% left-associative style,
%% \item @mkLets@ takes a list of binders and a body expression, and
  %% creates a sequence of nested let-binders, and
%% \item @isStuck@ tests whether a value is the \stuck term.
%% \end{itemize}

%%%  \begin{figure*}[t]
  %%%  \centering
  %%%  \begin{mcode}
  %%%  check :: [($x$, $e$)] -> Result
  %%%  check bnds =
    %%%  -- (2) search for a witness
    %%%  case find (isStuck . fst) results of
      %%%  Nothing      -> Safe
      %%%  Just (_, su) -> Unsafe (mkApps f (subst su args))
    %%%  where
      %%%  (args, results) = loop []
      %%%  f               = snd (last bnds)
      %%%  build args      = mkLets bnds (mkApps f args)
%%%
      %%%  -- (1) find the correct number of arguments
      %%%  loop :: [$v$] -> ([$v$], [($v$, $\su$)])
      %%%  loop args = case eval (build args, []) of
        %%%  ($\efun{x}{e}$, _) : _ -> loop (args `snoc` $\ehole{}$)
        %%%  results      -> (args, results)
  %%%  \end{mcode}
  %%%  \caption{A procedure for generating witnesses}
    %%%  \ES{should address case where output types of successive runs dont match}
  %%%  }
  %%%  \label{fig:search-algo}
%%%  \end{figure*}
%

% !TEX root = main.tex


\chapter{Heat and Salt fluxes}
\label{ch:heatsaltflux}
% \section{Introduction}

The ocean is moved by three main forcings - friction from the winds, body forces due to astronomical forcing i.e. the tides, and differences in buoyancy forcing.  This section discusses the buoyancy forcing.  The buoyancy of water is how dense it is, and as we saw in the previous chapter, it is determined by the temperature of the water and how much salt it has in it (and of course the pressure).

This is crucially important for the circulation of the ocean.  We already saw how estuaries have differential buoyancy forcing due to a freshwater source at the head of the estuary being more buoyant than the salty ocean water.  Buoyancy forcing by fresh water is also important on a global scale.  Perhaps even more important is the differential heating, with the ocean gaining heat near the equator and losing it near the poles.  This transport of heat is approximately 30\% of the poleward transport of heat, the rest is done by the atmosphere.  However, it also drives the overturning circulation of the global ocean.

Like an estuary, if the ocean only had heating and cooling at the surface the total volume transport of water in the overturning circulation would be weak.  However, mixing brings heat to great depths in the ocean and this creates pressure gradients that move a large slow overturning.

\section{Surface heat fluxes}

Most heat exchange in the ocean occurs at the surface, with short wave radiation passing through the atmosphere and impacting the ocean, and then the ocean losing heat to back radiation, latent heat flux, and heating and cooling from direct contact with the atmosphere.

\begin{figure}[hbt]
  \begin{center}
    \includegraphics{figs/FluxesFigs/382_2014_2430_Fig2_HTML}
    \caption{Heat fluxes over land and ocean, units of $\mathrm{W\,m^{-2}}$.  Note the net ``thermal'', or longwave back radiation is $H_{LW} = 53\ \mathrm{W\,m^{-2}}$ \citep{wildetal15}.  ``Solar absorbed'' is the same as $H_{SW}$ below, and ``evaporation'' is the same as $H_{latent}$.  Note the error bounds next to each estimate.  The estimated net imbalance is the warming of the oceans due to global warming.  }
    \label{fig:382_2014_2430_Fig2_HTML}
  \end{center}
\end{figure}

An estimate of the net heat flux in the ocean is given in \fref{fig:382_2014_2430_Fig2_HTML} and spatially in \fref{fig:SurfaceHeatFluxes}e.  Recall that heat is specified in units of Joules, where 1 Joule is the energy (or heat) required to warm a kilogram of water one degree Kelvin.  Heat fluxes are a rate of heating, and hence are in units of Watts, or Joules per second.  If we want to make a map like \fref{fig:SurfaceHeatFluxes}e, then we want to know the rate of heating per area, and we plot in units of Watts per meter squared, or $\mathrm{W\,m^{-2}}$.  The average over the surface area of the ocean is shown in \fref{fig:382_2014_2430_Fig2_HTML}.

Overall, there is a net input from shortwave radiation of about $H_{SW} = 170\ \marthrm{W\,m^{-2}}$ once sunlight penetrates the cloud cover (\fref{fig:382_2014_2430_Fig2_HTML}). There is a net longwave radiation of $H_{LW}=53\ \mathrm{W\, m^{-2}}$ just due to the temperature of the water and the temperature of the atmosphere and clouds above.  The remaining two terms are ``sensible'' heat loss, which is just due to the temperature difference diffusing across the sea surface ($H_{sensible}=16\ \mathrm{W\, m^{-2}}$), and the ``latent'' heat loss due to evaporation, which is the largest loss ($H_{latent}=100\ \mathrm{W\, m^{2}}$).

Note that the estimate in \fref{fig:382_2014_2430_Fig2_HTML} has a net warming of $0.8\ \mathrm{W\,m^{-2}}$, which is due to global warming.  We can consider the average heating for a $D=4000$-m deep ocean using this number.  The heat capacity of water is $c_p=4.2\times10^{3}J\,kg^{-1},K^{-1}$, and the density $1030\ \mathrm{kg\,m^{-3}}$, so the rate of temperature change is $dT/dt = H_{net} / (D \rho c_p) = 0.0015\ \mathrm{K/y}$, or greater than 600 years to increase by 1 degree at the current rate.  This of course does not mean climate change is not important to the ocean - much of this change is confined to the upper few hundred meters, and climate change strongly affects winds that drive much of the ocean circulation.

\subsection{Incoming shortwave radiation}

The spatial distribution of these fluxes and their net magnitudes indicate that, unsurprisingly, the net incoming shortwave radiation is strongest at the equator (\fref{fig:SurfaceHeatFluxes}a).  This radiation comes from the sun, and is modified as it passes through the atmosphere.  Of course it includes all wavelengths of radiation, but it is very much dominated by short scale radiation, so it is called "shortwave".

\begin{figure}[htb]
  \centering
  \includegraphics[width=4.5in]{FluxesFigs/SurfaceHeatFluxes}
  \caption{Net surface heat fluxes, annually averaged.  a) Net shortwave heat flux, b) net longwave flux, c) sensible heat flux, d) latent heat flux and e) net heat flux.
  Data source:  \href{https://psl.noaa.gov/data/gridded/data.ncep.reanalysis.derived.surfaceflux.html}{NCEP1 reanalysis.}  }
  \label{fig:SurfaceHeatFluxes}
\end{figure}

Estimating the radiation coming from the sun and impacting the ocean would be a simple matter depending only on the tilt of the earth, and to a (much) lesser extent whether the sun is in aphelion or perihelion.  However, the atmosphere severely modifies the net flux reaching the earth, due to ozone and clouds.  Indeed, much of the east-west spatial variability to the shortwave surface fluxes is due to clouds. For instance, the inter-tropical convergence zone can be clearly seen just north of the equator (\fref{fig:SurfaceHeatFluxes}a) as a region of reduced incoming radiation.

Given this, it is important to realize that it is very difficult to make accurate estimates of the incoming radiation over the full expanse of the ocean.  Satellites make this easier, because they give a good idea of cloud cover, but the thickness of the clouds and their humidity all matter to making an estimate.  Radiation can be measured with \href{https://en.wikipedia.org/wiki/Radiometer}{radiometers}, but these need ships, moorings, or other platforms to make direct measurements. Hence estimates of shortwave radiation are derived from empirical models.

Some idea of the magnitude of the problem can be seen by considering the net heat flux of 1.2 PW (\fref{fig:SurfaceHeatFluxes}e) integrated over the entire global ocean (1 PW is $10^{15}\ \mathrm{Watts}$). The volume of the oceans is approximately $1.335\times10^{18}\ \mathrm{m^3}$. The heat capacity of water is $4180 \mathrm{J\, kg^{-1}}$ and its density approximately $1030 \ \mathrm{kg\,m^{-3}}$.  Using these numbers, this 1.2 PW discrepancy in the annual mean would lead to a one-degree warming of the ocean over 140 years.  This would be quite large and rapid, even on the scale of anthropogenic climate change, and this net should be considered a bound on how accurate the various terms of the budget are, rather than a direct estimate of how fast the ocean is warming.

\begin{figure}[htb]
  \centering
  \includegraphics[width=4.5in]{FluxesFigs/SurfaceHeatFluxesJFM}
  \caption{Net surface heat fluxes, January to March.  a) Net shortwave heat flux, b) net longwave flux, c) sensible heat flux, d) latent heat flux and e) net heat flux.
  Data source:  \href{https://psl.noaa.gov/data/gridded/data.ncep.reanalysis.derived.surfaceflux.html}{NCEP1 reanalysis.}  }
  \label{fig:SurfaceHeatFluxesJFM}
\end{figure}

We can see the effect of the earth's tilt and changing cloud cover if we look at seasonal averages of the shortwave radiation (\fref{fig:SurfaceHeatFluxesJFM}a, \fref{fig:SurfaceHeatFluxesAMJ}a, \fref{fig:SurfaceHeatFluxesJAS}a, \fref{fig:SurfaceHeatFluxesOND}a).  Not surprisingly, during the boreal winter, the net radiation is low in the northern hemisphere, than in the boreal summer.

\begin{figure}[htb]
  \centering
  \includegraphics[width=4.5in]{FluxesFigs/SurfaceHeatFluxesAMJ}
  \caption{Net surface heat fluxes, April to June.  a) Net shortwave heat flux, b) net longwave flux, c) sensible heat flux, d) latent heat flux and e) net heat flux.
  Data source:  \href{https://psl.noaa.gov/data/gridded/data.ncep.reanalysis.derived.surfaceflux.html}{NCEP1 reanalysis.}  }
  \label{fig:SurfaceHeatFluxesAMJ}
\end{figure}

\begin{figure}[htb]
  \centering
  \includegraphics[width=4.5in]{FluxesFigs/SurfaceHeatFluxesJAS}
  \caption{Net surface heat fluxes, July to September.  a) Net shortwave heat flux, b) net longwave flux, c) sensible heat flux, d) latent heat flux and e) net heat flux.
  Data source:  \href{https://psl.noaa.gov/data/gridded/data.ncep.reanalysis.derived.surfaceflux.html}{NCEP1 reanalysis.}  }
  \label{fig:SurfaceHeatFluxesJAS}
\end{figure}

\begin{figure}[htb]
  \centering
  \includegraphics[width=4.5in]{FluxesFigs/SurfaceHeatFluxesOND}
  \caption{Net surface heat fluxes, October to December.  a) Net shortwave heat flux, b) net longwave flux, c) sensible heat flux, d) latent heat flux and e) net heat flux.
  Data source:  \href{https://psl.noaa.gov/data/gridded/data.ncep.reanalysis.derived.surfaceflux.html}{NCEP1 reanalysis.}  }
  \label{fig:SurfaceHeatFluxesOND}
\end{figure}
\clearpage
\subsection{Longwave radiation (back radiation)}

The earth and ocean emit radiation back into the atmosphere due to their temperature due to blackbody radiation.  If there were no atmosphere, then $H_{LW}$ would depend on temperature of the water to the fourth power, the Stefan-Boltzman constant, and the emissivity of the water.  If this were the case, the ocean would have very large spatial differences in $H_{LW}$ that depended the sea surface temperature.  That is not the case (\fref{fig:SurfaceHeatFluxes}b) because the atmosphere acts as a strong insulator, and back-reflects much of the emission.  Hence, the \emph{net} longwave radiation is surprisingly uniform.  This is tru spatially, as well as temporally  (\fref{fig:SurfaceHeatFluxesJFM}b, \fref{fig:SurfaceHeatFluxesAMJ}b, \fref{fig:SurfaceHeatFluxesJAS}b, \fref{fig:SurfaceHeatFluxesOND}b).  It is indeed surprising that the net back radiation is strongest in the winter when the sea surface is usually colder.

This again points to the difficulty in estimating this flux.  As with shortwave radiation it requires radiometers to measure directly, and some correlation with atmospheric forcing must be made to come up with a net value.

\subsection{Sensible heat transfer}

\begin{figure}[htb]
\includegraphics[width=3in]{FluxesFigs/TemperatureProfilesUpperOcean}
 \caption{Sketch of ocean and atmospheric temperature profiles.  During calm weather and stable conditions (warm atmosphere compared to ocean), there is only weak sensible heat transfer.  If there is turbulence in the atmosphere or in the ocean, then the transfer can be greatly enhanced.}
  \label{fig:TemperatureProfilesUpperOcean}
\end{figure}

Sensible heat transfer is the weakest term, despite the fact it is probably what most people think of as how heat is transferred, i.e. when the atmosphere is cold  the ocean loses heat and when it is warm it gains heat.  The heat exchange is usually assumed to be driven by the temperature difference between the top of the ocean and the atmosphere, i.e. $H_{Sensible} \approx k \left(T_{air} - T_{ocean}\right)$ where $k$ is a transfer co-efficient ($W\, K^{-1}$).  However, the temperature of the upper ocean and lower atmosphere of course depends on the flux. So if there is only weak mixing of heat by upper ocean turbulence, the temperature profiles will rapidly equilibrate in thin layers at the interface (\fref{fig:TemperatureProfilesUpperOcean}a).  Conversely, if there is substantial mixing, the temperature difference between the two layers can remain large because heat is mixed away from the interface.

\begin{figure}[htb]
\includegraphics[width=2in]{FluxesFigs/TemperatureProfilesConvect}
 \caption{Sketch of ocean and atmospheric temperature profiles.  During calm weather and stable conditions (warm atmosphere compared to ocean), there is only weak sensible heat transfer.  If there is turbulence in the atmosphere or in the ocean, then the transfer can be greatly enhanced.}
  \label{fig:TemperatureProfilesConvect}
\end{figure}

If the interfaces are unstable, then the ocean (and atmosphere) will tend to ``convect''.  i.e. if the atmosphere at sea level is colder than the ocean, then the cooling of the ocean will lead to cold water on top of warm water and that water will sink.  As it does so, it mixes with the warm water below, and the mixed layer will tend to deepen. Again, mechanical mixing by the wind can enhance this process.

Needless to say, parameterizing these effects to come up with maps like \fref{fig:SurfaceHeatFluxes}c, has required many years of effort and specialized instrumentation \fref{fig:fluxplatforms}.  Measured heat fluxes, as we will see below, are notoriously hard to collect, as are the forcing parameters for those heat fluxes, involving wind speed, ocean current speed, and parameterization for the strength of turbulence in the lower atmosphere and ocean.  For an overview of the state of the art, and the pressing need to improve these measurements see \citet{croninetal19}.

\begin{figure}[htb]
\includegraphics[width=4in]{FluxesFigs/fluxplatforms}
 \caption{Platforms used to measure heat fluxes in the ocean, from \citet{croninetal19}.}
  \label{fig:fluxplatforms}
\end{figure}

The sensible heat losses show a relatively straight forward patterns with the seasons, with a bit of warming of the ocean in the summer, and substantial cooling in the winter (\fref{fig:SurfaceHeatFluxesJFM}c to \fref{fig:SurfaceHeatFluxesOND}c) .  Particularly for the winters there is substantial east-west asymmetry in the ocean's basins, for instance in \fref{fig:SurfaceHeatFluxesJFM}c, there is strong losses off Japan and the Eastern US, centred at around 40 N.  This is due to dry cold winds off the continents, blowing from west to east with the Jet Stream coming into contact with warm water being advected north by the Kuroshio and Gulf Stream.

\subsection{Latent heat losses}

Latent heat losses are those due to heat used to evaporate water.  Evaporation occurs when the atmospheric humidity, $q$, is less than 100\%.  During evaporation water mass is transferred from the ocean to the atmosphere, \emph{and} the heat stored in that water is transferred from the ocean to the atmosphere.   This transfer is not 100\% efficient because some energy is used to achieve the evaporation, but it does result in a substantial transfer of heat.

The strength of this transfer depends on the humidity of the ocean directly at the air-sea interface, and hence, just like for the sensible heat transfer, it depends on the turbulence in the atmospheric ``boundary layer'' - if the layer is turbulent because there are strong winds and/or convection, then the moist near-interface air is mixed more thoroughly and the evaporation and exchange of heat is enhanced.   Note that indirectly the exchange depends on the temperature difference between the ocean and the air as cool air has relatively little moisture, and warm water is more readily evaporated.

Again, this leads to seasonal changes in the heat flux that mean more evaporative heat flux occurs in the winter than the summer, (\fref{fig:SurfaceHeatFluxesJFM}d to \fref{fig:SurfaceHeatFluxesOND}d).  Further there are clear east-west asymmetries across the basins with the western boundaries at mid-latitude being conspicuous for their evaporative heat loss.  This is because cold dry continental air blows west-east across the warm western boundary currents at these latitudes, leading to substantial evaporation.

\subsection{Integrated Surface Heat Fluxes}

The maps in \fref{fig:SurfaceHeatFluxes} to \fref{fig:SurfaceHeatFluxesOND} can be integrated to get the net heat flux over the year (\fref{fig:IntegratedHeatFlux}).  Remember that there is a geometric effect of the area of the earth per degree of latitude at the pole goes to zero, where as it is very large at the equator.  Keeping this in mind, the integral around the globe for each degree shows that there is a net heat input at the equator, and losses to the north and south.  Interestingly, in the southern hemisphere, there is a net heat \emph{input} into the ocean between the subpolar band of 40 and 60 S.   The general lack of land mass at these latitudes means that the air has largely equilibrated with the ocean in terms of both temperature and humidity and hence the sensible and latent heat fluxes are negligible.  Note the same thing happens in the North Pacific but is masked in the net by the north Atlantic and the fluxes in the Kuroshio (\fref{fig:SurfaceHeatFluxes}e)

\begin{figure}[htb]
\includegraphics[width=4in]{FluxesFigs/IntegratedHeatFlux}
 \caption{a) Globally integrated annual-mean surface heat flux per one degree of latitude.  The sum of the heat flux is shown as the ``residual'', and is about 4\% of the net incoming radiation. b) the inferred heat transport carried by the ocean, computed by integrating the curve in (a).  Negative heatflux means heat is flowing from north to south.  Theoretically, this should come to zero, so this curve requires correction (orange curve is a crude correction).}
  \label{fig:IntegratedHeatFlux}
\end{figure}

As noted above, the 2.3 PW of residual heating is likely a result of error in the estimate rather than a robust idea of how fast the ocean is heating.  2.3 PW is 4\% of the total incoming flux, and the estimates likely have more than 4\% error in total, so the net flux is not significantly different from zero.

On moderate time scales it is often helpful to think of the ocean as being in steady-state, so in this case it is helpful to think of the ocean as not changing temperature.  On the other hand, if there is a net flux of heat into the ocean at some latitude, and a net flux out at another latitude, either the ocean where there is a net heatflux in is getting warmer and warmer, or it is transporting heat to the region where it loses heat, as sketched in \fref{fig:HeatFluxBoxes}.

\begin{marginfigure}
\includegraphics[width=2in]{FluxesFigs/HeatFluxBoxes}
 \caption{Schematic two-box representation where the right-hand box is being heated, the left-hand box cooled, and a heat flux in the interior of the box is assumed to be from warm to cold.}
  \label{fig:HeatFluxBoxes}
\end{marginfigure}

Extending this idea to many boxes is straight forward, but we have to remember that the boxes have a heat source both at the surface, and an interior flux on both sides of the box.   (\fref{fig:HeatFluxSchematic}). If we assume steady state and that these heat fluxes balance, then we have $H_{in}^{(N)} = H_{in}^{(N-1)} + H_S^{(N)}$ where the bracketed superscripts refer to the cell number. Surface fluxes are defined as positive into the ocean, and interior fluxes are defined as positive if they are to the north.  We can also write this as $\Delta H_{in}^{(N)} = H_S^{(N)}$ and we can see that if we have a positive heat flux into the ocean, then the northward heat flux must grow.  Because this relation starts at a pole, where the interior heat flux is zero, the interior heatflux at any given latitude is the sum of the surface fluxes to the south of that latitude:
\begin{equation}
    H_{in}^{(N)} = \sum_{i=0}^N H_{S}^{(i)}
\end{equation}
This gives us the curve in \fref{fig:IntegratedHeatFlux}b, which shows northward heatflux in the northern hemisphere and southwards in the southern hemisphere.  Again, there is a residual heat flux, so this procedure does not yield a completely sensible curve with large southwards heatflux at the southernmost ocean cell, so a correction is applied that removes the mean flux.

\begin{marginfigure}
\includegraphics[width=2in]{FluxesFigs/HeatFluxSchematic}
 \caption{Schematic two-box representation where the right-hand box is being heated, the left-hand box cooled, and a heat flux in the interior of the box is assumed to be from warm to cold.}
  \label{fig:HeatFluxSchematic}
\end{marginfigure}

Direct estimates of the interior fluxes have also been made from ocean-based measurements, and a number of these are compared in \fref{fig:GanachaudWunsch03Fig5}.  The level of variability in these estimates indicates the difficulties in making these types of estimates, even if the patterns make the sense.

\begin{figure}
\begin{center}
\includegraphics[width=2.7in]{FluxesFigs/GanachaudWunsch03Fig5}
 \caption{Estimates of heat transport by the world's oceans \citep{ganachaudwunsch03}.}
  \label{fig:GanachaudWunsch03Fig5}
\end{center}
\end{figure}

If we look at these fluxes by ocean basin, then there are substantial differences between the Atlantic ocean and the Pacific/Indian Oceans in terms of the structure of the heat flux.  In the Atlantic ocean the heat flux is everywhere north.  This is because the heat flux at the equator is relatively weak, and there is substantial heat flux in the south Atlantic.

\begin{figure}
\begin{center}
\includegraphics[width=2.7in]{FluxesFigs/GanachaudWunsch03Fig3}
\includegraphics[width=2.7in]{FluxesFigs/GanachaudWunsch03Fig4}
 \caption{Estimates of heat transport in the Atlantic (upper panel) and Pacific and Indian oceans (lower panel)\citep{ganachaudwunsch03}.}
  \label{fig:GanachaudWunsch03Fig3_4}
\end{center}
\end{figure}

We have said nothing about what drives these heat fluxes, however, the basic idea is that warm water is less dense, and hence more buoyant, whereas cold water is less buoyant.  Just like in an estuary, this creates north-south pressure gradients that drive the warm water northwards.

\marginpar{Make a demo about this!}

\section{Surface freshwater fluxes}

The ocean has a large reservoir of salinity compared to the rate of inputs (from rivers) and outputs (from forming \Wikiref{evaporite}s), to the point that the mass of salt in the ocean can be regarded as a constant on the timescale of decades to centuries.  Given the volume of the ocean is approximately $1.335\times10^{18}\ \mathrm{m^3}$, and a typical density is $1030\ \mathrm{kg\,m^{-3}}$, and salinity is $S\approx35 \ \mathrm{ppt}$, the mass of salt in the oceans is approximately $4.5\times10^{19} \ \mathrm{kg}$.  Conversely, rivers deliver on the order of  $3\times10^{12} \ \mathrm{kg}$ of salt per year, so, in order to increase ocean salinity by one part per thousand, it would take more than 450,000 years of salt input.  Given that much of this input is balanced by output to forming evaporites, we tend to think of the salt balance as not changing over sub-geological timescales.

Of course this doesn't mean that salinity is constant in the ocean!  Salinity is almost zero in rivers, and is zero in rainfall and at locations where these meet the ocean the salinity drops.  Conversely if there is evaporation and ice formation, then salinity is left behind and the ocean is saltier in those locations.  When we think about the large-scale budgets then, we often think of them as freshwater budgets, with the understanding that these are also salinity budgets.

\subsection{Evaporation and precipitation patterns}

As noted above, evaporation is complicated to estimate because it depends on the temperature difference of the ocean ant atmosphere, the humidity of the atmosphere, and all of those parameters depend on the turbulence near the boundary between the two fluids. Many of the spatial patterns of evaporation are the same as those for latent heat loss (\fref{fig:E-P}a), with evaporation largest where cold dry air passes over warm ocean currents.

\begin{figure}
\begin{center}
\includegraphics[width=4in]{FluxesFigs/E-P}
 \caption{Annual average evaporation and precipitation (from \href{https://www.ecmwf.int/en/forecasts/datasets/reanalysis-datasets/cera-20c}{ECMWF-CERA-20c} re-analysis).  }
  \label{fig:E-P}
\end{center}
\end{figure}

Freshwater re-enters the ocean via precipitation and rivers.  The precipitation patterns are largely driven by the \Wikiref{Hadley cell} circulation of the atmosphere, with large upward motion at the equator driving strong rainfall there (\fref{fig:E-P}).  The subpolar latitudes see more rainfall as well because there is also rising air in the midlatitude cell, centred at about 60 degrees.  Dry air predominates at the subtropical latitudes where this air tends to be subsiding.

\begin{marginfigure}
\begin{center}
\includegraphics[width=2.5in]{FluxesFigs/Schmitt18Fig5}
 \caption{Net annual fresh-water transport \citep[from][Fig.\ 5]{Schmitt18}.  The ocean curve is estimates by integrating the net surface flux curve integrated from \fref{fig:E-P}, as described above for heat.  The atmospheric curve is estimated from the ERA moisture transport simulations.  }
  \label{fig:Schmitt18Fig5}
\end{center}
\end{marginfigure}

The net pattern of evaporation minus precipitation generally says that moisture is removed from the oceans in a band from approximately 5 degrees to 35 degrees, and in the net, precipitates near the equator and north of 40 degrees.  As with heat, this necessitates a transport of freshwater from places of excess precipitation to places of excess evaporation if the ocean is to stay in approximate steady state (\fref{fig:Schmitt18Fig5}).

\section{Fluxes and transports in the ocean}
\label{sec:fluxestransports}

We saw in the previous section that heat and freshwater can enter and leave the ocean at the surface.  We also \emph{inferred} that it must be transported from sources to sinks at the surface if the ocean is believed to be in a quasi-steady state.  We call this an \emph{inverse estimate} because we infer the inner workings of the ocean from its external forcing.  In this chapter we start grappling with \emph{how} that transport takes place, and how we might quantify and measure it.  There are actually \emph{many} processes involved in the transport of heat from the equator poleward, and trying to understand them all is one of  the main challenges of oceanography.  Given that, this chapter only gives some tools to start understanding fluxes and transports, and as such uses some simple examples.

Note that temperature, (or heat), for instance, can change at a location away from the surface in two possible ways.  Currents can bring water with a different temperature from somewhere else; we call this an \emph{advective flux}.    Conversely the water can exchange heat via mixing, both at the large scale (stirring) and ultimately at the small scale (molecular diffusion).  We call this a \emph{diffusive flux}.

\subsection{Advective flux}

\begin{marginfigure}
\begin{center}
\includegraphics[width=2.5in]{FluxesFigs/ConcentrationRiver}
\includegraphics[width=2.5in]{FluxesFigs/ConcentrationRiverTime}
 \caption{Sketch of a river with a gradient of a contaminant.}
  \label{fig:ConcentrationRiver}
\end{center}
\end{marginfigure}

Advective fluxes are changes carried by gradients in the fluid.  So, imagine a river with a contaminant $C$ in it, measured with units of $\mathrm{g\,m^{-3}}$ (\fref{fig:ConcentrationRiver}), and the river is seen to have higher concentrations of contaminant upstream than downstream.    The river flows to the right with a speed $u\ \mathrm{[m\,s^{-1}]}$.   If we stand at some point on the river bank, say $x = x_0$, and measure the concentration over time, clearly, the concentration will be seen to be increasing.  Hopefully it is also clear that it is increasing at a rate that depends on how strong the gradient is and how fast the flow is.  If there were no gradient, then the concentration at $x_0$ would be constant, whereas if the gradient is strong the change will be faster.

We can easily quantify this if we know the gradient of $C$ with $x$, $dC/dx$ and the speed of the river $u$.  At $x=x_0$, the concentration at a time $t=\delta t$ is
$C(t=\delta t) \approx C(t=0) - \delta x \frac{dC}{dx}$, where the approximation is very good so long as $dC/dx$ doesn't change much over the distance $\delta x$.  Here $\delta x$ is just the distance the river has travelled in time $\delta t$, and is therefore given by $\delta x = u \delta t$.  So we can predict the new concentration based on things we can measure as  $C(t=\delta t) \approx C(t=0) - u\ \delta t \frac{dC}{dx}$.  We allow $\delta t$ to become very small then we can write this as a differential:
\begin{equation}
\frac{dC}{dt} = -u \frac{dC}{dx} + \mathrm{other\ directions} + \mathrm{diffusive\ fluxes}
\end{equation}
Note the minus sign, which accords with the sketch in \fref{fig:ConcentrationRiver} where $dC/dx < 0$ because the concentration decreases in the positive x direction.

The term $-u\frac{dC}{dx}$ is the \emph{advective flux} in the x-direction.  Note that it has units of $\mathrm{g m^{-2}s^{-1}}$ and can be thought of as mass per time per area of the flow.   We can think of a river as only having one spatial dimension, but if there is more than one direction that can have gradients, as is often the case in the ocean, we may also be interested in the flux in the y-direction ($-v\frac{dC}{dy}$) and the vertical ($-w\frac{dC}{dz}$).

\subsection{Advective transport}

Note the units of an advective \emph{flux} are concentration per time (in this case $\mathrm{g m^{-3} s^{-1}}$).  However, now imagine we have a lake that the river is flowing into, and we want to know how quickly the total amount of contaminant in the lake is increasing or decreasing.  In this case we want the amount of contaminant per time that the river is carrying into the lake (i.e.\ in units of $\mathrm{g\, s^{-1}}$). In this case, we need to know not only how fast the river is moving, but also what its cross-sectional area is to get an \emph{advective transport} via:
\begin{equation}
    F_C = -u\ \delta A\ C \ \ \ \mathrm{[g\,s^{-1}]}
\end{equation}
The term $Q = u\ \delta A$ is a volume transport, and usually has units of $\mathrm{ m^3\ s^{-1}}$.

\subsection{Conservation of mass (and heat)}

Transports get used to create mass balance equations.  Since we know mass must be conserved (plus or minus releasing nuclear energy), the rate of change of the mass of a contaminant in a volume of water must be equal to the net transports of the contaminant into the volume.  A simple situation is to imagine a lake with two rivers, one feeding the lake and one draining it.  Suppose we measure the cross sectional area of the river, and the velocity to get $Q_{in}$, and that we measure the concentration of a contaminant in the river to be $C_{in}$.  Suppose the river that drains the lake does so at the same rate ($Q_{out} = Q_{in}$) and the concentration is $C_{out}$.  Then the rate of change of mass in the lake is simply:
\begin{equation}
    \frac{d M_C}{dt} = Q_{in} C_{in} - Q_{out}C_{out}
\end{equation}
where again, the rate of change is in $g/s$.  Sometimes we want to express this as a rate of change of the average concentration in the lake $\overline{C}_{lake}$ (where the overline emphasizes a volume average), in which case we would write as
\begin{equation}
    \frac{d}{dt}\left(\overline{C}_{lake} V\right) = Q_{in} C_{in} - Q_{out}C_{out},
\end{equation}
where $V$ is the volume of the lake. If indeed $Q_{in} = Q_{out}$ then $V$ is a constant and
\begin{equation}
    V\frac{d}{dt} \overline{C}_{lake}= Q_{in} \left(C_{in} - C_{out}\right).
\end{equation}

The conservation of heat is very similar to the conservation of mass of a contaminant, but if the temperature range is not too large we can assume the heat capacity of water, $c_p$, is constant and think of this as the conservation of temperature:
\begin{equation}
    \frac{d}{dt}\left(\rho c_p \overline{T}_{lake} V\right) = Q_{in} T_{in}\rho_{in} c_p - Q_{out}T_{out} \rho_{out} c_p
\end{equation}
or if we assume density doesn't change very much with temperature we get an equation for temperature:
\begin{equation}
    \frac{d}{dt}\left(\overline{T}_{lake} V\right) \approx Q_{in} T_{in} - Q_{out}T_{out}
\end{equation}
which we lazily call the "heat" equation.

The conservation of salt is just the same as any other contaminant, but again, we just have to be careful of units.  Salinity is parts per thousand, so to get weight per volume, we need to multiply by the density of the water to get a mass equation:
\begin{equation}
    \frac{d}{dt}\left(\rho \overline{S}_{lake} V\right) \approx \rho_{in} Q_{in} S_{in} - \rho_{out}Q_{out}S_{out}
\end{equation}
but again we just drop the density to get:
\begin{equation}
    \frac{d}{dt}\left(\overline{S}_{lake} V\right) \approx Q_{in} S_{in} - Q_{out}S_{out}
\end{equation}

\begin{marginfigure}
    \includegraphics[width=2.5in]{FluxesFigs/SchematicLake}
    \caption{Schematic of lake with 5 possible inlets.  If $Q_i<0$ that means the flow is out of the lake.   }
\end{marginfigure}

The general form of all these equations is written as the rate of change of concentration in a fixed volume is equal to net sum of the inputs and outputs, so generally:
\begin{equation}
    \label{eq:sumMassCon}
    \frac{d}{dt}\left(C V\right) = \sum_{i=1}^N Q_{i} C_{i}
\end{equation}
where the sign of $Q_i$ is chosen to be positive into the volume, and negative if the flow is out of the volume.

\begin{derivbox}[label={box:volumeintegral}]{Vector form of \fref{eq:sumMassCon}}
    If you have had vector calculus, then \fref{eq:sumMassCon} is equivalent to
    \begin{equation}
        \frac{d}{dt}\int_V C \mathrm{d}V = - \oint_A C \mathbf{u}\cdot \mathrm{d}\mathbf{A},
    \end{equation}
    where $\mathrm{d}\mathbf{A}$ is defined as positive pointing out of the volume.  The volume integral is the same as the finite sum as we make more and more finite sums.
\end{derivbox}

\begin{marginfigure}
    \includegraphics[width=2.5in]{FluxesFigs/SchematicVolumeIntegral}
    \caption{Schematic of volume integral.  The total integral is the sum of all ``open'' faces of the area that bounds the lake (i.e. you can only have fluxes out the open sides of the lake).}
\end{marginfigure}

\subsection{Conservation of mass and volume}

This application of the conservation of mass can be applied to the mass of water as well, but here we usually make a crucial approximation called that the fluid obeys \Wikiref{Incompressible flow}, and that variations of density are small enough to ignore in the mass equation.  This may seem counter intuitive, but it arises because the density of water does not vary much through the world's oceans (\fref{chap:EquationOfState}).

For the mass of water, the conservation equation for our two-river lake is
\begin{equation}
    \frac{dM}{dt} = Q_{in}\rho_{in} - Q_{out}\rho_{out}  \ \ \ \ \ \mathrm{\left[kg\ s^{-1}\right]}
\end{equation}
where $\rho_{in}$ and $\rho_{out}$ are the inflowing and outflowing density of the water in the lake.  Suppose also that $\overline{\rho}_{lake}$ is the average density in the lake, then we can write
\begin{equation}
    \frac{d}{dt}\left(\overline{\rho}_{lake} V \right) = Q_{in}\rho_{in} - Q_{out}\rho_{out}  \ \ \ \ \ \mathrm{\left[kg\ s^{-1}\right]}
    \label{eq:Mass}
\end{equation}
At a fundamental level, the incompressible flow approximation just says that $\overline{\rho}_{lake} \approx \rho_{in} \approx \rho_{out}$ and we can write
\begin{equation}
    \frac{d}{dt} V  \approx Q_{in} - Q_{out}  \ \ \ \ \ \mathrm{\left[m^{3}\ s^{-1}\right]}
    \label{eq:Volume}
\end{equation}
A little more justification is given in \fref{box:volume}, but assuming incompressibility of the fluid makes the mathematics significantly easier.  Of course one can always be more precise if needed.

\begin{derivbox}[label={box:volume}]{More justification for dropping $\rho$}

    To see why its a good approximation to drop the density from the mass equation and just conserve volume, lets say that $\rho_{in} = \overline{\rho} + \rho'_{1}$ and $\rho_{out} = \overline{\rho} + \rho'_{2}$.
    More formally, we can expand \fref{eq:Mass} using the chain rule:

    \begin{equation}
        V\frac{d}{dt}\left(\overline{\rho} \right) + \overline{\rho} \frac{d}{dt}\left(V\right) = \overline{\rho}Q_{in} - \overline{\rho}Q_{out} + \rho'_{1} Q_{in} - \rho'_{2} Q_{out}
    \end{equation}
    or, dividing by $\overline{\rho}$:
    \begin{equation}
        V\frac{1}{\overline{\rho}}\frac{d}{dt}\left(\overline{\rho} \right) + \frac{d}{dt}\left(V\right) = Q_{in} - Q_{out} + \frac{\rho'_{1}}{\overline{\rho}} Q_{in} - \frac{\rho'_{2}}{\overline{\rho}} Q_{out}
    \end{equation}
    A very large density variation in the ocean would be 1\%, so all the terms multiplied by $\frac{1}{\rho}$ are on order of 1\% of the terms that have not been multiplied by this factor.  Given that 1\% would be a very good estimate of any of the other terms, in a volume budget we usually ignore the small error and use \fref{eq:Volume}.

    As an example, suppose $Q_{in} = 1\times10^{4}\ \mathrm{m^3\,s^{-1}}$ and $\rho_{in}=1000\ \mathrm{kg\,m^{-3}}$, and  $Q_{out} = 1.1\times10^{4}$ and $\rho_{out} = 1005$, and $\overline{\rho} = 1002.5$, and $V=10^{10}\ \mathrm{m^3}$.  We see that the terms are
    \begin{equation}
    \frac{1}{\overline{\rho}}\frac{dM}{dt} = 10^{3} + 2.5 \ \ \mathrm{m^3\,s^{-1}}
    \end{equation}
and that the second term is far smaller than the first.

Getting at the left-hand term is actually a bit of work and requires an equation of state (\fref{chap:EquationofState}).  The density in the lake changes because of temperature or salinity changes in the body of water from the river.  This will give us $V\frac{1}{\rho}\frac{d}{dt}\left(\overline{\rho}\right)$.  To make life simple, imagine a linear equation of state that depends only on temperature: $\rho = 1000 (1 + \alpha (T - T_0))$ where $\alpha = 2\times10^{-4}\ \mathrm{\left[^oC^{-1}\right]}$ is a thermal expansion co-efficient.
\end{derivbox}

\section{Diffusive Fluxes}

The other way that the concentration of a contaminant can change in a fluid is for the concentration to diffuse (or heat, or momentum).  The diffusion of a contaminant is at the molecular level due to the random motion of the particles in solution (\fref{fig:SchematicDiffusion}) which acts to homogenize the concentration of the contaminant.  This means that the flux of contaminant ($\mathrm{g m^{-2}s^{-1}}$) is from high concentration to low concentration.    Note that diffusive fluxes have exactly the same units as advective fluxes, and have the same effect on the concentration of the contaminant.

\begin{figure}[hbt]
  \begin{center}
    \includegraphics[width=4in]{FluxesFigs/SchematicDiffusion}
    \caption{Schematic of diffusion of a contaminant.  Random motions in the fluid act to homogenize the concentration, with a flux from high concentration to low concentration. }
    \label{fig:SchematicDiffusion}
  \end{center}
\end{figure}

Most contaminants in fluids are assumed to obey \emph{Fick's Law} where the diffusive flux is linearly proportional to the gradient in concentration.  i.e. if there is a sharp change in the concentration, the flux is stronger.  And of course the flux is always from high concentration to low concentration, so we need a minus sign:
\begin{equation}
    \label{eq:Ficks}
    F = -K_C \frac{dC}{dz}
\end{equation}
where $K_C$ is the \emph{diffusion co-efficient}, and has units of $\mathrm{m^2\,s^{-1}}$. The sign of this flux indicates whether the flux is in the positive z-direction, or the negative.  So in \fref{fig:SchematicDiffusion}b, the $dC/dz > 0$ so the net diffusive flux of $C$ is downwards.

The Diffusion co-efficient $K$ varies by the contaminant, or if temperature or momentum are being considered.  For salinity $K\sim 10^{-9} \ \mathrm{m^2\,s^{-1}}$, where the exact co-efficient depends on the salinity and temperature of the water.  For temperature, it is two orders of magnitude larger $K\sim 10^{-7}\ \mathrm{m^2\,s^{-1}}$, because the transfer of heat does not require molecules to trade position, but rather for vibrational energy (heat) to be transferred, which is a more efficient process.

Of course, if we have a two- or three-dimensional distribution of the contaminant, then we need to consider the gradients in each direction.  i.e. going back to the example of the river, the diffusive flux would be in the same direction as flow, since the high concentration is coming from upstream, so in this case the flux is $-K dC/dx$.

\section{Net flux: advection plus diffusion}

So, let us go back to considering how to determine how quickly the concentration changes at a given point in a fluid (\fref{fig:BothFluxesSchem}) by looking at a box in our river.  There is a flux from up stream ($F_L$) and one going downstream ($F_R$), so the net flux is $F_L - F_R$.  The rate of change is then given by
\begin{equation}
    \delta x\, W\, H  \frac{d C}{d t} = \left(F_L - F_R\right) W H
\end{equation}, where $W$ is the width of the channel, and $H$ the depth, and
 allowing $\delta x \to 0$
\begin{equation}
 \frac{d C}{d t} = -\frac{d F}{d x}
\end{equation}
So, if $F_L>F_R$ the concentration will increase in the volume.

\begin{marginfigure}
  \begin{center}
    \includegraphics[width=2.5in]{FluxesFigs/BothFluxesSchem}
    \caption{Schematic of net flux into a small volume.}
    \label{fig:BothFluxesSchem}
  \end{center}
\end{marginfigure}

At any given point, the flux in the x-direction is given by the advective plus the diffusive flux $F = uC - K \frac{dC}{dx}$, and the full conservation of mass is given by:
\begin{equation}
    \label{massboth}
    \frac{dC}{dt} = -u \frac{dC}{dx} + K \frac{d^2C}{dx^2} + \mathrm{other\ directions}
\end{equation}
Again, as discussed above, we can also think of this by considering the sum of fluxes across a number of inlets and outlets, but now with both advective and diffusive fluxes:
\begin{equation}
    \label{eq:sumMassConBoth}
    \frac{d}{dt}\left(C V\right) = \sum_{i=1}^N F_i = \sum_{i=1}^N \left( Q_i C_i - A_i K \frac{dC}{dn}\right)
\end{equation}
where $A_i$ is the area element the flux is passing through, and $dC/dn$ is a gradient, defined as positive into the volume.

\begin{marginfigure}
    \includegraphics[width=2.5in]{FluxesFigs/SchematicLakeBoth}
    \caption{Information needed for mass budget across a surface leading out of a lake.}
\end{marginfigure}

Finally given these two types of fluxes, its possible for a gradient of a property to exist in the ocean and a mean flow, if the mean flow is balanced by diffusion.  Imagine if the river in \fref{fig:BothFluxesSchem} were flowing from right to left, then the advective flux would be to the left and the diffusive flux would be to the right, and it would be possible for the two fluxes to be in balance, and the concentration of contaminant to not change.

\begin{derivbox}[label={box:volumeintegralboth}]{Vector form of \fref{eq:sumMassConBoth}}
Again, if we know a little vector calculus, we can rewrite \fref{eq:sumMassConBoth} as
   \begin{equation}
        \frac{d}{dt}\int_V C \mathrm{d}V = - \oint_A \left(C \mathbf{u} - K\nabla C\right)\cdot \mathrm{d}\mathbf{A},
    \end{equation}
where $C\mathbf{u}$ is the advective flux, and $-K\nabla C$ is the diffusive flux, both of which are vector quantities.
\end{derivbox}

\subsection{Turbulent diffusivities}

\marginnote{\textsc{Time scale of diffusion}}

Molecular diffusion can be quite slow.  The diffusion equation ($\frac{dC}{dt} = -K \frac{d^2 C}{dx^2}$) can be solved with exponential functions (see \fref{box:exponentials}) that represent how quickly the solution relaxes to its steady state.  E.g. in \fref{fig:SketchDiffusionTime}, the temperature is fixed at two ends of an iron bar, and the initial temperature is cold, then heat will diffuse from the warm side to the cold side until a steady state is reached.  In this case, the steady state is a linear temperature gradient so that $d^2 T/dx^2 = 0$.  How quickly this will happen is given by the exponential approach to the steady state.

\begin{figure}[hbt]
  \begin{center}
    \includegraphics[width=3in]{FluxesFigs/SketchDiffusionTime}
    \caption{Example diffusion where temperature is fixed on the left and right side of the domain and the temperature all starts out cold.  Imagine an iron bar, with one end in a fire and the other end in a pool of water.  Heat diffuses in the bar from hot to  cold.  The steady state is a straight line between the two temperatures.}
    \label{fig:SketchDiffusionTime}
  \end{center}
\end{figure}

We can estimate this exponential timescale, quite simply as $\mathcal{T} = L^2/K$, where $L$ is the spatial scale.  If we assume a 100-m lateral scale, and a molecular thermal diffusivity of $K=10^{-7}\ \mathrm{m^2\,s^{-1}}$, we would get a time scale of $10^{11}$ seconds, or more than 3000 years.  If we went all the way to the bottom of the 4000-m deep ocean, the time scale would be greater than 5 million years.  All these numbers are 100 times larger for salt.

\begin{derivbox}[label={box:exponentials}]{Solutions to diffusion equation}
The diffusion equation $\frac{dC}{dt} = -K \frac{d^2 C}{dx^2}$ has solutions of exponential form:
\begin{equation}
    C = A e^{-K t/x^2} + B e^{K t / x^2} + C(x,t)
\end{equation}
where the values of $A$, $B$, and the function $C(x,t)$ depend on the initial conditions and boundary conditions.
\end{derivbox}

\subsection{Turbulent diffusion}

Clearly, if molecular diffusivity were the only way that diffusion happened, we would just ignore these fluxes.  But we saw in \fref{chap:Estuaries} that mixing was an essential ingredient to the circulation.  What happens in the real ocean is that the flow is often turbulent, and turbulence stirs the fluid, and greatly enhances the efficiency of the molecular diffusion.  This is easy to see when you add milk to coffee or tea - if you do not stir the coffee, then the milk will stay in a blob.  Net diffusion only happens where there are gradients in the concentration (\fref{fig:SketchDiffTurb}). Stirring the fluid greatly increase the length of the fluid over which there is a gradient, and often sharpens the gradients. This leads to much more mixing than if the fluid is not stirred.

\begin{figure}[hbt]
  \begin{center}
    \includegraphics[width=3.5in]{./FluxesFigs/SketchDiffTurb}
    \caption{2-D sketch of the difference between diffusion and stirring and diffusion. Of course this happens in three dimensions in the ocean. }
    \label{fig:SketchDiffTurb}
  \end{center}
\end{figure}

Stirring in the ocean is driven by a number of instability processes.  \emph{Shear instability} is one such process (\fref{fig:Smythetal05}) where the flow goes unstable because it has different velocities between layers.  In the simulation in \fref{fig:Smythetal05}, the instability grows and accomplishes significant stirring before it becomes fully three-dimensional turbulence which does more stirring.  The result is net mixing and thickened interface between the two layers.  A natural example from Admiralty inlet shows that this type of instability is often found in nature (\fref{fig:SeimGreggPlate2Trim}).

\begin{figure}[hbt]
  \begin{center}
    \includegraphics{./FluxesFigs/SmythEtAl05}
    \caption{Numerical simulation of mixing in the ocean.  Here a flow in the upper layer from left to right causes an instability on the temperature interface in the middle.  The flow breaks down into turbulence, and the turbulence stirs the fluid driving greatly enhanced mixing. \citep{smythetal05}}
    \label{fig:Smythetal05}
  \end{center}
\end{figure}

\begin{figure}[hbt]
  \begin{center}
    \includegraphics[width=4.5in]{FluxesFigs/SeimGreggPlate2Trim}
    \caption{Shear instability in the ocean measured at Admiralty Inlet in Puget Sound \citep{seimgregg94}. }
    \label{fig:SeimGreggPlate2Trim}
  \end{center}
\end{figure}

A second common instability is \emph{convective instability}.  This happens when dense fluid is on top of light fluid, such as happens when you boil water on the stove.  A more oceanographic experiment is shown in \fref{fig:ConvectionANU} where the water is warmed from below.  As it rises, there is clear instabilities, however note that these instabilities themselves are actually shear instabilities when looked at closely.

\begin{figure}[hbt]
  \begin{center}
    \includegraphics[width=4.5in]{FluxesFigs/ConvectionANU}
    \caption{Horizontal convection experiment where the left side of a tank is heated at the bottom, and the right hand side (not shown) is cooled.  Water rises, primarily along the left hand wall.  \url{http://rses.anu.edu.au/gfd/AR2005/index.php?p=ar05res}}
    \label{fig:ConvectionANU}
  \end{center}
\end{figure}

The effect of turbulence is to greatly increase the rate at which properties are fluxed across interfaces.  However the flux still tends to be from regions of high concentration to regions of low concentration.  Hence we will often represent these fluxes by a linear flux law.  For instance, in the x-direction: $F_x = -K \frac{dC}{dx}$ where now $K$ is a \emph{turbulent diffusivity} rather than a molecular one.  When estimated, turbulent diffusivities are \emph{much} larger than molecular diffusivities.  Estimates in the open ocean range from molecular levels to $K\sim 10^{-1}\ \mathrm{m^2s^{-1}}$ and levels can reach even higher values in coastal waters where there are strong flows.  Estimating turbulence requires sophisticated instrumentation and often simplifying models, and remains a great challenge today to oceanographers.  Often the approach to estimating turbulence is to relate the turbulence to external parameters that can be measured or simulated.  This has lead to many schemes to estimate turbulence in models and ocean calculations.

The consequence of turbulent mixing is that it makes the importance of mixing much more apparent.  If the timescale is $10^{11}\ \mathrm{s}$ for molecular diffusion at $K=10^{-7}\ \mathrm{m^2\,s^{-1}}$, then it is $10^{7}\ \mathrm{s}$ for $K=10^{-3}\ \mathrm{m^2\,s^{-1}}$, or 100 days.  That starts to matter for long-lasting flows.

\section{Exercises}

\paragraph{Heat budget of the surface mixed layer}

\includegraphics[width=2.5in]{figs/FluxesFigs/HotsT}

\begin{itemize}
    \item What is the specific heat capacity of water?
    \item Sketch what the temperature profile below would look like if it lost enough heat so that it was a uniform temperature to 100 m?
\item How much heat would have to be lost from the surface of the ocean for this to happen? Estimate from the temperature profile and show how you calculated this.
\item What is the average heat flux this heat loss would represent if it occurred over 6 months, in $W/m^2$. Based on the plots in the reading is this a reasonable net surface heat flux?
\end{itemize}

\paragraph{Interpret the evolution of the surface mixed layer}

Based on the following plot, try and answer the following questions:

\includegraphics[width=3in]{figs/FluxesFigs/MixedLayerPapa}

From Emery et. al. 2007

\begin{itemize}
  \item Why does the mixed layer deepen in the winter?

  \item Why does it shoal in the spring and summer?

  \item Why do you think there is often a spring bloom at Station Papa? Why does it stop?
\end{itemize}

\paragraph{Diffusive fluxes}

The geometry and units of fluxes are a bit difficult to get clear, so lets practice them here. Consider the following table of data points:

\begin{tabular}{l|c}
Depth [m]  & Concentration Sulfur $g\, m^{-3}$\\
  \hline
5	& 2\\
15	& 3\\
25	& 6\\
35	& 6\\
45	& 4\\
55	& 2\\
\end{tabular}

\begin{itemize}
    \item Sketch the profile of Sulfur with depth.

    \item Doing this sort of thing abstractly becomes hard. Lets assume this profile was collected in a column of water that is 65-m high, 1 meter wide and 1 meter deep. Divide the column into 10 x 1 x 1 $m^3$ blocks centered on our measurements. Approximately, how many grams of Sulfur are there in each of the 6 blocks?
    \item Approximate the gradient of Sulfur concentration at each the boundaries of each of the boxes (i.e. 10 m, 20 m, etc..).

\item If the turbulent diffusivity is $K=10^{-3} m/s$
 what is the diffusive transport across each of the block faces? Make sure you get the direction correct. Report your result in grams/second.

\item Consider any one block. You now should know the value and direction of flux (in g/s) across the upper and lower face, and you should know the total grams inside the block. If you assume the flux remains constant for 1000 s, how many grams of Sulfur are in your cell after 1000 s?

\item Repeat for all 6 cells (hint, use a computer!) and plot the new sulfur profile after a 1000 s.

\item Repeat the procedure 9 more times to get the gradient after 10000 s; (Hint: really use a computer;). Plot the sulfur profile at each timestep.

\item Do the changes of the profile correspond to your intuition of how diffusion works?
\end{itemize}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\section{Results}

\subsection{Joint Impact of Coaches}

In aggregate, coach fixed effects explain a significant portion of the variation in team wins. Table 3 displays the results from two models, a baseline Model (I) that doesn't include fixed effects and Model (II) that includes them. A comparison between (I) and (II) highlights the importance of including them. For example, the adjusted R-squared increases by 12.8 percentage points, pointing to greater explanatory power in Model (II). Additionally, the coefficients on Players 1 through 5 experience a decrease in magnitude in (II). This decrease is highest in absolute terms for players 1 and 5, which indicates that much of these players' performance is associated with the roles assigned to them by their coach. In other words, great coaches tend to make a better use of their best and worst player in the main rotation. Moreover, the F-test on the joint significance of coaches is highly significant, underlining the importance of including them.

%\subfile{tables/table3.tex}

By looking at Model (II), we can infer that roster quality and injuries are important determinants of success, with statistically significant coefficients across the board. On average, a full point increase in Player 1's BPM is associated with 1.73 additional wins for team \textit{i}, compared to 0.84 for player 5. This suggests that, on average, player 1 is almost twice as important for success as player 5. In terms of injuries, a game missed by player 1 is associated with twice as many losses as player 5. For example, a player like Stephen Curry who achieves a BPM of 12 is associated with around 18 team wins and every game he misses is associates lowers wins by 0.17. These results confirm general intuition that star players matter to a great degree.

\subsection{Individual Coach Fixed Effects}

In terms of individual fixed effects, some coaches seem to make a bigger difference than others. Table 4 presents the estimations corresponding to each of the 164 head coaches represented in the sample, each one assigned a single coefficient. \footnote{Coefficients for \textit{non-movers} are greyed-out because they are subject to lower accuracy.} The coefficients are significant in magnitude and are subject to high variability. This means that not only do coaches improve performance by a statistically significant margin, but the choice of head coach matters to a large extennt.

The size of coefficients is relative to a reference category, which means that the magnitude and significance levels are subject to vary depending on the choice of reference category. In this case, the reference category is chosen as the mean head coach.

The high degree of heterogeneity in the resulting estimates indicates that the implications for making the \textit{right} hire are substantial. Given the magnitude of the coefficients, many head coaches are able to propel teams into playoff territory or make them championship contenders. Although my estimates are based solely on 3-seasons worth' of regular season performance, 7 of the top 10 coaches happen to be NBA champions. This suggests that organizations who hire the right managers are more likely to reach the ultimate prize.

\subsubsection{Exceptional Coaches}

By descending order of impact, the top coaches in the resulting ability estimates include Gregg Popovich (13.0), Brad Stevens (12.3), Tom Thibodeau (12.3), Pat Riley (10.5), Larry Bird (10.4), Steve Kerr (10.3), Rick Carlisle (9.6), Dave Joerger (9.5), and Billy Cunningham (9.1). As such, switching from Monty Williams who is at the mean of coaches,  to Gregg Popovich generates an additional 13.0 wins when roster characteristics are held at their means. To put this into perspective, this would have been enough to push Williams’ Pelicans to a playoff position in 2017.

\subsubsection{Overrated Coaches}

To get a feel for these estimates, it is helpful to compare between the left-hand side and the right-hand side of Table 4, where estimated fixed effects are compared against standard win percentages. Coaches who figure at the top of the ladder on the right-hand side but not the left can be considered as \textit{overrated} in the sense that their success has had a lot to do with the quality of the roster they've inherited. A prime example is Mike Brown who figures at 11 on the right but 67 on the left. In other words, at the time of his tenure, the roster's talent level [see LeBron James] helped propel his team to higher winning percentages. Other similar instances include David Blatt,....

\subsubsection{Underrated Coaches}

Similarly, this comparative exercise can help identify coaches who have had a great impact despite their team's mediocre end-of-season record. A primary case-in-point is Brad Stevens whose average wins over the course of his first three seasons ranks him 69th but whose estimated fixed effect ranks him second overall. In such cases, it can be said that teams tend to exceed expectations under this head coach. In Stevens' case, his team has achieved an average 38 wins with a roster equivalent to around 12.3 wins less than that.