% TODO: introduce notation and concept of fv/bv and fn/bn

\chapter{Background}\label{chapter:background}

\textit{
This chapter explores what \emph{formal systems} are and what they are useful for.
It looks at a number of related formal systems and their relation to computation.
It outlines the context on which the rest of this project is built.
}

% TODO: explain that ideas will become clearer as they are
%       used and we see how they interact with other concepts
%       introduced later. it's ok to leave concepts not entirely
%       developed

\section{Formal Systems}

Formal systems are sets of rules for writing and manipulating
formul\ae. Formul\ae\ are constructed from a set of characters called the
\emph{alphabet} by following some formul\ae-construction rules called
the \emph{grammar}. The only formul\ae\ considered \emph{well formed} in
a system are those constructed according to the grammar of a system.
Formal systems are used to model domains of knowledge to help better
and more formally understand those domains.

\subsection{Syntax and Grammars}

The grammar of a formal system describes the system's syntax. Grammars are
rules for constructing formul\ae\ that are well formed. Formul\ae\
produced according to the grammar of a system are well formed according to
the syntax of that system.

Grammars are normally defined by using Backus-Naur Form or BNF:

\[
  M ::= t\ \mid\ f
\]

This grammar describes that syntactically-valid constructs are either the letter $t$ or the letter $f$.
Grammars can be recursive which allows much more expressive construction rules:

\begin{figure}[!h]\label{fig:tf-grammar}
\[
  \begin{array}{lclr}
    M,N&::=&t&\textcolor{pale-gray}{(1)} \\
      &|&f&\textcolor{pale-gray}{(2)} \\
      &|&M\ a\ N&\textcolor{pale-gray}{(3)} \\
  \end{array}
\]
\caption{Grammar for producing the letters $t$ or $f$ connected by the letter $a$}
\end{figure}

This grammar describes formul\ae\ containing any number of occurrences of the letters $t$ or $f$ separated by an $a$.
\[
\begin{array}{ll}
  \by{t}{1} \\
  \by{t\ a\ f}{2 \& 3} \\
  \by{t\ a\ f\ a\ t}{1 \& 3}
\end{array}
\]

\subsection{Derivation Rules}
Whereas a grammar describes the rules for producing well-formed formul\ae,
the derivation rules describe rules for transforming formul\ae\ of a particular form into new formul\ae.
Using the grammar from Figure \ref{fig:tf-grammar}, we add derivation (rewriting) rules:

\[
\begin{array}{lcl}
  t\ a\ M &\to& M \\
  M\ a\ t &\to& M \\
  f\ a\ M &\to& f \\
  M\ a\ f &\to& f \\
\end{array}
\]

These rules describe that if a formula matches the pattern on the
left-hand side, where $M$ represents a well-formed formula, it can be
replaced by the formula on the right-hand side.

\subsection{Domain Modelling}

The syntax and derivation rules of a formal system are defined to model
some domain. This isomorphism between the domain and the formal system
means we can attempt to discover truths about the domain through studying
the formal system.

For example, take the $tf$-system described above.
Without understanding the domain, we are able to manipulate formul\ae\ of the system to create new formul\ae.
Without going into detail, it is easy to verify that the $tf$-system is isomorphic to a subset of Boolean algebra:

\[
\begin{array}{cc}
\text{$tf$-system} & \text{Boolean algebra} \\
t & 1 \\
f & 0 \\
a & \wedge \\
\end{array}
\]

Using formal systems allows us to understand the domains they model from
different perspectives and thereby learn novel truths about them.

\subsection{Derivation Strategies}
% TODO: explain that when you can make more than one decision,
%       you can have a strategy for which decision you take

When applying derivation rules to compound terms, we can imagine the
compound term being decomposed into simpler terms until one of the
derivation rules applies. When we decompose a term, we separate it into a
dominant term and a context.

\begin{figure}[!h]\label{fig:decomposing}
\[
\begin{array}{lcr}
  & t\ o\ f\ a\ t \\
  t\ o\ f\ && \square\ a\ t
\end{array}
\]
\end{figure}

The left-hand side is the dominant term and the right-hand side is the context.
The $\square$ in the context denotes a hole that needs to be filled to create a full term.
Once we have a term that we can apply a derivation rule to,
we apply the derivation rule and recombine the context with the resulting term.
For example, we start with the term:
\[
  t\ a\ f\ a\ t
\]
This term is decomposed into a dominant term (on the left) and a context (on the right):
\[
  t\ a\ f \quad\quad \square\ a\ t
\]
Using the derivation rules from above, we can rewrite the dominant term $t\ a\ f$ as $f$:
\[
  f \quad\quad \square\ a\ t
\]
Finally, once the dominant term cannot be reduced further, we recombine it with the context to get:
\[
  f\ a\ t
\]

However the the term $t\ a\ f\ a\ t$ can be decomposed in two ways:
\[
\begin{array}{rlr}
  1. & t\ a\ f\ & \square\ a\ t  \\
  2. & f\ a\ t & t\ a\ \square  \\
\end{array}
\]
When we have more than one way derivation rules can be applied to a term,
we can use \emph{derivation strategies} to determine which rule we apply.
For compound terms, derivation strategies decide which derivation rule to applied to which subterm.
In our $tf$-system, there are two obvious derivation strategies:
either apply derivations starting from the left or starting from the right.

\[
\begin{array}{rcl}

  \textbf{(left)} & t\ a\ f\ a\ t \\
  \to                & t\ a\ f & \square\ a\ t \\
  \to                & f       & \square\ a\ t \\
  \textit{recombine} & f\ a\ t & \\
  \\
  \textbf{(right)}   & t\ a\ f\ a\ t \\
  \to                & f\ a\ t & t\ a\ \square \\
  \to                & f       & t\ a\ \square \\
  \textit{recombine} & t\ a\ f & \\

\end{array}
\]
Whereas derivation rules are defined in the system,
derivation strategies are strategies \emph{about} the system: methods for choosing which derivation rule to apply when given a choice.

\section{\lam-Calculus}

In response to Hilbert's \emph{Entscheidungsproblem}, Alonzo Church defined the \lam-calculus \cite{ChurchEnt36}.
It is a formal system capable of expressing the set of effectively-computible algorithms.
Shortly after, G{\"o}del and Turing created their own models of effective computibility.
\footnote{General recursive functions and Turing machines, respectively}
These models were later proved to be equivalent.

\subsection{Syntax}

  \lam-variables are taken from an infinite set and are written $x,y,z$, etc. Variables are open positions in a term that can be filled by other terms.
  The grammar for constructing well-formed \lam-terms is:

  \begin{figure}[!h]
  \definition{
    \textsc{(Grammar for untyped \lam-calculus)}
    \item $`l$-variables are denoted by $x, y,\dots$ \\
    \[
    \begin{array}{rcll}
    M,N & ::= & x         & \text{(Variable)} \\
        & \mid\ & `lx.M & \text{(Abstraction)} \\
        & \mid\ & (M\ N)   & \text{(Application)} \\
    \end{array}
    \]
  }
  \end{figure}

  \lam-abstractions are anonymous functions and are represented by $`lx.M$ where $x$ is a parameter and $M$ is the body of the abstraction.
  The same idea is expressed by more conventional notation as a mathematical function $f(x) = M$.
  The $`l$ annotates the beginning of an abstraction and the $.$ separates the parameter from the body of the abstraction.
  This grammar is recursive, meaning the body of an abstraction is just another term constructed according to the grammar.
  Some examples of abstractions are:

  \begin{figure}[!h]
    \[
      \begin{array}{l}
      `lx.x \\
      `lx.xy \\
      `lx.(`ly.xy)
      \end{array}
    \]
  \caption{Examples of valid \lam-abstractions}
  \end{figure}

  Applications are represented by any two terms,
    both constructed according to the grammar,
  placed alongside one another.
  We assume the convention that application gives highest precedence to the left-most application.
  This means we can drop brackets where our convention will prevent ambiguity.
  Note that this means that if the term is an application, the outermost brackets can always be dropped.
  For example, we can drop the outermost, leftmost brackets from the term $((xy)z)$ and instead write $xyz$.
  To ensure the application of terms in any other order, we must leave the brackets explicit.
  For example the brackets in the term $x(yz)$ cannot be removed without changing the meaning of the term.
  Examples of applications are:
    \begin{figure}[!h]
      \[
        \begin{array}{l}
        (xy) \\
        xyz \\
        x(yz) \\
        (`lx.x)y
        \end{array}
      \]
    \caption{Examples of valid applications}
    \end{figure}

\subsection{Reduction Rules}

  The reduction rules for the \lam-calculus are like the derivation rules from formal systems.
  They determine how terms of a particular form can be transformed into new terms.

  First we will introduce the substitution notation $M[N/x]$. This denotes
  the term M with all occurrences of x replaced by N. The substitution
  notation is defined inductively as:

    \begin{figure}[!h]
    \definition{
      \textsc{(Substitution notation for \lam-terms)}
      \[
      \begin{array}{rclr}
      x[y/x] & \rightarrow & y \\
      z[y/x] & \rightarrow & z & (z \neq x) \\
      (`lz.M)[y/x] & \rightarrow & `lz.(M[y/x]) \\
      (M N)[y/x] & \rightarrow & M[y/x] N[y/x]
      \end{array}
      \]
    }
    \end{figure}

\subsubsection{\bta-reduction}
  The main derivation rule of the \lam-calculus is \bta-reduction. If term
  $M$ \bta-reduces to term $N$, we write $M \rightarrow_{`b} N$ although
  the \bta\ subscript can be omitted if it is clear from context. \bta-reduction
  is defined for the application of two terms:
  \begin{figure}[!h]\label{def:beta-reduction}
  \definition{
    \textsc{($`b$-reduction for \lam-calculus)}
    \cite{Bakel15}
    \\
    $i)$ $\to_{`b}$ is defined as the compatible closure of the rule:
    \[
    \begin{array}{rcl}
    (`lx.M) N & \rightarrow_{`b} & M[N/x]
    \end{array}
    \]
    $ii)$ $\to_{`b}^{*}$ is defined as the transitive closure of $\to_{`b}$
  }
  \end{figure}

  \lam-variables and \lam-abstactions are \emph{values}: they do not reduce to other terms.
  If a term is a value, the reduction cannot be applied to that term: it has \emph{terminated} on that value.
  Only applications whose left-hand side is an abstraction can reduce to other terms.
  This means that an application of this form is called a reducible expression or a \emph{redex}.
  Reducing a redex models the computing of a function.

% TODO: define free variables and bound variables
\subsubsection{$`a$-reduction}

  The variables in a \lam-term are either \emph{bound} or \emph{free}.
  The bound variables of a term are those introduced by \lam-abstractions before their use.

  \definition{
  \textsc{Bound variables $bv$ of \lam-terms}
  \cite{Bakel15}
  \[
  \begin{array}{lcl}
    bv(x)     & = & \varnothing \\
    bv(`lx.M) & = & bv(M) \cup \{x\} \\
    bv(MN) & = & bv(M) \cup bv(N) \\
  \end{array}
  \]
  }
  \definition{
  \textsc{Free variables $fv$ of \lam-terms}
  \cite{Bakel15}
  \\
  The free variables of a term $M$ are variables that occur in $M$ that are not bound.
  \\
  }

  % TODO: explain this in terms of bound and free variables
  The \lam-calculus defines a reduction rule for renaming variables.
  Variable names are arbitrary and chosen just to denote identity:
  all occurrences of $x$ are the same. This can become a problem
  in the following case:
  \[
    (`ly.`lx.xy)(`lx.x)
  \]
  After the application is reduced, we have the term:
  \[
    `lx.x(`lx.x)
  \]
  In this case, it is ambiguous which \lam-abstraction the right-most $x$
  is bound by. When this term is applied to another, will the substitution
  occur to all occurrences of $x$? From the initial term, it is clear that
  this would be incorrect. The \lam-calculus introduces $`a$-reduction to
  solve this:
  \begin{figure}[H]
  \definition{
    \label{def:alpha-reduction}
    \textsc{($`a$-reduction for \lam-calculus)}
  \[
    \begin{array}{rl}
    `lx.M \rightarrow_{`a} `ly.M[y/x] & (y \notin fv(M))
    \end{array}
  \]
  }
  \end{figure}
  This means we can rename the lead variable of an abstraction $M$ on the
  conditions that:
  \begin{enumerate}
    \item All variables bound by that abstraction are renamed the same
    \item The variable it is changed to does not already appear in $M$
  \end{enumerate}
  Returning to the term above, we can $`a$-reduce the right-hand term to reduce ambiguity before $`b$-reducing the whole term:
  \[
  \begin{array}{rll}
             & (`ly.`lx.xy)(`lx.x) \\
    \to_{`a} & (`ly.`lx.xy)(`lz.z) & (z \not= x \land z \not= y) \\
    \to_{`b} & `lx.x(`lz.z)
  \end{array}
  \]
  Using $`a$-reduction, we can ensure that the binding of every variable in a term is unambiguous.

  \subsection{Reduction Strategies}

  A term is in normal form for a reduction strategy if,
    following that reduction strategy,
  no more reductions can take place.
  For instance, a term that can no longer be \bta-reduced is in \bta-normal form.
  We use $\to_{`b}^{nf}$ to denote a term in \bta-normal form.
  Again, we can omit the \bta\ subscript if it is clear from context.

  Lazy reduction is a reduction strategy that restricts the reduction to the outermost, leftmost redex.
  Any other subterms that are redexes cannot be reduced.
  \definition{
  \textsc{Lazy reduction of \lam-terms}\\
  $i)$ The outermost, leftmost term is the only one that can be reduced \\
  $ii)$ No other reductions can take place, including underneath an abstraction \\
  $iii)$ We use $\to^{L}$ to denote lazy reduction
  }
  \\
  \\
  For example, let us begin \bta-reducing $(`lx.`ly.xy)(`lz.z)$:
  \[
  \begin{array}{rll}
            & (`lx.`ly.xy)(`lz.z) \\
    \to_{`b}& `ly.(`lz.z)y
  \end{array}
  \]
  Whereas \bta-reduction would continue and reduce the inner redex $(`lz.z)y$,
  lazy reduction does not reduce beneath \lam-abstractions.
  This term is in normal form for lazy reduction.

\section{Logic and Types}

  There are many formal systems for describing logic.
  These systems attempt to describe the relationship between logical statements.
  Like all formal systems,
  they allow us to derive new logical statements by following transformation rules.

  Logic relates statements together with logical connectives.
  Under an interpretation, a statement is either true or false.
  Depending on the truth value of the component statements, a compound statement is either true or false.
  As far as we are concerned, the logical connectives consist of $\land, \lor, \to$, and $\neg$.
  \begin{itemize}
    \item $M \land N$ (read: `and') is true only when both $M$ and $N$ are true.
    \item $M \lor N$ (read: `or') is true only if either $M$ or $N$ are true.
    \item $M \to N$ (read: `implies') is false when $M$ is true but $N$ is false.
    \item $\neg M$ (read: `not') is true only when $M$ is false.
  \end{itemize}
  The symbol $\to$ represents implication: whenever $M$ is true, $N$ is true.

  \subsection{Natural Deduction}

  Natural deduction is a system of logic.
  In natural deduction, there are \emph{introduction} and \emph{elimination} rules for each logical connective.
  These are presented as \emph{inference rules}.
  Logical inference rules describe that if some statement $A$ is true,
  then it follows that some other statement $B$ is true.
  This is denoted by:

  \[
    \Inf{A}{B}
  \]

  Using this notation, we can now describe the inference rules for logical implication, $\to$.
  For our purposes, this is the only logical connective we are interested in.
  The reason for this will become clear.

  \definition{
  \textsc{$\to$ introduction and elimination rules}
  \[
  \begin{array}{rl@{\quad\quad}rl}
    \Inf { A \to B \hspace{0.5cm} A }
        {B}
        {\hspace{0.2cm} \to \mathcal{E} }
    &
    \Inf { \infer*{B}{[A]} }
        { A \to B}
        {\hspace{0.2cm} \to \mathcal{I} }
  \end{array}
  \]
  }

  The $\to\mathcal{E}$ rule says that assuming the statement $A \to B$ and the statement $A$ are true,
  we can conclude that $B$ is the case. The rule is read as an implication.
  For instance, consider

  \[
  \begin{array}{lcl}
    A & = & \textit{It is raining} \\
    B & = & \textit{It is wet outside} \\
    A \to B & = & \textit{If (it is raining) then (it is wet outside)}
  \end{array}
  \]
  If we know that ``if (it is raining) then (it is wet outside)'' and we are told ``it is raining'',
  clearly we can take for granted ``it is wet outside''.

  The $\to\mathcal{I}$ rule says that if we assume $A$ and from that assumption we deduce $B$,
  we can conclude that $A$ implies $B$.
  Let us assume (where $[A]$ denotes that we assume $A$ is true):
  \[
    [A] = \textit{Turing did not see Church's work}
  \]
  From this assumption, it's clear that that
  \[
    B = \textit{Turing could not have stolen Church's work}
  \]
  The $\to\mathcal{I}$ rule lets us conclude from these statements that
  \[
    A \to B = \textit{If Turing did not see Church's work, he could not have stolen it}
  \]

  Note the removal of the assumption ([\ ]) from $A$.
  By restricting ourselves to these rules, we are working within \emph{Implicative Intuitionistic Logic} (IIL).
  IIL is a restricted form of logic.
  For example Pierce's law, which says $((P \to Q) \to P) \to P$, is not applicable to IIL.
  \footnote{IIL also rejects the law of excluded middle which says for every $P$, $P \lor \neg P$.
  According to intuitionistic logic, unless we have a \emph{constructive} proof for which of $P$ or $\neg P$ is true, the statement is false}

  \subsection{Sequent Calculus}

  Gentzen explored natural deduction through the sequent calculus \cite{Wadler15}.
  We have followed Girard \emph{et al.}'s observation that the syntax of the sequent calculus is overcomplicated for the purpose of natural deduction \cite{Girard89}.
  However it is useful for both classical natural deduction and type assignment systems (see Sections \ref{sec:classical-logic} and \ref{sec:type-assignment} respectively).

  Sequent calculus manipulates \emph{sequents} where a sequent is denoted by:

    \[
      `G \vdash `D
    \]

  On the left-hand side, the $`G$ represents a sequence of zero or more statements called the \emph{antecedent}.
  On the right-hand side of the $\vdash$, $`D$ represents a different sequence of zero or more statements called the \emph{succedent}.
  The whole sequent denotes that the conjunction of the statements in the antecedent imply the disjunction of the statements in the succedent.
  That is to say, if all the statements on the left are true then at least one of the statements on the right is true:

  \[
    \begin{array}{c}
    A_1,A_2,\dots,A_n \vdash S_1,S_2,\dots,S_m \\
      \text{denotes} \\
    A_1 \wedge A_2 \wedge \dots \wedge A_n \to S_1 \lor S_2 \lor \dots \lor S_m \\
    \end{array}
  \]

  \subsection{Classical Natural Deduction}\label{sec:classical-logic}

  Gentzen's classical natural deduction used the sequent calculus and introduced a set structural rules for manipulating sequents \cite{Groote94}.

  \definition{
  \textsc{Structural rules of classical natural deduction}
  \[
  \begin{array}{c@{\quad\quad}c}

    \Inf{`G,C,D,`G' \vdash `D}
       {`G,D,C,`G' \vdash `D}
       {\hspace{5pt}\mathcal{L}X}
    &
    \Inf{`G \vdash `D,C,D,`D'}
       {`G \vdash `D,D,C,`D'}
       {\hspace{5pt}\mathcal{R}X}
    \\
    \\
    \Inf{`G \vdash `D}
       {`G,C \vdash `D}
       {\hspace{5pt}\mathcal{L}W}
    &
    \Inf{`G \vdash `D}
       {`G \vdash `D,C}
       {\hspace{5pt}\mathcal{R}W}
   \\
   \\
   \Inf{`G,C,C \vdash `D}
       {`G,C \vdash `D}
       {\hspace{5pt}\mathcal{L}C}
    &
    \Inf{`G \vdash C,C,`D}
       {`G \vdash C,`D}
       {\hspace{5pt}\mathcal{R}C}
   \\
   \\
   \multicolumn{2}{c}{
    \Inf{`G \vdash `D,A \hspace{10pt} A,`G' \vdash `D'}
        {`G,`G' \vdash `D,`D'}
        {\hspace{5pt}\textbf{Cut}}
   }
  \end{array}
  \]
  }

  The first set of rules, $\mathcal{LX}$ and $\mathcal{RX}$, are left and right exchange rules.
  They express the commutativity of statements in the left and right sequences.
  The weakening rules, $\mathcal{LW}$ and $\mathcal{RW}$, allow the introduction of new formul\ae\ on either the left or right sequences.
  The contraction rules, $\mathcal{LC}$ and $\mathcal{RC}$, allow the contraction of multiple occurrences of a formula into a single occurrence.
  The cut rule allows us to replace assumptions of formul\ae\ with their concrete proofs, if we have proved them somewhere else.

  In addition to these, the system has a set of logical rules which relate closely to natural deduction.
  Although rules for all the logical connectives exist, we again choose to highlight just the cases important to this project:
  \definition{
  \textsc{Logical rules of classical natural deduction} \cite{Groote94}
  \[
  \begin{array}{c@{\quad\quad}c}
    \Inf{`G \vdash C,`D \hspace{10pt} `G',D \vdash `D'}
        {`G,`G',C \to D \vdash `D,`D'}
        {\hspace{5pt}\mathcal{L}\to}
    &
    \Inf{`G,C \vdash D,`D}
        {`G \vdash C \to D,`D}
        {\hspace{5pt}\mathcal{R}\to}
    \\
    \\
    \Inf{`G \vdash C,`D}
        {`G,\neg C \vdash `D}
        {\hspace{5pt}\mathcal{L}\neg}
    &
    \Inf{`G,C \vdash `D}
        {`G \vdash \neg C,`D}
        {\hspace{5pt}\mathcal{R}\neg}
  \end{array}
  \]
  }
  Moving a statement from the left-hand sequent to the right negates that statement.
  The same is true moving a statement from the right-hand sequent to the left.
  The implication rules allow the introduction of $\to$ in either the left or right sequent.

  \subsection{Type Assignment}\label{sec:type-assignment}

  There are many type assignment different systems.
  Systems of type assignment introduce additional grammar and restrictions on the reduction rules of a formal system.
  These extensions prevent logically inconsistent terms from being constructed.
  A type assignment has the form:
  \[
    M : `a
  \]
  which states that term $M$ has the type $`a$.
  Like variables, type variables are abstract:
  they do not describe anything more about a type than its identity.
  That is to say $x: A$ and $y : A$ have the same type but we cannot say any more about what that type is.

  A type is either some uppercase Latin letter or it is two valid types connected by a $\rightarrow$.
  This is described by the following BNF grammar:
 \\
  \definition{
    \textsc{(Grammar for constructing types)}
    \item Type variables are represented by the lower-case greek alphabet $`a,`b,`g,...$
    \[
      A ::= `v \mid `v \rightarrow B
    \]
  }

  \subsection{Typed \lam-Calculus}
  The typed \lam-calculus is an extension of the \lam-calculus with types assigned to \lam-terms.
  \lam-abstractions have arrow types: $A \to B$.
  This describes that the abstraction can only be applied to a value of type $A$ and returns one of type $B$.
  Type assignment rules for the \emph{typed} \lam-calculus are:

  \[
  \begin{array}{c@{\quad\quad}c@{\quad\quad\quad}c}
    \Inf{ `G,x:A \vdash x : A }
    &
    \Inf{
      `G \vdash M : A \to B \hspace{20pt} N:A
    }{
      `G \vdash M N : B
    }
    &
    \Inf{
      `G,x:A \vdash M : B
    }{
      `G \vdash `lx.M : A \to B
    }
    \vspace{5pt}
    \end{array}
  \]
  Similarly to the sequent calculus,
  the letter $`G$ represents a sequence of type assignment statements.
  In $`G$, each variable can only have one type.
  The form of these rules denote that the typing judgement on the right-hand side of the $\vdash$ are a consequence of the sequence of type assignment statements in $`G$.
  The first rule states that the application of a term of type $A \to B$ to
  a term of type $A$ has type $B$.
  The second rule says a term of type $B$ in a context where $x:A$ is the same as an abstraction of type $A \to B$ in a context without $x:A$.
  These rules add restrictions on what constitutes a well-formed term.
  These restrictions prevent the formation of terms with undesirable properties.

  \begin{example}[Type-assignment restricts set of valid terms]
  \[
  \begin{array}{lr}
    (`lx.xx)(`lx.xx) \\
    xx : B \\
    x : A \to B \\
    x : A
  \end{array}
  \]
  The variable $x$ is applied to a term so it must have type $A \to B$.
  The term it is applied to must have type $A$.
  However $x$ is applied to itself so it must have type $A$ and $A \to B$.
  This means the term $`lx.xx$ is untypable according to this simple type assignment system:
  the conflicting types for $x$ mean the term is disallowed by the rules of the typed \lam-calculus.
  \end{example}

  \subsection{Curry-Howard Isomophism}

  The Curry-Howard isomorphism states that there is a true isomorphism between the type of a term and a logical proposition.
  The type of a term is a logical proposition and the term itself its a proof of that proposition.
  The simplification of a proof maps to the evaluation of the corresponding program \cite{Wadler15}.

  Looking again at the type assignment rules of the typed \lam-calculus and the inference rules of IIL,
  the correspondence is clear:

  \[
  \begin{array}{cc@{\quad\quad}c}
    & `l\textbf{-calculus} & \textbf{IIL}
    \\
    \\
    \to\mathcal{E}
    &
    \Inf{ `G \vdash M : A \to B \hspace{20pt} N:A }
        { `G \vdash M N : B }
    &
    \Inf{ A \to B \hspace{0.5cm} A }
        {B}
    \\
    \\
    \to\mathcal{I}
    &
    \Inf{ `G,x:A \vdash M : B }
      { `G \vdash `lx.M : A \to B }
    &
    \Inf{ \infer*{B}{[A]} }
        { A \to B}
   \\
    \end{array}
  \]

  The correspondence between logic and programs is not limited to IIL and the typed \lam-calculus.
  There are many features of computer programs that have counter-parts in logical systems.
  % TODO: add more - examples and demonstration

\section{Haskell}
  % TODO: describe purity and laziness
  % TODO: add section on exceptions

  \subsection{Data Types}
  New data types can be introduced into Haskell in 3 distinct ways. First,
  using the \mono{data} keyword:

  \Verbatimcode
    data Animal a = Dog a
      | Cat
  \end{Verbatim}

  The \mono{data} keyword begins the definition of a new data type. The
  word immediately following determines the type constructor for the new
  type. Following this is a type parameter for the type constructor. There
  can be any number of type parameters, including zero. The right-hand
  side of the \mono{=} introduces a \mono{|}-separated list of data
  constructors.

  \Verbatimcode
    > let hector = Cat
    > :t hector
    hector :: Animal a
    > let topaz = Dog "foo"
    > :t topaz
    topaz :: Animal String
  \end{Verbatim}

  The type parameter is constrained by the type of the value the data
  constructor was initialized with. In the example above, calling the
  \mono{Dog} data constructor with a string makes the type \mono{Animal
  String} rather than the more general \mono{Animal a}.

  The second method for introducing new data types is the \mono{newtype} keyword.
  The key difference between \mono{data} and \mono{newtype} is that \mono{newtype} can only have one data constructor and this data constructor can only have a single argument
  Informally, this implies a kind of isomorphism:

  \Verbatimcode
    newtype Foo a = Foo (a -> Integer)
  \end{Verbatim}
  The type constructor can take type parameters which will be constrained
  by the inhabitants of the data constructor. This data type expresses
  an isomorphism between \mono{Foo a} and functions from \mono{a} to
  \mono{Integer}s.

  Finally, we can introduce type aliases using the \mono{type} keyword:
  \Verbatimcode
    type Name = String
  \end{Verbatim}

  Again, we introduce a type constructor \mono{Name} but this time we name another type in place of a data constructor,
  in this case \mono{String}.
  This means that the type \mono{Name} is a type alias for \mono{String} and will share the same data constructors.

  \subsection{Type Level/Value Level}

  Haskell distinguishes between terms on the type level and terms on the value level.
  This is the same as the separate layer of terms and types in the typed \lam-calculus.
  Types in Haskell are descriptions of the types of a value.
  They provide restrictions on the construction of invalid terms.
  For instance if we have a function of type \mono{String -> Integer},
  we cannot apply it to a term of type \mono{Boolean}.
  The type checker will throw an error before any value-level computation is initiated.

  % TODO: better description needed
  The value level is the level on which data is constructed and manipulated.
  The operation \mono{1+1} occurs on the value level. The value level is
  where computation takes place and the type level is where static analysis
  of the program type takes place.

  \subsection{Type Classes}
  Haskell adds type classes to the type level. Types can have instances of
  type classes. The most similar concept from Object-Oriented programming
  is \emph{interfaces}.

  \Verbatimcode
    class Addable a where
      (add) :: a -> a -> a
  \end{Verbatim}

  Type classes are introduced using the \mono{class} keyword. Beneath that
  are the function names and corresponding type-signatures of the functions
  that an instance of a class must implement.

  For example, we can create instances of the \mono{Addable} class:
  \Verbatimcode
    data Number = One | Two | ThreeOrMore

    instance Addable Number where
      add One One = Two
      add _ _     = ThreeOrMore
  \end{Verbatim}
  To declare an instance of a type class, we must supply the bodies for functions in the class specification.
  The \mono{Addable} class, for instance, requires that the body of the \mono{add} function is defined.
  When declaring an instance, we have to ensure the type specification of each function is respected.

% TODO: these explanations of continuations (diff between delim and
%       undelim) are incorrect
\section{Continuations}

  As in the example in Figure \ref{fig:decomposing},
  compound \lam-terms can be evaluated into a dominant term and a context:
  \begin{figure}[H]\label{fig:decomposing-lambda}
    \hspace{1cm}Assume that $M \rightarrow^{*}_{`b} M^\prime$
    \[
    \begin{array}{lrcl}
    \textit{(Compound term)}&& MN \\
    \textit{(Decompose)}&M && \square N \\
    \textit{(Beta-reduce dominant term)}& M^\prime && \square N \\
    \textit{(Refill hole of context)}&& M^\prime N \\
    \end{array}
    \]
  \caption{Decomposing a term into a dominant term and a context}
  \end{figure}

  Contexts are partial terms that remain to be reduced after the reduction of the dominant term.
  After the dominant term has reduced to a value,
  that value will fill the hole ($\square$) of the context to form a new term.
  For example, consider the term
  \[
    (`lx.`ly.xy)(`lz.z)t
  \]
  According to our bracketing convention, the leftmost redex is reduced first:
  \[
    (`lx.`ly.xy)(`lz.z)
  \]
  After this term has reduced, the reduct will be applied to $t$:
  \[
    \square t
  \]
  In this way, the context $\square t$ holds information about the \emph{rest} of the reduction steps.
  The context of a term is the future computation of that term: what will happen to the term after it has been reduced.
  For this reason, the context is also called a \emph{continuation}.

  \subsection{Undelimited Continuations}

  For more complex terms, the waiting context will grow as the dominant term gets further decomposed:

  \begin{figure}[H]
    \[
    \begin{array}{ll}
      (MM^\prime) M^{\prime\prime} \\
      (MM^\prime) & \square M^{\prime\prime} \\
      M & (\square M^\prime) M^{\prime\prime} \\
    \end{array}
    \]
  \caption{Decomposing a term into multiple contexts}
  \end{figure}

  By amalgamating continuations into one big continuation we only have two components at any point during the reduction:
  the current dominant term and the \emph{current continuation}.

  By adding additional operators to a language, the continuations of terms can be exposed.
  This provides programmers with the ability to control continuations.
  Control operators that only allow manipulation of the entire remaining continuation are \textbf{undelimited continuations}.
  For example, Scheme's \mono{call/cc} operator aborts the entire remaining continuation.

  \subsection{Delimited Continuations}

  Instead, if we maintain continuations in a stack when decomposing complex terms,
  we can keep the continuations separated:

  \begin{figure}[H]
    \[
    \begin{array}{lll}
      (MM^\prime) M^{\prime\prime} \\
      (MM^\prime) & \square M^{\prime\prime} \\
      M & \square M^\prime & \square M^{\prime\prime} \\
    \end{array}
    \]
  \caption{Decomposing a term into multiple contexts}
  \end{figure}

  Here, when a dominant term has been reduced,
  the reduct is returned to the continuation at the top of the stack.
  This newly joined term then becomes the dominant term.
  After this new dominant term has been reduced,
  it will be returned to the next waiting continuation, and so on.
  Throughout this process, we maintain each continuation separately.

  If the control operators of a language allow manipulation of portions of the continuation stack,
  the continuations are \textbf{delimited}.
  To manipulate portions of the stack, these operators need a \emph{control delimiter}.
  A control delimiter marks a point on the stack that delimits how far the operators can control.
  These delimiters commonly called \emph{prompts}, after Felleisen first introduced them as such \cite{Felleisen88}.
  By allowing prompts to be pushed onto the stack, we can recall portions of the stack \emph{up until} a prompt.
  This gives the operators a finer grain of control over continuations.

  \subsection{Continuation-Passing Style}

  By rewriting \lam-terms, a term's continuation can be made explicit.
  All terms must be turned into \lam-abstractions of some variable $k$ where $k$ is the continuation of a term.
  $k$ is then called on the result of the term, triggering the continuation to take control.
  This style of writing \lam-terms is called continuation-passing style or CPS \cite{Sussman98}.

  \definition{
    \textsc{(Translation of standard \lam-terms into CPS)}
    \cite{Groote94}
    \[
    \begin{array}{lcl}
      \tr{x}     & = & `lk.kx      \\
      \tr{`lx.M} & = & `lk.k(`lx.\tr{M}) \\
      \tr{M N} & = & `lk.M(`lm.m\tr{N}(`ln.mnk)
    \end{array}
    \]
  }

  The term that a CPS program terminates on will be of the form
  $`lk.kM$. In order to extract the value, a \emph{final continuation}
  must be provided. Depending on the context, this could be an identity
  function $`lx.x$ or a display operation $`lx.\textsc{display }x$ to
  display the results of the program.

  \begin{example}{Extracting the final value from a terminated CPS program}
  \[
  \begin{array}{ll}
                & (`lk.kM)(`lx.x) \\
    \rightarrow & (`lx.x)M \\
    \rightarrow & M
  \end{array}
  \]
  \end{example}

  The translation of standard \lam-terms into CPS similarly transforms the
  \emph{types} of \lam-terms. For example, a term $x : A$ becomes
  $`lk.kx : (A \to B) \to B$. This type represents a delayed computation:
  a computation that is waiting for a function to continue execution with.
  In order to resume the computation, the term must be applied to a
  continuation.

  As an example, take the term $M$ where

  \[
    M = `lk.kx
  \]

  To access the value $x$ contained in $M$, we have to apply $M$ to a
  continuation function $`lm.N$:

  \[
  \begin{array}{rl}
      & (`lk.kx)(`lm.N) \\
      \to & (`lm.N)x \\
      \to & N[x/m]
  \end{array}
  \]

  Within the body of $N$, $m$ is bound to the value contained by $M$.
  So we can think about $M$ as a suspended computation that, when applied
  to a continuation, applies the continuation to $x$. Looking at
  the type $(A \to B) \to B$ again, it is clear that $A$ is the type
  of the term passed to the continuation of the CPS-term:

  \[
  \begin{array}{l}
    `lk.kx : (A \to B) \to B \\
    k : (A \to B) \\
    kx : B \\
    x : A \\
  \end{array}
  \]

  % TODO: explain how order-of-evaluation can be enforced using
  %       cps.

  \subsection{Monads}

  If we have two suspended computations $M$ and $M^\prime$ and we want to
  run $M$ and then $M^\prime$, we have to apply $M$ to a continuation
  to access its value and then do the same to $M^\prime$:

  \[
    M(`lm.M^\prime(`lm^\prime.N))
  \]

  This is a common operation so we define a utility operator \mono{>>=}
    (read: 'bind')
  that binds the first suspended computation to a continuation which returns another suspended computation:
  \[
    \mono{>>=} : ((A \to B) \to B) \to (A \to ((B \to C) \to C)) \to ((B \to C) \to C)
  \]
  The type $(A \to B) \to B$ that represents a suspended computation returning
  a value of type $A$ to its continuation we will call an $A$-computation or
  $Comp\ A$. We can rewrite the type signature of \mono{>>=}:

  \[
    \mono{>>=} : Comp\ A \to (A \to Comp\ B) \to Comp\ B
  \]

  We define another operator, $return$, that takes a value and returns a
  suspended computation that returns that value:

  \[
    return : A \to Comp\ A
  \]

  The type constructor $Comp\ A$, together with the two utility functions
  \mono{>>=} and $return$, make up Haskell's Monad type class:

  \Verbatimcode
    class Monad M where
      (>>=) :: M a -> (a -> M b) -> M b
      return :: a -> M a
  \end{Verbatim}

  The Monad type class generalizes CPS terms: they represent suspended
  computations that can be composed using \mono{>>=}. Just like CPS terms,
  a Monad type \mono{M a} tells us that we have a term that will pass
  values of type \mono{a} to the continuation it is bound to using
  \mono{>>=}.

  % TODO: finish explanation of linking suspended-computations and CPS
  %       to monads

\section{\lmu-Calculus}

  Michel Parigot defined the \lmu-calculus as a system with an isomorphism to classical natural deduction.
  It is an extension of the \lam-calculus.
  This means that the grammar and reduction rules of the \lmu-calculus are a superset of those of the \lam-calculus.

  % TODO: intuitively, that mu-terms are expressed easily by continuation
  %       passing style gives us some idea that they are about control flow
  %       and that they will be easily expressed by monads
  % TODO: add rules for consumption of multiple terms
  % TODO: add typing rules
  % TODO: add reduction strategy and information about normal form
  \subsection{Syntax}

  Just as \lam\ introduces \lam-abstractions,
  $`m$ introduces $`m$-abstractions.
  The body of a $`m$-abstraction must be a named term.
  A named term consists of a name of the form $[`a]$ followed by an unnamed
  term.

  \begin{figure}[H]
  \definition{
    \textsc{(Grammar for \lmu-calculus)}
    \item $`l$-variables are denoted by $x, y,\dots$ and $`m$-variables are denoted by $`a, `b,\dots$ \\
    \[
    \begin{array}{lrcl}

    \text{(Unnamed term)} & M,N & ::= & x\ |\ `lx.M\ |\ M\ N\ |\ `m`a.C \\
    \text{(Named term)} & C & ::= & [`a]M
    \end{array}
    \]
  }
  \end{figure}

  \subsection{Reduction Rules}
  \begin{figure}[H]
  \definition{
    \textsc{(Reduction rules for \lmu-calculus)}\\
    $i)$ $\to_{`b`m}$ is defined as the compatible closure of rules:
    \\
    \[
    \begin{array}{rrcll}
    \text{(logical)} & (`lx.M) N & \to_{`b} & M[N/x] \\
    \text{(structural)} & (`m`a.[`b]M) N & \to{`m} & `m`a.[`b](M\{[`g]M^\prime N/[`a]M^\prime\}) & (`b \not= `a)\\
        & (`m`a.[`b]M) N & \to{`m} & `m`a.[`g](M\{[`g]M^\prime N/[`a]M^\prime\})N & (`b = `a) \\
    \text{($`m`h$)} & `m`a.[`a]M & \to{`m} & M & (`a \notin fn(M)) \\
    \text{(renaming)} & `m`d.[`b](`m`g.[`a]M) & \to{`m} & `m`d.([`a]M)[`b/`g] \\
    \end{array}
    \]
    $ii)$ $\to_{`b`m}^{*}$ is defined as the transitive closure of $\to_{`b`m}$.
  }
  \end{figure}

  The structural reduction rule simply states that the application of a $`m$-abstraction $`m`a.M$ to a term $N$ applies all the sub-terms of $M$ labelled $[`a]$ to $N$ and relabels them with a fresh $`m$ variable.
  It can be thought of as a \lam-abstraction that can be applied to any number of variables \cite{Parigot92}.
  If we knew how many variables the term is applied to, we could replace
  \[ `m`a\dots[`a]M \]
  with
  \[ `lx_1\dots`lx_n\dots Mx_1\dots x_n \]
  where $n$ is the number of variables the term is applied to.

  \subsection{Computational Significance}

  A \lmu-abstraction applied to some term $N$ points the $`m$-variable to the context $\square N$.
  In this sense, the \lmu-abstraction captures contexts where the contexts are supplied by application.
  For this reason, \lmu-calculus is described as using \emph{applicative} contexts.
  When an unnamed term is labelled with a \lmu-variable, it is evaluated in that context.
  For instance the named term $[`a]M$ has the effect of evaluating $M$ in the context pointed to by $`a$.

  By translating $`m$-terms into CPS, we can see the mapping between $`m$-variables and contexts more clearly:
  \definition{
  \textsc{CPS translation of $`m$-terms} \cite{Groote94}
  \[
  \begin{array}{rcl}
    \tr{`m`a.M} & \triangleq & `l`a.\tr{M} \\
    \tr{[`a]M} & \triangleq & `lk.\tr{M}\ `a\ k
  \end{array}
  \]
  }
  A $`m$-abstraction binds a continuation to a variable for use throughout the abstraction's body.
  A named term applies the term to the name, effectively running the term in the continuation.

  To make this more concrete, consider the compound term $(`m`a.[`b]M)\ N$.
  First we decompose the term into a dominant term $`m`a.[`b]M$ and a context $\square N$.
  The dominant term is now a $`m$-abstraction so we can think about the variable at the head of the $`m$-abstraction now mapping to the context $\{`a \Rightarrow \square N\}$:

  % TODO: reform to get rid of context mapping:
  %   keep mu abstraction in dominant and consult context for
  %   what to apply named-terms to
  \begin{example}[]
  \[
  \begin{array}{lc}
    \textbf{Dominant} & \textbf{Context} \\
    (`m`a.[`b]M) N \\
    `m`a.[`b]M & \square N \\
  \end{array}
  \]
  \end{example}

  All subterms of $M$ labelled $`a$ will now be evaluated in the context $\square N$ after which the context will be destroyed.
  For example, let us replace $M$ with \mbox{$`m\nonocc.[`a](`ls.fs)$}:
  \footnote{Following van Bakel, we use $\nonocc$ to denote a $`m$-variable that does not occur in the body of the \lmu-abstraction.}

  \begin{example}
    \[
    \begin{array}{lcr}
    \textbf{Dominant} & \textbf{Context} \\
    `m`a.[`b]`m\circ.[`a](`ls.fs)    & \square N \\
    `m`a.[`a](`ls.fs)    & \square N \\
    `m`g.[`g](`ls.fs)N   & & (`g\ \text{fresh})  \\
    \end{array}
    \]
  \end{example}

  % TODO: explain applicative contexts
  After applying the term $[`a](`ls.fs)$ to $N$,
  the context $\square N$ is consumed and every occurrence of $`a$ is replaced with a fresh variable
  -- in this case a $`g$ --
  to clarify that the $`m$-abstraction now points to a new context.
  This means that $`m$-abstractions will pass all of the applicative contexts to the named subterms:

  \begin{example}
    \[
    \begin{array}{lcr}
    \textbf{Dominant} & \textbf{Context} \\
    (`m`a.[`a](`ls.`lt.st)) M N \\
    (`m`a.[`a](`ls.`lt.st))M & \square N \\
    `m`a.[`a](`ls.`lt.st) & \square M:\square N \\
    `m`g.[`g](`ls.`lt.st)M & \square N & (`g\ \text{fresh}) \\
    `m`d.[`d](`ls.`lt.st)MN & \square N & (`d\ \text{fresh}) \\
    \end{array}
    \]
  \end{example}

  \subsection{Curry-Howard Isomorphism}
  The typed variant of the \lmu-calculus is isomorphic to classical natural deduction.
  The type assignment rules for the typed \lmu-calculus are as follows:

  \definition{
  \textsc{Typing rules for the typed \lmu-calculus} \\
  The three type-assignment rules from the \lam-calculus are still valid. In addition:
  \[
  \begin{array}{c@{\quad\quad}c}
    \Inf{`G \vdash M : B\ |\  `a : A, `b : B, `D}
        {`G \vdash `m`a.[`b]M : A\ |\ `b : B, `D}
    &
    \Inf{`G \vdash M : A\ |\ `a : A, `D}
        {`G \vdash `m`a.[`a]M : A\ |\ `D}
  \end{array}
  \]
  }

  These rules correspond to the structural rules of classical natural deduction.
  The original presentation of Parigot's type assignment makes the isomorphism clearer.
  Our presentation here makes it easier to understand the role of the operators.

  \subsection{Lazy Reduction}
  We define lazy-reduction for the \lmu-calculus similarly to \lam-calculus.
  The outermost, leftmost reduction takes place first.
  If no reductions can take place on the outermost level, then the term is in lazy normal form.
  For instance, consider the $`m$-term:
  \[
    `m`a.[`b]((`lx.x)z)
  \]
  This term is in lazy normal form: the only redex, $(`lx.x)z$, is beneath a $`m$-abstraction.
  The term cannot be reduced any further.

\section{Calculus of Delimited Continuations}

  Simon Peyton-Jones \textit{et al.}\ extended the \lam-calculus with additional operators in order create a framework for implementing delimited continuations \cite{JonesDS07}.
  This calculus will be referred to as the calculus of delimited-continuations or \emph{CDC}.
  Many calculi have been devised with control mechanisms for manipulating continuations, like the \lmu-calculus.
  These control mechanisms manipulate either delimited or undelimited continuations.
  CDC provides a set of operators that are capable of expressing many of the common control mechanisms found in the literature.

  \subsection{Syntax}
  The grammar of CDC is an extension of the standard \lam-calculus:

  \begin{figure}[!h]
  \definition{
    \textsc{(Grammar for CDC)}
    \[
    \begin{array}{lrcl}
    \textrm{(Variables)} & x, y, \dots \\
    \textrm{(Expressions)} & e & ::= & x\ |\ `lx.e\ |\ e\ e^\prime \\
                           &   &  |  &  newPrompt\ |\ pushPrompt\ e\ e \\
                           &   &  |  &  withSubCont\ e\ e\ |\ pushSubCont\ e\ e
    \end{array}
    \]
  }
  \end{figure}

  \subsection{Reduction Rules}

  The operational semantics can be understood through an abstract machine that transforms tuples of the form $\langle e,\ D,\ E,\ q \rangle$.
  The tuple consists of:
    \begin{itemize}
      \item $e$ - the current dominant term
      \item $D$ - the current context/continuation
      \item $E$ - the stack of remaining continuations
      \item $q$ - a global counter for producing fresh prompt values
    \end{itemize}
  By representing terms in this way,
  the reduction rules are able to make the control of terms and their continuations more explicit.

  The abstract machine also introduces some Haskell-style notation for dealing with sequences. An empty sequence is represented by $[]$.
  A value added to the head of a list is represented by the symbol $:$, for instance $D:[]$. $\app$ represents two lists appended together.
  $E\until{p}$ and $E\from{p}$ denote the subsequence of $E$ \emph{until} prompt $p$ and \emph{from} prompt $p$, respectively;
  neither of these subsequences contain $p$.

  \begin{figure}[!h]\label{fig:cdc-abstract-machine}
  \relscale{0.9}
  \definition{
    \textsc{(Operational semantics for CDC)} \cite{JonesDS07}
    \[
    \begin{array}{lcll}
      \langle e\ e^\prime, D, E, q \rangle &\to &\langle e, D[\square\ e^\prime], E, q \rangle &\text{e non-value} \\
      \langle v\ e, D, E, q \rangle &\to &\langle e, D[v\ \square], E, q \rangle &\text{e non-value} \\
      \langle pushPrompt\ e\ e^\prime, D, E, q \rangle &\to &\langle e, D[pushPrompt\ \square\ e^\prime], E, q \rangle &\text{e non-value} \\
      \langle withSubCont\ e\ e^\prime, D, E, q \rangle &\to &\langle e, D[withSubCont\ \square\ e^\prime], E, q \rangle &\text{e non-value} \\
      \langle withSubCont\ p\ e, D, E, q \rangle &\to &\langle e, D[withSubCont\ p\ \square], E, q \rangle &\text{e non-value} \\
      \langle pushSubCont\ e\ e^\prime, D, E, q \rangle &\to &\langle e, D[pushSubCont\ \square\ e^\prime], E, q \rangle &\text{e non-value} \\
    \\
      \langle (`lx.e)\ v, D, E, q \rangle &\to &\langle e[v/x], D, E, q \rangle \\
      \langle newPrompt, D, E, q \rangle &\to &\langle q, D, E, q+1 \rangle \\
      \langle pushPrompt\ p\ e, D, E, q \rangle &\to &\langle e, \square, p : D : E, q \rangle \\
      \langle withSubCont \ p\ v, D, E, q \rangle &\to &\langle v (D : E\until{p}, \square, E\from{p}, q \rangle \\
      \langle pushSubCont E^\prime\ e, D, E, q \rangle &\to &\langle e, \square, E^\prime \app (D : E), q \rangle \\
    \\
      \langle v, D, E, q \rangle &\to &\langle D[v], \square, E, q \rangle \\
      \langle v, \square, p : E, q \rangle &\to &\langle v, \square, E, q \rangle \\
      \langle v, \square, D : E, q \rangle &\to &\langle v, D, E, q \rangle
    \end{array}
    \]
  }
  \end{figure}

  \subsection{Significance}\label{cdc-explanation}

  % TODO: ensure prompts and continuation stack has been explained before reaching this point
  The additional terms behave as follows:
  \begin{itemize}
  \item \op{newPrompt} returns a new and distinct prompt.
  \item \op{pushPrompt}'s first argument is a prompt which is pushed onto the continuation stack before evaluating its second argument.
  \item \op{withSubCont} captures the subcontinuation from the most recent occurrence of the first argument (a prompt) on the excution stack to the current point of execution. Aborts this continuation and applies the second argument (a \lam-abstraction) to the captured continuation.
  \item \op{pushSubCont} pushes the current continuation and then its first argument (a subcontinuation) onto the continuation stack before evaluating its second argument.
  \end{itemize}

  The abstract machine defined in Figure \ref{fig:cdc-abstract-machine} also encodes the reduction strategy:
  the first block of rules define in what order redexes are reducted.

\section{\ltry-Calculus}

\ltry\ is a system defined by van Bakel in \cite{Bakel15} for modelling exceptions.
It is an syntactic and semantic extension of the \lam-calculus.
Unlike previous systems, the \ltry-calculus introduces the idea of named exceptions.

\subsection{Exceptions}

\emph{Exceptions} in programming languages are indications that control flow cannot continue.
For example, if you attempt to open a non-existent file,
the operation might throw an exception.
If an exception occurs without being caught, a program will exit with an error.
Exceptions can be caught and attempts at recovery can be made by \emph{exception handlers}.

The common syntax for introducing exception handlers is in try-catch blocks.
An exception that occurs in a try-catch block will be handled by a corresponding handler.
For example, see the Javascript syntax for this:

\begin{Verbatim}
  try {
    /* possibly throw exception */
  } catch (e) {
    /* recover thrown exception */
  }
\end{Verbatim}

The \mono{catch (e) \{ ... \}} introduces a single exception handler.
This exception handler will be called if an exception is thrown inside the try block.
If we want to introduce multiple exception handlers,
we need a mechanism for deciding which handler will be called for which exception.
Java solves this by registering different exception handlers based on their type:

\begin{Verbatim}
  try {
    /* possibly throw exception */
  } catch (IOException e) {
    /* recover from IOException */
  } catch (FileNotFoundException e) {
    /* recover from FileNotFoundException */
  }
\end{Verbatim}

Here, which handler is called depends on the type of the exception thrown.

\subsection{Syntax}

The grammar of the \ltry-calculus is as follows:
\definition{
\textsc{(Grammar of the \ltry-calculus)}
\[
  \begin{array}{rclr}
    C &::=& \catch{ n$_1$($x$) = $M_1$ }; \dots; \catch{n$_i$($x$) = $M_i$} & (i \geq 1) \\
    M,N &::=& x\ |\ `lx.M\ |\ MN\ |\ \try M;\ C\ |\ \throw{n($M$)}
  \end{array}
\]
}

The grammar for $C$ describes a catch block as a series of one or more catch statements.
For convenience, we will use the notation
\[
  \mult{\catch{n$_i$($x$) = $M_i$}}
\]
to describe a catch block with one or more catch statements.

The \ltry-calculus adds three new syntactic constructs:
\begin{itemize}
\item \textbf{throw n($M$)} denotes the throwing of an exception with name $n$ passing it the value $M$.
\item \textbf{catch n($x$) = $M$} registers the exception handler $M$ to the name $n$ with parameter $x$.
\item \textbf{try $M$; $C$} attempts to run term $M$ in an environment with the exception handlers in catch block $C$ registered.
\end{itemize}

\subsection{Reduction Rules}
In conjunction with the additional syntactic constructions,
the \ltry-calculus introduces some reduction rules:

\definition{
\textsc{(\ltry\ reduction rules)}\cite{Bakel15}
\[
\begin{array}{rlcl}
  \text{($`b$):}      & (`lx.M)N &\to& M[N/x] \\
  \text{(throw):}     & (\throw{n($M$)})N &\to& \throw{n($M$)} \\
  \text{(try-throw):} & \try \throw{n$_l$($N$)};\ \mcatch &\to& M_l[N/x] \\
  \text{(try-value):} & \try V;\ \mcatch &\to& V \\
\end{array}
\]
}

The $`b$ reduction rule is familiar from the \lam-calculus.
A \textbf{throw} term applied to any term discards the second term.
A \textbf{try} term that contains a throw reduces to the handler that corresponds to the name of the exception thrown with all occurrences of the parameter replaced by the value thrown.
For instance a $\throw{n(N)}$ inside a \textbf{try} will reduce to $M[N/x]$ if there is a $\catch{n($x$) = $M$}$ in the catch block.
A \textbf{try} term that contains a value reduces to just that value.

\subsection{Significance}
The occurrence of an exception aborts the current computation.
\ltry\ models this by discarding terms that a \textbf{throw} is applied to.
The \textbf{try-catch} statements mirror the syntax of try-catch statements in programming languages in the C-syntax family.


\documentclass[letterpaper, 11 pt, conference]{ieeeconf}
\IEEEoverridecommandlockouts
\overrideIEEEmargins

\usepackage{graphics} % for pdf, bitmapped graphics files
\usepackage{epsfig} % for postscript graphics files
\usepackage{mathptmx} % assumes new font selection scheme installed
%\usepackage{times} % assumes new font selection scheme installed
\usepackage{amsmath} % assumes amsmath package installed
\usepackage{url}
\usepackage{amssymb}  % assumes amsmath package installed

\title{\LARGE \bf
Exploring the Space of Machine Learning Algorithms on Learning Stencil Based Kernels
}

\author{Sandesh Borgaonkar \& Vinu Joseph}%
\begin{document}

\maketitle
\thispagestyle{empty}
\pagestyle{empty}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{abstract}
Abstract— Stencil based computations are used extensively in High Performance Computing (HPC) domain for finding
solutions of Partial Differential Equations (PDEs). These computations are represented in a polynomial form where the coefficients of the polynomial are influenced by the parameters such as boundary values, initial values, and the degree of accuracy. In this work, we present an empirical study to explore the solution space for following questions: i) is it possible to learn the coefficients of a given polynomial using regression analysis, and ii) how accurate the learning could be as compared to the original solution? And iii) what is the competitiveness of the various machine learning algorithms deployed for this purpose. Specifically, we use two state-of-the-art
machine learning packages, Libsvm and Liblinear, for learning the polynomial coefficients. In addition, we also implement our own version of learning model using stochastic gradient descent algorithm. Given the intractable size of the training data produced by the stencil kernels, we employ sampling strategy used in the prior work to reduce the sample size. We measure the effect of sample size on the quality of learning with respect to the three  different learning models described earlier.
\end{abstract}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{INTRODUCTION}
Stencil based computations are an important area of study in the scientific domain. They find a variety of applications in areas such as solutions to Partial Differential Equations (PDEs) and related areas of scientific computation research. Stencil computations essentially are repeated update of neighboring points on a multidimensional grid, based on a given intial condition (initial value problem) or intial and boundary conditions (boundary value problem), and can be used to represent a wide variety of real world differential equations such as heat and wave propagations. The effort in this project was to learn the stencil points using the constituent grid points as features, and compare the performance of various learning algorithms on such a learning. If the study is successful, the results could be potentially utilized in a variety of applications that mandate computational optimizations on stencils. Also, the comparitive analysis could help in making informed choices over picking a given learning algorithm over others. For this project, the PDE we used was heat1d equation.

\section{THEORETICAL BASIS}
\subsection{Equation}
As shown in Fig.1, the evaluation of any given grid point on the stencil of the heat1d PDE can be represented as :\\
\begin{align*}
h[i+1,j] &=\alpha*h[i][j-1] +(2-\alpha)*h[i][j] \\
&\qquad +h[i][j+1] + h[i][j+1] + f[i][j]*dt
\end{align*}
\\
Here, $i+1$ is indicative of the time step for which the computation is being done, and $j$ is the $j_{th}$ discrete point that is being evaluated on the basis of $(j-1)_{th}$, $j_{th}$ and $(j+1)_{th}$ positions of the current time step $i$. The parameter $\alpha$ (also known as $cfl$) is defined by \\\\
\centerline{$\alpha = \dfrac{dt}{(dx)^2}$}\\

where $dt$ and $dx$ are the sizes of discretized time and length steps. Further, the term $f[i][j]*dt$ is the value of actual heat1d equation at point $(i,j)$ multiplied by the discretized time step $dt$.

\subsection{Initial and Boundary Conditions }
\textit{Initial condition} refers to the value of initial temperature of the rod element to be considered for heating. Such a condition is spread across the length of the rod, except for the end points. The temperature of the end points constitute the \textit{Boundary Condition}, which is imposed at every progressive time step of computation. We consider the following three initial condition variants : \\
\indent1. Triangular \\
\indent2. Sinusoidal \\
\indent3. Piecewise Linear \\

\begin{figure}
\includegraphics[scale=0.35]{plot_test_original_uni_1.png}
\caption{Uniform IC:50 and BC:90,70}
\label{uni1}
\end{figure}

\begin{figure}
\includegraphics[scale=0.35]{plot_test_original_uni_2.png}
\caption{Uniform IC:25 and BC:50,50}
\label{uni2}
\end{figure}

\begin{figure}
\includegraphics[scale=0.35]{plot_test_original_uni_3.png}
\caption{Uniform IC:0 and BC:25,80}
\label{uni3}
\end{figure}

\begin{figure}
\includegraphics[scale=0.35]{plot_test_original_tri_1.png}
\caption{Triangular IC:70/(L/2)x and BC:0,0}
\label{tri1}
\end{figure}

\begin{figure}
\includegraphics[scale=0.35]{plot_test_original_tri_2.png}
\end{figure}

\begin{figure}
\includegraphics[scale=0.35]{plot_test_original_tri_3.png}
\end{figure}

\begin{figure}
\includegraphics[scale=0.35]{plot_test_original_tri_4.png}
\end{figure}

\begin{figure}
\includegraphics[scale=0.35]{plot_test_original_pwl_1.png}
\end{figure}
\begin{figure}
\includegraphics[scale=0.35]{plot_test_original_pwl_2.png}
\end{figure}
\begin{figure}
\includegraphics[scale=0.35]{plot_test_original_pwl_3.png}
\end{figure}

\begin{figure}
\includegraphics[scale=0.35]{plot_test_original_pwl_4.png}
\end{figure}

%TEST CASES
\begin{figure}
\includegraphics[scale=0.35]{plot_test_original_uni_4.png}
\end{figure}
while keeping the boundary conditions constant.
The goal of this project is to try to learn the computation of stencil points for heat1d stencil for a randomized set of initial condition, boundary condition, and discretized time steps $dt$ and $dx$, in order to learn weights that can be deployed towards computation of any heat1d stencil in general.  We deploy 3 learning algorithms - LibSVM, LibLinear and our own implementation of \textit{stochastic gradient descent} derived over an SVM objective, and also present a performance cross comparison of each of the algorithms used.

The following section decribes our experiments in detail. We try to outline the process of data collection, sampling, learning, cross validation and choice of hyper parameters, error collection finally the performance comparison of each of the algorithms used.

\section{EXPERIMENTS}
\subsection{Data collection}
We collected the data by running the standard stencil program for heat1d \cite{c7} and listing the results in the standard LibSVM format. The collection comprised of results from multiple runs of the program over randomized sets of initial conditions, boundary conditions and the discretized sets of time and length steps. The same randomized set of inputs was repeated for each variant of initial conditions (i.e. Triangular, Sinusoidal and Piecewise Linear) to give us corresponding 3 sets of raw data collections (one for each variant).
\subsection{Sampling}
In this work, we employed the sampling approach described in \cite{c5}, where the  number of samples are tabulated for various confidence levels expressed as $(1-\delta)*100$\% and error $\epsilon$ . The work described in \cite{c2} provides pointers on information theoretic (entropy) based sampling, where a new sample from the population is added into the sample pool, based on how much variation it brings in into the training set.
\subsection{Choice of Parameters}
Cross validation experiment on each of the variants per algorithm (9cases), and the parameters finally chosen.
\subsection{Learning}
Maybe show the weights reported in graphs?
\subsubsection{Using LibSVM}
3 variants
\subsubsection{Using LibLinear}
3 variants
\subsubsection{Using Our Implentation}
3 variants

\subsection{Test Error Analysis}
Deployment of the functions thus learned on the test set (9 cases again). Maybe show 3 plots, each for a variant with 3 lines for errors generated (bar graphs for training vs testing error)?
\subsubsection{Figures and Tables}

\section{Conclusion}

\addtolength{\textheight}{-12cm}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{ACKNOWLEDGMENT}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{thebibliography}{99}

\bibitem{c1} Vishal C. Sharma, Ganesh Gopalakrishnan, Greg Bronevetsky, Detecting Soft Errors in Stencil based Computations

\bibitem{c2}
Siamak Mehrkanoon, Johan A.K Suykens, Learning solutions to partial differcential equations using LS-SVM. 2015

\bibitem{c3}
Siamak Mehrkanoon, Johan A.K Suykens, LS-SVM approximate solutions to linear time varying descriptor systems, 2012

\bibitem{c4}
Eduardo Berrocal, Leonardo Bautista-Gomez, Sheng Di, Zhilling Lan and Frank Capello, Lightweight Silent Data Corruption Detection Based on Runtime Data Analysis for HPC Applications

\bibitem{c5}
Ken Kelly, Sample size planning for the coefficient of variation from the accuracy in parameter estimation

\bibitem{c6}
Richard H Byrd, Gillian M. Chin, Jorge Nocedal, Yuchen Wu, Sample Size Selection in Optimization Methods for Machine Learning

\bibitem{c7}
Burkardt, John, ”Scientific Computing Library” : \url{https://people.sc.fsu.edu/~jburkardt}

\bibitem{c8}
Kreyszig, Erwin.”Advanced engineering mathematics”. Wiley, 2011.

\bibitem{c9}
Vishal C Sharma, Approaches for Approximate Stencil based Computations

\bibitem{c10}
Rong-En Fan, Kai-Wei Chang, Cho-Jui Hsieh, Xiang-Rui Wang, Chih-Jen Lin, LIBLINEAR: A Library for Large Linear Classification , NTU

\bibitem{c11}
John burkardt, Florida State University, Codes and Data Sets \url{https://people.sc.fsu.edu/~jburkardt/py_src/fd1d_heat_explicit/}

\bibitem{c12}
Michael Bowles, Machine Learning in Python, Essential Techniques for Predictive Analysis

\bibitem{c13}
Bitbucket Repository \url{https://vinutah@bitbucket.org/ufmr/cs6350mlproj.git}

\bibitem{14}
Isaac Elias Lagaris, Aristidis Likas and Dimitirios I Fotiadis, Artificial Neural Networks for Solving ODE's and PDE's

\end{thebibliography}

\end{document}


\documentclass[../../main]{subfiles}
\pagestyle{fancy}

\begin{document}

\chapter{Internal core model induction}
\label{chapter.internal-core-model-induction}
\thispagestyle{fancy}

\section{Operators and hybrid mice}

We'll need a generalisation of the concept of mice as we move up to the higher reaches of the core model induction, a generalisation usually known as either \textit{hybrid mice} or \textit{operator mice}. The basic concept is simple. When we're constructing ``pure'' mice we're traversing the $\J$-hierarchy, applying the $\J(x):=\rud(x\cup\{x\})$ operator at every step, and taking unions at limit stages. In the hybrid case we're simply replacing $\J$ with another \textit{operator} $\F$, again applying it at every successor stage and taking unions at limits.

\qquad Figuring out what operators we're allowed to pick is the hard part, as we want to maintain all the fine structure that we get in the ``pure'' case. This has been done in great detail in \cite{SchlutzenbergTrang}, and we'll introduce a particularly simple case of their general definition here.

\defi[Schlutzenberg-Trang]{
  For a set $x$ write $\rho_x:x\to\rk x$ for the rank function of $x$ and define the \textbf{rank closure} $\hat x:=\trcl(\{x, \rho_x\})$ of $x$ and the \textbf{cone} $C_x := \{\hat y\in H_\kappa\mid x\in\J_1(\hat y)\}$ over $x$.
}

\defi[Schlutzenberg-Trang]{
  Let $\kappa$ be an infinite cardinal, $D$ a set of self-wellordered\footnote{A set $x$ is \textit{self-wellordered} if there's a wellorder of $x$ in $\J(x)$.} sets and fix $b\in H_\kappa$. An \textbf{operator on $H_\kappa$ over $b$ with support $D$} is a partial function
  \eq{
    \F\colon H_\kappa\dashrightarrow H_\kappa
  }

  such that $D\cap C_b\subset\dom\F$ and $\dom\F$ is closed under both unions and applications of $\F$. We also call $C_b$ the \textbf{cone over $b$} and $b$ the \textbf{base of $C_b$}.
}

Before we move on, let's take a step back and have a look at a few examples of operators. As this is supposed to be a generalisation of the $\J$-function we'd want that to also be an operator, of course. Indeed, if $V=L$ then note that $\hat x=x$ for all $x$, so if we take $\emptyset$ to be our base then $C_a=J_\kappa$, making $\dom\J=J_\kappa$ trivially closed under both unions and applications of $\J$.

\qquad We could also let $x$ be any set, assume that $V=L(\hat x)$ and consider the operator $\J_x$, which is simply applying $\J$ but with base $\hat x$ instead of $\emptyset$. A similar argument as above would show that this is an operator on $J_\kappa(\hat x)$ ($=H_\kappa^{L(\hat x)}$) as well.

\qquad A slightly more sophisticated example would be $\F := (-)^\sharp$, where $x^\sharp$ is the smallest initial segment of $\lp(x)$ with a measure\todo{Elaborate}. We can consider $\F$ as an operator on $\hc$ over $\emptyset$, where $\F(\emptyset) = 0^\sharp$ and $\F^\omega(\emptyset) = \bigcup_{n<\omega}\F^n(\emptyset)$ is the smallest $\sharp$-closed structure.

\todo[inline]{Check the last example}

We now, somewhat informally\footnote{For a comprehensive definition, see \cite[Section 2]{SchlutzenbergTrang}.}, define what hybrid mice are. Namely, an \textbf{$\F$-mouse on $\hat x$} is a structure $\M$ built by successively applying $\F$ to $\hat x$ and taking unions at limits. We can stop the construction at any point and denote the amount of $\F$-applications by $l(\M)$, the \textbf{length} of $\M$. The \textbf{initial segments} of $\M$ are simply the intermediate models up to $\M$, being of the form $\F^\alpha(\hat x)$ for some $\alpha<l(\M)$.

\qquad As with regular mice, we require that they are \textit{amenable}, \textit{acceptable} and that proper initial segments $\F$-mice are \textit{sound}. An extra property that we require in the hybrid context is that every proper initial segment $\P\pinit\M$ is \textit{${<}\omega$-condensing}, roughly stating that if $\pi\colon\N\to\P$ is a sufficiently elementary embedding from a ``nice'' $\F$-structure then either $\N$ is an initial segment of $\P$ or an element of an ultrapower of $\P$\footnote{For a proper definition of these concepts in the hybrid context, see \cite[Section 2]{SchlutzenbergTrang}.}.

\qdefi{
  Let $\kappa$ be an infinite cardinal and $\F$ an operator on $H_\kappa$ over $b\in H_\kappa$. We then define the \textbf{$\F$-lower part model on $b$} as
  \eq{
    \lp^{\F}(b) := \{\M\mid\text{$\M$ is a sound $\F$-mouse projecting to $b$}\}\tag*{$\circ$}
  }
}

\prop{
  Let $\kappa$ be an infinite cardinal and $\F$ an operator on $H_\kappa$ over $b\in H_\kappa$. Assuming $\dc_{\hat b}$ holds, $\lp^{\F}(b)$ is itself an $\F$-premouse.
}
\proof{
  If $\M\pinit\lp^{\F}(b)$ then $\M = \chull^{H_\nu}(\hat b\cup o(\M))$, so that there's an isomorphism from $\hat b^{<\omega}$ onto $\M$. Now, using $\dc_{\hat b}$, we can take a countable hull containing $\hat b$, meaning that without loss of generality we may assume that $\hat b$, and hence also $\M$, is countable.

  \qquad This means that whenever we have two $\M,\N\pinit\lp^{\F}(b)$ we may assume that they're both countable, which means that a comparison argument shows that one of them is an initial segment of the other. This means that all the mice in $\lp^{\F}(b)$ line up, which implies that all the axioms for being an $\F$-premouse trivially hold for $\lp^{\F}(b)$.
}

Further, $\lp^{\F}$ is itself an operator on $H_\kappa$ over $b$ and we write $\lp^{\F}_\alpha(b) := (\lp^{\F})^\alpha(b)$.

\todo[inline]{Check that the definition of $\lp^{\F}$ is correct and that it \textit{does} in fact have the two properties above.}

\defi[Schindler-Steel]{
  Let $\kappa$ be an infinite cardinal and $\F$ an operator on $H_\kappa$ over $b\in H_\kappa$. Then $\F$ \textbf{condenses well} if whenever $g\subset\col(\omega,\kappa)$ is $V$-generic and that there are models $\overline\M, \M\in H_\kappa$ and $\overline\M^+\in V[g]$, all on $b$, with
  \begin{enumerate}
    \item $\abs{\overline\M} = \abs{b}\cdot\aleph_0$;
    \item $\overline\M\in\overline\M^+$;
    \item $\overline\M^+=\hull_1^{\overline\M^+}(\overline\M)$;
    \item Either
    \begin{enumerate}
      \item There's a map $\pi\colon\overline\M^+\to\F(\M)$ in $V[g]$ with $\pi(\overline\M)=\M$ and $\pi\restr(b\cup\{b\})=\id$ which is $\Sigma_0$-cofinal or $\Sigma_2$-elementary; or
      \item There's a model $\P\in\dom\F$ on $b$ with $\F(\P)\in H_\kappa$ and maps $i\colon\F(\P)\to\overline\M^+$ and $\pi\colon\overline\M^+\to\F(\M)$, both in $V[g]$ but with their composition in $V$, with $i(\P)=\overline\M$, $\pi(\overline\M)=\M$,
      \eq{
        i\restr(b\cup\{b\})=\pi\restr(b\cup\{b\})=\id,
      }

      $i$ is $\Sigma_0$-cofinal or $\Sigma_2$ elementary, and $\pi$ is a weak $\Sigma_1$-embedding.
    \end{enumerate}
  \end{enumerate}

  Then $\overline\M^+ = \F(\overline\M)\in V$.
}

It turns out that \textit{condenses well} is a bit too strong to do proper core model theory, as shown in \cite{SchlutzenbergTrang}, and in that paper they propose a technical weakening of this concept which they call \textit{condenses finely}, whose definition is of a similar spirit as the above. We therefore formally require that our desired operators only condense finely, but as all the operators that we will encounter ``in the wild'' in this thesis condense well, we will omit the definition of fine condensation here.

\defi{
  An operator $\F$ \textbf{determines itself on generic extensions} if there exists a formula $\varphi(v_0, v_1)$ such that whenever $\M$ is an $\F$-premouse with
  \eq{
    \M\models\kp + \godel{\text{there are arbitrarily large cardinals}},
  }

  $\kappa$ is an $\M$-cardinal and $g\subset\col(\omega, \kappa)$ is $\M$-generic, then $\hc^{\M[g]}$ is closed under $\F$ and $\F\restr\hc^{\M[g]} = (\tau_\kappa^{\M})^g$, with $\tau_\kappa^{\M}$ being the unique $\tau$ such that $\M\models\varphi[\kappa, \tau]$.
}

\defi{
  Let $\kappa$ be an infinite cardinal and $\F$ an operator on $H_\kappa$ over $b\in H_\kappa$. We then say that $\F$ is \textbf{radiant}\footnote{The terminology is meant to suggest that the operator is preserved when moving in ``any direction'': down to smaller models or up to larger forcing extensions.} if $\F$ condenses finely and determines itself on generic extensions.
}

\section{Core model dichotomy}

\lemm[Mesken-N.][lemm.opit]{
  Let $\theta$ be a regular uncountable cardinal or $\theta=\infty$ and let $\N$ be a tame hybrid mouse operator on $H_\theta$ which relativises well. Then $\N$ is countably iterable iff it's $(\theta,\theta)$-iterable, guided by $\N$. Furthermore, for every $x\in H_\theta$, if $M_1^{\N}(x)$ exists and is countably iterable, then it's also $(\theta,\theta)$-iterable, guided by $\N$.
  \todo[inline]{Change this to model operators; perhaps change parts of the proof and/or assumptions needed.}
  }
\proof{
  Fix $x\in H_\theta$ and assume that $\N(x)$ is countable iterable. We first show that $\N(x)$ is $(\theta,\theta)$-iterable. Let $\T\in H_\theta$ be a normal tree of limit length on $\N(x)$. Let $\eta\gg\rk(\T)$ and let
  \eq{
    \h:= \chull^{H_\eta}(\{x,\N(x),\T\})
  }

  with uncollapse $\pi\colon\h\to H_\eta$. Set $\overline a:=\pi^{-1}(a)$ for every $a\in\ran\pi$. Note that $\overline{\N(x)}=\N(\overline x)$ since $\N$ relativises well. Now $\overline\T$ is a normal, countable iteration tree on $\N(\overline x)$ and hence our iteration strategy yields a wellfounded cofinal branch $\overline b\in V$ for $\overline\T$. Note that $\overline\Q:=\Q(\overline b,\overline\T)$ exists, since if $\overline b$ drops then there's nothing to do, and otherwise we have that
  \eq{
    \rho_{1}(\M^{\overline\T}_{\overline b})=\rho_{1}(\N(\overline x))=\rk\overline x<\delta(\overline\T),
  }

  so $\delta(\overline\T)$ is not definably Woodin over $\M^{\overline\T}_{\overline b}$, as there is a definable surjection from $\rho_1(\M^{\overline\T}_{\overline b})$ onto $\delta(\overline\T)$.
  \clai{
    $\overline\Q\init\N(\M(\overline\T))$
  }

  \cproof{
    If $\overline\Q=\M(\overline\T)$ then the claim is trivial, so assume that $\M(\overline\T)\pinit\overline\Q$. Note that $\overline\Q\init M_{\overline b}^{\overline\T}$ by definition of $\Q$-structures, and that $M_{\overline b}^{\overline\T}$ satisfies $(2)$ of the definition of relativises well, meaning that
    \eq{
      M_{\overline b}^{\overline\T}\models\godel{\text{$\forall\eta\forall\zeta>\eta:$ if $\eta$ is a cutpoint then $M_{\overline b}^{\overline\T}|\zeta\not\models\varphi_{\N}[\bar x,p_{\N}]$}}.\tag*{(1)}
    }

    This statement is $\Pi^1_2$ and $\overline\Q$ is $\Pi^1_2$-correct since it contains a Woodin cardinal, so that $\Q$ satisfies the statement as well. Since $\N$ is tame we get that $\delta(\overline\T)$ is a cutpoint of $\overline\Q$, so that $\N(\M(\overline\T))=\N(\overline\Q|\delta(\overline\T))$ is \textit{not} a proper initial segment of $\overline\Q$. Further, as we're assuming that both $\N(\M(\overline\T))$ and $\M^{\overline\T}_{\overline b}$ are $(\omega_{1}{+}1)$-iterable above $\delta(\overline\T)$ the same thing holds for $\overline\Q\init\M_{\overline b}^{\overline\T}$, so that we can compare $\N(\M(\overline\T))$ with $\overline\Q$ (in $V$). Let
    \eq{
      (\N(\M(\overline\T)),\overline\Q) \leadsto (\P,\R)
    }

    be the result of the coiteration. We claim that $\R\init\P$. Suppose $\P\pinit\R$. Then there is no drop in $\N(\M(\overline\T))\leadsto\P$ and in fact $\N(\M(\overline\T))=\P$ since $\N(\M(\overline\T))$ projects to $\delta(\overline\T)$. Furthermore, as we established that $\N(\M(\overline\T))=\N(\overline\Q|\delta(\overline\T))$ isn't a proper initial segment of $\overline\Q$ it can't be a proper initial segment of $\R$ either, as the coiteration is above $\delta(\overline\T)$. But we're assuming that $\N(\M(\overline\T))=\P\pinit\R$, a contradiction. So $\R\init\P$.

    \qquad Since $\N(\M(\overline\T))$ and $\overline\Q$ agree up to $\delta(\overline\T)$ and there is no drop $\overline\Q\leadsto\R$ we have that $\overline\Q=\R$. If $\N(\M(\overline\T))\leadsto\P$ doesn't move either we're done, so assume not. Let $F$ be the first exit extender of $\N(\M(\overline\T))$ in the coiteration. We have $\lh(F) \le o(\overline\Q)$, $\overline\Q\init\P$ and $\lh(F)$ is a cardinal in $\P$.

    \qquad As $\overline\Q$ is $\delta(\overline\T)$-sound and projects to $\delta(\overline\T)$ it follows that $J(\overline\Q|\lh(F))$ collapses $\lh(F)$, so it has to be the case that $\overline\Q|\lh(F)=\P$ and thus $o(\P)=\lh(F)$. But this means that $\P=\N(\M(\overline\T))$ even though we assumed that $\N(\M(\T))\leadsto\P$ moved, a contradiction.
  }

  Now, in a sufficiently large collapsing extension extension of $\h$, $\overline b$ is the unique cofinal, wellfounded branch of $\overline\T$ such that $\Q(\overline b,\overline\T) \init\N(\M(\overline\T))$ exists. Hence, by the homogeneity of $\col(\omega,\theta)$, $\overline b \in H$. By elementarity there is a unique cofinal, wellfounded branch $b$ of $\T$ such that $\Q(b,\T)\init\N(\M(\T))$. This proves that $M$ is (uniquely) $\on$-iterable and virtually the same argument yields the iterability of $M$ via successor-many stacks of normal trees.

  \qquad To show that $M$ is fully iterable, it remains to be seen that the unique iteration strategy (guided by $\N$) of $M$ outlined above leads to wellfounded direct limits for stacks of normal trees on $M$ of limit length. Let $\lambda$ be a limit ordinal and $\vec\T = (\T_i \mid i<\lambda)$ a stack according to our iteration strategy. Suppose $\lim_{i<\lambda}\M^{\T_i}_\infty$ is illfounded.

  \qquad Redefine $\eta\gg\rk(\vec\T)$, $\h:=\chull^{H_\eta}(\{x,M,\vec\T\})$ and $\pi:\h\to H_\eta$ the uncollapse, again with $\overline a:=\pi^{-1}(a)$ for every $a\in\ran\pi$. By elementarity we get that $\h\models\godel{\lim_{i<\overline\lambda}\M^{\overline\T_i}_\infty\text{ is illfounded}}$. But $\overline{\vec\T}$ is countable and according to the iteration strategy guided by $\N$, so that
  \eq{
  V\models\godel{\lim_{i<\overline\lambda}\M^{\overline\T_i}_\infty\text{ is wellfounded.}}
  }

  Now note that $(\lim_{i<\overline\lambda}\M^{\overline\T_i}_\infty)^{\h}=(\lim_{i<\overline\lambda}\M^{\overline\T_i}_\infty)^V$ and wellfoundedness is absolute between $\h$ and $V$, a contradiction.

  \qquad Now assume that $M_1^{\N}(x)$ exists for some $x\in H_\theta$, and that it's countably iterable. We then do exactly the same thing as with $\N(x)$ \textit{except} that in the claim we replace $(1)$ with
  \eq{
    \overline\Q\models\forall\eta(\overline\Q|\eta\not\models\godel{\text{$\delta(\overline\T)$ is not Woodin}}),
  }

  so that if $\P\pinit\R$ then $\delta(\overline\T)$ is still Woodin in $\P=\N(\M(\overline\T))$, contradicting the defining property of $M_1^{\N}(x)$ (and thus also of $\R$). The rest of the proof is a copy of the above.
}

\theo[Hybrid core model dichotomy][theo.kdichotomy]{
  Let $\theta$ be a $\beth$-fixed point or $\theta=\infty$, and let $\F$ be a model operator on $H_\theta$ that condenses well. Let $x\in H_\theta$. Then either:
  \begin{enumerate}
    \item The core model $K^{\F}(x)|\theta$ exists and is $(\theta,\theta)$-iterable; or
    \item $M_1^{\F}(x)$ exists and is $(\theta,\theta)$-iterable.
  \end{enumerate}
}
\proof{
  Assume first that $K^{c,\F}(x)|\theta$ reaches a premouse which isn't $\F$-small; let $\N_\xi$ be the first part of the construction witnessing this. Then $\core(\N_\xi)=M_1^{\F}(x)$\todo{Insert argument?}, and by Lemma \ref{lemm.opit} it suffices to show that $M_1^{\F}(x)$ is countably iterable.\todo[inline]{Show that $M_1^{\F}(x)$ is countably iterable.}

  \qquad We can thus assume that $K^{c,\F}(x)|\theta$ is $\F$-small. Note that if $K^{c,\F}(x)|\theta$ has a Woodin cardinal then because the model is $\F$-closed we contradict $\F$-smallness, so the model has no Woodin cardinals either, making it $(\theta,\theta)$-iterable.

  \qquad Let $\kappa<\theta$ be any uncountable cardinal and let $\Omega:=\beth_\kappa(\kappa)^+$. Note that $\Omega<\theta$ since we assumed that $\theta$ is a $\beth$-fixed point and $\kappa<\theta$. If $\Omega$ is a limit cardinal in $K^{c,\F}(x)|\theta$ then let $\S:=\lp(K^{c,\F}(x)|\Omega)$ and otherwise let $\S:=K^{c,\F}(x)|\Omega$. Then by Lemma 3.3 of
  \cite{mousestack} we get that $\S$ is countably iterable, with largest cardinal $\Omega$ in the ``limit cardinal case''.

  \qquad This also means that $\Omega$ isn't Woodin in $L[\S]$, as it's trivial in the case where $\Omega$ is a successor cardinal of $K^{c,\F}(x)|\theta$ by our case assumption, and in the ``limit cardinal case'' it also holds since
  \eq{
    K^{c,\F}(x)|\Omega^{+K^{c,\F}(x)|\theta}\subseteq\S.
  }

  By \cite{fernandes} and \cite{JensenSteel} this means that we can build $K^{\F}(x)|\kappa$, as the only places they use that there's no inner model with a Woodin are to guarantee that $K^{c,\F}(x)|\Omega$ exists and has no Woodin cardinals, and in Lemma 4.27 of \cite{JensenSteel} in which they only require that $\Omega$ isn't Woodin in $L[\S]$.

  \qquad As $\kappa<\theta$ was arbitrary we then get that $K^{\F}(x)|\theta$ exists. Note that $K^{\F}(x)|\theta$ has no Woodin cardinals either and is $\F$-small, so that $\Q$-structures trivially exist, making it $(\theta,\theta)$-iterable.
}

\section{Mouse witness equivalence}

\defi{
  \todo[inline]{Define coarse $(k,U,x)$-Woodin pairs}
}

\defi{
  Let $\F$ be a total condensing operator and let $\alpha$ be an ordinal. Then the \textbf{coarse mouse witness condition at $\alpha$ with $\F$}, written $W^*_\alpha(\F)$, states that given any scaled-co-scaled $U\subset\mathbb R$ whose associated sequences of prewellorderings are elements of $\lp^{\F}_\alpha(\mathbb R)$, we have for every $k<\omega$ and $x\in\mathbb R$ a coarse $(k,U,x)$-Woodin pair $(N,\Sigma)$ with $\Sigma\restr\hc\in\lp^{\F}_\alpha(\mathbb R)$.\todo{Check if this is a reasonable definition.}
}

\theo[Hybrid witness equivalence][theo.witness]{
  Let $\theta>0$ be a cardinal, $g\subset\col(\omega,{<}\theta)$ $V$-generic,  $\mathbb R^g:=\bigcup_{\alpha<\theta}\mathbb R^{V[g\restr\alpha]}$, $\F$ a total radiant operator and $\alpha$ a critical ordinal of $\lp^{\F}(\mathbb R^g)$. Assume that
  \eq{
    \lp^{\F}(\mathbb R^g)\models\dc+\godel{W^*_\beta(\F)\text{ holds for all }\beta\leq\alpha}.
  }

  Then there is a hybrid mouse operator $\N\in V$ on $H_{\aleph_1^{V[g]}}$ such that
  \eq{
    \lp^{\F}(\mathbb R^g)\models W^*_{\alpha+1}(\F)\quad\text{iff}\quad V\models\godel{\text{$M_n^{\N}$ is total on $H_{\aleph_1^{V[g]}}$ for all $n<\omega$}}
  }

  Furthermore, if $\theta<\aleph_1^V$ then we only need to assume that $\F$ is total and condensing.
}

\todo[inline]{Be more explicit about what the given operator $\N$ looks like.}

\end{document}


\section{Conclusion}\label{sec:conclusion}

This work presents a novel approach to investigating a healthcare population
that encompasses the topics of segmentation analysis, queuing models, and the
recovery of queuing parameters from incomplete data. This is done despite common
limitations in operational research with regard to the availability of
fine-grained data, and this work only uses administrative hospital spell data
from patients presenting COPD from the Cwm Taf Morgannwg UHB.\

By considering a variety of attributes present in the data, and engineering
some, an effective clustering of the spell population is identified that
successfully feeds into a multi-class, \(M/M/c\) queue to model a hypothetical
COPD ward. With this model, a number of insights are gained by investigating
purposeful changes in the parameters of the model that have the potential to
inform actual public health policy.

In particular, since neither the resource capacity of the system or the clinical
processes of the spells are evident in the data, service times and resource
levels are not available. However, length of stay is. Using what is available,
this work assumes that mean service times can be parameterised using mean
lengths of stay. By using the Wasserstein distance to compare the distribution
of the simulated lengths of stay data with the observed data, a best performing
parameter set is found via a parameter sweep.

This parameterisation ultimately recovers a surrogate for service times for each
cluster, and a common number of servers to emulate resource availability. The
parameterisation itself offers its strengths by being simple and effective.
Despite its simplicity, a good fit to the observed data is found, and --- as is
evident from the closing section of this work --- substantial and useful
insights can be gained into the needs of the population being studied.

This analysis, and the formation of the entire model, in effect, considers all
types of patient arrivals and how they each impact the system in terms of
resource capacity and length of stay. By investigating scenarios into changes in
both overall patient arrivals and resource capacity, it is clear that there is
no quick solution to be employed from within the hospital to improve COPD
patient spells.  The only effective, non-trivial intervention is to improve the
overall health of the patients arriving at the hospital. This is shown by moving
patient arrivals between clusters. In reality, this would correspond to an
external, preventative policy that improves the overall health of COPD patients.