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

\begin{fclemma}
  \label{lemma:2.1.19}
  % Lemma 2.1.19
  The class of \gls{ldef} functions is closed
  under \gls{compon}.
\end{fclemma}

\begin{proof}
  Say $G$ is a \gls{ulterm} defining $g : \Nbb^k
  \to \Nbb$, and that \glspl{ulterm} $H_1, \ldots,
  H_k$ define $h_1, \ldots, h_k : \Nbb^m \to
  \Nbb$. Then the composite map $f : \ol{n}
  \mapsto g(h_1(\ol{n}), \ldots, h_k(\ol{n}))$ is
  definable by the \gls{uterm}
  \[
    F \defeq \lambda x_1 \ldots x_m : (G(H_1 x_1
    \ldots x_m) \ldots (H_k x_1 \ldots x_m))
  \]
  by inspection.
\end{proof}

\begin{fclemma}
  \label{lemma:2.1.20}
  % Lemma 2.1.20
  The class of \gls{ldef} functions is closed
  under \gls{primrec}.
\end{fclemma}

\begin{proof}
  Suppose $f : \Nbb^{m + 1} \to \Nbb$ is obtained
  from $h : \Nbb^{m + 2} \to \Nbb$ and $g : \Nbb^m
  \to \Nbb$ by \gls{primrec}.
  \begin{align*}
    f(0, \ol{n})
    &\defeq g(\ol{n}) \\
    f(k + 1, \ol{n})
    &\defeq h(f(k, \ol{n}), k, \ol{n})
  \end{align*}
  and the \glspl{ulterm} $H$ and $G$ define $h$
  and $h$ respectively.

  We need a \gls{ulterm} to keep track of a pair
  that records the current state of computation:
  the value of $k$ and the value of $f$ at that
  stage.

  So define
  \[
    T \defeq \lambda p . \pair[\fsucc(p\pi_1), H(p\pi_2)
    (p\pi_1) x_1 \ldots x_n]
  ,\]
  which acts on a pair $\pair[\cn_k, \cn_{f(k, \ol{n}}]$ by
  updating the iteration data. Then $f$ ought to
  be definable by
  \[
    F \defeq \lambda x . \lambda x_1 \ldots x_m .
    x T \pair[\cn_0, G x_1 \ldots x_m] \pi_2
  .\]
  Indeed,
  \begin{align*}
    F \cn_k \cn_{n_1} \ldots \cn_{n_m}
    &\equivb \cn_k T\pair[\cn_0, G \cn_{n_1} \ldots
    \cn_{n_m}] \pi_2 \\
    &\equivb T^k \pair[\cn_0, \cn_{g(\pi)}] \pi_2
  \end{align*}
  by definition of $\cn_k$, and since
  \begin{align*}
    T\pair[\cn_k, \cn_{f(k, \pi)}]
    &\equivb \pair[\fsucc \cn_k, H \cn_{f(k, \ol{n})}
    \cn_k \cn_{n_1}, \ldots, \cn_{n_m}] \\
    &\equivb [\cn_{k + 1}, \cn_{h(f(k, \ol{n}), k,
    \ol{n})}]
  \end{align*}
  we have
  \[
    F \cn_k \cn_{n_1} \ldots \cn_{n_m} \equivb T^k
    ([\cn_0, G \cn_{n_1} \ldots \cn_{n_m}]) \pi_2
    \equivb \cn_{f(k, \ol{n})}
  \]
  as needed.
\end{proof}

\begin{fclemma}
  \label{lemma:2.1.21}
  % Lemma 2.1.21
  The \gls{ldef} functions are closed under
  \gls{minon}.
\end{fclemma}

\begin{proof}
  Suppose $G$ \glspl{ldefine} $g : \Nbb^{m + 1} \to
  \Nbb$, and that $f : \Nbb^m \to \Nbb$ is defined
  from $g$ by \gls{minon}: $f(\ol{n}) = \mu k .
  g(k, \ol{n}) = 0$.

  We can \gls{ldefine} $f$ by implementing an
  algorithm that searches for the least $k$ in the
  following way:

  \newcommand\zero{\operatorname{zero?}}
  First define a \gls{uterm} that can check if a
  \gls{cn} is $\cn_0$, for example
  \[
    \zero \defeq \lambda x . x (\lambda y
    . \bot) \top
  .\]
  You can check that
  \[
    \zero \cn_n \equivb \begin{cases}
      \top & \text{if $n = 0$} \\
      \bot & \text{otherwise}
    \end{cases}
  .\]
  Now we want a \gls{uterm} that, on input $k$,
  checks if $g(k, \ol{n}) = 0$ and returns $k$ if
  so, else runs itself on $k + 1$. If we can do
  this, running it on input $k = 0$ will perform
  the search.

  \newcommand\search{\operatorname{Search}}
  Let:
  \[
    \search
    \defeq \lambda f . \lambda g . \lambda k .
    \lambda x_1 \ldots \lambda x_m . (\ife{\zero
    (g k x_1 \ldots x_m)}{k}{(f(g(\fsucc k) x_1 \ldots
    x_m))})
  ,\]
  and set
  \[
    F \defeq \lambda x_1 \ldots \lambda x_m . (\Y
    \search) G \cn_0 x_1 \ldots x_m
  .\]
  Note that
  \[
    (\Y\search) G \cn_k \cn_{n_1} \ldots \cn_{n_m}
    \equivb \search (\Y\search) G \cn_k \cn_{n_1}
    \ldots \cn_{n_m}
  ,\]
  which is
  \[
    \ife{\zero(G \cn_k \cn_{n_1} \ldots
    \cn_{n_m})}{\cn_k}{((\Y\search) G\cn_{k + 1}
    \cn_{n_1} \ldots \cn_{n_m}}
  .\]
  Thus
  \[
    (\Y \search) G \cn_k \cn_{n_1} \ldots \cn_{n_m}
    \equivb \cn_k
  \]
  if $g(k, \ol{n}) = 0$ and
  \[
    (\Y \search) G\cn_k \cn_{n_1} \ldots \cn_{n_m}
    \equivb (\Y \search) G\cn_{k + 1} \cn_1 \ldots
    \cn_m
  \]
  otherwise, as $g$ is
  \glsref[ldef]{$\lambda$-defined} by $G$. Hence
  \[
    F\cn_{n_1} \ldots \cn_{n_m}
    \equivb (\Y\search) G \cn_0 \cn_{n_1} \ldots
    \cn_{n_m}
    \equivb \cn_{f(\ol{n})}
  \]
  if $f$ is defined on $\ol{n}$. So $F$
  \glspl{ldefine} $f$.
\end{proof}

\begin{fclemma}
  \label{thm:2.1.22}
  % Theorem 2.1.22
  Every \gls{prf} is \gls{ldef}.
\end{fclemma}
