content/lambda-calculus/lambda-definability/lambda-definability.tex
1% Part: lambda-calculus2% Chapter: lambda-definability34\documentclass[../../../include/open-logic-chapter]{subfiles}56\begin{document}78\olchapter{lam}{rep}{Lambda Definability}910\begin{editorial}11 This chapter is experimental. It needs more explanation, and the12 material should be structured better into definitions and13 propositions with proofs, and more examples.14\end{editorial}1516\olimport{introduction}17\olimport{arithmetical-functions}18\olimport{pairs}19\olimport{truth-values}20%\olimport{lists}21\olimport{primitive-recursive-functions}22\olimport{fixpoints}23\olimport{minimization}24\olimport{partial-recursive-functions}25\olimport{lambda-definable-recursive}2627\OLEndChapterHook2829\end{document}
content/lambda-calculus/lambda-definability/introduction.tex
1% Part: lambda-calculus2% Chapter: lambda-definability3% Section: introduction45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{ldf}{int}10\olsection{Introduction}1112At first glance, the lambda calculus is just a very abstract calculus13of expressions that represent functions and applications of them to14others. Nothing in the syntax of the lambda calculus suggests that15these are functions of particular kinds of objects, in particular, the16syntax includes no mention of natural numbers. Its basic17operations---application and lambda abstractions---are operations that18apply to any function, not just functions on natural19numbers.2021Nevertheless, with some ingenuity, it is possible to define22arithmetical functions, i.e., functions on the natural numbers, in the23lambda calculus. To do this, we define, for each natural number~$n \in24\Nat$, a special $\lambd$-term~$\num n$, the \emph{Church numeral}25for~$n$. (Church numerals are named for Alonzo Church.)2627\begin{defn}28 If $n \in \Nat$, the corresponding \emph{Church numeral} $\num{n}$29 represents~$n$:30 \[31 \num{n} \ident \lambd[fx][f^n(x)]32 \]33 Here, $f^n(x)$ stands for the result of applying $f$ to~$x$ $n$34 times. For example, $\num{0}$ is $\lambd[fx][x]$, and $\num{3}$ is35 $\lambd[fx][f(f(f\,x))]$.36\end{defn}37 38The Church numeral $\num n$ is encoded as a lambda term which39represents a function accepting two arguments $f$ and $x$, and40returns $f^n(x)$. Church numerals are evidently in normal form.4142A representation of natural numbers in the lambda calculus is only43useful, of course, if we can compute with them. Computing with Church44numerals in the lambda calculus means applying a $\lambd$-term~$F$ to45such a Church numeral, and reducing the combined term~$F\, \num n$ to46a normal form. If it always reduces to a normal form, and the normal47form is always a Church numeral~$\num m$, we can think of the output48of the computation as being the number~$m$. We can then think of~$F$49as defining a function $f\colon \Nat \to \Nat$, namely the function50such that $f(n) = m$ iff $F\, \num n \red \num m$. Because of the51Church--Rosser property, normal forms are unique if they exist. So if52$F\, \num n \red \num m$, there can be no other term in normal form,53in particular no other Church numeral, that $F \, \num n$ reduces to.5455Conversely, given a function $f\colon \Nat \to \Nat$,56we can ask if there is a term $F$ that defines~$f$ in this way. In57that case we say that $F$ \emph{!!{lambda define}s}~$f$, and that $f$ is58!!{lambda definable}. We can generalize this to many-place and partial59functions.6061\begin{defn}62Suppose $f\colon \Nat^k \to \Nat$. We say that a lambda term~$F$63\emph{!!{lambda define}s} $f$ if for all 64$n_0$, \dots,~$n_{k-1}$,65\[66F \, \num{n_0} \, \num{n_1} \dots \num{n_{k-1}} \red \num{f(n_0, n_1, \dots,67 n_{k-1})}68\]69if $f(n_0, \dots, n_{k-1})$ is defined, and $F \, \num{n_0} \,70\num{n_1} \dots \num{n_{k-1}}$ has no normal form otherwise.71\end{defn}7273A very simple example are the constant functions. The term $C_k \ident74\lambd[x][\num{k}]$ !!{lambda define}s the function $c_k\colon \Nat \to75\Nat$ such that $c(n) = k$. For $C_k \, \num n \ident76(\lambd[x][\num{k}])\num n \redone \num{k}$ for any~$n$. The identity77function is !!{lambda defined} by $\lambd[x][x]$. More complex78functions are of course harder to define, and often require a lot of79ingenuity. So it is perhaps surprising that every computable function80is !!{lambda definable}. The converse is also true: if a function is81!!{lambda definable}, it is computable.8283\end{document}
content/lambda-calculus/lambda-definability/arithmetical-functions.tex
1% Part: lambda-calculus2% Chapter: lambda-definablity3% Section: arithmetical-functions45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{rep}{arf}10\olsection{\usetoken{S}{lambda definable} Arithmetical Functions}1112\begin{prop}13 \ollabel{prop:succ-ld}14 The successor function~$\Succ$ is !!{lambda definable}.15\end{prop}1617\begin{proof}18A term that !!{lambda define}s the successor function is19\begin{align*}20 \fn{Succ} & \ident \lambd[a][\lambd[fx][f ({a} f x)]].21 \intertext{Given our conventions, this is short for}22 \fn{Succ} & \ident \lambd[a][\lambd[f][\lambd[x][(f (({a} f) x))]]].23 \intertext{$\fn{Succ}$ is a function that accepts as argument a24 number~$a$, and evaluates to another function, $\lambd[fx][f ({a}25 f x)]$. That function is not itself a Church numeral. However,26 if the argument $a$ is a Church numeral, it reduces to one. Consider:}27 (\lambd[a][\lambd[fx][f ({a} f x)]])\,\num{n} & \redone28 \lambd[fx][f ({\num{n}} f x)].29 \intertext{The embedded term $\num{n}fx$ is a redex, since30 $\num{n}$ is $\lambd[fx][f^nx]$. So $\num{n}fx \redone f^nx$ and31 so, for the entire term we have}32 \fn{Succ}\, \num n & \red \lambd[fx][f(f^n(x))],33\end{align*}34i.e., $\num{n+1}$.35\end{proof}3637\begin{ex}38 Let's look at what happens when we apply $\fn{Succ}$ to~$\num{0}$,39 i.e., $\lambd[fx][x]$. We'll spell the terms out in full:40 \begin{align*}41 \fn{Succ}\, \num{0} & \ident (\lambd[a][\lambd[f][\lambd[x][(f (({a} f) x))]]])(\lambd[f][\lambd[x][x]])\\42 & \redone \lambd[f][\lambd[x][(f (({(\lambd[f][\lambd[x][x]])} f) x))]] \\43 & \redone \lambd[f][\lambd[x][(f ({(\lambd[x][x])} x))]] \\44 & \redone \lambd[f][\lambd[x][(f x)]] \ident \num{1}\\45 \end{align*}46\end{ex}4748\begin{prob}49 The term 50 \begin{align*}51 \fn{Succ}' & \ident \lambd[{n}][\lambd[fx][{n} f (f x)]]52 \end{align*}53 !!{lambda define}s the successor function. Explain why.54\end{prob}5556\begin{prop}57 \ollabel{prop:add-ld}58 The addition function~$\Add$ is !!{lambda definable}.59\end{prop}6061\begin{proof}62Addition is !!{lambda define}d by the terms 63\begin{align*}64 \fn{Add} & \ident \lambd[{a}{b}][\lambd[fx][{a} f ({b} f x)]]65\intertext{or, alternatively,}66 \fn{Add}' & \ident \lambd[{a}{b}][{a}\, \fn{Succ} \, {b}].67\intertext{The first addition works as follows: $\fn{Add}$ first68 accept two numbers ${a}$ and ${b}$. The result is a function that69 accepts $f$ and $x$ and returns $af(bfx)$. If $a$ and $b$ are Church70 numerals $\num{n}$ and $\num{m}$, this reduces to $f^{n+m}(x)$,71 which is identical to $f^{n}(f^{m}(x))$. Or, slowly:}72 (\lambd[{a}{b}][\lambd[fx][{a} f ({b} f x)]])\num n\,\num m & \redone73 \lambd[fx][\num{n}\, f (\num {m}\, f x)] \\74 & \redone \lambd[fx][\num{n}\, f (f^m x)] \\75 & \redone \lambd[fx][f^n (f^m x)] \ident \num {n+m}.76\intertext{The second representation of addition $\fn{Add'}$ works77 differently: Applied to two Church numerals $\num{n}$ and78 $\num{m}$,}79\fn{Add}' \num n \,\num m80& \redone \num{n}\, \fn{Succ}\, \num{m}.81\intertext{But $\num{n} f x$ always reduces to $f^n(x)$. So,}82 \num{n}\, \fn{Succ}\, \num{m} & \red \fn{Succ}^n(\num{m}).83\end{align*}84And since $\fn{Succ}$ !!{lambda define}s the successor function, and the85successor function applied $n$ times to~$m$ gives $n+m$, this in turn86reduces to~$\num{n+m}$.87\end{proof}8889\begin{prop}90 \ollabel{prop:mult-ld}91 Multiplication is !!{lambda definable} by the term92 \[93 \fn{Mult} \ident \lambd[ab][\lambd[fx][a (b f) x]]94 \]95\end{prop}9697\begin{proof}98 To see how this works, suppose we apply $\fn{Mult}$ to Church numerals99 $\num{n}$ and $\num{m}$: $\fn{Mult} \, \num{n} \, \num{m}$ reduces to100 $\lambd[fx][\num{n}(\num{m}\, f)x]$. The term $\num{m} f$ defines a101 function which applies $f$ to its argument $m$ times. Consequently,102 $\num{n} (\num{m} f) x$ applies the function ``apply $f$ $m$ times''103 itself $n$ times to~$x$. In other words, we apply $f$ to $x$, $n\cdot104 m$ times. But the resulting normal term is just the Church numeral105 $\num{nm}$.106\end{proof}107108\begin{editorial}109We can actually simplify this term further by $\eta$-reduction:110\[111 \fn{Mult} \ident \lambd[ab][\lambd[f][a (b f)]].112\]113114But then we first have to explain $\eta$-reduction.115\end{editorial}116117\begin{prob}118Multiplication can be !!{lambda define}d by the term119\[120 \fn{Mult}' \ident \lambd[ab][a (\fn{Add}\, a) \num{0}].121\]122Explain why this works.123\end{prob}124125The definition of exponentiation as a $\lambd$-term is126surprisingly simple:127\[128 \fn{Exp} \ident \lambd[be][e b].129\]130The first argument $b$ is the base and the second~$e$ is the exponent.131Intuitively, $e f$ is $f^e$ by our encoding of numbers. If you find it132hard to understand, we can still define exponentiation also by133iterated multiplication:134\[135 \fn{Exp}' \ident \lambd[be][e (\fn{Mult}\, b) \num{1}].136\]137138Predecessor and subtraction on Church numeral is not as simple as we139might think: it requires encoding of pairs.140141\end{document}
content/lambda-calculus/lambda-definability/pairs.tex
1% Part: lambda-calculus2% Chapter: lambda-definability3% Section: pairs45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{ldf}{pai}10\olsection{Pairs and Predecessor}1112\begin{defn}13The pair of $M$ and $N$ (written $\tuple{M,N}$) is defined as follows:14\[15\tuple{M,N} \ident \lambd[f][fMN].16\]17\end{defn}18 19Intuitively it is a function that accepts a function, and applies that20function to the two elements of the pair. Following this idea we have21this constructor, which takes two terms and returns the pair containing22them:23\[24 \fn{Pair} \ident \lambd[mn][\lambd[f][fmn]]25\]26Given a pair, we also want to recover its elements.27For this we need two access functions, which accept a pair as argument and28return the first or second elements in it:29\begin{align*}30 \fn{Fst} & \ident \lambd[p][p(\lambd[mn][m])]\\31 \fn{Snd} & \ident \lambd[p][p(\lambd[mn][n])]32\end{align*}3334\begin{prob}35 Explain why the access functions $\fn{Fst}$ and $\fn{Snd}$ work.36\end{prob}3738Now with pairs we can !!{lambda define} the predecessor function:39\[40 \fn{Pred} \ident \lambd[n][\fn{Fst}(n (\lambd[p][\tuple{\fn{Snd}\, {p}, \fn{Succ}(\fn{Snd}\, {p})}]) \tuple{\num 0, \num 0})]41\]42Remember that $\num n\, f x$ reduces to $f^{n}(x)$; in this43case $f$ is a function that accepts a pair $p$ and returns a new44pair containing the second component of $p$ and the successor of the45second component; $x$ is the pair $\tuple{0,0}$. Thus, the46result is $\tuple{0,0}$ for $n=0$, and $\tuple{\num{n-1}, \num n}$47otherwise. $\fn{Pred}$ then returns the first component of the result.4849Subtraction can be defined as $\fn{Pred}$ applied to $a$, $b$ times:50\[51\fn{Sub} \ident \lambd[ab][b \fn{Pred}\, a].52\]53 54\end{document}
content/lambda-calculus/lambda-definability/truth-values.tex
1% Part: lambda-calculus2% Chapter: lambda-definability3% Section: truth-values45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{ldf}{tvr}10\olsection{Truth Values and Relations}1112We can encode truth values in the pure lambda calculus as follows:13\begin{align*}14 \fn{true} & \ident \lambd[x][\lambd[y][x]]\\15 \fn{false} & \ident \lambd[x][\lambd[y][y]]16\end{align*}1718Truth values are represented as \emph{selectors}, i.e., functions that19accept two arguments and returning one of them. The truth value20$\fn{true}$ selects its first argument, and $\fn{false}$ its21second. For example, $\fn{true}\, M N$ always reduces to $M$, while22$\fn{false}\, M N$ always reduces to~$N$.2324\begin{defn}25We call a relation $R \subseteq \Nat^n$ !!{lambda definable} if there is26a term~$R$ such that27\begin{align*}28 R\, \num{n_1} \dots \num{n_k} & \bred \fn{true}29 \intertext{whenever $R(n_1, \dots, n_k)$ and}30 R\, \num{n_1} \dots \num{n_k} & \bred \fn{false}31\end{align*}32otherwise.33\end{defn}3435For instance, the relation $\fn{IsZero} = \{0\}$ which holds of $0$36and $0$ only, is !!{lambda definable} by37\[38 \fn{IsZero} \ident \lambd[n][n (\lambd[x][\fn{false}])\, \fn{true}].39\]40How does it work? Since Church numerals are defined as iterators41(functions which apply their first argument $n$~times to the second),42we set the initial value to be $\fn{true}$, and for every step of43iteration, we return $\fn{false}$ regardless of the result of the last44iteration. This step will be applied to the initial value $n$45times, and the result will be $\fn{true}$ if and only if the step is46not applied at all, i.e., when $n = 0$.4748On the basis of this representation of truth values, we can further49define some truth functions. Here are two, the representations of50negation and conjunction:51\begin{align*}52 \fn{Not} & \ident \lambd[x][x\, \fn{false}\, \fn{true}]\\53 \fn{And} & \ident \lambd[x][\lambd[y][xy \,\fn{false}]]54\end{align*}55The function ``$\fn{Not}$'' accepts one argument, and returns56$\fn{true}$ if the argument is $\fn{false}$, and $\fn{false}$ if the57argument is~$\fn{true}$. The function ``$\fn{And}$'' accepts two58truth values as arguments, and should return $\fn{true}$ iff both59arguments are~$\fn{true}$. Truth values are represented as selectors60(described above), so when $x$ is a truth value and is applied to two61arguments, the result will be the first argument if $x$ is $\fn{true}$62and the second argument otherwise. Now $\fn{And}$ takes its two63arguments $x$ and $y$, and in return passes $y$ and $\fn{false}$ to64its first argument~$x$. Assuming $x$ is a truth value, the result65will evaluate to~$y$ if $x$ is $\fn{true}$, and to $\fn{false}$ if $x$66is $\fn{false}$, which is just what is desired.6768Note that we assume here that only truth values are used as arguments69to $\fn{And}$. If it is passed other terms, the result (i.e., the70normal form, if it exists) may well not be a truth value.7172\begin{prob}73 Define the functions $\fn{Or}$ and $\fn{Xor}$ representing the truth74 functions of inclusive and exclusive disjunction using the encoding75 of truth values as $\lambd$-terms.76\end{prob}7778\end{document}
content/lambda-calculus/lambda-definability/primitive-recursive-functions.tex
1% Part: lambda-calculus2% Chapter: lambda-definability3% Section: primitive-recursive-functions45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{ldf}{prf}10\olsection{Primitive Recursive Functions are \usetoken{S}{lambda definable}} 1112Recall that the primitive recursive functions are those that can be13defined from the basic functions $\Zero$, $\Succ$, and $\Proj{n}{i}$14by composition and primitive recursion.1516\begin{lem}17 \ollabel{lem:basic}18 The basic primitive recursive functions $\Zero$, $\Succ$, and19 projections~$\Proj{n}{i}$ are !!{lambda definable}.20\end{lem}2122\begin{proof}23They are !!{lambda define}d by the following terms:24\begin{align*}25 \fn{Zero} & \ident \lambd[a][\lambd[fx][x]]\\26 \fn{Succ} & \ident \lambd[a][\lambd[fx][f (a f x)]] \\27 \fn{Proj}^n_i & \ident \lambd[x_0\dots x_{n-1}][x_i]28\end{align*}29\end{proof}3031\begin{lem}32 \ollabel{lem:comp} Suppose the $k$-ary function $f$, and $n$-ary33 functions $g_0, \dots, g_{k-1}$, are !!{lambda definable} by terms34 $F$, $G_0$, \dots, $G_k$, and $h$ is defined from them by composition.35 Then $H$ is !!{lambda definable}.36\end{lem}3738\begin{proof}39 $h$ can be !!{lambda define}d by the term40 \[41 H \ident \lambd[x_0 \dots x_{n-1}][F\, (G_0 x_0 \dots42 x_{n-1}) \dots (G_{k-1} x_0 \dots x_{n-1})]43 \]44 We leave verification of this fact as an exercise.45\end{proof}4647\begin{prob}48 Complete the proof of \olref[lam][ldf][prf]{lem:comp} by showing49 that $H\num{n_0}\dots\num{n_{n-1}} \red \num{h(n_0, \dots,50 n_{n-1})}$.51\end{prob}5253Note that \olref{lem:comp} did not require that $f$ and $g_0$,54\dots,~$g_{k-1}$ are primitive recursive; it is only required that55they are total and !!{lambda definable}.5657\begin{lem}\ollabel{lem:prim}58 Suppose $f$ is an $n$-ary function and~$g$ is an $n+2$-ary function,59 they are !!{lambda definable} by terms $F$ and~$G$, and the60 function~$h$ is defined from $f$ and $g$ by primitive61 recursion. Then $h$ is also !!{lambda definable}.62\end{lem}6364\begin{proof}65 Recall that $h$ is defined by66 \begin{align*}67 h(x_1, \dots, x_n, 0) &= f(x_1, \dots, x_n)\\68 h(x_1, \dots, x_n, y+1) & = h(x_1, \dots, x_n, y, h(x_1, \dots, x_n, y)).69 \end{align*}70 Informally speaking, the primitive recursive definition iterates the71 application of the function $h$ $y$ times and applies it to $f(x_1,72 \dots, x_n)$. This is reminiscent of the definition of Church73 numerals, which is also defined as a iterator.7475 For simplicity, we give the definition and proof for a single76 additional argument~$x$. The function $h$ is !!{lambda define}d by:77 \begin{align*}78 H \ident & \lambd[x][\lambd[y][\fn{Snd} (y79 D \tuple{\num{0}, F x})]]80 \intertext{where }81 D \ident & \lambd[p][\tuple{\fn{Succ} (\fn{Fst}\, p), (G x82 (\fn{Fst}\, p) (\fn{Snd}\, p))}]83 \end{align*}84 The iteration state we maintain is a pair, the first of which is the85 current $y$ and the second is the corresponding value of~$h$. For86 every step of iteration we create a pair of new values of $y$ and87 $h$; after the iteration is done we return the second part of the88 pair and that's the final $h$ value. We now prove this is indeed a89 representation of primitive recursion.9091 We want to prove that for any $n$ and $m$, $H\,\num{n}\,\num{m} \red92 \num{h(n,m)}$. To do this we first show that if $D_n \ident93 \Subst{D}{\num{n}}{x}$, then $D_n^m \tuple{\num{0}, F\, \num{n}} \red94 \tuple{\num{m}, \num{h(n, m)}}$ We proceed by induction on~$m$.9596 If $m=0$, we want $D_n^0 \tuple{\num{0}, F\, \num{n}} \red97 \tuple{\num{0}, \num{h(n, 0)}}$. But $D_n^0 \tuple{\num{0}, F\,98 \num{n}}$ just is $\tuple{\num{0}, F\, \num{n}}$. Since $F$99 !!{lambda define}s~$f$, this reduces to $\tuple{\num{0},100 \num{f(n)}}$, and since $f(n) = h(n, 0)$, this is $\tuple{\num{0},101 \num{h(n,0)}}$102103 Now suppose that $D_n^m \tuple{\num{0}, F\, \num{n}} \red104 \tuple{\num{m}, \num{h(n, m)}}$. We want to105 show that $D_n^{m+1} \tuple{\num{0}, F\, \num{n}} \red106 \tuple{\num{m+1}, \num{h(n, m+1)}}$.107 \begin{align*}108 D_n^{m+1} \tuple{\num{0}, F\, \num{n}}109 & \ident D_n(D_n^{m} \tuple{\num{0}, F\, \num{n}})\\110 & \red D_n\,\tuple{\num{m}, \num{h(n, m)}} \text{\qquad (by IH)}\\111 & \ident (\lambd[p][112 \tuple{113 \fn{Succ} (\fn{Fst}\, p), (G \, \num{n} (\fn{Fst}\, p) (\fn{Snd}\, p))114 }]) \tuple{\num{m}, \num{h(n,m)}}\\115 & \redone116 \tuple{\fn{Succ} (\fn{Fst}\, \tuple{\num{m}, \num{h(n,m)}}),\\117 & \qquad (G \, \num{n} (\fn{Fst}\, \tuple{\num{m}, \num{h(n,m)}}) (\fn{Snd}\, \tuple{\num{m}, \num{h(n,m)}}))}\\118 & \red 119 \tuple{\fn{Succ}\, \num{m}, (G \, \num{n} \,\num{m} \, \num{h(n,m)})}\\120 & \red \tuple{\num{m+1}, \num{g(n, m, h(n, m))}}121 \end{align*}122 Since $g(n, m, h(n, m)) = h(n, m+1)$, we are done.123124 Finally, consider125 \begin{align*}126 H\,\num n\,\num m &\ident \lambd[x][\lambd[y][\fn{Snd} (y127 (\lambd[p].\tuple{\fn{Succ} (\fn{Fst}\, p), (G\, x\,128 (\fn{Fst}\, p)\, (\fn{Snd}\, p))}) \tuple{\num{0}, F x})]]\\129 & \qquad\,\num n\, \num m\\130 & \red \fn{Snd} (\num m \,131 \underbrace{(\lambd[p].\tuple{\fn{Succ} (\fn{Fst}\, p), (G \,\num n\,132 (\fn{Fst}\, p) (\fn{Snd}\, p))})}_{D_n} \tuple{\num{0}, F \num n})\\133 & \ident \fn{Snd} (\num m \, D_n\, \tuple{\num{0}, F \num n})\\134 & \red \fn{Snd} \,(D_n^m \tuple{\num{0}, F \num n})\\135 & \red \fn{Snd} \,\tuple{\num{m}, \num{h(n, m)}} \\136 & \red \num{h(n,m)}. 137 \end{align*} 138\end{proof}139140141\begin{prop}142 Every primitive recursive function is !!{lambda definable}.143\end{prop}144145\begin{proof}146 By \olref{lem:basic}, all basic functions are !!{lambda definable},147 and by \olref{lem:comp} and \olref{lem:prim}, the !!{lambda definable}148 functions are closed under composition and primitive recursion.149\end{proof}150151\end{document}152
content/lambda-calculus/lambda-definability/fixpoints.tex
1% Part: lambda-calculus2% Chapter: lambda-definability3% Section: fixpoints45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{ldf}{fp}1011\olsection{Fixpoints}1213Suppose we wanted to define the factorial function by recursion as a14term $\fn{Fac}$ with the following property:15\[16\fn{Fac} \ident \lambd[n][\fn{IsZero}\, n\, \num 1 (\fn{Mult}\, n (\fn{Fac}(\fn{Pred} \, n)))]17\]18That is, the factorial of $n$ is $1$ if $n = 0$, and $n$ times the19factorial of $n-1$ otherwise. Of course, we cannot define the term20$\fn{Fac}$ this way since $\fn{Fac}$ itself occurs in the right-hand21side. Such recursive definitions involving self-reference22are not part of the lambda calculus. Defining a term, e.g., by23\[24\fn{Mult} \ident \lambd[ab][a (\fn{Add}\, a) 0]25\]26only involves previously defined terms in the right-hand side, such as27$\fn{Add}$. We can always remove $\fn{Add}$ by replacing it with its28defining term. This would give the term $\fn{Mult}$ as a pure lambda29term; if $\fn{Add}$ itself involved defined terms (as, e.g.,30$\fn{Add}'$ does), we could continue this process and finally arrive at31a pure lambda term.3233However this is not true in the case of recursive definitions like the34one of $\fn{Fac}$ above. If we replace the occurrence of $\fn{Fac}$ on35the right-hand side with the definition of $\fn{Fac}$ itself, we get:36\begin{align*}37 \fn{Fac} & \ident \lambd[n][\fn{IsZero}\, n\, \num{1}] \\38 & \qquad (\fn{Mult}\, n39 ((\lambd[n][\fn{IsZero} \, n \, \num 1\, (\fn{Mult}\, n\, (\fn{Fac}40 (\fn{Pred}\, n)))]) (\fn{Pred}\, n)))41\end{align*}42and we still haven't gotten rid of $\fn{Fac}$ on the right-hand43side. Clearly, if we repeat this process, the definition keeps growing44longer and the process never results in a pure lambda term. Thus this45way of defining factorial (or more generally recursive functions)46is not feasible.4748The recursive definition does tell us something, though: If $f$ were a49term representing the factorial function, then the term50\[51\fn{Fac}' \ident \lambd[g][\lambd[n][\fn{IsZero} \, n \, \num 1\, (\fn{Mult} \, n\, (g (\fn{Pred} n)))]]52\]53applied to the term $f$, i.e., $\fn{Fac}'\,f$, also represents the54factorial function. That is, if we regard $\fn{Fac}'$ as a function55accepting a function and returning a function, the value of56$\fn{Fac'}\, f$ is just~$f$, provided $f$ is the factorial. A57function~$f$ with the property that $\fn{Fac}' \, f \equal[\beta] f$ is called58a \emph{fixpoint} of $\fn{Fac}'$. So, the factorial is a fixpoint of59$\fn{Fac}'$.6061There are terms in the lambda calculus that compute the fixpoints of a62given term, and these terms can then be used to turn a term like63$\fn{Fac}'$ into the definition of the factorial. 6465\begin{defn}66 \ollabel{defn:Turing-Y}67 The \emph{Y-combinator} is the term:68 \[69 Y \ident (\lambd[ux][x(uux)])(\lambd[ux][x(uux)]).70 \]71\end{defn}7273\begin{thm}74 $Y$ has the property that $Yg \red g(Yg)$ for any term $g$. Thus,75 $Yg$ is always a fixpoint of~$g$.76\end{thm}7778\begin{proof}79 Let's abbreviate $(\lambd[ux][x(uux)])$ by~$U$, so that $Y \ident UU$. Then80 \begin{align*}81 Y g &\ident (\lambd[ux][x(uux)])U\,g \\82 &\red (\lambd[x][x(UUx)])g\\83 & \red g(UUg) \ident g(Yg).84 \end{align*}85 Since $g(Yg)$ and $Yg$ both reduce to $g(Yg)$, $g(Yg) \equal[\beta]86 Yg$, so $Yg$ is a fixpoint of~$g$.87\end{proof}8889Of course, since $Yg$ is a redex, the reduction can continue indefinitely:90\begin{align*}91 Y g &\red g (Y g) \\92 &\red g (g (Y g)) \\93 &\red g(g (g (Y g)))\\94 &\ldots95\end{align*}96So we can think of $Yg$ as $g$ applied to itself infinitely many97times. If we apply~$g$ to it one additional time, we---so to98speak---aren't doing anything extra; $g$~applied to~$g$ applied99infinitely many times to~$Yg$ is still $g$~applied to~$Yg$ infinitely100many times.101102Note that the above sequence of $\beta$-reduction steps starting103with~$Yg$ is infinite. So if we apply $Yg$ to some term, i.e.,104consider $(Yg)N$, that term will also reduce to infinitely many105different terms, namely $(g(Yg))N$, $(g(g(Yg)))N$, \dots. It is106nevertheless possible that some \emph{other} sequence of reduction107steps does terminate in a normal form.108109Take the factorial for instance. Define $\fn{Fac}$ as $Y\, \fn{Fac}'$110(i.e., a fixpoint of $\fn{Fac'}$). Then:111\begin{align*}112 \fn{Fac}\,\num 3113 &\red Y\, \fn{Fac}' \, \num 3 \\114 &\red \fn{Fac}' (Y \,\fn{Fac}') \, \num 3 \\115 &\ident (\lambd[x][\lambd[n][\fn{IsZero} \, n\, \num 1\,116 (\fn{Mult}\, n\, (x (\fn{Pred}\, n)))]]) \, \fn{Fac} \, \num 3\\117 & \red \fn{IsZero} \, \num 3\, \num 1\,118 (\fn{Mult}\, \num 3\, (\fn{Fac} (\fn{Pred}\, \num 3))) \\119 &\red \fn{Mult}\, \num 3 \, (\fn{Fac} \, \num 2).120 \intertext{Similarly,}121 \fn{Fac}\,\num 2122 &\red \fn{Mult}\, \num 2 \, (\fn{Fac} \, \num 1) \\123 \fn{Fac}\,\num 1124 &\red \fn{Mult}\, \num 1 \, (\fn{Fac} \, \num 0)125 \intertext{but}126 \fn{Fac}\,\num 0127 &\red \fn{Fac}' (Y \,\fn{Fac}') \, \num 0 \\128 &\ident (\lambd[x][\lambd[n][\fn{IsZero} \, n\, \num 1\,129 (\fn{Mult}\, n\, (x (\fn{Pred}\, n)))]]) \, \fn{Fac} \, \num 0\\130 & \red \fn{IsZero} \, \num 0\, \num 1\,131 (\fn{Mult}\, \num 0\, (\fn{Fac} (\fn{Pred}\, \num 0))).\\132 &\red \num 1.133 \intertext{So together}134 \fn{Fac}\,\num 3135 &\red \fn{Mult}\, \num 3 \,136 (\fn{Mult} \, \num 2\, (\fn{Mult} \, \num 1\, \num 1)).137\end{align*}138139What goes for $\fn{Fac'}$ goes for any recursive definition. Suppose we140have a recursive equation141\begin{align*}142g\,x_1\dots x_n & \equal[\beta] N143\intertext{where $N$ may contain~$g$ and $x_1$, \dots,~$x_n$. Then there is144always a term~$G \ident (Y \lambd[g][\lambd[x_1\dots x_n][N]])$ such145that}146G\,x_1 \dots x_n & \equal[\beta] \Subst{N}{G}{g}.147\intertext{For by the fixpoint theorem,}148G \ident (Y \lambd[g][\lambd[x_1\dots x_n][N]]) & \red \lambd[g][\lambd[x_1\dots x_n][N]](Y \lambd[g][\lambd[x_1\dots x_n][N]])\\149& \ident (\lambd[g][\lambd[x_1\dots x_n][N]])\,G150\intertext{and consequently}151G\,x_1 \dots x_n 152& \red (\lambd[g][\lambd[x_1\dots x_n][N]])\,G\,x_1\dots x_n\\153& \red (\lambd[x_1\dots x_n][\Subst{N}{G}{g}])\,x_1\dots x_n\\154 & \red \Subst{N}{G}{g}.155\end{align*}156157The $Y$ combinator of \olref{defn:Turing-Y} is due to Alan158Turing. Alonzo Church had proposed a different version which we'll159call~$Y_C$:160\[161Y_C \ident \lambd[g][(\lambd[x][g(xx)])(\lambd[x][g(xx)])].162\]163Church's combinator is a bit weaker than Turing's in that $Yg164\equal[\beta] g(Yg)$ but not $Yg \bred g(Yg)$. Let $V$ be the term165$\lambd[x][g(xx)]$, so that $Y_C \ident \lambd[g][VV]$. Then166\begin{align*}167 VV & \ident (\lambd[x][g(xx)])V \red g(VV)168 \text{ and thus}\\169 Y_C g & \ident (\lambd[g][VV])g \red VV \red g(VV), \text{ but also}\\170 g(Y_C g) & \ident g((\lambd[g][VV])g) \red g(VV).171\end{align*}172In other words, $Y_Cg$ and $g(Y_Cg)$ reduce to a common term $g(VV)$; so $Y_Cg173\equal[\beta] g(Y_Cg)$. This is often enough for applications.174175\end{document}176
content/lambda-calculus/lambda-definability/minimization.tex
1% Part: lambda-calculus2% Chapter: lambda-definability3% Section: minimization45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{ldf}{min}10\olsection{Minimization}1112The general recursive functions are those that can be obtained from13the basic functions $\Zero$, $\Succ$, $\Proj{n}{i}$ by composition,14primitive recursion, and regular minimization. To show that all15general recursive functions are !!{lambda definable} we have to show16that any function defined by regular minimization from !!a{lambda17 definable} function is itself !!{lambda definable}.1819\begin{lem}20 \ollabel{lem:min} If $f(x_1, \dots, x_k, y)$ is regular and21 !!{lambda definable}, then $g$~defined by22 \[23 g(x_1, \dots, x_k) = \umin{y}{f(x_1,\dots,x_k, y) = 0}24 \]25 is also !!{lambda definable}.26\end{lem}2728\begin{proof}29 Suppose the lambda term~$F$ $\lambda$-defines the regular30 function $f(\vec x, y)$. To !!{lambda define}~$h$ we use a search31 function and a fixpoint combinator:32 \begin{align*}33 \fn{Search} & \ident \lambd[g][\lambd[f\,\vec{x}\,y][34 \fn{IsZero} (f\, \vec{x}\, y)\, y\, (g\, \vec{x} (\fn{Succ}\, y)]]\\35 H & \ident \lambd[\vec x][(Y \, \fn{Search}) F\, \vec{x}\, \num{0}],36 \end{align*}37 where $Y$ is any fixpoint combinator. Informally speaking,38 $\fn{Search}$ is a self-referencing function: starting with~$y$,39 test whether $f\, \vec x\, y$ is zero: if so, return~$y$, otherwise call40 itself with $\fn{Succ}\, y$. Thus $(Y \, \fn{Search}) F41 \num{n_1}\dots\num{n_k}\,\num{0}$ returns the least~$m$ for which $f(n_1,42 \dots, n_k, m) = 0$.43 44 Specifically, observe that45 \begin{align*}46 (Y \, \fn{Search}) F \num{n_1}47 \dots\num{n_k}\, \num{m} & \red \num{m}48 \intertext{if $f(n_1, \dots,49 n_k, m) = 0$, or}50 & \red (Y \, \fn{Search}) F\, \num{n_1} \dots\num{n_k}\, \num{m+1}51 \intertext{otherwise. Since $f$ is regular, $f(n_1, \dots, n_k, y)52 = 0$ for some $y$, and so}53 (Y \, \fn{Search}) F \num{n_1} \dots\num{n_k}\,\num{0}54 & \red \num{h(n_1, \dots, n_k)}.55 \end{align*}56\end{proof}575859\begin{prop}60 Every general recursive function is !!{lambda definable}.61\end{prop}6263\begin{proof}64 By \olref[prf]{lem:basic}, all basic functions are65 !!{lambda definable}, and by \olref[prf]{lem:comp},66 \olref[prf]{lem:prim}, and \olref{lem:min}, the !!{lambda definable}67 functions are closed under composition, primitive recursion, and68 regular minimization.69\end{proof}7071\end{document}
content/lambda-calculus/lambda-definability/partial-recursive-functions.tex
1% Part: lambda-calculus2% Chapter: lambda-definability3% Section: partial-recursive-functions45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{ldf}{par}10\olsection{Partial Recursive Functions are \usetoken{S}{lambda definable}}1112Partial recursive functions are those obtained from the basic13functions by composition, primitive recursion, and unbounded14minimization. They differ from general recursive function in that the15functions used in unbounded search are not required to be regular.16Not requiring regularity means that functions defined by minimization17may sometimes not be defined. 1819At first glance it might seem that the same methods used to show that20the (total) general recursive functions are all !!{lambda definable} can21be used to prove that all partial recursive functions are22!!{lambda definable}. For instance, the composition of $f$ with $g$ is23!!{lambda define}d by $\lambd[x][F (G x)]$ if $f$ and $g$ are24!!{lambda define}d by terms $F$ and~$G$, respectively. However, when the25functions are partial, this is problematic. When $g(x)$ is undefined,26meaning $G x$ has no normal form. In most cases this means that $F (G27x)$ has no normal forms either, which is what we want. But consider28when $F$ is $\lambd[x][\lambd[y][y]]$, in which case $F (G x)$ does29have a normal form ($\lambd[y][y]$). 3031This problem is not insurmountable, and there are ways to32!!{lambda define} all partial recursive functions in such a way that33undefined values are represented by terms without a normal form.34These ways are, however, somewhat more complicated and less intuitive35than the approach we have taken for general recursive functions. We36record the theorem here without proof:3738\begin{thm}39 All partial recursive functions are !!{lambda definable}.40\end{thm}4142\end{document}43
content/lambda-calculus/lambda-definability/lambda-definable-recursive.tex
1% Part: lambda-calculus2% Chapter: lambda-definability3% Section: lambda-definable-recursive45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{lam}{dfl}{ldr}10\olsection{\usetoken{S}{lambda definable} Functions are Recursive}1112Not only are all partial recursive functions !!{lambda definable}, the13converse is true, too. That is, all !!{lambda definable} functions are14partial recursive.1516\begin{thm}17 \ollabel{thm:lambda-computable} If a partial function $f$ is18 !!{lambda definable}, it is partial recursive.19\end{thm}2021\begin{proof}22 We only sketch the proof. First, we arithmetize $\lambd$-terms,23 i.e., systematially assign G\"odel numbers to $\lambd$-terms, using24 the usual power-of-primes coding of sequences. Then we define a25 partial recursive function $\fn{normalize}(t)$ operating on the26 G\"odel number~$t$ of a lambda term as argument, and which returns27 the G\"odel number of the normal form if it has one, or is undefined28 otherwise. Then define two partial recursive functions29 $\fn{toChurch}$ and $\fn{fromChurch}$ that maps natural numbers to30 and from the G\"odel numbers of the corresponding Church numeral.3132 Using these recursive functions, we can define the function~$f$ as a33 partial recursive function. There is a $\lambd$-term~$F$ that34 !!{lambda define}s~$f$. To compute $f(n_1, \dots, n_k)$, first obtain35 the G\"odel numbers of the corresponding Church numerals using36 $\fn{toChurch}(n_i)$, append these to $\Gn{F}$ to obtain the G\"odel37 number of the term $F \num{n_1}\dots\num{n_k}$. Now use38 $\fn{normalize}$ on this G\"odel number. If $f(n_1, \dots, n_k)$ is39 defined, $F \num{n_1}\dots\num{n_k}$ has a normal form (which must40 be a Church numeral), and otherwise it has no normal form (and so41 \[\fn{normalize}(\Gn{F\num{n_1}\dots\num{n_k}})\] is undefined).42 Finally, use $\fn{fromChurch}$ on the G\"odel number of the43 normalized term.44\end{proof}4546\end{document}