Turing Machines Presentation - Princeton University
Turing Machines Presentation - Princeton University
S E D G E W I C K / W A Y N E
http://introcs.cs.princeton.edu
Principia Mathematics
Fundamental questions Mathematics. Any formal system powerful enough to express arithmetic. Peano arithmetic
• What is a general-purpose computer? Zermelo-Fraenkel set theory
.
• Are there limits on the power of digital computers? .
.
• Are there limits on the power of machines we can build? 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.
Pioneering work at Princeton in the 1930s.
David Hilbert Kurt Gödel Alsonzo Church Alan Turing Q. (Hilbert's Entscheidungsproblem) Is mathematics decidable?
1862 1943 1906 1978 1903 1995 1912 1954
Goals
• Develop a model of computation that encompasses all known computational processes.
• Make the model as simple as possible.
A DFA is an abstract machine that solves a pattern matching problem. A Turing machine (TM) is an abstract model of computation.
• A string is specified on an input tape (no limit on its length). • A string is specified on a tape (no limit on its length).
• The DFA reads each character on input tape once, moving left to right. • The TM reads and writes characters on the tape, moving left or right.
• The DFA lights "YES" if it recognizes the string, "NO" otherwise. • The TM lights "YES" if it recognizes the string, "NO" otherwise.
Each DFA defines a set of strings (all the strings that it recognizes). • The TM may halt, leaving the result of the computation on the tape.
NO NO
HALT
YES YES
b b a a b b a b b ... ...
7 8
Previous lecture: DFA details and example This lecture: Turing Machine details and example
A DFA is an abstract machine with a finite number of states, each labelled Y or N and A Turing Machine is an abstract machine with a finite number of states, each labelled Y, N,
transitions between states, each labelled with a symbol. One state is the start state. H, L, or R and transitions between states, each labelled with a read/write pair of symbols.
• Begin in the start state. • Begin in the designated start state.
• Read an input symbol and move to the indicated state. • Read an input symbol, move to the indicated state and write the indicated output.
• Repeat until the last input symbol has been read. • Move tape head left if new state is labelled L, right if it is labelled R.
• Turn on the "YES" or "NO" light according to the label on the current state. • Repeat until entering a state labelled Y, N, or H ( and turn on associated light).
NO NO
1:0 #:1
a a a
Does this DFA recognize
Y b N b N this string? R #:# L H HALT
b 0:1
YES YES
b b a a b b a b b # # # 1 0 1 1 0 0 1 1 1 # # #
9 10
Similarities
• Simple model of computation.
• Input on tape is a finite string with symbols from a finite alphabet.
NO
• Finite number of states.
0:1
• State transitions determined by current state and input symbol.
1:0
R #:# L H HALT
Differences
YES
DFAs TMs # # # 0
1 0 1 0 1 0 0 0 0 # # #
• Can read input symbols from the tape. • Can read from or write onto the tape.
• Can only move tape head to the right. • Can move tape head either direction.
• Tape is finite (a string). • Tape does not end (either direction).
Input 1 0 1 0 1 0 0 0 0
• One state transition per input symbol. • No limit on number of transitions.
• Can recognize (turn on "YES" or "NO"). • Can also compute (with output on tape). Output 1 0 1 0 0 1 1 1 1
11 12
TM example 1: Binary decrementer TM example 2: Binary incrementer
Q. What happens when we try to decrement 0? Note: This adds a 1 at the left as the
last step when incrementing 111...1
NO NO
0:1 #:# 1:0 #:1
# # # 1
# # # # # 0 0 0 0 # # # # # # 1
0 0 1 0 0 1 1 1 1 # # #
# 1 0 1 1 + 1 0 1 0 # NO
To compute x + y Find +
0:1
# 1 0 1 1 + 1 0 0 1 # Decrement y L 1:0 L HALT
• Move right to right end of y. ... # # 1 0 1 1 + 1 0 1 0 # # ...
+:# +:+
1:# Halt
# 1 0 1 1 + 1 0 0 1 # YES
• Decrement y. ... # # 1 0 1 1 + 1 0 0 1 # # ...
R #:# H
# 1 1 0 0 + 1 0 0 1 # Clean Up
• Move left to right end of x (left of +) . ... # # 1 0 1 1 + 1 0 0 1 # # ... #:#
0:1
... Increment x
• Increment x. ... # # 1 1 0 0 + 1 0 0 1 # # ... Find right end R L
#:1
1 0 1 0 1 + 1 1 1 1 # 1:0
• Continue until y = 0 is decremented. ... # 1 0 1 0 1 + 1 1 1 1 # # ...
Found + when seeking 1? Just decremented 0.
1 0 1 0 1 # # # # # #
• Clean up by erasing + and 1s. ... # 1 0 1 0 1 # # # # # # # ...
# # # 1 0 1 1 + 1 0 1 0 # # #
Clean up
15 16
Simulating an infinite tape with two stacks Simulating the operation of a Turing machine
http://introcs.cs.princeton.edu http://introcs.cs.princeton.edu
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
any TM and input for that TM on a TM tape. UTM HALT
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 $
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
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 #
• Critical part of the calculation is to update machine which can be used to do any computable task. YES
Profound implications
• Any machine that can simulate a TM can simulate a UTM.
Want to see the details or build your own TM? • Any machine that can simulate a TM can do any computable task.
Use the booksite's TM development environment.
Church-Turing thesis. Turing machines can do anything that can be described by any Evidence. Many, many models of computation have turned out to be equivalent (universal).
physically harnessable process of this universe: All computational devices are equivalent.
model of computation description
YES
PCP systems string matching puzzles (stay tuned)
... ...
Example of a universal model: Extended Lindenmayer systems for synthetic plants COMPUTER SCIENCE
S E D G E W I C K / W A Y N E
http://introcs.cs.princeton.edu
http://astronomy.swin.edu.au/~pbourke/modelling/plants
27
COMPUTER SCIENCE Post's correspondence problem (PCP)
S E D G E W I C K / W A Y N E
PCP. A family of puzzles, each based on a set of cards. PCP. A family of puzzles, each based on a set of cards.
• N types of cards. • N types of cards.
• No limit on the number of cards of each type. • No limit on the number of cards of each type.
• Each card has a top string and bottom string. • Each card has a top string and bottom string.
Does there exist an arrangement of cards with matching top and bottom strings? Does there exist an arrangement of cards with matching top and bottom strings?
Solution 2 (easy): NO. No way to match even the first character! Challenge for the bored: Find a solution that starts with a card of type 0.
31 32
Post's correspondence problem (PCP) Another impossible problem
PCP. A family of puzzles, each based on a set of cards. Halting problem. Write a Java program that reads in code for Java static method f()
• N types of cards. and an input x, and decides whether or not f(x) results in an infinite loop.
• No limit on the number of cards of each type.
• Each card has a top string and bottom string.
Does there exist an arrangement of cards with matching top and bottom strings? Example 1 (easy). Example 2 (difficulty unknown).
public void f(int x) public void f(int x)
{ {
while (x != 1) while (x != 1)
Involves Collatz conjecture
{ {
(see Recursion lecture)
... if (x % 2 == 0) x = x / 2; if (x % 2 == 0) x = x / 2;
else x = 2*x + 1; else x = 3*x + 1;
} }
} }
0 1 2 3 4 N
f(7): 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Halts only if x is a positive power of 2 f(-17): -17 -50 -25 -74 -37 -110 -55 -164 -82 -41 -122 ... -17 ...
A reasonable idea. Write a program to take N card types as input and solve PCP.
A surprising fact. It is not possible to write such a program. Next. A proof that it is not possible to write such a program.
33 34
Definition. A yes-no problem is undecidable if no Turing machine exists to solve it. Liar paradox (dates back to ancient Greek philosophers).
(A problem is computable if there does exist a Turing machine that solves it.) • Divide all statements into two categories: true and false.
• Consider the statement "This statement is false."
• Is it true? If so, then it is false, a contradiction.
• Is it false? If so, then it is true, a contradiction.
Theorem (Turing, 1936). The halting problem is undecidable. Logical conclusion. Cannot label all statements as true or false.
Profound implications
• There exists a problem that no Turing machine can solve.
• There exists a problem that no computer can solve.
• There exist many problems that no computer can solve (stay tuned).
35 36
37 38
Proof of the undecidability of the halting problem Proof of the undecidability of the halting problem
Theorem (Turing, 1936). The halting problem is undecidable. Theorem (Turing, 1936). The halting problem is undecidable.
39 40
COMPUTER SCIENCE COMPUTER SCIENCE
S E D G E W I C K / W A Y N E S E D G E W I C K / W A Y N E
http://introcs.cs.princeton.edu http://introcs.cs.princeton.edu
Primary implication. If you know that a problem is undecidable... Q. Why is debugging difficult?
A. All of the following are undecidable.
...don't try to solve it!
Hey, Alice. We came up with a
great idea at our hackathon.
We're going for startup funding. Halting problem. Give a function f, does it halt on a given input x?
What's the idea? Totality problem. Give a function f, does it halt on every input x?
No-input halting problem. Give a function f with no input, does it halt?
An app that you can use to
make sure that any app you Program equivalence. Do two functions f() and g() always return same value?
download won't hang your phone! Uninitialized variables. Is the variable x initialized before it's used? LE
AB
Dead-code elimination. Does this statement ever get executed? C ID
DE
Ummm. I think UN
that's undecidable.
Prove each by reduction to the halting problem: A solution would solve the halting problem.
?
Will your app
work on itself ? Q. Why are program development environments complicated?
??? A. They are programs that manipulate programs.
43 44
Another undecidable problem Another undecidable problem
Lambda calculus E
BL
• DA
Formulated by Church in the 1930s to address the Entscheidungsproblem. E CI
U ND
• Also the basis of modern functional languages.
Alsonso Church
1903 1995
Theorem (Church and Turing, 1936). The Entscheidungsproblem is undecidable. Theorem (Post, 1946). Post's correspondence problem is undecidable.
45 46
Examples of undecidable problems from computational mathematics Examples of undecidable problems from computer science
C ID Ex. 2 NO E
. . .
DE BL
BreakUmOffASlice.Subject = "Important Message From " &
UN DA
Application.UserName
CI
BreakUmOffASlice.Body = "Here is that document you asked
E for ... don't show anyone else ;-)"
U ND . . .
Universality. Concept of general-purpose programmable computers. — John Hodges, in Alan Turing, the Enigma
UTM
=
HALT
YES
Turing's paper was published in 1936, ten years before Eckert and Mauchly worked on ENIAC (!) ... ...
http://introcs.cs.princeton.edu http://introcs.cs.princeton.edu