Computer Science 18. Turing Machines
Total Page:16
File Type:pdf, Size:1020Kb
COMPUTER SCIENCE Universality and computability SEDGEWICK/WAYNE Fundamental questions • What is a general-purpose computer? • Are there limits on the power of digital computers? • Are there limits on the power of machines we can build? Computer Science Pioneering work at Princeton in the 1930s. Computer 18. Turing Machines Science An Interdisciplinary Approach R O B E R T S E D G E W I C K KEVIN WAYNE David Hilbert Kurt Gödel Alsonzo Church Alan Turing Section 7.4 1862−1943 1906−1978 1903−1995 1912−1954 http://introcs.cs.princeton.edu Asked the questions Solved the math Solved the decision Provided THE answers problem problem 2 Context: Mathematics and logic COMPUTER SCIENCE SEDGEWICK/WAYNE Principia Mathematics Mathematics. Any formal system powerful enough to express arithmetic. Peano arithmetic Zermelo-Fraenkel set theory . Complete. Can prove truth or falsity of any arithmetic statement. Consistent. Cannot prove contradictions like 2 + 2 = 5. Decidable. An algorithm exists to determine truth of every statement. 18. Turing Machines •A simple model of computation Q. (Hilbert, 1900) Is mathematics complete and consistent? •Universality A. (Gödel's Incompleteness Theorem, 1931) NO (!!!) •Computability •Implications Q. (Hilbert's Entscheidungsproblem) Is mathematics decidable? A. (Church 1936, Turing 1936) NO (!!) 3 CS.18.A.Turing.Machine Starting point Previous lecture: DFAs Goals A DFA is an abstract machine that solves a pattern matching problem. • Develop a model of computation that encompasses all known computational processes. • A string is specified on an input tape (no limit on its length). • Make the model as simple as possible. • The DFA reads each character on input tape once, moving left to right. • The DFA lights "YES" if it recognizes the string, "NO" otherwise. Each DFA defines a set of strings (all the strings that it recognizes). Example: A familiar computational process. NO 1 0 1 0 3 1 4 2 Characteristics 7 1 8 2 YES • Discrete. 1 0 3 2 4 • Local. b b a a b b a b b • States. 5 6 This lecture: Turing machines Previous lecture: DFA details and example A Turing machine (TM) is an abstract model of computation. A DFA is an abstract machine with a finite number of states, each labelled Y or N and • A string is specified on a tape (no limit on its length). transitions between states, each labelled with a symbol. One state is the start state. • The TM reads and writes characters on the tape, moving left or right. • Begin in the start state. • The TM lights "YES" if it recognizes the string, "NO" otherwise. • Read an input symbol and move to the indicated state. • The TM may halt, leaving the result of the computation on the tape. • Repeat until the last input symbol has been read. • Turn on the "YES" or "NO" light according to the label on the current state. NO NO HALT a a a Does this DFA recognize Y b N b N this string? YES b YES . b b a a b b a b b 7 8 This lecture: Turing Machine details and example DFAs vs TMs A Turing Machine is an abstract machine with a finite number of states, each labelled Y, N, Similarities H, L, or R and transitions between states, each labelled with a read/write pair of symbols. • Simple model of computation. • Begin in the designated start state. • Input on tape is a finite string with symbols from a finite alphabet. • Read an input symbol, move to the indicated state and write the indicated output. • Finite number of states. • Move tape head left if new state is labelled L, right if it is labelled R. • State transitions determined by current state and input symbol. • Repeat until entering a state labelled Y, N, or H ( and turn on associated light). Differences NO 1:0 #:1 DFAs TMs R #:# L H HALT • Can read input symbols from the tape. • Can read from or write onto the tape. 0:1 • Can only move tape head to the right. • Can move tape head either direction. YES • Tape is finite (a string). • Tape does not end (either direction). • One state transition per input symbol. • No limit on number of transitions. # # # 1 0 1 1 0 0 1 1 1 # # # • Can recognize (turn on "YES" or "NO"). • Can also compute (with output on tape). 9 10 TM example 1: Binary decrementer TM example 1: Binary decrementer Q. What happens when we try to decrement 0? NO NO 0:1 0:1 #:# R #:# L 1:0 H HALT R #:# L 1:0 H HALT YES YES # # # 01 0 1 0 1 0 0 0 0 # # # # # # 1# # # # # 0 0 0 0 # # # A. Doesn't halt! TMs can have bugs, too. Input 1 0 1 0 1 0 0 0 0 Output 1 0 1 0 0 1 1 1 1 Fix to avoid infinite loop. Check for #. 11 12 TM example 2: Binary incrementer TM example 3: Binary adder (method) Note: This adds a 1 at the left as the To compute x + y last step when incrementing 111...1 NO • Move right to right end of y. ... # # 1 0 1 1 + 1 0 1 0 # # ... 1:0 #:1 R #:# L H HALT • Decrement y. ... # # 1 0 1 1 + 1 0 0 1 # # ... 0:1 YES • Move left to right end of x (left of +) . ... # # 1 0 1 1 + 1 0 0 1 # # ... • Increment x. ... # # 1 1 0 0 + 1 0 0 1 # # ... # # # 10 0 1 0 0 1 1 1 1 # # # • Continue until y = 0 is decremented. ... # 1 0 1 0 1 + 1 1 1 1 # # ... Found + when seeking 1? Just decremented 0. Input 1 0 1 0 0 1 1 1 1 • Clean up by erasing + and 1s. ... # 1 0 1 0 1 # # # # # # # ... Clean up Output 1 0 1 0 1 0 0 0 0 13 14 TM example 3: Binary adder Simulating an infinite tape with two stacks # 1 0 1 1 + 1 0 1 0 # NO Q. How can we simulate a tape that is infinite on both ends? 0:1 Find + A. Use two stacks, one for each end. # 1 0 1 1 + 1 0 0 1 # Decrement y L 1:0 L HALT private Stack<Character> left; +:# private Stack<Character> right; Halt +:+ # 1 0 1 1 + 1 0 0 1 # 1:# YES private char read() # # # 1 0 1 1 + 1 0 1 0 # # # { R #:# H if (right.isEmpty()) return '#'; return right.pop(); "tape head" is top of right stack # 1 1 0 0 + 1 0 0 1 # Clean Up } assumes #:# write just 1 0:1 private char write(char c) after each { right.push(c); } read 1 move 1 ... Increment x Find right end R L right private void moveRight() + + #:1 { 1 1 1 0 1 0 1 + 1 1 1 1 # 1:0 char c = '#'; move 0 1 0 if (!right.isEmpty()) c = right.pop(); left left.push(c); 0 0 0 1 } 1 0 1 0 1 # # # # # # 1 1 1 0 private void moveLeft() # # # 1 0 1 1 + 1 0 1 0 # # # { # # # # char c = '#'; empty? assume # is there if (!left.isEmpty()) c = left.pop(); right.push(c); } 15 16 Simulating the operation of a Turing machine COMPUTER SCIENCE SEDGEWICK/WAYNE public class TM fixes bug { 0:1 #:# private int state; R #:# L 1:0 H private int start; 0 1 2 private String[] action; private ST<Character, Integer>[] next; private ST<Character, Character>[] out; action[] next[] out[] /* Stack code from previous slide */ 0 1 # 0 1 # public TM(In in) { /* Fill in data structures */ } 0 R 0 0 0 1 0 0 1 # public String simulate(String input) 1 L 1 1 2 2 1 1 0 # { 18. Turing Machines state = start; 2 H 2 2 2 2 2 0 1 # for (int i = input.length()-1; i >= 0; i--) entries in gray are implicit in graphical representation right.push(input.charAt(i); while (action(state).equals("L") || •A simple model of computation action(state).equals("R")) % more dec.txt { 3 01# 0 char c = read(); •Universality R 0 0 1 0 1 # state = next[state].get(c); L 1 2 2 1 0 # write(out[state].get(c)); •Computability if (action[state].equals("R") moveRight(); H 2 2 2 0 1 # if (action[state].equals("L") moveLeft(); % java TM dec.txt } 000111 •Implications return action[state]; 000110 } 010000 public static void main(String[] args) 001111 { /* Similar to DFA's main() */ } 000000 } 111111 17 CS.18.B.Turing.Universality Representing a Turing machine Universal Turing machine (UTM) NO Turing's key insight. A TM is nothing more than a finite sequence of symbols. Universal Turing machine. A TM that takes as input HALT any TM and input for that TM on a TM tape. UTM dec.txt YES decrementer TM 0:1 #:# 3 01# 0 . # # # 1 1 0 0 0 3 0 1 # 0 R 0 0 1 0 1 $ L 1 2 1 1 0 $ H 2 2 2 0 1 $ / R 0 0 1 0 1 # R #:# L 1:0 H L 1 2 2 1 0 # 0 1 2 H 2 2 2 0 1 # input to decrementer TM decrementer TM Result. Whatever would happen if that TM were to NO Implication. Can put a TM and its input on a TM tape. run with that input (could loop or end in Y, N or H). UTM HALT YES . # # # 1 0 1 1 1 # # # 1 1 0 0 0 3 0 1 # 0 R 0 0 1 0 1 # L 1 2 1 1 0 # H 2 2 2 0 1 # result that decrementer TM would produce Turing.