computability
The Halting Problem
You should know: turing machines, computability and decidability
Overview
The halting problem asks: given the description of an arbitrary program and an input, will that program eventually halt, or will it run forever? Alan Turing proved in 1936 that no algorithm can solve this problem correctly for every possible program/input pair — the halting problem is undecidable. This was the first natural example of an undecidable problem and remains the central result of computability theory: it establishes, once and for all, that there are precisely stated, well-defined yes/no questions that no computer program can ever answer correctly in general, no matter how clever or how much time and memory it is given.
Intuition
Imagine a universal antivirus program that could look at ANY piece of code and its input and always correctly tell you whether it will eventually stop or loop forever — no false positives, no false negatives, no timeouts, ever. Turing showed such a program is a logical impossibility, using a trick similar to the classic liar's paradox ('this sentence is false'). Suppose the perfect halt-checker existed. Then you could build a devious program that asks the checker about ITSELF, and does the exact opposite of whatever the checker predicts: if the checker says 'you'll halt,' the devious program loops forever; if it says 'you'll loop forever,' the devious program halts immediately. Either way the checker's prediction about this specific program is wrong — a flat contradiction. Since the checker was assumed perfect, no such universal checker can exist.
Formal Definition
The halting problem is the language HALT (equivalently framed as a decision problem over pairs of a machine description and an input):
Notation
| Notation | Meaning |
|---|---|
| An encoding of a Turing machine M together with an input string w, as a single string | |
| A hypothetical Turing machine that decides HALT (assumed for contradiction) | |
| The diagonal machine built from H that produces the contradiction |
Proofs
- (This is the assumption we will refute — H is a total, always-correct halting decider.)
- (D feeds M's own description as both the machine and the input to H, asking 'does M halt when run on its own description?')
- (D is deliberately built to do the opposite of whatever H predicts.)
- (This is the self-referential, 'diagonal' move — feed the machine its own code, exactly as Cantor's diagonal argument feeds a list its own index.)
- (Either way, H's prediction about D running on its own description is wrong, contradicting the assumption that H always decides HALT correctly.)
- (The assumption in step 1 leads to a logical impossibility in all cases, so it must be false.)
Theorems
Applications
Worked Examples
Undecidability of HALT is about the GENERAL problem over all machine/input pairs, not any one fixed, simple case — plenty of individual programs are easy to analyze by hand or simulation.
Answer: Yes — this particular tiny program obviously halts; undecidability means there is no single ALGORITHM that correctly answers the question for every possible program, not that every individual case is mysterious.
Practice Problems
Explain in your own words why the machine D constructed in the proof cannot be assigned a consistent halting behavior on input ⟨D⟩, assuming H exists and is correct.
A team wants to build a tool that PERFECTLY flags every possible infinite loop in any submitted code, with zero false positives and zero false negatives. Is this achievable, and what do real static analysis tools do instead?
The proof that the halting problem is undecidable uses which technique?
Is the halting problem's complement — the set of ⟨M,w⟩ pairs where M does NOT halt on w — Turing-recognizable? Why does this matter for HALT's decidability status?
Common Mistakes
Believing undecidability of the halting problem means we can never determine whether ANY specific program halts.
Undecidability is about the absence of a single general-purpose algorithm that works correctly on EVERY program/input pair. Countless individual programs can be proven to halt or loop forever by direct analysis, simulation, or mathematical argument — it's the universal, one-size-fits-all decider that's impossible.
Thinking the diagonalization proof is just a clever trick specific to halting, unrelated to other famous results.
The same self-referential, diagonal structure underlies Cantor's uncountability of the reals (1891), Russell's paradox, and Gödel's incompleteness theorems (1931) — it is one of the most powerful and recurring proof techniques in all of logic and mathematics.
Assuming faster computers or quantum computers could eventually solve the halting problem.
Undecidability is a fact about the mathematical structure of the problem, not about computational speed or resources — no amount of extra time, memory, or even a hypothetical infinitely fast machine changes the logical impossibility proven by the diagonal argument.
Quiz
Historical Background
Turing proved the undecidability of the halting problem in his 1936 paper 'On Computable Numbers, with an Application to the Entscheidungsproblem,' as a route to answering David Hilbert's 1928 Entscheidungsproblem — whether there exists an algorithm that decides the truth of any mathematical statement. Turing's negative answer, arrived at independently and almost simultaneously by Alonzo Church via lambda calculus, showed that mechanical mathematics has fundamental, provable limits. The proof technique — diagonalization — traces back to Georg Cantor's 1891 proof that the real numbers are uncountable, adapted by Turing (and earlier by Gödel, for incompleteness) into a self-referential construction over programs instead of numbers.
- 1891
Georg Cantor introduces the diagonal argument, proving the reals are uncountable
Georg Cantor
- 1928
David Hilbert poses the Entscheidungsproblem: does an algorithm exist to decide the truth of any mathematical statement?
David Hilbert
- 1931
Kurt Gödel's incompleteness theorems use a related self-referential technique to show formal systems have unprovable true statements
Kurt Gödel
- 1936
Alan Turing proves the halting problem is undecidable, answering the Entscheidungsproblem negatively
Alan Turing
- 1936
Alonzo Church independently proves an equivalent undecidability result using lambda calculus
Alonzo Church
Summary
- HALT = {⟨M,w⟩ : M halts on input w} — deciding it for all machine/input pairs is impossible (Turing, 1936).
- The proof assumes a decider H exists, builds a self-referential machine D = 'do the opposite of what H predicts about me', and derives a contradiction by running D(⟨D⟩).
- This diagonalization technique echoes Cantor's uncountability proof and Gödel's incompleteness theorems.
- HALT is Turing-recognizable (simulate and accept) but not co-recognizable, which is exactly why it is undecidable rather than merely 'hard'.
- Undecidability is about the impossibility of a universal algorithm, not about any single program being unanalyzable — it explains why real tools use timeouts and heuristics instead of a perfect halt-checker.
References
- WebsiteWikipedia — Halting problem
- PaperAlan Turing, 'On Computable Numbers, with an Application to the Entscheidungsproblem', Proceedings of the London Mathematical Society, 1936
Mathematics