% This is really cool extra stuff that unfortunately I just don't have time for!

\subsection{Grasped Objects}
\label{subsec:family:grasped-objects}

One instance specific to manipulation problems is the handling of
grasped objects.
For example,
consider a manipulator which grasps a geometric object.
This affects the set of collision-free configurations
across a large section of $\mathcal{C}$
relative to the old set of valid configurations $S_{\ms{old}}$.
However,
the resulting $\mathcal{C}$-subset $S_{new}$
can be represented simply as
$S_{\ms{new}} = S_{\ms{old}} \cap G$,
with $G$ the set of robot configurations in which
the \emph{grasped object} (only)
is deemed free of collision with the robot and environment.
This structure is discussed in the context of the
\emph{conditional reachability graph},
part of the \textsc{FFRob} heuristic framework
\citep{garrett2014ffrob}.

For example,
consider the manipulation problem in
Figure~\ref{fig:family:testherb-problem}.
The robot must find a path which moves its arm to grasp the cup.
After the cup is grasped,
the robot can reuse any edge in the existing roadmap
by simply checking the grasped cup
against the remainder of the environment.
This structure,
together with the approach to dynamic environments,
are included together in the experimental results
(Section~\ref{subsec:family:herb-experiment}).

\section{Application: Caching Invariant Geometry}
\label{subsec:family:cached-self-valid}

\paragraph{Cached Self-Collision-Checked Roadmaps.}
Self-collision checking is a potentially expensive component to
articulated motion planning;
in contrast to environment checking,
it is fundamentally quadratic in the number of moving links.
Further, pairs of links to be checked
tend to be relatively close to each other,
reducing the effectiveness of broad-phase approaches.

Leven and Hutchinson \citep{leven2000changing}
introduced the concept of a pre-cached roadmap consisting of
configurations and paths already known to be valid w.r.t.
self-collision.
As a type of invariant in $\mathcal{C}$,
this can be seen as a particular instance of family motion planning.
See Fig.~\ref{fig:family:self-collision-example}.

\begin{marginfigure}
   \centering
   \includegraphics{build/self-collision}
   \caption{A roadmap is pre-computed in $R$,
      the subset of $\mathcal{C}$ consisting of configurations free
      of robot self-collision.
      Online, the planner must find a path that's also within $E$,
      the subset free of environment collision.
      When solving this query in $S = R \cap E$,
      the Family PRM automatically prefers potential paths with
      pre-computed edges (e.g. shown in grey)
      due to lower planning costs over alternatives with lower
      execution costs.}
   \label{fig:family:self-collision-example}
\end{marginfigure}

\begin{figure*}
   \centering
   \includegraphics{build/family-sq-caching/pvxs}
   \caption[LEMUR single-query results on several HERB problems,
      all using baked calls.
      Traces are LEMUR,
      the family motion planner (no caching),
      and the family motion planner with self-checked caching.
   ]{LEMUR single-query results on several HERB problems,
      all using baked calls.
      Traces are LEMUR
      (\protect\tikz{\protect\draw[thick,black] (0,0) -- (0.15,0.15);}),
      the family motion planner (no caching)
      (\protect\tikz{\protect\draw[thick,blue] (0,0) -- (0.15,0.15);}),
      and the family motion planner with self-checked caching
      (\protect\tikz{\protect\draw[thick,green] (0,0) -- (0.15,0.15);}).}
\end{figure*}

\section{Application: Exploiting Conservative Geometric Approximations}
\label{subsec:family:broad-phase}

The family formulation also enables motion planners to
reason directly about different robot or environment models.
For example, consider two geometric robot models,
one with high quality (e.g. from a CAD program),
and one hand-tuned ``padded'' model consisting of
a small number of simple conservative bounding volumes.
The $\mathcal{C}$-subsets derived from these models
are related by $R_{\ms{padded}} \subseteq R_{\ms{CAD}}$.
Collision checkers currently use a similar approach internally
to speed up collision checks (see Fig.~\ref{fig:family:broad-phase}.
and Fig~\ref{fig:family:broad-phase-2d}).

See Figure~\ref{fig:family:broad-phase-2d}
for a simple example with integrated broad-phase collision checking
as described in Section~\ref{subsec:family:broad-phase}.

\begin{figure*}[b]
   \centering

   \subfloat[
      A single-set planner testing simply for membership in
      $\mathcal{C}_{\mbox{\scriptsize free}}$
      treats a collision validity checker as a
      ``black box.''
      Internally,
      modern checkers first employ an inexpensive broad-phase check
      using a low-dimensional conservative representation
      to quickly identify non-colliding bodies before
      resorting to an expensive narrow-phase check.
   ]{%
      \includegraphics{build/broadphase-single}%
   }%
   \quad%
   \subfloat[
      A family motion planner can explicitly reason about the
      conservative nature of the broad-phase check.
      This allows it to defer some narrow phase checks
      (often indefinitely)
      and instead prefer paths that require fewer expensive checks.
   ]{%
      \includegraphics{build/broadphase-multi}%
   }

   \caption[][0.0in]{Collision validity checking is a commonly used
     indicator function.
     The family formulation allows an intelligent planner to
     reach inside the checker's ``black box'' and reduce the number
     of costly narrow-phase checks.
     Resulting paths tend to be cheaper to compute and
     stay further from obstacles.}
   \label{fig:family:broad-phase}
\end{figure*}

% this is the broad phase bean figure
\begin{figure}
   \centering

   % left side
   \subfloat[Paths with lambda=0][%
      \centering
      Paths with $\lambda = 0$\par
      Average length: 733.0\par
      Average check cost: 7219.5
   ]{
      \includegraphics[width=0.45\textwidth]
      {figs/bean-allpaths-lambda0.png}
   }
   % right side
   \subfloat[Paths with lambda=1][%
      \centering
      Paths with $\lambda = 1$\par
      Average length: 836.5\par
      Average check cost: 4692.6
   ]{
      \includegraphics[width=0.45\textwidth]
      {figs/bean-allpaths-lambda1.png}
   }
   \\
   \subfloat[Paths with lambda=0][%
      \centering
      Paths with $\lambda = 0$\par
      Average length: 733.0\par
      Average check cost: 2685.7
   ]{
      \includegraphics[width=0.45\textwidth]
      {figs/bean-allpaths-padded-lambda0.png}
   }
   % right side
   \subfloat[Paths with lambda=1][%
      \centering
      Paths with $\lambda = 1$\par
      Average length: 907.1\par
      Average check cost: 1064.5
   ]{
      \includegraphics[width=0.45\textwidth]
      {figs/bean-allpaths-padded-lambda1.png}
   }

   \caption{A simple 2D example of the Family PRM using
     a broad-phase check.
     Checking for collision with the grey box is 10x less expensive
     than with the actual black obstacle.}
     %\cdnote{I need to talk about this in the text.}}
   \label{fig:family:broad-phase-2d}
\end{figure}

\section{Application: Dynamic Environments}

\paragraph{Dynamic Environments.}
\label{subsec:family:dynamic-environments}

\begin{figure}
   \centering

   \begin{minipage}{.6\textwidth}
      \begin{tikzpicture}
      \tikzset{>=latex} % arrow heads
      \node[anchor=south west,inner sep=0] at (0,0)
        {\includegraphics[width=0.8\textwidth]{figs/chimp-voxels-delta.png}};

      \node[draw,inner sep=3pt,fill=white,fill opacity=0.9,align=center]
        (debrislab) at (0.7,1.0) {Debris object\\removed};
      \node[circle,inner sep=2,draw,fill=white] (debris) at (2.2,2.9) {};
      \draw[draw=black, double=white, double distance=1pt, line width=1pt]
        (debrislab.north) -- (debris);

      \node[draw,inner sep=3pt,fill=white,fill opacity=0.9,align=center]
        (addlab) at (5.0,2.0) {Additional\\voxels seen};
      \node[circle,inner sep=2,draw,fill=white] (added) at (4.4,5.0) {};
      \draw[draw=black, double=white, double distance=1pt, line width=1pt]
        (addlab.north) -- (added);

      \end{tikzpicture}
   \end{minipage}%
   \,%
   \begin{minipage}{.35 \textwidth}
      \includegraphics{build/retroactive-a}

      \includegraphics{build/retroactive-b}
   \end{minipage}

   \caption{
      Structured or unstructured dynamic environments
      can be represented as a family problem
      (see Section~\ref{subsec:family:dynamic-environments}).

      \vspace{0.05in}
      \noindent
      (Left) A disaster response robot maintains a
      dynamic unstructured environment model
      using coarse voxels
      (scene data from a debris-clearing task at a
      recent disaster response competition).
      Since the last planning query,
      voxels have been added (green) and removed (red).

      \vspace{0.05in}
      \noindent
      (Right) $\mathcal{C}$-subsets and relations
      can be added retroactively.
      Here, the graph for an initial query is checked w.r.t $S_1$.
      After environment changes,
      $S_1$ is redefined in terms of the $\mathcal{C}$-subsets
      derived from the set of common, added, and removed elements,
      allowing for reuse on a query in $S_2$.
      Here,
      the planner need only check existing path segments
      against added voxels in order to reuse them for the current query.}
   \label{fig:family:dynamic-environments}
\end{figure}

The sensors on most articulated robots allow them to maintain
dynamic environment models to track changing collision geometry.
These models might be
structured (e.g. recognizing objects with known models)
or unstructured (e.g. occupancy models).
In both cases,
even in a changing world,
there are often areas that are fixed between planning queries.

Prior work (e.g. \citep{jaillet2004dynamicprm})
leverages this by imposing a dichotomy between
\emph{fixed} and \emph{moving} components of $\mathcal{C}$.
Our formulation extends this to an arbitrary number of such labels,
including ones defined retroactively (i.e. during planning;
see Fig.~\ref{fig:family:dynamic-environments}).
By explicitly labeling such areas in workspace
(and leveraging the \emph{set containment} property
\citep{newmanbranicky1991cspacetransforms}),
we can represent this structure as a family motion planning problem.

\section{Other Stuff}

\pgfplotstableread{
barname mean error
RRTConnect 2.56378257751494 0.1819787187133847
LazyPRM(*) 2.59627093791926 0.07645708288005078
LEMUR(SQ) 1.95654549121872 0.04821756844687192
LEMUR(MQ) 1.5911829090115797 0.03471955055186312
}{\datalemurherbbin}

\begin{figure}
   \centering
   \begin{tikzpicture}
      \begin{axis}[
         width=6.0cm,
         height=4.0cm,
         xbar,
         bar width=12,
         xmin=0,
         enlarge y limits={abs=0.45cm},
         %xmin=0,xmax=95,
         %xtick pos=bottom,
         %symbolic y coords={E, F, R, A, B, W, P},
         yticklabels from table={\datalemurherbbin}{barname},
         ytick=data,
         ytick pos=left,
         xmajorgrids,
         %xmajorticks=false,
         ticklabel style={font=\footnotesize},
         xlabel={\footnotesize{Planning time $p$ (s)}},
         xlabel near ticks,
         ]
      %\node[circle,fill=white,inner sep=1pt,text=black!40] at (axis cs:40,-5.6) {\scriptsize 40};
      %\node[circle,fill=white,inner sep=1pt,text=black!40] at (axis cs:60,-5.6) {\scriptsize 60};
      %\node[circle,fill=white,inner sep=1pt,text=black!40] at (axis cs:80,-5.6) {\scriptsize 80};
      \addplot[color=black,fill=black!20,error bars/.cd,x dir=both,x explicit]
         table[y expr=-\coordindex,x=mean,x error expr=\thisrow{error}]
         {\datalemurherbbin};
      \end{axis}
   \end{tikzpicture}
   \caption{LEMUR Herbbin, $\lambda = 1$}
\end{figure}

\pgfplotstableread{
barname mean error
RRTConnect 17.735649337767864 0.8572182241158215
LazyPRM(*) 11.02914777755716 0.2718847854283968
LEMUR(SQ) 8.987455253601558 0.22683157494511255
LEMUR(MQ) 5.995352478027379 0.23607184395127515
}{\datalemurworkcell}

\begin{figure}
   \centering
   \begin{tikzpicture}
      \begin{axis}[
         width=6.0cm,
         height=4.0cm,
         xbar,
         bar width=12,
         xmin=0,
         enlarge y limits={abs=0.45cm},
         %xmin=0,xmax=95,
         %xtick pos=bottom,
         %symbolic y coords={E, F, R, A, B, W, P},
         yticklabels from table={\datalemurworkcell}{barname},
         ytick=data,
         ytick pos=left,
         xmajorgrids,
         %xmajorticks=false,
         ticklabel style={font=\footnotesize},
         xlabel={\footnotesize{Planning time $p$ (s)}},
         xlabel near ticks,
         ]
      \node[circle,fill=white,inner sep=1pt,text=black!40] at (axis cs:40,-5.6) {\scriptsize 40};
      \node[circle,fill=white,inner sep=1pt,text=black!40] at (axis cs:60,-5.6) {\scriptsize 60};
      \node[circle,fill=white,inner sep=1pt,text=black!40] at (axis cs:80,-5.6) {\scriptsize 80};
      \addplot[color=black,fill=black!20,error bars/.cd,x dir=both,x explicit]
         table[y expr=-\coordindex,x=mean,x error expr=\thisrow{error}]
         {\datalemurworkcell};
      \end{axis}
   \end{tikzpicture}
   \caption{LEMUR Workcell, $\lambda = 1$}
\end{figure}

% data over 84 data points
% there are 85 PlanToNamedConfiguration planning requests in
% my planning_queries/yaml/* directory;
% i paired them up, and these are the numbers across all pairs
\pgfplotstableread{
barname mean stderror95
FamilyFirst 0.3858953203473929 0.011569714605374383
FamilySecond 0.18286271038508334 0.0070175681823757274
}{\datalemurtableclear}

\begin{figure}
   \centering
   \begin{tikzpicture}
      \begin{axis}[
         width=6.0cm,
         height=3.0cm,
         xbar,
         bar width=12,
         xmin=0,
         enlarge y limits={abs=0.45cm},
         %xmin=0,xmax=95,
         %xtick pos=bottom,
         %symbolic y coords={E, F, R, A, B, W, P},
         yticklabels from table={\datalemurtableclear}{barname},
         ytick=data,
         ytick pos=left,
         xmajorgrids,
         %xmajorticks=false,
         ticklabel style={font=\footnotesize},
         xlabel={\footnotesize{Planning time $p$ (s)}},
         xlabel near ticks,
         ]
      %\node[circle,fill=white,inner sep=1pt,text=black!40] at (axis cs:40,-5.6) {\scriptsize 40};
      %\node[circle,fill=white,inner sep=1pt,text=black!40] at (axis cs:60,-5.6) {\scriptsize 60};
      %\node[circle,fill=white,inner sep=1pt,text=black!40] at (axis cs:80,-5.6) {\scriptsize 80};
      \addplot[color=black,fill=black!20,error bars/.cd,x dir=both,x explicit]
         table[y expr=-\coordindex,x=mean,x error expr=\thisrow{stderror95}]
         {\datalemurtableclear};
      \end{axis}
   \end{tikzpicture}
   \caption{Family Tableclear, $\lambda = 0$}
\end{figure}


\chapter{Beyond \kl(tit){CastCIC}: Models, Indices and Pure Reasoning}
\label{chap:beyond-gcic}

\cref{chap:bidir-gradual-elab} establishes quite a few properties of \kl{GCIC} and
\kl{CCIC}, culminating with \kl{elaboration graduality}. This is, however, still far
from satisfactory. First, it is missing proofs of \kl{normalization} for the two variants
which are supposed to satisfy it – \kl{CCICT} and \kl{CCICs} –, and of \kl{graduality} – for
\kl{CCICP} and \kl{CCICs}.
Next, it only treats \kl{CIC-}, \ie it does not handle indexed inductive types.
But these are crucial in order to really exploit dependency:
for instance, most of our introductory examples were based on the vector type, an indexed
inductive type outside the scope of \cref{chap:bidir-gradual-elab}.

In this chapter we go over these issues and possible solutions:
\cref{sec:realizing-cast-calculus} describes model constructions to establish
both \kl{normalization} and \kl{graduality};
\cref{sec:indices-issue} describes the issue with indexed inductive types, and gives
possible solutions in case the indices are “nice enough”, which covers vectors; finally,
\cref{sec:ReTT} gives a much more ambitious solution to handle indices, giving a proper
treatment of the equality type – the stereotypical pathologic inductive type –, and much more.

As these have no direct relation to bidirectional typing \textit{per se},
we do not dwell on the technical details in this chapter.
The interested reader can consult either \sidetextcite{LennonBertrand2022} –
\cref{sec:realizing-cast-calculus} corresponds roughly to
Section 6 there, and \cref{sec:indices-issue} to Sections 6 and 8.3 –, or
\sidetextcite{Maillard2022} – corresponding to \cref{sec:ReTT}.

\section{Realizing \kl(tit){CastCIC}}
\label{sec:realizing-cast-calculus}

\subsection{The discrete model}

To inform the design and justify the reduction rules provided for
\kl{CCIC}, we build a syntactic model%
\sidenote{
  \intro{Syntactic models} \cite{Boulier2017,Boulier2018},
  are a kind of models of type theory defined directly by induction
  on the raw syntax, in a way akin to program translation or compilation. This allows for
  simple models, that moreover can be used to capture fine-grained properties that only
  make sense on that raw syntax,
  typically those that need to separate between convertible terms.
}%
\margincite{Boulier2017}%
\margincite{Boulier2018}
of \kl{CCIC} by translation to \kl{CIC}
augmented with induction-recursion
\sidecite{MartinLoef1996,Dybjer2003,Ghani2015}.

From a type theoretical point of view, what makes \kl{CCIC} peculiar are the
possibility of having \emph{exceptions} – both “pessimistic” ($\err$) and
“optimistic” ($\?$) –, and the necessity to do \emph{intensional type
analysis} in order to resolve casts. For the former, we build upon the work of
\sidetextcite{Pedrot2018} on the exceptional type theory \kl{ExTT}.
For the latter, we reuse the technique of \textcite{Boulier2017} to equip the universe with
an elimination principle $\intro*\typerec$%
\sidenote{Corresponding to a form of ad-hoc polymorphism.},
which requires induction-recursion to be implemented.
%

\AP We call this syntactic model of \kl{CCIC} the \intro{discrete model}.
% , in contrast with a semantic model motivated by \kl{graduality}. This \kl{discrete model}
It captures the intuition that the unknown type is
inhabited by “tagged values”, \eg a term together with its type.
In other words, the \kl{unknown type} $\?[\uni]$ behaves as a dependent sum $\Sb A : \uni.\ A$.
Projecting out of it is realized through type analysis using $\typerec$,
and may fail – raising an error in the \kl{ExTT} sense.

Note that we provide a particular interpretation of the unknown
term in the universe, which is legitimized by an observation
made by \textcite{Pedrot2018}: \kl{ExTT} does not constrain in
any way the definition of exceptions in the universe.
This is crucial to combine \kl{ExTT} with a universe equipped with $\typerec$.
%

The key point to prove \kl{normalization} is that reduction is preserved,
in the sense that a reduction step in the source theory \kl{CCIC} is mapped to at least one step
in the target. Thus, the target being \kl{normalizing}, so is \kl{CCIC}.

\begin{theorem}[Normalization for \kl{CCIC}]
  Both \kl{CCICT} and \kl{CCICs} have the \kl{normalization}
  property (\cref{prop:normalization}).
\end{theorem}

An important corollary of this property, in combination with \kl{safety}
(\cref{thm:ccic-psafe}), is a weak form of \kl{logical consistency}, characterizing the
possible inhabitants of the empty type:

\begin{theorem}[Weak logical consistency]
  Suppose $\tcol{t}$ is a closed inhabitant of the empty type
  $\tcol{\Empty}$ in \kl{CCICT} or \kl{CCICs}, \eg $\vdash \tcol{t} \cty \tcol{\Empty}$.
  Then $\tcol{t}$ must reduce to either $\tcol{\err[\Empty]}$ or $\tcol{\?[\Empty]}$.
\end{theorem}

\subsection{The monotone model}

The simplicity of the discrete model comes at the price of an inherent inability to
characterize which casts are guaranteed to succeed, \ie a \kl{graduality} theorem.
%
\AP To overcome this limitation, and prove graduality of \kl{CCICs},
we can build a more elaborate \intro{monotone model}, inducing a
precision relation that is well-behaved with respect to conversion.

In this model, each type $A$ comes equipped with an order
structure $\sqsubseteq^A$ – a reflexive, transitive, antisymmetric and
proof-irrelevant relation – modelling precision between terms.
%
In particular, the exceptions $\err[A]$ and $\?[A]$ correspond respectively to the smallest
and greatest element of $A$ for this order.
%
Saying that this interpretation of types as posets is a model is equivalent to
saying that each term and type former is enforced to be monotone,
providing a strong form of \kl{graduality}.
%
This implies in particular that such a model cannot be defined for \kl{CCICT},
as this type theory lacks graduality.

\AP The precision order of the monotone model can be reflected back to
\kl{CCIC}, giving rise to the \intro{propositional precision} judgment
$\tcol{\Gamma} \vdash \tcol{t} \intro*\precision{\tcol{T}}{\tcol{U}} \tcol{u}$, where
$\tcol{T}$ and $\tcol{U}$ are the respective types of $\tcol{t}$ and $\tcol{u}$.
Type dependency naturally demands such a notion of inhomogeneous precision,
rather than a simpler notion relating only terms of the same type.

This precision relation bears a similar relationship to \kl{definitional precision}
$\cdpre$ as propositional equality to \kl{conversion}/definitional equality in \kl{CIC}.
%
\kl{Propositional precision} can be used to prove precision statements
inside the target type theory, for instance we can show by
case analysis on $\tcol{b} : \tcol{\Bool}$ that
\[ \tcol{b} : \tcol{\Bool} \vdash \tcol{\ind{\Bool}{b}{x.\uni}{A,A}}
\precision{\tcol{\square}}{\tcol{\square}} \tcol{A}\]
a judgment that does not hold for \kl{definitional precision}.
%
In particular, \kl{propositional precision} is invariant by \kl{conversion}: if
$\tcol{t} \aconv \tcol{t'}$, $\tcol{u} \aconv \tcol{u'}$ and
$\tcol{\Gamma} \vdash \tcol{t} \precision{\tcol{T}}{\tcol{U}} \tcol{u}$
then
$\tcol{\Gamma} \vdash \tcol{t'} \precision{\tcol{T}}{\tcol{U}}
\tcol{u'}$.
%
But this means that \kl{propositional precision} is too coarse to capture properties such
as the \kl{simulation} property (\cref{thm:simulation}) and its corollaries
(\cref{cor:red-types,cor:mon-cons}), because these distinguish \kl{convertible} terms.

Still, we can relate the two notions, as follows:
\begin{theorem}[Compatibility of \kl{structural} and \kl{propositional} precision]
  \label{thm:prop-prop-prec}

  If $\cinferty{\Gamma}{t}{T}$, $\cinferty{\Gamma}{u}{U}$ and
  $\tcol{\Gamma \mid \Gamma} \vdash \tcol{t} \capre \tcol{u}$, then
  $\tcol{\Gamma} \vdash \tcol{t} \precision{\tcol{T}}{\tcol{U}} \tcol{u}$.

  Conversely, if
    $\emptycon \vdash \tcol{v_1} \precision{\tcol{\Bool}}{\tcol{\Bool}}
    \tcol{v_2}$ for normal forms $\tcol{v_1}, \tcol{v_2}$, then
    $\emptycon \vdash \tcol{v_1} \capre \tcol{v_2}$.

\end{theorem}
Again, this is similar to the relation between \kl{conversion} and propositional equality:
the former always implies the latter, and one can come back from the second to the first in
a constrained enough setting – here, on closed booleans.

Finally, the main property satisfied by \kl{propositional precision} is \kl{graduality}:

\begin{theorem}[\kl{Graduality}]
  \label{thm:graduality}

  \kl{Propositional precision} satisfies the \kl{Dynamic Gradual Guarantee}: if
  $\cinferty{\Gamma}{t}{T}$, $\cinferty{\Gamma}{t'}{T}$ and
  $\tcol{\Gamma} \vdash \tcol{t} \precision{\tcol{T}}{\tcol{T}} \tcol{t'}$ hold, then
  $ \tcol{t} \obsRef \tcol{t'}$.

  Casts form \kl{embedding-projection pairs}. That is, if
  $\cinferty{\Gamma}{t}{T}$ and $\cinferty{\Gamma}{u}{U}$,
  and moreover
    $\tcol{\Gamma} \vdash \tcol{T} \precision{\tcol{\square}}{\tcol{\square}} \tcol{U}$,
  then the following three properties are equivalent:
  \begin{align*}
    \tcol{\Gamma} \vdash \tcol{\cast{T}{U}{t}} \precision{\tcol{U}}{\tcol{U}} \tcol{u}
    \Leftrightarrow  \tcol{\Gamma} \vdash \tcol{t} \precision{\tcol{T}}{\tcol{U}} \tcol{u}
    \Leftrightarrow  \tcol{\Gamma} \vdash \tcol{t}
    \precision{\tcol{T}}{\tcol{T}} \tcol{\cast{U}{T}{u}}
  \end{align*}
  And furthermore,
    $\tcol{\Gamma} \vdash \tcol{\cast{U}{T}{\cast{T}{U}{t}}} \precision{\tcol{T}}{\tcol{T}} \tcol{t}$ – this is the retraction property.
\end{theorem}

\section{The issue with indices: gradual vectors and equalities}
  [Gradual vectors and equalities]
\label{sec:indices-issue}

\subsection{The issue with propositional equality}

% In \kl{CIC}, propositional equality \coqe{eq A x y}, corresponds to the Martin-L{\"o}f
% identity type~\cite{Martin-Lof-1973}, with a single constructor \coqe{refl} for reflexivity, and the
% elimination principle known as \coqe{J}:
% %
% \begin{coq}
%   Inductive eq (A : Type) (x : A) : A -> Type := refl : eq A x x.
% \end{coq}
% \begin{coq}
%   J : forall (A : Type) (P : A -> Type) (x : A) (t : P x) (y : A) (e : eq A x y), P y
% \end{coq}
% together with the conversion rule:

% \begin{center}
%  \coqe{J A P x t x (refl A x) equiv t}
% \end{center}

For the sake of exposing the problem, suppose that we can define the equality type
$\tcol{\eqty[A]{a}{a'}}$ in \kl{CCIC},
while still satisfying \kl{canonicity}, \kl{conservativity} and \kl{graduality}.
%
This means that for an equality $\tcol{\eqty{t}{u}}$ involving closed terms
$\tcol{t}$ and $\tcol{u}$ of \kl{CIC}, there should only be three possible canonical forms:
$\tcol{\refl[A][t]}$ whenever $\tcol{t}$ and $\tcol{u}$ are convertible terms,
as well as $\tcol{\err}$ and $\tcol{\?}$.

Just under these assumptions,
we can show that there exist two functions that are pointwise equal in \kl{CIC},
but are no longer equivalent in \kl{CCIC}.
Consider the two functions $\tcol{\id_{\Nat}}$ and $\tcol{\addz}$ defined respectively as
$\tcol{\id_{\Nat}} \coloneqq \tcol{\l x : \Nat.\ n}$ and
$\tcol{\addz} \coloneqq \tcol{\l x : \Nat.\ x + \z}$.
In \kl{CIC}, these functions are not convertible, but they are pointwise equal,
and observationally equivalent.
However, they would not be observationally equivalent in \kl{GCIC} under our assumptions.
%
To see why, consider the following term:
\[\tcol{\test} \coloneqq \tcol{\l f : \Nat \to \Nat. \ind{\eqtyop}{y.z.\Bool}{\cast{\eqty{\id_{\Nat}}{\id_{\Nat}}}{\eqty{\id_{\Nat}}{f} \Leftarrow \?[\uni]}{\refl}}{\true}} \]

We have $\tcol{\test \id_{\Nat} \red \tcol{\true}}$ because, by \kl{graduality}, the
upcast-downcast in the scrutinee must succeed, \ie
\[\tcol{\cast{\eqty{\id_{\Nat}}{\id_{\Nat}}}{\eqty{\id_{\Nat}}{\id_{\Nat}} \Leftarrow \?[\uni]}{\refl}} \red \tcol{\refl[\id_{\Nat}]}\]
However, since $\tcol{\addz}$ is \emph{not} \kl{convertible} to $\tcol{\id_{\Nat}}$,
\[\tcol{\cast{\eqty{\id_{\Nat}}{\id_{\Nat}}}{\eqty{\id_{\Nat}}{\addz} \Leftarrow \?[\uni]}{\refl}}\]
cannot possibly reduce to $\tcol{\refl}$, and
thus would need to reduce either to $\tcol{\err}$ or $\tcol{\?}$;
and so does $\tcol{\test \addz}$.
%
This means that the theory would be very intensional, by being able to distinguish between
any two functions that are not \kl{convertible}, even if they are pointwise equal.

More generally, the issue is the following: given a constructor $\tcol{c}$
of an inductive type $\tcol{I}$, we need to decide what to do when confronted with some
$\tcol{\cast{I(b_1, \dots, b_n)}{I(b'_1, \dots, b'_n)}{c(a_1, \dots, a_m)}}$.
If $\tcol{I}$ does not have indices, as in the case of lists, we know that $\tcol{c}$
can always be used to inhabit $\tcol{I(b_1', \dots, b_n')}$,
if given arguments of the right types.
If $\tcol{I}$ has indices, however, this might not be possible due to typing constraints,
as in the example of $\tcol{\refl}$.
But we still need to provide an inhabitant of that type as redex for the cast! If we resort to
the wildcards $\tcol{\?}$ or $\tcol{\err}$,
then we expose a very intensional behaviour such as the one above.
However, in the setting of a generic inductive type
– such as that of the equality –, deciding whether it is inhabited by a “valid”,
non-wildcard, term in a given non-empty context is undecidable,
so we cannot hope to always decide whether the cast should fail or
return such a valid term.

\subsection{Solutions for vectors}

Thankfully, not all indexed inductive types are as thorny as equality. Indeed,
in examples such as $\Vect$,%
\sidenote{And quite a lot of those used in the context of dependently typed programming.}
solutions are possible that avoid the dead-end identified for equality, by carefully
using the structure of indices. These rely on two well-known alternatives to indexed
inductive types for capturing properties intrinsically: type-level eliminators,
and “forded” inductive types.

\paragraph{Type-level eliminators.}

Instead of an inductive type, $\Vect$ can be defined as a recursive function on its index,
at the type level, effectively representing lists as nested pairs:
\[\FVect \coloneqq \l (A : \uni) (n : \Nat). \ind{\Nat}{n}{x.\uni}{\Unit,y.p_y.A \times p_y}\]
corresponding to the \kl{Coq} definition

\begin{coqcode}
  Fixpoint FVect (A : Type) (n : ℕ) : Type :=
    match n with 0 => unit | S n => A * FVec A n end.
\end{coqcode}

\AP Type-level eliminators can be used as soon as the indices are \intro{concretely
forceable} \sidecite{Brady2004}. Intuitively, \kl{concretely forceable} indices
are those that can be matched upon (like $n$ in the example of $\Vect$).
\sidetextcite{Gilbert2019} give a general translation of this kind
to build mock-up inductive types inside a sort of definitionally irrelevant propositions.

This presentation coincides with the indexed inductive one  on standard, non-exceptional terms, but quickly becomes very imprecise in presence of unknown indices.

\paragraph{Forded inductive type.}

Instead of using an indexed inductive type, one can use a parametrized inductive type, with \emph{explicit} equalities as arguments to constructors.%
\sidenote{This technique has been coined “fording” by \textcite[Section 3.5]{McBride1999},
as an allusion to Henry Ford' quote “\textit{Any customer can have a car painted any color that he wants, so long as it is black.}”}%
\margincite{McBride1999}
For instance, vectors can be defined in this style as follows:
\begin{coqcode}
  Inductive Vectf (A : Type) (n : ℕ) : Type :=
  | nilf : eq_nat 0 n -> Vectf A n
  | consf : A -> forall m : ℕ, eq_nat (S m) n
      -> Vectf A m -> Vectf A n.
\end{coqcode}
Here, \coqe{eq_nat} is the type of \emph{decidable} equality proofs between natural numbers,
expressing the constraints on $n$ – \eg $\eqty{n}{\z}$ – but avoiding the use of the unavailable propositional equalities –, which can be defined like this:
\begin{coqcode}
  Fixpoint eq_nat (m n : ℕ) : Type :=
  match m, n with
    | 0, 0 => True
    | S m, S n => eq_nat m n
    | _, _ => False
  end.
\end{coqcode}

This presentation is more accurate than the previous one when dealing with unknown indices,
but is too permissive with invalid index assertions. It fails very late when such invalid
assertions are made, meaning that error-reporting is bad.

\paragraph{Direct support.}

In contrast to the two previously-exposed encodings that both have serious shortcomings,
extending \kl{CCIC} with direct support for indexed inductive types can provide a
much more satisfactory solution. The idea is to reason about indices directly in
the reduction of casts.

To do so, we first add two new canonical forms,
corresponding to the casts of $\tcol{\vnil}$ and $\tcol{\vconsop}$ to
$\tcol{\GVect(A,\?[\Nat])}$: namely, $\tcol{\gvnil[A]}$ and
$\tcol{\gvcons[A][n]{a}{v}}$.

\begin{figure*}[ht]
\ContinuedFloat*
\begin{mathpar}
  % \inferrule{  }{\can{\GnilU{A}}}
  % \and
  % \inferrule{ }{\can{(\GconsU{A}{a}{n}{v})}}
  % \\
  \redrule{\tcol{
    \castrev{\left(\vcons[A][k]{a}{v}\right)}{\GVect(A,\S n)}{\GVect(B,\?[\Nat])}
  }}
  {\tcol{ \gvcons[B][n]{\left(\castrev{a}{A}{B}\right)}{\left(\castrev{v}{\GVect(A,k)}{\GVect(B,n)}\right)}
  }}[V-cons-?] \label{red:v-S?} \and
  \redrule{\tcol{
    \castrev{\left(\gvcons[A][k]{a}{v}\right)}{\GVect(A,\?[\Nat])}{\GVect(B,\S n)}
  }}
  {\tcol{ \vcons[B][n]{\left(\castrev{a}{A}{B}\right)}{\left(\castrev{v}{\GVect(A,k)}{\GVect(B,n)}\right)}
  }}[V-cons?-s] \label{red:v-?S} \and
  \redrule{\tcol{
    \castrev{\left(\vcons[A][k]{a}{v}\right)}{\GVect(A,\S n)}{\GVect(B, \S m)}
  }}
  {\tcol{\vcons[B][m]{\left(\castrev{a}{A}{B}\right)}
    {\left(\castrev{v}{\GVect(A,k)}{\GVect(B,m)}\right)}
  }}[V-cons-s]\label{red:v-S}
  \and
  \redrule{\tcol{
    \castrev{\left(\vcons[A][k]{a}{v}\right)}{\GVect(A,\S n)}{\GVect(B,\z)}
  }}{\tcol{
    \err[\GVect(B,\z)]
  }}[V-cons-0]\label{red:v-S0} \and
  \redrule{\tcol{
    \castrev{\left(\gvcons[A][k]{a}{v}\right)}{\GVect(A,\?[\Nat])}{\GVect(B,\z)}
  }}{\tcol{
    \err[\GVect(B,\z)]
  }}[V-cons?-0]\label{red:v-S?0}
\end{mathpar}
\caption{Casts between gradual vector types (excerpt)}
\label{fig:grad-vect-casts}
\end{figure*}

We then extend \kl{reduction} to account for casts on vectors
in canonical forms. \cref{fig:grad-vect-casts} presents these rules
when the argument of the cast is a non-empty vector.
%
\ruleref{red:v-S?} propagates the cast on the arguments,
but using the newly introduced $\tcol{\gvconsop}$. This effectively loses precision in the type
information, but keeps it all recorded in the term, so that it can be used
in case of a downcast. This is exactly what \ruleref{red:v-?S} does.
%
\ruleref{red:v-S} applies when both source and target indices are successors,
and propagates the cast of the arguments, just like in the case of lists.
%
Finally, as expected, \ruleref{red:v-S0} raises an error when the indices do not match.
Similarly, \ruleref{red:v-S?0} also raises an error when trying to create a vector of length
$\z$ from one with an unknown index, but whose underlying vector is non-empty.

\begin{figure}[ht]
\ContinuedFloat
\begin{mathpar}

\redrule{\tcol{
  \ind{\Vect}{\gvnil[A]}{y.z.P}{b_{\vnil},y_1.y_2.y_3.p_{y_3}.b_{\vconsop}}
}}
{\tcol{\castrev{b_{\vnil}}{\mathop{P}\z}{\mathop{P} \?[\Nat]}
}}[V-rect-nil?]\label{red:v-rect-unk-0}

\end{mathpar}
\caption{Eliminator for the gradual vector type (excerpt)}
\label{fig:vectors-excerpt}
\end{figure}

For the eliminator, there are two new computation rules, one for each
new constructor. We give the one for the case of $\tcol{\gvnil}$,
this is \ruleref{red:v-rect-unk-0}. These rules apply the eliminator to the
underlying non-exceptional constructor,
and then cast the result to $\tcol{\mathop{P} \?[\Nat]}$.
Intuitively, they transfer the cast on vectors to a cast on the reduct.

Note that all of these rules crucially use the fact that it is possible to discriminate between
$\tcol{\z}$, $\tcol{\S n}$ and $\tcol{\?[\Nat]}$,
which is a specificity of the vector type and explains why
this solution is not possible for \eg the equality.

This “definitive” presentation is justified by a modification of the models described in
\cref{sec:realizing-cast-calculus}, and gives the satisfactory behaviour described
in the examples of \cref{sec:indices,sec:specif}: it preserves as much computational content
as possible, while failing early when invalid assumptions are used.

\section{A Reasonably Gradual Type Theory}
\label{sec:ReTT}

In the context of a gradual proof assistant based on \kl{CIC},
the \kl{normalizing} and \kl{conservative} variant \kl{GCICT} is the most appealing,
as it ensures decidability of typing, (weak) \kl{canonicity}, and supports all existing developments and libraries by virtue of being a \kl{conservative extension} of \kl{CIC}.
Unfortunately, the universe shift introduced in \kl{CCICT} during reduction
means that some terms break graduality.
For instance, while the term
\[\tcol{\nArrow} \coloneqq
  \tcol{\l n : \Nat.\ \ind{\Nat}{n}{x.\uni[0]}{\Nat,y.p_{y}.\Nat \to p_{y}}} \]
or, in \kl{Coq},
\begin{coqcode}
  Fixpoint nArrow (n : ℕ) : Type :=
  match n with
    | 0 => ℕ
    | S m => ℕ -> nArrow n
  end.
\end{coqcode}
is well-typed in \kl{GCICT},
the type $\tcol{\P n : \Nat. \nArrow n}$ does not satisfy the \kl{embedding-projection} property
with respect to any unknown type $\tcol{\?[\uni[i]]}$,
because the appropriate universe level is not known \textit{a priori}.
However, apart from the fact that \kl{GCICT} does not satisfy \kl{graduality} globally,
little is known about its gradual properties as its metatheory
in this regard has not been developed. In particular, there is no clear characterization
of a class of terms for which \kl{graduality} holds.

\paragraph{A refined stratification of precision}
%
\AP However, by refining the stratification of
\kl{precision} we can develop a full account of graduality for an extension
of \kl{CCICT}, called \intro{CCICPrec}. The key idea is that $\tcol{\?[\uni[i]]}$ should be
the least precise type among all types at level $i$ and below,
\emph{except} for dependent function types at level $i$
– which are however still less precise than $\tcol{\?[\uni[\unext{i}]]}$.

\AP We can precisely characterize problematic terms as those that are not \intro{self-precise} –
\ie more precise than themselves. For function types,
self-precision means monotonicity with respect to precision.
A recursive large elimination as in $\tcol{\nArrow}$ is not monotone because
there is no fixed level $i$ for which
$\tcol{\nArrow n} \precision{}{} \tcol{\?[\uni[i]]}$, given
$\tcol{n} \precision{}{} \tcol{\?[\Nat]}$.

On the contrary, we can prove that the \kl{dynamic gradual guarantee} holds in \kl{CCICPrec}
for \emph{any} \kl{self-precise} context, and that casts between types related by
\kl{precision} induce \kl{embedding-projection pairs} between \kl{self-precise} terms.
%
Therefore, this shift in perspective in the interpretation of the unknown type and
the associated notion of precision yields a gradual theory
that conservatively extends \kl{CIC}, is normalizing,
and satisfies graduality for a large and well-defined class of terms.

\paragraph{Internalizing precision, reasonably}
While we could study graduality for \kl{CCICPrec} externally,
we observe that we can exploit the expressiveness of the type-theoretic setting to
internalize precision and its associated reasoning.
In particular, this makes it possible to state and prove, within the theory itself,
results about (self-)precision and graduality for specific terms.
In a way, this is the natural next step following the definition of
\kl{propositional precision} in \cref{sec:realizing-cast-calculus}.

Introducing \intro{internal precision} in a gradual type theory however
requires us to address two main obstacles.
First, when adding exceptions to CIC \sidecite{Pedrot2018},
the theory becomes inconsistent as a logic,
because it is possible to inhabit any type $\tcol{A}$ by raising an
exception $\tcol{\err[A]}$.
In the gradual setting, there is also the alternative of using the \kl{unknown term}
$\tcol{\?[A]}$ to inhabit any type $\tcol{A}$.
If we want to support valid internal reasoning about precision and graduality,
we need to avoid these degenerate proofs and provide a logically consistent theory.
Second, the gradual type theory needs to satisfy extensionality principles in order to support
the notion of precision as error approximation \sidecite{New2018}.
% In particular, precision on function types
% should be defined pointwise, \ie~functions should be extensionally monotone with respect to precision (if $t \precision{}{} u$ then
% $f\;t \precision{}{} f\;u$ for any function $f$).
Embracing extensionality principles in an intensional type theory such as \kl{CIC}
is a challenge in itself.

\AP We can address both issues by combining recent advances in type theory:
the reasonably exceptional type theory \intro{RETT} \sidecite{Pedrot2019}
and the observational type theory \intro{TTOBS} \sidecite{Pujet2022}.
First, \kl{RETT} supports consistent reasoning about exceptional terms.
It features a layer of possibly exceptional terms,
and a separate layer of pure terms in which raising an exception is prohibited.
This way, the consistency of the logical layer is guaranteed,
while allowing non-trivial interaction with the exceptional layer.
Technically, the two layers are defined using two distinct universe hierarchies.
%
Second, based on the seminal work on Observational Type Theory \sidecite{Altenkirch2007},
\kl{TTOBS} provides a setoidal equality in a specific universe $\intro*\SProp$
of definitionally proof-irrelevant propositions.
This universe of strict propositions, introduced by \sidecite{Gilbert2019}
and supported in recent versions of \kl{Coq} and \kl{Agda},
makes it possible to define an extensional notion of equality,
while trivializing the so-called higher coherence hell by imposing that any two proofs
of a given equality are \emph{convertible}.
% The resulting theory is arguably much simpler and closer to the current practice of proof assistants than
% cubical type theory~\cite{cubicaltt,cubical-agda}, which is another approach to provide extensional principles with computational content.

A major insight of \kl{CCICPrec} is to realize that we can actually merge
the logical universe of \kl{RETT} used to reason about exceptional terms
with the universe $\SProp$ of proof-irrelevant propositions in order to
define an internal notion of precision that is extensional and whose proofs
cannot be trivialized with exceptional terms.

\paragraph{Applications of internal precision}

In addition to supporting reasoning about the graduality of terms in a theory
that is not globally gradual, \kl{internal precision} makes it possible
to support gradual subset types, in which a type can be refined
by a proposition expressed using precision.
Moreover, in the literature, exception handling is never considered
when proving \kl{graduality} because this mechanism inherently allows terms that
do not behave monotonically with respect to \kl{precision}.
\kl{Internal precision} enables us to support exception handling in the impure layer
of the type theory,
and to consistently reason about the graduality (or not) of exception-handling terms.


\chapter{Mathematics 2}

This chapter contains skipped mathematical details.
Theoretical physicists may have to care.
Engineers don't have to.

\section{Deriving the Lorentz transformation}

\label{sec:derive-lorentz-transform}

\index{Lorentz transformation!derivation}%
We can derive the Lorentz transformation in many ways,
but I think the most elegant derivation is \cite{pal2003nothing}.
It does so without assuming the constancy of the speed of light.
You \emph{should} read that paper to understand special relativity.
It's very readable.
I think every physics text should be at least as readable as that.
Next, read \cite{gannett2007nothing} which improves that paper.

Other derivations%
\footnote{\url{https://en.wikipedia.org/wiki/Lorentz_transformation\#Derivation_of_the_group_of_Lorentz_transformations}}.

% From E = mc2 to the Lorentz transformations via the law of addition of relativistic % velocities
% http://iopscience.iop.org/article/10.1088/0143-0807/26/4/006

% Unified derivation of the Galileo and the Lorentz transformations
% http://iopscience.iop.org/article/10.1088/0143-0807/3/2/008

% Yet another unified derivation of galilean and Lorentz transformations
% http://iopscience.iop.org/article/10.1088/0143-0807/4/4/110

\section{Differential 1-form}

% practical introduction to differential forms
% https://www.cefns.nau.edu/~schulz/diff.pdf

% primer on differential forms
% https://arxiv.org/pdf/1206.3323.pdf

% mathematics for physics
% http://www.goldbart.gatech.edu/PostScript/MS_PG_book/bookmaster.pdf

In \( dx(t) = a(t) \cdot dt(t) \),
each of \(dx\), \(a\), and \(dt\) is a \emph{function of \(t\)}.
We lift multiplication from real numbers to function space \( f \cdot g = t \to f(t) \cdot g(t) \),
and we write \( dx = a \cdot dt \).

\section{Limit}

(Delete this. Use non-standard (infinitesimal) analysis instead.)

\index{definitions!limit}%
Practically,
the \emph{limit of \(E\) as \(h\) approaches \(a\)}, written \( \lim_{h \to a} E \),
is what the expression \( E \) converges to
if \(h\) is taken from a sequence that converges to \(a\).

\section{Integral}

\subsection{Measure of real interval}

Let \( a, b: \Real \).
Let \(a \le b\).

The notation \( [a,b] \) describes the set of all real numbers between \(a\) and \(b\), including both.
Formally, \( [a,b] = \{ x ~|~ a \le x ~\wedge~ x \le b \} \).

\index{definitions!measure}%
Its \emph{measure} is \( \mu([a,b]) = b - a \).

The measure of a set describes how big that set is.

Do not confuse \emph{measure} with \emph{cardinality} (how many elements a set has).

\subsection{Partitioning a set}

\index{definitions!partition (to partition a set)}%
To \emph{partition} a set is to divide it into disjoint subsets.
A partitioning of a set \(S\) into \(n\) partitions is \((S_1,\ldots,S_n)\) such that
every pair of partitions is disjoint and the union of all of partitions is \(S\).

\subsection{Riemann integral}

% FIXME: COIK (clear only if known)
\index{definitions!integral}%
Practically, the \emph{integral} of a function \(f\) in \(A\)
is the area between the curve of the function and the x-axis that coincides with \(A\).
Let \( f : \Real \to \Real \).
Partition \(A\) into \(n\) partitions \(A_1,\ldots,A_n\).
For each \(k\), pick one point \(a_k \in A_k\).
\index{definitions!Riemann sum}%
A \emph{Riemann sum} is \( S_n = \sum_{k=1}^n f(a_k) \cdot A_k \).
\index{definitions!Riemann integral}%
The \emph{Riemann integral of \(f\) in \(A\)} is
\( \int_A f = \lim_{n \to \infty} S_n \).

\section{Calculus}

\subsection{Antiderivative}

% https://en.wikipedia.org/wiki/Antiderivative
Let \( f = d(F) \).

Then \( f \) is \emph{the derivative} of \( F \).

\index{definitions!antiderivative}%
Then \( F \) is \emph{an antiderivative} of \(f\).

A function has many antiderivatives.
Let \(c\) be a constant.
If \(F\) is an antiderivative of \(f\), then so is \(F + c\).

Interpretation:
\( (d(f))(x) \) is the rate of change of \(f\) at \(x\).
\( (d(f))(x) \) is the slope of the tangent line of \(f\) at \(x\).

\section{Differential equation}
\label{sec:diff-eqn}

We overload the notation 0 to mean not only the number zero but also the constant function \( x \to 0 \).

\index{definitions!differential equation}%
A \emph{differential equation} is an equation that has the derivative operator \(d\).
Example: \( f + d(d(f)) = 0 \).
Let \(e\) be the base of natural logarithm.
Example: \( f(x) = e^x \) is one of the solutions of \( f = d(f) \).

\section{Calculus of variations?}

\subsection{Path, functional of a path, calculus of variations, Euler\textendash{}Lagrange equation}

\index{definitions!path}%
A \emph{path} in \( \Real^n \) can be described by a function \( f : \Real \to \Real^n \).
The path is then the set \( \{ f(a) ~|~ a \in \Real \} \), that is the \emph{image} of \(f\).
We overload notation.
\( (f+g)(x) = f(x) + g(x) \).

A \emph{functional} has type \( (\Real \to \Real^n) \to \Real \).

\section{Differential form, 1-form, 2-form}

% https://en.wikipedia.org/wiki/Differential_form
% prerequisite for:
% - sympletic manifold
% - K\"ahler manifold
% - Calabi\textendash{}Yau manifold

\section{Relating derivatives, tangent lines, best linear approximations}

\section{Generalizing vector spaces}

(We don't seem to need vector space over a field beyond \(\Complex\) in this book.
We don't need this level of generality in this book.
We should remove this paragraph.)
Let \(F\) be a field (in abstract algebra).
A \emph{vector space over \(F\)} is a space \( V \), a \emph{vector addition} operator \( + : V \to V \to V \),
a \emph{scalar multiplication} operator \( \cdot : F \to V \to V \), and the \emph{vector space axioms},
which can be seen in the \enquote{Definition} section in the Wikipedia article for \enquote{vector space}.
A \emph{vector} is a point in \( V \).
If \( F = \Real \), then \( V \) is called a \emph{real vector space}.
If \( V = \Real^n \), then \( V \) is called the \emph{\(n\)-dimensional real vector space}.
Do not confuse the field in physics (multivariate function) with the field in abstract algebra.

\section{Defining the derivative using infinitesimals}

\footnote{\url{https://en.wikipedia.org/wiki/Non-standard_calculus}}

\index{definitions!derivative (by infinitesimal)}%
\( d(f) = x \to st\left(\frac{f(x+\delta)-f(x)}{\delta}\right) \)

\section{Defining derivative using limit}

Let \( f : \Real \to \Real \).

\index{definitions!derivative}%
The \emph{derivative of \( f \) at \(x\)} is \( \dd(f,x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h} \).

The type of \(\dd\) is \( (\Real \to \Real) \to (\Real \to \Real) \),
which means that \(\dd\) takes a function and gives a function.

\section{Understanding the derivative as the best linear approximation at a point}

\( f(x+h) \approx f(x) + h \cdot f'(x) \)

Using this, we can approximate \(\sqrt{x}\) near a point we know,
we can approximate \(\sin(x)\) and \(\exp(x)\) for small \(x\).

\section{Understanding partial derivatives}

% https://en.wikipedia.org/wiki/Automatic_differentiation
% Symbolic differentiation: https://en.wikipedia.org/wiki/Computer_algebra
% https://en.wikipedia.org/wiki/Numerical_differentiation

Let \( f : \Real^n \to \Real \).

\index{definitions!partial derivative}%
The \emph{partial derivative of \(f\) at \( x \) with respect to the \(k\)th input}
is \( d_k(f,x) = \lim_{h \to 0} \frac{f(x + h e_k) - f(x)}{h} \).

The type of \(d_k\) is \((\Real^n \to \Real) \to (\Real^n \to \Real)\),
which means that \( d_k \) takes a function and gives another function.

\section{Understanding the gradient}

Let \(f : \Real^n \to \Real\).

\index{definitions!gradient}%
The \emph{gradient of \(f\) at \(x\)} is the vector \( (\nabla f)(x) \) whose \(k\)th component is \( ((\nabla f)(x))_k = d_k(f,x) \).
Note that \( (\nabla f)(x) \) is a vector in \( \Real^n \) and \( (\nabla f) : \Real^n \to \Real^n \).

The type of \(\nabla f\) is \(\Real^n \to \Real^n\).

\section{Understanding total derivative}

\section{(Does not belong in this chapter)}

Let \( M \) be a \emph{point mass} with \emph{mass} \( m : \Real \) and \emph{velocity} \( v : \Real^n \).
Its \emph{momentum} is \( p = m \cdot v \).

% https://en.wikipedia.org/wiki/Mechanical_equilibrium
In statics, a system is in \emph{static equilibrium} iff it is at rest and it stays at rest.

The \emph{momentum} of an object is the difficulty of stopping it.

An object is \emph{at rest} iff its velocity is zero.
To \emph{stop} an object (to put it to \emph{rest}) is to make its velocity zero.
\emph{Stationary} is another word for \emph{at rest}.


\newsection
\subsection{Environment}
\label{sec:environment}
\sectionauthors{Peter Henderson, Lauren Gillespie, Dan Jurafsky}

\begin{figure}[!ht]
\centering
\includegraphics[width=\linewidth]{society/figures/Environment_Equation.png}
\caption{\label{fig:environment} A visualization of a cost-benefit analysis for deploying a foundation model. The total value of a model can be approximated by first considering the net positive social benefits of the model, as well as any environmental benefits. Then, we subtract the negative energy costs to train and deploy the model, the social cost of the carbon emitted to train the model, and the secondary environmental effects. If the net costs outweigh the benefits, then foundation model developers and large-scale deployers should consider harm reduction strategies. This could include deploying a more efficient model or not deploying the model at all. }
\end{figure}

Foundation models can potentially lead to many social and environmental benefits, for example in legal domains (\refsec{law}), healthcare (\refsec{healthcare}), or even tackling climate change~\citep{rolnick2019tackling}. But because of their scale, they themselves can negatively impact the environment through increased carbon emissions if model creators are not careful~\citep{strubell2019energy,lottick2019nergy,schwartz2019green,lacoste2019quantifying,cao2020towards,henderson2020towards,bender2021,patterson2021carbon,lannelongue2021green,parcollet2021energy}. Addressing such emissions is an imperative: current forecasts show that climate change is occurring more rapidly than previously thought~\citep{ipcc2021}.

To understand where such emissions can occur in foundation models, we consider their lifecycle. First, they are trained on vast amounts of data, possibly for up to months of time and often distributed across hundreds to thousands of GPUs. Afterwards, they may be adapted to new domains or perhaps distilled into smaller models. All of this can be considered part of the training regime. Models used purely for research may not move beyond these steps.
After models have been adapted and/or distilled, they might move on to be deployed into production. At this point many rounds of inference will run through the model until a new model is trained and the cycle repeats.

Each one of these steps has the potential to utilize large amounts of energy and can contribute to carbon emissions. Foundation models can generate large, one-time energy costs and carbon emissions during the initial training phase. For example, the amount of emissions from training one BERT-base model, under some conditions, would only be offset by 40 trees grown for 10 years.\footnote{\citet{strubell2019energy} calculate carbon emissions for training BERT on an average energy grid in the U.S. and we use \url{https://www.epa.gov/energy/greenhouse-gas-equivalencies-calculator} to convert that to equivalent emissions in other domains. We note that this number can vary depending on the energy grid and other considerations~\citep{henderson2020towards,patterson2021carbon}.}
And if deployed at scale, foundation models can require substantial energy to service millions of requests\footnote{For example, transformers are already used at scale for search both at Microsoft and Google. See \url{https://www.blog.google/products/search/search-language-understanding-bert/} and  \url{https://azure.microsoft.com/en-us/blog/microsoft-makes-it-easier-to-build-popular-language-representation-model-bert-at-large-scale/}.}\dash{}translating to large carbon emissions if nonrenewable resources are used.

Therefore, the environmental impacts of certain design decisions for both training and deploying foundation models can be substantial.
Even seemingly minuscule decisions, like reducing the number of layers a model has, may lead to significant environmental cost reductions at scale.
For example, based on calculations from \citet{henderson2020towards}, a slightly more energy efficient translation model deployed at the scale of a commercial translation service could save between 78 kgCO2eq and 12,768 kgCO2eq of carbon emissions \textit{per day} depending on the energy grid used.
This is roughly equivalent to the carbon sequestered by 1 to 211 trees grown for 10 years, or the carbon sequestered by .35 to 57.4 acres of forest in one year.\footnote{Sequestration estimated via \url{https://www.epa.gov/energy/greenhouse-gas-equivalencies-calculator}, but may be larger depending on other estimation methods. More efficient energy grids will emit less carbon, resulting in wide estimated ranges of impacts.}
Thus the design, deployment, and post-deployment monitoring of foundation models should adequately reflect these risks.

There are of course uncertainties in calculating the amount of energy used or carbon emitted by any given model~\citep{henderson2020towards,cao2020towards,patterson2021carbon}, and other sources of emissions may currently be much greater than those generated by foundation models~\citep{mora2018bitcoin}. But if foundation models continue to scale and gain in popularity, they may very well become a significant contributor to carbon emissions.
Our goal is to provide a framework for foundation model developers and large-scale deployers\footnote{We focus on model developers and large-scale deployers, like those who build production systems on top of foundation models, because they are most able to make meaningful changes to reduce energy use and carbon emissions. A single change by these actors\dash{}like using a more efficient model\dash{}can scale to massive carbon savings, which would otherwise require a massive campaign to reach all downstream model users.} to consider how they can mitigate any unnecessary carbon emissions and keep the net social impact of these models positive.
We recommend that:
\begin{enumerate}
    \item Carbon impacts can and should be mitigated in many cases. This can be accomplished by training models in low-carbon intensity regions,
    or by using more efficient models and hardware (\refsec{environment-mitigation}).
    \item When all mechanisms for mitigation have been exhausted and mitigation is no longer possible,
    the costs and benefits to society should be assessed
    to determine if and when a larger foundation model should be deployed over a smaller, more efficient, model
   \dash{}with the understanding that the up-front costs of a large foundation model may be amortized over the lifetime of the model (\refsec{environment-costs}).
    \item Energy, computational, and carbon costs\dash{}as well as any efforts taken to mitigate negative impacts\dash{}should be clearly reported to inform policymaking and research (\refsec{environment-reporting}).
\end{enumerate}

\subsubsection{Carbon impacts can and should be mitigated in many cases}
\label{sec:environment-mitigation}
The  carbon impacts of training foundation models differ from the impacts of deploying them for inference.
Model training has no latency requirements, so  training  can be moved across energy grids with relative ease in cloud environments.
Every energy grid has its own carbon intensity\dash{}the amount of carbon emitted per kilowatt-hour of energy used. For example, Qu\'ebec has an extremely low carbon intensity due to its reliance on hydroelectricity, while Estonia's energy grid has an extremely high carbon intensity due to its reliance on shale oil (though that is changing quickly)~\citep{henderson2020towards}.
Recent research has even suggested that the top 5\% of polluting power plants contributed 73\% of all electricity-based emissions~\citep{grant2021reducing}.
Thus, while training foundation models can be quite energy intensive, researchers have demonstrated that the carbon impacts of these models can be partly mitigated by \textit{selecting energy grids with minimal carbon emissions}~\citep{henderson2020towards,lacoste2019quantifying,patterson2021carbon}.

\textit{Carbon offsets} have also been proposed as a stopgap until carbon-free renewable electricity is available at all data centers. This strategy involves reducing carbon emissions in one activity to offset the emissions from another. But most\dash{}if not all\dash{}carbon offsets are a strictly worse solution than not emitting CO$_{2}$ in the first place \cite{holl_tree_2020}. Some carbon offset programs can even have a negative impact. For example,  studies of forest planting campaigns (often a source of carbon offsetting) show that they can do more harm than good. They can yield monocultures (the use of one particular species of tree) that diminish the biodiversity of the region and reduce carbon storage in the forest soil~\citep{heilmayr2020impacts,hong2020divergent}. This could result in more carbon emissions when using carbon offsets than if the original carbon had never been emitted in the first place.
Therefore, when training or deploying a foundation model, we recommend designing for as little carbon emission as possible up-front, rather than simply relying on carbon offsets to cancel emissions.

When it is not possible to run in low-carbon regions, other mitigation strategies should be leveraged, reducing unnecessary energy usage. This includes:
\begin{itemize}
    \item using more efficient hardware,\footnote{Notably, California now regulates computers with inefficient GPUs for this reason, requiring that they stay below 30-100 kWhs/year, depending on the manufacturing date and computer type. \emph{See} Sections 1601-1608 of California’s Appliance Efficiency
Regulations (Title 20).}
\item using mixed-precision training~\citep{micikevicius2017mixed} or quantization~\citep{gholami2021survey},
\item using more efficient architectures (\eg using an evolved transformer over a vanilla transformer architecture; or using sparse models)~\citep{pmlr-v97-so19a,patterson2021carbon,mostafa_parameter_2019},
\item distilling models and using distilled models (\eg~\citep{sanh2019distilbert}),
\item and utilizing other optimization strategies that will reduce energy costs (see more discussion in \refsec{systems}).
\end{itemize}

Maintainers of open source projects and cloud compute should strive to set their default settings to the most efficient possible, since ``green defaults'' are known to be the most effective mitigation strategies (see discussion in ~\citep{henderson2020towards}).
Other mitigation strategies can be found in recent literature~\citep{strubell2019energy,lacoste2019quantifying,schwartz2019green,henderson2020towards}.
We also note that reducing and mitigating energy usage also has the added benefit of making models more accessible to those with limited compute access (see \refsec{ethics} for more discussion).

However, when a model is mainly used for inference, \eg  deployed in a production application, it often cannot be moved to a less carbon-intensive energy grid for low-latency applications.
In addition to using the mitigation strategies specified above, in this case it is important to weigh
the benefits of the proposed foundation model versus a more energy efficient alternative. We discuss this further in the subsequent section.

\subsubsection{Costs and benefits should be assessed before using foundation models}
\label{sec:environment-costs}

After taking as many steps as possible towards mitigation (or where mitigation is not possible),
it is vital to assess the required size of a foundation model\dash{}or whether a foundation model should be used at all. This cost-benefit analysis should consider:

\begin{enumerate}
    \item Is the social cost and environmental cost from deploying the foundation model greater than the social benefit of the model?
    \item Would another, computationally simpler and cheaper approach achieve comparable social benefit (\eg a much more efficient foundation model, or perhaps simple baseline)?
\end{enumerate}

A simplified scheme for assessing this trade-off considers the overall impact of a model $M$ as:

\begin{equation}
\label{eqn:env}
    V(M) = S(M) - C(M) - E(M) - O(M).
\end{equation}

\reffig{environment} represents this equation and the costs and benefits that may enter each variable. Here, $M$ is the model and $S$ is the net social benefit, as well as environmental benefit, in dollars. $S$ can be increased by improving healthcare, access to justice, decreasing poverty, improving environmental monitoring, aiding ecosystem conservation efforts, and so on.

$C$ is the social cost of carbon from energy use. This represents the future harm to society from the carbon released as a present-day monetary value. The upper bound U.S. Environmental Protection Agency (EPA) estimate from 2017 for the social cost of carbon was \$105 (in 2007 U.S. dollars) per metric ton of $\text{CO}_2$ emitted.\footnote{See \url{https://19january2017snapshot.epa.gov/climatechange/social-cost-carbon_.html}. But note that the social cost of carbon can be a contentious metric~\citep{stern2021social}. By using a favorable discount factor, one can reduce carbon costs. As such, it can the calculation of this metric can vary across methodologies.}

$E$ is the energy cost of the model. For example, in April 2021, the average U.S. residential energy cost was about \$0.1376 per kWh.\footnote{\url{https://www.eia.gov/electricity/monthly/epm_table_grapher.php?t=epmt_5_6_a}} Added to this variable could be the costs from increased strain on the energy grid. For example, a recent study suggested that the cost per energy grid interruption event, normalized by average demand, could be as high as \$15.9 per average kW~\citep{sullivan_updated_2015}.\footnote{Like the social cost of carbon, calculation of these costs can fluctuate across modeling methodologies.}

$O$ is the social cost of other second order environmental effects. This could include:
\begin{itemize}
    \item The compounding carbon impacts from increased chip demand and chip production~\citep{gupta2021chasing}.
    \item Other environmental impacts of chip manufacturing, like the creation of toxic waste sites in
 Silicon Valley, whose health effects are unequally distributed to socially vulnerable populations~\citep{stewart2014uneven}, or pollution from manufacturing in Taiwan that has been linked to chronic health problems~\citep{tu2009ineffective,lin2016increased}.
 \item The compounding effects of climate change that are not already included in the SCC model. For example, these effects could include accelerated desertification~\cite{huang_accelerated_2016}, rapid ecosystem changes that put many species at risk of extinction~\cite{urban_accelerating_2015}, and increased carbon emissions due to melting permafrost~\citep{schuur_climate_2015}.
 \item Unnecessary strain on chip production capacities. Recent chip shortages have led to work stoppages in automobile manufacturing.\footnote{\url{https://www.reuters.com/business/autos-transportation/ford-shut-some-n-american-plants-few-weeks-chip-shortage-2021-06-30/}} There is no evidence to suggest that increasing demand for ML-optimized chips led to this shortage.\footnote{Though recent reports have suggested that demand for datacenter chips have surpassed the gaming sector. \emph{See} \href{https://www.nextplatform.com/2020/08/21/the-local-maxima-ascension-of-datacenter-at-nvidia/}{https://www.nextplatform.com/2020/08/21/the-local-maxima-ascension-of-datacenter-at-nvidia/}.}
 But such considerations fall into second order effects, where researchers might weigh whether the risks, however slight, of contributing to such negative impacts are worth using or deploying a large model.\footnote{Like for other metrics described previously, there is uncertainty as to how these impacts might be calculated and attributed to models.}
\end{itemize}

It is important to consider in this analysis that the economic benefits and social costs of carbon could be distributed unequally across communities, with poorer communities being impacted more heavily by climate change and wealthier communities being benefited by a model~\citep{bender2021}.\footnote{See also, \url{https://www.un.org/sustainabledevelopment/blog/2016/10/report-inequalities-exacerbate-climate-impacts-on-poor/} and \url{https://blogs.imf.org/2020/12/02/how-artificial-intelligence-could-widen-the-gap-between-rich-and-poor-nations/}.} As such, when conducting the Equation~\ref{eqn:env}  analysis, one should consider the benefits and harms to society more broadly rather than for a given organization or country. In this case $V(M)$ can be viewed as a distribution and should ideally be evenly distributed across the population. In cases where the distribution is highly uneven\dash{}for example where all the benefits fall to the model designer while all the harms fall to populations that will never benefit from the model\dash{}the designer should spend substantially more effort on mitigation before deploying the model.

There is, of course, some uncertainty in which methodology to use when valuing each component of Equation~\ref{eqn:env}.
Empirical estimates for many of these terms can range in multiple magnitudes depending on the data source and modeling choice for the phenomena, such as the different mechanisms for evaluating the social cost of carbon. The key takeaway of this cost-benefit analysis, however, is not the dollar valuation of each term in the equation, but rather the \emph{existence of} and relative importance of each of these effects.
Our goal is to provide a high-level framework for beginning to consider these trade-offs.
Future research may give more guidance on how to quantify each of these values.

\begin{figure}[t]
    \centering
    \includegraphics[width=.55\textwidth]{society/figures/Environment_Plot.png}
    \caption{A hypothetical example of amortized fine-tuning showing the point at which a foundation model (in this case BERT Base) will have lower energy costs than a transformer model trained from scratch. We estimate the up-front energy cost for training BERT from~\citet{strubell2019energy}, and cost for fine-tuning a downstream task from~\citet{chaudhary2020topicbert}. We compare against the linearly increasing cost of training a transformer from scratch, from~\citet{strubell2019energy}. If BERT is used for less than $\sim$80 tasks, the up-front energy costs are not recovered. After that point, BERT is  more energy efficient than the model trained from scratch.}
    \label{fig:env_bert_fine-tune}
\end{figure}

Finally, we note that these factors should also be evaluated over the lifetime of the model, not on a per-run basis. Consider an alternative baseline model that must be trained from scratch for every new task. The baseline may well require an expensive hyperparameter search to achieve equivalent performance on downstream tasks. In contrast, the foundation model places the brunt of the costs on the initial pretraining procedure, with fine-tuning perhaps being much simpler and more energy efficient. Over the lifetime of the foundation model, it could be more carbon efficient than the baseline  (\reffig{env_bert_fine-tune}).
Even more efficient adaptation mechanisms could improve this amortization further (see \refsec{adaptation}).

The efficiency of adaptation, however, is not guaranteed. It may be true that some foundation models will never be more efficient than a particular baseline, even when amortized over many tasks.
For example, it cannot be assumed that a smaller model with fewer parameters will translate to energy efficiency improvements. Due to increased hyperparameter tuning costs or other optimizations, the number of parameters has been shown not to correlate with energy efficiency in some cases~\citep{zhou2020hulk,henderson2020towards}.
Therefore, foundation model developers should rigorously assess the efficiency of their models and adaptation mechanisms before beginning large-scale training efforts.

The framework in this section is meant to guide the reader in thinking about the environmental and societal trade-offs in training and deploying their model, but there are other substantial social justice considerations involved in deploying a foundation model, discussed in \refsec{ethics}.
\refsec{economics} also discusses in more detail the dynamics of social welfare from algorithm deployment.

\subsubsection{Carbon/energy impacts should be systematically reported}
\label{sec:environment-reporting}

A cost-benefit analysis cannot be conducted unless researchers and engineers working on foundation models report the computational, energy, and carbon costs of their models.
We encourage foundation model developers, providers, and curators to report these metrics, as well as what carbon reduction strategies were used in the making of the foundation model.
See \citep{henderson2020towards,lottick2019nergy,lacoste2019quantifying,codecarbon,anthony2020carbontracker} for examples of a Carbon Impact Statement and for tools that can facilitate this reporting.
For researchers, such reporting can occur at publication time, but we also encourage industry actors to adopt transparency mechanisms to report these metrics for their deployed models.\footnote{A small step toward this has been taken by some cloud compute providers that identify the most carbon friendly cloud regions. See, for example, \href{https://cloud.google.com/blog/topics/sustainability/pick-the-google-cloud-region-with-the-lowest-co2}{https://cloud.google.com/blog/topics/sustainability/pick-the-google-cloud-region-with-the-lowest-co2}.}
This will help set policy recommendations within industry and academia, as well as help downstream users identify carbon-friendly usage patterns.
Standardized reporting will also aid in determining which models are accessible to those with limited compute access (see \refsec{ethics} for more discussion on accessibility).

To encourage more reporting of energy and carbon impacts, we suggest, among other strategies: giving green badges at conferences, requiring reporting of relevant metrics for submission to conference venues, lobbying large-scale deployers of foundation models to provide more transparency, and generally shifting professional norms in academia and industry towards standard reporting of these metrics (see more discussion on professional norms in \refsec{ethics} and more discussion on reporting mechanisms by \citet{henderson2020towards}).


\newcommand{\eg}{{\em e.g.~}}
\newcommand{\ie}{{\em i.e.~}}
\newcommand{\wrt}{w.r.t.~}
\newcommand{\co}[2]{\langle #1, \: #2 \rangle}

\section{Decision procedures}
\label{sec-decide}
\label{system:sec}
A detailed description of the main decision procedures of {\tt GETFOL}
is given in \cite{armando5}.

The set of procedures of the {\tt GETFOL} system is depicted in figure
\ref{system:fig}.
Each box represents either a decider ({\tt PTAUT}, {\tt PTAUTEQ},
{\tt FOLTAUT}) or a rewriting procedure ({\tt tautren}, {\tt  phexp},
{\tt  reduce}).

\begin{figure}
\begin{center}
\makebox[3.375in][l]{
  \vbox to 2.750in{
    \vfill
    \special{psfile=decide/NEWFIG.PS}
  }
  \vspace{-\baselineskip}
}
\end{center}
\caption{The system of deciders}
\label{system:fig}
\end{figure}

\subsubsection*{{\tt PTAUT} and {\tt PTAUTEQ}}
{\tt PTAUT} and {\tt PTAUTEQ}
are deciders working on a quantifier-free first order language (hereon by
abuse of language we call them propositional deciders).
{\tt PTAUT} decides the set of first order formulas provable using
only the propositional deductive machinery (moreover it returns a
falsifying assignment whenever the input formula is not a tautology).
For instance, the formula $(P(x)\con R(x,b))\imp (P(x)\dis R(x,b))$ can be
easily inferred by a single application of {\tt PTAUT}.
{\tt PTAUT} is a generalization of the Davis-Putnam-Loveland procedure
(hereon DPL) \cite{davis2,davis6} to non clausal formulas.
The core of {\tt PTAUT} is a procedure capable of partially evaluating
the input formula \wrt a partial assignment of truth-values to the atomic
subformulas.
A step of statistical analysis (of polynomial time complexity) collects
information about the {\em polarity} of the subformulas and the existence
of {\em Top-Level Disjunctive Occurrences} (TLDO) of atomic subformulas.
A formula $\alpha$ occurs as a TLDO in $\beta$
if and only if $\beta$ can be rewritten into a formula either of the form
$(\alpha\dis\gamma)$ or $(\neg\alpha\dis\gamma)$ by means of rules
expressing  the usual properties of the propositional connectives such as
the associativity, commutativity and distributivity of the propositional
connectives.
The notion of positive (negative) subformula occurrence
is inductively defined as follows: $\alpha$ occurs positively in $\alpha$,
$\alpha$ occurs negatively in $\neg\alpha$;
$\alpha$ and $\beta$ occur positively in $(\alpha\con\beta)$ and
$(\alpha\dis\beta)$;
$\alpha$ occurs negatively and $\beta$ occurs positively in $(\alpha\imp\beta)$;
finally $\alpha$ and $\beta$ occur both positively and negatively in
$(\alpha\liff\beta)$.
A subformula $\alpha$ is positive (negative) in $\beta$ if and only if
each occurrence of $\alpha$ occurs positively (negatively) in $\beta$.

The statistical analysis may suggest a partial assignment $\mu$ (\wrt which
the formula can be simplified) according to the following criteria:
\begin{itemize}
\item for each positive (negative) atomic formula $\alpha$ occurring
in $\beta$, $\mu(\alpha)=F$ ($\mu(\alpha)=T$);
\item if $\beta$ contains a positive (negative) TLDO of $\alpha$ and there
are no negative (positive) TLDO of $\alpha$, then $\mu(\alpha)=F$
($\mu(\alpha)=T$).
\end{itemize}

If $\mu$ is not completely undefined, then {\tt PTAUT} simplifies the
formula in input \wrt $\mu$ and recurs on the resulting (simplified) formula.
If the input formula contains both a positive and a negative TLDO of an
atomic formula the input formula is a tautology.
These optimizations generalize the {\em Affirmative-Negative Rule} and the
{\em Rule for the Elimination of One-Literal Clauses} of DPL.
If $\mu$ is totally undefined, then
an atomic formula is chosen, two partial assignments are created
(one assigning $T$, the other $F$ to the chosen atomic formula),
the formula is partially evaluated \wrt such assignments and finally
the procedure branches recurring on the two simplified formulas.
This last step generalizes the {\em Splitting Rule} of DPL.

{\tt PTAUTEQ} is the result of adapting {\tt PTAUT}
to take into account the properties of equality.
The main difference is that, before a formula is simplified \wrt some
assignment, the assignment is tested to check whether it is model of the
quantifier-free theory of equality.
The formula $(P(x)\con x=y)\imp (P(y)\con y=x)$ can be
easily inferred by a single application of {\tt PTAUTEQ}.

\subsubsection*{{\tt nnf} and {\tt skolemize}}
{\tt nnf} rewrites the input formula into a logically equivalent one in
{\em negative normal form}.

{\tt skolemize} computes the skolemization of the input formula.

\subsubsection*{{\tt tautren} and {\tt phexp}}
The procedures on top of the propositional deciders (namely {\tt tautren}
and {\tt phexp}) map the first-order formula in input into a quantifier-free
formula.
The mappings are such that the decision problem of the input (first-order)
formula is related to the decision problem of the returned (quantifier-free)
formula in a useful way.
In particular, {\tt tautren} atomizes equal (modulo renaming of bound
variables) quantified subformulas into newly introduced propositional
letters.
For instance the formula
\begin{equation}\label{pb29-reduced}
%\setlength{\templength}{\arraycolsep}
\setlength{\arraycolsep}{0cm}
\begin{array}{rl}
(\exists x.F(x) \con \exists x.G(x)) \imp (&( \forall x.(F(x) \imp H(x)) \con \forall x.(G(x) \imp J(x))) \liff \\
& ((\exists y.G(y) \imp \forall x.(F(x) \imp H(x))) \con\\
&\ (\exists x.F(x) \imp \forall y.(G(y) \imp J(y)))))
\end{array}
\end{equation}
is mapped into the propositional formula
\begin{equation}\label{pb29-prop}
(A \con B) \imp ((C \con D) \liff ((B \imp C) \con (A \imp D)))
\end{equation}
The relation between the decision problems of the input formula (say $\alpha$)
and of the output formula (say $\alpha'$) is that
$\der{}\alpha'$ only if $\der{}\alpha$.

A more careful reduction to the quantifier-free fragment is performed by
{\tt phexp}.
{\tt phexp} maps an existential formula $\alpha$ into a quantifier-free formula
$\alpha'$ such that $\der{}{\alpha'}$ if and only if $\der{}{\alpha}$.%
\footnote{The set of existential formualas is the class of prenex
universal-existential formulas without function symbols.}

The formula $\alpha'$ is an improved version of the Herbrand's expansion
of $\alpha$ \cite{dreben1}.
An application of {\tt phexp} to the following formula:
\begin{equation}\label{pb28}\small
    (((P(x) \con \neg Q(y)) \dis
     ((Q(a) \dis R(a)) \con (\neg Q(b) \dis \neg S(b)))) \dis
     ((F(z) \con \neg G(z)) \con S(v))) \dis
       ((\neg P(c) \dis \neg F(c)) \dis G(c))
\end{equation}
yields
\begin{equation}\label{pb28-exp}\small
\begin{array}{l}
((((P(a) \dis P(b) \dis P(c)) \con (\neg Q(a) \dis \neg Q(b) \dis
\neg Q(c)))\dis\\
((Q(a) \dis R(a)) \con (\neg Q(b) \dis \neg S(b)))) \dis \\
(((F(a) \con \neg G(a)) \dis (F(b) \con \neg G(b)) \dis
(F(c) \con \neg G(c)))\con\\
(S(a) \dis S(b) \dis S(c)))) \dis ((\neg P(c) \dis \neg F(c)) \dis G(c))\\
\end{array}
\end{equation}
In \cite{armando5} it is shown that, the size of (\ref{pb28-exp})
is 44 times smaller than the size of the standard Herbrand's expansion of
(\ref{pb28}).

\subsubsection*{{\tt reduce}}
{\tt reduce} tries a set of rewriting rules on the input
formula aiming at rewriting it into a logically equivalent formula that
can be easily turned into an existential one via skolemization.
The rewriting rules employed by {\tt reduce} are the usual rules
expressing the distributivity of quantifiers through propositional connectives
and the commutativity and associativity of propositional connectives
listed in the following table.\\

    \renewcommand{\arraystretch}{1.5}
    {\small
      $$
      \begin{array}{|c|rcl|} \hline
        (1) & Q x. \alpha[x] & \mapsto & \alpha \\ \hline
        %(2) & Q x. (\neg \alpha(x)) & \mapsto & (\neg \hat{Q} x. \alpha(x)) \\ \hline
        (2) & Q x. (\alpha \circ \beta)(x) & \mapsto & (Q x. \alpha \circ Q x. \beta)
        \\ \hline
        (3) & Q x. (\alpha[x] + \beta(x)) & \mapsto & (\alpha[x] + Q x. \beta(x))
        \\ \hline \hline
        (4) & (\alpha(x) + \beta[x]) & \mapsto & (\beta[x] + \alpha(x)) \\ \hline
        (5) & ((\alpha[x] + \beta(x)) + \gamma(x)) & \mapsto &
        (\alpha[x] + (\beta(x) + \gamma(x))) \\ \hline
        (6) & ((\alpha \circ \beta)(x) + \gamma(x)) & \mapsto &
        ((\alpha + \gamma(x)) \circ (\beta + \gamma(x))) \\ \hline
        (7) & (\alpha(x) + (\beta[x] + \gamma(x))) & \mapsto &
        (\beta[x] + (\alpha(x) + \gamma(x))) \\ \hline
        (8) & ((\alpha(x) + (\beta \circ \gamma)(x))) & \mapsto &
        ((\alpha(x) + \beta) \circ (\alpha(x) + \gamma)) \\ \hline
      \end{array}
      $$
      }
    \renewcommand{\arraystretch}{1}
{\small
{\em Restrictions}:
\begin{itemize}
\item In rules $\{(4)-(8)\}$ the left hand side must be a top normalizable
formula.
\item In rules $\{(7),(8)\}$ $\alpha$ must be minimal \wrt $\co{Q}{x}$.
%\item Rules $\{(4)-(8)\}$ can be applied only to subformulae (say $\alpha$)
%of a formula $Qx.\beta$ in which there is no proper
%subformula $Qy.\gamma$ of which $\alpha$ is a subformula.
\end{itemize}}

Where
$\alpha(x)$ denotes a formula in which there is at least one free occurrence
of the variable $x$.
$\alpha{[x]}$ denotes a formula in which there is no free occurrences of $x$.
$Q$ and $Q'$ stand either for $\forall$ or for $\exists$.
If $Q = \forall$, then $\circ = \con$ and $+ = \dis$.
If $Q = \exists$, then $\circ = \dis$ and $+ = \con$.
%$\con$ is said to be $\forall$-compatible and $\exists$-incompatible,
%$\dis$ is said to be $\exists$-compatible and $\forall$-incompatible.
%If $\cal S$ is a set of rewriting rules then $\mapsto_{\cal S}$ is the
%reducibility relation induced by $\cal S$ and
%$\stackrel{*}{\mapsto}_{\cal S}$ is the reflexive and transitive closure
%of $\mapsto_{\cal S}$.
The definition of {\em top normalizable formula} and of {\em minimal
formula} are given in \cite{armando5}.

For instance, a single application of {\tt reduce} turns the formula
\begin{equation}\label{pb29}
%\setlength{\templength}{\arraycolsep}
\setlength{\arraycolsep}{0cm}
\begin{array}{rl}
(\exists x.F(x) \con \exists x.G(x)) \imp (&( \forall x.(F(x) \imp H(x)) \con \forall x.(G(x) \imp J(x))) \liff \\
&(\forall x.\forall y.((F(x) \con G(y)) \imp (H(x) \con J(y)))))
\end{array}
\end{equation}
into (\ref{pb29-reduced}).
{\tt reduce} considerably enlarges the set of formulas which can be solved
by using the system of deciders.
In particular, any prenex first order formula
$$
\forall \vec{y}_n \exists \vec{x}_n \ldots
\forall \vec{y}_i \exists \vec{x}_i \ldots
\forall \vec{y}_1 \exists \vec{x}_1 . \Phi
$$
such that each literal in $\Phi$ contains no variables in $\vec{y}_k$ and
in $\vec{x}_l$ with $k < l$, or in $\vec{x}_k$ and in $\vec{x}_l$ with
$k \neq l$ can be ``reduced" to an existential formula.
On the basis of the previous result it is a trivial consequence
that the {\em monadic calculus} can be reduced to the existential fragment
by means of {\tt reduce}.