Turing Machines Part II COMP2600 Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2, 2016 Slides created by Katya Lebedeva COMP 2600 Turing Machines 1
Why computer programs are not well-suited to develop a theory of computation? complex language constructs finite memory state of the computation is complicated to represent would need to show that the results for a specific programming language are in fact general We resort to an abstract computing device, the Turing Machine (TM). It represents a simple and universal programming language has an unbounded memory has states of computation that are easy to describe can simulate any known computing device COMP 2600 Turing Machines 2
Definition A Turing Machine has the form (Q,q 0,F, Γ,Σ,Λ, δ), where Q is the finite set of states q 0 Q is the initial state F Q is the set of final/accepting states Γ is the tape alphabet (the finite set of tape symbols) Σ Γ is the input alphabet (the finite set of input symbols) Λ Γ/Σ is the blank symbol δ is a (partial) transition function δ : Q Γ Q Γ {L,R,S} COMP 2600 Turing Machines 3
Transitions δ(q,x) = (p,y,d) means that if M is in state q and the tape head is over symbol x, then changes state to p replaces x with y on the tape moves the head in the direction given by d Note: the TM is deterministic, i.e. for each (q,x) we have at most one move. δ(q,x) can be undefined for some q and x. COMP 2600 Turing Machines 4
Initialisation some input (a finite string w over Σ) is written on the tape every other tape cell is a blank symbol Λ the tape head is positioned on the left-most symbol of w the state is the start state q 0 Acceptance w Σ is accepted by M, if M, when started with w on the tape eventually enters a final state. COMP 2600 Turing Machines 5
Halting A state q is halting for a symbol x Γ if δ(q,x) is not defined. A state q is halting if it is halting for all symbols in Γ. We can assume without loss of generality that all final states are halting. Rejection M rejects a string w in either of the following cases it halts in a non-final state it executes transitions forever, without ever reaching a final state (in this case we say M loops on w ) COMP 2600 Turing Machines 6
Automata vs. Turing Machines Finite State Automata and Pushdown Automata scan over w (from left to right) and accept/reject when they reach the end of w. Turing Machines can move back and forth over w and accept/reject when they halt or reject when they loop forever. COMP 2600 Turing Machines 7
Computer vs. Turing Machine A Turing Machine has an infinite tape. Physical computing devices have finite memory, so they can only exist in a finite number of states. They are Finite State Automata. The number of states can be very, very large: for example 2 4294967296 Physical computers can only approximate Turing Machines :-( If a Turing Machine has a finite tape we can simulate it with an FSA: Just take the finite state control and replicate it for every possible string that could exist on the tape. Combinatorial explosion! For a tape containing 16 binary symbols, we need at least 2 16 states in our FSA! COMP 2600 Turing Machines 8
Random Access Memory saves the day An FSA has no auxiliary store/memory...... this makes them less expressive. A PDA has an auxiliary store...... but we can only directly access the symbol on the top of the stack. A Turing Machine is infinite...... but we have to access the tape sequentially. My laptop is finite...... but accessing a store location requires only a small, constant time.... that s usually enough. COMP 2600 Turing Machines 9
Using States to Remember a Tape Symbol 1 1, L 0 0, L S 0 0, R Λ 0, L Λ Λ, R Λ 1, L U 0 T 0 Λ, R 1 Λ, R 1 1, R Given a string of 0 or 1 surrounded by blanks, this machine repeatedly forever erases the leftmost bit, and writes it on the right hand end. (Not so useful, but illustrates the point) We use the choice of states U 0 0 0, R U 1 1 1, R or U 1 to remember which symbol has been erased and is to be written Note: We begin in state S, the head points at the first symbol of the given word. The first transition will move the head to the left, replacing the first symbol 1 with 1 or 0 with 0. COMP 2600 Turing Machines 10
Example Define a TM that accepts exactly all strings in L = {w# wt w {0,1} +,t {0,1,#} } Initially... Λ Λ... w... #... #... w...... t... Λ Λ... Idea: remember leftmost symbol and erase it move to leftmost symbol after # s if the two don t match, reject otherwise replace the symbol by #, move left and start again COMP 2600 Turing Machines 11
... Λ Λ... w... #... #... w...... t... Λ Λ... M = (Q,q 0,F,Γ,Σ,Λ,δ) Q = {q 0,q 1,q 2,q 3,q 4,q 5,q 6,q 7 } F = {q 7 } Γ = {0,1,#,Λ} Σ = {0,1,#} COMP 2600 Turing Machines 12
... Λ Λ... w... #... #... w...... t... Λ Λ... erase 0 and start looking for matching 0 δ(q 0,0) = (q 1,Λ,R) erase 1 and start looking for matching 1 δ(q 0,1) = (q 2,Λ,R) COMP 2600 Turing Machines 13
... Λ Λ... w... #... #... w...... t... Λ Λ... skip over 0 s and 1 s until first # is found (remembering 0) δ(q 1,0) = (q 1,0,R) δ(q 1,1) = (q 1,1,R) δ(q 1,#) = (q 3,#,R) skip over 0 s and 1 s until first # is found (remembering 1) δ(q 2,0) = (q 2,0,R) δ(q 2,1) = (q 2,1,R) δ(q 2,#) = (q 4,#,R) COMP 2600 Turing Machines 14
... Λ Λ... w... #... #... w...... t... Λ Λ... skip over # s, look for 0 and replace it with # δ(q 3,#) = (q 3,#,R) δ(q 3,0) = (q 5,#,L) Note: if right after # s a 1 is found, M rejects skip over # s, look for 1 and replace it with # δ(q 4,#) = (q 4,#,R) δ(q 4,0) = (q 5,#,L) Note: if right after # s a 0 is found, M rejects COMP 2600 Turing Machines 15
... Λ Λ... w... #... #... w...... t... Λ Λ... Move left skipping # s: δ(q 5,#) = (q 5,#,L) If to the left of # s a 0 or 1 is found, move to q 6 to skip them too: δ(q 5,0) = (q 6,0,L) δ(q 5,1) = (q 6,1,L) If to the left of # s a Γ is found, accept: δ(q 5,Λ) = (q 7,Λ,R) COMP 2600 Turing Machines 16
... Λ Λ... w... #... #... w...... t... Λ Λ... Move left skipping over 0 s and 1 s and restart δ(q 6,0) = (q 6,0,L) δ(q 6,1) = (q 6,1,L) δ(q 6,Λ) = (q 0,Λ,R) COMP 2600 Turing Machines 17
COMP 2600 Turing Machines 18
0 0,R 1 1,R # #,R # #,L q 0 0 Λ,R start q 1 1 Λ,R 0 0,R 1 1,R # #,R q 3 # #,R 0 #,L 1 #,L Λ Λ,R q 5 q 7 Λ Λ,R q 2 # #,R q 4 0 0,L 1 1,L q 6 COMP 2600 Turing Machines 19 0 0,L 1 1,L
Instanteneous Descriptions A configuration of a TM is represented as α 1 qα 2, where α 1,α 2 Γ and q Q Meaning non-blanc portion of the tape contains α 1 α 2 the machine is in state q the head is positioned on left-most symbol of α 2... Λ Λ α 1 α 2 Λ Λ... ID 1 ID 2 means that there is a transition of M from ID 1 to ID 2 ID 1 ID 2 means that there is a sequence of transitions of M that leads from ID 1 to ID 2 COMP 2600 Turing Machines 20
Transitions for 01##01: q 0 01##01 q 1 1##01 1q 1 ##01 1#q 3 #01 1##q 3 01 1#q 5 ##1 1q 5 ###1 q 5 1###1 q 6 Λ1###1 continued on the next slide! COMP 2600 Turing Machines 21
q 0 1###1 q 2 ###1 #q 4 ##1 ##q 4 #1 ###q 4 1 ##q 5 ## #q 5 ### q 5 #### q 5 Λ#### q 7 #### COMP 2600 Turing Machines 22
Programming Techniques for TMs Some notational conventions make it easier to write TM programs. Idea: Use structured states and tape symbols. Storage in the state Idea: State names are tuples of the form [q,d 1,...,D k ] each D i acts as a stored symbol q is the control portion of the state COMP 2600 Turing Machines 23
Example M = (Q,q 0,F,Γ,Σ,Λ,δ) E = 01 + 10 L(E) = {0,01,011,...,1,10,100,...} Q = {[q i,x] i {a,b},x {0,1, }} = {[q a,0],[q a,1],[q a, ],[q b,0],[q b,1],[q b, ]} q 0 = [q a, ] F = {[q b, ]} Γ = {0,1,Λ} Σ = {0,1} COMP 2600 Turing Machines 24
Meaning of [q i,x]: control portion q a M has not yet read the first input symbol q b M has read the first input symbol data portion x is the first input symbol read COMP 2600 Turing Machines 25
Transitions: M remembers in [q 1,x] that it has read x δ([q a, ],x) = ([q b,x],x,r) for x {0,1} M moves right as long as it sees a symbol different from the first symbol δ([q b,0],1) = ([q b,0],1,r) δ([q b,1],0) = ([q b,1],0,r) M accepts when it reaches the first blanc symbol δ([q b,x],λ) = ([q b, ],Λ,R) for x {0,1} COMP 2600 Turing Machines 26
Multiple tracks Idea: View tape as having multiple tracks, i.e. each symbol in Γ has multiple components:... 0 # Λ...... 1 0 0...... a a c... The symbols on the tape are [ 01 a ], [ #0 a ] and [ Λ0 c ]. COMP 2600 Turing Machines 27
Example L = {ww w {0,1} + } Two stages find the midpoint match symbols To find the midpoint, view the tape as 2 tracks: [ ] [ ] [ ] [ ] [ ] Γ = { ΛΛ, Λ0, Λ1, *1, *0 }... Λ Λ Λ Λ Λ...... 0 1 1 0 1 1... Idea: Put markers on two out-most symbols and move them inwards! COMP 2600 Turing Machines 28
Extensions of the basic TM Multi-tape TM A Multi-tape TM is like an ordinary Turing machine with several tapes. Each tape has its own head. Initially the input w is on tape 1 with the head on the left-most symbol. The other tapes are all blank. Transitions specify behaviour of each head independently: δ(q,x 1,...,x k ) = (p,(y 1,d 1 ),...,(y k,d k )) where x i is the symbol under head i is the new symbol under head i y i d i direction in which head i moves Note: we can simulate a k-tape TM with a 1-tape TM. COMP 2600 Turing Machines 29
Extensions of the basic TM Non-deterministic TM (NTM) In a (deterministic) TM δ(q, x) is unique or undefined. In a NTM, δ(q,x) is a finite set of triplets δ(q,x) = {(p 1,y 1,d 1 ),...,(p m,y m,d m )} At each step, a NTM can non-deterministically choose which transition to make. As for NFA s a NTM accepts if there is a sequence of guesses (non-deterministic choice) that leads to a final state. COMP 2600 Turing Machines 30
Languages and TMs The language recognised by a TM M L(M) = {w Σ q 0 w α 1 pα 2 with p F,α 1,α 2 Γ } The class of languages accepted by TMs is called recursively enumerable languages. for a string w in the language the TM halts on input w in a final state for a string w not in the language the TM either halts in a non-final state or loops forever Those languages for which the TM halts (regardless of whether it accepts or not) are called recursive languages. COMP 2600 Turing Machines 31
Recursive Languages A language L is recursive if L = L(M) for some TM M such that if w L, then M accepts (and therefore halts) if w is not in L, then M eventually halts, although it never enters an accepting state A TM of this type corresponds to an algorithm: a well defined sequence of steps that always finishes and produces an answer. If we think of a language L as a problem, then problem L is called decidable if it is a recursive language; and it is called undecidable if it is not a recursive language. Recursive (or decidable ) languages are not only recursively enumerable but are also accepted by a TM that always halts regardless of whether or not it accepts. COMP 2600 Turing Machines 32