Some programs stop. Some run forever. It feels like, with enough cleverness, you could always tell which — write one master program, HALTS(program, input), that reads any code and answers yes, it stops or no, it loops. In 1936 Alan Turing proved that this master program cannot exist. Not "is hard to write" — cannot exist, for any machine, ever. Scroll through to see the dream, then watch a single self-referential program tear it apart.
Here are four tiny programs. Press Run and watch each one execute.
The simple ones you can judge by eye — but "will this program halt on this input?" in
general has no algorithm. So we imagine a perfect oracle box,
HALTS(program, input), that always answers correctly, instantly, without running anything.
print "hi" — does one thing, then ends. HALTSfor n = 1..5: print n — a bounded loop. HALTSwhile true: x = x + 1 — never reaches an end. LOOPS FOREVERsearch for an odd perfect number; stop if found — nobody knows. UNDECIDED by eyeProgram D is the catch: deciding it would settle a 2,000-year-old open question in
mathematics. A perfect HALTS would answer it — and every question like it —
for free. That is exactly the power we'll prove impossible.
Suppose HALTS exists. Then we can build a small program around it called
TROUBLE. TROUBLE takes a program P, asks
HALTS(P, P) — does P halt when fed its own code? — and then does the opposite:
if HALTS(P, P) says "halts" → loop forever;
else → stop immediately.Now feed TROUBLE its own code: run TROUBLE(TROUBLE). Step through both branches and
watch each one collapse. The button toggles which answer the oracle gives about TROUBLE; both are wrong.
Either way, HALTS is wrong about TROUBLE. A correct oracle can't be wrong —
so no universal HALTS can exist. The contradiction comes from self-reference: TROUBLE
is built to defy whatever HALTS predicts about it. This is the same diagonal move Cantor used on infinite
sets and Gödel used on provability.
The halting problem is the first known undecidable problem: a precise yes/no question for which no algorithm can give the right answer for every input. Not "too slow" — impossible in principle, with unlimited time and memory.
Gödel (1931) showed any rich enough formal system has true statements it can't prove — using the
same self-referential trick ("this statement is unprovable"). Turing's 1936 halting result is the
computational twin of it.
Why it bites in practice: a perfect bug detector ("does this program ever crash / leak memory /
reach this line?") or a perfect virus detector ("does this program ever do something malicious?")
would let you decide halting — so by the same proof, no perfect general detector can exist. Real
tools approximate: they're sound or complete, never both, and punt with "maybe" on the hard cases.
Some limits aren't about being clever enough; they're built into computation itself.