The undecidable problem of determining whether a Turing machine halts on a given input.
The halting problem asks whether a Turing machine eventually stops.
Given a machine and an input word , we ask:
Here “halt” means that the computation eventually reaches a stopping state, such as:
or:
If reaches one of these states after finitely many steps, then halts on . If the computation continues forever, then does not halt on .
The Halting Language
We can define the halting problem as a language:
$$ \mathrm{HALT}_{\mathrm{TM}}
{\langle M,w\rangle : M \text{ halts on input } w} $$
The input is an encoding:
where is a Turing machine and is an input word.
The question is whether this encoded pair belongs to .
Recognizable but Not Decidable
The halting problem is recognizable.
There is a Turing machine that recognizes :
- Given , simulate on .
- If halts, accept.
- If runs forever, keep simulating forever.
So positive cases can be found. If really halts, simulation eventually sees it.
But negative cases are the problem. If keeps running, there may be no finite moment when we can safely say:
The main theorem says that no algorithm solves this for all machines and inputs.
Theorem
There is no Turing machine that decides:
Equivalently, no algorithm can always determine whether an arbitrary machine halts on an arbitrary input.
To be a decider, a machine must halt on every input and answer correctly:
| Case | Correct answer |
|---|---|
| halts on | accept |
| does not halt on | reject |
The theorem says that such a decider does not exist.
Proof Idea
Assume, for contradiction, that there is a decider:
for the halting problem.
Then takes input:
and always halts.
It answers:
Now we build a new machine that uses as a subroutine.
On input , the machine does this:
- Run on .
- If says that halts on its own code, then loop forever.
- If says that does not halt on its own code, then halt.
So behaves opposite to what predicts about self-application.
In symbols:
Now ask what happens when runs on its own code:
There are two cases.
First, suppose halts on .
Then must say that halts on .
But by the definition of , if says this, then loops forever.
Contradiction.
Second, suppose does not halt on .
Then must say that does not halt on .
But by the definition of , if says this, then halts.
Contradiction again.
Both possibilities lead to contradiction. Therefore the assumed decider cannot exist.
What the Proof Uses
The proof depends on two earlier ideas.
First, machines can be encoded as strings:
Second, a machine can be run on the encoding of another machine, even on its own encoding:
This makes self-reference possible.
The contradiction comes from designing to disagree with on the special input:
This is a diagonal argument. It is similar in spirit to Cantor’s diagonal proof, but applied to computation.
Meaning of the Result
The halting problem does not say that we can never prove a particular program halts.
Many specific programs can be analyzed. For example, a program with a simple loop over a finite list clearly halts.
The theorem says something stronger and more precise:
So the failure is global, not local.
For some individual machine and input , we may prove that halts or prove that it does not halt. But no universal decision procedure can always do this automatically.
Relation to Program Analysis
The halting problem explains why perfect program analysis is impossible.
A tool may detect many infinite loops, but it cannot detect all of them while always giving correct answers.
A compiler may prove that many programs terminate, but it cannot prove termination for every terminating program and also reject every non-terminating one.
This is not a limitation of current technology. It is a mathematical limit.
Accepting vs Halting
It is useful to distinguish two related questions.
The acceptance problem asks:
The halting problem asks:
A machine may halt by accepting or rejecting. Therefore halting is broader than accepting.
The languages are:
$$ A_{\mathrm{TM}}
{\langle M,w\rangle : M \text{ accepts } w} $$
and
$$ \mathrm{HALT}_{\mathrm{TM}}
{\langle M,w\rangle : M \text{ halts on } w} $$
Both are central undecidable problems.
The halting problem marks a boundary in computation. Some questions can be asked clearly, encoded precisely, and still have no algorithmic solution.