%! TEX root = LC.tex
% vim: tw=50
% 18/10/2024 11AM

\begin{fclemma}[Generation Lemma]
  \label{lemma:gen}
  % Lemma 1.2.6
  \phantom{}
  \begin{cenum}[(1)]
    \item For every variable $x$, \gls{cont}
      $\Gamma$, and \gls{type} $\sigma$, if
      $\Gamma \trel x : \sigma$, then $x : \sigma
      \in \Gamma$;
    \item If $\Gamma \trel (MN) : \sigma$, then
      there is a type $\tau$ such that $\Gamma
      \trel M : \tau \to \sigma$ and $\Gamma \trel
      N : \tau$;
    \item If $\Gamma \trel (\lambda x.M) :
      \sigma$, then there are \glspl{type} $\tau$
      and $\rho$ such that $\Gamma, x : \tau \trel
      M : \rho$ and $\sigma = (\tau \to \rho)$.
  \end{cenum}
\end{fclemma}

\noproof

\begin{fclemma}[Substitution Lemma]
  \label{lemma:sub}
  % Lemma 1.2.7
  \phantom{}
  \begin{cenum}[(1)]
    \item If $\Gamma \trel M : \sigma$ and
      $\alpha$ is a \gls{type} variable, then
      $\Gamma\subst[\alpha \defeq \tau] \trel M :
      \sigma\subst[\alpha \defeq \tau]$;
    \item If $\Gamma, x : \tau \trel M : \sigma$
      and $\Gamma \trel N : \tau$, then $\Gamma
      \trel M\subst[x \defeq N] : \sigma$.
  \end{cenum}
\end{fclemma}

\noproof

\begin{fcprop}[Subject reduction]
  % Proposition 1.2.8
  Assuming:
  - $\Gamma \trel M : \sigma$
  - $M \tob N$
  Then:
  $\Gamma \trel N : \sigma$.
\end{fcprop}

\begin{proof}
  By induction on the derivation of $M \tob N$,
  using \cref{lemma:gen} and \cref{lemma:sub}.
\end{proof}

\begin{notation*}
  \glssymboldefn{tobev}%
  We will write $M \twoheadrightarrow_\beta N$ if
  $M$ \gls{red} to $N$ after (potentially
  multiple) \glspl{breduc}.
\end{notation*}

\begin{fcthm}[Church-Rosser for lambda(->)]
  \label{chross}
  % Theorem 1.2.9
  Assuming:
  - $\Gamma \trel M : \sigma$
  - $M \tobev N_1$
  - $M \tobev N_2$
  Then:
  there is a \gls{lterm} $L$ such that $N_1 \tobev
  L$, $N_2 \tobev L$, and $\Gamma \trel L :
  \sigma$.
\end{fcthm}

Pictorially:
\begin{picmath}
  \begin{tikzcd}
    &
    M \ar[ld, "\beta", two heads, swap] \ar[rd, "\beta",
    two heads]
    & \\
    N_1 \ar[rd, "\beta", dashed, two heads, swap]
    &
    & N_2 \ar[ld, "\beta", dashed, two heads] \\
    &
    L
    &
  \end{tikzcd}
\end{picmath}

\begin{fcdefnstar}[$beta$-normal form]
  \glsnoundefn{bnf}{$\beta$-normal form}{NA}%
  A \gls{lterm} $M$ is in \emph{$\beta$-normal
  form} if there is no \gls{term} $N$ such that $M
  \tob N$.
\end{fcdefnstar}

\begin{fccoro}[Uniqueness of normal form]
  % Corollary 1.2.10
  If a \gls{stlterm} admits a \gls{bnf}, then it
  is unique.
\end{fccoro}

\noproof

\begin{fcprop}[Uniqueness of types]
  \phantom{}
  \begin{cenum}[(1)]
    \item If $\Gamma \trel M : \sigma$ and $\Gamma
      \trel M : \tau$, then $\sigma = \tau$.
    \item If $\Gamma \trel M : \sigma$, $\Gamma
      \trel N : \tau$, and $M \equivb N$, then
      $\sigma = \tau$.
  \end{cenum}
\end{fcprop}

\begin{proof}
  \phantom{}
  \begin{enumerate}[(1)]
    \item Induction.
    \item By the hypothesis and \nameref{chross},
      there is a \gls{term} $L$ which both $M$ and
      $N$ reduce to. By \cref{lemma:sub}, we have
      $\Gamma \trel L : \sigma$ and $\Gamma \trel
      L : \tau$, so $\sigma = \tau$ by (1).
      \qedhere
  \end{enumerate}
\end{proof}

\begin{example}
  % Example 1.2.12
  There is no way to assign a \gls{type} to
  $\lambda x : x . x$. If $x$ is of type $\tau$,
  then in order to apply $x$ to $x$, it has to be
  of type $\tau \to \sigma$ for some $\sigma$. But
  $\tau \neq \tau \to \sigma$.
\end{example}

\begin{center}
  \includegraphics[width=0.6\linewidth]{images/7c572e2c39d648c3.png}
\end{center}

\begin{fcdefn}[Height]
  \glssymboldefn{h}%
  \glsnoundefn{height}{height}{heights}%
  The \emph{height} function is the recursively
  defined map $h : \stypes \to \Nbb$ that maps a
  \gls{type} variable to $0$, and a function type
  $\sigma \to \tau$ to $1 + \max(h(\sigma),
  h(\tau))$.

  We extend the height function from \glspl{type}
  to \glspl{bred} by taking the height of its
  \gls{lab}.
\end{fcdefn}

Not.: $(\lambda x : \sigma . P^\tau)^{\sigma \to
\tau} R^\sigma$.

\begin{fcthm}[Weak normalisation for lambda(->)]
  Assuming:
  - $\Gamma \trel M : \sigma$
  Then: there is a finite \gls{reduc} path $M
  \defeq M_0 \tob M_1 \tob M_2 \tob \cdots \tob
  M_n$, where $M_n$ is in \gls{bnf}.
\end{fcthm}

\begin{proof}[Proof (``Taming the Hydra'')]
  The idea is to apply induction on the complexity
  of $M$. Define a function $m : \lterms \to \Nbb
  \times \Nbb$ by
  \[
    m(M) = \begin{cases}
      (0, 0) & \text{if $M$ is in \gls{bnf}} \\
      (\height(M), \mathrm{redex}(M)) & \text{otherwise}
    \end{cases}
  ,\]
  where $h(M)$ is the greatest \gls{height} of a
  \gls{redex} in $M$, and $\mathrm{redex}(M)$ is the
  number of \glspl{redex} in $M$ of that
  \gls{height}.

  We will use induction over $\omega \times
  \omega$ to show that if $M$ is typable, then it
  admits a reduction to \gls{bnf}.

  \textbf{Problem:} \glspl{reduc} can copy
  \glspl{redex} or create new ones.

  \textbf{Strategy:} always \glsref[red]{reduce} the
  right most \gls{redex} of maximum \gls{height}.

  We will argue that by following this strategy, any
  new \glspl{redex} we generate have to be lower
  than the height of the \gls{redex} we picked to
  \glsref[red]{reduce}.
