AQA 7517, section 4.4.4.7
What is the halting problem?
Some programs finish after a while. Others never do, because they get stuck in an infinite loop.
def stops(n):
while n > 0:
n = n - 1 # n reaches 0, so the loop ends
return "stopped"
def never_stops(n):
while n > 0:
n = n + 1 # n keeps growing, so the loop never ends
return "stopped"
For a program like these you can often see the answer by reading the code. The halting problem asks for a general solution:
Given any program and any input, can we write an algorithm that decides whether the program will eventually stop (halt) or run forever?
The answer is no. The halting problem is unsolvable: no algorithm can do this correctly for every program and every input. This was shown by Alan Turing in 1936.
You need to be able to describe the halting problem, but you do not need to prove it.
Why is it hard?
You might think you could just run the program and see what happens. That works if it halts, because you see it finish. But if the program is still running, you can't tell whether it's about to finish a moment later or will never finish. You can never be sure it is running forever.
Even apparently simple programs can be hard to predict. Consider this one, which stops when it reaches 1:
def collatz(n):
while n != 1:
if n % 2 == 0:
n = n // 2
else:
n = 3 * n + 1
Nobody has ever found a starting value for which this runs forever, and nobody has proved that it always stops. Mathematicians have tried to answer this (the Collatz conjecture) for decades, and it remains unsolved.
The idea behind the proof (not required)
The argument is a proof by contradiction, and the key idea is as follows.
- Suppose a perfect halting checker
halts(program, input)existed. It always answersTrueorFalse, correctly. - Then write a new program that does the opposite of what the checker predicts about itself: if the checker says "this program halts", it loops forever, and if the checker says "it runs forever", it halts.
- Now ask the checker about that program. Whatever the checker answers, it is wrong.
This is a contradiction, so the perfect checker can't exist.
Why does it matter?
- It shows that there are problems that no computer can solve, however powerful it is or however clever the programmer. These are non-computable problems (4.4.4.6).
- It sets a fundamental limit on computation, so we can't build a perfect program to find every infinite loop, or to prove that every program is bug-free.
- It showed that the idea of computability is a precise mathematical subject.
- It uses the idea of the Turing machine (4.4.5.1).
Note that this doesn't mean we can never tell if a particular program halts. We often can. It only means there is no single algorithm that works for all programs.
Worked example
Task: a software company claims their new tool can analyse any program and tell you for certain whether it will ever get stuck in an infinite loop. Explain why you should be sceptical.
The claim amounts to saying that they have solved the halting problem, which Turing proved is impossible: no algorithm can decide correctly whether every program halts. The tool might work for many programs, or give a guess, but it can't be right for all programs. It would have to fail on some, either by giving a wrong answer or by never finishing itself.
Exam tip: the key phrase is "the unsolvable problem of determining whether any program will eventually stop, given particular input". Add its significance: it shows there are problems that cannot be solved by a computer, which gives a limit on what computers can do. You don't have to give the proof.
Check yourself
Describe the halting problem
The problem of determining whether any given program will eventually stop (halt) or run forever when given a particular input. It has been shown that no algorithm can solve it for all programs.
Why can't we simply run the program to find out if it halts?
If it halts, we'd see it stop. But if it is still running, we can't tell whether it will finish soon or never finish, however long we wait.
What is the significance of the halting problem?
It demonstrates that there are problems that cannot be solved by a computer, which sets a fundamental limit on what can be computed.