Turing Machines & Computability

What can be computed at all — Turing machines, the Church–Turing thesis, universal machines and Turing completeness, decidable vs semi-decidable problems, the halting problem and its proof, reductions, Rice's theorem, and what undecidability means for real software tools.

Advanced 6 min readLesson 8 of 9 Interview Q&A

Some problems can't be solved by any program, no matter how clever or how much time and memory it has. Computability theory draws that line, using the simplest possible model of a general-purpose computer: the Turing machine.

Turing machines

Alan Turing (1936) imagined a machine with:

  • An infinite tape of cells, each holding a symbol (initially the input, then blanks).
  • A head that reads and writes one cell and moves left or right.
  • A finite set of states, including accept and reject states.
  • A transition function: (state, symbol read) → (new state, symbol to write, move L/R).
read/writeFinite controlstate q₃1011__…

Unlike a finite automaton, it can write and move both ways, giving it unbounded memory. Compared with a pushdown automaton (Context-free grammars), it can access any part of its memory, not just the top of a stack.

Example: to decide {anbncn}\{a^nb^nc^n\} (not context-free), repeatedly cross off one a, one b and one c, scanning back and forth; accept if all are crossed off together.

The Church–Turing thesis

Every reasonable model of computation — lambda calculus, register machines, cellular automata, Python, C, your laptop — computes exactly the same set of functions as Turing machines. (Some are faster, but none can solve more problems.) The Church–Turing thesis says Turing machines capture what "algorithmically computable" means.

A system that can simulate a Turing machine is Turing complete: all general-purpose programming languages, but also SQL with recursive CTEs, C++ templates, Excel with LAMBDA, Conway's Game of Life, Minecraft redstone, and (with an infinite tape) even some card games. Turing completeness is easy to stumble into — which is one reason some configuration languages deliberately avoid it (Dhall, Starlark's restrictions, CSS's lack of loops) to guarantee termination.

Universal Turing machines

A universal Turing machine takes the description of another machine plus its input, and simulates it. That's the idea of the stored-program computer: programs are data, and one machine can run any program. Interpreters, virtual machines and emulators are universal machines in practice (Interpreters and VMs).

Decidable and undecidable problems

Phrase problems as languages (yes/no questions about strings — Automata):

ClassMeaningExample
Decidable (recursive)A machine always halts with the correct yes/no answerIs n prime? Does a DFA accept any string? Does this CFG generate string w?
Semi-decidable (recognisable, recursively enumerable)A machine halts and says yes on yes-instances; may run forever on no-instancesDoes program P halt on input x? (just run it…)
Not even semi-decidableNeitherDoes program P run forever on input x?

A problem is decidable exactly when both it and its complement are semi-decidable.

The halting problem

Given a program P and an input x, does P eventually halt when run on x?

Turing proved no program can decide this for all P and x.

Proof by contradiction (diagonalisation). Suppose a function halts(P, x) always returns correctly. Build:

Python
def paradox(P):
    if halts(P, P):         # would P halt when given its own source as input?
        while True: pass    # …then loop forever
    else:
        return              # …otherwise halt immediately

Now ask: does paradox(paradox) halt?

  • If halts(paradox, paradox) returns True, then paradox(paradox) loops forever — so it doesn't halt. Contradiction.
  • If it returns False, then paradox(paradox) returns immediately — so it halts. Contradiction.

Either way halts is wrong, so no such function exists. ∎

This is the same self-reference as Cantor's diagonal argument (Sets, relations and functions) and Gödel's incompleteness theorems. And it's not a contrived edge case: there are only countably many programs but uncountably many yes/no problems, so almost all problems are undecidable.

Reductions

To show a new problem B is undecidable, reduce a known undecidable problem A to it: show that a decider for B could be used to build a decider for A. Since A has no decider, neither does B.

Example — "Does program P ever print 'hello'?" Given (P, x) for the halting problem, construct P′: "run P on x (with its own output suppressed); then print 'hello'". P′ prints "hello" iff P halts on x. A decider for the printing problem would decide halting. So it's undecidable.

Reductions are also how NP-completeness is proved (Complexity classes) — the same idea, with a polynomial-time restriction.

Rice's theorem

Every non-trivial property of what a program computes is undecidable.

"Non-trivial" means some programs have the property and some don't; "what a program computes" means it depends on the program's input/output behaviour, not its text. Undecidable, in general:

  • Does this function ever return null? Ever divide by zero? Ever access an array out of bounds?
  • Are these two programs equivalent?
  • Is this code dead? Does it leak memory? Is it malware?
  • Does this program meet its specification?

(Syntactic properties — "does the source contain a goto?", "is it under 100 lines?" — are decidable.)

What this means for real tools

Undecidability doesn't make program analysis hopeless — it means every analysis tool must give something up:

ToolCompromise
Type checkersSound but incomplete: reject some correct programs ("can't prove this is safe") rather than accept unsafe ones
Linters, static analysers (ESLint, SpotBugs, CodeQL)Heuristics with false positives and false negatives
Borrow checkerConservative — rejects some valid programs, requiring restructuring (Borrowing)
Compilers' optimisersOptimise only what they can prove safe; dead-code elimination is necessarily partial
TestingChecks specific inputs; can show the presence of bugs, never their absence
Verification (Dafny, Coq/Rocq, Lean, TLA+)Requires humans to supply invariants and proofs, or restricts the language
AntivirusSignature and behaviour heuristics — perfect malware detection is impossible
Timeouts and watchdogsSince you can't predict halting, you bound execution time instead

Practical takeaways: when someone promises a tool that finds all bugs of some semantic kind with no false alarms for arbitrary programs, Rice's theorem says it can't exist. Restrict the language (total functional languages, regular expressions without backtracking, config languages without loops) or accept approximations.

Beyond: computability and complexity

Computability asks whether a problem can be solved at all. Among the decidable problems, complexity theory asks how much time and space they need — and finds that many decidable problems are still practically intractable (Complexity classes).

Key takeaways
  • A Turing machine is a finite control with an unbounded read/write tape — the reference model of computation.
  • Church–Turing thesis: every reasonable model computes the same functions; Turing-complete systems are everywhere.
  • Universal machines run other machines' descriptions — the stored-program idea behind interpreters and VMs.
  • Decidable problems always get an answer; semi-decidable ones may run forever on no-instances.
  • The halting problem is undecidable, proved by diagonalisation; most problems are undecidable.
  • Reductions spread undecidability; Rice's theorem makes every non-trivial semantic property of programs undecidable.
  • Real analysis tools are therefore conservative or approximate, or restrict the language.
Interview questions
QWhat is the halting problem and why does it matter to software engineers?medium

It asks whether there is an algorithm that, given any program and input, decides whether the program eventually halts. Turing proved there isn't, by constructing a program that does the opposite of whatever the supposed decider predicts about it running on itself. For engineers, it means — via reductions and Rice's theorem — that no tool can perfectly decide non-trivial behaviours of arbitrary programs: perfect dead-code detection, null-safety, equivalence checking or malware detection are impossible in general. Real tools are conservative (type systems, borrow checkers), heuristic (linters), or rely on tests, timeouts and human-supplied proofs.