The Program That Cannot Exist
Your phone app freezes. Is it thinking hard, or is it stuck for ever? You wish for a checker: give it any program and its input, and it tells you "stops" or "loops". Could such a checker exist? That is the question of this lesson.
The question
A program, given an input, either stops after some steps, or loops: it runs for ever (see loops). The halting problem asks: given a program and an input, which one happens? Some cases are easy. A program that prints "hello" and ends stops. A program that says "repeat for ever" loops.
But we want one method for all programs and all inputs. In 1936 Alan Turing showed that there is none. A program is only text, so it can be the input of another program, even of itself, as an editor can open its own file. The proof uses that.
Suppose Stopper exists
Suppose someone has built Stopper. Given any program and input, it always stops, and it always answers "stops" or "loops" correctly. Now we use Stopper to build a new program, Contrary. Contrary takes a program p. It asks Stopper: "Does p stop when its input is p itself?"
Then Contrary does the opposite. If Stopper says "stops", Contrary loops for ever. If Stopper says "loops", Contrary stops at once.
Run Contrary on itself
Now give Contrary to itself. Stopper must say what Contrary does on that input. Suppose it says "stops". Then Contrary loops for ever, so Stopper was wrong. Suppose it says "loops". Then Contrary stops, so Stopper was wrong again.
Both answers are wrong. But Stopper was said to be always right. So Stopper cannot exist. Contrary is easy to build once Stopper exists, so the thing that cannot exist is Stopper. This is the diagonal trick: Contrary differs from every program p on the input p.
Be careful. This does not say we can never tell if a program stops. We can for many. It says there is no single method that answers correctly for every program and input.
Careful
This does not say that no program can be known to stop. It says no single method works for all.
Try it at home
- Write a one-line program that stops, and one that loops for ever. Could you tell which is which? Say how.
- In the story, what does Contrary do if Stopper says "stops" about it? And if Stopper says "loops"?
- In your own words: why does the story end with Stopper, and not Contrary, being impossible?
Words to know
- halting problem - tell, for any program and input, whether it stops or loops for ever
- loops - runs for ever and never gives an answer
- Contrary - the program built from Stopper that does the opposite of what Stopper says about it
Big idea
No program answers "stops or loops?" correctly for every program and input: Contrary, run on itself, defeats any Stopper.