এই বইটি এখনো বাংলা ভাষায় পাওয়া যায় না। সংস্করণ: English।
University Computer Science — Year 3 · স্নাতক তৃতীয় বর্ষ
15Turing Machines and Computability
Some problems have no program at all: not a slow one, not a clever one, none. The school volumes showed one such problem, deciding whether a program halts, with a short argument about programs. This chapter rebuilds that result on a model of computation exact enough to prove things about: the Turing machine. A simulator of thirty lines runs three machines; a second tape changes the speed but not the power; primitive and -recursive functions give a second model that computes the same functions; one program runs every machine from its description; and then the limits come, by diagonalisation and by reduction, up to Rice’s theorem, which says that no program can decide any non-trivial property of what programs compute. The weekend problem hunts the busy beavers of two states, a function no program computes.
You already know
The grade-12 chapter on the halting problem showed that no program decides, for every program and input, whether that program halts: from a claimed decider it built a program that does the opposite of what the decider predicts about it. A set is countable when its elements can be listed as a sequence; the set of finite words over a finite alphabet is countable, and Cantor’s diagonal argument shows that the set of infinite sequences of bits is not (mathematics volumes). A finite automaton reads its input once, from left to right, and its transition function gives the next state from the current state and the letter read (Year 2 volume).
15.1 More problems than programs
Before any machine is defined, a count already shows that some problems have no program. A program, in any programming language, is a finite word over a finite alphabet; a problem with yes-or-no answers about natural numbers is a set of natural numbers, the numbers whose answer is yes.
Theorem 15.1 (Countably many programs, uncountably many problems)
There are countably many programs, and uncountably many sets of natural numbers. Some set of natural numbers is therefore decided by no program.
Proof. List the programs by length, and the programs of each length in alphabetical order: ; every program appears at a finite position. Suppose the sets of natural numbers could be listed too, as . The set differs from each on the number , so it is not in the list: a contradiction. If every set were decided by a program, mapping each set to the first program that decides it would list the sets, which is impossible. ∎
The proof shows that non-computable problems exist but names none. The rest of the chapter makes the argument concrete: it replaces “set of natural numbers” by a problem one would like to solve, and “program” by a precise machine.
15.2 Turing machines
Alan Turing’s paper “On computable numbers, with an application to the Entscheidungsproblem”, of 1936–37, defined the machine below to say what a person computing with pencil and paper can do.
Definition 15.2 (Turing machine, configuration, transition function of a Turing machine)
A Turing machine has a finite set of states with a start state, a finite tape alphabet containing a blank symbol, a tape of cells unbounded in both directions, and a head on one cell. Its transition function maps some pairs (state, symbol read) to a triple (new state, symbol written, move left, right or stay). A step applies it; the machine halts when no transition applies, and accepts if it halts in its accepting state. A configuration is the state, the tape contents and the head position: written , the tape left of the head, the state, and the tape from the head on.
A finite automaton is the special case that only reads and only moves right. Writing, and moving back, is what makes the difference. The simulator stores the tape as a dictionary from positions to symbols, so the tape grows on demand in both directions.
def run(delta, word, start="q0", max_steps=10_000, trace=None):
"""Run on `word`; returns (halted, state, tape contents, steps)."""
tape = {i: c for i, c in enumerate(word)}
head, state, steps = 0, start, 0
while (state, tape.get(head, BLANK)) in delta:
if steps == max_steps:
return False, state, contents(tape), steps
if trace is not None:
trace(config(tape, head, state))
state, tape[head], move = delta[(state, tape.get(head, BLANK))]
head += {"L": -1, "R": 1, "S": 0}[move]
steps += 1
if trace is not None:
trace(config(tape, head, state))
return True, state, contents(tape), steps
Three machines: a binary increment, which walks to the last digit and carries leftwards; a palindrome checker over , which erases the first letter, remembers it in its state, and checks it against the last; and a unary adder (machines.py).
INCREMENT = { # binary + 1: go to the last digit, then carry leftwards
("q0", "0"): ("q0", "0", "R"), ("q0", "1"): ("q0", "1", "R"),
("q0", B): ("carry", B, "L"),
("carry", "1"): ("carry", "0", "L"), # 1 + carry = 0, carry on
("carry", "0"): ("done", "1", "S"), ("carry", B): ("done", "1", "S"),
}
PALINDROME = { # cross off the first letter, check the last one matches, repeat
("q0", "a"): ("has_a", B, "R"), ("q0", "b"): ("has_b", B, "R"),
("q0", B): ("accept", B, "S"),
("has_a", "a"): ("has_a", "a", "R"), ("has_a", "b"): ("has_a", "b", "R"),
("has_a", B): ("end_a", B, "L"),
("has_b", "a"): ("has_b", "a", "R"), ("has_b", "b"): ("has_b", "b", "R"),
("has_b", B): ("end_b", B, "L"),
("end_a", "a"): ("back", B, "L"), ("end_a", B): ("accept", B, "S"),
("end_b", "b"): ("back", B, "L"), ("end_b", B): ("accept", B, "S"),
("back", "a"): ("back", "a", "L"), ("back", "b"): ("back", "b", "L"),
("back", B): ("q0", B, "R"),
}
B is the blank. code/bachelor-3/15-computability/listings/machines.py$ python machines.py
[q0]1011
1[q0]011
10[q0]11
101[q0]1
1011[q0]_
101[carry]1_
10[carry]10_
1[carry]000_
1[done]100_
increment 0: 1
increment 111: 1000
increment 1001: 1010
palindrome 'abba': True, steps 15
palindrome 'aba': True, steps 10
palindrome 'abab': False, steps 5
palindrome '': True, steps 1
add 111+11: 11111
The photograph shows the Pilot ACE, one of the first computers built in the United Kingdom, a cut-down version of the design Turing wrote at the National Physical Laboratory; it first ran in 1950. Like every computer since, it is a universal machine in the sense of Section 15.4: one machine that runs whatever program it is given.
15.2.1 More tapes, same power
A machine with several tapes, one head on each, is easier to program: the palindrome check copies the word to a second tape, then reads the first tape forwards and the second backwards.
Definition 15.3 (Multi-tape Turing machine)
A multi-tape Turing machine with tapes reads the symbols under its heads; its transition function gives the new state, the symbols written and the moves. The input starts on the first tape; the others start blank.
PAL2 = {}
for x in "ab":
PAL2[("copy", (x, B))] = ("copy", (x, x), ("R", "R")) # copy, both move right
PAL2[("rewind", (x, B))] = ("rewind", (x, B), ("L", "S")) # tape 1 back to start
for y in "ab":
PAL2[("rewind", (x, y))] = ("rewind", (x, y), ("L", "S"))
PAL2[("compare", (x, y))] = ("compare", (x, y), ("R", "L")) if x == y \
else ("reject", (x, y), ("S", "S"))
PAL2[("copy", (B, B))] = ("rewind", (B, B), ("L", "L"))
for y in "ab" + B:
PAL2[("rewind", (B, y))] = ("compare", (B, y), ("R", "S"))
PAL2[("compare", (B, B))] = ("accept", (B, B), ("S", "S"))
$ python multitape.py
n one tape two tapes
2 6 9
4 15 15
8 45 27
16 153 51
32 561 99
64 2145 195
128 8385 387
The one-tape machine walks the whole word for each pair of letters it crosses off: steps. The two-tape machine walks the word three times. The gap is quadratic, and that is the worst that can happen.
Theorem 15.4 (One tape simulates )
For every -tape Turing machine there is a one-tape machine that halts on the same inputs with the same output; if the first takes steps on an input of length , the second takes steps.
Proof idea. Store the tapes on one, side by side: cell of the single tape holds a tuple of symbols, cell of each tape, each marked if a head is there (the alphabet of tuples is finite). One step of the -tape machine becomes a sweep: left to right to read the marked symbols into the state, then back to write and move the marks. After steps the used part of the tape has at most cells, so each sweep costs steps and the whole run . ∎
Hartmanis and Stearns proved this in 1965, and used multi-tape machines for counting time because they are closer to real computers. A quadratic slowdown changes running times, which Chapter 16 studies, but it changes nothing about what can be computed at all.
15.3 The Church–Turing thesis
The Turing machine is one model; the 1930s produced several others, built on quite different ideas. Alonzo Church’s 1936 paper used the -calculus of Chapter 5. A third model builds functions on natural numbers from a few basic ones.
Definition 15.5 (Primitive recursive function, -recursive function, Ackermann function)
The primitive recursive functions are the functions on natural numbers built from zero, the successor and the projections by composition and by primitive recursion: and . The -recursive functions also allow minimisation: is the least with , undefined if there is none. The Ackermann function is , , .
Primitive recursion is a loop whose number of rounds is known before it starts, a for loop; minimisation is a while loop, which may never end. primrec.py builds both as Python combinators.
def primrec(base, step):
"""h(0, ys) = base(ys); h(n + 1, ys) = step(n, h(n, ys), ys)."""
def h(n, *ys):
acc = base(*ys)
for k in range(n): # the loop runs exactly n times: no unbounded search
acc = step(k, acc, *ys)
return acc
return h
add = primrec(proj(0), compose(succ, proj(1))) # add(n, y) = n + y
mul = primrec(zero, compose(add, proj(1), proj(2))) # mul(n, y) = n * y
power = primrec(compose(succ, zero), compose(mul, proj(1), proj(2))) # y ** n
def mu(p):
"""Minimisation: the least n with p(n, ys) = 0. It may search for ever."""
def f(*ys):
n = 0
while p(n, *ys) != 0:
n += 1
return n
return f
@cache
def ackermann(m, n):
if m == 0:
return n + 1
if n == 0:
return ackermann(m - 1, 1)
return ackermann(m - 1, ackermann(m, n - 1))
$ python primrec.py
add(3, 4) = 7 mul(3, 4) = 12 power(3, 2) = 2^3 = 8
isqrt(50) by minimisation = 7
A(0, n) for n = 0..5: [1, 2, 3, 4, 5, 6]
A(1, n) for n = 0..5: [2, 3, 4, 5, 6, 7]
A(2, n) for n = 0..5: [3, 5, 7, 9, 11, 13]
A(3, n) for n = 0..5: [5, 13, 29, 61, 125, 253]
Every primitive recursive function is total, since every loop ends. Ackermann’s function is total too, and computed by the short recursion above, but it is not primitive recursive: each row grows like an iteration of the previous one, and every primitive recursive function is eventually overtaken by one of the rows. Wilhelm Ackermann gave such a function in 1928, by a nested recursion; the two-argument form above follows Péter’s of 1935.
The fourth row already explodes: , and has 19,729 digits.
Theorem 15.6 (The models agree)
The total functions computable by a Turing machine, the total -recursive functions, the total functions definable in combinatory logic (a relative of the -calculus) and the total functions computable by register machines with unbounded registers are the same class; so are the partial functions of the four models.
Proof idea. Each model simulates the others. A Turing machine can carry out composition, a bounded loop and an unbounded search on numbers written on its tape. Conversely, a configuration of a Turing machine can be coded as a number, one step is a primitive recursive function of that code, and minimisation finds the first step at which the machine halts. ∎
Stephen Kleene gave minimisation its standard form in 1943, and the register machines are Shepherdson and Sturgis’s, of 1963.
Definition 15.7 (Church–Turing thesis)
The Church–Turing thesis states that every function computable by an effective procedure, one that a person could follow mechanically with unlimited paper and time, is computable by a Turing machine.
The thesis is not a theorem: “effective procedure” is not a mathematical notion, so it cannot be proved. It is supported by Theorem 15.6: every attempt to define computation has given the same class. In practice it lets a proof describe an algorithm in words, or in Python, and conclude that a Turing machine exists.
15.4 A universal machine
A Turing machine is a finite table, so it can be written as a word, and a word can be the input of another machine.
Definition 15.8 (Encoding of a machine, universal Turing machine)
An encoding of Turing machines maps each machine to a word over a fixed alphabet, so that the table of can be computed back from it. A universal Turing machine takes and a word , and behaves as on : it halts if and only if halts, accepts if and only if accepts, and leaves ’s output.
utm.py numbers the states, with the start state first, and the symbols, writes each number in unary, and each transition as five numbers separated by 0s; transitions are separated by #. The universal program decodes the word and runs it.
def encode(delta, start="q0"):
"""The machine as a string <M>; the start state gets number 1."""
named = {s for s, _ in delta} | {t for t, _, _ in delta.values()}
states = [start] + sorted(named - {start})
symbols = sorted({a for _, a in delta} | {b for _, b, _ in delta.values()})
num = {**{s: i + 1 for i, s in enumerate(states)},
**{("sym", a): i + 1 for i, a in enumerate(symbols)}}
code = []
for (q, a), (r, b, m) in sorted(delta.items()):
fields = [num[q], num[("sym", a)], num[r], num[("sym", b)], MOVES.index(m) + 1]
code.append("0".join("1" * f for f in fields))
return "#".join(code), symbols
def universal(code, symbols, word):
"""Decode <M> and run it on word: one program for every machine."""
delta = {}
for t in code.split("#"):
q, a, r, b, m = (len(f) for f in t.split("0"))
delta[(q, symbols[a - 1])] = (r, symbols[b - 1], MOVES[m - 1])
return run(delta, word, start=1)
$ python utm.py
symbols ['0', '1', '_'] are 1, 2, 3; <INCREMENT> has 85 characters
110101110110111 = [2, 1, 3, 2, 3]
110110110101 = [2, 2, 2, 1, 1]
on 1011: directly 1100, through the universal program 1100
on 111: directly 1000, through the universal program 1000
A first version numbered the start state 4 instead of 1 and printed its inputs unchanged: named - {start} had been written inside a union, and - binds more tightly than | in Python, so the start state was subtracted from one operand only and listed twice. The test that compares direct and universal runs over every word of length 6 catches that kind of slip. Turing built a universal machine in his 1936–37 paper, as a Turing machine, and used it to prove that some problems have no machine. universal is the same thing in Python; by the thesis, it is a Turing machine too.
Definition 15.9 (Decider, decidable language, recognisable language)
A machine recognises the language of the words it accepts. A language is recognisable (semi-decidable, recursively enumerable) when some machine recognises it: on a word of the language it halts and accepts; on another word it rejects or runs for ever. A decider is a machine that halts on every input; a language is decidable when a decider recognises it.
Theorem 15.10 (Decidable means recognisable both ways)
A language is decidable if and only if and its complement are both recognisable.
Proof. If a decider recognises , then recognises , and with its answers swapped, still halting on every input, recognises . The converse runs the two recognisers side by side; Exercise 15.7 completes it. ∎
15.5 Undecidability
Definition 15.11 (Halting problem)
The halting problem is the language .
is recognisable: run the universal machine on and , and accept if it halts. It is not decidable. Turing formulated the problem and proved it in the same paper.
Theorem 15.12 (The halting problem is undecidable)
No Turing machine decides .
Proof. Suppose a decider answers whether halts on . Build the machine that, on input , runs on , loops for ever if answers yes, and halts if answers no. Run on . If halts on , answers yes on , so loops: a contradiction. If does not halt, answers no, so halts: a contradiction again. So does not exist. ∎
The machines against their encodings form a table whose entry says whether halts on ; is built to disagree with the diagonal, exactly as the set of Theorem 15.1 does. diagonal.py runs the construction against four candidate deciders: each is wrong about the program built from it.
def make_paradox(halts):
def paradox(f):
if halts(f, f): # the decider says f(f) halts: then loop for ever
while True:
pass
return "done" # the decider says it loops: then halt at once
return paradox
CANDIDATES = {
"always yes": lambda f, x: True,
"always no": lambda f, x: False,
"no 'while' in the source": lambda f, x: "while" not in inspect.getsource(f),
"short source": lambda f, x: len(inspect.getsource(f)) < 400,
}
$ python diagonal.py
always yes says paradox(paradox) halts; it is still running
always no says paradox(paradox) loops; it halted
no 'while' in the source says paradox(paradox) loops; it halted
short source says paradox(paradox) halts; it is still running
(budget: 100,000 Python lines)
“Is still running” after 100,000 lines is not a proof that the program never halts; here the program’s text shows it, since the branch taken is while True: pass. A budget is how every practical tool meets the theorem: it answers “halts”, “loops” for the cases it can prove, and “don’t know” after its budget.
15.5.1 Reductions
Once one problem is known undecidable, others follow by translating questions.
Definition 15.13 (Many-one reduction)
A many-one reduction from a language to a language is a computable function such that if and only if , for every word . We write .
Theorem 15.14 (Reductions transfer undecidability)
If and is decidable, then is decidable. So if and is undecidable, is undecidable.
Proof. To decide whether , compute and run the decider of on it; both steps halt, and the answer is right because . ∎
Take . Given , build : it erases its input, writes , returns to its first letter and runs , accepting wherever halts. Then accepts the empty word, and in fact every word, if and only if halts on . The map is computable; reductions.py computes it.
def reduce(delta, w, alphabet="01"):
tape = alphabet + B
m = {("erase", s): ("erase", B, "R") for s in alphabet} # wipe any input
m[("erase", B)] = ("w0", B, "S")
for i, c in enumerate(w): # write w, one state per letter
m[(f"w{i}", B)] = (f"w{i + 1}", c, "R")
m[(f"w{len(w)}", B)] = ("b0", B, "L")
for j in range(len(w)): # walk back to the first letter of w
for s in tape:
m[(f"b{j}", s)] = (f"b{j + 1}", s, "L" if j + 1 < len(w) else "S")
for s in tape:
m[(f"b{len(w)}", s)] = (("M", "q0"), s, "S")
states = {q for q, _ in delta} | {r for r, _, _ in delta.values()}
for q in states: # M's transitions, and "accept" wherever M halts
for s in tape:
r = delta.get((q, s))
m[(("M", q), s)] = (("M", r[0]), r[1], r[2]) if r else ("accept", s, "S")
return m
$ python reductions.py
M_w accepts the empty word / the word 0110, within 10,000 steps:
M = INCREMENT, w = 1011: 32 transitions, [True, True]
M = RUN_RIGHT, w = 1: 14 transitions, [False, False]
So is undecidable. So is the emptiness problem, whether : is empty exactly when does not halt on , and a decider for emptiness, with its answer swapped, would decide halting. And so is equivalence, whether : taking a machine that accepts nothing turns it into emptiness. These are all questions about what a machine accepts; one theorem settles them all.
Definition 15.15 (Semantic property, non-trivial property)
A property of Turing machines is semantic when it depends only on the language a machine recognises: two machines with the same language both have it or both lack it. It is non-trivial when some machine has it and some machine lacks it.
Theorem 15.16 (Rice’s theorem)
Every non-trivial semantic property of Turing machines is undecidable.
Proof. Let be one. Suppose first that the machines whose language is empty lack , and let be a machine that has it. Given , build : on input , run on ; if that halts, run on and answer as . If halts on , , so has ; if not, , so lacks . The map is computable, so , and is undecidable by Theorem 15.14. The other case, where the empty language has , is Exercise 15.10. ∎
H. G. Rice published it in 1953. It covers “accepts the empty word”, “recognises a finite language”, “recognises a regular language”, “recognises the same language as this other machine”. It says nothing about properties of the machine’s text, such as “has at most five states”, which is decidable, or “halts within 1,000 steps on the empty word”, decidable too, by running it.
Definition 15.17 (Post correspondence problem)
An instance of the Post correspondence problem is a finite list of tiles , pairs of words; it asks whether some non-empty sequence of indices , with repetitions allowed, gives .
The tiles , , have the match 3, 2, 3, 1: both rows read bbaabbbaa. Emil Post introduced the problem in 1946 and proved it undecidable. The usual proof builds, from a machine and an input, a set of tiles whose matches spell out the machine’s run, so that a match exists if and only if the machine accepts. The problem mentions no machine at all, which makes it a convenient starting point for reductions to problems on words and grammars.
15.5.2 The busy beaver
Definition 15.18 (Busy beaver function)
Among the Turing machines with states, a halting state and the symbols 0 and 1, started on a tape of 0s, consider those that halt. The busy beaver function is the largest number of steps one of them takes, the halting step included; is the largest number of 1s one of them leaves.
Tibor Radó defined it in 1962, as one of the simplest functions no program computes.
For small a program can still find its values: enumerate every machine and run each one for a while.
def run(machine, cap=CAP):
"""Steps and number of 1s if the machine halts within cap steps, else None."""
tape, head, state = {}, 0, 0
for step in range(1, cap + 1):
write, move, state = machine[2 * state + tape.get(head, 0)]
tape[head] = write
head += move
if state == "H":
return step, sum(tape.values())
return None
def busy_beaver(n):
actions = list(product((0, 1), (-1, 1), [*range(n), "H"]))
halting, best_s, best_sigma, champion = 0, 0, 0, None
for machine in product(actions, repeat=2 * n):
result = run(machine)
if result is None:
continue
halting += 1
best_s = max(best_s, result[0])
if result[1] > best_sigma:
best_sigma, champion = result[1], machine
return len(actions) ** (2 * n), halting, best_s, best_sigma, champion
CAP steps from the blank tape. code/bachelor-3/15-computability/listings/busy_beaver.py$ python busy_beaver.py
n = 1: 64 machines, 32 halt within 100 steps; S(1) = 1, Sigma(1) = 1
n = 2: 20,736 machines, 9,784 halt within 100 steps; S(2) = 6, Sigma(2) = 4
a Sigma(2) champion: 1LB 1RB 1RA 1LH
Theorem 15.19 ( is not computable)
No Turing machine computes .
Proof. If one did, the following would decide whether a two-symbol machine with states halts on the blank tape: compute , run for steps, and answer “halts” if it has halted. A machine that is still running then never halts, since a halting one would have taken at most steps. But halting on the blank tape is undecidable: the map of Listing 15.7 reduces to it, once the symbols of are coded in binary, which costs states but not halting. ∎
Facts as of 2026
Five values of are known: , , , and , with . The fifth was settled by the online bbchallenge collaboration, whose proof, checked by the Coq proof assistant, enumerated 181,385,789 machines; the proof appeared in the spring of 2024 and the paper in 2025. For six states only lower bounds are known, beyond any number that can be written out.
15.6 Exercises
Exercise 15.1 ★
Run the palindrome machine of Listing 15.2 on aba for five steps, writing the six configurations from [q0]aba.
Solution
Solution of Exercise 15.1.
[q0]aba, _[has_a]ba, _b[has_a]a, _ba[has_a]_, _b[end_a]a_, _[back]b__. The machine has erased the first a, walked to the end, found an a there, erased it, and is walking back. Five more steps accept: the b left alone is erased and the machine finds blanks on both sides.
Exercise 15.2 ★
Decidable, recognisable but not decidable, or not even recognisable? (a) The encodings of machines with at most five states. (b) , the machines that accept the empty word. (c) The complement of . (d) The pairs such that halts on within 1,000 steps.
Solution
Solution of Exercise 15.2.
(a) Decidable: read the encoding and count the states. (b) Recognisable, since a machine can run on the empty word through the universal machine and accept if it accepts; not decidable, by the reduction of the chapter. (c) Not recognisable: if it were, and its complement would both be recognisable, and would be decidable by Theorem 15.10. (d) Decidable: run on for at most 1,000 steps.
Exercise 15.3 ★
With the conventions of utm.py, the symbols of the adder are +, 1, _, numbered 1, 2, 3, and its states q0, done, last. Write the code of the transition , and that of .
Solution
Solution of Exercise 15.3.
is the numbers 1, 1, 1, 2, 2: 10101011011. is 3, 2, 2, 3, 3: 11101101101110111. The whole adder has 58 characters (utm.encode(ADD)).
Exercise 15.4 ★
Predict, without running them: the tape and the number of steps of the increment machine on 1111; the tape of the adder on 1+1; the number of steps of the palindrome machine on aa.
Solution
Solution of Exercise 15.4.
The increment walks right over four digits (4 steps), steps back onto the blank’s left (1), turns four 1s into 0s (4) and writes a 1 on the blank (1): tape 10000, 10 steps. The adder on 1+1 turns + into 1 and erases the last 1: 11. The palindrome machine on aa erases the first a, passes the second, finds the blank, steps back, erases the second, steps back, and finds an empty word: 6 steps, (test_exercise_4_predictions).
Exercise 15.5 ★★
exercises/ex5_increment.py gives a binary increment that turns 111 into 000. Find the missing transition and fix it; the test checks every number below 256.
Solution
Solution of Exercise 15.5.
On 111 the carry turns all three 1s into 0s and reaches the blank left of the number, where no transition applies: the machine halts with 000. The missing transition is ("carry", B): ("done", "1", "S"), a new leading 1 (exercises/ex5_increment.py).
Exercise 15.6 ★★
Write a Turing machine for the simulator that accepts exactly the words , , over . It is tested on every word of length at most 8.
Solution
Solution of Exercise 15.6.
exercises/ex6_anbn.py: each round crosses off the leftmost a as X, walks right over as and Ys to the first b, crosses it off as Y, and walks back to the last X. When the next letter is a Y instead of an a, every a is crossed off, and the machine accepts if only Ys follow. A b before an a, an a after the bs, or a missing b leaves the machine with no transition, and it rejects.
Exercise 15.7 ★★
Prove the converse in Theorem 15.10: if and are recognisable, is decidable. (Run the two recognisers one step each in turn.)
Solution
Solution of Exercise 15.7.
Let recognise and recognise . The decider , on , simulates one step of on , then one step of , and so on, keeping both configurations on its tape (a two-tape machine, then one tape by Theorem 15.4). If accepts, accepts; if accepts, rejects. Every word is in or in , so one of the two accepts after finitely many steps, and halts. It never answers wrongly, since accepts only words of and only words of .
Exercise 15.8 ★★
Prove that is undecidable, by a reduction from . Is it a case of Rice’s theorem as stated?
Solution
Solution of Exercise 15.8.
The map of the chapter works: ignores its input and runs on , so it halts on every input if halts on and on none otherwise. Hence , and is undecidable. It is not a case of Rice’s theorem as stated: halting on every input is not a property of the language accepted, since a machine that rejects by halting and one that rejects by looping can accept the same words.
Exercise 15.9 ★★
Explain the two columns of Output 15.2: why the two-tape machine takes exactly steps on a palindrome of length , and why the one-tape machine takes .
Solution
Solution of Exercise 15.9.
Two tapes: steps copy, one step at the end of the word, steps rewind and one more step back onto the first letter, then comparisons and one step to accept: . One tape: each round on a word of length walks right over cells and back over , plus the steps onto the ends, about steps, and removes two letters; summing over gives , quadratic as Theorem 15.4 allows (test_palindromes_one_and_two_tapes checks both formulas for every palindrome of length at most 8).
Exercise 15.10 ★★★
Finish the proof of Rice’s theorem: the case where the machines whose language is empty have the property.
Solution
Solution of Exercise 15.10.
If the machines with empty language have , then , the property of lacking , is semantic and non-trivial, and machines with the empty language lack it. By the case proved in the chapter, is undecidable. A decider for , with its answers swapped, would decide ; so is undecidable too.
Exercise 15.11 ★★★
Let be any total computable function on natural numbers. Prove that for all large enough . (Build, for each , a machine of states, fixed, that writes in unary and then writes a value of in unary.)
Solution
Solution of Exercise 15.11.
Facts used: is total and computable, so is too, and a two-symbol machine computes it from written in unary, with some number of states, leaving 1s; writing 1s on the blank tape takes states, one per 1; is increasing (problem, question 18).
The machine writes 1s, then runs the machine for . It halts, has states, and takes at least steps, since it leaves 1s and writes one per step at most. So . For , , hence , which exceeds both and . So for every . In particular for every computable : a second proof of Theorem 15.19.
Exercise 15.12 ★★★
With only zero, succ, proj, compose, primrec and the functions they already build, define pred (the predecessor, 0 for 0), monus (, the difference cut at 0) and div (integer division for ). The test compares them with Python’s operators.
Solution
Solution of Exercise 15.12.
exercises/ex12_primrec.py. pred is primitive recursion with base 0 and step “the previous ”. monus recurses on the subtracted number, applying pred once per step, then swaps the arguments. A test for zero, , and equality, zero of , give the remainder: count up and wrap to 0 when the count reaches . div adds 1 each time the remainder of the next number is 0 (test_exercise_12_primitive_recursive).
15.7 Weekend problem
Problem 15.1
Weekend problem — The busy beaver
This problem counts the two-state machines, runs them all, finds the champion of Output 15.7 by hand, and proves that no program computes the busy beaver function. Machines are written as in busy_beaver.py: 1LB means write 1, move left, go to state B; the transitions are listed for A0, A1, B0, B1.
Part I — How many machines.
- How many actions can one transition take, with states and a halting state? How many machines with states are there?
- Check the counts 64 and 20,736 of Output 15.7. How many three-state machines are there?
- Why do exactly 32 of the 64 one-state machines halt, and why do they all halt after one step?
- The five-state proof enumerated 181,385,789 machines. How many five-state machines does the formula of question 1 give? Why can a proof enumerate far fewer?
- Two machines that differ only by swapping left and right take the same number of steps. Why?
Part II — Running them all.
- Why does
busy_beaver.pyneed a step cap at all? - What can the cap make the program get wrong, and in which direction?
- 9,784 machines halted within 100 steps. Why is it safe to say the other 10,952 never halt, given that is proved?
- Without knowing , what would a cap of 100 prove about ?
- The test runs every two-state machine for 1,000 steps and finds none that halts after step 6. Why is that evidence, and not a proof?
- Give a simple argument proving that a machine never halts when it has no transition to H that can ever be taken from the states it visits.
Part III — The champion.
- Run the champion
1LB 1RB 1RA 1LHby hand from the blank tape, writing the configuration after each step. How many steps, how many 1s? - Why does its last transition write a 1 rather than a 0?
- Write its mirror image. Why does it have the same counts?
- The program finds 4 machines that leave four 1s. Which are they?
- It finds 40 machines that take six steps. Why many more than for ?
- Can a machine take steps without leaving 1s? Use the counts above.
Part IV — No program computes .
- Prove for every .
- Prove, as in Theorem 15.19, that a program computing would decide halting on the blank tape.
- Why does that contradict Theorem 15.12?
- From Exercise 15.11: why does a function that grows faster than every computable function fail to be computable?
- Each known value of needed a proof that every other machine never halts. Why does no single program give those proofs for every ?
- What does a proof checked by a proof assistant add, for a result that rests on 181 million cases?
- Sum up what was computed, and what cannot be.
Solution
Solution of Problem 15.1.
1. Write 0 or 1, move left or right, go to one of states: actions. Each of the pairs (state, symbol) gets one: machines.
2. and . For three states, .
3. From the blank tape the only transition ever used is A0. Of its 8 actions, 4 go to H and 4 back to A: those 4 times the 8 choices for the unused A1 make 32 halting machines, each after one step. The others repeat A0 for ever.
4. . A proof needs one representative per class of equivalent machines: it fixes the first move, ignores mirror images, and builds the table only as far as the run uses it, filling a transition the first time it is reached; unused transitions never multiply the count.
5. Every run is the mirror image of the other’s: the tape is reversed, and the steps and the 1s are the same.
6. Some machines never halt, and no program can tell in general which: without a cap the enumeration would never end.
7. It can count as non-halting a machine that halts after the cap, and so underestimate and ; it never overestimates them.
8. Every halting two-state machine halts within steps, so one that is still running at step 100 never halts.
9. Only : some machine might halt after step 100.
10. A machine could halt after step 1,000; a larger cap pushes the doubt back without removing it. The proof of shows for each remaining machine why it never halts.
11. From the blank tape, consider the states the machine can reach by following the transitions from A. If none of them has a transition to H, then at every step the machine is in one of these states, and some transition applies (every pair has one), so it never halts.
12. [A]0, [B]01, 1[A]1, 11[B]0, 111[A]0, 11[B]11, 1[H]111, with 0 for blank: 6 steps, four 1s.
13. The halting transition is B1: it reads a 1. Writing 1 keeps it; writing 0 would erase it and leave three 1s.
14. 1RB 1LB 1LA 1RH: every move reversed, the same run reflected.
15. The champion and its mirror, each with the halting move left or right: the last move does not change the tape.
16. The steps do not depend on what the halting transition writes, nor on its move; and several different runs of six steps exist, leaving fewer 1s.
17. Yes: 40 machines take six steps, only 4 leave four 1s.
18. Take a machine with states that takes steps. Replace its halting transition by one to a new state, which then halts in one more step: states, steps.
19. Given with states, compute and run for steps from the blank tape. If it has not halted, it never will, since a halting machine of states takes at most steps.
20. Halting on the blank tape is undecidable: reduces to it by , written with two symbols.
21. If it were computable, it would exceed itself at all large .
22. A single program giving them would compute : from the proofs, enumerate the machines, discard those proved non-halting, and run the others to the end.
23. A computer counted the 181 million cases; the proof assistant checks that the program which did so, and the argument it implements, are correct, rather than asking readers to trust the code.
24. An enumeration with a cap gives lower bounds for and , and a proof that the remaining machines never halt makes them exact: and by the chapter’s program, by a collaboration. No program computes for every , since that program would decide halting, and outgrows every computable function.
এই অধ্যায়ে সংজ্ঞায়িত পরিভাষা-শব্দ
- Busy beaver function
- Church–Turing thesis
- Decider, decidable language, recognisable language
- Encoding of a machine, universal Turing machine
- Halting problem
- Many-one reduction
- Multi-tape Turing machine
- Post correspondence problem
- Primitive recursive function, μ-recursive function, Ackermann function
- Semantic property, non-trivial property
- Turing machine, configuration, transition function of a Turing machine