Computability

The Halting Problem

1936. Alan Turing, age 23, publishes 'On Computable Numbers'. Inside: a proof that some mathematical problems cannot be solved by any computer, ever, with any amount of time and memory. Not a practical limitation - a mathematical impossibility. The Halting Problem: no program can decide, for an arbitrary program P and input X, whether P(X) will terminate or run forever. Coverity can't guarantee no infinite loops. Antivirus software can't definitively identify all malware. Compilers can't find all dead code. Every one of these limitations traces back to 24 pages written before real computers existed.

  • **Static analysis**: Coverity and SonarQube cannot guarantee absence of infinite loops - this is a direct consequence of the Halting Problem
  • **Antivirus**: determining whether an arbitrary program is a virus is undecidable in general (Rice's Theorem) - all antivirus software uses heuristics
  • **Compilers**: dead code elimination cannot provably find all dead code - some is structurally hidden behind undecidable conditions
  • **Formal verification**: Coq and Isabelle work around the limitation by requiring programmers to supply termination proofs explicitly

Turing, Gödel, and the limits of computation

In 1931 Kurt Gödel proved the incompleteness theorems: in any sufficiently powerful formal system, true statements exist that cannot be proved within the system. In 1936 Alan Turing independently showed the computational analogue: well-defined problems exist that no algorithm can solve. His Turing machine became the formal definition of 'algorithm'. The undecidability proof uses Cantor's diagonalization - a technique from 1891 originally applied to prove real numbers are uncountable. Turing was 23. The paper was written before any real computers existed. The same year, Alonzo Church independently proved an equivalent result via lambda calculus - today known as the Church-Turing thesis: anything computable by any reasonable model of computation is computable by a Turing machine.

Proof that HALT cannot exist

The Halting Problem asks: does an algorithm HALT(P, X) exist that - for any program P and input X - returns true if P(X) terminates and false if P(X) runs forever? Turing proved in 1936 that no such algorithm can exist.

**Proof structure:** Assumption (HALT exists) → Construction (PARADOX) → Self-application (PARADOX(PARADOX)) → Contradiction in both branches → Assumption refuted. This is proof by contradiction via self-reference. The same logical structure appears in Russell's paradox (the set of all sets that don't contain themselves) and the Liar's paradox.

PARADOX(PARADOX) calls HALT(PARADOX, PARADOX). HALT returns false (predicts looping). What does PARADOX do next?

Diagonalization: the proof technique

Diagonalization is Cantor's 1891 technique for proving uncountability. In computability theory: construct an object that differs from every element in a countable list - making it impossible to list all such objects.

**Programs are countable; functions are not.** Programs are finite strings - countably many. Functions f: ℕ → {0,1} are uncountably many (cardinality of the continuum). Therefore most functions are uncomputable - no program can compute them. The Halting Problem is a specific uncomputable function with practical consequences.

Why does the proof apply PARADOX to itself (PARADOX(PARADOX)) rather than to some arbitrary input?

Self-reference and the Recursion Theorem

Self-reference - a program that uses its own code as data. Kleene's Recursion Theorem (1938) guarantees that any such program can always be constructed. The PARADOX in the halting proof is not a thought experiment - it is constructive.

A Quine is a program that outputs its own source code. How does this relate to the halting proof?

Decidability classes

Computability theory classifies problems by decidability: decidable (algorithm always halts with an answer), semi-decidable (halts on 'yes'), and undecidable (no algorithm at all).

**Practical implication:** Static analyzers, compilers, and verifiers all hit Rice's Theorem. They use approximations: over-approximation (report more errors, including false positives) or under-approximation (only report guaranteed errors, miss some). Perfect precision is a mathematical impossibility, not an engineering challenge.

If a program runs for a long time, it must be stuck - kill the process

Finite running time tells nothing about whether a program will eventually terminate. This is a direct consequence of the Halting Problem.

For any timeout T, there exists a program that terminates after T+1 seconds. No algorithm distinguishes a 'slow but terminating' program from an 'infinite' one. That's why kill-on-timeout is a heuristic, not a solution.

A company is building a 'perfect' static analyzer that precisely identifies all infinite loops. Why is this impossible per computability theory?

Key Ideas

  • **Halting Problem**: no algorithm HALT(P, X) exists that determines for arbitrary P and X whether P(X) terminates
  • **Proof by contradiction + diagonalization**: PARADOX is constructed to contradict any hypothetical HALT prediction in both branches
  • **Self-reference**: the Liar's paradox in computational form - PARADOX applies HALT to itself
  • **Decidable / Semi-decidable / Undecidable**: three tiers of problems; most functions are uncomputable because programs are countable but functions are not
  • **Rice's Theorem**: any non-trivial semantic property of programs is undecidable - explains why perfect static analysis is impossible

What's next

Undecidability sets the absolute ceiling. Complexity theory partitions what lies below it.

  • Decidability — Broader framework of language and problem classification, with reduction proofs between undecidable problems
  • Rice's Theorem — Generalizes the halting proof - every non-trivial property of programs is undecidable

Вопросы для размышления

  • How does the Halting Problem explain why compilers cannot provably eliminate all dead code?
  • Rice's Theorem says any non-trivial semantic property is undecidable. But antivirus software works in practice. How does it circumvent this mathematical limitation?
  • Cantor's diagonalization proved the reals are uncountable in 1891. Turing used the same technique in 1936. What is the precise structural analogy between the two proofs?

Связанные уроки

  • comp-03 — Computability foundations and Turing machine model needed for understanding the halting proof
  • fl-18-turing-machine — Formal definition of 'algorithm' via Turing machines is the basis of the proof
  • fl-22-decidability — Decidability theory is the broader context - the Halting Problem is its canonical undecidable case
  • fl-25-rice-theorem — Rice's Theorem generalizes the Halting Problem to all non-trivial semantic properties of programs
  • fl-29-space-complexity — Complexity classes (PSPACE, etc.) build on the computability foundation established here
  • fl-23-halting-problem
  • cplx-04
  • fl-01-intro
The Halting Problem

0

1

Sign In