content/turing-machines/undecidability/undecidability.tex
1% Part: turing-machines2% Chapter: undecidability34\documentclass[../../../include/open-logic-chapter]{subfiles}56\begin{document}78\olchapter{tur}{und}{Undecidability}910\olimport{introduction}1112\olimport{enumerating-tms}1314\olimport{universal-tm}1516\olimport{halting-problem}1718\olimport{decision-problem}1920\olimport{representing-tms}2122\olimport{verification}2324\olimport{unsolvability-decision-problem}2526\olimport{trakhtenbrot}2728\OLEndChapterHook2930\end{document}
content/turing-machines/undecidability/introduction.tex
1% Part: turing-machines 2% Chapter: undecidability3% Section: introduction45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{und}{int} 10\olsection{Introduction}1112It might seem obvious that not every function, even every arithmetical13function, can be computable. There are just too many, whose behavior14is too complicated. Functions defined from the decay of radioactive15particles, for instance, or other chaotic or random behavior. Suppose16we start counting 1-second intervals from a given time, and define the17function $f(n)$ as the number of particles in the universe that decay18in the $n$-th 1-second interval after that initial moment. This seems19like a candidate for a function we cannot ever hope to compute.2021But it is one thing to not be able to imagine how one would compute22such functions, and quite another to actually prove that they are23uncomputable. In fact, even functions that seem hopelessly24complicated may, in an abstract sense, be computable. For instance,25suppose the universe is finite in time---some day, in the very distant26future the universe will contract into a single point, as some27cosmological theories predict. Then there is only a finite (but28incredibly large) number of seconds from that initial moment for which29$f(n)$ is defined. And any function which is defined for only finitely30many inputs is computable: we could list the outputs in one big table,31or code it in one very big Turing machine state transition diagram.3233We are often interested in special cases of functions whose values give34the answers to yes/no questions. For instance, the question ``is $n$35a prime number?'' is associated with the function36\[37\fn{isprime}(n) = \begin{cases}38 1 & \text{if $n$ is prime}\\39 0 & \text{otherwise.}40 \end{cases}41\]42We say that a yes/no question can be \emph{effectively decided}, if43the associated $1/0$-valued function is effectively computable.4445To prove mathematically that there are functions which cannot be46effectively computed, or problems that cannot effectively decided, it47is essential to fix a specific model of computation, and show48that there are functions it cannot compute or problems it cannot49decide. We can show, for instance, that not every function can be50computed by Turing machines, and not every problem can be decided by51Turing machines. We can then appeal to the Church--Turing thesis to52conclude that not only are Turing machines not powerful enough to53compute every function, but no effective procedure can.5455The key to proving such negative results is the fact that we can56assign numbers to Turing machines themselves. The easiest way to do57this is to enumerate them, perhaps by fixing a specific way to write58down Turing machines and their programs, and then listing them in a59systematic fashion. Once we see that this can be done, then the60existence of Turing-uncomputable functions follows by simple61cardinality considerations: the set of functions from $\Nat$ to~$\Nat$ (in62fact, even just from $\Nat$ to $\{0, 1\}$) are !!{nonenumerable}, but63since we can enumerate all the Turing machines, the set of Turing-computable64functions is only !!{denumerable}.6566We can also define \emph{specific} functions and problems which we can67prove to be uncomputable and undecidable, respectively. One such68problem is the so-called \emph{Halting Problem.} Turing machines can69be finitely described by listing their instructions. Such a70description of a Turing machine, i.e., a Turing machine program, can71of course be used as input to another Turing machine. So we can72consider Turing machines that decide questions about other Turing73machines. One particularly interesting question is this: ``Does the74given Turing machine eventually halt when started on input~$n$?'' It75would be nice if there were a Turing machine that could decide this76question: think of it as a quality-control Turing machine which77ensures that Turing machines don't get caught in infinite loops and78such. The interesting fact, which Turing proved, is that there cannot79be such a Turing machine. There cannot be a single Turing machine80which, when started on input consisting of a description of a Turing81machine $M$ and some number~$n$, will always halt with either output82$1$ or $0$ according to whether $M$ machine would have halted when83started on input $n$ or not.8485Once we have examples of specific undecidable problems we can use them86to show that other problems are undecidable, too. For instance, one87celebrated undecidable problem is the question, ``Is the first-order88!!{formula}~$!A$ valid?''. There is no Turing machine which, given as89input a first-order !!{formula}~$!A$, is guaranteed to halt with90output $1$ or $0$ according to whether $!A$ is valid or not.91Historically, the question of finding a procedure to effectively solve92this problem was called simply ``the'' decision problem; and so we say93that the decision problem is unsolvable. Turing and Church proved this94result independently at around the same time, so it is also called the95Church--Turing Theorem.9697\end{document}
content/turing-machines/undecidability/enumerating-tms.tex
1% Part: turing-machines2% Chapter: undecidability3% Section: enumerating-tms45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{und}{enu}10\olsection{Enumerating Turing Machines}1112\begin{explain}13We can show that the set of all Turing machines is !!{enumerable}.14This follows from the fact that each Turing machine can be finitely15described. The set of states and the tape vocabulary are finite sets.16The transition function is a partial function from $Q \times \Sigma$17to $Q \times \Sigma \times \{\TMleft, \TMright, \TMstay\}$, and so18likewise can be specified by listing its values for the finitely many19argument pairs for which it is defined.2021This is true as far as it goes, but there is a subtle difference. The22definition of Turing machines made no restriction on what !!{element}s23the set of states and tape alphabet can have. So, e.g., for every real24number, there technically is a Turing machine that uses that number as25a state. However, the \emph{behavior} of the Turing machine is26independent of which objects serve as states and vocabulary. Consider27the two Turing machines in \olref{fig:variants}.28\begin{figure}29\begin{center}30\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,31 semithick]32 \tikzstyle{every state}=[fill=none,draw=black,text=black]3334 \node[initial,state] (A) {$q_0$};35 \node[state] (B) [right of=A] {$q_1$};3637 \path (A) edge [bend left] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (B)38 (B) edge [loop above] node {\TMtrans{\TMblank}{\TMblank}{\TMright}} (B)39 edge [bend left] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (A);40\end{tikzpicture}\\41\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,42 semithick]43\tikzstyle{every state}=[fill=none,draw=black,text=black]4445\node[initial,state] (A) {$s$};46\node[state] (B) [right of=A] {$h$};4748\path (A) edge [bend left] node {\TMtrans{A}{A}{\TMright}} (B)49(B) edge [loop above] node {\TMtrans{\TMblank}{\TMblank}{\TMright}} (B)50edge [bend left] node {\TMtrans{A}{A}{\TMright}} (A);51\end{tikzpicture}52\end{center}53\caption{Variants of the \emph{Even} machine}54\ollabel{fig:variants}55\end{figure}56These two diagrams correspond to two machines, $M$ with the tape57alphabet $\Sigma = \{\TMendtape,\TMblank,\TMstroke\}$ and set of58states $\{q_0,q_1\}$, and $M'$ with alphabet $\Sigma' =59\{\TMendtape,\TMblank,A\}$ and states $\{s,h\}$. But their60instructions are otherwise the same: $M$ will halt on a sequence of61$n$ $\TMstroke$'s iff $n$ is even, and $M'$ will halt on a sequence of62$n$ $A$'s iff $n$ is even. All we've done is rename $\TMstroke$63to~$A$, $q_0$ to~$s$, and $q_1$ to~$h$. This example generalizes: we64can think of Turing machines as the same as long as one results from65the other by such a renaming of symbols and states. In fact, we can66simply think of the symbols and states of a Turing machine as positive67integers: instead of $\sigma_0$ think~$1$, instead of $\sigma_1$68think~$2$, etc.; $\TMendtape$ is~$1$, $\TMblank$ is~$2$, etc. In this69way, the \emph{Even} machine becomes the machine depicted in70\olref{fig:standard-even}.71\begin{figure}72\[\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,73 semithick]74\tikzstyle{every state}=[fill=none,draw=black,text=black]7576\node[initial,state] (A) {$1$};77\node[state] (B) [right of=A] {$2$};7879\path (A) edge [bend left] node {\TMtrans{3}{3}{\TMright}} (B)80(B) edge [loop above] node {\TMtrans{2}{2}{\TMright}} (B)81edge [bend left] node {\TMtrans{3}{3}{\TMright}} (A);82\end{tikzpicture}83\]84\caption{A standard \emph{Even} machine}85\ollabel{fig:standard-even}86\end{figure}87We might call a Turing machine with states and symbols that are88positive integers a \emph{standard} machine, and only consider89standard machines from now on.\footnote{The terminology ``standard90machine'' is not standard.}9192We wanted to show that the set of Turing machines is !!{enumerable},93and with the above considerations in mind, it is enough to show that94the set of standard Turing machines is !!{enumerable}. Suppose we are95given a standard Turing machine $M = \tuple{Q, \Sigma, q_0, \delta}$.96How could we describe it using a finite string of positive integers?97We'll first list the number of states, the states themselves, the98number of symbols, the symbols themselves, and the starting state.99(Remember, all of these are positive integers, since $M$ is a standard100machine.) What about~$\delta$? The set of possible arguments, i.e.,101pairs $\tuple{q,\sigma}$, is finite, since $Q$ and~$\Sigma$ are102finite. So the information in~$\delta$ is simply the finite list of103all $5$-tuples $\tuple{q, \sigma, q', \sigma', d}$ where104$\delta(q,\sigma) = \tuple{q', \sigma', D}$, and $d$ is a number that105codes the direction~$D$ (say, $1$ for~$\TMleft$, $2$ for~$\TMright$,106and $3$ for~$\TMstay$).107108In this way, every standard Turing machine can be described by a109finite list of positive integers, i.e., as a sequence $s_M \in110(\PosInt)^*$. For instance, the standard \emph{Even} machine is coded111by the sequence112\[1132, \underbrace{1, 2}_Q, 3, \overbrace{1, 2, 3}^\Sigma, 1, \underbrace{1, 3, 2, 3, 2}_{\delta(1,3) = \tuple{2,3,R}}, 114\overbrace{2, 2, 2, 2, 2}^{\delta(2,2) = \tuple{2,2,R}},115\underbrace{2, 3, 1, 3, 2}_{\delta(2,3) = \tuple{1,3,R}}.116\]117\end{explain}118119\begin{thm}120There are functions from $\Nat$ to~$\Nat$ which are not Turing121computable.122\end{thm}123124\begin{proof}125We know that the set of finite sequences of positive126integers~$(\PosInt)^*$ is !!{enumerable}127(\cref{sfr:siz:zigzag:prob:posint-star}). This gives us that the set128of descriptions of standard Turing machines, as a subset129of~$(\PosInt)^*$, is itself enumerable. Every Turing computable130function $\Nat$ to~$\Nat$ is computed by some (in fact, many) Turing131machines. By renaming its states and symbols to positive integers (in132particular, $\TMendtape$ as~$1$, $\TMblank$ as~$2$, and $\TMstroke$133as~$3$) we can see that every Turing computable function is computed134by a standard Turing machine. This means that the set of all Turing135computable functions from $\Nat$ to~$\Nat$ is also enumerable.136137On the other hand, the set of all functions from $\Nat$ to~$\Nat$ is138not !!{enumerable} (\cref{sfr:siz:red:prob:nat-nat}). If all functions139were computable by some Turing machine, we could enumerate the set of140all functions by listing all the descriptions of Turing machines that141compute them. So there are some functions that are not Turing142computable. 143\end{proof}144145\begin{prob}146 Can you think of a way to describe Turing machines that does not147 require that the states and alphabet symbols are explicitly listed?148 You may define your own notion of ``standard'' machine, but say149 something about why every Turing machine can be computed by a150 ``standard'' machine in your new sense.151\end{prob}152153\end{document}
content/turing-machines/undecidability/universal-tm.tex
1% Part: turing-machines2% Chapter: undecidability3% Section: universal-tm45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{und}{uni}10\olsection{Universal Turing Machines}1112In \olref[enu]{sec} we discussed how every Turing machine can be13described by a finite sequence of integers. This sequence encodes the14states, alphabet, start state, and instructions of the Turing machine.15We also pointed out that the set of all of these descriptions is16!!{enumerable}. Since the set of such descriptions is !!{denumerable},17this means that there is !!a{surjective} function from~$\Nat$ to18these descriptions. Such !!a{surjective} function can be obtained, for19instance, using Cantor's zig-zag method. It gives us a way of20enumerating all (descriptions) of Turing machines. If we fix one such21enumeration, it now makes sense to talk of the $1$st, $2$nd, \dots,22$e$th Turing machine. These numbers are called \emph{indices}.2324\begin{defn}25If $M$~is the $e$th Turing machine (in our fixed enumeration), we26say that $e$~is an \emph{index} of~$M$. We write $M_e$ for the $e$th27Turing machine.28\end{defn}2930A machine may have more than one index, e.g., two descriptions of~$M$31may differ in the order in which we list its instructions, and these32different descriptions will have different indices.3334Importantly, it is possible to give the enumeration of Turing machine35descriptions in such a way that we can effectively compute the36description of~$M$ from its index, and to effectively compute an index37of a machine~$M$ from its description. By the Church--Turing thesis,38it is then possible to find a Turing machine which recovers the39description of the Turing machine with index~$e$ and writes the40corresponding description on its tape as output. The description would41be a sequence of blocks of~$\TMstroke$'s (representing the positive42integers in the sequence describing~$M_e$).4344Given this, it now becomes natural to ask: what functions of Turing45machine indices are themselves computable by Turing machines? What46properties of Turing machine indices can be decided by Turing47machines? An example: the function that maps an index~$e$ to the48number of states the Turing machine with index~$e$ has, is computable49by a Turing machine. Here's what such a Turing machine would do:50started on a tape containing a single block of $e$~$\TMstroke$'s, it51would first decode $e$ into its description. The description is now52represented by a sequence of blocks of~$\TMstroke$'s on the tape.53Since the first !!{element} in this sequence is the number of states.54So all that has to be done now is to erase everything but the first55block of $\TMstroke$'s and then halt.5657A remarkable result is the following:5859\begin{thm}\ollabel{thm:universal-tm} There is a \emph{universal60 Turing machine}~$U$ which, when started on input $\tuple{e,n}$ 61 \begin{enumerate}62 \item halts iff $M_e$ halts on input~$n$, and63 \item if $M_e$ halts with output $m$, so does~$U$.64 \end{enumerate}65 $U$ thus computes the function $f\colon \Nat \times \Nat \pto \Nat$66 given by $f(e,n) = m$ if $M_e$ started on input~$n$ halts with67 output~$m$, and undefined otherwise.68\end{thm}6970\begin{proof}71 To actually produce~$U$ is basically impossible, since it is an72 extremely complicated machine. But we can describe in outline how it73 works, and then invoke the Church--Turing thesis. When it starts,74 $U$'s tape contains a block of $e$ $\TMstroke$'s followed by a block75 of $n$~$\TMstroke$'s. It first ``decodes'' the index~$e$ to the76 right of the input~$n$. This produces a list of numbers (i.e.,77 blocks of $\TMstroke$'s separated by~$\TMblank$'s) that describes78 the instructions of machine~$M_e$. $U$ then writes the number of the79 start state of~$M_e$ and the number~$1$ on the tape to the right of80 the description of~$M_e$. (Again, these are represented in unary, as81 blocks of $\TMstroke$'s.) Next, it copies the input (block of82 $n$~$\TMstroke$'s) to the right---but it replaces each $\TMstroke$83 by a block of three $\TMstroke$'s (remember, the number of the $\TMstroke$84 symbol is~$3$, $1$ being the number of~$\TMendtape$ and $2$ being85 the number of~$\TMblank$). At the left end of this sequence of blocks86 (separated by $\TMblank$ symbols on the tape of~$U$), it writes a87 single~$\TMstroke$, the code for~$\TMendtape$.8889 $U$ now has on its tape: the index~$e$, the number~$n$, the code90 number of the start state (the ``current state''), the number of the91 initial head position~$1$ (the ``current head position''), and the92 initial contents of the ``tape'' (a sequence of blocks93 of~$\TMstroke$'s representing the code numbers of the symbols94 of~$M_e$---the ``symbols''---separated by~$\TMblank$'s).9596 It now simulates what $M_e$ would do if started on input~$n$, by97 doing the following:98 \begin{enumerate}99 \item Find the number~$k$ of the ``current head position'' (at the100 beginning, that's~$1$),101 \item Move to the $k$th block in the ``tape'' to see what the102 ``symbol'' there is,103 \item\ollabel{find-inst}%104 Find the instruction matching the current ``state'' and105 ``symbol,''106 \item Move back to the $k$th block on the ``tape'' and replace the107 ``symbol'' there with the code number of the symbol $M_e$ would108 write,109 \item Move the head to where it records the current ``state'' and110 replace the number there with the number of the new state,111 \item Move to the place where it records the ``tape position'' and112 erase a~$\TMstroke$ or add a~$\TMstroke$ (if the instruction says113 to move left or right, respectively).114 \item Repeat.\footnote{We're glossing over some subtle115 difficulties here. E.g., $U$~may need some extra space when it116 increases the counter where it keeps track of the ``current head117 position''---in that case it will have to move the entire ``tape''118 to the right.}119 \end{enumerate}120 If $M_e$ started on input~$n$ never halts, then $U$ also never121 halts, so its output is undefined.122123 If in step~\olref{find-inst} it turns out that the description124 of~$M_e$ contains no instruction for the current125 ``state''/``symbol'' pair, then $M_e$ would halt. If this happens,126 $U$ erases the part of its tape to the left of the ``tape.'' For127 each block of three~$\TMstroke$'s (representing a~$\TMstroke$ on128 $M_e$'s tape), it writes a $\TMstroke$ on the left end of its own129 tape, and successively erases the ``tape.'' When this is done,130 $U$'s~tape contains a single block of~$\TMstroke$'s of length~$m$.131 132 If $U$ encounters something other than a block of133 three~$\TMstroke$'s on the ``tape,'' it immediately halts. Since134 $U$'s~tape in this case does not contain a single block135 of~$\TMstroke$'s, its output is not a natural number, i.e., $f(e,n)$136 is undefined in this case.137\end{proof}138139\end{document}
content/turing-machines/undecidability/halting-problem.tex
1% Part: computability2% Chapter: machines-computations3% Section: halting-problem45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{und}{hal}10\olsection{The Halting Problem}1112\begin{explain}13Assume we have fixed some enumeration of Turing machine14descriptions. Each Turing machine thus receives an \emph{index}: its15place in the enumeration $M_1$, $M_2$, $M_3$, \dots{} of Turing machine16descriptions.1718We know that there must be non-Turing-computable functions: the set of19Turing machine descriptions---and hence the set of Turing machines---is20!!{enumerable}, but the set of all functions from $\Nat$ to $\Nat$ is21not. But we can find specific examples of non-computable functions as22well. One such function is the halting function.23\end{explain}2425\begin{defn}[Halting function]26 The \emph{halting function}~$h$ is defined as27\[28h(e,n) =29\begin{cases}30 \text{0} & \text{if machine~$M_e$ does not halt for input $n$} \\31 \text{1} & \text{if machine~$M_e$ halts for input $n$}32\end{cases}33\]34\end{defn}3536\begin{defn}[Halting problem]37The \emph{Halting Problem} is the problem of determining (for any $e$, $n$)38whether the Turing machine~$M_e$ halts for an input of~$n$ strokes.39\end{defn}4041\begin{explain}42We show that~$h$ is not Turing-computable by showing that a related43function,~$s$, is not Turing-computable. This proof relies on the fact44that anything that can be computed by a Turing machine can be computed45by a disciplined Turing machine (\olref[mac][dis]{sec}), and the fact46that two Turing machines can be hooked together to create a single47machine (\olref[mac][cmb]{sec}).48\end{explain}4950\begin{defn} The function~$s$ is defined as51\[52s(e) =53\begin{cases}54 \text{0} & \text{if machine~$M_e$ does not halt for input $e$} \\55 \text{1} & \text{if machine~$M_e$ halts for input $e$}56\end{cases}57\]58\end{defn}5960\begin{lem}61The function~$s$ is not Turing computable.62\end{lem}6364\begin{proof}65We suppose, for contradiction, that the function~$s$ is Turing66computable. Then there would be a Turing machine~$S$ that67computes~$s$. We may assume, without loss of generality, that when $S$68halts, it does so while scanning the first square (i.e., that it is69disciplined). This machine can be ``hooked up'' to another70machine~$J$, which halts if it is started on input~$0$ (i.e., if it71reads $\TMblank$ in the initial state while scanning the square to the72right of the end-of-tape symbol), and otherwise wanders off to the73right, never halting. $S \concat J$, the machine created by hooking74$S$ to~$J$, is a Turing machine, so it is $M_e$ for some~$e$ (i.e., it75appears somewhere in the enumeration). Start $M_e$ on an input of~$e$76$\TMstroke$s. There are two possibilities: either $M_e$ halts or it77does not halt.78\begin{enumerate}79\item Suppose $M_e$ halts for an input of $e$ $\TMstroke$s. Then $s(e)80 = 1$. So $S$, when started on~$e$, halts with a single $\TMstroke$81 as output on the tape. Then $J$ starts with a $\TMstroke$ on the82 tape. In that case $J$ does not halt. But $M_e$ is the machine $S83 \concat J$, so it should do exactly what $S$ followed by $J$ would84 do (i.e., in this case, wander off to the right and never halt). So85 $M_e$ cannot halt for an input of $e$ $\TMstroke$'s.8687\item Now suppose $M_e$ does not halt for an input of $e$88 $\TMstroke$s. Then $s(e) = 0$, and $S$, when started on input~$e$,89 halts with a blank tape. $J$,~when started on a blank tape,90 immediately halts. Again, $M_e$ does what $S$ followed by~$J$ would91 do, so $M_e$ must halt for an input of $e$ $\TMstroke$'s.92\end{enumerate}93In each case we arrive at a contradiction with our assumption. This94shows there cannot be a Turing machine~$S$: $s$~is not Turing95computable.96\end{proof}9798\begin{thm}[Unsolvability of the Halting Problem]99\ollabel{thm:halting-problem} The halting problem is unsolvable, i.e.,100the function~$h$ is not Turing computable.101\end{thm}102103\begin{proof}104Suppose $h$ were Turing computable, say, by a Turing machine~$H$. We105could use $H$ to build a Turing machine that computes~$s$: First, make106a copy of the input (separated by a~$\TMblank$ symbol). Then move back107to the beginning, and run~$H$. We can clearly make a machine that108does the former (see \cref{tur:mac:dis:prob:copier}), and if $H$109existed, we would be able to ``hook it up'' to such a copier machine110to get a new machine which would determine if $M_e$ halts on111input~$e$, i.e., computes~$s$. But we've already shown that no such112machine can exist. Hence, $h$~is also not Turing computable.113\end{proof}114115\begin{prob}116The Three Halting (3-Halt) problem is the problem of giving a decision117procedure to determine whether or not an arbitrarily chosen Turing118Machine halts for an input of three $\TMstroke$'s on an otherwise119blank tape. Prove that the 3-Halt problem is unsolvable.120\end{prob}121122\begin{prob}123Show that if the halting problem is solvable for Turing machine and124input pairs $M_e$ and~$n$ where $e \neq n$, then it is also solvable125for the cases where $e = n$.126\end{prob}127128\begin{prob}129We proved that the halting problem is unsolvable if the input is a130number~$e$, which identifies a Turing machine~$M_e$ via an enumeration131of all Turing machines. What if we allow the description of Turing132machines from \olref[tur][und][enu]{sec} directly as input? Can there133be a Turing machine which decides the halting problem but takes as134input descriptions of Turing machines rather than indices? Explain135why or why not.136\end{prob}137138\begin{prob} Show that the \emph{partial} function~$s'$ is defined as139 \[140 s'(e) =141 \begin{cases}142 \text{1} & \text{if machine~$M_e$ halts for input $e$}\\143 \text{undefined} & \text{if machine~$M_e$ does not halt for input $e$}144 \end{cases}145 \]146 \emph{is} Turing computable.147\end{prob}148149\end{document}
content/turing-machines/undecidability/decision-problem.tex
1% Part: turing-machines2% Chapter: undecidability3% Section: unsolvability-decision-problem45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{und}{dec}10\olsection{The Decision Problem}1112We say that first-order logic is \emph{decidable} iff there is an13effective method for determining whether or not a given !!{sentence}14is valid. As it turns out, there is no such method: the problem of15deciding validity of first-order sentences is unsolvable.1617In order to establish this important negative result, we prove that18the decision problem cannot be solved by a Turing machine. That is,19we show that there is no Turing machine which, whenever it is started20on a tape that contains a first-order !!{sentence}, eventually halts21and outputs either $1$ or~$0$ depending on whether the22!!{sentence} is valid or not. By the Church--Turing thesis, every23function which is computable is Turing computable. So if this24``validity function'' were effectively computable at all, it would be25Turing computable. If it isn't Turing computable, then, it also cannot26be effectively computable.2728Our strategy for proving that the decision problem is unsolvable is to29reduce the halting problem to it. This means the following: We have30proved that the function~$h(e,w)$ that halts with output~$1$ if the31Turing machine described by~$e$ halts on input~$w$ and outputs~$0$32otherwise, is not Turing computable. We will show that if there were33a Turing machine that decides validity of first-order sentences, then34there is also Turing machine that computes~$h$. Since $h$ cannot be35computed by a Turing machine, there cannot be a Turing machine that36decides validity either.3738The first step in this strategy is to show that for every input~$w$39and a Turing machine~$M$, we can effectively describe !!a{sentence}40$!T(M, w)$ representing the instruction set of~$M$ and the input~$w$41and !!a{sentence}~$!E(M, w)$ expressing ``$M$ eventually halts'' such42that:43\begin{quote}44 $\Entails !T(M, w) \lif !E(M,w)$ iff $M$ halts for input~$w$.45\end{quote}46The bulk of our proof will consist in describing these sentences47$!T(M, w)$ and~$!E(M, w)$ and in verifying that $!T(M, w) \lif !E(M, w)$48is valid iff $M$~halts on input~$w$.4950\end{document}
content/turing-machines/undecidability/representing-tms.tex
1% Part: turing-machines2% Chapter: undecidability3% Section: representing-tms45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{und}{rep}10\olsection{Representing Turing Machines}1112\begin{explain}13In order to represent Turing machines and their behavior by !!a{sentence}14of first-order logic, we have to define a suitable language. The15language consists of two parts: !!{predicate}s for describing16configurations of the machine, and expressions for numbering execution17steps (``moments'') and positions on the tape.1819We introduce two kinds of !!{predicate}s, both of them 2-place: For20each state~$q$, !!a{predicate}~$\Obj Q_q$, and for each tape21symbol~$\sigma$, !!a{predicate}~$\Obj S_\sigma$. The former allow us22to describe the state of~$M$ and the position of its tape head, the23latter allow us to describe the contents of the tape. 2425In order to express the positions of the tape head and the number of26steps executed, we need a way to express numbers. This is done using27!!a{constant}~$\Obj 0$, and a $1$-place function~$\prime$, the28successor function. By convention it is written \emph{after} its29argument (and we leave out the parentheses).3031For each number $n$ there is a canonical term~$\num{n}$, the32\emph{numeral} for~$n$, which represents it in~$\Lang L_M$. $\num{0}$33is $\Obj 0$, $\num{1}$ is $\Obj 0'$, $\num{2}$ is $\Obj 0''$, and so34on. More formally:35\begin{align*}36\num{0} & = \Obj 0 \\37\num{n+1} &= \num{n}'38\end{align*}39The term $\num{0}$, i.e., $\Obj 0$ names the leftmost position on the40tape as well as the time before the first execution step (the initial41configuration). The term $\num{1}$, i.e., $\Obj 0'$ names the square42to the right of the leftmost square, and the time after the first43execution step, and so on.4445We also introduce !!a{predicate}~$<$ to express both the ordering of46tape positions (when it means ``to the left of'') and execution steps47(then it means ``before'').4849Once we have the language in place, we list the ``axioms'' of $!T(M,50w)$, i.e., the !!{sentence}s which, taken together, describe the51behavior of~$M$ when run on input~$w$. There will be !!{sentence}s52which lay down conditions on $\Obj 0$, $\prime$, and $<$,53!!{sentence}s that describes the input configuration, and54!!{sentence}s that describe what the configuration of $M$ is after it55executes a particular instruction.56\end{explain}5758\begin{defn}59 \ollabel{defn:tm-descr}60Given a Turing machine $M = \tuple{Q, \Sigma, q_0, \delta}$, the61language~$\Lang L_M$ consists of:62\begin{enumerate}63\item A two-place !!{predicate} $\Obj Q_q(x, y)$ for every state~$q \in64 Q$. Intuitively, $\Obj Q_q(\num{m}, \num{n})$ expresses ``after $n$65 steps, $M$ is in state~$q$ scanning the $m$th square.''66\item A two-place !!{predicate} $\Obj S_\sigma(x, y)$ for every67 symbol~$\sigma\in \Sigma$. Intuitively, $\Obj S_\sigma(\num{m},68 \num{n})$ expresses ``after $n$ steps, the $m$th square contains69 symbol~$\sigma$.''70\item A !!{constant} $\Obj 0$71\item A one-place !!{function} $\prime$72\item A two-place !!{predicate} $<$73\end{enumerate}74\end{defn}7576The !!{sentence}s describing the operation of the Turing machine~$M$ on77input $w = \sigma_{i_1}\dots\sigma_{i_k}$ are the following:78\begin{enumerate}79\item Axioms describing numbers and~$<$:80\begin{enumerate}81\item !!^a{sentence} that says that every number is less than its successor:82\[83\lforall[x][x < x']84\]85\item !!^a{sentence} that ensures that $<$ is transitive:86\[87\lforall[x][\lforall[y][\lforall[z][88 ((x < y \land y < z) \lif x < z)]]]89\]90\end{enumerate}91\item Axioms describing the input configuration:92\begin{enumerate}93\item After $0$~steps---before the machine starts---$M$ is in94 the initial state~$q_0$, scanning square~$1$:95\[96\Obj Q_{q_0}(\num{1}, \num{0})97\]98\item The first $k+1$ squares contain the symbols $\TMendtape$,99 $\sigma_{i_1}$, \dots, $\sigma_{i_k}$:100\[101\Obj S_\TMendtape(\num{0}, \num{0}) \land102\Obj S_{\sigma_{i_1}}(\num{1}, \num{0}) \land103\dots \land104\Obj S_{\sigma_{i_k}}(\num{k}, \num{0})105\]106\item Otherwise, the tape is empty:107\[108\lforall[x][(\num{k} < x \lif \Obj S_\TMblank(x, \num{0}))]109\]110\end{enumerate}111\item Axioms describing the transition from one configuration to112 the next:113114For the following, let $!A(x, y)$ be the conjunction of all !!{sentence}s115of the form116\[117\lforall[z][118 (((z < x \lor x < z) \land \Obj S_\sigma(z, y))119 \lif \Obj S_\sigma(z, y'))]120\]121where $\sigma \in \Sigma$. We use $!A(\num{m},\num{n})$ to express122``other than at square~$m$, the tape after $n+1$ steps is the same as123after $n$ steps.''124\begin{enumerate}125\item \ollabel{rep-right} For every instruction $\delta(q_i, \sigma) =126 \tuple{q_j, \sigma', \TMright}$, the !!{sentence}:127\begin{align*}128& \lforall[x][\lforall[y][(129 (\Obj Q_{q_i}(x, y) \land \Obj S_{\sigma}(x, y)) \lif {}]] \\130&\qquad (\Obj Q_{q_j}(x', y') \land \Obj S_{\sigma'}(x, y') \land131!A(x, y)))132\end{align*}133This says that if, after~$y$ steps, the machine is in state~$q_i$134scanning square~$x$ which contains symbol~$\sigma$, then after $y+1$135steps it is scanning square~$x+1$, is in state~$q_j$, square~$x$ now136contains~$\sigma'$, and every square other than~$x$ contains the137same symbol as it did after~$y$ steps.138139\item \ollabel{rep-left} For every instruction $\delta(q_i, \sigma) =140 \tuple{q_j, \sigma', \TMleft}$, the !!{sentence}:141\begin{align*}142& \lforall[x][\lforall[y][143 ((\Obj Q_{q_i}(x', y) \land \Obj S_{\sigma}(x', y)) \lif {}]]\\144& \qquad (\Obj Q_{q_j}(x, y') \land \Obj S_{\sigma'}(x', y') \land145!A(x, y))) \land {}\\146& \lforall[y][((\Obj Q_{q_i}(\num{0}, y) \land \Obj S_{\sigma}(\num{0},147 y)) \lif {}]\\148& \qquad (\Obj Q_{q_j}(\num{0}, y') \land \Obj S_{\sigma'}(\num{0},149 y') \land !A(\num{0}, y)))150\end{align*}151Take a moment to think about how this works: now we don't start with152``if scanning square~$x$ \dots'' but: ``if scanning square $x+1$153\dots'' A move to the left means that in the next step the machine is154scanning square~$x$. But the square that is written on is~$x+1$. We155do it this way since we don't have subtraction or a predecessor156function.157158Note that numbers of the form $x+1$ are $1$, $2$, \dots, i.e., this159doesn't cover the case where the machine is scanning square~$0$ and is160supposed to move left (which of course it can't---it just stays161put). That special case is covered by the second conjunction: it says162that if, after $y$ steps, the machine is scanning square~$0$ in state163$q_i$ and square~$0$ contains symbol~$\sigma$, then after $y+1$ steps164it's still scanning square~$0$, is now in state~$q_j$, the symbol on165square~$0$ is $\sigma'$, and the squares other than square~$0$ contain166the same symbols they contained ofter $y$~steps.167\item \ollabel{rep-stay} For every instruction $\delta(q_i, \sigma) =168 \tuple{q_j, \sigma', \TMstay}$, the !!{sentence}:169\begin{align*}170& \lforall[x][\lforall[y][(171 (\Obj Q_{q_i}(x, y) \land \Obj S_{\sigma}(x, y)) \lif {}]] \\172&\qquad (\Obj Q_{q_j}(x, y') \land \Obj S_{\sigma'}(x, y') \land173!A(x, y)))174\end{align*}175\end{enumerate}176\end{enumerate}177Let $!T(M, w)$ be the conjunction of all the above !!{sentence}s for Turing178machine~$M$ and input~$w$.179180In order to express that~$M$ eventually halts, we have to find a181!!{sentence} that says ``after some number of steps, the transition182function will be undefined.'' Let $X$~be the set of all pairs183$\tuple{q, \sigma}$ such that~$\delta(q, \sigma)$ is undefined. Let184$!E(M, w)$ then be the !!{sentence}185\[186\lexists[x][\lexists[y][(\bigvee_{\tuple{q, \sigma} \in187 X}(\Obj Q_q(x, y) \land \Obj S_\sigma(x, y)))]]188\]189190If we use a Turing machine with a designated halting state~$h$, it191is even easier: then the !!{sentence}~$!E(M, w)$192\[193\lexists[x][\lexists[y][\Obj Q_h(x, y)]]194\]195expresses that the machine eventually halts.196197\begin{prop}198\ollabel{prop:mlessk}199If $m < k$, then $!T(M, w) \Entails \num{m} < \num{k}$200\end{prop}201202\begin{proof}203Exercise.204\end{proof}205206\begin{prob}207Prove \olref[tur][und][rep]{prop:mlessk}.208(Hint: use induction on $k-m$).209\end{prob}210211\end{document}
content/turing-machines/undecidability/verification.tex
1% Part: turing-machines 2% Chapter: undecidability 3% Section: verification45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{und}{ver} 10\olsection{Verifying the Representation}1112\begin{explain}13In order to verify that our representation works, we have to prove two14things. First, we have to show that if $M$ halts on input~$w$, then15$!T(M, w) \lif !E(M, w)$ is valid. Then, we have to show the converse,16i.e., that if $!T(M, w) \lif !E(M, w)$ is valid, then $M$ does in fact17eventually halt when run on input~$w$.1819The strategy for proving these is very different. For the first20result, we have to show that !!a{sentence} of first-order logic21(namely, $!T(M, w) \lif !E(M, w)$) is valid. The easiest way to do22this is to give !!a{derivation}. Our proof is supposed to work for all23$M$ and $w$, though, so there isn't really a single !!{sentence} for24which we have to give !!a{derivation}, but infinitely many. So the best25we can do is to prove by induction that, whatever $M$ and~$w$ look26like, and however many steps it takes $M$ to halt on input~$w$, there27will be !!a{derivation} of $!T(M, w) \lif !E(M, w)$.2829Naturally, our induction will proceed on the number of steps $M$ takes30before it reaches a halting configuration. In our inductive proof,31we'll establish that for each step~$n$ of the run of $M$ on input~$w$,32$!T(M, w) \Entails !C(M, w, n)$, where $!C(M, w, n)$ correctly33describes the configuration of $M$ run on~$w$ after~$n$ steps. Now if34$M$ halts on input~$w$ after, say, $n$ steps, $!C(M, w, n)$ will35describe a halting configuration. We'll also show that $!C(M, w, n)36\Entails !E(M, w)$, whenever $!C(M, w, n)$ describes a halting37configuration. So, if $M$ halts on input~$w$, then for some~$n$, $M$38will be in a halting configuration after $n$~steps. Hence, $!T(M, w)39\Entails !C(M, w, n)$ where $!C(M, w, n)$ describes a halting40configuration, and since in that case $!C(M, w, n) \Entails !E(M, w)$,41we get that $T(M, w) \Entails !E(M, w)$, i.e., that $\Entails !T(M, w)42\lif !E(M, w)$.4344The strategy for the converse is very different. Here we assume that45$\Entails !T(M, w) \lif !E(M, w)$ and have to prove that $M$ halts on46input~$w$. From the hypothesis we get that $!T(M, w) \Entails !E(M,47w)$, i.e., $!E(M, w)$ is true in every !!{structure} in which $!T(M,48w)$ is true. So we'll describe !!a{structure}~$\Struct{M}$ in which49$!T(M, w)$ is true: its domain will be $\Nat$, and the interpretation50of all the $\Obj Q_q$ and $\Obj S_\sigma$ will be given by the51configurations of~$M$ during a run on input~$w$. So, e.g.,52$\Sat{M}{\Obj Q_q(\num{m}, \num{n})}$ iff $T$, when run on input~$w$53for $n$ steps, is in state~$q$ and scanning square~$m$. Now since54$!T(M, w) \Entails !E(M, w)$ by hypothesis, and since $\Sat{M}{!T(M,55 w)}$ by construction, $\Sat{M}{!E(M, w)}$. But $\Sat{M}{!E(M, w)}$56iff there is some $n \in \Domain{M} = \Nat$ so that $M$, run on57input~$w$, is in a halting configuration after~$n$ steps.58\end{explain}5960\begin{defn} 61Let $!C(M, w, n)$ be the !!{sentence}62\[ 63\Obj Q_q(\num{m}, \num{n}) \land \Obj S_{\sigma_0}(\num{0}, \num{n})64\land \dots \land \Obj S_{\sigma_k}(\num{k}, \num{n}) \land65\lforall[x][(\num{k} < x \lif \Obj S_\TMblank(x, \num{n}))]66\] 67where $q$ is the state of $M$ at time~$n$, $M$ is scanning square~$m$68at time~$n$, square~$i$ contains symbol~$\sigma_i$ at time~$n$ for $069\le i \le k$ and $k$ is the right-most non-blank square of the tape at70time~$0$, or the right-most square the tape head has visited after $n$71steps, whichever is greater.72\end{defn}7374\begin{lem}\ollabel{lem:halt-config-implies-halt}75If $M$ run on input~$w$ is in a halting configuration after $n$ steps,76then $!C(M, w, n) \Entails !E(M, w)$.77\end{lem}7879\begin{proof}80Suppose that $M$ halts for input~$w$ after $n$ steps.81There is some state~$q$, square~$m$, and symbol~$\sigma$ such that:82\begin{enumerate} 83\item After $n$ steps, $M$ is in state~$q$ scanning square~$m$ on84 which~$\sigma$ appears.85\item The transition function $\delta(q, \sigma)$ is undefined.86\end{enumerate}87$!C(M, w, n)$ is the description of this configuration and will88include the clauses $\Obj Q_{q}(\num{m}, \num{n})$ and $\Obj89S_{\sigma}(\num{m}, \num{n})$. These clauses together imply $!E(M,90w)$:91\[92\lexists[x][\lexists[y][(\bigvee_{\tuple{q, \sigma} \in93 X}(\Obj Q_q(x, y) \land \Obj S_\sigma(x, y)))]]94\]95since $\Obj Q_{q'}(\num{m}, \num{n}) \land S_{\sigma'}(\num{m},96\num{n}) \Entails \bigvee_{\tuple{q, \sigma} \in X} (\Obj Q_q(\num{m},97\num{n}) \land \Obj S_{\sigma}(\num{m}, \num{n}))$, as98$\tuple{q',\sigma'} \in X$. 99\end{proof}100101\begin{explain}102So if $M$ halts for input $w$, then there is some~$n$ such that $!C(M,103w, n) \Entails !E(M,w)$. We will now show that for any time~$n$,104$!T(M, w) \Entails !C(M, w, n)$.105\end{explain}106107\begin{lem}108\ollabel{lem:config}109For each $n$, if $M$ has not halted after $n$ steps, $!T(M, w)110\Entails !C(M, w, n)$.111\end{lem}112113\begin{proof}114Induction basis: If $n = 0$, then the conjuncts of $!C(M, w, 0)$ are115also conjuncts of $!T(M, w)$, so entailed by it.116117Inductive hypothesis: If $M$ has not halted before the $n$th step,118then $!T(M,w) \Entails !C(M, w, n)$. We have to show that (unless119$!C(M, w, n)$ describes a halting configuration), $!T(M, w) \Entails120!C(M, w, n+1)$.121122Suppose $n > 0$ and after $n$ steps, $M$ started on $w$ is in123state~$q$ scanning square~$m$. Since $M$ does not halt after~$n$124steps, there must be an instruction of one of the following three125forms in the program of~$M$:126127\begin{enumerate} 128\item \ollabel{right} $\delta(q, \sigma) = \tuple{q', \sigma', \TMright}$129130\item \ollabel{left} $\delta(q, \sigma) = \tuple{q', \sigma', \TMleft}$131132\item \ollabel{stay} $\delta(q, \sigma) = \tuple{q', \sigma', \TMstay}$133\end{enumerate}134135We will consider each of these three cases in turn. 136137\begin{enumerate} 138\item Suppose there is an instruction of the form~\olref{right}.139 By \olref[rep]{defn:tm-descr}\olref[rep]{rep-right}, this means that140\begin{align*} 141& \lforall[x][\lforall[y][((\Obj Q_{q}(x,142 y) \land \Obj S_\sigma(x, y)) \lif {}]]\\143& \qquad (\Obj Q_{q'}(x',144 y') \land \Obj S_{\sigma'}(x, y') \land !A(x, y))) 145\intertext{is a conjunct of $!T(M,w)$. This entails the following146 !!{sentence} (universal instantiation, $\num{m}$ for~$x$ and147 $\num{n}$ for~$y$):}148& (\Obj Q_{q}(\num{m}, \num{n}) \land \Obj S_{\sigma}(\num{m},149\num{n})) \lif {}\\150& \qquad (\Obj Q_{q'}(\num{m}', \num{n}') \land151\Obj S_{\sigma'}(\num{m}, \num{n}') \land !A(\num{m}, \num{n})).152\intertext{By induction hypothesis, $!T(M, w) \Entails !C(M, w, n)$,153 i.e.,}154& \Obj Q_q(\num{m}, \num{n}) \land \Obj S_{\sigma_0}(\num{0}, \num{n})155\land \dots \land \Obj S_{\sigma_k}(\num{k}, \num{n}) \land \\156& \qquad\lforall[x][(\num{k} < x \lif \Obj S_\TMblank(x, \num{n}))]\\157\intertext{Since after $n$ steps, tape square~$m$ contains~$\sigma$,158 the corresponding conjunct is~$\Obj S_\sigma(\num{m}, \num{n})$,159 so this entails:}160& \Obj Q_{q}(\num{m}, \num{n}) \land \Obj S_{\sigma}(\num{m},161 \num{n})162\intertext{We now get}163& \Obj Q_{q'}(\num{m}', \num{n}') \land \Obj S_{\sigma'}(\num{m},164 \num{n}') \land {}\\165& \qquad\Obj S_{\sigma_0}(\num{0}, \num{n}') \land \dots \land166 \Obj S_{\sigma_k}(\num{k}, \num{n}') \land {}\\167& \qquad \lforall[x][(\num{k} < x \lif \Obj S_\TMblank(x, \num{n}'))]168\end{align*}169as follows: The first line comes directly from the consequent of the170preceding conditional, by modus ponens. Each conjunct in the middle171line---which excludes $S_{\sigma_m}(\num{m},\num{n}')$---follows from172the corresponding conjunct in~$!C(M, w, n)$ together with $!A(\num{m},173\num{n})$.174175If $m < k$, $!T(M,w) \Proves \num{m} < \num {k}$176(\olref[rep]{prop:mlessk}) and by transitivity of~$<$, we have177$\lforall[x][(\num{k} < x \lif \num{m} < x)]$. If $m = k$, then178$\lforall[x][(\num{k} < x \lif \num{m} < x)]$ by logic alone. The179last line then follows from the corresponding conjunct in $!C(M, w,180n)$, $\lforall[x][(\num{k} < x \lif \num{m} < x)]$, and $!A(\num{m},181\num{n})$. If $m<k$, this already is $!C(M, w, n+1)$.182183Now suppose $m=k$. In that case, after $n+1$ steps, the tape head has184also visited square~$k+1$, which now is the right-most square185visited. So $!C(M, w, n+1)$ has a new conjunct, $\Obj186S_\TMblank(\num{k}',\num{n}')$, and the last conjunct is187$\lforall[x][(\num{k}' < x \lif \Obj S_\TMblank(x, \num{n}'))]$. We188have to verify that these two !!{sentence}s are also implied.189190We already have $\lforall[x][(\num{k} < x \lif \Obj S_\TMblank(x,191 \num{n}'))]$. In particular, this gives us $\num{k} < \num{k}' \lif192\Obj S_\TMblank(\num{k}', \num{n}')$. From the axiom $\lforall[x][x <193 x']$ we get $\num{k} < \num{k}'$. By modus ponens, $\Obj194S_\TMblank(\num{k}',\num{n}')$ follows.195196Also, since $!T(M,w) \Proves \num{k} < \num{k}'$, the axiom for197transitivity of~$<$ gives us $\lforall[x][(\num{k}' < x \lif \Obj198 S_\TMblank(x, \num{n}'))]$. (We leave the verification of this as an199exercise.)200201\item Suppose there is an instruction of the form~\olref{left}.202 Then, by \olref[rep]{defn:tm-descr}\olref[rep]{rep-left},203\begin{align*} 204& \lforall[x][\lforall[y][((\Obj Q_{q}(x', y) \land \Obj205 S_{\sigma}(x', y)) \lif {}]]\\206& \qquad (\Obj Q_{q'}(x, y') \land \Obj207 S_{\sigma'}(x', y') \land !A(x, y))) \land {}\\208& \lforall[y][((\Obj Q_{q_i}(\num{0}, y) \land \Obj S_{\sigma}(\num{0},209 y)) \lif {}]\\210& \qquad (\Obj Q_{q_j}(\num{0}, y') \land \Obj S_{\sigma'}(\num{0}, y')211 \land !A(\num{0}, y)))212\intertext{is a conjunct of $!T(M,w)$. If $m>0$, then let $l = m - 1$213 (i.e., $m = l+1$). The first conjunct of the above !!{sentence}214 entails the following:}215& (\Obj Q_{q}(\num{l}', \num{n}) \land \Obj S_{\sigma}(\num{l}', \num{n}))216\lif {} \\217& \qquad (\Obj Q_{q'}(\num{l}, \num{n}') \land \Obj S_{\sigma'}(\num{l}',218\num{n}') \land !A(\num{l}, \num{n}))219\intertext{Otherwise, let $l = m = 0$ and consider the following !!{sentence}220 entailed by the second conjunct:}221& ((\Obj Q_{q_i}(\num{0}, \num{n}) \land \Obj S_{\sigma}(\num{0}, \num{n})) \lif {}\\222& \qquad (\Obj Q_{q_j}(\num{0}, \num{n}') \land \Obj S_{\sigma'}(\num{0}, \num{n}') \land223!A(\num{0}, \num{n}))) 224\intertext{Either sentence implies}225& \Obj Q_{q'}(\num{l}, \num{n}') \land \Obj S_{\sigma'}(\num{m},226 \num{n}') \land {}\\227&\qquad \Obj S_{\sigma_0}(\num{0}, \num{n}')\land \dots \land228 \Obj S_{\sigma_k}(\num{k}, \num{n}') \land {} \\229& \qquad \lforall[x][(\num{k} < x230 \lif \Obj S_\TMblank(x, \num{n}'))]231\end{align*}232as before. (Note that in the first case, $\num{l}' \ident \num{l+1}233\ident \num{m}$ and in the second case $\num{l} \ident \num{0}$.) But234this just is $!C(M, w, n+1)$.235236\item Case \olref{stay} is left as an exercise.237\end{enumerate}238We have shown that for any~$n$, $!T(M, w) \Entails !C(M, w, n)$.239\end{proof}240241\begin{prob}242Complete case~\olref[tur][und][ver]{stay} of the proof of243\olref[tur][und][ver]{lem:config}.244\end{prob}245246\begin{prob}247Give !!a{derivation} of $\Obj S_{\sigma_i}(\num{i}, \num{n}')$ from248$\Obj S_{\sigma_i}(\num{i}, \num{n})$ and $!A(m, n)$ (assuming $i \neq249m$, i.e., either $i < m$ or $m < i$).250\end{prob}251252\begin{prob}253Give !!a{derivation} of $\lforall[x][(\num{k}' < x \lif \Obj254 S_\TMblank(x, \num{n}'))]$ from $\lforall[x][(\num{k} < x \lif \Obj255 S_\TMblank(x, \num{n}'))]$, $\lforall[x][x < x']$, and256$\lforall[x][\lforall[y][\lforall[z][ ((x < y \land y < z) \lif x <257 z)]]]$.)258\end{prob}259260261\begin{lem}262\ollabel{lem:valid-if-halt}263If $M$ halts on input~$w$, then $!T(M, w) \lif264!E(M, w)$ is valid.265\end{lem}266267\begin{proof}268By \olref{lem:config}, we know that, for any time~$n$, the269description~$!C(M, w, n)$ of the configuration of $M$ at time~$n$ is270entailed by~$!T(M, w)$. Suppose $M$ halts after $k$ steps. At that271point, it will be scanning square~$m$, for some~$m \in \Nat$. Then272$!C(M, w, k)$ describes a halting configuration of~$M$, i.e., it273contains as conjuncts both $\Obj Q_q(\num{m}, \num{k})$ and $\Obj274S_\sigma(\num{m}, \num{k})$ with $\delta(q,\sigma)$ undefined. Thus,275by \olref{lem:halt-config-implies-halt}, $!C(M, w, k) \Entails !E(M,276w)$. But since $!T(M, w) \Entails !C(M, w, k)$, we have $!T(M, w)277\Entails !E(M, w)$ and therefore $!T(M, w) \lif !E(M, w)$ is valid.278\end{proof}279280281\begin{explain} 282To complete the verification of our claim, we also have to283establish the reverse direction: if $!T(M, w) \lif !E(M, w)$ is valid, then284$M$ does in fact halt when started on input~$w$. 285\end{explain}286287\begin{lem}288\ollabel{lem:halt-if-valid}289If $\Entails !T(M, w) \lif !E(M, w)$, then $M$ halts on input~$w$.290\end{lem}291292\begin{proof}293Consider the $\Lang L_M$-!!{structure}~$\Struct M$ with294domain~$\Nat$ which interprets $\Obj 0$ as~$0$, $\prime$~as the successor295function, and $<$~as the less-than relation, and the predicates $\Obj Q_q$296and~$\Obj S_\sigma$ as follows:297\begin{align*}298 \Assign{\Obj Q_q}{M} & =299\Setabs{\tuple{m, n}}{\begin{array}{ll}\text{started on~$w$, after~$n$ steps,}\\ \text{$M$ is in state $q$300 scanning square~$m$}\end{array}} \\301\Assign{\Obj S_\sigma}{M} & = \Setabs{\tuple{m, n}}{\begin{array}{ll}302\text{started on~$w$, after $n$ steps,}\\ \text{square~$m$ of $M$ contains303 symbol~$\sigma$}\end{array}}304\end{align*}305In other words, we construct the !!{structure}~$\Struct{M}$ so that it306describes what $M$ started on input~$w$ actually does, step by step.307Clearly, $\Sat{M}{!T(M, w)}$. If $\Entails !T(M, w) \lif !E(M, w)$,308then also $\Sat{M}{!E(M, w)}$, i.e.,309\[310\Sat{M}{\lexists[x][\lexists[y][(\bigvee_{\tuple{q, \sigma} \in311 X}(\Obj Q_q(x, y) \land \Obj S_\sigma(x, y)))]]}.312\]313As $\Domain{M} = \Nat$, there must be $m$, $n \in \Nat$ so that314$\Sat{M}{\Obj Q_q(\num{m}, \num{n}) \land \Obj S_\sigma(\num{m},315\num{n})}$ for some~$q$ and~$\sigma$ such that $\delta(q, \sigma)$ is316undefined. By the definition of~$\Struct M$, this means that $M$317started on input~$w$ after~$n$ steps is in state~$q$ and reading318symbol~$\sigma$, and the transition function is undefined, i.e.,319$M$~has halted.320\end{proof}321322\end{document}
content/turing-machines/undecidability/unsolvability-decision-problem.tex
1% Part: turing-machines 2% Chapter: undecidability 3% Section: unsolvability-decision-problem45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{und}{uns} 10\olsection{The Decision Problem is Unsolvable}1112\begin{thm}13\ollabel{thm:decision-prob}14The decision problem is unsolvable: There is no Turing machine~$D$,15which when started on a tape that contains !!a{sentence}~$!B$ of16first-order logic as input, $D$~eventually halts, and outputs~$1$ iff17$!B$ is valid and $0$ otherwise.18\end{thm}1920\begin{proof}21Suppose the decision problem were solvable, i.e., suppose there were a22Turing machine~$D$. Then we could solve the halting problem as23follows. We construct a Turing machine~$E$ that, given as input the24number~$e$ of Turing machine~$M_e$ and input~$w$, computes the25corresponding !!{sentence}~$!T(M_e, w) \lif !E(M_e, w)$ and halts,26scanning the leftmost square on the tape. The machine $E \concat D$27would then, given input $e$ and $w$, first compute~$!T(M_e, w) \lif28!E(M_e, w)$ and then run the decision problem machine~$D$ on that29input. $D$ halts with output~$1$ iff $!T(M_e, w) \lif !E(M_e, w)$ is30valid and outputs~$0$ otherwise. By \olref[ver]{lem:halt-if-valid} and31\olref[ver]{lem:valid-if-halt}, $!T(M_e, w) \lif !E(M_e, w)$ is valid32iff $M_e$ halts on input~$w$. Thus, $E\concat D$, given input $e$ and33$w$ halts with output~$1$ iff $M_e$ halts on input~$w$ and halts with34output~$0$ otherwise. In other words, $E \concat D$ would solve the35halting problem. But we know, by \olref[hal]{thm:halting-problem},36that no such Turing machine can exist.37\end{proof}3839\begin{cor}\ollabel{cor:undecidable-sat}%40It is undecidable if an arbitrary !!{sentence} of first-order logic is satisfiable.41\end{cor}4243\begin{proof}44 Suppose satisfiability were decidable by a Turing machine~$S$. Then45 we could solve the decision problem as follows: Given46 !!a{sentence}~$B$ as input, move $!B$ to the right one square.47 Return to square~$1$ and write the symbol~$\lnot$.4849 Now run the Turing machine~$S$. It eventually halts with output50 either $1$ (if $\lnot !B$ is satisfiable) or~$0$ (if $\lnot !B$ is51 unsatisfiable) on the tape. If there is a~$\TMstroke$ on square~$1$,52 erase it; if square~$1$ is empty, write a~$\TMstroke$, then halt.5354 This Turing machine always halts, and its output is~$1$ iff $\lnot55 !B$ is unsatisfiable and $0$~otherwise. Since $!B$ is valid iff56 $\lnot !B$~is unsatisfiable, the machine outputs~$1$ iff $!B$ is57 valid, and $0$~otherwise, i.e., it would solve the decision problem.58\end{proof}5960\begin{explain}61So there is no Turing machine which always gives a62correct ``yes'' or ``no'' answer to the question ``Is $!B$ a valid63!!{sentence} of first-order logic?'' However, there \emph{is} a Turing64machine that always gives a correct ``yes'' answer---but simply does65not halt if the answer is ``no.'' This follows from the soundness and66completeness theorem of first-order logic, and the fact that67!!{derivation}s can be effectively enumerated.68\end{explain}6970\begin{thm}71 \ollabel{thm:valid-ce}%72 Validity of first-order !!{sentence}s is semi-decidable: There is a73 Turing machine~$E$, which when started on a tape that contains74 !!a{sentence}~$!B$ of first-order logic as input, $E$~eventually75 halts and outputs~$1$ iff $!B$ is valid, but does not halt76 otherwise.77\end{thm}7879\begin{proof}80 All possible !!{derivation}s of first-order logic can be generated,81 one after another, by an effective algorithm. The machine~$E$ does82 this, and when it finds !!a{derivation} that shows that $\Proves83 !B$, it halts with output~$1$. By the soundness theorem, if $E$84 halts with output~$1$, it's because~$\Entails !B$. By the85 completeness theorem, if $\Entails !B$ there is !!a{derivation} that86 shows that~$\Proves !B$. Since $E$ systematically generates all87 possible !!{derivation}s, it will eventually find one that88 shows~$\Proves !B$, so will eventually halt with output~$1$.89\end{proof}9091\end{document}
content/turing-machines/undecidability/trakhtenbrot.tex
1% Part: turing-machines2% Chapter: undecidability3% Section: trakhtenbrot45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{und}{tra}10\olsection{Trakhtenbrot's Theorem}1112\begin{explain}13 In \olref[rep]{sec} we defined !!{sentence}s $!T(M,w)$ and~$!E(M,w)$14 for a Turing machine~$M$ and input string~$w$. Then we showed in15 \olref[ver]{lem:valid-if-halt} and \olref[ver]{lem:halt-if-valid}16 that $!T(M,w) \lif !E(M,w)$ is valid iff $M$, started on input~$w$,17 eventually halts. Since the Halting Problem is undecidable, this18 implies that validity and satisfiability of !!{sentence}s of19 first-order logic is undecidable20 (\cref{tur:und:uns:thm:decision-prob,tur:und:uns:cor:undecidable-sat}).2122 But validity and satisfiability of sentences is defined for23 arbitrary !!{structure}s, finite or infinite. You might suspect that24 it is easier to decide if !!a{sentence} is satisfiable in a finite25 !!{structure} (or valid in all finite !!{structure}s). We can adapt26 the proof of the unsolvability of the decision problem so that it27 shows this is not the case.2829 First, if you go back to the proof of30 \olref[ver]{lem:halt-if-valid}, you'll see that what we did there is31 produce a model~$\Struct M$ of~$!T(M,w)$ which describes exactly32 what machine~$M$ does when started on input~$w$. The domain of that33 model was~$\Nat$, i.e., infinite. But if $M$ actually halts on34 input~$w$, we can build a finite model~$\Struct M'$ in the same way.35 Suppose $M$ started on input~$w$ halts after~$k$ steps. Take as36 domain~$\Domain{M'}$ the set $\{0, \dots, n\}$, where $n$ is the37 larger of~$k$ and the length of~$w$, and let38 \[39 \Assign{\prime}{M'}(x) = 40 \begin{cases}41 x + 1 &\text{if $x < n$}\\42 n &\text{otherwise,}43 \end{cases}44 \]45 and $\tuple{x,y} \in \Assign{<}{M'}$ iff $x < y$ or $x = y = n$.46 Otherwise $\Struct{M'}$ is defined just like~$\Struct{M}$. By the47 definition of~$\Struct{M'}$, just like in the proof of48 \olref[ver]{lem:halt-if-valid}, $\Sat{M'}{!T(M,w)}$. And since we49 assumed that $M$ halts on input~$w$, $\Sat{M'}{!E(M,w)}$. So,50 $\Struct{M'}$ is a finite model of~$!T(M,w) \land !E(M,w)$ (note51 that we've replaced $\lif$ with~$\land$).52 53 We are halfway to a proof: we've shown that if $M$ halts on54 input~$w$, then $!T(M,w) \land !E(M,w)$ has a finite model.55 Unfortunately, the converse of this does not hold, i.e., there are56 Turing machines that don't halt on some input~$w$, but $!T(M,w)57 \land !E(M,w)$ still has a finite model. For instance, consider the58 machine~$M$ with the single state $q_0$ and instruction59 $\delta(q_0,\TMblank) = \tuple{q_0,\TMblank,\TMstay}$. Started on60 empty input~$w = \emptyseq$, this machine never halts: it is in an61 infinite loop, but does not change the tape or move the head. All62 configurations are the same (same state, same head position, same63 tape contents). We can define a finite !!{structure}~$\Struct{M''}$64 that satisfies $!T(M,\emptyseq) \land !E(M,\emptyseq)$ (exercise).65 We can, however, change~$!T(M,w)$ in a suitable way so that such66 !!{structure}s are ruled out.6768 \begin{prob}69 Let $M$ be a Turing machine with the single state $q_0$ and single70 instruction $\delta(q_0,\TMblank) = \tuple{q,\TMblank,\TMstay}$.71 Let $\Domain{M''} = \{0, 1, 2\}$, $\Assign{\prime}{M''}(0) =72 \Assign{\prime}{M'}(1) = 1$ and $\Assign{\prime}{M''}(2) = 2$, and73 $\Assign{<}{M''} = \{\tuple{0,1}, \tuple{1,1}, \tuple{2,2}\}$.74 Define $\Assign{\Obj Q_{q_0}}{M''}$, $\Assign{\Obj75 S_{\TMblank}}{M''}$, and $\Assign{\Obj S_{\TMendtape}}{M''}$ so76 that $!T(M,\emptyseq)$ and $!E(M,\emptyseq)$ become true and77 explain why they are. Hint: Observe that $\delta(q_0, \TMendtape)$78 is undefined. Ensure that79 \begin{align*}80 & \Obj Q_{q_0}(\num{1}, \num{n}) \land \Obj S_{\TMendtape}(\num{0}, \num{n})81 \land 82 \lforall[x][(\num{0} < x \lif \Obj S_\TMblank(x, \num{n}))] \text{\quad for all $n \in \Nat$}\\83 & \lexists[y][(\Obj Q_{q_0}(\num{0}, y) \land \Obj S_{\TMendtape}(\num{0}, y))]84 \end{align*}85 are both true in~$\Struct{M''}$.86 \end{prob}87\end{explain}8889Consider the !!{sentence}s describing the operation of the Turing90machine~$M$ on input $w = \sigma_{i_1}\dots\sigma_{i_k}$:91\begin{enumerate}92\item Axioms describing numbers and~$<$ (just like in the definition93of~$!T(M,w)$ in \olref[rep]{sec}).94\item Axioms describing the input configuration: just like in the definition95of~$!T(M,w)$.96\item Axioms describing the transition from one configuration to97 the next:9899For the following, let $!A(x, y)$ be as before, and let100\[101 !B(y) \ident \lforall[x][(x < y \lif \eq/[x][y])].102\]103\begin{enumerate}104\item \ollabel{rep-right} For every instruction $\delta(q_i, \sigma) =105 \tuple{q_j, \sigma', \TMright}$, the !!{sentence}:106\begin{align*}107& \lforall[x][\lforall[y][(108 (\Obj Q_{q_i}(x, y) \land \Obj S_{\sigma}(x, y)) \lif {}]] \\109&\qquad (\Obj Q_{q_j}(x', y') \land \Obj S_{\sigma'}(x, y') \land110!A(x, y) \land !B(y')))111\end{align*}112\item \ollabel{rep-left} For every instruction $\delta(q_i, \sigma) =113 \tuple{q_j, \sigma', \TMleft}$, the !!{sentence}114\begin{align*}115& \lforall[x][\lforall[y][116 ((\Obj Q_{q_i}(x', y) \land \Obj S_{\sigma}(x', y)) \lif {}]]\\117& \qquad (\Obj Q_{q_j}(x, y') \land \Obj S_{\sigma'}(x', y') \land118!A(x, y))) \land {}\\119& \lforall[y][((\Obj Q_{q_i}(\num{0}, y) \land \Obj S_{\sigma}(\num{0},120 y)) \lif {}]\\121& \qquad (\Obj Q_{q_j}(\num{0}, y') \land \Obj S_{\sigma'}(\num{0},122 y') \land !A(\num{0}, y) \land !B(y')))123\end{align*}124\item \ollabel{rep-stay} For every instruction $\delta(q_i, \sigma) =125 \tuple{q_j, \sigma', \TMstay}$, the !!{sentence}:126\begin{align*}127& \lforall[x][\lforall[y][(128 (\Obj Q_{q_i}(x, y) \land \Obj S_{\sigma}(x, y)) \lif {}]] \\129&\qquad (\Obj Q_{q_j}(x, y') \land \Obj S_{\sigma'}(x, y') \land130!A(x, y) \land !B(y')))131\end{align*}132\end{enumerate}133As you can see, the !!{sentence}s describing the transitions of~$M$ 134are the same as the corresponding !!{sentence}135in~$!T(M,w)$, except we add $!B(y')$ at the end. $!B(y')$~ensures136that the number $y'$ of the ``next'' configuration is different from137all previous numbers $\Obj 0$, $\Obj 0'$, \dots.138% \item Sentences that express consistency conditions:139% \begin{enumerate}140% \item\ollabel{rep-con-symb}%141% !!^a{sentence} that says that no square can have more than one142% symbol on it at any given time:143% \[\lforall[x][\lforall[y][(\Obj S_{\sigma}(x, y) \lif \lnot \Obj144% S_{\sigma'}(x, y))]],\]145% for every pair $\sigma \neq \sigma'$ of tape symbols of~$T$.146% \item\ollabel{rep-con-state}%147% !!^a{sentence} that says that $M$ cannot be in more than one state148% at any given time:149% \[\lforall[x][\lforall[y][(\Obj Q_{q}(x, y) \lif150% \lforall[z][\lnot \Obj151% Q_{q'}(z, y))]],\]152% for every pair $q \neq q'$ of states of~$T$.153% \item\ollabel{rep-con-tape}%154% !!^a{sentence} that says that $M$ cannot be on more than one tape155% square at any given time:156% \[\lforall[x][\lforall[y][(\Obj Q_{q}(x, y) \lif157% \lforall[z][\eq/[z][y]\lif \lnot \Obj158% Q_{q'}(z, y))]],\]159% for every pair $q$, $q'$ of states of~$T$.160% \end{enumerate}161\end{enumerate}162Let $!T'(M, w)$ be the conjunction of all the above !!{sentence}s for Turing163machine~$M$ and input~$w$.164165\begin{lem}\ollabel{lem:halts-sat}166 If $M$ started on input~$w$ halts, then $!T'(M,w) \land !E(M,w)$ has167 a finite model.168\end{lem}169170\begin{proof}171 Let $\Struct{M'}$ be as in the proof of172 \olref[ver]{lem:halt-if-valid}, except173 \begin{align*}174 \Domain{M'} & = \{0, \dots, n\},\\175 \Assign{\prime}{M'}(x) & = 176 \begin{cases}177 x + 1 &\text{if $x < n$}\\178 n &\text{otherwise,}179 \end{cases} \\180 \tuple{x,y} \in \Assign{<}{M'} &\text{iff $x < y$ or $x = y = n$,}181 \end{align*}182 where $n = \max(k,\len{w})$ and $k$~is the least number such that183 $M$ started on input~$w$ has halted after~$k$ steps. We leave the184 verification that $\Sat{M'}{!T'(M,w) \land E(M,w)}$ as an exercise.185\end{proof}186187\begin{prob}188 Complete the proof of \olref[tur][und][tra]{lem:halts-sat} by189 proving that $\Sat{M'}{!T(M,w) \land E(M,w)}$.190\end{prob}191192\begin{lem}\ollabel{lem:sat-halts}193 If $!T'(M,w) \land !E(M,w)$ has a finite model, then $M$ started on194 input~$w$ halts.195\end{lem}196197\begin{proof}198 We show the contrapositive. Suppose that $M$ started on~$w$ does not199 halt. If $!T'(M,w) \land !E(M,w)$ has no model at all, we are done.200 So assume $\Struct{M}$ is a model of~$!T(M,w) \land !E(M, w)$. We201 have to show that it cannot be finite.202 203 We can prove, just like in \olref[ver]{lem:config}, that if~$M$,204 started on input~$w$, has not halted after~$n$ steps, then $!T'(M,w)205 \Entails !C(M, w, n) \land !B(\num{n})$. Since $M$ started on206 input~$w$ does not halt, $!T'(M,w) \Entails !C(M, w, n) \land207 !B(\num{n})$ for all~$n \in \Nat$. Note that by208 \olref[rep]{prop:mlessk}, $!T'(M,w) \Entails \num{k} < \num{n}$ for209 all~$k < n$. Also $!B(\num{n}) \Entails \num{k} < \num{n} \lif210 \eq/[\num{k}][\num{n}]$. So, $\Sat{M}{\eq/[\num{k}][\num{n}]}$ for211 all~$k < n$, i.e., the infinitely many terms~$\num{k}$ must all have212 different values in~$\Struct{M}$. But this requires that213 $\Domain{M}$ be infinite, so $\Struct{M}$ cannot be a finite model214 of~$!T'(M,w) \land !E(M, w)$.215\end{proof}216217\begin{prob}218 Complete the proof of \olref[tur][und][tra]{lem:sat-halts} by219 proving that if~$M$, started on input~$w$, has not halted after~$n$220 steps, then $!T'(M,w) \Entails !B(\num{n})$.221\end{prob}222223\begin{thm}[Trakhtenbrot's Theorem]224 \ollabel{thm:trakhtenbrodt}225 It is undecidable if an arbitrary !!{sentence} of first-order logic has226 a finite model (i.e., is finitely satisfiable).227\end{thm}228229\begin{proof}230 Suppose there were a Turing machine~$F$ that decides the finite231 satisfiability problem. Then given any Turing machine~$M$ and232 input~$w$, we could compute the sentence~$!T'(M,w) \land !E(M,w)$,233 and use~$F$ to decide if it has a finite model. By234 \cref{tur:und:tra:lem:halts-sat,tur:und:tra:lem:sat-halts}, it does iff $M$235 started on input~$w$ halts. So we could use $F$ to solve the halting236 problem, which we know is unsolvable.237\end{proof}238239\begin{cor}\ollabel{cor:fproof-incomp}%240 There can be no !!{derivation} system that is sound and complete for241 \emph{finite} validity, i.e., !!a{derivation} system which has $\Proves !B$242 iff $\Sat{M}{!B}$ for every finite !!{structure}~$\Struct{M}$.243\end{cor}244245\begin{proof}246 Exercise.247\end{proof}248249\begin{prob}250 Prove \olref[tur][und][tra]{cor:fproof-incomp}. Observe that $!B$ is251 satisfied in every finite !!{structure} iff $\lnot !B$ is not252 finitely satisfiable. Explain why finite satisfiability is253 semi-decidable in the sense of254 \olref[tur][und][uns]{thm:valid-ce}. Use this to argue that if255 there were !!a{derivation} system for finite validity, then finite256 satisfiability would be decidable.257\end{prob}258259260\end{document}