414 - F24 - Lecture6 - Turing Machines - Multiplication - Coding - Halting - Problem
414 - F24 - Lecture6 - Turing Machines - Multiplication - Coding - Halting - Problem
1
1 Turing Machine for Multiplication of fixed k and arbitrary n
2
Here are the crucial moments on the diagrams on slide
??. The n 7→ 2n machine prints a 1, moves to q4, prints
another 1 and goes into q5. (Instructions q3B1q3; q31Lq4;
q4B1q4; q41Rq5; q51Rq5)
q3 /q4 q4 q4 /q5
1 1 1 1 1 1 1 1 1 1 1 1 1 1
q3 /q4 q4 q4 /qa
1 1 1 1 1 1 1 1 1 1 1 1 1 1
qa qa /q5
1 1 1 1 1 1 1 1 1 1 1 1
3
After the three cycles corresponding to the three counters
are completed, the modified machine will halt on the left-
most of a string of 9 strokes, on an otherwise blank tape.
Clearly, for any k, we can get the function n 7→ k × n
from the n 7→ 2n machine by dropping the instruction
q41Rq5, adding additional states {qa1 , qa2 , . . . qak−2 } and
adding additional instructions:
q41Lqa1 ;
qa1 01qa1 ; qa1 1Lqa2 ;
qa2 01qa2 ; qa2 1Lqa3 ;
qa3 01qa3 ; qa3 1Lqa4 ;
.. ..
..
qak−3 01qak−3 ; qa3 1Lqak−2 ;
qak−2 01qak−2 ; qak−2 1Rq5 )
4
It is crucial that this is a function that multiplies n by
some specific number given in advance, before we set
out to construct the machine.
5
It’s important to see why this strategy won’t work to get
a machine that multiplies a given number n with some
other, arbitrary number m, with both m and n supplied
as input on the tape.
6
2 Turing Machine for Multiplication
7
To see the challenge, let’s look at one failed try. Say we
use the first block of m strokes as counters, as in the du-
plication machine. (This is, in fact, what we’ll do.)
Say we try to build cycles that work like this: (next slide)
8
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
9
1 1 1 1 1 1 1 1 1
10
I won’t go into all of the details of how the machine works:
you can extract those from the diagram p. 30 of BJB.
Opening configuration:
1 1 1 1 1 1 1
11
After 1 cycle, the first counter has been erased, then the
right-hand block of 3 1’s has been pushed three squares
down the tape, and the head has moved right right to
begin another cycle:
1 1 1 1 1 1
1 1 1 1 1
12
After 3 cycles, the third counter has been erased and then
the block of 3 strokes has been shifted 9 squares down.
1 1 1 1
1 1 1 1
13
When it erases the last remaining counter, it moves right
one square, to the square that originally separated the
two blocks of strokes. That square is supposed to remain
blank:
1 1 1
14
After the machine registers (by changing states) that it
has passed a square that is supposed to stay blank, it
continues to move right, writing 1 on every blank it en-
counters, until it encounters a 1. Once it encounters a 1,
it just moves to the left-most 1, and halts.
1 1 1
1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1
qhalt
1 1 1 1 1 1 1 1 1 1 1 1
15
To construct the machine, there are three different mod-
ules that need to be put together.
16
OK, so let’s look at the beginning of one instance of the
subroutine, in the calculation of 4 × 3.
1 1 1 1
17
The reason you need a bit of finesse here because you
need to figure out a way to move the strokes down the
tape, in such a way that the machine can tell just from
the configuration of the tape, assisted with some fixed
finite number of states:
18
Here is the plan, called the “Leapfrog routine” in BJB
p.31.
1 1 1 1
19
Some pictures, illustrating with the case of moving 4
strokes. We join the computation with the read/write
head on the far left stroke of the right-most block, in
state q4:
q4
1 1 1 1
1 1 1
q6
1 1 1
20
If, in q6, the machine were to read another blank, that
would tell it that the whole block had been erased, so it
should write a 1 and exit the leapfrog routine. I look at
that path beginning on slide 27.
q6 /q7
1 1 1
q7
1 1 1
21
q7
1 1 1
q7 /q8
1 1 1
22
Reading a blank in q8 , the machine writes a 1, and goes
into q10. (q8B1q10).
q8 /q10
1 1 1 1
1 1 1 1 1 1 1 1
23
I’ll pause to explain the minor divergence I mentioned on
slide 22.
q7 /q8 q8
1 1 1 1 1 1
q8 /q9 q9 /q10
1 1 1 1 1 1 1
24
Where we left off (on slide 23), the machine had just gone
from q10 to q11 and moved left. I’ll return to that point.
1 1 1 1 1 1 1 1
25
Here’s a fast-forwarded diagram version of the next cy-
cle, to put us in a position to follow the steps to exit the
leapfrog routine.
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1
q11 /q4 q4
1 1 1 1 1 1 1 1
26
[Skipping 1 full cycle later:] After 1 more cycle, we come
to the choice-point that will lead to exiting the leapfrog
routine.
That is a sign that the machine has erased the last of the
counters for the leapfrog subroutine, so it’s time to write
a final 1 (on the square above which the machine head
is currently resting) and move to q12 to exit the routine.
(q6B1q12)
q4 q4 /q5 q5 /q6
1 1 1 1 1 1 1 1 1 1
q6 q6 /q12
1 1 1 1 1 1 1
27
So let’s pull the camera back to get a wider view of the
tape.
q12
1 1 1 1 1 1 1
28
The effect of the transitions q121Lq13; q13BLq13; q131Lq14,
q141Lq14; q14BRq1 in pictures:
q12 /q13
1 1 1 1 1 1 1
q13
1 1 1 1 1 1 1
q13 /q14
1 1 1 1 1 1 1
q14
1 1 1 1 1 1 1
q14 /q1
1 1 1 1 1 1 1
q1
1 1 1 1 1 1 1
29
I’ll leave it as an exercise for you to work through the
BJB p. 30 flowchart to work out:
a) How the machine uses the first block of m strokes as
as counters, so that you perform the leapfrog subroutine
m − 1 times
(States q1, q2, q3)
b) and the routine that kicks in when the last of the coun-
ters is erased, which cleans up at the end by instructing
the machine to keep moving right and writing 1’s in the
blank spaces it has created, then getting into the desired
halting configuration. (States q2, q15, q16, q17, q18)
30
3 Computing Functions and Standard Configurations
31
Canonical representation of f : N
| ×N×
{z. . . × N} → N:
k times
32
3. If f (m1, . . . , mn) = n then the machine represent-
ing f will eventually halt scanning the leftmost 1 of
a tape consisting of n consecutive strokes, otherwise
blank.
f (m1, . . . , mn) = ↑.
f (m1, . . . , mn) = ↓.
33
A few trivial-looking Turing computable functions.
34
You might ask: how can we hope to get machines to com-
pute more complicated functions, given how hard it was
just to get apparently elementary operations like addition
and multiplication?
35
It’s important to appreciate how the multiplication ma-
chine described in the last lecture (and BJB p. 28-29)
was obtained. This is a general pattern.
36
More generally, with the nesting technique, can get is a
natural hierarchy of recursively defined functions by be-
ginning with successor (i.e. +1):
37
4 The Turing (aka Church-Turing) Thesis
Since (as we’ll see) the recursive functions are exactly the
Turing computable ones, people sometimes speak of the
“Church - Turing thesis”.
38
When you are trying to devise a Turing machine, it is of-
ten useful in the early stages to think in informal terms,
trusting the Church-Turing thesis.
39
Note though that such appeals to the Turing thesis are
usually just for orientation.
40
Turing’s thesis is not the sort of thing that can be proven.
41
Now it is in principle possible that someone could find
a function that is intuitively effectively computable, but
which is not Turing computable.
42
The fact that 70 years of research has passed without
any plausible counterexamples, or even suggestions that
look like they might be fleshed out into counter-examples,
gives some evidence for the truth of the thesis.
43
5 Effective Coding and the Diagonal Function
44
So: There are only countably many Turing machines.
45
First we need to code up the Turing machines. It isn’t
so crucial that we know what the actual coding scheme
is, though you might want to look at BJB p. 36 to get a
sense of what one such coding looks like.
46
5.2 Coding and The Universal Turing Machine
47
Can you carry out “machine composition” mechanically?
Sure, in principle.
48
True enough, but Turing machines can operate on Turing
machine codes to produce other Turing machine codes.
49
Another crucial operation a Turing machine can perform
is the function of what is called the Universal Turing Ma-
chine.
50
Let us write fn for the function computed by the machine
Mn .
51
This is easy to see:
52
5.3 The Diagonal Function
53
After the diagonal arguments we’ve already seen, the ar-
gument that d is not Turing computable is familiar.
54
That gives one example of an uncomputable function, but
it’s a bit abstract and theoretical.
55
First note that it is possible for a Turing machine, given
an input, to never halt at all.
56
For certain machines and inputs it’s clear that the ma-
chine won’t halt on that input.
q11Rq1, q1BRq1.
57
Is there a machine that will take a code c and a number
k, and:
58
6 The Halting Problem
6.1 Framing
59
When you consider different functions and ask if they can
be effectively computed, you often find that the main ob-
stacle is knowing whether or not some procedure will halt.
60
Procedure: Take the input you are given — say that it is
l — and find the description of Ml .
61
But there is one case left over: what do you do if the
machine never halts?
This is a tough one: Say that you have been sitting for
awhile, waiting for the machine to halt.
But you don’t know that it will never halt, just that it
hasn’t halted yet.
But if it doesn’t halt, and our only strategy is the “sit and
wait and watch” strategy, at no point in the computation
will we be able to decide between i) the machine will never
halt, or ii) it just hasn’t halted yet, but it will.
62
Another way to cast the point is this:
63
The halting function is a two-variable reframing of the
diagonal function in terms of machines:
(
1 if Mx halts on input y
h(x, y) =
2 otherwise
64
The argument is quite straightforward, but I’m going to
go into some details, because this argument reveals one
of the basic techniques for proving that Turing machine
M ∗ satisfying a certain description is impossible:
65
Since we already know that the diagonal function is un-
computable, I’ll show that if we could solve the halting
problem, we could compute (a variation on) the diagonal
function.
66
OK, so let’s Assume that we can build the machine Mh
that computes:
(
1 if Mx halts on input y
h(x, y) =
2 otherwise
67
The “dithering machine” is pretty trivial to build.
68
6.2 The Copying Machine
69
There is a first “set up” cycle you go through, to place a
couple of counters on the right. In q1, reading a 1, move
right until you hit a blank, move right and go into q2
(q11Rq1; q1BRq2)
q1 q2
1 1 1 1 1 1 1 1
1 1 1 1 1
q4 /q5
1 1 1 1 1 1
70
As the machine cruises left, it passes a blank, then a 1,
then a blank, and a bunch of 1’s, changing states at ap-
propriate points, to keep track of the fascinating things
it has seen. (q5BLq5; q51Lq6; q6BLq7; q71Lq7; q7BRq8)
When it hits the first blank on the far left, it moves right
once, and begins a cycle that it will repeat until it has
erased the counters. (Now it has left the “set up” phase
once it has laid down the two placeholders on the right.)
1 1 1 1 1 1
1 1 1 1 1
71
If the head were reading a blank in q9, it would know it
had erased the last of the counters. It would then just
move right and halt as soon as it encountered the first 1,
so it would be in standard final configuration. (q9BRq ∗;
q ∗BRq∗; q ∗11qhalt )
1 1 1 1 1
72
Now we have a delicate move. We want to keep shoving
the second block down to create room to expand the first
by one, and also expand the second by one. So we write
a 1 in the blank that the read/write head is occupying,
and go into q11:
q10 /q11
1 1 1 1 1 1
This adds 1 to the first block, all right, but at the cost
of eliminating the blank separating the two blocks. So
we need to erase the leftmost 1 of the second block, and
then move right and write two strokes on the first two
available blanks. (One stroke to replace the stroke we
erased, and one stroke because we want to expand the
second block by 1).
Here are the state-transitions for this:
73
q11 /q12
1 1 1 1 1 1
q12
1 1 1 1 1 1/0
q12 /q13
1 1 1 1 1
q13
1 1 1 1 1 0/1
q13 /q14
1 1 1 1 1 1
q14 /q15
1 1 1 1 1 1 0/1
74
Note how this works: you write an additional 1 on the
first new block, which means (danger!) that there is no
longer a blank between the two blocks.
75
And now it’s just a mad dash to the left, to find the first
blank after the leftmost block.
76
q15
1 1 1 1 1 1 1
q15 /q16
1 1 1 1 1 1 1
q16
1 1 1 1 1 1 1
q16 /q17
1 1 1 1 1 1 1
q17 /q18
1 1 1 1 1 1 1
q18 /q19
1 1 1 1 1 1 1
q19 /q8
1 1 1 1 1 1 1
77
Now that you’ve completed a cycle, go back to the far
left, erase a counter, and run the cycle again!
78
q8
1 1 1 1 1 1 1
q8
1 1 1 1 1 1
q11
1 1 1 1 1 1
q12
1 1 1 1 1 1 1
q13
1 1 1 1 1 1 1
q13
1 1 1 1 1 1
q13
1 1 1 1 1 1
79
q14
1 1 1 1 1 1
q14
1 1 1 1 1 1 1
q15
1 1 1 1 1 1 1
q15
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
At this point the blocks on the right are what you want
them to be, and the machines next steps are to finish off
the sequence: Move left and erase next counter.
80
In this case the machine detected it is the last counter,
so instead of starting another cycle, it moves right one
square and halt.
q15
1 1 1 1 1 1 1 1 1
qcleanup
1/0 1 1 1 1 1 1 1 1
qcleanup
1 1 1 1 1 1 1 1
qcleanup /qhalt
1 1 1 1 1 1 1 1
qhalt
1 1 1 1 1 1 1 1
81
6.3 An impossible machine
82
The machine Mh ◦Mc is a “self-halting machine”: it halts
and prints 1 on input k if the one-variable machine Mk
halts on input k. It halts and prints 2 if Mk doesn’t halt
on input k.
83
Since Mc, Md, and (by assumption) Mh all are Turing
machines, Md ◦ Mh ◦ Mc is a Turing machine as well.
84
6.4 “Reducing to the halting problem”
85
Now you might think that this is the sort of thing we
could get a machine to check:
Answer: nope!
86
Assume for the sake of reducing to a contradiction that
Msame function exists.
First note that for any given n we can easily write down
a machine that will do the following: check to see if the
input is exactly n. If it isn’t, then return to the initial
configuration. If it is, then go into an infinite loop and
never halt.
87
Mtest n looks like this:
88
Here is another thing that we can intuitively do effec-
tively: Given k and l, return the code of the machine
Mk ◦ Ml .
˜
Say that the code of Mk n−modified is l.
89
This will give a yes or no answer in a finite number of
steps to the question “Do Mk and M˜l compute the same
function?”
90
You can use similar moves to show that there is no Tur-
ing machine Mhalt on all that will decide, given k as input,
whether or not Mk halts on all inputs.
If you had such a machine Mhalt on all you could solve the
halting problem this way:
91
6.5 Rice’s Theorem
92
There are different ways to state the theorem, I’ll put it
in terms of functions. Say that an index set C of ma-
chine codes has this property:
93
Say that a class C of numbers is decidable if and only if
there is a Turing machine that i) halts and prints 1 on
input k if k ∈ C ii) halts and prints 2 on input k if k 6∈ C.
94
So consider any property of Turing-computable functions:
{f |f is constant }
{f |f is not constant }
{f |f (x) = 0 for at least one input x}
{f |f (x) 6= 5 for any input x}
..
.
95
6.6 Productivity/Busy Beaver functions - uncomputability via rapid
growth
96
The productivity function (also called the “Busy Beaver
Function”) is shown to be uncomputable because it even-
tually dominates every Turing computable function.
97
Consider the a natural hierarchy of functions beginning
with successor (i.e. +1) and applying recursion.
f1(x, n) = x + n:
x+0=x
x + (y + 1) = (x + y) + 1
f2(x, n) = x × n:
x×1=x
x × (y + 1) = (x × y) + y
f3(x, n) = xn:
x1 = x
xy+1 = xy × x
f4(x, n) =n x:
1
x=x
. .x
x.
y+1
x = (y x)x = |xx{z }
y+1 times
..
.
98
γ will grow faster than any of the functions fi.
99