\chapter{Optimization Candidates}
\label{chap:candidates}
In order to optimize the theory graph we must first identify cases where it is possible to apply any optimizations. We call such theories \underline{optimization candidates} or just \underline{candidates}.
All candidates have in common that they are theories where inclusions can be removed or replaced with weaker inclusions, while still yielding a valid theory as a result without any undeclared symbols (assuming of course a valid theory graph at the start).
For something to be declared a candidates we will only require that the theory itself must remain valid, but not necessarily the stronger case that replacing the candidate with the optimized theory will result in a graph that is still valid as a whole. Of course these considerations will still be applied elsewhere.
\section{Types of optimization candidates}
\subsection{Simply redundant inclusion}
\label{sec:redinc}
An inclusion of a theory $A\hookrightarrow{}C$ is \underline{simply redundant}, if $C$ includes a theory $B$, such that $B\hookrightarrow{}C$ (see \autoref{fig:redundantbasic}).
\providecommand\myxscale{3.9}
\providecommand\myyscale{2.2}
\providecommand\myfontsize{\footnotesize}
\begin{figure}[!htb]
\centering
\begin{tikzpicture}[node distance=3cm]\footnotesize
\node[thy] (bottom) {\begin{tabular}{l}
\textsf{bottom}\\\hline
...\\\hline
...
\end{tabular}};
\node[thy, above of = bottom] (middle) {\begin{tabular}{l}
\textsf{middle}\\\hline
...\\\hline
...
\end{tabular}};
\node[thy, above of = middle] (top) {\begin{tabular}{l}
\textsf{top}\\\hline
...\\\hline
...
\end{tabular}};
\draw[include] (bottom) -- (middle);
\draw[include, bend left] (bottom) edge (top);
\draw[include] (middle) -- (top);
\end{tikzpicture}
\caption{Simply redundant inclusion example}
\label{fig:redundantbasic}
\end{figure}
Simply redundant inclusions can be safely optimized by simply removing the redundant inclusion, as seen in \autoref{fig:redundantoptimized}, without changing the flattened graph due to the transitive nature of inclusions.
\begin{figure}[!htb]
\centering
\begin{tikzpicture}[node distance=3cm]\footnotesize
\node[thy] (bottom) {\begin{tabular}{l}
\textsf{bottom}\\\hline
...\\\hline
...
\end{tabular}};
\node[thy, above of = bottom] (middle) {\begin{tabular}{l}
\textsf{middle}\\\hline
...\\\hline
...
\end{tabular}};
\node[thy, above of = middle] (top) {\begin{tabular}{l}
\textsf{top}\\\hline
...\\\hline
...
\end{tabular}};
\draw[include] (bottom) -- (middle);
\draw[include] (middle) -- (top);
\end{tikzpicture}
\caption{Example of simply redundant inclusion optimized}
\label{fig:redundantoptimized}
\end{figure}
The simply redundant inclusions in a theory T are the set $I_R(T) =$\\ $ (\bigcup_{I \in DirectIncludes(T)}Includes(I)) \cap DirectIncludes(T)$,\\
where $Includes(I)$ are the transitively included theories in I and $DirectIncludes(T)$ are the theories included directly in the theory T.
It is immediately obvious that removing a simply redundant inclusion I from a theory T also removes it from the above set via the intersection with $DirectIncludes(T)$.\\
Slightly less obvious is that doing so neither adds nor removes any other inclusions. This is because by design the set $\bigcup_{I \in DirectIncludes(T)}Includes(I))$ still includes I and any of its children and thus remains unchanged.
\subsection{Superfluous Inclusion}
\label{sec:superinc}
An inclusion of a theory $A\hookrightarrow{}B$ is \underline{superfluous}, if $B$ uses none of the constants declared in $A$. Such an inclusion can be \underline{purely superfluous} (\autoref{sec:puresi}) if it can be entirely removed, or \underline{partially superfluous} if it can be reduced to a subset of the theory inclusions in $A$ \autoref{sec:partiallysi}.\\
In both of these cases the inclusion can be replaced or removed entirely, while still yielding a well formed theory. However this changes the resulting flattened theory graph and can invalidate theories that include the optimized theory.\\
The superfluous inclusions in a theory T are the set $I_{S}(T) =$\\ $ \{ I \in DirectInludes(T) | I \notin UsedTheories(T) \}$,\\
where $DirectIncludes(T)$ are the theories included directly in the theory T and UsedTheories(T) are those theories whose terms are used in T.
\subsubsection{Purely Superfluous Inclusion}
An inclusion of a theory $A\hookrightarrow{}B$ is \underline{purely superfluous}, if $B$ uses none of the constants in $A$, not even if they were declared in a theory $C$ such that $C\hookrightarrow{}A$.\\
\label{sec:puresi}
\begin{figure}[!htb]
\centering
\begin{tikzpicture}[node distance=3cm]\footnotesize
\node[thy] (bottom) {\begin{tabular}{l}
\textsf{bottom}\\\hline
X\\\hline
...
\end{tabular}};
\node[thy, above of = bottom] (top) {\begin{tabular}{l}
\textsf{top}\\\hline
...\\\hline
no X
\end{tabular}};
\draw[include] (bottom) -- (top);
\end{tikzpicture}
\caption{Purely superfluous inclusion example}
\label{fig:purelysuperfluousbasic}
\end{figure}
Purely superfluous inclusions can be removed while still retaining a valid theory, however this will change the resulting theory graph. These changes may or may not be what is desired. An example for such an optimization can be seen in \autoref{fig:purelysuperfluousoptimized}.
\begin{figure}[!htb]
\centering
\begin{tikzpicture}[node distance=3cm]\footnotesize
\node[thy] (bottom) {\begin{tabular}{l}
\textsf{bottom}\\\hline
X\\\hline
...
\end{tabular}};
\node[thy, above of = bottom] (top) {\begin{tabular}{l}
\textsf{top}\\\hline
...\\\hline
no X
\end{tabular}};
\end{tikzpicture}
\caption{Example of purely superfluous inclusion optimized}
\label{fig:purelysuperfluousoptimized}
\end{figure}
The purely superfluous inclusions in a theory T are the set $I_{PuS}(T) =$\\ $ \{ I \in DirectInludes(T) | I \notin UsedTheories(T) \land Includes(I) \cap UsedTheories(T) \setminus Includes(UsedTheories(T)\setminus \{ T \}) = \emptyset \}$,\\
where $Includes(I)$ are the transitively included theories in I, $DirectIncludes(T)$ are the theories included directly in the theory T and $UsedTheories(T)$ are those theories whose terms are used in T.
\subsubsection{Partially Superfluous Inclusion}
\label{sec:partiallysi}
An inclusion of a theory $A\hookrightarrow{}B$ is \underline{partially superfluous}, if $B$ uses none of the constants in $A$, but uses some declarations that were declared in one or more theory $C$ such that C is (transitively) included in A. We describe theories that have declarations being used in a theory A, which includes them, as \underline{theories used by A}.\\
\begin{figure}[!htb]
\centering
\begin{tikzpicture}[node distance=3cm]\footnotesize
\node[thy] (bottom) {\begin{tabular}{l}
\textsf{bottom}\\\hline
$X_1$\\\hline
...
\end{tabular}};
\node[right of = bottom] (dots) {...};
\node[thy, right of = dots] (bottomn) {\begin{tabular}{l}
\textsf{$bottom_n$}\\\hline
$X_n$\\\hline
...
\end{tabular}};
\node[thy, above of = bottom] (middle) {\begin{tabular}{l}
\textsf{middle}\\\hline
Y\\\hline
...
\end{tabular}};
\node[thy, above of = middle] (top) {\begin{tabular}{l}
\textsf{top}\\\hline
...\\\hline
$X\subseteq\bigcup{}\{X_1,...,X_n\}$, but not Y
\end{tabular}};
\draw[include] (bottom) -- (middle);
\draw[include] (bottomn) -- (middle);
\draw[include] (middle) -- (top);
\end{tikzpicture}
\caption{Partially superfluous inclusion example}
\label{fig:partiallysuperfluousbasic}
\end{figure}
Partially superfluous inclusions can be optimized by identifying the used subset of (transitive) inclusions and replacing the superfluous inclusion with these inclusions. As seen in \autoref{fig:partiallysuperfluousoptimized}.
\begin{figure}[!htb]
\centering
\begin{tikzpicture}[node distance=3cm]\footnotesize
\node[thy] (bottom) {\begin{tabular}{l}
\textsf{bottom}\\\hline
$X_1$\\\hline
...
\end{tabular}};
\node[right of = bottom] (dots) {...};
\node[thy, right of = dots] (bottomn) {\begin{tabular}{l}
\textsf{$bottom_n$}\\\hline
$X_n$\\\hline
...
\end{tabular}};
\node[thy, above of = bottom] (middle) {\begin{tabular}{l}
\textsf{middle}\\\hline
Y\\\hline
...
\end{tabular}};
\node[thy, above of = middle] (top) {\begin{tabular}{l}
\textsf{top}\\\hline
...\\\hline
$X\subseteq\bigcup{}\{X_1,...,X_n\}$, but not Y
\end{tabular}};
\draw[include] (bottom) -- (middle);
\draw[include] (bottomn) -- (middle);
\draw[include] (bottomn) -- (top);
\draw[include, bend left] (bottom) edge (top);
\end{tikzpicture}
\caption{Example of partially superfluous inclusion optimized}
\label{fig:partiallysuperfluousoptimized}
\end{figure}
The partially superfluous inclusions in a theory T are the set $I_{PaS}(T) =$\\ $\{ I \in DirectInludes(T) | I \notin UsedTheories(T) \land Includes(I) \cap UsedTheories(T) \setminus Includes(UsedTheories(T)\setminus \{ T \}) \neq \emptyset \}$,\\
where $Includes(I)$ are the transitively included theories in I, $DirectIncludes(T)$ are the theories included directly in the theory T and $UsedTheories(T)$ are those theories whose terms are used in T.
\section{Viability and dangers of candidate optimizations}
\label{sec:viability}
The candidates discussed earlier are cases in which inclusions can be changed, not necessarily where they should be changed, as the only criterion that makes a theory an optimization candidate is that an optimization can be performed while still retaining a valid theory. There is nothing that guarantees that this theory is also preferable to the original or even desirable at all to have.\\
The danger of performing the proposed optimizations varies between the type of candidate and the context, but overall application without at least some user guidance is not recommended.
\subsection{Simply redundant inclusion}
As noted in \autoref{sec:puresi} there is very little that speaks against removing simply redundant inclusions, since the flattened graph is preserved. However there are some cases where they might do more harm than good, particularly when they overlap with superfluous inclusions, as seen in \autoref{fig:redundantoverlap}.\\ Removing the redundant edge will not change the flattened graph, but it will complicate the changes needed to remove the superfluous edge between middle and top. A simple solution to avoiding this problem is to relegate the removal of simple redundancies until after other optimizations have been performed on the theory.\\
\begin{figure}[!htb]
\centering
\begin{tikzpicture}[node distance=2cm]\footnotesize
\node[thy] (bottom) {\begin{tabular}{l}
\textsf{bottom}\\\hline
X\\\hline
...
\end{tabular}};
\node[thy, above of = bottom] (middle) {\begin{tabular}{l}
\textsf{middle}\\\hline
Y\\\hline
...
\end{tabular}};
\node[thy, above of = middle] (top) {\begin{tabular}{l}
\textsf{top}\\\hline
...\\\hline
X, but not Y
\end{tabular}};
\draw[include] (bottom) -- (middle);
\draw[include, bend left] (bottom) edge (top);
\draw[include] (middle) -- (top);
\end{tikzpicture}
\caption{Example of overlap between simply redundant inclusion and superfluous inclusion}
\label{fig:redundantoverlap}
\end{figure}
\subsection{Purely superfluous inclusion}
\label{sec:puresivia}
Purely superfluous includes are a little trickier, as their removal can fundamentally change the underlying theory graph. Removing a purely superfluous inclusion.
A trivial example for a purely superfluous inclusion that should not be optimized is any theory union, ie. a theory that consists entirely of theory inclusions (\autoref{fig:theoryunion}).\\
Removing purely superfluous includes from a theory union will leave behind an entirely empty theory. Since an empty theory is rarely a desired outcome an easy fix is to skip theory unions for this or in fact any kind of optimization.
\begin{figure}[!htb]
\centering
\begin{tikzpicture}[node distance=2cm]\footnotesize
\node[thy] (union) {\begin{tabular}{l}
\textsf{union}\\\hline
\\\hline
\end{tabular}};
\node[thy, below left of = union] (A) {\begin{tabular}{l}
\textsf{A}\\\hline
...\\\hline
...
\end{tabular}};
\node[thy, below right of = union] (B) {\begin{tabular}{l}
\textsf{B}\\\hline
...\\\hline
...
\end{tabular}};
\draw[include] (A) -- (union);
\draw[include] (B) -- (union);
\end{tikzpicture}
\caption{Example of a simple theory union}
\label{fig:theoryunion}
\end{figure}
However theory unions are not the only cases where optimization may be misplaced. When culling seemingly superfluous inclusions of non-empty theories we fundamentally change the theory graph, by cutting the content of the included theory from our candidate. This means that unlike the simply redundant case, this optimization should be carefully considered.
If this content is required by a theory that originally included it via an inclusion of the candidate, this may even break the entire theory graph. To avoid this problem it makes sense to not only watch what the candidate theory itself uses, but to look further ahead and also consider the symbols used by theories which include the candidate. We call these theories the \underline{future lite code}.
Another problem arises were inclusion paths branch of before eventually meeting again. Ideally optimizing superfluous inclusions should retain exactly those theory inclusions that are necessary for keeping a valid theory graph with no undeclared objects. Unfortunately this is complicated by the requirement to keep a theory's future valid. Specifically the problem arises if a future theory indirectly includes a theory over more than one path (see \autoref{fig:doubleinclusion}).
\begin{figure}[!htb]
\centering
\begin{tikzpicture}[node distance=3cm]\footnotesize
\node[thy] (bottom) {\begin{tabular}{l}
\textsf{bottom}\\\hline
X\\\hline
...
\end{tabular}};
\node[thy, above right of = bottom] (right) {\begin{tabular}{l}
\textsf{right}\\\hline
Y\\\hline
not X
\end{tabular}};
\node[thy, above left of = bottom] (left) {\begin{tabular}{l}
\textsf{left}\\\hline
Z\\\hline
not X
\end{tabular}};
\node[thy, above left of = right] (top) {\begin{tabular}{l}
\textsf{top}\\\hline
...\\\hline
X, Y, Z
\end{tabular}};
\draw[include] (bottom) -- (right);
\draw[include] (bottom) -- (left);
\draw[include] (right) -- (top);
\draw[include] (left) -- (top);
\end{tikzpicture}
\caption{Example of a graph where optimization leaves ambiguous choice.}
\label{fig:doubleinclusion}
\end{figure}
As we can see the branched inclusion leaves us with two paths for top to include bottom. Either via inclusion of left or right. Therefore we could remove one of the inclusions of bottom in left or right, but not both of them. However recognizing such a case is difficult and even if one of the inclusions should be removed it is not easily deducible which one is truly superfluous. Thus it seems prudent to err on the side of caution and ignore such a case entirely and treat it like a regular case of a use of bottom in left and right's respective futures.
\subsection{Partially superfluous inclusion}
Due to the inherent similarities many of the prior considerations also apply to partially superfluous inclusions.
Let us consider the following example from elementary Algebra (\autoref{fig:elalg}). It is easy to define a monoid as a semigroup with the addition of a neutral element. The problem this poses for our optimizations is that our definition doesn't make use of associativity in any way. If all the symbols needed for defining the neutral element are delivered through a theory included by semi groups called Magma, then replacing the inclusion of semigroups with magma is a perfectly valid optimization. This is however a vastly different theory then we would expect of something that calls itself a monoid and can lead to complications if a theory including this theory expects it to contain the associativity rule, as it should. Again this problem can be somewhat alleviated by looking at the future lite code.
\begin{figure}[!htb]
\centering
\begin{tikzpicture}[node distance=2cm]\myfontsize
\node[thy] (magma) {\begin{tabular}{l}
\textsf{Magma}\\\hline
$G,\circ$\\\hline
$\scriptstyle x\circ y\in G$
\end{tabular}};
\node[thy, above of=magma] (sg) {\begin{tabular}{l}
\textsf{SemiGrp}\\\hline
\\\hline
$\scriptstyle (x\circ y)\circ z=x\circ (y\circ z)$
\end{tabular}};
\node[thy, above of=sg] (m) {\begin{tabular}{l}
\textsf{Monoid}\\\hline
$e$\\\hline
$\scriptstyle e\circ x=x\circ e=x$
\end{tabular}};
\draw[include] (magma) -- (sg);
\draw[include] (sg) -- (m);
\end{tikzpicture}
\caption{Simple example from elementary Algebra}
\label{fig:elalg}
\end{figure}
This problem is not entirely unlike the problem of the theory unions and other variations may apply to purely superfluous inclusions.
\section{Structures}
It should be noted that all the cases discussed in this chapter assume the relationship between these theories to be one of simple theory inclusion, while ignoring the existence of named structures entirely.
As a rule of thumb we can assume that anything that was declared as a named structure is there for a reason and should therefore not be touched by our optimizations. Obviously this reasoning does not apply in all cases, but it is reason enough to avoid them in an automated tool.
In addition structures have proven to be especially difficult to consider for our future lite code, for reasons that will be discussed in \autoref{sec:futurelite}.