content/turing-machines/machines-computations/machines-computations.tex
1% Part: turing-machines2% Chapter: machines-computations34\documentclass[../../../include/open-logic-chapter]{subfiles}56\begin{document}78\olchapter{tur}{mac}{Turing Machine Computations}910\olimport{introduction}1112\olimport{representing-tms}1314\olimport{turing-machines}1516\olimport{configuration}1718\olimport{unary-numbers}1920\olimport{halting-states}2122\olimport{disciplined-machines}2324\olimport{combining-machines}2526\olimport{variants}2728\olimport{church-turing-thesis}2930\OLEndChapterHook3132\end{document}
content/turing-machines/machines-computations/introduction.tex
1% Part: turing-machines 2% Chapter: machines-computations 3% Section: introduction45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{mac}{int}10\olsection{Introduction}1112What does it mean for a function, say, from $\Nat$ to $\Nat$ to be13\emph{computable}? Among the first answers, and the most well known14one, is that a function is computable if it can be computed by a15Turing machine. This notion was set out by Alan Turing in 1936.16Turing machines are an example of \emph{a model of computation}---they17are a mathematically precise way of defining the idea of a18``computational procedure.'' What exactly that means is debated, but19it is widely agreed that Turing machines are one way of specifying20computational procedures. Even though the term ``Turing machine''21evokes the image of a physical machine with moving parts, strictly22speaking a Turing machine is a purely mathematical construct, and as23such it idealizes the idea of a computational procedure. For24instance, we place no restriction on either the time or memory25requirements of a Turing machine: Turing machines can compute26something even if the computation would require more storage space or27more steps than there are atoms in the universe.2829\begin{explain}30It is perhaps best to think of a Turing31machine as a program for a special kind of imaginary mechanism. This32mechanism consists of a \emph{tape} and a \emph{read-write head}. In33our version of Turing machines, the tape is infinite in one direction34(to the right), and it is divided into \emph{squares}, each of which35may contain a symbol from a finite \emph{alphabet}. Such alphabets can36contain any number of different symbols, but we will mainly make do37with three: $\TMendtape$, $\TMblank$, and $\TMstroke$. When the38mechanism is started, the tape is empty (i.e., each square contains39the symbol $\TMblank$) except for the leftmost square, which contains40$\TMendtape$, and a finite number of squares which contain the41\emph{input}. At any time, the mechanism is in one of a finite number42of \emph{states}. At the outset, the head scans the leftmost square43and in a specified \emph{initial state}. At each step of the44mechanism's run, the content of the square currently scanned together45with the state the mechanism is in and the Turing machine program46determine what happens next. The Turing machine program is given by a47partial function which takes as input a state~$q$ and a48symbol~$\sigma$ and outputs a triple~$\tuple{q', \sigma',49 D}$. Whenever the mechanism is in state $q$ and reads symbol50$\sigma$, it replaces the symbol on the current square with $\sigma'$,51the head moves left, right, or stays put according to whether $D$ is52$\TMleft$, $\TMright$, or $\TMstay$, and the mechanism goes into53state~$q'$.5455For instance, consider the situation in \olref{fig:tm}.56\begin{figure}57 \olasset{\olpath/assets/diagrams/turing-machine.tikz}58 \caption{A Turing machine executing its program.}59 \ollabel{fig:tm}60\end{figure}61The visible part of the tape of the Turing machine contains the62end-of-tape symbol $\TMendtape$ on the leftmost square, followed by63three $1$'s, a $0$, and four more $1$'s. The64head is reading the third square from the left, which contains65a~$1$, and is in state~$q_1$---we say ``the machine is reading66a $1$ in state~$q_1$.'' If the program of the Turing machine67returns, for input $\tuple{q_1, 1}$, the triple $\tuple{q_2,680, \TMstay}$, the machine would now replace the~$1$ on69the third square with a~$0$, leave the read/write head where it70is, and switch to state~$q_2$. If then the program returns71$\tuple{q_3, 0, \TMright}$ for input $\tuple{q_2, 0}$,72the machine would now overwrite the~$0$ with another~$0$73(effectively, leaving the content of the tape under the read/write74head unchanged), move one square to the right, and enter state~$q_3$.75And so on.7677We say that the machine \emph{halts} when it encounters some state,78$q_n$, and symbol, $\sigma$ such that there is no instruction for79$\tuple{q_n, \sigma}$, i.e., the transition function for input80$\tuple{q_n,\sigma}$ is undefined. In other words, the machine has no81instruction to carry out, and at that point, it ceases82operation. Halting is sometimes represented by a specific halt83state~$h$. This will be demonstrated in more detail later on.84\end{explain}8586\begin{digress}87The beauty of Turing's paper, ``On computable numbers,'' is that he88presents not only a formal definition, but also an argument that the89definition captures the intuitive notion of computability.90From the definition, it should be clear that any function computable91by a Turing machine is computable in the intuitive sense. Turing92offers three types of argument that the converse is true, i.e., that93any function that we would naturally regard as computable is94computable by such a machine. They are (in Turing's words):95\begin{enumerate}96\item A direct appeal to intuition.97\item A proof of the equivalence of two definitions (in case the new98 definition has a greater intuitive appeal).99\item Giving examples of large classes of numbers which are100 computable.101\end{enumerate}102Our goal is to try to define the notion of computability ``in103principle,'' i.e., without taking into account practical limitations104of time and space. Of course, with the broadest definition of105computability in place, one can then go on to consider computation106with bounded resources; this forms the heart of the subject known as107``computational complexity.''108\end{digress}109110\begin{history}111Alan Turing invented Turing machines in 1936. While his interest at112the time was the decidability of first-order logic, the paper has been113described as a definitive paper on the foundations of computer114design. In the paper, Turing focuses on computable real numbers, i.e.,115real numbers whose decimal expansions are computable; but he notes116that it is not hard to adapt his notions to computable functions on117the natural numbers, and so on. Notice that this was a full five118years before the first working general purpose computer was built in1191941 (by the German Konrad Zuse in his parent's living room), seven120years before Turing and his colleagues at Bletchley Park built the121code-breaking Colossus (1943), nine years before the American ENIAC122(1945), twelve years before the first British general purpose123computer---the Manchester Small-Scale Experimental Machine---was built in124Manchester (1948), and thirteen years before the Americans first125tested the BINAC (1949). The Manchester SSEM has the distinction of126being the first stored-program computer---previous machines had to be127rewired by hand for each new task.128\end{history}129130\end{document}
content/turing-machines/machines-computations/representing-tms.tex
1% Part: turing-machines2% Chapter: machines-computations3% Section: representing-tms45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{mac}{rep}10\olsection{Representing Turing Machines}1112\begin{explain}13Turing machines can be represented visually by \emph{state diagrams}. 14The diagrams are composed of15state cells connected by arrows. Unsurprisingly, each state cell represents16a state of the machine. Each arrow represents an instruction that can be17carried out from that state, with the specifics of the instruction written above18or below the appropriate arrow. Consider the following machine, which has 19only two internal states, $q_0$ and $q_1$, and one instruction:20\[21\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,22 semithick]23 \tikzstyle{every state}=[fill=none,draw=black,text=black]2425 \node[initial,state] (A) {$q_0$};26 \node[state] (B) [right of=A] {$q_1$};2728 \path (A) edge node {\TMtrans{\TMblank}{\TMstroke}{\TMright}} (B);29\end{tikzpicture}30\]31Recall that the Turing machine has a read/write head and a tape with32the input written on it. The instruction can be read as \emph{if33reading a~$\TMblank$ in state $q_0$, write a~$\TMstroke$, move right,34and move to state $q_1$}. This is equivalent to the transition35function mapping $\tuple{q_0, \TMblank}$ to $\tuple{q_1, \TMstroke,36\TMright}$.37\end{explain}3839\begin{ex}40\emph{Even Machine}: The following Turing machine halts if, and only41if, there are an even number of $\TMstroke$'s on the tape (under the42assumption that all $\TMstroke$'s come before the first $\TMblank$ on43the tape).44\[45\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,46 semithick]47 \tikzstyle{every state}=[fill=none,draw=black,text=black]4849 \node[initial,state] (A) {$q_0$};50 \node[state] (B) [right of=A] {$q_1$};5152 \path (A) edge [bend left] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (B)53 (B) edge [loop above] node {\TMtrans{\TMblank}{\TMblank}{\TMright}} (B)54 edge [bend left] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (A);55\end{tikzpicture}56\]5758The state diagram corresponds to the following transition function:59\begin{align*}60\delta(q_0, \TMstroke) & = \tuple{q_1, \TMstroke, \TMright},\\61\delta(q_1, \TMstroke) & = \tuple{q_0, \TMstroke, \TMright},\\62\delta(q_1, \TMblank) & = \tuple{q_1, \TMblank, \TMright}63\end{align*}64\end{ex}6566\begin{explain}67The above machine halts only when the input is an even number of strokes.68Otherwise, the machine (theoretically) continues to operate indefinitely. 69For any machine and input, it is possible to trace through the 70\emph{configurations} of the machine in order to determine the output. 71We will give a formal definition of configurations later. For now,72we can intuitively think of configurations as a series of diagrams showing the 73state of the machine at any point in time during operation.74Configurations show the content of the tape, the state of the machine and the75location of the read/write head.7677Let us trace through the configurations of the even machine if it is78started with an input of four $\TMstroke$'s. In this case, we expect that the79machine will halt. We will then run the machine on an input of three80$\TMstroke$'s, where the machine will run forever.8182The machine starts in state~$q_0$, scanning the leftmost~$\TMstroke$.83We can represent the initial state of the machine as follows:84\[85\TMendtape \TMstroke_0 \TMstroke \TMstroke \TMstroke \TMblank \ldots86\]87The above configuration is straightforward. As can be seen, the88machine starts in state one, scanning the leftmost~$\TMstroke$. This89is represented by a subscript of the state name on the90first~$\TMstroke$. The applicable instruction at this point is91$\delta(q_0, \TMstroke) = \tuple{q_1, \TMstroke, \TMright}$, and so92the machine moves right on the tape and changes to state~$q_1$.93\[94\TMendtape \TMstroke \TMstroke_1 \TMstroke \TMstroke \TMblank \ldots95\]96Since the machine is now in state~$q_1$ scanning a~$\TMstroke$, we have to97``follow'' the instruction $\delta(q_1, \TMstroke) = \tuple{q_0,98 \TMstroke, \TMright}$. This results in the configuration99\[100\TMendtape \TMstroke \TMstroke \TMstroke_0 \TMstroke \TMblank \ldots101\]102As the machine continues, the rules are applied again in the same103order, resulting in the following two configurations:104\[105\TMendtape \TMstroke \TMstroke \TMstroke \TMstroke_1 \TMblank \ldots106\]107\[108\TMendtape \TMstroke \TMstroke \TMstroke \TMstroke \TMblank_0 \ldots109\]110The machine is now in state~$q_0$ scanning a~$\TMblank$. Based on the111transition diagram, we can easily see that there is no instruction to be112carried out, and thus the machine has halted. This means that the input113has been accepted.114115Suppose next we start the machine with an input of three116$\TMstroke$'s. The first few configurations are similar, as the same117instructions are carried out, with only a small difference of the tape118input:119\[120\TMendtape \TMstroke_0 \TMstroke \TMstroke \TMblank \ldots121\]122\[123\TMendtape \TMstroke \TMstroke_1 \TMstroke \TMblank \ldots124\]125\[126\TMendtape \TMstroke \TMstroke \TMstroke_0 \TMblank \ldots127\]128\[129\TMendtape \TMstroke \TMstroke \TMstroke \TMblank_1 \ldots130\]131The machine has now traversed past all the $\TMstroke$'s, and is132reading a~$\TMblank$ in state~$q_1$. As shown in the diagram, there is133an instruction of the form $\delta(q_1, \TMblank) =\tuple{q_1,134\TMblank, \TMright}$. Since the tape is filled with $\TMblank$135indefinitely to the right, the machine will continue to execute this136instruction \emph{forever}, staying in state~$q_1$ and moving ever137further to the right. The machine will never halt, and does not accept138the input.139\end{explain}140141\begin{explain}142It is important to note that not all machines will halt. If halting143means that the machine runs out of instructions to execute, then we144can create a machine that never halts simply by ensuring that there is145an outgoing arrow for each symbol at each state. The even machine can146be modified to run indefinitely by adding an instruction for scanning a147$\TMblank$ at~$q_0$.148\end{explain}149150\begin{ex}151\[152\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,153 semithick]154 \tikzstyle{every state}=[fill=none,draw=black,text=black]155156 \node[initial,state] (A) {$q_0$};157 \node[state] (B) [right of=A] {$q_1$};158159 \path160 (A) edge [bend left] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (B)161 edge [loop above] node {\TMtrans{\TMblank}{\TMblank}{\TMright}} (A)162 (B) edge [loop above] node {\TMtrans{\TMblank}{\TMblank}{\TMright}} (B)163 edge [bend left] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (A);164\end{tikzpicture}165\]166\end{ex}167168\begin{explain}169Machine tables are another way of representing Turing170machines. Machine tables have the tape alphabet displayed on the171$x$-axis, and the set of machine states across the $y$-axis. Inside the172table, at the intersection of each state and symbol, is written the173rest of the instruction---the new state, new symbol, and direction of174movement. Machine tables make it easy to determine in what state, and175for what symbol, the machine halts. Whenever there is a gap in the176table is a possible point for the machine to halt. Unlike state177diagrams and instruction sets, where the points at which the machine178halts are not always immediately obvious, any halting points are179quickly identified by finding the gaps in the machine table.180\end{explain}181182\begin{ex}183The machine table for the even machine is:184\[185\centering186\begin{tabular}{lllll}187\cline{2-4}188\multicolumn{1}{l|}{} & \multicolumn{1}{c|}{$\TMblank$} 189& \multicolumn{1}{c|}{$\TMstroke$} & \multicolumn{1}{c|}{$\TMendtape$} & \\ \cline{1-4}190\multicolumn{1}{|l|}{$q_0$} & \multicolumn{1}{l|}{} 191& \multicolumn{1}{l|}{\TMtrans{\TMstroke}{q_1}{\TMright}} & 192\multicolumn{1}{l|}{\phantom{\TMtrans{\TMstroke}{q_1}{\TMright}}} & \\ \cline{1-4}193\multicolumn{1}{|l|}{$q_1$} & \multicolumn{1}{l|}{\TMtrans{\TMblank}{q_1}{\TMright}} 194& \multicolumn{1}{l|}{\TMtrans{\TMstroke}{q_0}{\TMright}} & 195\multicolumn{1}{l|}{\phantom{\TMtrans{\TMstroke}{q_1}{\TMright}}} & \\ \cline{1-4}196\end{tabular}197\]198As we can see, the machine halts when scanning a~$\TMblank$ in state~$q_0$.199\end{ex}200201\begin{explain}202So far we have only considered machines that read and accept input.203However, Turing machines have the capacity to both read and write. An204example of such a machine (although there are many, many examples) is205a \emph{doubler}. A doubler, when started with a block of~$n$206$\TMstroke$'s on the tape, outputs a block of~$2n$ $\TMstroke$'s.207\end{explain}208209\begin{ex}210 \ollabel{ex:doubler} Before building a doubler machine, it is211 important to come up with a \emph{strategy} for solving the problem.212 Since the machine (as we have formulated it) cannot remember how213 many $\TMstroke$'s it has read, we need to come up with a way to214 keep track of all the $\TMstroke$'s on the tape. One such way is to215 separate the output from the input with a~$\TMblank$. The machine216 can then erase the first $\TMstroke$ from the input, traverse over217 the rest of the input, leave a $\TMblank$, and write two new218 $\TMstroke$'s. The machine will then go back and find the second219 $\TMstroke$ in the input, and double that one as well. For each one220 $\TMstroke$ of input, it will write two $\TMstroke$'s of output. By221 erasing the input as the machine goes, we can guarantee that no222 $\TMstroke$ is missed or doubled twice. When the entire input is223 erased, there will be $2n$ $\TMstroke$'s left on the tape. The state224 diagram of the resulting Turing machine is depicted in225 \olref{fig:doubler}.226 \begin{figure}227\[228\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,229 semithick]230 \tikzstyle{every state}=[fill=none,draw=black,text=black]231232 \node[initial,state] (1) {$q_0$};233 \node[state] (2) [right of=1] {$q_1$};234 \node[state] (3) [right of=2] {$q_2$};235 \node[state] (4) [below of=3] {$q_3$};236 \node[state] (5) [left of=4] {$q_4$};237 \node[state] (6) [left of=5] {$q_5$};238239 \path (1) edge node {\TMtrans{\TMstroke}{\TMblank}{\TMright}} (2)240 (2) edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (2)241 edge node {\TMtrans{\TMblank}{\TMblank}{\TMright}} (3)242 (3) edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (3)243 edge node {\TMtrans{\TMblank}{\TMstroke}{\TMright}} (4)244 (4) edge [loop below] node {\TMtrans{\TMblank}{\TMstroke}{\TMleft}} (4)245 edge node {\TMtrans{\TMstroke}{\TMstroke}{\TMleft}} (5)246 (5) edge [loop below] node {\TMtrans{\TMstroke}{\TMstroke}{\TMleft}} (5)247 edge node {\TMtrans{\TMblank}{\TMblank}{\TMleft}} (6)248 (6) edge [loop below] node {\TMtrans{\TMstroke}{\TMstroke}{\TMleft}} (6)249 edge node {\TMtrans{\TMblank}{\TMblank}{\TMright}} (1);250\end{tikzpicture}251\]252\caption{A doubler machine}253\ollabel{fig:doubler}254\end{figure}255\end{ex}256257\begin{prob}258Choose an arbitrary input and trace through the configurations of the259doubler machine in \olref[tur][mac][rep]{ex:doubler}.260\end{prob}261262\begin{prob}263Design a Turing-machine with alphabet $\{\TMendtape,\TMblank, A, B\}$264that accepts, i.e., halts on, any string of $A$'s and $B$'s where the265number of $A$'s is the same as the number of $B$'s \emph{and} all the266$A$'s precede all the $B$'s, and rejects, i.e., does not halt on, any267string where the number of $A$'s is not equal to the number of~$B$'s or268the $A$'s do not precede all the~$B$'s. (E.g., the machine should accept269$AABB$, and $AAABBB$, but reject both $AAB$ and $AABBAABB$.)270\end{prob}271272\begin{prob}273Design a Turing-machine with alphabet $\{\TMendtape,\TMblank, A, B\}$274that takes as input any string $\alpha$ of $A$'s and $B$'s and275duplicates them to produce an output of the form $\alpha\alpha$. (E.g.276input $ABBA$ should result in output $ABBAABBA$).277\end{prob}278279\begin{prob}280\emph{Alphabetical?:} Design a Turing-machine with alphabet281$\{\TMendtape,\TMblank, A, B\}$ that when given as input a finite282sequence of $A$'s and $B$'s checks to see if all the $A$'s appear to283the left of all the $B$'s or not. The machine should leave the input284string on the tape, and either halt if the string is285``alphabetical'', or loop forever if the string is not.286\end{prob}287288\begin{prob}289\emph{Alphabetizer:} Design a Turing-machine with alphabet290$\{\TMendtape,\TMblank, A, B\}$ that takes as input a finite sequence291of $A$'s and $B$'s rearranges them so that all the $A$'s are to the292left of all the~$B$'s. (e.g., the sequence $BABAA$ should become the293sequence $AAABB$, and the sequence $ABBABB$ should become the sequence294$AABBBB$).295\end{prob}296297\end{document}
content/turing-machines/machines-computations/turing-machines.tex
1% Part: turing-machines2% Chapter: machines-computations3% Section: turing-machines45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{mac}{tur}10\olsection{Turing Machines}1112\begin{explain}13The formal definition of what constitutes a Turing machine looks14abstract, but is actually simple: it merely packs into one15mathematical structure all the information needed to specify the16workings of a Turing machine. This includes (1) which states the17machine can be in, (2) which symbols are allowed to be on the tape, (3)18which state the machine should start in, and (4) what the instruction19set of the machine is.20\end{explain}2122\begin{defn}[Turing machine]23A \emph{Turing machine} $M$ is a tuple $\langle Q, \Sigma, q_0,24\delta\rangle$ consisting of25\begin{enumerate}26\item a finite set of \emph{states}~$Q$,27\item a finite \emph{alphabet} $\Sigma$ which includes $\TMendtape$ and28 $\TMblank$,29\item an \emph{initial state}~$q_0 \in Q$,30\item a finite \emph{instruction set}~$\delta\colon Q \times \Sigma31 \pto Q \times \Sigma \times \{\TMleft, \TMright, \TMstay\}$.32\end{enumerate}33The partial function~$\delta$ is also called the \emph{transition function}34of~$M$.35\end{defn}3637\begin{explain}38We assume that the tape is infinite in one direction only. For this39reason it is useful to designate a special symbol~$\TMendtape$ as a40marker for the left end of the tape. This makes it easier for Turing41machine programs to tell when they're ``in danger'' of running off the42tape. We could assume that this symbol is never overwritten, i.e.,43that $\delta(q,\TMendtape) = \tuple{q', \TMendtape, x}$ if44$\delta(q,\TMendtape)$ is defined. Some textbooks do this, we do not.45You can simply be careful when constructing your Turing machine that46it never overwrites~$\TMendtape$. Moreover, there are cases where47allowing such overwriting provides some convenient flexibility.48\end{explain}4950\begin{ex}51\emph{Even Machine:} The even machine is formally52the quadruple $\tuple{Q, \Sigma, q_0, \delta}$ where53\begin{align*}54Q & = \{ q_0, q_1 \} \\55\Sigma & = \{ \TMendtape, \TMblank, \TMstroke \}, \\56\delta(q_0, \TMstroke) & = \tuple{q_1, \TMstroke, \TMright},\\57\delta(q_1, \TMstroke) & = \tuple{q_0, \TMstroke, \TMright},\\58\delta(q_1, \TMblank) & = \tuple{q_1, \TMblank, \TMright}.59\end{align*}60\end{ex}6162\end{document}
content/turing-machines/machines-computations/configuration.tex
1% Part: computability2% Chapter: machines-computations3% Section: configuration45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{cmp}{tur}{con}10\olsection{Configurations and Computations}1112\begin{explain}13Recall tracing through the configurations of the even machine earlier.14The imaginary mechanism consisting of tape, read/write head, and15Turing machine program is really just an intuitive way of visualizing16what a Turing machine computation is. Formally, we can define the17computation of a Turing machine on a given input as a sequence of18\emph{configurations}---and a configuration in turn is a sequence of19symbols (corresponding to the contents of the tape at a given point in20the computation), a number indicating the position of the read/write21head, and a state. Using these, we can define what the Turing22machine~$M$ computes on a given input.23\end{explain}2425\begin{defn}[Configuration]26A \emph{configuration} of Turing machine $M = \tuple{Q, \Sigma, q_0,27\delta}$ is a triple $\tuple{C, m, q}$ where28\begin{enumerate}29\item $C \in \Sigma^*$ is a finite sequence of symbols from $\Sigma$,30\item $m \in \Nat$ is a number $< \len{C}$, and31\item $q \in Q$32\end{enumerate}33Intuitively, the sequence~$C$ is the content of the tape (symbols of34all squares from the leftmost square to the last non-blank or35previously visited square), $m$~is the number of the square the36read/write head is scanning (beginning with $0$ being the number of37the leftmost square), and $q$ is the current state of the machine.38\end{defn}3940\begin{explain}41The potential input for a Turing machine is a sequence of symbols,42usually a sequence that encodes a number in some form. The initial43configuration of the Turing machine is that configuration in which we44start the Turing machine to work on that input: the tape contains the45tape end marker immediately followed by the input written on the46squares to the right, the read/write head is scanning the leftmost47square of the input (i.e., the square to the right of the left end48marker), and the mechanism is in the designated start state~$q_0$.49\end{explain}5051\begin{defn}[Initial configuration]52The \emph{initial configuration} of $M$ for input $I \in \Sigma^*$ is53\[54\tuple{\TMendtape \frown I, 1, q_0}.55\]56\end{defn}5758\begin{explain}59The~$\frown$ symbol is for \emph{concatenation}---the input string60begins immediately to the left end marker.61\end{explain}6263\begin{defn}64We say that a configuration $\tuple{C, m, q}$ \emph{yields the65configuration $\tuple{C', m', q'}$ in one step} (according to~$M$),66iff67\begin{enumerate}68\item the $m$-th symbol of $C$ is $\sigma$,69\item the instruction set of $M$ specifies $\delta(q, \sigma) =70 \tuple{q', \sigma', D}$,71\item the $m$-th symbol of $C'$ is $\sigma'$, and 72\item73\begin{enumerate}74\item $D = L$ and $m' = m - 1$ if $m>0$, otherwise $m'=0$, or75\item $D = R$ and $m' = m + 1$, or76\item $D = N$ and $m' = m$,77\end{enumerate}78\item if $m' = \len{C}$, then $\len{C'} = \len{C} + 1$ and the $m'$-th79 symbol of $C'$ is~$\TMblank$. Otherwise $\len{C'}=\len{C}$.80\item for all $i$ such that $i < \len{C}$ and $i \neq m$, $C'(i) = C(i)$,81\end{enumerate}82\end{defn}8384\begin{defn}\ollabel{defn:run-output}85A \emph{run of $M$ on input~$I$} is a sequence $C_i$ of configurations86of $M$, where $C_0$ is the initial configuration of $M$ for input~$I$,87and each $C_i$ yields $C_{i+1}$ in one step.8889We say that $M$ \emph{halts on input $I$ after $k$ steps} if $C_k =90\tuple{C, m, q}$, the $m$th symbol of~$C$ is~$\sigma$, and $\delta(q,91\sigma)$ is undefined. In that case, the \emph{output} of~$M$ for92input~$I$ is~$O$, where $O$ is a string of symbols not ending93in~$\TMblank$ such that $C = \TMendtape \concat O \concat \TMblank^j$94for some~$j \in \Nat$. ($\TMblank^j$ is a sequence of $j$ $\TMblank$'s.)95\end{defn}9697\begin{explain}98According to this definition, the output~$O$ of~$M$ always 99ends in a symbol other than~$\TMblank$, or, if at time~$k$ the entire100tape is filled with~$\TMblank$ (except for the leftmost~$\TMendtape$),101$O$~is the empty string.102\end{explain}103104\end{document}
content/turing-machines/machines-computations/unary-numbers.tex
1% Part: turing-machines2% Chapter: machines-computations3% Section: unary-numbers45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{mac}{una}10\olsection{Unary Representation of Numbers}1112\begin{explain}13Turing machines work on sequences of symbols written on their tape.14Depending on the alphabet a Turing machine uses, these sequences of15symbols can represent various inputs and outputs. Of particular16interest, of course, are Turing machines which compute17\emph{arithmetical} functions, i.e., functions of natural numbers. A18simple way to represent positive integers is by coding them as19sequences of a single symbol~$\TMstroke$. If $n \in \Nat$, let20$\TMstroke^n$ be the empty sequence if $n = 0$, and otherwise the21sequence consisting of exactly $n$ $\TMstroke$'s.22\end{explain}2324\begin{defn}[Computation]25A Turing machine~$M$ \emph{computes} the function $f\colon \Nat^k \to \Nat$ iff26$M$~halts on input27\[28\TMstroke^{n_1} \TMblank \TMstroke^{n_2} \TMblank \dots \TMblank \TMstroke^{n_k}29\]30with output $\TMstroke^{f(n_1, \dots, n_k)}$.31\end{defn}3233\begin{prob}34 Give a definition for when a Turing machine~$M$ computes the35 function $f\colon \Nat^k \to \Nat^m$.36\end{prob}3738\begin{ex}\ollabel{ex:adder}39\emph{Addition:}40Let's build a machine that computes the function $f(n,m) = n + m$.41This requires a machine that starts with two blocks of $\TMstroke$'s42of length $n$ and~$m$ on the tape, and halts with one block consisting43of $n+m$~$\TMstroke$'s. The two input blocks of~$\TMstroke$'s are44separated by a~$\TMblank$, so one method would be to write a stroke on45the square containing the $\TMblank$, and erase the last~$\TMstroke$.46\begin{figure}\[47\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,48 semithick]49 \tikzstyle{every state}=[fill=none,draw=black,text=black]5051 \node[initial,state] (A) {$q_0$};52 \node[state] (B) [right of=A] {$q_1$};53 \node[state] (C) [right of=B] {$q_2$};5455 \path (A) edge node {\TMtrans{\TMblank}{\TMstroke}{\TMstay}} (B)56 edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (B)57 (B) edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (B)58 edge node {\TMtrans{\TMblank}{\TMblank}{\TMleft}} (C)59 (C) edge [loop above] node {\TMtrans{\TMstroke}{\TMblank}{\TMstay}} (C);60\end{tikzpicture}61\]\caption{A machine computing $f(x,y) = x+y$}62\ollabel{fig:adder}63\end{figure}64\end{ex}6566\begin{prob}67Trace through the configurations of the machine from68\olref[tur][mac][una]{ex:adder} for input~$\tuple{3,2}$. What happens69if the machine computes $0+0$?70\end{prob}7172\begin{explain}73In \olref[rep]{ex:doubler}, we gave an example of a Turing machine74that takes as input a sequence of~$\TMstroke$'s and halts with a sequence75of twice as many~$\TMstroke$'s on the tape---the doubler machine.76However, because the output contains $\TMblank$'s to the left of the77doubled block of~$\TMstroke$'s, it does not actually compute the78function $f(x) = 2x$, as you might have assumed. We'll describe two79ways of fixing that.80\end{explain}8182\begin{ex}83The machine in \olref{fig:doubler-disc} computes the function $f(x) =842x$. Instead of erasing the input and writing two $\TMstroke$'s at the85far right for every $\TMstroke$ in the input as the machine from86\olref[rep]{ex:doubler} does, this machine adds a single~$\TMstroke$87to the right for every~$\TMstroke$ in the input. It has to keep track88of where the input ends, so it leaves a~$\TMblank$ between the input89and the added strokes, which it fills with a~$\TMstroke$ at the very90end. And we have to ``remember'' where we are in the input, so we91temporarily replace a~$\TMstroke$ in the input block by a~$\TMblank$.92 \begin{figure}93\[94\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,95 semithick]96 \tikzstyle{every state}=[fill=none,draw=black,text=black]9798 \node[initial,state] (0) {$q_0$};99 \node[state] (1) [above of=0] {$q_1$};100 \node[state] (2) [above of=1] {$q_2$};101 \node[state] (3) [right of=2] {$q_3$};102 \node[state] (4) [below of=3] {$q_4$};103 \node[state] (5) [below of=4] {$q_5$};104 \node[state] (6) [above right of=4] {$q_6$};105 \node[state] (7) [below of=6] {$q_7$};106 \node[state] (8) [below of=7] {$q_8$};107108 \path (0) edge node {\TMtrans{\TMstroke}{\TMblank}{\TMright}} (1)109% (0) edge node {\TMtrans{\TMblank}{\TMblank}{\TMstay}} (h)110 (1) edge [loop left] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (1)111 edge node {\TMtrans{\TMblank}{\TMblank}{\TMright}} (2)112 (2) edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (2)113 edge node {\TMtrans{\TMblank}{\TMstroke}{\TMleft}} (3)114 (3) edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMleft}} (3)115 edge node[left] {\TMtrans{\TMblank}{\TMblank}{\TMleft}} (4)116 (4) edge node[left] {\TMtrans{\TMstroke}{\TMstroke}{\TMleft}} (5)117 (5) edge [loop below] node {\TMtrans{\TMstroke}{\TMstroke}{\TMleft}} (5)118 edge node {\TMtrans{\TMblank}{\TMstroke}{\TMright}} (0)119 (4) edge node[sloped] {\TMtrans{\TMblank}{\TMstroke}{\TMright}} (6)120 (6) edge node {\TMtrans{\TMblank}{\TMstroke}{\TMright}} (7)121 (7) edge [loop right] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (7)122 edge node {\TMtrans{\TMblank}{\TMblank}{\TMleft}} (8)123 (8) edge [loop right] node {\TMtrans{\TMstroke}{\TMblank}{\TMstay}} (8);124 \end{tikzpicture}125\]126\caption{A machine computing $f(x) = 2x$}127\ollabel{fig:doubler-disc}128\end{figure}129\end{ex}130131\begin{ex}\ollabel{ex:mover}132A second possibility for computing $f(x) = 2x$ is to keep the original133doubler machine, but add states and instructions at the end which move134the doubled block of strokes to the far left of the tape. The machine135in \olref{fig:mover} does just this last part: started on a tape136consisting of a block of~$\TMblank$'s followed by a block137of~$\TMstroke$'s (and the head positioned anywhere in the block138of~$\TMblank$'s), it erases the $\TMstroke$'s one at a time and writes139them at the beginning of the tape. In order to be able to tell when it140is done, it first marks the end of the block of $\TMstroke$'s with141a~$\TMendtape$ symbol, which gets deleted at the end. We've started142numbering the states at~$q_6$, so they can be added to the doubler143machine. All you'll need is an additional instruction $\delta(q_0,144\TMblank) = \tuple{q_6,\TMblank,\TMstay}$, i.e., an arrow from~$q_0$145to~$q_6$ labelled~$\TMtrans{\TMblank}{\TMblank}{\TMstay}$. (There is146one subtle problem: the resulting machine does not work for147input~$x=0$. We'll leave this as an exercise.)148\begin{figure}149 \[150 \begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,151 semithick]152 \tikzstyle{every state}=[fill=none,draw=black,text=black]153 \node[initial,state] (6) {$q_6$};154 \node[state] (7) [right of=6] {$q_7$};155 \node[state] (8) [right of=7] {$q_8$};156 \node[state] (9) [below of=8] {$q_9$};157 \node[state] (10) [left of=9] {$q_{10}$};158 \node[state] (11) [left of=10] {$q_{11}$};159 \node[state] (12) [below of=11] {$q_{12}$};160 \node[state] (13) [right of=12] {$q_{13}$};161 \node[state] (14) [right of=13] {$q_{14}$};162 \path163 (6) edge node {\TMtrans{\TMblank}{\TMblank}{\TMright}} (7)164 (7) edge [loop above] node {\TMtrans{\TMblank}{\TMblank}{\TMright}} (7)165 edge node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (8)166 (8) edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (8)167 edge node {\TMtrans{\TMblank}{\TMendtape}{\TMleft}} (9)168 (9) edge [loop right] node {\TMtrans{\TMstroke}{\TMstroke}{\TMleft}} (9)169 edge node {\TMtrans{\TMblank}{\TMblank}{\TMright}} (10)170 (10) edge node[above] {\TMtrans{\TMstroke}{\TMblank}{\TMleft}} (11)171 (11) edge [loop above] node {\TMtrans{\TMblank}{\TMblank}{\TMleft}} (11)172 edge node[left] {\begin{tabular}{@{}l@{}}173 \TMtrans{\TMendtape}{\TMendtape}{\TMright}\\174 \TMtrans{\TMstroke}{\TMstroke}{\TMright}175 \end{tabular}} (12)176 (12) edge node[] {\TMtrans{\TMblank}{\TMstroke}{\TMright}} (13)177 (13) edge [loop below] node {\TMtrans{\TMblank}{\TMblank}{\TMright}} (13)178 edge node[sloped] {\TMtrans{\TMstroke}{\TMblank}{\TMleft}} (11)179 (13) edge node {\TMtrans{\TMendtape}{\TMblank}{\TMstay}} (14);180 \end{tikzpicture}181 \]182 \caption{Moving a block of $\TMstroke$'s to the left}183 \ollabel{fig:mover}184\end{figure}185\end{ex}186187\begin{prob}188In \olref[tur][mac][una]{ex:mover} we described a machine consisting189of a combination of the doubler machine from190\olref[tur][mac][una]{fig:doubler-disc} and the mover machine from191\olref[tur][mac][una]{fig:mover}. What happens if you start this192combined machine on input~$x=0$, i.e., on an empty tape? How would193you fix the machine so that in this case the machine halts with194output~$2x=0$? (You should be able to do this by adding one state and195one transition.)196\end{prob}197198\begin{prob}199\emph{Subtraction:} Design a Turing machine that when given an input200of two non-empty strings of strokes of length $n$ and~$m$, where $n >201m$, computes the function $f(n,m) = n - m$.202\end{prob}203204\begin{prob}205\emph{Equality:} Design a Turing machine to compute the following function:206\[207\fn{equality}(n,m) = 208\begin{cases}209 \text{1} & \text{if~$n = m$} \\210 \text{0} & \text{if~$n \neq m$}211\end{cases}212\]213where~$n$ and~$m \in \PosInt$.214\end{prob}215216\begin{prob}217Design a Turing machine to compute the function $\min(x,y)$ where $x$218and $y$ are positive integers represented on the tape by strings of219$\TMstroke$'s separated by a $\TMblank$. You may use additional220symbols in the alphabet of the machine.221222The function $\min$ selects the smallest value from its arguments, so223$\min(3,5)=3$, $\min(20,16)=16$, and $\min(4,4)=4$, and so on.224\end{prob}225226\begin{defn}227 A Turing machine~$M$ computes the partial function $f\colon \Nat^k228 \pto \Nat$ iff, 229 \begin{enumerate}230 \item $M$ halts on input $\TMstroke^{n_1}\concat\TMblank\concat231 \dots \concat\TMblank\concat\TMstroke^{n_k}$ with output $\TMstroke^{m}$ if $f(n_1, \dots, n_k) = m$.232 \item $M$ does not halt at all, or with an output that is not a233 single block of~$\TMstroke$'s if $f(n_1, \dots, n_k)$ is undefined.234 \end{enumerate}235\end{defn}236237\end{document}
content/turing-machines/machines-computations/halting-states.tex
1% Part: turing-machines2% Chapter: machines-computations3% Section: halting-states45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{mac}{hal}10\olsection{Halting States}1112\begin{explain}13Although we have defined our machines to halt only when there14is no instruction to carry out, common representations of Turing15machines have a dedicated \emph{halting state}~$h$, such that16$h \in Q$.1718The idea behind a halting state is simple: when the machine has19finished operation (it is ready to accept input, or has finished20writing the output), it goes into a state~$h$ where it halts. Some21machines have two halting states, one that accepts input and one that22rejects input.23\end{explain}2425\begin{ex}\emph{Halting States}.26To elucidate this concept, let us begin with an alteration of the27even machine. Instead of having the machine halt in state~$q_0$28if the input is even, we can add an instruction to send the machine29into a halting state.30\[31\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,32 semithick]33 \tikzstyle{every state}=[fill=none,draw=black,text=black]3435 \node[initial, state] (A) {$q_0$};36 \node[state] (B) [right of=A] {$q_1$};37 \node[state] (C) [below of=A] {$h$};3839 \path (A) edge [bend left] node {\TMtrans{\TMstroke}{\TMstroke}{R}} (B)40 edge node {\TMtrans{\TMblank}{\TMblank}{N}} (C)41 (B) edge [loop above] node {\TMtrans{\TMblank}{\TMblank}{R}} (B)42 edge [bend left] node {\TMtrans{\TMstroke}{\TMstroke}{R}} (A);43\end{tikzpicture}44\]4546Let us further expand the example. When the machine determines that the input47is odd, it never halts. We can alter the machine to48include a \emph{reject} state by replacing the looping instruction with an49instruction to go to a reject state~$r$.50\[51\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,52 semithick]53 \tikzstyle{every state}=[fill=none,draw=black,text=black]5455 \node[initial,state] (A) {$q_0$};56 \node[state] (B) [right of=A] {$q_1$};57 \node[state] (C) [below of=A] {$h$};58 \node[state] (D) [below of=B] {$r$};5960 \path (A) edge [bend left] node {\TMtrans{\TMstroke}{\TMstroke}{R}} (B)61 edge node {\TMtrans{\TMblank}{\TMblank}{N}} (C)62 (B) edge node {\TMtrans{\TMblank}{\TMblank}{N}} (D)63 edge [bend left] node {\TMtrans{\TMstroke}{\TMstroke}{R}} (A);64\end{tikzpicture}65\]66\end{ex}6768\begin{explain}69Adding a dedicated halting state can be advantageous in cases like70this, where it makes explicit when the machine accepts/rejects certain71inputs. However, it is important to note that no computing power is72gained by adding a dedicated halting state. Similarly, a less73formal notion of halting has its own advantages. The definition of74halting used so far in this chapter makes the proof of the75\emph{Halting Problem} intuitive and easy to demonstrate. For this76reason, we continue with our original definition.77\end{explain}7879\end{document}
content/turing-machines/machines-computations/disciplined-machines.tex
1% Part: turing-machines2% Chapter: machines-computations3% Section: well-behaved-machines45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{mac}{dis}10\olsection{Disciplined Machines}1112\begin{explain}13In section \olref[hal]{sec}, we considered Turing machines that have a14single, designated halting state~$h$---such machines are guaranteed to15halt, if they halt at all, in state~$h$. In this way, machines with a16single halting state are more ``disciplined'' than we allow Turing17machines in general to be. There are other restrictions we might18impose on the behavior of Turing machines. For instance, we also have19not prohibited Turing machines from ever erasing the tape-end marker20on square~$0$, or to attempt to move left from square~$0$. (Our21definition states that the head simply stays on square~$0$ in this22case; other definitions have the machine halt.) It is likewise23sometimes desirable to be able to assume that a Turing machine, if24it halts at all, halts on square~$1$.25\end{explain}2627\begin{defn}\ollabel{defn:disciplined}28A Turing machine~$M$ is \emph{disciplined} iff29\begin{enumerate}30 \item it has a designated single halting state~$h$,31 \item it halts, if it halts at all, while scanning square~$1$,32 \item it never erases the $\TMendtape$ symbol on square~$0$, and33 \item it never attempts to move left from square~$0$.34\end{enumerate}35\end{defn}3637\begin{explain}38We have already discussed that any Turing machine can be changed into39one with the same behavior but with a designated halting state. This is40done simply by adding a new state~$h$, and adding an instruction41$\delta(q, \sigma) = \tuple{h, \sigma, N}$ for any pair42$\tuple{q,\sigma}$ where the original $\delta$~is undefined. It is43true, although tedious to prove, that any Turing machine~$M$ can be44turned into a disciplined Turing machine~$M'$ which halts on the same45inputs and produces the same output. For instance, if the Turing46machine halts and is not on square~$1$, we can add some instructions47to make the head move left until it finds the tape-end marker, then48move one square to the right, then halt. We'll leave you to think49about how the other conditions can be dealt with.50\end{explain}5152\begin{ex}53 In \olref{fig:adder-disc}, we turn the addition machine54 from \olref[una]{ex:adder} into a disciplined machine.55 \begin{figure}\[56\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,57 semithick]58 \tikzstyle{every state}=[fill=none,draw=black,text=black]5960 \node[initial,state] (A) {$q_0$};61 \node[state] (B) [right of=A] {$q_1$};62 \node[state] (C) [below right of=B] {$q_2$};63 \node[state] (D) [below left of=C] {$q_3$};64 \node[state] (H) [left of=D] {$h$};6566 \path (A) edge node {\TMtrans{\TMblank}{\TMstroke}{\TMstay}} (B)67 edge [loop below] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (B)68 (B) edge [loop below] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (B)69 edge node {\TMtrans{\TMblank}{\TMblank}{\TMleft}} (C)70 (C) edge node {\TMtrans{\TMstroke}{\TMblank}{\TMleft}} (D)71 (D) edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMleft}} (D)72 edge node {\TMtrans{\TMendtape}{\TMendtape}{\TMright}} (H);73\end{tikzpicture}74\]\caption{A disciplined addition machine}75\ollabel{fig:adder-disc}76\end{figure}77\end{ex}7879\begin{prop}\ollabel{prop:disciplined} For every Turing machine~$M$,80there is a disciplined Turing machine~$M'$ which halts with output~$O$81if $M$~halts with output~$O$, and does not halt if $M$~does not halt.82In particular, any function $f\colon\Nat^n \to \Nat$ computable by a83Turing machine is also computable by a disciplined Turing machine.84\end{prop}8586\begin{prob}\label{tur:mac:dis:prob:disc-succ}87Give a disciplined machine that computes $f(x) = x+1$.88\end{prob}899091\begin{prob}\label{tur:mac:dis:prob:copier}92Find a disciplined machine which, when started on input $\TMstroke^n$93produces output $\TMstroke^n \concat \TMblank \concat \TMstroke^n$.94\end{prob}9596\end{document}
content/turing-machines/machines-computations/combining-machines.tex
1% Part: turing-machines2% Chapter: machines-computations3% Section: combining-machines45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{mac}{cmb}10\olsection{Combining Turing Machines}1112\begin{explain}13The examples of Turing machines we have seen so far have been fairly14simple in nature. But in fact, any problem that can be solved with any15modern programming language can also be solved with Turing machines.16To build more complex Turing machines, it is important to convince17ourselves that we can combine them, so we can build machines to solve18more complex problems by breaking the procedure into simpler parts.19If we can find a natural way to break a complex problem down into20constituent parts, we can tackle the problem in several stages,21creating several simple Turing machines and combining them into one22machine that can solve the problem. This point is especially important23when tackling the Halting Problem in the next section.2425How do we combine Turing machines $M = \tuple{Q, \Sigma, q_0, \delta}$26and~$M' = \tuple{Q', \Sigma', q_0', \delta'}$? We now use the27configuration of the tape after $M$~has halted as the input28configuration of a run of machine~$M'$. To get a single Turing29machine $M \frown M'$ that does this, do the following:30\begin{enumerate}31 \item Renumber (or relabel) all the states~$Q'$ of~$M'$ so that $M$32 and~$M'$ have no states in common ($Q \cap Q' = \emptyset$).33 \item The states of $M \frown M'$ are $Q \cup Q'$.34 \item The tape alphabet is $\Sigma \cup \Sigma'$.35 \item The start state is~$q_0$.36 \item The transition function is the function $\delta''$ given by:37 \[\delta''(q,\sigma) =38 \begin{cases}39 \delta(q,\sigma) & \text{if $q \in Q$}\\40 \delta'(q,\sigma) & \text{if $q \in Q'$}\\41 \tuple{q_0', \sigma, \TMstay} & \text{if $q \in Q$ and42 $\delta(q,\sigma)$ is undefined}43 \end{cases}\]44\end{enumerate}45The resulting machine uses the instructions of~$M$ when it is in a46state $q \in Q$, the instructions of~$M'$ when it is in a state~$q \in47Q'$. When it is in a state $q \in Q$ and is scanning a symbol~$\sigma$48for which $M$ has no transition (i.e., $M$ would have halted), it49enters the start state of~$M'$ (and leaves the tape contents and head50position as it is).5152Note that unless the machine~$M$ is disciplined, we don't know where53the tape head is when $M$~halts, so the halting configuration of~$M$54need not have the head scanning square~$1$. When combining machines,55it's important to keep this in mind.56\end{explain}5758\begin{ex}59\emph{Combining Machines:} We'll design a machine which, when started60on input consisting of two blocks of~$\TMstroke$'s of length $n$61and~$m$, halts with a single block of $2(m+n)$ $\TMstroke$'s on the62tape. In order to build this machine, we can combine two machines we63are already familiar with: the addition machine, and the doubler. We64begin by drawing a state diagram for the addition machine.65\[66\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,67 semithick]68 \tikzstyle{every state}=[fill=none,draw=black,text=black]6970 \node[initial,state] (A) {$q_0$};71 \node[state] (B) [right of=A] {$q_1$};72 \node[state] (C) [right of=B] {$q_2$};7374 \path (A) edge node {\TMtrans{\TMblank}{\TMstroke}{\TMstay}} (B)75 edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (B)76 (B) edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (B)77 edge node {\TMtrans{\TMblank}{\TMblank}{\TMleft}} (C)78 (C) edge [loop above] node {\TMtrans{\TMstroke}{\TMblank}{\TMstay}} (C);79\end{tikzpicture}80\]81Instead of halting in state~$q_2$, we want to continue operation in82order to double the output. Recall that the doubler machine erases the83first stroke in the input and writes two strokes in a separate output.84Let's add an instruction to make sure the tape head is reading the85first stroke of the output of the addition machine.86\[87\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,88 semithick]89 \tikzstyle{every state}=[fill=none,draw=black,text=black]9091 \node[initial,state] (A) {$q_0$};92 \node[state] (B) [right of=A] {$q_1$};93 \node[state] (C) [right of=B] {$q_2$};94 \node[state] (D) [below left of=C] {$q_3$};95 \node[state] (E) [below left of=D] {$q_4$};9697 \path (A) edge node {\TMtrans{\TMblank}{\TMstroke}{\TMstay}} (B)98 edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (B)99 (B) edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (B)100 edge node {\TMtrans{\TMblank}{\TMblank}{\TMleft}} (C)101 (C) edge node {\TMtrans{\TMstroke}{\TMblank}{\TMleft}} (D)102 (D) edge [loop left] node {\TMtrans{\TMstroke}{\TMstroke}{\TMleft}} (D)103 edge node[left, xshift=-2mm] {\TMtrans{\TMendtape}{\TMendtape}{\TMright}} (E);104\end{tikzpicture}105\]106It is now easy to double the input---all we have to do is connect the107doubler machine onto state~$q_4$. This requires renaming the states of108the doubler machine so that they start at~$q_4$ instead109of~$q_0$---this way we don't end up with two starting states. The110final diagram should look as in \olref{fig:combined}.111\begin{figure}112\[113\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,114 semithick]115 \tikzstyle{every state}=[fill=none,draw=black,text=black]116 \node[initial,state] (A) {$q_0$};117 \node[state] (B) [right of=A] {$q_1$};118 \node[state] (C) [right of=B] {$q_2$};119 \node[state] (D) [below left of=C] {$q_3$};120 \node[state] (E) [below left of=D] {$q_4$};121 \node[state] (2) [right of=E] {$q_5$};122 \node[state] (3) [right of=2] {$q_6$};123 \node[state] (4) [below of=3] {$q_7$};124 \node[state] (5) [left of=4] {$q_8$};125 \node[state] (6) [left of=5] {$q_9$};126127 \path (A) edge node {\TMtrans{\TMblank}{\TMstroke}{\TMstay}} (B)128 edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (B)129 (B) edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (B)130 edge node {\TMtrans{\TMblank}{\TMblank}{\TMleft}} (C)131 (C) edge node {\TMtrans{\TMstroke}{\TMblank}{\TMleft}} (D)132 (D) edge [loop left] node {\TMtrans{\TMstroke}{\TMstroke}{\TMleft}} (D)133 edge node[left, xshift=-2mm] {\TMtrans{\TMendtape}{\TMendtape}{\TMright}} (E)134 (E) edge node {\TMtrans{\TMstroke}{\TMblank}{\TMright}} (2)135 (2) edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (2)136 edge node {\TMtrans{\TMblank}{\TMblank}{\TMright}} (3)137 (3) edge [loop above] node {\TMtrans{\TMstroke}{\TMstroke}{\TMright}} (3)138 edge node {\TMtrans{\TMblank}{\TMstroke}{\TMright}} (4)139 (4) edge [loop below] node {\TMtrans{\TMblank}{\TMstroke}{\TMleft}} (4)140 edge node {\TMtrans{\TMstroke}{\TMstroke}{\TMleft}} (5)141 (5) edge [loop below] node {\TMtrans{\TMstroke}{\TMstroke}{\TMleft}} (5)142 edge node {\TMtrans{\TMblank}{\TMblank}{\TMleft}} (6)143 (6) edge [loop below] node {\TMtrans{\TMstroke}{\TMstroke}{\TMleft}} (6)144 edge node {\TMtrans{\TMblank}{\TMblank}{\TMright}} (E);145\end{tikzpicture}146\]147\caption{Combining adder and doubler machines}148\ollabel{fig:combined}149\end{figure}150\end{ex}151152\begin{prop}153 If $M$ and $M'$ are disciplined and compute the functions $f\colon154 \Nat^k \to \Nat$ and $f'\colon \Nat \to \Nat$, respectively, then155 $M \frown M'$ is disciplined and computes~$\comp{f}{f'}$.156\end{prop}157158\begin{proof}159 Since $M$ is disciplined, when it halts with160 output~$f(n_1,\dots,n_k) = m$, the head is scanning square~$1$. If161 we now enter the start state of~$M'$, the machine will halt with162 output $f'(m)$, again scanning square~$1$. The other conditions of163 \olref[dis]{defn:disciplined} are also satisfied.164\end{proof}165166\begin{prob}167 Give a disciplined Turing machine computing $f(x) = x+2$ by taking168 the machine~$M$ from \cref{tur:mac:dis:prob:disc-succ} and169 construct $M \frown M$.170\end{prob}171\end{document}
content/turing-machines/machines-computations/variants.tex
1% Part: turing-machines2% Chapter: machines-computations3% Section: variants45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{mac}{var}10\olsection{Variants of Turing Machines}1112There are in fact many possible ways to define Turing machines, of13which ours is only one. In some ways, our definition is more liberal14than others. We allow arbitrary finite alphabets, a more restricted15definition might allow only two tape symbols, $\TMstroke$16and~$\TMblank$. We allow the machine to write a symbol to the tape17and move at the same time, other definitions allow either writing or18moving. We allow the possibility of writing without moving the tape19head, other definitions leave out the $\TMstay$ ``instruction.'' In20other ways, our definition is more restrictive. We assumed that the21tape is infinite in one direction only, other definitions allow the22tape to be infinite both to the left and the right. In fact, one can23even allow any number of separate tapes, or even an infinite grid of24squares. We represent the instruction set of the Turing machine by a25transition function; other definitions use a transition relation where26the machine has more than one possible instruction in any given27situation.2829This last relaxation of the definition is particularly interesting.30In our definition, when the machine is in state~$q$ reading31symbol~$\sigma$, $\delta(q, \sigma)$ determines what the new symbol,32state, and tape head position is. But if we allow the instruction set33to be a relation between current state-symbol pairs $\tuple{q,34 \sigma}$ and new state-symbol-direction triples $\tuple{q', \sigma',35 D}$, the action of the Turing machine may not be uniquely36determined---the instruction relation may contain both $\tuple{q,37 \sigma, q', \sigma', D}$ and $\tuple{q, \sigma, q'', \sigma'', D'}$.38In this case we have a \emph{non-deterministic} Turing machine. These39play an important role in computational complexity theory.4041There are also different conventions for when a Turing machine halts:42we say it halts when the transition function is undefined, other43definitions require the machine to be in a special designated halting44state. We have explained in \olref[hal]{sec} why requiring a45designated halting state is not a restriction which impacts what46Turing machines can compute. Since the tapes of our Turing machines47are infinite in one direction only, there are cases where a Turing48machine can't properly carry out an instruction: if it reads the49leftmost square and is supposed to move left. According to our50definition, it just stays put instead of ``falling off'', but we could51have defined it so that it halts when that happens. This definition is52also equivalent: we could simulate the behavior of a Turing machine53that halts when it attempts to move left from square~$0$ by deleting54every transition $\delta(q,\TMendtape) =55\tuple{q',\sigma,\TMleft}$---then instead of attempting to move left56on~$\TMendtape$ the machine halts.\footnote{This doesn't \emph{quite}57work, since nothing prevents us from writing and reading $\TMendtape$58on squares other than square~$0$ (see \olref[una]{ex:mover}). We can59get around that by adding a second~$\TMendtape'$ symbol to use instead60for such a purpose.}6162There are also different ways of representing numbers (and hence the63input-output function computed by a Turing machine): we use unary64representation, but you can also use binary representation. This65requires two symbols in addition to $\TMblank$ and~$\TMendtape$.6667Now here is an interesting fact: none of these variations matters as68to which functions are Turing computable. \emph{If a function is Turing69computable according to one definition, it is Turing computable70according to all of them.}7172We won't go into the details of verifying this. Here's just one73example: we gain no additional computing power by allowing a tape that74is infinite in both directions, or multiple tapes. The reason is,75roughly, that a Turing machine with a single one-way infinite tape can76simulate multiple or two-way infinite tapes. E.g., using additional77states and instructions, we can ``translate'' a program for a78machine with multiple tapes or two-way infinite tape into one with a79single one-way infinite tape. The translated machine can use the80even squares for the squares of tape~$1$ (or the ``positive'' squares81of a two-way infinite tape) and the odd squares for the squares of82tape~$2$ (or the ``negative'' squares).8384\end{document}
content/turing-machines/machines-computations/church-turing-thesis.tex
1% Part: computability2% Chapter: tm-computations3% Section: church-turing-thesis45\documentclass[../../../include/open-logic-section]{subfiles}67\begin{document}89\olfileid{tur}{mac}{ctt}10\olsection{The Church--Turing Thesis}1112Turing machines are supposed to be a precise replacement for the13concept of an effective procedure. Turing thought that anyone who14grasped both the concept of an effective procedure and the concept15of a Turing machine would have the intuition that anything that could16be done via an effective procedure could be done by Turing machine.17This claim is given support by the fact that all the other proposed18precise replacements for the concept of an effective procedure turn19out to be extensionally equivalent to the concept of a Turing machine20---that is, they can compute exactly the same set of functions. This21claim is called the \emph{Church--Turing thesis}.2223\begin{defn}[Church--Turing thesis]24The \emph{Church--Turing Thesis} states that anything computable via25an effective procedure is Turing computable.26\end{defn}2728The Church--Turing thesis is appealed to in two ways. The first kind29of use of the Church--Turing thesis is an excuse for laziness. Suppose30we have a description of an effective procedure to compute something,31say, in ``pseudo-code.'' Then we can invoke the Church--Turing thesis32to justify the claim that the same function is computed by some Turing33machine, even if we have not in fact constructed it.3435The other use of the Church--Turing thesis is more philosophically36interesting. It can be shown that there are functions which cannot be37computed by Turing machines. From this, using the Church--Turing38thesis, one can conclude that it cannot be effectively computed, using39any procedure whatsoever. For if there were such a procedure, by the40Church--Turing thesis, it would follow that there would be a Turing41machine for it. So if we can prove that there is no Turing machine42that computes it, there also can't be an effective procedure. In43particular, the Church--Turing thesis is invoked to claim that the44so-called halting problem not only cannot be solved by Turing45machines, it cannot be effectively solved at all.4647\end{document}