\documentclass[acmtog, authorversion]{acmart}

\usepackage{booktabs} % For formal tables

% TOG prefers author-name bib system with square brackets
\citestyle{acmauthoryear}
\setcitestyle{square}

% Copyright
%\setcopyright{acmcopyright}
%\setcopyright{acmlicensed}
%\setcopyright{rightsretained}
%\setcopyright{usgov}
\setcopyright{usgovmixed}
%\setcopyright{cagov}
%\setcopyright{cagovmixed}

% Document starts
\begin{document}
% Title portion
\title{A Neural Network Approach to Stock Prediction}

\author{Paul Soma}
\affiliation{%
  \institution{Michigan State University}}
\email{somapaul@msu.edu}

\maketitle

\section{Introduction}
Accurately forecasting financial markets has been studied extensively by retail traders, investors, and financial institutions the world over. This work employs two neural-network approaches to predicting price action on the Dow Jones Industrial Average.
The Dow Jones Industrial Average (DJI) is a stock market index composed of 30 large publicly traded companies in the United States. The price of the DJI is given by the sum of the price of one share of stock for each of its 30 constituent companies\cite{economics}.

\section{Data}
The data to be studied contains the daily high price, low price, opening price, and closing price, for the DJI from the beginning of January, 2018 through then end of September, 2018. The training set consists of data from Jan 2, 2018 through July 31, 2018, while the test contains set contains data August 1, 2018 through September 28, 2018. Thus the training set consists of 146 days, while the test set consists of 41 days. The validation set consists of 20\% of the data in the training set.

\section{Methods}
This work employs a neural network-based regression approach to predicting price action on the Dow Jones Industrial Average. The data to be studied will contain daily measurements of the DJIA from January through September 2018. Two different approaches were used to predict the DJI. Both approaches utilize artificial neural networks. The difference between the two approaches is that the first approach applied a simple neural network model, while the second approach applied a deep learning model. The differences between the two networks will be covered later in the paper.

\subsection{Data Collection and Preprocessing}
The data was downloaded from Yahoo Finance. A few examples from the data are shown in Table 1. The data was normalized using min-max scaling. The operation for min-max scaling is given in the following equation:
\begin{gather}
    x' = \frac{x - \min x}{\max x - \min x}
\end{gather}
The target variable for each day $t$ in the data was the closing price on the day $t+1$.

\begin{table}[h]
\centering
\caption{Data Features}
\begin{tabular}{p{2.5cm}p{4cm}}

\toprule
\multicolumn{1}{c}{\textbf{Feature}} & \multicolumn{1}{c}{\textbf{Description}}                  \\ \midrule
Open                           & Value at the start of the day         \\
High                           & Highest value of day      \\
Low                           & Lowest value of the day                                     \\
Close                           & Value at the end of the day               \\
Volume                          & Number of shares traded in the day        \\ \bottomrule
\end{tabular}
\end{table}

\begin{table}[]
\centering
\caption{Example Data}
\begin{tabular}{@{}lllll@{}}
\toprule
\textbf{Open} & \textbf{High} & \textbf{Low} & \textbf{Close} & \textbf{Volume} \\ \midrule
22423.47 & 22559.38 & 22416.00 & 22557.59 & 268530000              \\
22564.44 & 22646.32 & 22562.90 & 22641.66 & 238830000              \\
22645.66 & 22685.93 & 22632.80 & 22661.64 & 235730000 \\ \bottomrule
\end{tabular}
\end{table}

\subsection{Model 1}
The architecture of the neural network for model one consisted of 2 hidden layers with 64 and 32 nodes respectively. For the activation function, rectified linear unit is used in both layers. The rectified linear unit (ReLU) is generally a common choice for activation functions for neural networks. The computation of rectified linear unit is shown by equation (2). Mean absolute error was used as the loss function. The network was trained on 1000 epochs. Each epoch represents an iteration of training the model through the entire test set. The model was evaluated using mean absolute error and mean squared error.

\begin{gather}
    ReLU = \max(0,x)
\end{gather}

\subsection{Model 2}
In the model 2 regression approach, the target variable for each $x_t$ is the closing price of $x_{t+1}$. The architecture of the neural network consisted of 5 hidden layers with 128, 64, 32, 16, and 8 nodes respectively. Rectified linear unit is used as the activation function for each layer. Batch normalization is applied after each layer to improve training speed and combat over-fitting. Mean absolute error was used as the loss function. The network was trained on 1000 epochs. The model was evaluated using mean absolute error and mean squared error.

\section{Results}

\subsection{Model 1}
Mean absolute error for model 1 on the training and validation sets are shown in Figure 1. On the test set, model 1 reported a loss of 0.0906, a mean squared error of 0.0106. The actual price and the predicted by the model are shown in Figure 2. In U.S. dollars, the average error for the test set predictions was \$279.55.

\begin{figure}[h]
\caption{Model 1 MAE}
\includegraphics[scale=0.4]{images/model_1_mae.png}
\centering
\end{figure}

\begin{figure}[h]
\caption{Model 1 Prediction}
\includegraphics[scale=0.4]{images/model_1_pred.png}
\centering
\end{figure}

\subsection{Model 2}
Mean absolute error for model 2 on the training and validation sets are shown in Figure 3. On the test set, the model 2 reported a loss of 0.0588, a mean squared error of 0.00655, and a mean absolute error of 0.0588. The actual price and the predicted by the model are shown in Figure 2. The average error in U.S. dollars was \$181.46.

\begin{figure}[h]
\caption{Model 2 MAE}
\includegraphics[scale=0.4]{images/model_2_mean_absolute_error.png}
\centering
\end{figure}

\begin{figure}[h]
\caption{Model 2 Prediction}
\includegraphics[scale=0.4]{images/model_2_closing_price.png}
\centering
\end{figure}

\section{Conclusion}
Model 1 suffers from over-fitting. As can be seen from the loss function shown in Figure 1, the validation set has a lower loss than the test set, which should not be the case. Also, the mean absolute error was reduced to 0.0106. We should expect a somewhat higher error. When considering the predicted price shown in Figure 2, we see that the prediction closely mirrors the actual price, but with a large error. This is a sign of overfitting. \\

Model 2 is somewhat more successful than model 1 in that it has less overfitting, likely due to the batch normalization. The price predictions generated by this model are more accurate than the predictions of model 1. However, this model would not be sufficient for use as an actual trading strategy. Future improvements could include trying different activation and loss functions, adding in some computed technical indicators, or using shorter time intervals than 1 day between each record.

\bibliographystyle{plain}
\bibliography{bibliography}

\end{document}


Each fixed point implementation uses the next higher integer data type for the integral value storage variable.

A rising flank at the \textit{Enable} inport will preload the integral part with the value present on the \textit{Init} inport.
\newline

\noindent
Transfer function (zero-order hold discretization method):
\begin{eqnarray*}
	G(z) &=&  K_{P} + K_{I}T_{s}\frac{1}{z-1}
\end{eqnarray*}

\paragraph{Developer note:} The fixed point implementation source code of this block is used for block \nameref{block:PI}.


\section{Related Work}
\label{sec:prototype-semantics:Related_Work}
Hooman and Van der Zwaag~\cite{Hooman2006} used the interactive theorem prover \PVS to define the semantics of a subset of the \UML.
In this subset, the behavior of objects is specified using state machines that communicate with each other both synchronously and asynchronously.
Proving properties of models in this approach is done manually using \PVS.
This is a complex task that requires expertise in \PVS, which can be simplified using certain predefined strategies.
A disadvantage of this approach is that is does not offer the reusability of other existing tools that our approach offers.
An advantage of this approach is that it does not suffer from the state-space explosion problem, because the complete state space of models does not have to be generated for property verification.

Di Ruscio~\cite{Ruscio06apractical} et al.\ define the semantics of a DSML for the development of telephony services using Abstract State Machines (ASMs).
Because ASMs can be executed, this definition can be used to simulate models specified in their DSML.
The approach is meant for the specification of the behavioral semantics of the DSML only and does not offer verification of models.
Proving properties for all models in general or any specific model is not investigated.
In theory, however, properties of models could be verified in the domain of ASMs.

Sadilek and Wachsmuth~\cite{Sadilek:2008:PVI:1426334.1426341} propose a technique for defining the semantics of DSMLs that uses model instances as configurations and \QVT relations to define steps between configurations.
Configurations, representing model instances, can be visualized using the same editors used to create models.
By reusing the existing editors, visual interpreters and visual debuggers can be created with relatively little effort.
Although this technique is suited for simulation of models, it is not efficient enough for state-space generation.
Because each configuration is represented by a model, a lot of memory is needed to store all possible configurations.

A number of approaches use \Maude to specify the operational semantics of DSMLs~\cite{Rusu:2011:EDM:1921532.1921557, Rivera:2009:FSA:1631662.1631666}.
Given the operational semantics of a DSML in \Maude, other techniques can be applied to verify properties of models specified in such a DSML.
Both an LTL model checker~\cite{wrla2002:mmc} and a $\mu$-calculus model checker~\cite{Wang:2005:9MC:1705545.1705992} are available for rewrite systems specified in \Maude.
Although it is clear that model checking techniques can be implemented in \Maude and applied to specifications of the semantics of DSMLs, not all techniques applicable to labeled transition systems that we aim to exploit, such as reduction and visualization, have been implemented in \Maude.
It might be the case, therefore, that a given technique must first be implemented in \Maude before it can be used in combination with a specification of the semantics of a DSML.
With our approach, we can connect to various tools and apply existing techniques only by adapting the representation of labeled transition systems, if~needed.


\section{Metatheory}\label{sec:metatheory}

This section presents the metatheory of the algorithmic system
presented in the previous section. We show that three main results hold:
\emph{soundness}, \emph{completeness} and \emph{decidability}.
These three results have been mechanically formalized and proved in the
Abella theorem prover~\citep{AbellaDesc}.

%-------------------------------------------------------------------------------
\subsection{Declarative Worklist and Transfer}
%\jimmy{The transfer rule outputs declarative judgment chains, which are later processed by another declarative relation, handling all the guessing.}

\begin{figure}[t]
\begin{gather*}
\begin{aligned}
\text{Declarative worklist}\qquad&\Om &::=&\quad \nil \mid \Om, a \mid \Om, x: A \mid \Om \Vdash \jg
\end{aligned}
\end{gather*}
\hfill \framebox{$\Gm \sto \Om$} \hfill $\Gm$ instantiates to $\Om$.
\begin{gather*}
\inferrule*[right=$\mathtt{{\sto}}\Om$]
{~}
{\Om \sto \Om}
\quad
\inferrule*[right=$\mathtt{{\sto}\al}$]
{\Om\vdash\tau \\ \Om,[\tau/\al]\Gm \sto \Om}
{\Om,\al,\Gm \sto \Om}
\end{gather*}
\Description{Declarative Worklists and Instantiation}
\caption{Declarative Worklists and Instantiation}
\label{fig:trans}
\end{figure}

To aid formalizing the correspondence between the declarative and algorithmic
systems, we introduce the notion of a declarative worklist $\Om$, defined in
Figure~\ref{fig:trans}. A declarative worklist $\Om$ has the same structure as an
algorithmic worklist $\Gm$, but does not contain any existential variables $\al$.

\paragraph{Worklist instantiation.}
The relation $\Gm \sto \Om$ expresses that the algorithmic worklist $\Gm$ can
be instantiated to the declarative worklist $\Om$, by appropriately instantiating
all existential variables $\al$ in $\Gm$ with well-scoped monotypes $\tau$.
The rules of this instantiation relation are shown in Figure~\ref{fig:trans} too.
Rule $\mathtt{{\sto}\al}$ replaces the first existential variable with a well-scoped monotype and
repeats the process on the resulting worklist until no existential variable remains
and thus the algorithmic worklist has become a declarative one.
In order to maintain well-scopedness,
the substitution is applied to all the judgments and term variable bindings in the scope of $\al$.

Observe that the instantiation $\Gm\sto\Om$ is not deterministic.
% \bruno{Do you mean here
%   ``is not deterministic''? Would that be more appropriate?}
From left to right, there are infinitely many possibilities to instantiate an existential variable and
thus infinitely many declarative worklists that one can get from an algorithmic one.
In the other direction, any valid monotype in $\Om$ can be abstracted to an
existential variable in $\Gm$. Thus different $\Gm$'s can be instantiated to the same
$\Om$.

Lemmas~\ref{lem:insert} and \ref{lem:extract}
generalize Rule $\mathtt{{\sto}\al}$ from substituting the first existential variable
to substituting any existential variable.

\begin{lemma}[Insert]\label{lem:insert}
If $\Gm_L, [\tau/\al]\Gm_R \sto \Om$ and $\Gm_L\vdash \tau$
, then $\Gm_L, \al, \Gm_R \sto \Om$.
\end{lemma}
\begin{lemma}[Extract]\label{lem:extract}
If $\Gm_L, \al, \Gm_R \sto \Om$
, then there exists $\tau$ s.t. $\Gm_L\vdash\tau$ and $\Gm_L, [\tau/\al]\Gm_R \sto \Om$.
\end{lemma}

\begin{figure}[t]
\hfill \framebox{$\|\Om\|$} \hfill Judgment erasure.
\begin{gather*}
\begin{aligned}
\|\nil\| &= \nil\\
\|\Om,a\| &= \|\Om\|, a\\
\|\Om,x:A\| &= \|\Om\|, x:A\\
\|\Om\Vdash\jg\| &= \|\Om\|
\end{aligned}
\end{gather*}

\hfill \framebox{$\Om \rto \Om'$} \hfill Declarative transfer.
\begin{gather*}
\begin{aligned}
\Om,a &\rto \Om \\  \Om,x:A & \rto \Om\\
\Om\Vdash A\le B &\rto \Om &\text{ when } \|\Om\| \vdash A\le B\\
\Om\Vdash e\Lto A &\rto \Om & \text{ when } \|\Om\| \vdash e\Lto A\\
\Om\Vdash e\To_a \jg &\rto \Om\Vdash[A/a]\jg & \text{ when } \|\Om\| \vdash e\To A\\
\Om\Vdash \appInfAlg{A}{e} &\rto \Om\Vdash[C/a]\jg & \text{ when } \|\Om\| \vdash \appInf{A}{e}{C}\\
\end{aligned}
\end{gather*}
\Description{Declarative Transfer}
\caption{Declarative Transfer}
\label{fig:decl:worklist}
\end{figure}

\paragraph{Declarative transfer.}
Figure~\ref{fig:decl:worklist} defines a relation $\Om \rto \Om'$,
which transfers all judgments in the declarative worklists to the
declarative type system. This relation checks that every judgment entry in the worklist
holds using a corresponding conventional declarative judgment.
The typing contexts of declarative judgments are recovered using an
auxiliary erasure function $\|\Om\|$
\footnote{In the proof script we do not use the erasure function,
for the declarative system and well-formedness judgments automatically fit
the non-erased declarative worklist just as declarative contexts.}.
The erasure function simply drops all judgment entries from the worklist,
keeping only variable and type variable declarations.

%-------------------------------------------------------------------------------
\subsection{Non-Overlapping Declarative System}
\label{sec:metatheory:non-overlapping}

DK's declarative system, shown in Figures \ref{fig:decl:sub} and
\ref{fig:decl:typing}, has a few overlapping rules. In contrast, our algorithm
has removed all overlap; at most one rule applies in any given situation.
This discrepancy makes it more difficult to relate the two systems.

To simplify matters, we introduce an intermediate system that is still declarative
in nature, but has no overlap. This intermediate system differs only in a few
rules from DK's declarative system:
\begin{enumerate}
\item Restrict the shape of $B$ in the rule $\mathtt{\forall L}$ subtyping rule:
$$
\inferrule*[right=$\mathtt{\forall L'}$]
{B \neq \all[b]B' \\ \Psi \vdash \tau \\ \Psi \vdash [\tau/a]A \le B}
{\Psi \vdash \all A \le B}
$$
\item Drop the redundant rule $\mathtt{Decl1I}$,
which can be easily derived by a combination of
$\mathtt{DeclSub}$, $\mathtt{Decl1I{\To}}$ and $\mathtt{{\le}Unit}$:
$$
\inferrule*[right=$\mathtt{DeclSub}$]
{
    \inferrule*[right=$\mathtt{Decl1I{\To}}$]
    {~}
    {\Psi \vdash () \To 1}
    \\
    \inferrule*[right=$\mathtt{{\le}Unit}$]
    {~}
    {\Psi \vdash 1 \le 1}
}
{\Psi \vdash () \Lto 1}
$$
\item Restrict the shapes of $e$ and $A$ in the subsumption rule $\mathtt{DeclSub}$:
$$
\inferrule*[right=$\mathtt{DeclSub'}$]
{e \neq \lam e' \\ A \neq \all A' \\ \Psi \vdash e \To A \\ \Psi \vdash A \le B}
{\Psi \vdash e \Lto B}
$$
\end{enumerate}
The resulting declarative system has no overlapping rules
and more closely resembles the algorithmic system,
which contains constraints of the same shape.

We have proven soundness and completeness of the non-overlapping declarative
system with respect to the overlapping one to establish their equivalence.
Thus the restrictions do not change the expressive power of the system.
Modification (2) is relatively easy to justify, with the derivation given above:
the rule is redundant and can be replaced by a combination of three other rules.
Modifications (1) and (3) require inversion lemmas for the rules that overlap.
Firstly, Rule $\mathtt{\forall L}$ overlaps with Rule $\mathtt{\forall R}$ for the judgment
$\Psi \vdash \all A \le \all[b]B$.
The following inversion lemma for Rule $\mathtt{\forall R}$ resolves the overlap:
\begin{lemma}[Invertibility of $\mathtt{\forall R}$]\label{lem:inv_allR}
If $\Psi \vdash A \le \all B$ then $\Psi, a \vdash A \le B$.
\end{lemma}
The lemma implies that preferring Rule $\mathtt{\forall R}$ does not affect the derivability of the judgment.
Therefore the restriction $B \neq \all[b]B'$ in $\mathtt{\forall L'}$ is valid.

Secondly, Rule $\mathtt{DeclSub}$ overlaps with both $\mathtt{Decl\forall I}$ and $\mathtt{Decl{\to}I}$.
We have proven two inversion lemmas for these overlaps:
\begin{lemma}[Invertibility of $\mathtt{Decl\forall I}$]\label{lem:inv_chkAll}
If $\Psi \vdash e \Lto \all A$ then $\Psi, a \vdash e \Lto A$.
\end{lemma}
\begin{lemma}[Invertibility of $\mathtt{Decl{\to}I}$]\label{lem:inv_chkLam}
If $\Psi \vdash \lam e \Lto A \to B$ then $\Psi, x:A \vdash e \Lto B$.
\end{lemma}
These lemmas express that applying the more specific rules,
rather than the more general rule $\mathtt{DeclSub}$, does not reduce the expressive power.
The is required by the completeness lemma,
as the algorithmic system explicitly prioritize the more specific rules.

The proofs of the above two lemmas rely on an important property of the
declarative system, the \emph{subsumption lemma}. To be able to formulate this
lemma, Figure~\ref{fig:context_subtyping} introduces the \emph{context
subtyping relation} $\Psi \le \Psi'$. Context $\Psi$ subsumes context $\Psi'$ if they
bind the same variables in the same order, but the types $A$ of the term variables $x$
in the former are subtypes of types $A'$ assigned to those term variables in the latter.
Now we can state the subsumption lemma:

\begin{figure}[t]
\framebox{$\Psi' \le \Psi$}
$$\begin{aligned}
\inferrule*[right=$\mathtt{CtxSubEmpty}$]
{~}{\nil \le \nil}\qquad
\inferrule*[right=$\mathtt{CtxSubTyVar}$]
{\Psi' \le \Psi}{\Psi', a \le \Psi, a}\\
\inferrule*[right=$\mathtt{CtxSubTmVar}$]
{\Psi' \le \Psi \\ \Psi \vdash A' \le A}{\Psi', x:A' \le \Psi, x:A}
\end{aligned}$$
\Description{Context Subtyping}
\caption{Context Subtyping}
\label{fig:context_subtyping}
\end{figure}

\begin{lemma}[Subsumption]\label{lem:subsumption}
Given $\Psi' \le \Psi$:
\begin{enumerate}
    \item If $\Psi \vdash e \Lto A$ and $\Psi \vdash A \le A'$ then $\Psi' \vdash e \Lto A'$;
    \item If $\Psi \vdash e \To B$ then there exists
        $B'$ s.t. $\Psi \vdash B' \le B$ and $\Psi' \vdash e \To B'$;
    \item If $\Psi \vdash \appInf{A}{e}{C}$ and $\Psi \vdash A' \le A$,
        then there exists $C'$ s.t. $\Psi \vdash C' \le C$ and $\Psi' \vdash \appInf{A'}{e}{C'}$.
\end{enumerate}
\end{lemma}
This lemma expresses that any derivation in a context $\Psi$ has a corresponding derivation in any context
$\Psi'$ that it subsumes.

While being written in a clear format and providing enough details,
some proof is not fully accepted by the proof assistant.
Specifically for the subsumption lemma,
we have tried to follow DK's manual proof,
but we discovered several problems in their reasoning that we have been unable to address.
Fortunately we have found a different way to prove the lemma.
A description of the problem and our fix are discussed in-depth as follows.

% \subsection{A Fix to DK's Subsumption Lemma}
% \label{appendix:subsumption}

In the appendix of DK's paper~\citep{dunfield2013complete},
the first two applications of induction hypotheses on page 22 are not accepted.
Either of them seems to use a slightly different ``induction hypothesis'' than the true one.
In fact, we cannot think of simple ways to fix the problem,
since the induction hypothesis seems not strong enough for these two cases.

To fix the proof, we propose a new induction scheme
by making use of our worklist measures.
Recall that $|e|_e$ measures term size;
and the judgment measure counts checking as 2,
inference as 1 and application inference as 3;
and $|A|_\forall$ counts the number of $\forall$'s in a type.

\begin{proof}
The proof is by a nested mutual induction on the lexicographical order of the measures
$$\langle |e|_e, |\cdot|_\Leftrightarrow, |A|_\forall + |A'|_\forall \rangle,$$
where the second measure simple give an natural number for each type of judgment:
(1) checking judgments ($\Psi \vdash e \Lto A$) count as 2;
(2) inference judgments ($\Psi \vdash e \To A$) count as 1; and
(3) application inference judgments ($\Psi \vdash \appInf{e}{A}{C}$) count as 3;
and the third measure does not apply to (case 2) since no $A$ is used.
Compared with DK's, our third measure that counts the degree of polymorphism
fixes problems that occurred in DK's proof: in both places,
our new induction hypothesis is more generalized.

All but two cases can be finished easily following the declarative typing derivation,
and the proof shares a similar structure to DK's.
One tricky case related to Rule $\mathtt{Decl\forall I}$ indicates that $A$ has the shape $\all A_0$,
thus the subtyping relation derives from either $\mathtt{{\le}\forall L}$ or $\mathtt{{\le}\forall R}$.
For each of the case, the third measure $|A|_\forall + |A'|_\forall$ decreases
(the $\mathtt{{\le}\forall L}$ case requires a type substitution lemma obtaining
$\Psi \vdash e \Lto [\tau/a]A_0$ from the typing derivation).

Another tricky case is $\mathtt{Decl{\to}I}$.
When the subtyping relation is derived from $\mathtt{{\le}{\to}}$,
a simple application of induction hypothesis finishes the proof.
When the subtyping relation is derived from $\mathtt{{\le}\forall R}$,
$|A'|_\forall$ decreases, and thus the induction hypothesis finishes this case.
\end{proof}

In short, we found a specific problem when trying to prove the subsumption lemma
following DK's manual proof, yet we addressed the problem
by using a slightly different induction scheme.

\paragraph{Three-Way Soundness and Completeness Theorems}
We now have three systems that can be related: DK's overlapping declarative system,
our non-overlapping declarative system, and our algorithmic system.
We have already established the first relation, that the two declarative
systems are equivalent.
In what follows, we will establish the soundness of our algorithm directly
against the original overlapping declarative system. However, we have found
that showing completeness of the algorithm is easier against the
non-overlapping declarative system. Of course, as a corollary, it follows that
our algorithm is also complete with respect to DK's declarative system.

%-------------------------------------------------------------------------------
\subsection{Soundness}

Our algorithm is sound with respect to DK's declarative system.
For any worklist $\Gm$ that reduces successfully,
there is a valid instantiation $\Om$ that transfers all judgments
to the declarative system.
\begin{theorem}[Soundness]
If \emph{wf }$\Gm$ and $\Gm \redto \nil$,
then there exists $\Om$ s.t. $\Gm\sto\Om$ and $\Om\redto\nil$.
\end{theorem}

The proof proceeds by induction on the derivation of $\Gm\redto\nil$.
Interesting cases are those involving existential variable instantiations,
including Rules 10, 11, 21 and 29.
Applications of Lemmas \ref{lem:insert} and \ref{lem:extract}
help analyse the full instantiation of those existential variables.
For example, when $\al$ is solved to $\al[1] \to \al[2]$ in the algorithm,
applying the Extract lemma gives two instantiations $\al[1] = \sigma$ and $\al[2] = \tau$.
It follows that $\al = \sigma \to \tau$, which enables the induction hypothesis
and finishes the corresponding case. Some immediate corollaries which
show the soundness for specific judgment forms are:

\begin{corollary}[Soundness, single judgment form]
Given \emph{wf }$\Gm$:
\begin{enumerate}
    \item If $\Gm \Vdash A \le B \redto \nil$\\
        then there exist $A', B', \Om$ s.t.
        $\Gm \Vdash A \le B \sto \Om \Vdash A' \le B'$ and $\|\Om\| \vdash A' \le B'$;
    \item If $\Gm \Vdash e \Lto A \redto \nil$\\
        then there exist $ A', \Om$ s.t.
        $\Gm \Vdash e \Lto A \sto \Om \Vdash e \Lto A'$ and $\|\Om\| \vdash e \Lto A'$;
    \item If $\Gm \Vdash e \To_a \jg \redto \nil$ for any $\jg$\\
        then there exists $\Om, \jg', A$ s.t.
        $\Gm \sto \Om$ and $\|\Om\| \vdash e \To A$;
    \item If $\Gm \Vdash \appInfAlg{A}{e} \redto \nil$ for any $\jg$\\
        then there exists $\Om, \jg', A', C$ s.t.
        $\Gm \Vdash \appInfAlg{A}{e} \sto \Om \Vdash \appInfAlg{A'}{e}[a][\jg']$
            and $\|\Om\| \vdash \appInf{A'}{e}{C}$.
\end{enumerate}
\end{corollary}

It is also possible to express a even simpler form of corollary,
where the judgments consist of declarative expressions and types.
% This can be viewed as the initial judgment of the algorithm.

\begin{corollary}[Soundness, declarative form]
Given \emph{wf }$\Psi$:
\begin{enumerate}
    \item If $\Psi \Vdash A \le B \redto \nil$ then $\Psi \vdash A \le B$;
    \item If $\Psi \Vdash e \Lto A \redto \nil$ then $\Psi \vdash e \Lto A$;
    \item If $\Psi \Vdash e \To_a \jg \redto \nil$ then
        there exists $A$ s.t. $\Psi \vdash e \To A$;
    \item If $\Psi \Vdash \appInfAlg{A}{e} \redto \nil$ then
        there exists $C$ s.t. $\Psi \vdash \appInf{A}{e}{C}$.
\end{enumerate}
\end{corollary}

%-------------------------------------------------------------------------------
\subsection{Completeness}

The completeness of our algorithm means that any derivation in the
declarative system has an algorithmic counterpart.
% \jimmy{In response to "treat declarative and algorithmic worklists as a subsets":}
We explicitly relate between an algorithmic context $\Gm$ and
a declarative context $\Omega$ to avoid potential confusion.

\begin{theorem}[Completeness]
If \emph{wf }$\Gm$ and $\Gm\sto\Om$ and $\Om\redto\nil$, then $\Gm \redto \nil$.
\label{thm:completeness}
\end{theorem}

We prove completeness by induction on the derivation of $\Om\redto\nil$
and use the non-overlapping declarative system.
Since the declarative worklist is reduced judgment by judgment
(shown in Figure~\ref{fig:decl:worklist}),
the induction always analyses the first judgment by a small step.
As the algorithmic system introduces existential variables,
a declarative rule may correspond to multiple algorithmic rules,
and thus we analyse each of the possible cases.
% \jimmy{Cite DK's proof as the sketch.}
% I later think it might not be needed; the induction is not that hard to think of

Most cases are relatively easy to prove.
The Insert and Extract lemmas are applied when the algorithm uses existential variables,
but transferred to a monotype for the declarative system,
such as Rules 6, 8, 10, 11, 12-17, 21, 25, 27 and 29.

Algorithmic Rules 10 and 11 require special treatment.
When the induction reaches the $\mathtt{{\le}{\to}}$ case,
the first judgment is of shape $A_1 \to A_2 \le B_1 \to B_2$.
One of the corresponding algorithmic judgments is $\al \le A \to B$.
However, the case analysis does not imply that $\al$ is fresh in $A$ and $B$,
therefore Rule~10 cannot be applied and the proof gets stuck.
The following lemma helps us out in those cases:
the success in declarative subtyping indicates the freshness of $\al$ in $A$ and $B$
in its corresponding algorithmic judgment.
In other words, the declarative system does not accept infinite types.
A symmetric lemma holds for $A\to B \le \al$.
%thus, as an example, we cannot find a monotype $\tau$ such that $\tau\le 1\to \tau$,
%which could be transferred by $\al\le 1\to\al$.

\begin{lemma}[Prune Transfer for Instantiation]\label{lem:prune_inst}
If $(\Gm \Vdash \al \le A \to B) \sto (\Om \Vdash C \le A_1 \to B_1)$ and
$\|\Om\| \vdash C \le A_1 \to B_1$, then $\al\notin FV(A) \cup FV(B)$.
\end{lemma}

The following corollary is derived immediately from Theorem~\ref{thm:completeness}.
\begin{corollary}[Completeness, single judgment form]
Given \emph{wf }$\Gm$ containing no judgments:
\begin{enumerate}
    \item If $\Om \vdash A' \le B'$ and $\Gm \Vdash A \le B \sto \Om \Vdash A' \le B'$
        \\then $\Gm \Vdash A \le B \redto \nil$;
    \item If $\Om \vdash e \Lto A'$ and $\Gm \Vdash e \Lto A \sto \Om \Vdash e \Lto A'$
        \\then $\Gm \Vdash e \Lto A \redto \nil$;
    \item If $\Om \vdash e \To A$ and $\Gm \Vdash e \To_a 1 \le 1 \sto \Om \Vdash e \To_a 1 \le 1$
        \\then $\Gm \Vdash e \To_a 1 \le 1 \redto \nil$;
    \item If $\Om \vdash \appInf{A'}{e}{C}$ and
        $\Gm \Vdash \appInfAlg{A}{e}[a][1 \le 1] \sto \Om \Vdash \appInfAlg{A'}{e}[a][1 \le 1]$
        \\then $\Gm \Vdash \appInfAlg{A}{e}[a][1 \le 1] \redto \nil$.
\end{enumerate}
\end{corollary}

Similar to soundness, we also provide a simpler corollary of completeness
for declarative types and expressions.

\begin{corollary}[Completeness, declarative form]
Given \emph{wf }$\Psi$:
\begin{enumerate}
    \item If $\Psi \vdash A \le B$ then $\Psi \Vdash A \le B \redto \nil$;
    \item If $\Psi \vdash e \Lto A$ then $\Psi \Vdash e \Lto A \redto \nil$;
    \item If $\Psi \vdash e \To A$ then $\Psi \Vdash e \To_a 1 \le 1 \redto \nil$;
    \item If $\Psi \vdash \appInf{A}{e}{C}$
        then $\Psi \Vdash \appInfAlg{A}{e}[a][1 \le 1] \redto \nil$.
\end{enumerate}
\end{corollary}

%-------------------------------------------------------------------------------
\subsection{Decidability}

\begin{figure}[h!]
    \begin{minipage}[t]{.5\textwidth}
        \centering\framebox{$|\Gm|_e$, $|\jg|_e$, $|e|_e$} {Size measure.}
        \begin{gather*}
            \begin{aligned}
            |\Gm|_e &= \sum_{\jg \in \Gm} |\jg|_e\\[2mm]
            |A \le B|_e &= 0\\
            |e \Lto A|_e &= |e|_e\\
            |e \To \jg|_e &= |e|_e + |\jg|_e\\
            |\appInfAlg{A}{e}|_e &= |e|_e + |\jg|_e\\[2mm]
            |x|_e &= |()|_e = 1\\
            \qquad |\lam e|_e &= |e|_e + 1\\
            |e_1~e_2|_e &= |e_1|_e + |e_2|_e + 1\\
            |e:A|_e &= |e|_e + 1
            \end{aligned}
        \end{gather*}

        \centering\framebox{$|\Gm|_\Leftrightarrow$, $|\jg|_\Leftrightarrow$} {Judgment measure.}
        \begin{gather*}
            \begin{aligned}
            |\Gm|_\Leftrightarrow &= \sum_{\jg \in \Gm} |\jg|_e\\[2mm]
            % 2\cdot\#_{e\Lto A\in\Gm} +
            %    \#_{e\To_a \jg\in\Gm} + 3\cdot\#_{\appInfAlg{A}{e}\in\Gm}
            |A \le B|_\Leftrightarrow &= 0\\
            |e \Lto A|_\Leftrightarrow &= 2\\
            |e \To \jg|_\Leftrightarrow &= |\jg|_\Leftrightarrow + 1\\
            |\appInfAlg{A}{e}|_\Leftrightarrow &= |\jg|_\Leftrightarrow + 3
            \end{aligned}
        \end{gather*}
        \centering\framebox{$|\Gm|_{\al}$} {Existential measure.}
        \begin{gather*}
            \begin{aligned}
            |\Gm|_{\al} &= \#_{\al\in\Gm}\\
            \end{aligned}
        \end{gather*}
    \end{minipage}%
    \begin{minipage}[t]{.5\textwidth}
        \centering\framebox{$|\Gm|_\forall$, $|\jg|_\forall$, $|A|_\forall$} {Polymorphism measure.}
        \begin{gather*}
            \begin{aligned}
            |\Gm|_\Leftrightarrow &= \sum_{\jg \in \Gm} |\jg|_\forall\\[2mm]
            |A \le B|_\forall &= |A|_\forall + |B|_\forall\\
            |e \Lto A|_\forall &= |A|_\forall\\
            |e \To \jg|_\forall &= |\jg|_\forall\\
            |\appInfAlg{A}{e}|_\forall &= |A|_\forall + |\jg|_\forall\\[2mm]
            |1|_\forall &= |a|_\forall = |\al|_\forall = 1\\
            |A\to B|_\forall &= |A|_\forall + |B|_\forall\\
            |\all A|_\forall &= |A|_\forall + 1
            \end{aligned}
        \end{gather*}

        \centering\framebox{$|\Gm|_\to$, $|\jg|_\to$, $|A|_\to$} {Function measure.}
        \begin{gather*}
            \begin{aligned}
            |\Gm|_\to &= \sum_{\jg \in \Gm} |\jg|_\forall\\[2mm]
            |A \le B|_\forall &= |A|_\forall + |B|_\forall\\
            |e \Lto A|_\forall &= |A|_\forall\\
            |e \To \jg|_\forall &= |\jg|_\forall\\
            |\appInfAlg{A}{e}|_\forall &= |A|_\forall + |\jg|_\forall\\[2mm]
            |1|_\to &= |a|_\to = |\al|_\to = 1\\
            |A\to B|_\to &= |A|_\to + |B|_\to + 1\\
            |\all A|_\to &= |A|_\to
            % \sum_{e\Lto A\in\Gm}|A|_\to + \sum_{\appInfAlg{A}{e}\in\Gm}|A|_\to +
            %     \sum_{A\le B\in\Gm} |A|_\to + |B|_\to\\
            \end{aligned}
        \end{gather*}
    \end{minipage}
\Description{Worklist measures}
\caption{Worklist measures}
\label{fig:measures}
\end{figure}

Finally, we show that our algorithm is decidable:
\begin{theorem}[Decidability]
Given \emph{wf} $\Gm$, it is decidable whether $\Gm\redto\nil$ or not.
\end{theorem}
Our decidability proof is based on a lexicographic group of induction measures\\
$\langle |\Gm|_e, |\Gm|_\Leftrightarrow, |\Gm|_\forall, |\Gm|_{\al}, |\Gm|_\to + |\Gm| \rangle$
on the worklist $\Gm$. Formal definitions of these measures can be found in
Figure~\ref{fig:measures}.
The first two,
$|\cdot|_e$ and $|\cdot|_\Leftrightarrow$, measure the total size of terms
and the total difficulty of judgments, respectively. In the latter, check judgments
count for 2, inference judgments for 1 and function inference judgments for 3.
Another two measures, $|\cdot|_\forall$ and $|\cdot|_\to$, count the total number of
universal quantifiers and function types, respectively. Finally,
$|\cdot|_{\al}$ counts the number of existential variables in the worklist,
and $|\cdot|$ is simply the length of the worklist.

It is not difficult to see that all but two algorithmic reduction rules
decrease the group of measures.  (The result of Rule 29 could be directly
reduced by Rule 28, which decreases the measures.) The two exceptions are Rules
10 and 11. Both rules increase the number of existential variables without
decreasing the number of universal quantifiers. However, they are both
immediately followed by Rule 7, which breaks the subtyping problem into two
smaller problems of the form $\al \le A$ and $A \le \al$ which we call
\emph{instantiation judgments}.

We now show that entirely reducing these smaller problems leaves the worklist
in a state with an overall smaller measure. Our starting point is a
worklist $\Gm,\Gm_i$ where $\Gm_i$ are instantiation judgments.
\begin{gather*}
\begin{aligned}
\Gm_i &:= \nil \mid \Gm_i, \al\le A \mid \Gm_i, A\le\al \quad
    \text{where } \al\notin FV(A) \cup FV(\Gm_i)
\end{aligned}
\end{gather*}
Fully reducing these instantiation judgments at the top of the worklist
has a twofold impact. Firstly, new entries may be pushed onto the worklist which
are not instantiation judgments. This only happens when $\Gm_i$ contains a universal quantifier
that is reduced by Rule 8 or 9. The new entries then are of the form $\Gm_\le$:
\begin{gather*}
\begin{aligned}
\Gm_\le &:= \nil \mid \Gm_\le, a \mid \Gm_\le, \al \mid \Gm_\le, A\le B
\end{aligned}
\end{gather*}
Secondly, reducing the instantiation judgments may also affect the remainder of the worklist $\Gm$,
by solving existing existentials and introducing new ones. This worklist update is captured in the
update judgment $\Gm \jExt \Gm'$ defined in Figure~\ref{fig:worklist_ext}.
For instance, an existential variable instantiation,
$\Gm_L,\al,\Gm_R \jExt \Gm_L, \al[1], \al[2], [\al[1] \to \al[2] / \al]\Gm_R$,
can be derived as a combination of the three rules that define the update relation.

\begin{figure}
\hfill \framebox{$\Gm\jExt\Gm'$} \hfill \llap{$\Gm$ updates to $\Gm'$}.
\begin{gather*}
\inferrule*[right=$\mathtt{\jExt id}$]
    {~}{\Gm\jExt\Gm}
\qquad
\inferrule*[right=$\mathtt{\jExt solve}$]
    {|A|_\forall = 0 \quad \Gm_L,[A/\al]\Gm_R \jExt \Gm'}{\Gm_L,\al,\Gm_R \jExt \Gm'}
\qquad
\inferrule*[right=$\mathtt{\jExt \al}$]
    {\Gm_L,\al,\Gm_R \jExt \Gm'}{\Gm_L,\Gm_R \jExt \Gm'}
\end{gather*}
\Description{Worklist Update}
\caption{Worklist Update}\label{fig:worklist_ext}
\end{figure}

The good news is that worklist updates do not affect the three main worklist measures:
\newcommand{\equivGm}[1]{|\Gm|_{#1} = |\Gm'|_{#1}}
\begin{lemma}[Measure Invariants of Worklist Extension]\label{lemma:inst:invariant}
If $\Gm\jExt\Gm'$ then $\equivGm{e}$ and $\equivGm\Leftrightarrow$ and $\equivGm\forall$.
\end{lemma}

Moreover, we can characterize the reduction of the instantiation judgments as follows.
\begin{lemma}[Instantiation Decidability]\label{lemma:inst:decidable}
For any well-formed algorithmic worklist $(\Gm, \Gm_i)$:
\begin{enumerate}[1)]
    \item If $|\Gm_i|_\forall = 0$,
        then there exists $\Gm'$\\
        s.t. $(\Gm, \Gm_i) \redto \Gm'$ and $|\Gm'|_{\al} = |\Gm|_{\al} - |\Gm_i|$ and $\Gm\jExt\Gm'$.
    \item If $|\Gm_i|_\forall > 0$,
        then there exist $\Gm', \Gm_\le$\\
        s.t. $(\Gm, \Gm_i) \redto (\Gm', \Gm_\le)$ and $|\Gm_\le|_\forall = |\Gm_i|_\forall - 1$ and $\Gm\jExt\Gm'$.
\end{enumerate}
\end{lemma}
Hence, reducing the instantiation judgment prefix $\Gm_i$ either
decreases the number of universal quantifiers or
eliminates one existential variable per instantiation judgment.
The proof of this lemma proceeds by induction on the measure $2*|\Gm_i|_\to + |\Gm_i|$
of the instantiation judgment list $\Gm_i$.

Let us go back to the whole algorithm and summarize our findings.
The decidability theorem is shown through a lexicographic group of induction measures
$$\langle |\Gm|_e, |\Gm|_\Leftrightarrow, |\Gm|_\forall, |\Gm|_{\al}, |\Gm|_\to + |\Gm| \rangle$$
which is decreased by nearly all rules.
In the exceptional case that the measure does not decrease immediately,
we encounter an instantiation judgment at the top of the worklist. We can then
make use of Lemma~\ref{lemma:inst:decidable} to show that $|\Gm|_{\al}$ or $|\Gm|_\forall$ decreases
when that instantiation judgment is consumed or partially reduced.
Moreover, Lemma~\ref{lemma:inst:invariant} establishes
that no higher-priority measure component increases.
Hence, in the exceptional case we have an overall measure decrease too.

Combining all three main results (soundness, completeness and decidability), we conclude that the declarative system is decidable
by means of our algorithm.
\begin{corollary}[Decidability of Declarative Typing]
Given \emph{wf }$\Om$, it is decidable whether $\Om\redto\nil$ or not.
\end{corollary}

% ---------------------------------------------
\subsection{Abella and Proof Statistics}

We have chosen the Abella (v2.0.7-dev
\footnote{We use a development version because the developers
just fixed a serious bug that accepts a simple proof of false,
which also affects our proof.
Specifically, our scripts compile against commit 92829a of Abella's GitHub repository.}
) proof assistant~\citep{AbellaDesc} to develop our formalization.
Abella is designed to help with formalizations of programming languages,
due to its built-in support for variable binding and
the $\lambda$-tree syntax~\citep{miller2000abstract}, which is a form of HOAS.
Nominal variables, or $\nabla$-quantified variables, are used as an unlimited name supply,
which supports explicit freshness control and substitutions.
Although Abella lacks packages, tactics and support for modules,
its higher-order unification and the ease of formalizing substitution-intensive relations
are very helpful.

While the algorithmic rules are in a small-step style,
the proof script rewrites them into a big-step style
for easier inductions.
In addition, we do prove the equivalence of the two representations.

\paragraph{Statistics of the Proof}
The proof script consists of 7,977 lines of Abella code with a total of
60 definitions and 596 theorems.
Figure~\ref{table:proof_statistics} briefly summarizes the contents of each file.
The files are linearly dependent due to the limitations of Abella.

\begin{table}[t]
\renewcommand{\arraystretch}{1.2}
\caption{Statistics for the proof scripts}
\centering\begin{tabular}{@{}lrrl@{}}
\toprule
    File(s) & LOC & \#Thm & Description\\
\midrule
    olist.thm, nat.thm  &   311 & 57  & Basic data structures\\
    typing.thm          &   245 & 7   & Declarative \& algorithmic system, debug examples\\
    decl.thm            &   226 & 33  & Basic declarative properties\\
    order.thm           &   235 & 27  & The $|\cdot|_\forall$ measure; decl. subtyping strengthening\\
    alg.thm             &   679 & 80  & Basic algorithmic properties\\
    trans.thm           &   616 & 53  & \makecell[l]{Worklist instantiation and declarative transfer;\\
                            Lemmas~\ref{lem:insert}, \ref{lem:extract}}\\
    declTyping.thm      &   909 & 70  & \makecell[l]{Non-overlapping declarative system; \\
                            Lemmas~\ref{lem:inv_allR}, \ref{lem:inv_chkAll},
                                \ref{lem:inv_chkLam}, \ref{lem:subsumption}}\\
    soundness.thm       & 1,107 & 78  & Soundness theorem; aux. lemmas on transfer\\
    depth.thm           &   206 & 14  & The $|\cdot|_\to$ measure; Lemma~\ref{lem:prune_inst}\\
    dcl.thm             &   380 & 12  & Non-overlapping declarative worklist \\
    completeness.thm    & 1,124 & 61  &
                            Completeness theorem; aux. lemmas and relations\\
    inst\_decidable.thm &   837 & 45  & Other worklist measures; Lemma~\ref{lemma:inst:decidable}\\
    decidability.thm    &   983 & 57  & Decidability theorem and corollary\\
    smallStep.thm       &   119 &  2  & The equivalence between big-step and small-step\\
\midrule
    \emph{Total}        & 7,977 & 596 & (60 definitions in total)\\
\bottomrule
\end{tabular}
\label{table:proof_statistics}
\end{table}

\paragraph{Translation Table for the Proof Scripts}
In the proof scripts, we use textual relational definitions
rather than the symbolic ones used in the paper.
The mapping, shown in Table~\ref{table:translation_table}, should be helpful for anyone who reads the script.

\afterpage{%
\clearpage% Flush earlier floats (otherwise order might not be correct)
\begin{landscape}% Landscape page

\begin{table}[ht]
\renewcommand{\arraystretch}{1.3}
\caption{Translation Table for the Proof Scripts}
\centering
\begin{tabular}{@{}llll@{}}
\toprule
Symbol & Abella textual relation & File (.thm) & Description\\
\midrule
$A$    & \texttt{ty, wft, wfta} & typing &
    The "type" type, decl./alg. well-formedness\\
$e$    & \texttt{tm, wftm} & typing &
    The "term" type, (alg.) well-formedness\\
$\Psi, \Gm$ & \texttt{olist, wfj} & typing &
    \texttt{olist} is Abella's built-in list type, i.e. \texttt{[o]}\\
$\jg$  & \texttt{judgment, wfjg} & typing &
    Judgment chain and its well-formedness\\

$\Gm\rto\Gm'$ & \texttt{reduction} & smallStep &
    Algorithmic reduction: paper version\\
$\Gm\redto\nil$ & \texttt{judge} & typing &
    (\texttt{judge} $\Gm$): a success reduction on $\Gm$\\

$\Gm \sto \Om$ & \texttt{tex} & trans &
    "transfer existential variables"\\
$\Om\redto\nil$ & \texttt{dc, dcl} & trans, dcl &
    Declarative chain representation\\

$\Psi'\le\Psi$ & \texttt{esub} & declTyping & Declarative context subtyping\\

$\Gm\jExt\Gm'$ & \texttt{jExt} & inst\_decidable & "Judgment extension"\\
$\Gm_i$ & \texttt{iexp} & inst\_decidable & instantiation judgments ($\Gm \vdash_{wf} \Gm_i$)\\
$\Gm_\le$ & \texttt{subExp} & inst\_decidable & subtyping judgments\\

$|\cdot|_e$ & \texttt{tmSize, tmSizeJ, tmSizel} & declTyping, decidability &
    Size measure for term, judgment chain or context\\
$|\cdot|_\Leftrightarrow$ & \texttt{m\_judgeJ, m\_judge} & decidability &
    Judgment measure for judgment chain or context\\
$|\cdot|_{\al}$ & \texttt{nVar} & inst\_decidable &
    Existential measure for context\\
$|\cdot|_\forall$ & \texttt{order, orderJ, orderl} & order, inst\_decidable &
    Polymorphism measure for type, judgment chain or context\\
$|\cdot|_\to$ & \texttt{depth, depthJ, depthl} & order, decidability &
    Function measure for type, judgment chain or context\\
\bottomrule
\end{tabular}
\label{table:translation_table}
\end{table}

\end{landscape}
\clearpage% Flush page
}