content/computability/recursive-functions/recursive-functions.tex
1% Part: computability2% Chapter: recursive-functions34\documentclass[../../../include/open-logic-chapter]{subfiles}56\begin{document}78\olchapter{cmp}{rec}{Recursive Functions}910\begin{editorial}11 These are Jeremy Avigad's notes on recursive functions, revised and12 expanded by Richard Zach. This chapter does contain some exercises,13 and can be included independently to provide the basis for a14 discussion of arithmetization of syntax.15\end{editorial}16171819\olimport{introduction}2021\olimport{primitive-recursion}2223\olimport{composition}2425\olimport{pr-functions}2627\olimport{notation-pr-functions}2829\olimport{pr-functions-computable}3031\olimport{examples}3233\olimport{pr-relations}3435\olimport{bounded-minimization}3637\olimport{primes}3839\olimport{sequences}4041\olimport{trees}4243\olimport{other-recursions}4445\olimport{non-pr-functions}4647\olimport{partial-functions}4849\olimport{normal-form}5051\olimport{halting-problem}5253\olimport{general-recursive-functions}5455\OLEndChapterHook5657\end{document}
content/computability/recursive-functions/introduction.tex
1% Part: computability2% Chapter: recursive-functions3% Section: introduction45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{int}10\olsection{Introduction}1112In order to develop a mathematical theory of computability, one has to,13first of all, develop a \emph{model} of computability. We now think of14computability as the kind of thing that computers do, and computers15work with symbols. But at the beginning of the development of16theories of computability, the paradigmatic example of computation was17\emph{numerical} computation. Mathematicians were always interested18in number-theoretic functions, i.e., functions $f\colon \Nat^n \to19\Nat$ that can be computed. So it is not surprising that at the20beginning of the theory of computability, it was such functions that21were studied. The most familiar examples of computable numerical22functions, such as addition, multiplication, exponentiation (of23natural numbers) share an interesting feature: they can be defined24\emph{recursively}. It is thus quite natural to attempt a general25definition of \emph{computable function} on the basis of recursive26definitions. Among the many possible ways to define number-theoretic27functions recursively, one particularly simple pattern of definition28here becomes central: so-called \emph{primitive recursion}.2930In addition to computable functions, we might be interested in31computable sets and relations. A set is computable if we can compute32the answer to whether or not a given number is !!a{element} of the33set, and a relation is computable iff we can compute whether or not a34tuple $\tuple{n_1, \dots, n_k}$ is !!a{element} of the relation. By35considering the \emph{characteristic function} of a set or relation,36discussion of computable sets and relations can be subsumed under that37of computable functions. Thus we can define primitive recursive38relations as well, e.g., the relation ``$n$ evenly divides $m$'' is a39primitive recursive relation.4041Primitive recursive functions---those that can be defined using just42primitive recursion---are not, however, the only computable43number-theoretic functions. Many generalizations of primitive44recursion have been considered, but the most powerful and45widely-accepted additional way of computing functions is by unbounded46search. This leads to the definition of \emph{partial recursive47 functions}, and a related definition to \emph{general recursive48 functions}. General recursive functions are computable and total,49and the definition characterizes exactly the partial recursive50functions that happen to be total. Recursive functions can simulate51every other model of computation (Turing machines, lambda calculus,52etc.) and so represent one of the many accepted models of computation.535455\end{document}
content/computability/recursive-functions/primitive-recursion.tex
1% Part: computability2% Chapter: recursive-functions3% Section: primitive-recursion45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{pre}10\olsection{Primitive Recursion}1112A characteristic of the natural numbers is that every natural number13can be reached from~$0$ by applying the successor operation~$+1$14finitely many times---any natural number is either~$0$ or the15successor of \dots{} the successor of~$0$. One way to specify a16function~$h\colon\Nat \to \Nat$ that makes use of this fact is this:17(a)~specify what the value of $h$ is for argument~$0$, and (b)~also18specify how to, given the value of $h(x)$, compute the value of19$h(x+1)$. For (a) tells us directly what $h(0)$ is, so $h$~is defined20for~$0$. Now, using the instruction given by (b) for $x=0$, we can21compute $h(1) = h(0+1)$ from~$h(0)$. Using the same instructions for22$x=1$, we compute $h(2) = h(1+1)$ from~$h(1)$, and so on. For every23natural number~$x$, we'll eventually reach the step where we define24$h(x)$ from $h(x+1)$, and so $h(x)$ is defined for all $x \in \Nat$.2526For instance, suppose we specify $h\colon \Nat \to \Nat$ by the following27two equations:28\begin{align*}29h(0) & = 1\\30h(x+1) & = 2 \cdot h(x)31\end{align*}32If we already know how to multiply, then these equations give us the33information required for (a) and~(b) above. By successively applying34the second equation, we get that35\begin{align*}36 h(1) & = 2\cdot h(0) = 2,\\37 h(2) & = 2\cdot h(1) = 2\cdot 2,\\38 h(3) & = 2 \cdot h(2) = 2\cdot 2 \cdot 2,\\39 & \vdots40\end{align*}41We see that the function~$h$ we have specified is $h(x) = 2^x$.4243The characteristic feature of the natural numbers guarantees that44there is only one function~$h$ that meets these two criteria. A pair45of equations like these is called a \emph{definition by primitive46recursion} of the function~$h$. It is so-called because we define~$h$47``recursively,'' i.e., the definition, specifically the second48equation, involves $h$ itself on the right-hand-side. It is49``primitive'' because in defining $h(x+1)$ we only use the50value~$h(x)$, i.e., the immediately preceding value. This is the51simplest way of defining a function on~$\Nat$ recursively.5253We can define even more fundamental functions like addition and54multiplication by primitive recursion. In these cases, however, the55functions in question are $2$-place. We fix one of the argument56places, and use the other for the recursion. E.g, to define57$\Add(x, y)$ we can fix~$x$ and define the value first for $y=0$58and then for $y+1$ in terms of~$y$. Since $x$ is fixed, it will appear59on the left and on the right side of the defining equations.60\begin{align*}61\Add(x,0) & = x\\62\Add(x,y+1) & = \Add(x,y)+163\end{align*}64These equations specify the value of $\Add$ for all $x$65\emph{and}~$y$. To find $\Add(2,3)$, for instance, we apply the66defining equations for $x = 2$, using the first to find67$\Add(2,0) = 2$, then using the second to successively find68$\Add(2,1) = 2 + 1 = 3$, $\Add(2, 2) = 3 + 1 = 4$, $\Add(2,693) = 4 + 1 = 5$.7071In the definition of $\Add$ we used $+$ on the right-hand-side of72the second equation, but only to add~$1$. In other words, we used the73successor function $\Succ(z) = z+1$ and applied it to the previous value74$\Add(x,y)$ to define $\Add(x,y+1)$. So we can think of the75recursive definition as given in terms of a single function which we76apply to the previous value. However, it doesn't hurt---and sometimes77is necessary---to allow the function to depend not just on the previous78value but also on $x$ and~$y$. Consider:79\begin{align*}80 \Mult(x,0) & = 0 \\81 \Mult(x,y+1) & = \Add(\Mult(x,y),x)82\end{align*}83This is a primitive recursive definition of a function $\Mult$ by84applying the function $\Add$ to both the preceding value85$\Mult(x,y)$ and the first argument~$x$. It also defines the86function~$\Mult(x,y)$ for all arguments $x$ and~$y$. For instance,87$\Mult(2,3)$ is determined by successively computing $\Mult(2,0)$,88$\Mult(2,1)$, $\Mult(2,2)$, and~$\Mult(2,3)$:89\begin{align*}90 \Mult(2,0) & = 0\\91 \Mult(2,1) & = \Mult(2,0+1) =92 \Add(\Mult(2,0), 2) = \Add(0, 2) = 2\\93 \Mult(2,2) & = \Mult(2,1+1) =94 \Add(\Mult(2,1), 2) = \Add(2, 2) = 4\\95 \Mult(2,3) & = \Mult(2,2+1) =96 \Add(\Mult(2,2), 2) = \Add(4, 2) = 697\end{align*}9899The general pattern then is this: to give a primitive recursive100definition of a function~$h(x_0, \dots, x_{k-1}, y)$, we provide two101equations. The first defines the value of $h(x_0, \dots, x_{k-1}, 0)$102without reference to~$h$. The second defines the value of $h(x_0,103\dots, x_{k-1}, y+1)$ in terms of $h(x_0, \dots, x_{k-1}, y)$, the104other arguments $x_0$, \dots,~$x_{k-1}$, and~$y$. Only the immediately105preceding value of~$h$ may be used in that second equation. If we106think of the operations given by the right-hand-sides of these two107equations as themselves being functions $f$ and~$g$, then the general108pattern to define a new function~$h$ by primitive recursion is this:109\begin{align*}110 h(x_0, \dots, x_{k-1}, 0) & = f(x_0, \dots, x_{k-1})\\111 h(x_0, \dots, x_{k-1}, y+1) & =112 g(x_0, \dots, x_{k-1}, y, h(x_0, \dots, x_{k-1}, y))113\end{align*} 114In the case of $\Add$, we have $k=1$ and $f(x_0) = x_0$ (the115identity function), and $g(x_0, y, z) = z + 1$ (the $3$-place function116that returns the successor of its third argument):117\begin{align*}118 \Add(x_0, 0) & = f(x_0) = x_0\\119 \Add(x_0, y+1) & = g(x_0, y, \Add(x_0, y)) =120 \Succ(\Add(x_0, y))121\end{align*}122In the case of $\Mult$, we have $f(x_0) = 0$ (the constant123function always returning~$0$) and $g(x_0, y, z) = \Add(z,x_0)$124(the $3$-place function that returns the sum of its last and first125argument):126\begin{align*}127 \Mult(x_0,0) & = f(x_0) = 0 \\128 \Mult(x_0,y+1) & = g(x_0, y, \Mult(x_0,y)) =129 \Add(\Mult(x_0,y), x_0)130\end{align*}131132\end{document}
content/computability/recursive-functions/composition.tex
1% Part: computability2% Chapter: recursive-functions3% Section: composition45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{com}10\olsection{Composition}1112If $f$ and $g$ are two one-place functions of natural numbers, we can13compose them: $h(x) = f(g(x))$. The new function~$h(x)$ is then14defined by \emph{composition} from the functions $f$ and~$g$. We'd15like to generalize this to functions of more than one argument.1617Here's one way of doing this: suppose $f$ is a $k$-place function,18and $g_0$, \dots, $g_{k-1}$ are $k$ functions which are all19$n$-place. Then we can define a new $n$-place function $h$ as follows:20\[21h(x_0, \dots, x_{n-1}) =22f(g_0(x_0, \dots, x_{n-1}), \dots, g_{k-1}(x_0, \dots, x_{n-1}))23\]24If $f$ and all $g_i$ are computable, so is $h$: To compute $h(x_0,25\dots, x_{n-1})$, first compute the values $y_i = g_i(x_0, \dots,26x_{n-1})$ for each $i = 0$, \dots,~$k-1$. Then feed these values into27$f$ to compute $h(x_0, \dots, x_{k-1}) = f(y_0, \dots, y_{k-1})$.2829This may seem like an overly restrictive characterization of what30happens when we compute a new function using some existing ones. For31one thing, sometimes we do not use all the arguments of a function, as32when we defined $g(x, y, z) = \Succ(z)$ for use in the primitive33recursive definition of~$\Add$. Suppose we are allowed use of the34following functions:35\[36\Proj{n}{i}(x_0, \dots, x_{n-1}) = x_i37\]38The functions~$\Proj{k}{i}$ are called \emph{projection} functions:39$\Proj{n}{i}$ is an $n$-place function. Then $g$ can be defined by40\[41g(x, y, z) = \Succ(\Proj{3}{2}(x, y, z)).42\]43Here the role of $f$ is played by the $1$-place function $\Succ$, so44$k=1$. And we have one $3$-place function $\Proj{3}{2}$ which plays45the role of $g_0$. The result is a $3$-place function that returns the46successor of the third argument.4748The projection functions also allow us to define new functions by49reordering or identifying arguments. For instance, the function $h(x)50= \Add(x, x)$ can be defined by51\[52h(x_0) = \Add(\Proj{1}{0}(x_0),\Proj{1}{0}(x_0)).53\]54Here $k=2$, $n=1$, the role of $f(y_0,y_1)$ is played by $\Add$, and55the roles of $g_0(x_0)$ and $g_1(x_0)$ are both played56by~$\Proj{1}{0}(x_0)$, the one-place projection function (aka the57identity function).5859If $f(y_0, y_1)$ is a function we already have, we can define the60function $h(x_0, x_1) = f(x_1, x_0)$ by61\[62h(x_0, x_1) = f(\Proj{2}{1}(x_0, x_1),\Proj{2}{0}(x_0, x_1)).63\]64Here $k=2$, $n = 2$, and the roles of $g_0$ and $g_1$ are played by65$\Proj{2}{1}$ and~$\Proj{2}{0}$, respectively.6667You may also worry that $g_0$, \dots,~$g_{k-1}$ are all required68to have the same arity~$n$. (Remember that the \emph{arity} of a69function is the number of arguments; an $n$-place function has70arity~$n$.) But adding the projection functions provides the desired71flexibility. For example, suppose $f$ and~$g$ are $3$-place functions72and $h$~is the $2$-place function defined by73\[74h(x,y) = f(x,g(x,x,y),y).75\]76The definition of~$h$ can be rewritten with the projection77functions, as78\[79h(x,y) = f(\Proj{2}{0}(x,y), g(\Proj{2}{0}(x,y), \Proj{2}{0}(x,y),80\Proj{2}{1}(x,y)), \Proj{2}{1}(x,y)).81\]82Then $h$ is the composition of $f$ with $\Proj{2}{0}$, $l$, and83$\Proj{2}{1}$, where84\[85l(x,y) = g(\Proj{2}{0}(x,y),\Proj{2}{0}(x,y),\Proj{2}{1}(x,y)),86\]87i.e., $l$ is the composition of $g$ with $\Proj{2}{0}$, $\Proj{2}{0}$,88and~$\Proj{2}{1}$.8990\end{document}
content/computability/recursive-functions/pr-functions.tex
1% Part: computability2% Chapter: recursive-functions3% Section: pr-functions45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{prf}10\olsection{Primitive Recursion Functions}1112Let us record again how we can define new functions from existing ones13using primitive recursion and composition.1415\begin{defn}16 \ollabel{defn:primitive-recursion} Suppose $f$ is a $k$-place17 function ($k\ge 1$) and $g$ is a $(k+2)$-place function. The18 function defined by \emph{primitive recursion from $f$ and $g$} is19 the $(k+1)$-place function~$h$ defined by the equations20 \begin{align*}21 h(x_0,\dots,x_{k-1},0) & = f(x_0,\dots,x_{k-1}) \\22 h(x_0,\dots,x_{k-1},y+1) & = g(x_0,\dots,x_{k-1}, y, h(x_0,\dots,x_{k-1}, y))23 \end{align*}24\end{defn}2526\begin{defn}27 \ollabel{defn:composition} Suppose $f$ is a $k$-place function, and28 $g_0$, \dots, $g_{k-1}$ are $k$ functions which are all29 $n$-place. The function defined by \emph{composition from $f$ and30 $g_0$, \dots,~$g_{k-1}$} is the $n$-place function~$h$ defined by31 \[32 h(x_0, \dots, x_{n-1}) =33 f(g_0(x_0, \dots, x_{n-1}), \dots, g_{k-1}(x_0, \dots, x_{n-1})).34 \]35\end{defn}3637In addition to $\Succ$ and the projection functions38\[39\Proj{n}{i}(x_0,\dots,x_{n-1}) = x_i,40\]41for each natural number $n$ and $i < n$, we will include among the42primitive recursive functions the function~$\Zero(x) = 0$.4344\begin{defn}45 The set of primitive recursive functions is the set of functions 46 from $\Nat^n$ to $\Nat$, defined inductively by the47 following clauses:48 \begin{enumerate}49 \item $\Zero$ is primitive recursive.50 \item $\Succ$ is primitive recursive.51 \item Each projection function $\Proj{n}{i}$ is primitive recursive.52 \item If $f$ is a $k$-place primitive recursive function and $g_0$,53 \dots,~$g_{k-1}$ are $n$-place primitive recursive functions, then54 the composition of $f$ with $g_0$, \dots,~$g_{k-1}$ is primitive55 recursive.56 \item If $f$ is a $k$-place primitive recursive function and $g$ is57 a $k+2$-place primitive recursive function, then the function58 defined by primitive recursion from $f$ and $g$ is primitive59 recursive.60\end{enumerate}61\end{defn}6263\begin{explain}64Put more concisely, the set of primitive recursive functions is the65smallest set containing $\Zero$, $\Succ$, and the projection66functions~$\Proj{n}{j}$, and which is closed under composition and67primitive recursion.6869Another way of describing the set of primitive recursive functions is70by defining it in terms of ``stages.'' Let $S_0$ denote the set of71starting functions: $\Zero$, $\Succ$, and the projections. These are72the primitive recursive functions of stage~$0$. Once a stage $S_i$ has73been defined, let $S_{i+1}$ be the set of all functions you get by74applying a single instance of composition or primitive recursion to75functions already in~$S_i$. Then76\[77S = \bigcup_{i \in \Nat} S_i78\]79is the set of all primitive recursive functions80\end{explain}8182Let us verify that $\Add$ is a primitive recursive function.8384\begin{prop}85 The addition function $\Add(x,y) = x+y$ is primitive recursive.86\end{prop}8788\begin{proof}89We already have a primitive recursive definition of $\Add$ in terms of90two functions $f$ and~$g$ which matches the format of91\olref{defn:primitive-recursion}:92\begin{align*}93 \Add(x_0, 0) & = f(x_0) = x_0\\94 \Add(x_0, y+1) & = g(x_0, y, \Add(x_0, y)) =95 \Succ(\Add(x_0, y))96\end{align*}97So $\Add$ is primitive recursive provided $f$ and $g$ are as98well. $f(x_0) = x_0 = \Proj{1}{0}(x_0)$, and the projection functions99count as primitive recursive, so $f$ is primitive recursive. The100function $g$ is the three-place function $g(x_0, y, z)$ defined by101\[102g(x_0, y, z) = \Succ(z).103\]104This does not yet tell us that $g$ is primitive recursive, since $g$105and $\Succ$ are not quite the same function: $\Succ$ is one-place, and106$g$ has to be three-place. But we can define $g$ ``officially'' by107composition as108\[109g(x_0, y, z) = \Succ(\Proj{3}{2}(x_0, y, z))110\]111Since $\Succ$ and $\Proj{3}{2}$ count as primitive recursive112functions, $g$ does as well, since it can be defined by composition113from primitive recursive functions.114\end{proof}115116\begin{prop}117 \ollabel{prop:mult-pr}118 The multiplication function $\Mult(x,y) = x \cdot y$ is primitive recursive.119\end{prop}120121\begin{proof}122 Exercise.123\end{proof}124125\begin{prob}126 Prove \olref[cmp][rec][prf]{prop:mult-pr} by showing that the127 primitive recursive definition of $\Mult$ can be put into the128 form required by \olref[cmp][rec][prf]{defn:primitive-recursion} and129 showing that the corresponding functions $f$ and~$g$ are primitive130 recursive.131\end{prob}132133\begin{ex}134Here's our very first example of a primitive recursive definition:135\begin{align*}136h(0) & = 1 \\137h(y+1) & = 2 \cdot h(y).138\end{align*}139This function cannot fit into the form required by140\olref{defn:primitive-recursion}, since $k=0$. The definition also141involves the constants $1$ and $2$. To get around the first problem,142let's introduce a dummy argument and define the function~$h'$:143\begin{align*}144h'(x_0, 0) & = f(x_0) = 1 \\145h'(x_0, y+1) & = g(x_0, y, h'(x_0, y)) = 2 \cdot h'(x_0, y).146\end{align*}147The function $f(x_0) = 1$ can be defined from $\Succ$ and $\Zero$ by148composition: $f(x_0) = \Succ(\Zero(x_0))$. The function $g$ can be149defined by composition from $g'(z) = 2 \cdot z$ and projections:150\begin{align*}151g(x_0, y, z) & = g'(\Proj{3}{2}(x_0, y, z))152\intertext{and $g'$ in turn can be defined by composition as}153g'(z) & = \Mult(g''(z), \Proj{1}{0}(z))154\intertext{and}155g''(z) & = \Succ(f(z)),156\end{align*}157where $f$ is as above: $f(z) = \Succ(\Zero(z))$. Now that we158have~$h'$, we can use composition again to let $h(y) =159h'(\Proj{1}{0}(y),\Proj{1}{0}(y))$. This shows that $h$ can be defined160from the basic functions using a sequence of compositions and161primitive recursions, so $h$~is primitive recursive.162\end{ex}163164\end{document}
content/computability/recursive-functions/notation-pr-functions.tex
1% Part: computability2% Chapter: recursive-functions3% Section: notations-pr-functions45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{not}10\olsection{Primitive Recursion Notations}1112One advantage to having the precise inductive description of the primitive13recursive functions is that we can be systematic in describing them.14For example, we can assign a ``notation'' to each such function, as15follows. Use symbols $\Zero$, $\Succ$, and $\Proj{n}{i}$ for zero,16successor, and the projections. Now suppose $h$ is defined by17composition from a $k$-place function~$f$ and $n$-place functions $g_0$,18\dots,~$g_{k-1}$, and we have assigned notations $F$, $G_0$,19\dots,~$G_{k-1}$ to the latter functions. Then, using a new symbol20$\fn{Comp}_{k,n}$, we can denote the function $h$ by21$\fn{Comp}_{k,n}[F,G_0,\dots,G_{k-1}]$. 2223For functions defined by primitive recursion, we can use analogous24notations. Suppose the $(k+1)$-ary function~$h$ is defined by25primitive recursion from the $k$-ary function~$f$ and the $(k+2)$-ary26function~$g$, and the notations assigned to $f$ and~$g$ are $F$27and~$G$, respectively. Then the notation assigned to~$h$ is28$\fn{Rec}_k[F,G]$. 2930Recall that the addition function is defined by primitive recursion as31\begin{align*}32 \Add(x_0, 0) & = \Proj{1}{0}(x_0) = x_0\\33 \Add(x_0, y+1) & = \Succ(\Proj{3}{2}(x_0, y, \Add(x_0, y))) = \Add(x_0, y) +134\end{align*}35Here the role of~$f$ is played by $\Proj{1}{0}$, and the role of~$g$36is played by $\Succ(\Proj{3}{2}(x_0, y, z))$, which is assigned the37notation $\fn{Comp}_{1,3}[\Succ,\Proj{3}{2}]$ as it is the result of38defining a function by composition from the $1$-ary function~$\Succ$39and the $3$-ary function~$\Proj{3}{2}$. With this setup, we can denote40the addition function by41\[42\fn{Rec}_1[\Proj{1}{0},\fn{Comp}_{1,3}[\Succ,\Proj{3}{2}]].43\]44Having these notations sometimes proves useful, e.g., when enumerating45primitive recursive functions.4647\begin{prob}48 Give the complete primitive recursive notation for~$\Mult$.49\end{prob}5051\end{document}
content/computability/recursive-functions/pr-functions-computable.tex
1% Part: computability2% Chapter: recursive-functions3% Section: pr-functions-computable45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{cmp}10\olsection{Primitive Recursive Functions are Computable}1112Suppose a function $h$ is defined by primitive recursion13\begin{eqnarray*}14h(\vec x, 0) & = & f(\vec x) \\15h(\vec x, y + 1) & = & g(\vec x, y, h(\vec x, y))16\end{eqnarray*}17and suppose the functions $f$ and $g$ are computable. (We use $\vec x$ to abbreviate $x_0$, \dots, $x_{k-1}$.) Then $h(\vec18x, 0)$ can obviously be computed, since it is just $f(\vec x)$ which we19assume is computable. $h(\vec x, 1)$ can then also be computed, since20$1 = 0 + 1$ and so $h(\vec x, 1)$ is just21\begin{align*}22 h(\vec x, 1) & = g(\vec x, 0, h(\vec x, 0)) = g(\vec x, 0, f(\vec x)).23\intertext{We can go on in this way and compute}24h(\vec x, 2) & = g(\vec x, 1, h(\vec x, 1)) = g(\vec x, 1, g(\vec x, 0, f(\vec x)))\\25h(\vec x, 3) & = g(\vec x, 2, h(\vec x, 2)) = g(\vec x, 2, g(\vec x, 1, g(\vec x, 0, f(\vec x))))\\26h(\vec x, 4) & = g(\vec x, 3, h(\vec x, 3)) = g(\vec x, 3, g(\vec x, 2, g(\vec x, 1, g(\vec x, 0, f(\vec x)))))\\27& \vdots28\end{align*}29Thus, to compute $h(\vec x, y)$ in general, successively compute30$h(\vec x, 0)$, $h(\vec x, 1)$, \dots, until we reach $h(\vec x, y)$.3132Thus, a primitive recursive definition yields a new computable33function if the functions $f$ and $g$ are computable. Composition of34functions also results in a computable function if the functions $f$35and $g_i$ are computable.3637Since the basic functions $\Zero$, $\Succ$, and $\Proj{n}{i}$ are38computable, and composition and primitive recursion yield computable39functions from computable functions, this means that every primitive40recursive function is computable.4142\end{document}
content/computability/recursive-functions/examples.tex
1% Part: computability2% Chapter: recursive-functions3% Section: examples45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{exa}10\olsection{Examples of Primitive Recursive Functions}111213We already have some examples of primitive recursive functions: the14addition and multiplication functions~$\Add$ and $\Mult$. The15identity function $\fn{id}(x) = x$ is primitive recursive, since it is16just~$\Proj{1}{0}$. The constant functions $\fn{const}_n(x) = n$ are17primitive recursive since they can be defined from $\Zero$ and $\Succ$18by successive composition. This is useful when we want to use19constants in primitive recursive definitions, e.g., if we want to20define the function $f(x) = 2 \cdot x$ can obtain it by composition21from $\fn{const}_n(x)$ and multiplication as $f(x) =22\Mult(\fn{const}_2(x), \Proj{1}{0}(x))$. We'll make use of this trick23from now on.2425\begin{prop}26 The exponentiation function $\fn{exp}(x, y) = x^y$ is primitive recursive.27\end{prop}2829\begin{proof}30 We can define $\fn{exp}$ primitive recursively as31 \begin{align*}32 \fn{exp}(x, 0) & = 1\\33 \fn{exp}(x, y+1) & = \Mult(x, \fn{exp}(x,y)).34 \intertext{Strictly speaking, this is not a recursive definition35 from primitive recursive functions. Officially, though, we36 have:}37 \fn{exp}(x, 0) & = f(x)\\38 \fn{exp}(x, y+1) & = g(x, y, \fn{exp}(x,y)).39 \intertext{where}40 f(x) & = \Succ(\Zero(x)) = 1\\41 g(x, y, z) & = \Mult(\Proj{3}{0}(x, y, z), \Proj{3}{2}(x, y, z)) = x \cdot z42 \end{align*}43 and so $f$ and $g$ are defined from primitive recursive functions by44 composition.45\end{proof}4647\begin{prop}48 The predecessor function $\fn{pred}(y)$ defined by49 \[50 \fn{pred}(y) = \begin{cases}51 0 & \text{if $y=0$}\\52 y-1 & \text{otherwise}53 \end{cases}54 \]55 is primitive recursive.56\end{prop}5758\begin{proof}59 Note that 60 \begin{align*}61 \fn{pred}(0) & = 0 \text{ and}\\62 \fn{pred}(y+1) & = y.63 \end{align*}64 This is almost a primitive recursive definition. It does not,65 strictly speaking, fit into the pattern of definition by primitive66 recursion, since that pattern requires at least one extra67 argument~$x$. It is also odd in that it does not actually use68 $\fn{pred}(y)$ in the definition of $\fn{pred}(y+1)$. But we can69 first define $\fn{pred}'(x, y)$ by70 \begin{align*}71 \fn{pred}'(x, 0) & = \Zero(x) = 0,\\72 \fn{pred}'(x, y+1) & = \Proj{3}{1}(x, y, \fn{pred'}(x, y)) = y.73 \end{align*}74and then define $\fn{pred}$ from it by composition, e.g., as75$\fn{pred}(x) = \fn{pred}'(\Zero(x), \Proj{1}{0}(x))$.76\end{proof}7778\begin{prop}79 The factorial function $\fn{fac}(x) = \fact{x} = 1 \cdot 2 \cdot 380 \cdot \dots \cdot x$ is primitive recursive.81\end{prop}8283\begin{proof}84 The obvious primitive recursive definition is85 \begin{align*}86 \fn{fac}(0) &= 1\\87 \fn{fac}(y+1) & = \fn{fac}(y) \cdot (y+1).88 \intertext{Officially, we have to first define a two-place function $h$}89 h(x, 0) & = \fn{const}_1(x)\\90 h(x, y+1) & = g(x, y, h(x, y))91 \intertext{where $g(x, y, z) = \Mult(\Proj{3}{2}(x, y, z),92 \Succ(\Proj{3}{1}(x, y, z)))$ and then let}93 \fn{fac}(y) & = h(\Proj{1}{0}(y), \Proj{1}{0}(y)) = h(y,y).94 \end{align*}95 From now on we'll be a bit more laissez-faire and not give the official96 definitions by composition and primitive recursion.97\end{proof}9899\begin{prop}100 Truncated subtraction, $x \tsub y$, defined by101 \[102 x \tsub y = \begin{cases}103 0 & \text{if $x < y$}\\104 x-y & \text{otherwise}105 \end{cases}106 \]107 is primitive recursive.108\end{prop}109110\begin{proof}111 We have:112 \begin{align*}113 x \tsub 0 & = x\\114 x \tsub (y+1) & = \fn{pred}(x \tsub y) 115 \end{align*}116\end{proof}117118\begin{prop}119 The distance between $x$ and $y$, $\left|x-y\right|$, is primitive recursive.120\end{prop}121122\begin{proof}123 We have $\left| x-y \right| = (x \tsub y) + (y \tsub x)$, so124 the distance can be defined by composition from $+$ and $\tsub$,125 which are primitive recursive.126\end{proof}127128\begin{prop}129 The maximum of $x$ and $y$, $\fn{max}(x,y)$, is primitive recursive.130\end{prop}131132\begin{proof}133 We can define $\fn{max}(x,y)$ by composition from $+$ and $\tsub$ by134 \[135 \fn{max}(x,y) \defis x + (y \tsub x).136 \]137 If $x$ is the maximum, i.e., $x \ge y$, then $y \tsub x = 0$, so $x138 + (y \tsub x) = x + 0 = x$. If $y$ is the maximum, then $y \tsub x =139 y - x$, and so $x + (y \tsub x) = x + (y - x) = y$.140\end{proof}141142\begin{prop}143 \ollabel{prop:min-pr}144 The minimum of $x$ and $y$, $\fn{min}(x,y)$, is primitive recursive.145\end{prop}146147\begin{proof}148 Exercise.149\end{proof}150151\begin{prob}152 Prove \olref[cmp][rec][exa]{prop:min-pr}.153\end{prob}154155\begin{prob}156Show that \[157f(x, y) =1582^{(2^{\iddots^{2^{x}}})}\raisebox{1ex}{\bigg\rbrace}159\raisebox{1ex}{\text {$y$ $2$'s}}\] is primitive recursive.160\end{prob}161162\begin{prob}163Show that integer division $d(x, y) = \lfloor x/y \rfloor$ (i.e.,164division, where you disregard everything after the decimal point) is165primitive recursive. When $y = 0$, we stipulate $d(x, y) = 0$. Give an166explicit definition of~$d$ using primitive recursion and167composition.168\end{prob}169170\begin{prop}171The set of primitive recursive functions is closed under the172following two operations:173\begin{enumerate}174\item Finite sums: if $f(\vec x, z)$ is primitive recursive, then so175is the function176\[177g(\vec x, y) \defis \sum_{z = 0}^y f(\vec x, z).178\]179\item Finite products: if $f(\vec x, z)$ is primitive recursive, then180so is the function181\[182h(\vec x, y) \defis \prod_{z = 0}^y f(\vec x, z).183\]184\end{enumerate}185\end{prop}186187\begin{proof}188For example, finite sums are defined recursively by the equations189\begin{align*}190 g(\vec x, 0) & = f(\vec x, 0)\\191 g(\vec x, y+1) & = g(\vec x, y) + f(\vec x, y+1).192\end{align*}193\end{proof}194195196\end{document}
content/computability/recursive-functions/pr-relations.tex
1% Part: computability2% Chapter: recursive-functions3% Section: pr-relations45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{prr}10\olsection{Primitive Recursive Relations}111213\begin{defn}14A relation $R(\vec x)$ is said to be primitive recursive if its characteristic15function,16\[17\Char{R}(\vec x) = \left\{18 \begin{array}{ll}19 1 & \mbox{if $R(\vec x)$} \\20 0 & \mbox{otherwise}21 \end{array}22\right.23\]24is primitive recursive.25\end{defn}2627In other words, when one speaks of a primitive recursive relation28$R(\vec x)$, one is referring to a relation of the form $\Char{R}(\vec29x) = 1$, where $\Char{R}$ is a primitive recursive function which, on30any input, returns either 1 or 0. For example, the relation31$\fn{IsZero}(x)$, which holds if and only if $x = 0$, corresponds to the32function $\Char{\fn{IsZero}}$, defined using primitive recursion by33\begin{align*}34\Char{\fn{IsZero}}(0) & = 1,\\35\Char{\fn{IsZero}}(x+1) & = 0.36\end{align*}3738It should be clear that one can compose relations with other primitive39recursive functions. So the following are also primitive recursive:40\begin{enumerate}41\item The equality relation, $x = y$, defined by $\fn{IsZero}(\left|x -42 y\right|)$43\item The less-than relation, $x \leq y$, defined by $\fn{IsZero}(x44 \tsub y)$45\end{enumerate}464748\begin{prop}49 The set of primitive recursive relations is closed under Boolean50 operations, that is, 51 if $P(\vec x)$ and $Q(\vec x)$ are primitive recursive, so are52 \begin{enumerate}53 \item $\lnot P(\vec x)$54 \item $P(\vec x) \land Q(\vec x)$55 \item $P(\vec x) \lor Q(\vec x)$56 \item $P(\vec x) \lif Q(\vec x)$57 \end{enumerate}58\end{prop}5960\begin{proof}61 Suppose $P(\vec x)$ and $Q(\vec x)$ are primitive recursive, i.e.,62 their characteristic functions $\Char{P}$ and $\Char{Q}$ are. We63 have to show that the characteristic functions of $\lnot P(\vec x)$,64 etc., are also primitive recursive.65 \[66 \Char{\lnot P}(\vec x) = \begin{cases}67 0 & \text{if $\Char{P}(\vec x) = 1$}\\68 1 & \text{otherwise}69 \end{cases}70 \]71 We can define $\Char{\lnot P}(\vec x)$ as $1 \tsub \Char{P}(\vec x)$.72 \[73 \Char{P \land Q}(\vec x) = \begin{cases}74 1 & \text{if $\Char{P}(\vec x) = \Char{Q}(\vec x) = 1$}\\75 0 & \text{otherwise}76 \end{cases}77 \]78 We can define $\Char{P \land Q}(\vec x)$ as $\Char{P}(\vec x) \cdot79 \Char{Q}(\vec x)$ or as $\fn{min}(\Char{P}(\vec x), \Char{Q}(\vec80 x))$. Similarly, 81 \begin{align*}82 \Char{P \lor Q}(\vec x) & = \fn{max}(\Char{P}(\vec x), \Char{Q}(\vec x)) \text{ and}\\83 \Char{P \lif Q}(\vec x) & = \fn{max}(1 \tsub84 \Char{P}(\vec x), \Char{Q}(\vec x)).85 \end{align*}86\end{proof}8788\begin{prop}89 The set of primitive recursive relations is closed under bounded90 quantification, i.e., if $R(\vec x, z)$ is a primitive recursive91 relation, then so are the relations92 \begin{align*}93 & \bforall{z < y}{R(\vec x, z)} \text{ and}\\94 & \bexists{z < y}{R(\vec x, z)}.95 \end{align*}96 $\bforall{z < y}{R(\vec x, z)}$ holds of $\vec x$ and $y$ if and97 only if $R(\vec x, z)$ holds for every~$z$ less than~$y$, and98 similarly for $\bexists{z < y}{R(\vec x, z)}$.99\end{prop}100101\begin{proof}102 By convention, we take $\bforall{z < 0}{R(\vec x, z)}$ to be true103 (for the trivial reason that there are no $z$ less than~$0$) and104 $\bexists{z < 0}{R(\vec x, z)}$ to be false. A bounded105 universal quantifier functions just like a finite product or106 iterated minimum, i.e., if $P(\vec x, y) \defiff \bforall{z <107 y}{R(\vec x, z)}$ then $\Char{P}(\vec x, y)$ can be defined by108 \begin{align*}109 \Char{P}(\vec x, 0) & = 1\\110 \Char{P}(\vec x, y+1) & =111 \fn{min}(\Char{P}(\vec x, y), \Char{R}(\vec x, y)).112 \end{align*}113 Bounded existential quantification can similarly be defined using114 $\fn{max}$. Alternatively, it can be defined from bounded universal115 quantification, using the equivalence $\bexists{z < y}{R(\vec x, z)}116 \liff \lnot \bforall{z < y}{\lnot R(\vec x, z)}$. Note that, for117 example, a bounded quantifier of the form $\bexists{x \leq y}{\dots118 x\dots}$ is equivalent to $\bexists{x < y+1}{\dots x \dots}$.119\end{proof}120121\begin{prob}122 Show that the three place relation $x \equiv y \mod n$ (congruence123 modulo~$n$) is primitive recursive.124\end{prob}125126Another useful primitive recursive function is the conditional127function, $\fn{cond}(x,y,z)$, defined by128\begin{align*}129 \fn{cond}(x,y,z) & = \begin{cases}130 y & \text{if $x = 0$} \\131 z & \text{otherwise}.132\end{cases}133\intertext{This is defined recursively by}134\fn{cond}(0,y,z) & = y,\\135\fn{cond}(x+1,y,z) & = z.136\end{align*}137One can use this to justify definitions of primitive recursive functions138by cases from primitive recursive relations:139140\begin{prop}141If $g_0(\vec x)$, \dots,~$g_m(\vec x)$ are primitive recursive functions, and $R_0(\vec142x)$, \dots, $R_{m-1}(\vec x)$ are primitive recursive relations, then143the function $f$ defined by144\[145f(\vec x) = \begin{cases}146 g_0(\vec x) & \text{if $R_0(\vec{x})$} \\147 g_1(\vec x) & \text{if $R_1(\vec{x})$ and not $R_0(\vec{x})$} \\148 \vdots & \\149 g_{m-1}(\vec x) & \text{if $R_{m-1}(\vec{x})$ and none of the150 previous hold}151 \\152 g_m(\vec x) & \mbox{otherwise}153\end{cases}154\]155is also primitive recursive.156\end{prop}157158\begin{proof}159 When $m = 1$, this is just the function defined by160 \[161 f(\vec x) = \fn{cond}(\Char{\lnot R_0}(\vec x),g_0(\vec x),g_1(\vec162 x)).163 \]164 For $m$ greater than $1$, one can just compose definitions of this165 form.166\end{proof}167\end{document}
content/computability/recursive-functions/bounded-minimization.tex
1% Part: computability2% Chapter: recursive-functions3% Section: bounded-minimization45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{bmi}10\olsection{Bounded Minimization}1112\begin{explain}13It is often useful to define a function as the least number satisfying14some property or relation~$P$. If $P$ is decidable, we can compute15this function simply by trying out all the possible numbers, $0$, $1$,16$2$, \dots, until we find the least one satisfying~$P$. This kind of17unbounded search takes us out of the realm of primitive recursive18functions. However, if we're only interested in the least number19\emph{less than some independently given bound}, we stay primitive20recursive. In other words, and a bit more generally, suppose we have a21primitive recursive relation~$R(x,z)$. Consider the function that maps22$x$ and~$y$ to the least $z < y$ such that $R(x, z)$. It, too, can be23computed, by testing whether $R(x, 0)$, $R(x, 1)$, \dots, $R(x, y-1)$.24But why is it primitive recursive?25\end{explain}2627\begin{prop}28If $R(\vec x, z)$ is primitive recursive, so is the function29$m_R(\vec{x}, y)$ which returns the least~$z$ less than~$y$ such that30$R(\vec x, z)$ holds, if there is one, and $y$ otherwise. We will31write the function~$m_R$ as32\[33\bmin{z < y}{R(\vec{x}, z)},34\]35\end{prop}3637\begin{proof}38Note than there can be no~$z < 0$ such that $R(\vec{x}, z)$ since39there is no $z < 0$ at all. So $m_R(\vec x, 0) = 0$.4041In case the bound is of the form $y + 1$ we have three cases:42\begin{enumerate}43\item There is a $z < y$ such that $R(\vec{x}, z)$, in which case44$m_R(\vec{x}, y+1) = m_R(\vec{x}, y)$.45\item There is no such~$z<y$ but $R(\vec{x}, y)$ holds, then46$m_R(\vec{x}, y+1) = y$.47\item There is no $z < y+1$ such that $R(\vec{x}, z)$, then48$m_R(\vec{z}, y+1) = y+1$.49\end{enumerate}50So we can define $m_R(\vec x, 0)$ by primitive recursion as follows:51\begin{align*}52m_R(\vec{x}, 0) & = 0\\53m_R(\vec{x}, y+1) & =54\begin{cases}55m_R(\vec{x}, y) & \text{if $m_R(\vec x, y) \neq y$}\\56y & \text{if $m_R(\vec x, y) = y$ and $R(\vec{x}, y)$}\\57y+1 & \text{otherwise.}58\end{cases}59\end{align*}60Note that there is a $z<y$ such that $R(\vec x, z)$ iff $m_R(\vec x,61y) \neq y$.62\end{proof}6364\begin{prob}65Suppose $R(\vec x, z)$ is primitive recursive. Define the function66$m'_R(\vec{x}, y)$ which returns the least~$z$ less than~$y$ such that67$R(\vec{x}, z)$ holds, if there is one, and $0$ otherwise, by68primitive recursion from~$\Char{R}$.69\end{prob}7071\end{document}
content/computability/recursive-functions/primes.tex
1% Part: computability2% Chapter: recursive-functions3% Section: primes45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{pri}10\olsection{Primes}1112Bounded quantification and bounded minimization provide us with a good13deal of machinery to show that natural functions and relations are14primitive recursive. For example, consider the relation ``$x$15divides $y$'', written $x \mid y$. The relation $x \mid y$ holds if16division of $y$ by~$x$ is possible without remainder, i.e., if $y$ is17an integer multiple of~$x$. (If it doesn't hold, i.e., the remainder18when dividing $x$ by $y$ is $> 0$, we write $x \nmid y$.) In other19words, $x \mid y$ iff for some~$z$, $x \cdot z = y$. Obviously, any20such $z$, if it exists, must be $\leq y$. So, we have that $x \mid y$21iff for some $z \le y$, $x \cdot z = y$. We can define the relation22$x \mid y$ by bounded existential quantification from $=$ and23multiplication by24\[25x \mid y \defiff \bexists{z \leq y}{(x \cdot z) = y}.26\]27We've thus shown that $x \mid y$ is primitive recursive.2829A natural number~$x$ is \emph{prime} if it is neither $0$ nor $1$ and30is only divisible by $1$ and itself. In other words, prime numbers are31such that, whenever $y \mid x$, either $y = 1$ or~$y=x$. To test if32$x$~is prime, we only have to check if $y \mid x$ for all $y \le x$,33since if $y > x$, then automatically~$y \nmid x$. So, the relation34$\fn{Prime}(x)$, which holds iff $x$ is prime, can be defined by35\[36\fn{Prime}(x) \defiff x \geq 2 \land \bforall{y \leq x}{(y \mid x \lif y37 = 1 \lor y = x)}38\]39and is thus primitive recursive.4041The primes are $2$, $3$, $5$, $7$, $11$, etc. Consider the function42$p(x)$ which returns the $x$th prime in that sequence, i.e., $p(0) =432$, $p(1) = 3$, $p(2) = 5$, etc. (For convenience we will often write44$p(x)$ as $p_x$ ($p_0=2$, $p_1=3$, etc.)4546If we had a function47$\fn{nextPrime(x)}$, which returns the first prime number larger48than~$x$, $p$~can be easily defined using primitive recursion:49\begin{align*}50 p(0) & = 2\\51 p(x+1) & = \fn{nextPrime}(p(x))52\end{align*}53Since $\fn{nextPrime}(x)$ is the least $y$ such that $y > x$ and54$y$~is prime, it can be easily computed by unbounded search. But it55can also be defined by bounded minimization, thanks to a result due to56Euclid: there is always a prime number between $x$ and $\fact{x}+1$.57\[58 \fn{nextPrime(x)} =59 \bmin{y \leq \fact{x}+1}{(y > x \land \fn{Prime}(y))}.60\]61This shows, that $\fn{nextPrime}(x)$ and hence $p(x)$ are (not just62computable but) primitive recursive.6364(If you're curious, here's a quick proof of Euclid's theorem. Suppose65$p_n$ is the largest prime $\le x$ and consider the product $p =66p_0\cdot p_1 \cdot \dots \cdot p_n$ of all primes~$\le x$. Either67$p+1$ is prime or there is a prime between $x$ and~$p+1$. Why?68Suppose $p+1$ is not prime. Then some prime number $q \mid p+1$ where69$q < p+1$. None of the primes $\le x$ divide $p+1$. (By definition70of~$p$, each of the primes $p_i \le x$ divides~$p$, i.e., with71remainder~$0$. So, each of the primes $p_i \le x$ divides $p+1$ with72remainder~$1$, and so $p_i \nmid p+1$.) Hence, $q$ is a prime $>73x$ and $< p+1$. And $p \le \fact{x}$, so there is a prime $> x$ and $\le74\fact{x}+1$.)7576\begin{prob}77Define integer division $d(x, y)$ using bounded minimization.78\end{prob}7980\end{document}
content/computability/recursive-functions/sequences.tex
1% Part: computability2% Chapter: recursive-functions3% Section: sequences45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{seq}10\olsection{Sequences}1112The set of primitive recursive functions is remarkably robust. But we13will be able to do even more once we have developed a adequate means14of handling \emph{sequences}. We will identify finite sequences of15natural numbers with natural numbers in the following way: the16sequence $\langle a_0, a_1, a_2, \dots, a_k \rangle$ corresponds to17the number18\[19p_0^{a_0+1} \cdot p_1^{a_1+1} \cdot p_2^{a_2+1} \cdot \dots \cdot20p_k^{a_k+1}.21\]22We add one to the exponents to guarantee that, for example, the23sequences $\langle 2, 7, 3\rangle$ and $\langle 2, 7, 3, 0, 0 \rangle$24have distinct numeric codes. We can take both $0$ and~$1$ to code the25empty sequence; for concreteness, let $\emptyseq$ denote~$0$.2627The reason that this coding of sequences works is the so-called28Fundamental Theorem of Arithmetic: every natural number $n \ge 2$ can29be written in one and only one way in the form30\[31n = p_0^{a_0} \cdot p_1^{a_1} \cdot \dots \cdot p_k^{a_k}32\]33with $a_k \ge 1$. This guarantees that the mapping $\tuple{}(a_0,34\dots, a_k) = \tuple{a_0, \dots, a_k}$ is injective: different35sequences are mapped to different numbers; to each number only at most36one sequence corresponds.3738We'll now show that the operations of determining the length of a39sequence, determining its $i$th element, appending an element to a40sequence, and concatenating two sequences, are all primitive41recursive.4243\begin{prop}44 The function $\len{s}$, which returns the length of the sequence45 $s$, is primitive recursive.46\end{prop}4748\begin{proof}49 Let $R(i, s)$ be the relation defined by50 \[51 R(i, s) \text{ iff }52 p_i \mid s \land p_{i+1} \nmid s.53 \]54 $R$ is clearly primitive recursive. Whenever $s$ is the code of a55 non-empty sequence, i.e.,56 \[57 s = p_0^{a_0+1} \cdot \dots \cdot p_{k}^{a_{k}+1},58 \]59 $R(i,s)$ holds if $p_i$ is the largest prime such that60 $p_i \mid s$, i.e., $i = k$. The length of $s$ thus is $i+1$ iff61 $p_i$ is the largest prime that divides~$s$, so we can let62 \[63 \len{s} =64 \begin{cases}65 0 & \text{if $s = 0$ or $s = 1$} \\66 1 + \bmin{i < s}{R(i, s)} & \text{otherwise}67 \end{cases}68 \]69 We can use bounded minimization here, since there is only one $i$ that70 satisfies $R(i,s)$ when $s$~is a code of a sequence, and if $i$71 exists it is less than~$s$ itself.72\end{proof}7374\begin{prop}75 The function $\fn{append}(s,a)$, which returns the result of appending $a$ to76 the sequence $s$, is primitive recursive.77\end{prop}7879\begin{proof}80 $\fn{append}$ can be defined by:81 \[82 \fn{append}(s,a) =83 \begin{cases}84 2^{a+1} & \text{if $s = 0$ or $s = 1$} \\85 s \cdot p_{\len{s}}^{a+1} & \text{otherwise.}86 \end{cases}87 \]88\end{proof}8990\begin{prop}91 The function $\fn{element}(s,i)$, which returns the $i$th element of $s$92 (where the initial element is called the $0$th), or $0$ if $i$ is93 greater than or equal to the length of $s$, is primitive recursive.94\end{prop}9596\begin{proof}97Note that $a$ is the $i$th element of~$s$ iff $p_i^{a+1}$ is98the largest power of~$p_i$ that divides~$s$, i.e., $p_i^{a+1} \mid s$99but $p_i^{a+2} \nmid s$. So:100 \[101 \fn{element}(s,i) =102 \begin{cases}103 0 & \mbox{if $i \geq \len{s}$} \\104 \bmin{a < s}{(p_i^{a+2} \nmid s)} & \text{otherwise.}105 \end{cases}106 \]107\end{proof}108109Instead of using the official names for the functions defined above,110we introduce a more compact notation. We will use $(s)_i$ instead of111$\fn{element}(s,i)$, and $\tuple{s_0, \dots, s_k}$ to abbreviate112\[113\fn{append}(\fn{append}(\dots \fn{append}(\emptyseq,s_0)114\dots),s_k).115\]116Note that if $s$ has length~$k$, the elements of $s$ are117$(s)_0$, \dots,~$(s)_{k-1}$.118119\begin{prop}120The function $\fn{concat}(s,t)$, which concatenates two121sequences, is primitive recursive.122\end{prop}123124\begin{proof}125 We want a function $\fn{concat}$ with the property that126 \[127 \fn{concat}(\tuple{a_0, \dots, a_k}, \tuple{b_0, \dots, b_l}) =128 \tuple{a_0, \dots, a_k, b_0, \dots, b_l}.129 \]130 We'll use a ``helper'' function131 $\fn{hconcat}(s,t,n)$ which concatenates the first $n$ symbols of $t$132 to~$s$. This function can be defined by primitive recursion as133 follows:134 \begin{align*}135 \fn{hconcat}(s,t,0) & = s\\136 \fn{hconcat}(s,t,n+1) & = \fn{append}(\fn{hconcat}(s,t,n),(t)_n)137 \intertext{Then we can define $\fn{concat}$ by}138 \fn{concat}(s,t) & = \fn{hconcat}(s,t,\len{t}).139 \end{align*}140\end{proof}141142We will write $s \concat t$ instead of $\fn{concat}(s,t)$.143144It will be useful for us to be able to bound the numeric code of a145sequence in terms of its length and its largest element. Suppose $s$146is a sequence of length~$k$, each element of which is less than or equal147to some number~$x$. Then $s$ has at most $k$ prime factors, each at148most~$p_{k-1}$, and each raised to at most $x+1$ in the prime149factorization of~$s$. In other words, if we define150\[151\fn{sequenceBound}(x,k) = p_{k-1}^{k \cdot (x+1)},152\]153then the numeric code of the sequence~$s$ described above is at154most~$\fn{sequenceBound}(x,k)$.155156Having such a bound on sequences gives us a way of defining new157functions using bounded search. For example, we can define158$\fn{concat}$ using bounded search. All we need to do is write down a159primitive recursive \emph{specification} of the object (number of the160concatenated sequence) we are looking for, and a bound on how far to161look. The following works:162\begin{align*}163 \fn{concat}(s,t) = {} & \bmin{v < \fn{sequenceBound}(s+t,\len{s} +164 \len{t})}{} \\165 & \quad(\len{v} = \len{s} + \len{t} \land {}\\166 & \qquad \bforall{i < \len{s}}{((v)_i = (s)_i) \land {} \\167 & \qquad \bforall{j < \len{t}}{((v)_{\len{s}+j} = (t)_j)})}168\end{align*}169170\begin{prob}171Show that there is a primitive recursive function~$\fn{sconcat}(s)$172with the property that173\[174\fn{sconcat}(\tuple{s_0, \dots, s_k}) = s_0 \concat \dots \concat s_k.175\]176\end{prob}177178\begin{prob}179Show that there is a primitive recursive function~$\fn{tail}(s)$180with the property that181\begin{align*}182 \fn{tail}(\emptyseq) & = 0 \text{ and}\\183 \fn{tail}(\tuple{s_0, \dots, s_{k}}) & = \tuple{s_1, \dots, s_{k}}.184\end{align*}185\end{prob}186187\begin{prop}188 \ollabel{prop:subseq}189 The function $\fn{subseq}(s, i, n)$ which returns the subsequence190 of $s$ of length~$n$ beginning at the $i$th element, is primitive191 recursive.192\end{prop}193194\begin{proof}195 Exercise.196\end{proof}197198\begin{prob}199 Prove \olref[cmp][rec][seq]{prop:subseq}.200\end{prob}201 202\end{document}203
content/computability/recursive-functions/trees.tex
1% Part: computability2% Chapter: recursive-functions3% Section: trees45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{tre}10\olsection{Trees}1112Sometimes it is useful to represent trees as natural numbers, just13like we can represent sequences by numbers and properties of and14operations on them by primitive recursive relations and functions on15their codes. We'll use sequences and their codes to do this. A tree16can be either a single node (possibly with a label) or else a node17(possibly with a label) connected to a number of subtrees. The node is18called the \emph{root} of the tree, and the subtrees it is connected19to its \emph{immediate subtrees}.2021We code trees recursively as a sequence $\tuple{k, d_1, \dots, d_k}$,22where $k$ is the number of immediate subtrees and $d_1$, \dots,~$d_k$23the codes of the immediate subtrees. If the nodes have labels, they24can be included after the immediate subtrees. So a tree consisting25just of a single node with label~$l$ would be coded by $\tuple{0,l}$,26and a tree consisting of a root (labelled~$l_1$) connected to two27single nodes (labelled $l_2$, $l_3$) would be coded by $\tuple{2,28 \tuple{0, l_2}, \tuple{0, l_3}, l_1}$.2930\begin{prop}31 \ollabel{prop:subtreeseq}32 The function $\fn{SubtreeSeq}(t)$, which returns the code of a33 sequence the elements of which are the codes of all subtrees of the34 tree with code~$t$, is primitive recursive.35\end{prop}3637\begin{proof}38 First note that $\fn{ISubtrees}(t) = \fn{subseq}(t, 1, (t)_0)$ is39 primitive recursive and returns the codes of the immediate subtrees40 of a tree~$t$. Now we can define a helper function41 $\fn{hSubtreeSeq}(t,n)$ which computes the sequence of all subtrees42 which are $n$ nodes removed from the root. The sequence of subtrees43 of~$t$ which is $0$ nodes removed from the root---in other words,44 begins at the root of~$t$---is the sequence consisting just45 of~$t$. To obtain a sequence of all level~$n+1$ subtrees of $t$, we46 concatenate the level $n$ subtrees with a sequence consisting of all47 immediate subtrees of the level $n$ subtrees. To get a list of all48 these, note that if $f(x)$ is a primitive recursive function49 returning codes of sequences, then $g_f(s, k) = f((s)_0) \concat50 \dots \concat f((s)_k)$ is also primitive recursive:51 \begin{align*}52 g(s, 0) & = f((s)_0)\\53 g(s, k+1) & = g(s, k) \concat f((s)_{k+1})54 \end{align*}55 For instance, if $s$ is a sequence of trees, then56 $h(s) = g_{\fn{ISubtrees}}(s, \len{s})$ gives the sequence of the57 immediate subtrees of the elements of~$s$. We can use it to define58 $\fn{hSubtreeSeq}$ by59 \begin{align*}60 \fn{hSubtreeSeq}(t, 0) & = \tuple{t} \\61 \fn{hSubtreeSeq}(t, n+1) & = \fn{hSubtreeSeq}(t, n) \concat62 h(\fn{hSubtreeSeq}(t, n)).63 \end{align*}64 The maximum level of subtrees in a tree coded by~$t$, i.e., the65 maximum distance between the root and a leaf node, is bounded by66 the code~$t$. So a sequence of codes of all subtrees of the tree67 coded by~$t$ is given by $\fn{hSubtreeSeq}(t, t)$.68\end{proof}6970\begin{prob}71 The definition of $\fn{hSubtreeSeq}$ in the proof of72 \olref[cmp][rec][tre]{prop:subtreeseq} in general includes73 repetitions. Give an alternative definition which guarantees that74 the code of a subtree occurs only once in the resulting list.75\end{prob}7677\end{document}78
content/computability/recursive-functions/other-recursions.tex
1% Part: computability2% Chapter: recursive-functions3% Section: other-recursions45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{ore}10\olsection{Other Recursions}1112Using pairing and sequencing, we can justify more exotic (and13useful) forms of primitive recursion. For example, it is often useful14to define two functions simultaneously, such as in the following15definition:16\begin{align*}17h_0(\vec x, 0) & = f_0(\vec x) \\18h_1(\vec x, 0) & = f_1(\vec x) \\19h_0(\vec x, y+1) & = g_0(\vec x, y, h_0(\vec x, y), h_1(\vec x, y)) \\20h_1(\vec x, y+1) & = g_1(\vec x, y, h_0(\vec x, y), h_1(\vec x, y))21\end{align*}22This is an instance of \emph{simultaneous recursion}. Another useful23way of defining functions is to give the value of $h(\vec x, y+1)$ in24terms of \emph{all} the values $h(\vec x, 0)$, \dots,~$h(\vec x, y)$, as in25the following definition:26\begin{align*}27h(\vec x, 0) & = f(\vec x) \\28h(\vec x, y+1) & = g(\vec x, y, \tuple{h(\vec x, 0), \dots, h(\vec x, y)}).29\end{align*}30The following schema captures this idea more succinctly:31\[32h(\vec x, y) = g(\vec x, y, \tuple{h(\vec x, 0), \dots, h(\vec x, y-1)})33\]34with the understanding that the last argument to $g$ is just the35empty sequence when $y$ is $0$. In either formulation, the idea is36that in computing the ``successor step,'' the function $h$ can make37use of the entire sequence of values computed so far.38This is known as a \emph{course-of-values} recursion. For a particular39example, it can be used to justify the following type of definition:40\begin{align*}41h(\vec x, y) & = \begin{cases}42 g(\vec x, y, h(\vec x, k(\vec x, y))) & \text{if $k(\vec x, y) < y$} \\43 f(\vec x) & \text{otherwise}44\end{cases}45\end{align*}46In other words, the value of $h$ at $y$ can be computed in terms of47the value of $h$ at \emph{any} previous value, given by~$k$.484950\begin{prob}51 Define the remainder function $r(x,y)$ by course-of-values52 recursion. (If $x$, $y$ are natural numbers and $y > 0$, $r(x,y)$ is53 the number less than~$y$ such that $x = z\times y + r(x,y)$ for54 some~$z$. For definiteness, let's say that if $y=0$, $r(x,0) = 0$.)55\end{prob}5657You should think about how to obtain these functions using ordinary58primitive recursion. One final version of primitive recursion is more59flexible in that one is allowed to change the \emph{parameters} (side60values) along the way:61\begin{align*}62h(\vec x, 0) & = f(\vec x) \\63h(\vec x, y+1) & = g(\vec x, y, h(k(\vec x), y))64\end{align*}65This, too, can be simulated with ordinary primitive recursion. (Doing66so is tricky. For a hint, try unwinding the computation by hand.)6768\end{document}
content/computability/recursive-functions/non-pr-functions.tex
1% Part: computability2% Chapter: recursive-functions3% Section: non-pr-functions45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{npr}10\olsection{Non-Primitive Recursive Functions}1112The primitive recursive functions do not exhaust the intuitively13computable functions. It should be intuitively clear that we can make14a list of all the unary primitive recursive functions,15$f_0$, $f_1$, $f_2$,~\dots such that we can effectively compute the value of16$f_x$ on input $y$; in other words, the function $g(x,y)$, defined by17\[18g(x,y) = f_x(y)19\]20is computable. But then so is the function21\begin{eqnarray*}22h(x) & = & g(x,x) + 1 \\23& = & f_x(x) +1.24\end{eqnarray*}25For each primitive recursive function $f_i$, the value of $h$ and26$f_i$ differ at $i$. So $h$ is computable, but not primitive27recursive; and one can say the same about $g$. This is an28``effective'' version of Cantor's diagonalization argument.2930One can provide more explicit examples of computable functions that31are not primitive recursive. For example, let the notation $g^n(x)$32denote $g(g(\dots g(x)))$, with $n$ $g$'s in all; and define a33sequence $g_0,g_1,\dots$ of functions by34\begin{eqnarray*}35g_0(x) & = & x+1 \\36g_{n + 1}(x) & = & g_n^x(x)37\end{eqnarray*}38You can confirm that each function $g_n$ is primitive recursive. Each39successive function grows much faster than the one before; $g_1(x)$ is40equal to $2x$, $g_2(x)$ is equal to $2^x \cdot x$, and $g_3(x)$ grows41roughly like an exponential stack of $x$ $2$'s. The Ackermann--P\'eter42function is essentially the function $G(x) = g_x(x)$, and one can show43that this grows faster than any primitive recursive function.4445Let us return to the issue of enumerating the primitive recursive46functions. Remember that we have assigned symbolic notations to each47primitive recursive function; so it suffices to enumerate48notations. We can assign a natural number $\#(F)$ to each notation $F$,49recursively, as follows:50\begin{eqnarray*}51\#(0) & = & \langle 0 \rangle \\52\#(S) & = & \langle 1 \rangle \\53\#(\Proj{n}{i}) & = & \langle 2, n, i \rangle \\54\#(\fn{Comp}_{k,l}[H,G_0,\dots,G_{k-1}]) & = & \langle553,k,l,\#(H),\#(G_0),\dots,\#(G_{k-1}) \rangle \\56\#(\fn{Rec}_l[G,H]) & = & \langle 4, l, \#(G), \#(H) \rangle57\end{eqnarray*}58Here we are using the fact that every sequence of numbers can be viewed59as a natural number, using the codes from the last section. The upshot60is that every code is assigned a natural number. Of course, some61sequences (and hence some numbers) do not correspond to notations; but62we can let $f_i$ be the unary primitive recursive function with63notation coded as $i$, if $i$ codes such a notation; and the constant64$0$ function otherwise. The net result is that we have an explicit way of65enumerating the unary primitive recursive functions.6667(In fact, some functions, like the constant zero function, will appear68more than once on the list. This is not just an artifact of our69coding, but also a result of the fact that the constant zero function has70more than one notation. We will later see that one can not computably71avoid these repetitions; for example, there is no computable function72that decides whether or not a given notation represents the constant73zero function.)7475We can now take the function $g(x,y)$ to be given by $f_x(y)$, where76$f_x$ refers to the enumeration we have just described. How do we know77that $g(x,y)$ is computable? Intuitively, this is clear: to compute78$g(x,y)$, first ``unpack'' $x$, and see if it is a notation for a unary79function. If it is, compute the value of that function on input~$y$.8081\begin{tagblock}{TMs}82\begin{digress}83You may already be convinced that (with some work!) one can write a84program (say, in Java or C++) that does this; and now we can appeal to85the Church--Turing thesis, which says that anything that, intuitively,86is computable can be computed by a Turing machine.8788Of course, a more direct way to show that $g(x,y)$ is computable is to89describe a Turing machine that computes it, explicitly. This would,90in particular, avoid the Church--Turing thesis and appeals to91intuition. Soon we will have built up enough machinery to show92that $g(x,y)$ is computable, appealing to a model of computation that93can be \emph{simulated} on a Turing machine: namely, the recursive94functions.95\end{digress}96\end{tagblock}9798\end{document}
content/computability/recursive-functions/partial-functions.tex
1% Part: computability2% Chapter: recursive-functions3% Section: partial-functions45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{par}10\olsection{Partial Recursive Functions}111213To motivate the definition of the recursive functions, note that our14proof that there are computable functions that are not primitive15recursive actually establishes much more. The argument was16simple: all we used was the fact that it is possible to enumerate17functions $f_0,f_1,\dots$ such that, as a function of $x$ and $y$,18$f_x(y)$ is computable. So the argument applies to \emph{any class of19 functions that can be enumerated in such a way}. This puts us in a20bind: we would like to describe the computable functions explicitly;21but any explicit description of a collection of computable functions22cannot be exhaustive!2324The way out is to allow \emph{partial} functions to come into play. We25will see that it \emph{is} possible to enumerate the partial26computable functions.\iftag{TMs}{ In fact, we already pretty much know27 that this is the case, since it is possible to enumerate Turing28 machines in a systematic way.}{} We will come back to our diagonal29argument later, and explore why it does not go through when partial30functions are included.3132The question is now this: what do we need to add to the primitive33recursive functions to obtain all the partial recursive functions? We34need to do two things:35\begin{enumerate}36\item Modify our definition of the primitive recursive functions to37 allow for partial functions as well.38\item \emph{Add} something to the definition, so that some new partial39 functions are included.40\end{enumerate}4142The first is easy. As before, we will start with zero, successor, and43projections, and close under composition and primitive recursion. The44only difference is that we have to modify the definitions of45composition and primitive recursion to allow for the possibility that46some of the terms in the definition are not defined. If $f$ and $g$47are partial functions, we will write $f(x) \fdefined$ to mean that $f$48is defined at $x$, i.e., $x$ is in the domain of $f$; and $f(x)49\fundefined$ to mean the opposite, i.e., that $f$ is not defined at~$x$.50We will use $f(x) \simeq g(x)$ to mean that either $f(x)$ and $g(x)$51are both undefined, or they are both defined and equal. We will use these52notations for more complicated terms as well. We will adopt the53convention that if $h$ and $g_0$, \dots,~$g_k$ all are partial functions,54then55\[56h(g_0(\vec x),\dots,g_k(\vec x))57\]58is defined if and only if each $g_i$ is defined at $\vec x$, and $h$59is defined at $g_0(\vec x)$, \dots,~$g_k(\vec x)$. With this60understanding, the definitions of composition and primitive recursion61for partial functions is just as above, except that we have to replace62``$=$'' by ``$\simeq$''.6364What we will add to the definition of the primitive recursive65functions to obtain partial functions is the \emph{unbounded search66 operator}. If $f(x,\vec z)$ is any partial function on the natural67numbers, define $\mu x \; f(x,\vec z)$ to be68\begin{quote}69 the least $x$ such that $f(0,\vec z), f(1,\vec z), \dots, f(x,\vec70 z)$ are all defined, and $f(x,\vec z) = 0$, if such an $x$ exists71\end{quote}72with the understanding that $\mu x \; f(x,\vec z)$ is undefined73otherwise. This defines $\mu x \; f(x,\vec z)$ uniquely.7475\begin{explain}76Note that our definition makes no reference to\iftag{TMs}{ Turing77 machines, or}{} algorithms, or any specific computational model. But78like composition and primitive recursion, there is an operational,79computational intuition behind unbounded search. When it80comes to the computability of a partial function, arguments81where the function is undefined correspond to inputs for which the82computation does not halt. The procedure for computing $\mu x \;83f(x,\vec z)$ will amount to this: compute $f(0,\vec z), f(1,\vec z),84f(2,\vec z)$ until a value of 0 is returned. If any of the85intermediate computations do not halt, however, neither does the86computation of $\mu x \; f(x,\vec z)$.87\end{explain}8889If $R(x,\vec z)$ is any relation, $\mu x \; R(x,\vec z)$ is defined to90be $\mu x \; (1 \tsub \Char{R}(x,\vec z))$. In other words, $\mu x \;91R(x,\vec z)$ returns the least value of $x$ such that $R(x,\vec z)$92holds. So, if $f(x,\vec z)$ is a total function, $\mu x \; f(x,\vec93z)$ is the same as $\mu x \; (f(x,\vec z) = 0)$. But note that our94original definition is more general, since it allows for the95possibility that $f(x,\vec z)$ is not everywhere defined (whereas, in96contrast, the characteristic function of a relation is always total).9798\begin{defn}99The set of \emph{partial recursive functions} is the smallest set of100partial functions from the natural numbers to the natural numbers (of101various arities) containing zero, successor, and projections, and102closed under composition, primitive recursion, and unbounded search.103\end{defn}104105Of course, some of the partial recursive functions will happen to be106total, i.e., defined for every argument.107108\begin{defn}109\ollabel{defn:recursive-fn}110The set of \emph{recursive functions} is111the set of partial recursive functions that are total.112\end{defn}113114A recursive function is sometimes called ``total recursive'' to115emphasize that it is defined everywhere.116117\end{document}118119
content/computability/recursive-functions/normal-form.tex
1% Part: computability2% Chapter: recursive-functions3% Section: normal-form45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{nft}10\olsection{The Normal Form Theorem}1112\begin{thm}[Kleene's Normal Form Theorem]13\ollabel{thm:kleene-nf}14There is a primitive recursive relation $T(e, x, s)$ and a primitive15recursive function $U(s)$, with the following property: if $f$ is any16partial recursive function, then for some~$e$,17\[18f(x) \simeq U(\umin{s}{T(e, x, s)})19\]20for every $x$.21\end{thm}2223\begin{explain}24The proof of the normal form theorem is involved, but the basic idea25is simple. Every partial recursive function has an \emph{index}~$e$,26intuitively, a number coding its program or definition. If $f(x)27\fdefined$, the computation can be recorded systematically and coded28by some number~$s$, and the fact that $s$ codes the computation of~$f$29on input~$x$ can be checked primitive recursively using only $x$ and30the definition~$e$. Consequently, the relation~$T$, ``the function31with index~$e$ has a computation for input~$x$, and $s$ codes this32computation,'' is primitive recursive. Given the full record of the33computation~$s$, the ``upshot'' of~$s$ is the value of~$f(x)$, and it34can be obtained from~$s$ primitive recursively as well.3536The normal form theorem shows that only a single unbounded search is37required for the definition of any partial recursive function.38Basically, we can search through all numbers until we find one that39codes a computation of the function with index~$e$ for input~$x$. We40can use the numbers~$e$ as ``names'' of partial recursive functions,41and write $\cfind{e}$ for the function~$f$ defined by the equation in42the theorem. Note that any partial recursive function can have more43than one index---in fact, every partial recursive function has44infinitely many indices.45\end{explain}46\end{document}47
content/computability/recursive-functions/halting-problem.tex
1% Part: computability2% Chapter: recursive-functions3% Section: halting-problem45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{hlt}10\olsection{The Halting Problem}1112The \emph{halting problem} in general is the problem of deciding,13given the specification~$e$ (e.g., program) of a computable function14and a number~$n$, whether the computation of the function on input~$n$15halts, i.e., produces a result. Famously, Alan Turing proved that16this problem itself cannot be solved by a computable function, i.e.,17the function18\[19h(e, n) =20\begin{cases}211 & \text{if computation $e$ halts on input $n$}\\220 & \text{otherwise,}23\end{cases}24\]25is not computable.2627In the context of partial recursive functions, the role of the28specification of a program may be played by the index~$e$ given in29Kleene's normal form theorem. If $f$ is a partial recursive function,30any $e$~for which the equation in the normal form theorem holds, is an31index of~$f$. Given a number~$e$, the normal form theorem states that32\[33\cfind{e}(x) \simeq U(\mu s \; T(e, x, s))34\]35is partial recursive, and for every partial recursive $f\colon \Nat36\to \Nat$, there is an $e \in \Nat$ such that $\cfind{e}(x) \simeq37f(x)$ for all~$x \in \Nat$. In fact, for each such $f$ there is not38just one, but infinitely many such~$e$. The \emph{halting function}~$h$39is defined by40\[41h(e, x) =42\begin{cases}431 & \text{if $\cfind{e}(x) \fdefined$}\\440 & \text{otherwise.}45\end{cases}46\]47Note that $h(e, x) = 0$ if $\cfind{e}(x) \fundefined$, but also48when~$e$ is not the index of a partial recursive function at all.4950\begin{thm}51\ollabel{thm:halting-problem}52The halting function~$h$ is not partial recursive.53\end{thm}5455\begin{proof}56If $h$57were partial recursive, we could define58\[59d(y) =60\begin{cases}611 & \text{if $h(y, y) = 0$}\\62\umin{x}{x \neq x} & \text{otherwise.}63\end{cases}64\]65Since no number $x$ satisfies $x \neq x$, there is no $\umin{x}{x \neq66x}$, and so $d(y) \fundefined$ iff $h(y,y) \neq 0$. From this67definition it follows that68\begin{enumerate}69\item $d(y) \fdefined$ iff $\cfind{y}(y) \fundefined$ or $y$ is70 not the index of a partial recursive function.71\item $d(y) \fundefined$ iff $\cfind{y}(y) \fdefined$.72\end{enumerate}73If $h$ were partial recursive, then $d$ would be partial recursive as74well. Thus, by the Kleene normal form theorem, it has an index~$e_d$.75Consider the value of $h(e_d, e_d)$. There are two possible cases, $0$76and~$1$.77\begin{enumerate}78\item If $h(e_d, e_d) = 1$ then $\cfind{e_d}(e_d) \fdefined$. But79 $\cfind{e_d} \simeq d$, and $d(e_d)$ is defined iff $h(e_d, e_d) = 0$.80 So $h(e_d, e_d) \neq 1$.81\item If $h(e_d, e_d) = 0$ then either $e_d$ is not the index of a82 partial recursive function, or it is and $\cfind{e_d}(e_d)83 \fundefined$. But again, $\cfind{e_d} \simeq d$, and $d(e_d)$ is undefined84 iff $\cfind{e_d}(e_d) \fdefined$.85\end{enumerate}86The upshot is that $e_d$ cannot, after all, be the index of a partial87recursive function. But if $h$ were partial recursive, $d$ would be88too, and so our definition of $e_d$ as an index of it would be89admissible. We must conclude that $h$ cannot be partial recursive.90\end{proof}9192\end{document}9394
content/computability/recursive-functions/general-recursive-functions.tex
1% Part: computability2% Chapter: recursive-functions3% Section: general-recursive-functions45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{rec}{gen}10\olsection{General Recursive Functions}1112There is another way to obtain a set of total functions. Say a total13function $f(x,\vec z)$ is \emph{regular} if for every sequence of14natural numbers $\vec z$, there is an $x$ such that $f(x,\vec z) = 0$.15In other words, the regular functions are exactly those functions to16which one can apply unbounded search, and end up with a total17function. One can, conservatively, restrict unbounded search to18regular functions:1920\begin{defn}21\ollabel{defn:general-recursive}22The set of \emph{general recursive functions} is the smallest set of23functions from the natural numbers to the natural numbers (of various24arities) containing zero, successor, and projections, and closed under25composition, primitive recursion, and unbounded search applied to26\emph{regular} functions.27\end{defn}2829Clearly every general recursive function is total. The difference30between \olref{defn:general-recursive} and31\olref[par]{defn:recursive-fn} is that in the latter one is allowed to32use partial recursive functions along the way; the only requirement is33that the function you end up with at the end is total. So the word34``general,'' a historic relic, is a misnomer; on the surface,35\olref{defn:general-recursive} is \emph{less} general than36\olref[par]{defn:recursive-fn}. But, fortunately, the difference is37illusory; though the definitions are different, the set of general38recursive functions and the set of recursive functions are one and the39same.4041\end{document}42