Turing Machine
Turing Machine
Turing Machines
Rothenberg, Germany
1
Outline
Problems that Computers Cannot Solve
The Turing Machine (TM)
Programming Techniques for TM’s
Extensions to the Basic TM
Restricted TM’s
TM’s and Computers
2
8.0 Introduction
Concepts to be taught ---
– Studying questions about what languages can
be defined be any computational device??
– There are specific problems that cannot be
solved by computers! --- undecidable!
– Studying the Turing machine which seems
simple, but can be recognized as an accurate
model for what any physical computing
device is capable of doing.
3
8.1 Problems That Computers
Cannot Solve
Purpose of this section ---
to provide an informal proof , C-
programming-based introduction to proof of a
specific problem that computers cannot solve.
The problem:
whether the first thing a C program prints is
hello, world.
– We will give the intuition behind the formal
proof. 4
8.1 Problems That Computers
Cannot Solve
8.1.1 Programs that print “Hello, World”
– A C program that prints “Hello, World” :
main()
{
print(“hello, world\n”);
}
– Define hello, world problem to be:
determine whether a given C program, with a given
input, prints hello, world as the first 12 characters
that it prints. 5
8.1 Problems That Computers
Cannot Solve
8.1.1 Programs that print “Hello, World”
– The problem described alternatively using symbols:
Is there a program H that could examine any
program P and input I for P, and tell whether P,
run with I as its input, would print hello, world? (A
program H means an algorithm in concept here.)
– The answer is: undecidable! That is, there exists no
such program H. We will prove this by contradiction.
6
8.1 Problems That Computers
Cannot Solve
8.1.2 Hypothetical “Hello, World” Tester
– 1st step: assume H exists in the following form:
I
Hello-world
yes if P, with input I, prints “hello world”
tester
no if
P H Figure 8.3
not.
P H1 hello, world
(print ‘hello, world’ instead of ‘no’)
(2) transform H1 to H2 in the following way –
Hello-world yes
P Figure 8.5
tester
H2 hello, world
Hello-world yes
P Figure 8.5
tester
H2 hello, world
10
8.1 Problems That Computers
Cannot Solve
8.1.2 Hypothetical “Hello, World” Tester
– 3rd step (cont’d):
Prove H2 does not exist by contradiction as follows (cont’d) –
Hello-world yes
H2 Figure 8.6
tester
H2 hello, world
Now,
(1) if the box H2, given itself as input, makes output yes, then it
means that
the input H2, given itself as input, prints hello world as first output.
But this is contradictory because we just suppose that H2, given
itself as input, makes output yes.
11
8.1 Problems That Computers
Cannot Solve
8.1.2 Hypothetical “Hello, World” Tester
– 3rd step (cont’d):
Prove H2 does not exist as follows (cont’d) –
Hello-world yes
H2 Figure 8.6
tester
H2 Hello, world
Hello-world yes
H2 Figure 8.6
tester
H2 Hello, world
if and only if
Old New
problem problem no
16
8.1 Problems That Computers
Cannot Solve
8.1.3 Reducing One Problem to Another
– Example 8.1
We want to prove a new problem P2 (called calls-foo
problem):
“does program Q, given input y, ever call function foo?”
to be undecidable.
Solution: regard Q as P1;
reduce P1: the hello-world problem to P2: the calls-
foo problem in the following way:
17
(continued in the next page)
8.1 Problems That Computers
Cannot Solve
8.1.3 Reducing One Problem to Another
– Example 8.1 (cont’d)
Solution: reduce P1 to P2 in the following way:
If Q has a function called foo, rename it and all calls to that function
a new program Q1 doing the same as Q. ( 去除碰巧同名 )
Add to Q1 a function foo doing nothing & not called a new Q2
Modify Q2 to remember the first 12 characters that it prints, storing
them in a global array A Q3
Modify Q3 so that whenever it executes any output statement, it
checks A to see if it has written 12 characters or more, and if so,
whether hello, world are the first characters. In that case, call the new
function foo R with input z = y. 18
8.1 Problems That Computers
Cannot Solve
8.1.3 Reducing One Problem to Another
– Example 8.1 (cont’d)
Solution (cont’d): Now,
(1) if Q with input y prints hello, world as its first output, then
R will call foo;
(2) if Q with input y does not print hello, world, then R will
never call foo.
(3) That is, R, with input z = y, calls foo if and only if Q, with
input y, prints hello, world.
(4) So, if we can decide whether R, with input z, calls foo, then
we can decide whether Q, with input y, prints hello, world.
19
(5) But the latter is impossible, so the former is impossible.
8.1 Problems That Computers
Cannot Solve
8.1.3 Reducing One Problem to Another
– The above example illustrates how to reduce a
problem to another.
– An illustration of this idea is:
if and only if
Old New
problem problem no
20
8.2 The Turing Machine
Concepts to be taught:
– The study of decidability provides guidance to
programmers about what they might or might not be
able to accomplish through programming.
– Previous problems are dealt with programs. But not
all problems can be solved by programs.
21
8.2 The Turing Machine
Concepts to be taught:
– We need a simple model to deal with other decision
problems (like grammar ambiguity problems)
– The Turing machine is one of such models, whose
configuration is easy to describe, but whose function
is the most versatile: all computations done by a
modern computer can be done by a Turing machine.
(a hypothesis!)
22
8.2 The Turing Machine
8.2.1 The Quest to Decide All Mathematical
Questions
– At the turn of 20th century, D. Hilbert asked:
“whether it was possible to find analgorithm for
determining the truth or falsehood of any
mathematical proposition.”
(in particular, he asked if there was a way to decide
whether any formula in the 1st-order predicate
calculus, applied to integer, was true)
23
8.2 The Turing Machine
8.2.1 The Quest to Decide All Mathematical
Questions
– In 1931, K. Gödel published his incompleteness
theorem:
“A certain formula in the predicate calculus applied
to integers could not be neither proved nor
disproved within the predicate calculus.”
– The proof technique is diagonalization,
resembling the self-contradiction technique
used previously.
24
8.2 The Turing Machine
8.2.1 The Quest to Decide All Mathematical
Questions
– Natures of computational model ---
Predicate calculus --- declarative
Partial-recursive functions --- computational (a
programming-language-like notion)
Turing machine --- computational (computer-like)
(invented by Alan Turing several years before true
computers were invented)
25
8.2 The Turing Machine
8.2.1 The Quest to Decide All Mathematical
Questions
– Equivalence of maximal computational model:
They all compute the same functions or recognize
the same languages, having the same power of
computation.
26
8.2 The Turing Machine
8.2.1 The Quest to Decide All Mathematical
Questions
– Unprovable Church-Turing hypothesis (or
thesis):
Any general way to compute will allow us to
compute only the partial-recursive functions (or
equivalently, what the Turing machine or modern-
day computers can compute).
27
8.2 The Turing Machine
8.2.2 Notion for the Turing Machine
– A model for Turing machine:
Finite
control
… B B X1 X2 Xi Xn B B …
28
8.2 The Turing Machine
8.2.2 Notion for the Turing Machine
– A move of Turing machine includes:
change state;
write a tape symbol in the cell scanned;
move the tape head left or right.
– Formal definition:
A Turing machine (TM) is a 7-tuple M = (Q, , , ,
q0, B, F) where
Q: a finite set of states of the finite control;
: a finite set of input symbols;
: a set of tape symbols, with being a subset of it;
29
8.2 The Turing Machine
8.2.2 Notion for the Turing Machine
– Formal definition (cont’d):
: a transition function (q, X) = (p, Y, D) where
– q: the current state, in Q;
– X: a tape symbol being scanned;
– p: the next state, in Q;
– Y: the tape symbol written on the cell being scanned, used
to replace X;
– D: either L (left) or R (right) telling the move direction of
the tape head; 30
8.2 The Turing Machine
8.2.2 Notion for the Turing Machine
– Formal definition (cont’d):
q0: the start state, in Q;
B: the blank symbol in not in should not
be an input symbol);
F: the set of final (or accepting) states.
– A TM is a deterministic automaton with a two-
way infinite tape which can be read and written in
either direction. 31
8.2 The Turing Machine
8.2.3 Instantaneous Descriptions for Turing
Machine
– The instantaneous description (ID) of a TM is
represented by
– X1X2…Xi1qXiXi+1…Xn in which
– q is the current state;
– The tape head is scanning the ith symbol Xi from the
left;
– X1X2…Xn is the portion of the tape between the
leftmost and the rightmost nonblank symbols . 32
8.2 The Turing Machine
8.2.3 Instantaneous Descriptions for Turing
Machine
_| _|
– Moves of a TM M are denoted by M or as follows:
If (q, Xi) = (p, Y, L) (a leftward move), then we write
the following to describe the left move:
34
8.2 The Turing Machine
8.2.3 Instantaneous Descriptions for Turing Machine
– Example 8.2 --- Design a TM to accept the language L =
{0n1n | n 1} as follows. (continued)
35
8.2 The Turing Machine
8.2.3 Instantaneous Descriptions for Turing
Machine
– Example 8.2 --- Design a TM to accept the
language L = {0n1n | n 1} (cont’d).
M = ({q0~q4}, {0, 1}, {0, 1, X, Y, B}, , q0, B, {q4})
36
8.2 The Turing Machine
8.2.3 Instantaneous Descriptions for TM
– Example 8.2 ---
symbol
state 0 1 X Y B
q0
( q 1 , X, R ) 1 - - ( q 3 , Y , R) 8 -
q1
(q1, 0, R)2 (q2 , Y , L )4 - ( q 1 , Y , R) 3 -
q2
(q2, 0, L)5 - ( q 0 , X, R) 7 (q2 , Y , L )6 -
q3
- - - ( q 3 , Y , R) 9 (q4, B, R)10
q4
- - - - - 37
8.2 The Turing Machine
8.2.3 Instantaneous Descriptions for TM
– Example 8.2 (cont’d)
To accept 0011 ---
|_
(use instead of )
q00011 Xq1011 X0q111 Xq20Y1 q2X0Y1
Xq00Y1 XXq1Y1 XXYq11 XXq2YY Xq2XYY
XXq0YY XXYq3Y XXYYq3B XXYYBq4B
38
8.2 The Turing Machine
8.2.4 Transition Diagrams for TM’s
– If (q, X) = (p, Y, L), we use label X/Y on the arc.
– If (q, X) = (p, Y, R), we use label X/Y on the arc.
42
8.2 The Turing Machine
8.2.6 TM’s and Halting
– Languages with TM’s that do halt eventually,
regardless whether or not they accept, are called
recursive languages
– TM’s that always halt, regardless of whether or not
they accept, are a good model of an “algorithm.”
– So TM’s that always halt can be used for studies of
decidability (see Chapter 9).
43
8.3 Programming Techniques for TM’s
Concepts to be taught
– Showing how a TM computes.
– Indicating that TM’s are as powerful as conventional
computers.
– Even some extended TM’s can be simulated by the
original TM.
44
_
m n =mn if m n;
_
=0 if m < n.
45
_
46
_
symbol
0 1 B
state
q0 (q1, B, R) (q5, B, R) -
q1 (q1, 0, R) (q2, 1, R) -
q2 (q3, 1, L) (q2, 1, R) (q4, B, L)
q3 (q3, 0, L) (q3, 1, L) (q0, B, R)
q4 (q4, 0, L) (q4, B, L) (q6, 0, R)
q5 (q5, B, R) (q5, B, R) (q6, B, R)
q6 - - - 48
8.3 Programming Techniques for
TM’s
Section 8.2 revisited
q5 後把所有的 0 及 1 皆改為 B) 49
8.3 Programming Techniques for
TM’s
8.3.1 Storage in the State
8.3.2 Multiple Tracks
8.3.3 Subroutines
For details of the above three topics, see the
textbook.
50
8.4 Extensions to the Basic TM
Extended TM’s to be studied:
– Multitape Turing machine
– Nondeterministic Turing machine
The above extensions make no increase of the
original TM’s power, but make TM’s easier to
use:
– Multitape TM --- useful for simulating real computers
– Nondeterministic TM --- making TM programming
easier.
51
8.4 Extensions to the Basic TM
8.4.1 Multitape TM’s
Finite
control
Tape 1
Tape 2
Tape 3
54
8.4 Extensions to the Basic TM
8.4.4 Nondeterministic TM’s
– A nondeterministic TM (NTM) has multiple choices
of next moves, i.e.,
(q, X) = {(q1, Y1, D1), (q2, Y2, D2), …, (qk, Yk, Dk)}.
– The NTM is not more ‘powerful’ than a deterministic
TM (DTM),
– If MN is NTM, then there is a DTM MD such
that L(MN) = L(MD).
55
8.4 Extensions to the Basic TM
8.4.4 Nondeterministic TM’s
– The equivalent DTM constructed for an NTM in the
last theorem may take exponentially more time than
the DTM.
– It is unknown whether or not this exponential
slowdown is necessary!
– More investigation will be done in Chapter 10.
56