Part IV: Turing Machines and Undecidability
Total Page:16
File Type:pdf, Size:1020Kb
Part IV: Turing Machines and Undecidability We are about to begin our exploration of the two outer circles of the language hierarchy, as well as the background, the area outside all of the circles. Up until now, we have been placing limitations on what we could do in order that we could discover simple solutions when they exist. Now we are going to tear down all the barriers and explore the full power of formal computation. We will discover a whole range of problems that become solvable once we do that. We will also discover that there are fundamental limitations on what we can compute, regardless of the specific model with which we choose to work. Part IV 264 Turing Machines and Undecidability 17 Turing Machines We need a new kind of automaton that has two properties: • It must be powerful enough to describe all computable things. In this respect, it should be like real computers and unlike FSMs and PDAs. • It must be simple enough that we can reason formally about it. In this respect, it should be like FSMs and PDAs and unlike real computers. 17.1 Definition, Notation and Examples In our discussion of pushdown automata, it became clear that a finite state controller augmented by a single stack was not powerful enough to be able to execute even some very simple programs. What else must be added in order to acquire the necessary power? One answer is a second stack. We will explore that idea in Section 17.5.2. 17.1.1 What Is a Turing Machine? A more straightforward approach is to eliminate the stack and replace it by a more flexible form of infinite storage, a writeable tape. When we do that, we get a Turing machine . Figure 17. 1 showns a simple schematic diagram of a Turing machine M. M’s tape is infinite in both directions. The input to M is written on the tape, one character per square, before M is started. All other squares of the tape are initially blank (). As we have done for both FSMs and PDAs, M’s behavior will be defined only on input strings that are: • finite, and • contain only characters in M’s input alphabet. … a a b b b … Finite State Controller s, q1, q2, … h1, h2 Figure 17.1 The structure of a Turing machine M has a single read/write head, shown here with an arrow. We will almost always use the convention that, when M starts, its read/write head will be over the blank immediately to the left of the leftmost character of the input. But occasionally, when we are designing a machine to be used as a subroutine by some other machine, we may choose a different initial specification. M begins in its start state. At each step of its operation, M must: • choose its next state, • write on the current square, and • move the read/write head left or right one square. M can move back and forth on its tape, so there is no longer the idea that it consumes all of its input characters one at a time and then halts. M will continue to execute until it reaches a special state called a halting state. It is possible that M may never reach a halting state, in which case it will execute forever. Chapter 17 265 Turing Machines Notice that there will always be a finite number of nonblank squares on M’s tape. This follows from the fact that, before M starts, only a finite number of squares are nonblank. Then, at each step of its operation, M can write on at most one additional square. So, after any finite number of steps, only a finite number of squares can be nonblank. And, even if M never halts, at any point in its computation it will have executed only a finite number of steps. In Chapter 18 we are going to argue that the Turing machine, as we have just described it, is as powerful as any other reasonable model of computation, including modern computers. So, in the rest of this discussion, although our examples will be simple, remember that we are now talking about computation, broadly conceived. We are now ready to provide a formal definition: a Turing machine (or TM) M is a sixtuple (K, , , , s, H): • K is a finite set of states, • is the input alphabet, which does not contain , • is the tape alphabet, which must, at a minimum, contain and have as a subset, • s K is the start state, • H K is the set of halting states, and • is the transition function. It maps from: (K - H) to K {→, }. non-halting state tape character state tape character action (R or L) If contains the transition ((q0, a), (q1, b, A)) then, whenever M is in state q0 and the character under the read/write head is a, M will go to state q1, write b, and then move the read/write head as specified by A (either one square to the right or one square to the left). Notice that the tape symbol is special in two ways: • Initially, all tape squares except those that contain the input string contain . • The input string may not contain . But those are the only ways in which is special. A Turing machine may write just as it writes any other symbol in its tape alphabet . Be careful, though, if you design a Turing machine M that does write . Make sure that M can tell the difference between running off the end of the input and hitting a patch of ’s within the part of the tape it is working on. Some books use a definition of a Turing machine that does not allow writing . But we allow it because it can be quite useful if you are careful. In addition, this definition allows a Turing machine to output a string that is shorter than its input by writing ’s as necessary. Define the active tape of a Turing machine M to be the shortest fragment of M’s tape that includes the square under the read/write head and all the nonblank squares. We require that be defined for all (state, input) pairs unless the state is a halting state. Notice that is a function, not a relation. So this is a definition for deterministic Turing machines. One other important observation: a Turing machine can produce output, namely the contents of its tape when it halts. Example 17.1 Add b’s to Make Them Match the a’s Design a Turing machine M that takes as input a string in the language {aibj : 0 j i} and adds b's as required to make the number of b's equal the number of a's. The input to M will look like this: … a a a b … Chapter 17 266 Turing Machines On that input, the output (the contents of the tape when M halts) should be: … a a a b b b … M will operate as follows: 1. Move one square to the right. If the character under the read/write head is , halt. Otherwise, continue. 2. Loop: 2.1 Mark off an a with a $. 2.2 Scan rightward to the first b or . If b, mark it off with a # and get ready to go back and find the next matching a, b pair. If , then there are no more b’s but there are still a’s that need matches. So it is necessary to write another b on the tape. But that b must be marked so that it cannot match another a. So write a #. Then get ready to go back and look for remaining unmarked a’s. 2.3 Scan back leftward looking for a or . If a, then go back to the top of the loop and repeat. If , then all a’s have been handled. Exit the loop. (Notice that the specification for M guarantees that there will not be more b’s than a’s.) 3. Make one last pass all the way through the nonblank area of the tape, from left to right, changing each $ to an a and each # to a b. 4. Halt. M = {{1, 2, 3, 4, 5, 6}, {a, b}, {a, b, , $, #}, , 1, {6}), where = ( ((1, ), (2,, →)), ((1, a), (2,, →)), {These four transitions are required because M must be ((1, b), (2,, →)), defined for every state/ input pair, but since it isn’t possible ((1, $), (2,, →)), to see anything except in state 1, it doesn’t matter what ((1, #), (2,, →)), they do. } ((2, ), (6, $, →)), ((2, a), (3, $, →)), ((2, b), (3, $, →)), {Three more unusable elements of . We’ll omit the rest ((2, $), (3, $, →)), here for clarity.} ((2, #), (3, $, →)), ((3, ), (4, # , )), ((3, a), (3, a, →)), ((3, b), (4, #,)), ((3, $), (3, $, →)), ((3, #), (3, #, →)), ((4, ), (5, , →)), ((4, a), (3, $, →)), ((4, $), (4, $, )), ((4, #), (4, # , )), ((5, ), (6, ,)), {State 6 is a halting state and so has no transitions out of it.} ((5, $), (5, a, →)), ((5, #), (5, b , →)) ) People find it nearly impossible to read transition tables like this one, even for very simple machines. So we will adopt a graphical notation similar to the one we used for both FSMs and PDAs. Since each element of has five components, we need a notation for labeling arcs that includes all the required information. Let x/t/a on an arc of M mean that the transition can be taken if the character currently under the read/write head is x. If it is taken, write t and then move the read/write head as specified by a.