Turing Machines and the Halting Problem for Mathematicians
Run a small Turing machine by hand, follow the proof that halting cannot be decided, and see what that means for questions in mathematics.
Key takeaways
- A Turing machine is a finite rule table acting on an unbounded tape, and it captures what we mean by an algorithm.
- One universal machine can imitate any other machine when given its description as input.
- Assuming a halting decider exists lets you build a machine that does the opposite of what the decider predicts about it.
- Halting can be confirmed by running the machine, but running forever cannot always be confirmed.
- Undecidability transfers to other problems by showing that a solver for them would also solve halting.
A Turing machine is a stripped-down mathematical model of step-by-step calculation: a tape of symbols, a head that reads and writes one cell at a time and a finite table of rules. The halting problem asks for a single procedure that, given any machine and input, says whether the run will ever stop. No such procedure exists.
The proof takes only a few lines, and its consequences reach well beyond computing. Questions in logic, algebra and number theory turn out to be unsolvable by any algorithm because halting can be hidden inside them.
The parts of a machine
The tape is a row of cells extending as far as needed, each holding a symbol or blank. The head sits over one cell. The machine is always in one of finitely many states, which serve as its memory of what it is doing.
Each rule has the form: if in this state and reading this symbol, then write that symbol, move left or right, and enter that state. When the machine reaches a designated halting state, it stops.
That is the whole definition. Alan Turing introduced it in 1936 by asking what a person computing with pencil and paper can do in a single step: look at a bounded amount, change a bounded amount and move attention a bounded distance.
A worked example: adding one in binary
Here is a machine that adds 1 to a binary number. The head starts on the rightmost digit, in a state called "carry".
| State | Reads | Writes | Moves | Next state |
|---|---|---|---|---|
| carry | 1 | 0 | left | carry |
| carry | 0 | 1 | left | halt |
| carry | blank | 1 | left | halt |
Run it on 1011, which is eleven.
Tape 1011, head on last 1 read 1: write 0, move left
Tape 1010, head on next 1 read 1: write 0, move left
Tape 1000, head on 0 read 0: write 1, halt
Result: 1100 which is twelve On the input 111 the carry runs off the left end, reads a blank and writes 1, giving 1000. Three rules handle numbers of any length, because the tape, and not the rule table, holds the data. The arithmetic of base 2 is reviewed in binary numbers explained.
One machine to run them all
A rule table is finite, so it can be written as a string of symbols. Turing showed how to design a universal machine that takes such a description, plus an input, and carries out what the described machine would do.
This is the theoretical form of a programmable computer: one fixed device, with programs treated as data. It also means a machine can be given its own description as input, which the next argument uses.
The claim that Turing machines can carry out every effective procedure is not a theorem, since "effective procedure" is an informal idea. It is the Church-Turing thesis, supported by the fact that very different proposed models of computation have turned out to compute the same functions.
The halting problem
Some machines stop. Others loop or wander forever. Suppose there were a machine H that takes a program P and input x, always finishes, and answers correctly whether P run on x halts.
Using H, build a new program D that takes a program P as input:
D(P):
ask H whether P halts when given P as input
if H says "halts": loop forever
if H says "never halts": stop at once Now run D on its own description. If D(D) halts, then H answers "halts", so D loops forever. If D(D) never halts, then H answers "never halts", so D stops.
Both cases are impossible. Every step in building D was legitimate once H existed, so H does not exist. This is a proof by contradiction with the same diagonal shape as Cantor's argument and Russell's paradox.
What the theorem does not forbid
You can often tell whether a particular program halts. The result says only that no single method works for every program and input.
You can also build a partial tester that answers "halts", "never halts" or "I don't know", and make it very useful in practice. It cannot eliminate the third answer in all cases while staying correct.
And if you limit the memory to a fixed finite amount, halting becomes decidable in principle: there are finitely many configurations, so a run either halts or repeats one. The count of configurations is typically far too large to explore.
There is an asymmetry as well. If a program halts, you will eventually learn this by running it. If it does not, waiting never tells you. The set of halting pairs is called semi-decidable or computably enumerable.
Counting tells you most problems are unsolvable
There are only countably many Turing machines, since each is a finite string. There are uncountably many sets of natural numbers. So most sets have no machine that decides membership.
The same holds for real numbers: only countably many have digits a machine can produce, as discussed in computable and uncomputable numbers. The halting argument goes further than counting by naming a specific, natural question that cannot be decided.
Halting hiding inside mathematics
Consider a program that checks the even numbers 4, 6, 8 and so on, looking for one that is not a sum of two primes. It halts exactly if Goldbach's conjecture is false. Many unsolved questions can be phrased this way, so a halting decider would be an extraordinary oracle.
To prove another problem unsolvable, one uses a reduction: show how to translate any halting question into an instance of the new problem. Several celebrated results work like this.
- First-order validity has no decision procedure, as Church and Turing showed in 1936.
- Hilbert's tenth problem asked for a method to decide whether a polynomial equation with integer coefficients has integer solutions. Work of Martin Davis, Hilary Putnam and Julia Robinson, completed by Yuri Matiyasevich in 1970, showed there is none.
- The word problem for groups, deciding whether a product of generators equals the identity, is unsolvable for certain finitely presented groups, by work of Pyotr Novikov and William Boone in the 1950s.
- Tiling the plane with a given finite set of square tiles with colored edges is undecidable, as Robert Berger showed in the 1960s.
Rice's theorem adds a sweeping version: every nontrivial question about what a program computes, as opposed to how its text looks, is undecidable.
A reduction you can follow
Suppose someone claims to have a tester T that decides whether a program ever prints the word "done". You can turn T into a halting decider.
- Take any program P and input x.
- Remove or rename every place where P itself would print "done".
- Build a new program Q that runs this altered P on x and, if that run ever stops, prints "done".
- Ask T about Q.
Q prints "done" exactly when P halts on x. So the answer from T settles the halting question. Since no halting decider exists, neither does T.
Watch the direction, which is the most common error. To show a new problem is hard, you translate halting into the new problem. Translating the new problem into halting proves nothing about its difficulty, since even very easy questions can be phrased that way.
The link with incompleteness
Take a formal system whose theorems can be listed by a program and which never proves a false statement about halting. Suppose it were also complete. To learn whether a program halts, list all theorems until you find either "it halts" or "it does not halt".
Completeness guarantees one will appear, and soundness guarantees it is correct. That would decide the halting problem. So no such system is complete. This is a short route to a version of Gödel's first theorem.
Busy beavers
Among all machines with n states and two symbols that eventually halt when started on a blank tape, ask which runs longest. The resulting function of n is the busy beaver function. It grows faster than any computable function, since a computable upper bound would let you decide halting by waiting that long.
Individual small values can still be proved. The five-state case was settled in the mid-2020s by a collaborative effort with computer-checked proof. Each further state brings far harder machines, some tied to open number-theoretic questions, so check current sources for the state of play.