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

\begin{proof}
  We'll show that $A_+ \cn_n \cn_m \equivb \cn_{n + m}$,
  and leave the rest to you.

  First note that
  \[
    \cn_n s z = (\lambda f . \lambda
    x . f^n(x)) sz \equivb (\lambda x . s^n(x))z
    \equivb s^n(z)
  .\]
  So:
  \begin{align*}
    A_+ \cn_n \cn_m
    &= (\lambda x . \lambda y . \lambda s .
    \lambda z . xs(ysz)) \cn_n \cn_m \\
    &\equivb (\lambda y . \lambda s . \lambda z .
    \cn_n s(ysz)) \cn_m \\
    &\equivb \lambda s . \lambda z . \cn_n s(\cn_m
    sz)) \\
    &\equivb \lambda s . \lambda z . s^n (s^m z)
    \\
    &\equivb \lambda s . \lambda z . s^n (s^m z)
    \\
    &\equivb \lambda s . \lambda z . s^{m + n}(z)
    \\
    &\equivb \cn_{n + m}
    \qedhere
  \end{align*}
\end{proof}

In a similar fashion, we can also encode binary
truth-values:

\glssymboldefn{ctopbot}%
\begin{fcprop}[]
  \glssymboldefn{ifelse}%
  \label{prop:2.1.14}
  % Proposition 2.1.14
  Define the \glspl{ulterm}:
  \begin{citems}
    \item $\top \defeq \lambda x . \lambda y . x$
    \item $\bot \defeq \lambda x . \lambda y . y$
    \item $(\text{if $B$ then $P$ else $Q$} \defeq
      BPQ)$
  \end{citems}
  Then for \glspl{ulterm} $P$ and $Q$, we have
  \begin{cenum}[(i)]
    \item $(\text{if $\top$ then $P$ else $Q$})
      \equivb P$;
    \item $(\text{if $\bot$ then $P$ else $Q$})
      \equivb Q$.
  \end{cenum}
\end{fcprop}

\begin{proof}
  Just compute it!
\end{proof}

With this, we can encode logical connectives via:
\begin{itemize}
  \item $\pnot p \defeq \ife{p}{\cbot}{\ctop}$;
  \item $\pand p_1 p_2 \defeq
    \ife{p_1}{(\ife{p_2}{\ctop}{\cbot})}{\cbot}$;
  \item $\por p_1 p_2 \defeq
    \ife{p_1}{\ctop}{(\ife{p_2}{\ctop}{\cbot})}$.
\end{itemize}

\glssymboldefn{pair}%
We can also encode pairs: if we define $[P, Q]
\defeq \lambda x . x P Q$, then $[P, Q] \ctop
\equivb P$ and $[P, Q] \cbot \equivb Q$. However,
it is \emph{not true} that $[M \ctop, M \cbot]
\equivb M$!

Recursively defining terms within the
$\lambda$-calculus requires a clever idea: we see
such a \gls{uterm} as a solution to a fixed point
equation $F = \lambda x . M$ where $F$ occurs
somewhere in $M$.

\begin{fcthm}[Fixed Point Theorem]
  \label{thm:2.1.15}
  % Theorem 2.1.15
  There is a \gls{ulterm} $Y$ such that, for all
  $F$:
  \[
    \mathcloze{F(YF) \equivb YF}
  .\]
\end{fcthm}

\begin{proof}
  Define
  \[
    Y = \lambda f . (\lambda x . f(xx)) \lambda x
    . f(xx)
  .\]
  If we compute $YF$, we get:
  \begin{align*}
    YF
    &= (\lambda f . (\lambda x . f(xx)) \lambda x
    . f(xx)) F \\
    &\equivb (\lambda x . F(xx)) \lambda x . F(xx)
    \\
    &\equivb F((\lambda x . F(xx)) (\lambda x .
    F(xx))) \\
    &\equivb F ((\lambda f . (\lambda x . f(xx))
    \lambda x . f(xx)) F) \\
    &\equivb F(YF)
    \qedhere
  \end{align*}
\end{proof}

\glsnoundefn{fpc}{fixed-point
combinator}{fixed-point combinators}%
We call any combinator (i.e. a \gls{ulterm}
without free variables) $Y$ satisfying the
property $F(YF) \equivb YF$ for all \glspl{uterm}
$F$ a \emph{fixed-point combinator}.

\begin{fccoro}[]
  \label{coro:2.1.16}
  % Corollary 2.1.16
  Given a \gls{ulterm} $M$, there is a
  \gls{ulterm} $F$ such that $F \equivb M[f \defeq
  F]$.
\end{fccoro}

\begin{proof}
  Take $F = \Y \lambda f . M$. Then
  \[
    F
    \equivb (\lambda f . M) \Y (\lambda f . M)
    \equivb (\lambda f . M) F
    \equivb M[f \defeq F]
    .
    \qedhere
  \]
\end{proof}

\begin{example}
  \label{eg:2.1.17}
  % Example 2.1.17
  Suppose $D$ is a \gls{ulterm} encoding a
  predicate, i.e. $P\cn_n \equivb \cbot$ or
  $\ctop$ for every $n \in \Nbb$. Let's write down
  a \gls{ulterm}that encodes a program that takes
  a number and computes the next number satisfying
  the predicate.

  First consider
  \[
    M \defeq \lambda f . \lambda x .
    (\ife{(Px)}{x}{f(Sx)})
  ,\]
  where $S$ encodes the successor map. Our goal is
  to have $M$ run on itself. This can be done by
  using the \gls{uterm} $F \defeq YM$. Indeed:
  \[
    F \cn_n \equivb (\ife{P\cn_n}{\cn_n}{F\cn_{n +
    1}})
  \]
  for every $n \in \Nbb$.
\end{example}

\begin{notation*}
  $\lambda xsz . f$ will be short hand for
  $\lambda x . \lambda s . \lambda z . f$ (and the
  obvious generalisation to any number of
  variables, labelled in any way).
\end{notation*}

\glssymboldefn{succ}%
\begin{fclemma}[]
  \label{lemma:2.1.18}
  % Lemma 2.1.18
  The basic \glspl{prf} are $\lambda$-definable.
\end{fclemma}

\begin{proof}
  The $i$-th projection $\Nbb^k \to \Nbb$ is
  definable by $\pi_i^k : \lambda x_1 \ldots
  \lambda x_k . x_i$.

  \glsref[succ]{Successor} is implemented by $S \defeq \lambda x
  . \lambda s . \lambda z . s(xsz)$.

  The zero map is given by $Z \defeq \lambda x .
  \cn_0$.

  Just compute!
\end{proof}
