Turing achines Wolfgang Schreiner Wolfgang.Schreiner@risc.jku.at Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria http://www.risc.jku.at 1. Turing achines Wolfgang Schreiner http://www.risc.jku.at 1/28 Wolfgang Schreiner http://www.risc.jku.at 2/28 Turing achine odel Turing achines Turing machine tape sequence accepted The machine is always in one of a finite set of states. The machine starts its execution in a fixed start state. An infinite tape holds at its beginning the input word. Tape is read and written and arbitrarily moved by the machine. The machine proceeds in a sequence of state transitions. achine reads symbol, overwrites it, and moves tape head left or right. The symbol read and the current state determine the symbol written, the move direction, and the next state. If the machine cant make ather transition, it terminates. The machine signals whether it is in an accepting state. If the machine terminates in an accepting state, the word is accepted. Wolfgang Schreiner http://www.risc.jku.at 3/28 Turing achine = (Q,Γ,,Σ,δ,q,F ): The state set Q, a fine set of states. A tape alphabet Γ, a finite set of tape symbols. The blank symbol Γ. An input alphabet Σ Γ\{}. The (partial) transition function δ : Q Γ p Q Γ { L, R }, δ(q,x) = (q,x, L / R )... reads in state q symbol x, goes to state q, writes symbol x, and moves the tape head left/right. The start state q Q A set of accepting states (final states) F Q. The crucial difference to an automaton is the infinite tape that can be arbitrarily moved and written. Wolfgang Schreiner http://www.risc.jku.at 4/28
Example Turing achine Simulator = (Q,Γ,,Σ,δ,q,F ) Q = {q,q 1,q 2,q 3,q 4 } Γ = {,,1,X,Y } Σ = {,1} F = {q 4 } q Y,Y q 1 q 3 Y X 1 http://math.hws.edu/eck/js/turing-machine/t.html,y q 2 q 4 δ 1 X Y q (q 1,X,R) (q 3,Y,R) q 1 (q 1,,R) (q 2,Y,L) (q 1,Y,R) q 2 (q 2,,L) (q,x,r) (q 2,Y,L) q 3 (q 4,,R) (q 3,Y,R) q 4 achine accepts every word of form n 1 n (replacing it by X n Y n ). Wolfgang Schreiner http://www.risc.jku.at 5/28 Wolfgang Schreiner http://www.risc.jku.at 6/28 Generalized Turing achines Infinite tape in both directions. Can be simulated by a machine whose tape is infinite in one direction. ultiple tapes. Can be simulated by a machine with a single tape. Nondeterministic transitions. We can simulate a ndeterministic by a deterministic. Let r be the maximum number of choices that can make. operates with 3 tapes. Tape 1 holds the input (tape is only read). writes to tape 2 all finite sequences of numbers 1,...,r. First all sequences of length 1, then all of length 2, etc. After writing sequence s 1 s 2...s n to tape 2, simulates on tape 3. copies the input to tape 3 and performs at most n transitions. In transition i, attempts to perform choice s i. If choice i is t possible or terminates after n transitions in a n-accepting state, continues with next sequence. If terminates in accepting state, accepts the input. Every generalized Turing machine can be simulated by the core form. Wolfgang Schreiner http://www.risc.jku.at 7/28 1. Turing achines Wolfgang Schreiner http://www.risc.jku.at 8/28
Turing achine Configurations Configuration a 1...a k q a k+1...a m : q: the current state of. a k+1 : the symbol currently under the tape head. a 1...a k : the portion of the tape left to the tape head. a k+2...a m : the portion right to the head (followed by...). ove relation: a 1...a k q a k+1...a m b 1...b l p b l+1...b m If is a situation described by the left configuration, it can make a transition to the situation described by the right configuration. a i = b i for all i k + 1 and one of the following: l = k + 1 and δ(q,a k+1 ) = (p,b l,r), l = k 1 and δ(q,a k+1 ) = (p,b l+2,l). Extended move relation: c 1 c 2 can make in an arbitrary number of moves a transition from the situation described by configuration c 1 to the one described by c 2. c 1 c 2 : c 1 = c 2 c 1 i+1 c 2 : c : c 1 i c c c 2 c 1 c 2 : i N : c 1 i c 2 Wolfgang Schreiner http://www.risc.jku.at 9/28 The Language of a Turing achine The language L() of Turing machine = (Q,Γ,,Σ,δ,q,F ): The set of all inputs that drive from its initial configuration to a configuration with an accepting state such that from this configuration further move is possible: L() := { w Σ a,b Γ,q Q : q w a q b q F a,b Γ,q Q : a q b a q b L is a recursively enumerable language: There exists a Turing machine such that L = L(). L is a recursive language: There exists a Turing machine such that L = L() and terminates for every possible input. Every recursive language is recursively enumerable; as we will see, the converse does t hold. Wolfgang Schreiner http://www.risc.jku.at 1/28 } Recursiv. Enumerable/Recursive Languages Theorem: L is recursive, if and only if both L and its complement L are recursively enumerable. Proof : Let L be a recursive. Since by definition L is recursively enumerable, it remains to be shown that also L is recursively enumerable. Since L is recursive, there exists a Turing machine such that halts for every input w: if w L, then accepts w; if w L, then does t accept w. With the help of, we can construct the following with L( ) = L: function (w): case (w) of : return : return end case Wolfgang Schreiner http://www.risc.jku.at 11/28 w Recursiv. Enumerable/Recursive Languages Proof : Let L be such that both L and L are recursively enumerable. We show that L is recursive. Since L is r.e., there exists such that L = L() and halts for w L with (w) =. Since L is r.e., there exists with L = L() and halts for w L with (w) =. We can thus construct with L( ) = L that always halts: function (w): parallel begin if (w) = then return loop forever end begin if (w) = then return loop forever end end parallel Wolfgang Schreiner http://www.risc.jku.at 12/28 w
Closure of Recursive Languages Let L,L 1,L 2 be recursive languages. Then also the complement L, the union L 1 L 2, the intersection L 1 L 2 are recursive languages. Proof by construction of the corresponding Turing machines. 1. Turing achines Wolfgang Schreiner http://www.risc.jku.at 13/28 Wolfgang Schreiner http://www.risc.jku.at 14/28 Enumerators Recognizing versus Generating Languages Turing machine = (Q,Γ,, /,δ,q,f ) with special symbol # Γ. is an enumerator, if has an additional output tape on which moves its tape head only to the right, and writes only symbols different from. The generated language Gen() of enumerator is the set of all words that eventually writes on its output tape. The end of each word is marked by a trailing #. may run forever and thus Gen() may be infinite. Theorem: L is recursively enumerable, if and only if there exists some enumerator such that L = Gen(). Proof : Let L be recursively enumerable, i.e., L = L( ) for some. We construct enumerator such that L = Gen(). procedure : loop produce next (m,n) on working tape if (w m ) = in at most n steps then write w m to output tape end loop end procedure (,),(1,),(1,1),(2,),... (m,n) w m n write w m w m Wolfgang Schreiner http://www.risc.jku.at 15/28 Wolfgang Schreiner http://www.risc.jku.at 16/28
Recognizing versus Generating Languages Proof : Let L be such that L = Gen() for some enumerator. We show that there exists some Turing machine such that L = L( ). 1. Turing achines function (w): while is t terminated do writes next word w if w = w then return end while return w w on tape? Recognizing is possible, if and only if generating is possible. Wolfgang Schreiner http://www.risc.jku.at 17/28 Wolfgang Schreiner http://www.risc.jku.at 18/28 Functions Functions Take binary relation f A B. f : A B: f is a total function from A to B. For every a A, there is exactly one b B such that (a,b) f. f : A p B: f is a partial function from A to B. For every a A, there is at most one b B such that (a,b) f. Auxiliary tions: domain(f ) := {a b : (a,b) f } range(f ) := {b a : (a,b) f } f (a) := such b : (a,b) f Every total function f : A B is a partial function f : A p B; every partial function f : A p B is a total function f : domain(f ) B. Wolfgang Schreiner http://www.risc.jku.at 19/28 Let f : Σ p Γ where Σ Γ. f is a function over words in some alphabets. f is Turing computable, if there exists a Turing machine such that for input w (i.e. initial tape content w...), terminates in an accepting state, if and only if w domain(f ); for input w, terminates in an accepting state with output w (i.e. final tape content w...), if and only if w = f (w). Not every function f : Σ p Γ is Turing computable: The set of all Turing machines is countably infinite: all machines can be ordered in a single list (in the alphabetic order of their definitions). The set of all functions Σ p Γ is more than countably infinite (Cantor s diagonalization argument). Consequently, there are more functions than Turing machines. computes f, if terminates for arguments in the domain of f with output f (a) and does t terminate for arguments outside the domain. Wolfgang Schreiner http://www.risc.jku.at 2/28
Example Example (Contd) We show that natural number subtraction is Turing computable. Subtraction on N: = (Q,Γ,,Σ,δ,q,F ) Q = {q,...,q 6 } Σ = {},Γ = {,1,},F = {q 6 } q m n := Unary representation of n N: { m n if m n else... }{{} n times Input shall lead to output. 2 1 = 1. L( ) Idea: replace every pair of in m and n by. Wolfgang Schreiner http://www.risc.jku.at 21/28 δ 1 q (q 1,,R) (q 5,,R) (q 5,,R) q 1 (q 1,,R) (q 2,1,R) (q 2,1,R) q 2 (q 3,1,L) (q 2,1,R) (q 4,,L) q 3 (q 3,,L) (q 3,1,L) (q,,r) q 4 (q 4,,L) (q 4,,L) (q 6,,R) q 5 (q 5,,R) (q 5,,R) (q 6,,R) q 6 q 1 q 2 1, 1 q 3,1 q 4 q 5,1 In q, the leading is replaced by. In q 1, searches for the next and replaces it by a 1. In q 2, searches for the next and replaces it by 1, then moves left. In q 3, searches for previous, moves right and starts from begin. In q 4, has found a instead of and replaces all previous 1 by. In q 5, n is (has become) ; the rest of the tape is erased. In q 6, the computation successfully terminates. Wolfgang Schreiner http://www.risc.jku.at 22/28,1 1, q 6 Example (Contd) Turing Computability 2 1 = 1: q q 1 q 1 1q 2 q 3 11 q 3 11 q 3 11 q 11 q 1 11 1q 2 1 11q 2 1q 4 1 q 4 1 q 4 q 6 1 2 = : q q 1 1q 2 q 3 11 q 3 11 q 11 q 5 1 q 5 q 5 q 6. For m > n, leading blanks still have to be removed. Theorem: f : Σ p Γ is Turing computable, if and only if L f := {(a,b) Σ Γ a domain(f ) b = f (a)} is recursively enumerable. Proof : Since f : Σ p Γ is Turing computable, there exists a Turing machine which computes f. To show that L f is r.e., we construct with L( ) = L f : function (a,b): b (a) if b = b then return else return (a,b) b a b = Wolfgang Schreiner http://www.risc.jku.at 23/28 Wolfgang Schreiner http://www.risc.jku.at 24/28
Turing Computability Proof : Since L f is recursively enumerable, there exists an enumerator with Gen() = L f. We construct the following Turing machine which computes f : 1. Turing achines function (a): while is t terminated do writes (a,b ) to tape if a = a then return b end while loop forever a (a,.) on tape? (a, b) b Computing is possible, if and only if recognizing is possible. Wolfgang Schreiner http://www.risc.jku.at 25/28 Wolfgang Schreiner http://www.risc.jku.at 26/28 Algorithms The Church-Turing Thesis Computer science is based on algorithms. Compute as follows the greatest common divisor of two natural numbers m,n that are t both : 1. If m =, the result is n. 2. If n =, the result is m. 3. If m > n, subtract n from m and continue with step 1. 4. Otherwise subtract m from n and continue with step 1. Euklid( m, n, r) m n? ja ja m > n? nein m m n ja r n m =? n n m r m nein nein Euklid( m, n, r): while m n do if m > n then m m n else n n m if m = then r n else r m end Euklid. What is an algorithm and what is computable by an algorithm? Church-Turing Thesis: Every problem that is solvable by an algorithm (in an intuitive sense) is solvable by a Turing machine. Thus the set of intuitively computable functions is identical with the set of Turing computable functions. Replaces fuzzy tion algorithm by precise tion Turing machine. Unprovable thesis, exactly because the tion algorithm is fuzzy. Substantially validated, because many different computational models have more computational power than Turing machines. Random access machines, loop programs, recursive functions, goto programs, λ-calculus, rewriting systems, grammars,... Turing machines represent the most powerful computational model kwn, but there are many other equally powerful ( Turing complete ) models. Wolfgang Schreiner http://www.risc.jku.at 27/28 Wolfgang Schreiner http://www.risc.jku.at 28/28