Complexity Theory Module

Undecidability & Halting Problem

The mathematical limits of computation, Alan Turing's proof of the Halting Problem via diagonalization, and Rice's Theorem.

1. Computability and the Church-Turing Thesis

Before we ask how fast an algorithm can run, we must ask the deeper question: Can an algorithm exist at all?
A problem is Decidable (or computable) if there exists an algorithm (Turing machine) that halts on every input and gives the correct YES/NO answer. If no such algorithm can possibly exist, the problem is Undecidable.

The Cardinality Argument: Why Undecidable Problems Must Exist

  • Every computer program or algorithm can be represented as a finite string of binary code (ASCII / UTF-8 source code). Therefore, the set of all possible computer programs is Countably Infinite ($|\text{Programs}| = \aleph_0$).
  • A decision problem is a function $f: \Sigma^* \to {0, 1}$. The set of all possible decision problems is the power set of binary strings, which is Uncountably Infinite ($|\text{Problems}| = 2^{\aleph_0} = \mathfrak{c}$).
Because the set of problems is uncountably infinite while the set of programs is countably infinite, an infinity of problems exist for which NO computer program can ever be written!

2. The Halting Problem ($A_{\text{TM}}$)

The Halting Problem asks: Given the source code of any arbitrary program $P$ and an input $x$, determine whether $P$ will eventually finish executing (halt) or run forever in an infinite loop.
$$A_{\text{TM}} = { \langle M, w \rangle \mid \text{Turing machine } M \text{ halts and accepts string } w }$$

Turing's Proof by Contradiction (Diagonalization, 1936)

Assume, for the sake of contradiction, that there exists an algorithm Halt(P, x) that solves the Halting Problem:
python
def Halt(program_code: str, input_data: str) -> bool:
    """Returns True if program halts on input_data, False if it loops forever."""
    # Assume this function exists and always returns the correct answer in finite time
    ...
Now, construct an adversarial program called Opposite(P):
python
def Opposite(program_code: str):
    if Halt(program_code, program_code):
        # If Halt says program halts on its own code, loop forever!
        while True:
            pass
    else:
        # If Halt says program loops forever, halt immediately!
        return
Now, what happens when we execute Opposite and pass its own source code as the input?
$$\text{Opposite(Opposite)}$$
  • Case 1: Halt(Opposite, Opposite) returns True. Then according to the code, Opposite enters an infinite while True: loop! It does not halt. Contradiction!
  • Case 2: Halt(Opposite, Opposite) returns False. Then Opposite immediately executes return and halts! Contradiction!
In both cases, we reach an inescapable logical paradox. The only faulty assumption was that Halt(P, x) could exist. Therefore:
$$\text{The Halting Problem is Mathematically Undecidable!}$$
text
                Halt(Opposite, Opposite)
                      │
        ┌─────────────┴─────────────┐
     Returns True                Returns False
        │                           │
        ▼                           ▼
  Opposite loops!            Opposite halts!
 (Contradiction!)           (Contradiction!)

3. Rice's Theorem: The Ultimate Impossibility Result

In software engineering, developers frequently wish for static analysis linters or compilers that can automatically check properties of programs:
  • "Does this function ever throw a NullPointerException?"
  • "Does this service ever leak memory?"
  • "Are these two different microservice functions functionally equivalent?"
Rice's Theorem (1953) crushes this dream:
Any non-trivial semantic property of a Turing-complete programming language is mathematically undecidable.
  • Semantic Property: A property about the behavior or output of the program, not its syntactic source text.
  • Non-trivial: A property that holds for some programs, but not all programs.

What Rice's Theorem Means for Compilers and SREs

No compiler, linter, or formal verification tool can ever be $100%$ accurate on arbitrary code. Every real-world static analyzer (e.g. TypeScript compiler, SonarQube, Clang-Tidy) must make a compromise:
  • False Positives (Soundness): Flagging safe code as potentially buggy.
  • False Negatives (Completeness): Missing real production bugs.
  • Timeouts / Incompleteness: Giving up after a heuristic threshold.