Definition
The halting problem is the question whether a program stops on a given input. No algorithm §14.1 answers it correctly for all programs and inputs of a general language with unlimited memory, a result going back to Turing (1936). Rice’s theorem (Henry Rice, 1953) extends this to every question about what a program computes, rather than how it is written, unless all programs or none have the property. A restricted verifier checks a property only for a limited class of programs, or rejects what it cannot prove, and contradicts neither result.
The idea
A virtual machine §2.3 (VM), a program that runs other programs written for an imaginary processor, counts each instruction it executes and stops the guest program after 100 instructions. This counter is called an instruction budget §2.32, or fuel. Every guest program now stops, and someone concludes that the halting problem is solved. It is not. The halting problem asks whether a program stops by itself. The fuel counter asks nothing: it forces a stop, so the program that runs is no longer the original program but the original program cut off at 100 instructions. Whether a program stops within 100 instructions was always easy to decide: run it for 100 instructions and watch.
No algorithm can answer the general question, because a program can be built to contradict any claimed halting checker: it asks the checker about itself and does the opposite of the answer. Rice’s theorem shows that this is not a special defect of the halting question: a checker for almost any question about a program’s behavior could be turned into a halting checker, so none exists either.
Real verifiers work anyway, because they answer narrower questions. They accept only programs from a restricted class, they reject what they cannot prove, or they enforce a bound while the program runs. Each of these changes the question into one that is decidable §19.2.1; none decides the original question.
How it works
Why no halting checker exists. The argument is a proof by contradiction.
Suppose an algorithm takes the text of any program and any input , always stops, and answers yes exactly when stops on .
Build a program that reads the text of a program , runs on with the text of itself as the input, and then does the opposite of the answer: if says yes, loops forever; if says no, stops.
Run on its own text. If stops, then answered yes about on its own text, so loops forever. If loops forever, then answered no, so stops. Both cases are contradictions.
So cannot exist.
Feeding a program its own text, as step 2 does, is called diagonalization. The halting problem is still semidecidable, meaning that an algorithm confirms every yes: run on and answer yes when it stops.
Rice’s theorem. A property of programs is semantic when it depends only on the function the program computes: for each input, which output the program produces, or that it runs forever. Two programs that compute the same function have the same semantic properties, however different their text. “Returns 0 on every input”, “stops on every input” and “outputs 42 for some input” are semantic. “Contains a division instruction” and “has fewer than 100 instructions” are syntactic: they depend on the text, and reading the text decides them. A property is non-trivial when at least one program has it and at least one program lacks it. Rice’s theorem states that every non-trivial semantic property is undecidable. The proof turns a decider for the property into a halting checker.
Assume that a program that runs forever on every input lacks the property. Otherwise, argue about the opposite property, which is decidable exactly when the property is.
Pick a program that has the property; one exists because the property is non-trivial.
Given any program and input , build a program that, on input , first runs on and then, if that run stops, runs on .
If stops on , then computes the same function as and has the property. If never stops on , then runs forever on every input and lacks the property.
A decider for the property would therefore decide whether stops on , which is impossible.
Some properties of behavior are not properties of the computed function. “Never writes outside an array §14.5” is one: two programs can compute the same function while only one of them writes outside an array. Rice’s theorem, as stated, does not cover such properties, but the same construction does. A program that runs on and then writes outside an array is unsafe exactly when stops on , so memory safety of general programs is undecidable too.
Restricted verifiers. A verifier that must always stop and must never accept a program that lacks the property has four ways around both results.
Restrict the language. If a program has no backward jumps and no calls, each instruction runs at most once, and every program stops within as many steps as it has instructions. A program with finitely many possible states can be checked by exhaustive search, as the Finite-state hardware versus unbounded computation models §19.2.3 entry explains.
Refuse what cannot be proved. A sound verifier, one that never accepts a program that lacks the property, rejects every program it cannot prove correct, including some correct ones. This is conservative refusal §3.36. The verifier then decides the question “can my method prove this program correct?”, which differs from the question “is this program correct?”. The Linux kernel’s verifier for eBPF §2.1 (extended Berkeley Packet Filter) programs works this way: it follows the program’s paths, rejects any program whose memory accesses it cannot show to be within bounds, and rejects any program whose analysis exceeds a fixed limit.
Enforce while running. An instruction budget, or a bounds check before each memory access, makes a forbidden behavior impossible instead of predicting whether it would happen.
Check the text. A list of permitted instructions, checked by reading the program, is decidable, because it asks about the text.
Fuel. The VM sets a counter to the budget before each run, subtracts 1 for each instruction executed, and stops the run with an out-of-fuel result when the counter reaches zero before the program finishes. The run therefore ends within instructions, whatever the program does. The VM does not learn whether the program would have stopped by itself after more instructions.
Worked example
A guest program counts down from its input , a whole number of at least 0. Its instructions are:
set to ;
if , go to instruction 5;
subtract 1 from ;
go to instruction 2;
stop.
Count the instructions executed. Instruction 1 runs once. Each of the passes through the loop runs instructions 2, 3 and 4, which is instructions. The final pass runs instruction 2, finds , and runs instruction 5, which is 2 more. The total is .
With a budget of :
For , the program needs instructions and finishes normally.
For , it needs . The VM stops it after 100 instructions with an out-of-fuel result, 2 instructions before it would have stopped by itself.
A second program, whose only instruction is “go to instruction 1”, never stops by itself. The VM also stops it after 100 instructions with an out-of-fuel result.
The two out-of-fuel results are identical. The fuel counter did not tell the program that would have stopped at instruction 102 apart from the program that never stops; it ended both.
The question that the counter settles, whether the program finishes within 100 instructions, is decidable, and the VM settles it simply by running: holds exactly when , so inputs up to 32 finish and larger inputs run out of fuel. This does not contradict the halting theorem, which concerns stopping after any number of steps.
Stated precisely
Fix a programming language in which every Turing machine can be simulated, which requires unlimited memory. For a program , let be the partial function it computes: is the output of on input , and it is undefined when runs forever on .
Halting theorem. The set is semidecidable and not decidable.
Bounded halting. For each budget , the set is decidable: simulate at most steps.
Fuel semantics. A VM with budget computes when , and otherwise. The function is defined for every input; computing it decides membership in , not in .
Rice’s theorem. Let be a set of partial functions such that some program has and some program has . Then the set is undecidable.
Let a verifier answer accept or reject for each program, for a property . The verifier is total when it stops on every program, sound when implies that has , and complete when it accepts every program that has . For a non-trivial semantic property , and for memory safety, no verifier is total, sound and complete at once over all programs of the language. A restricted verifier keeps total and sound and gives up complete, or keeps all three over a restricted class of programs on which the property is decidable.
Limits and common mistakes
The theorems assume unlimited memory. A real machine with a fixed amount of memory has finitely many states, so whether it stops is decidable in principle: a deterministic machine that receives no input and has not stopped after more steps than it has states has repeated a state, and it loops forever. The number of states is so large that this procedure is useless in practice; the Finite-state hardware versus unbounded computation models entry works through the numbers.
Undecidable is a precise claim about all programs of a general model. A check that is merely slow because of state explosion §3.26, not supported by a tool, or stopped by a timeout §9.34 is not thereby undecidable. Name the actual obstacle.
Fuel bounds the number of instructions, not the time. One instruction that calls a host function, code that the VM provides to the guest, can take an unbounded time unless that host function is bounded too, and interrupts §6.11 delay the run further. The Termination versus time bound §2.33 entry treats this.
Fuel changes the meaning of programs. A correct program that needs more instructions than the budget fails. The system must define what an out-of-fuel result means, for example that the request is refused, and must undo or tolerate any effects the program produced before it was stopped.
A check of the text guarantees a property of the text only. A program that uses only permitted instructions can still compute a wrong result or run for a long time.
A rejection by a sound but incomplete verifier is not evidence of a bug. The rejected program may be correct; the verifier only failed to prove it.
Rice’s theorem concerns deciding a property exactly, for all programs. It does not forbid analyses that prove the property for many programs, nor tests that find violations.
Questions answered
Does a fuel counter solve the halting problem?
No. A fuel counter, also called an instruction budget, stops a program after a fixed number of executed instructions, so every run ends, but it does not tell whether the program would have stopped by itself. The halting problem asks exactly that: given any program and input, will the program stop after some finite number of steps? A result that goes back to Alan Turing’s 1936 paper shows that no algorithm answers this correctly for every program of a general programming language with unlimited memory. With a budget of 100 instructions, a program that would stop at instruction 102 and a program that loops forever both end with the same out-of-fuel result. The question that the counter does settle, whether a program stops within 100 instructions, is decidable by running the program for 100 instructions, so there is no contradiction. The counter changes the program’s meaning, by turning every long run into an out-of-fuel result, instead of deciding the original question. It also bounds instructions, not time, unless every instruction, including calls into functions that the host provides, takes a bounded time.
Related entries: Decidability and semidecidability (§19.2.1), Finite-state hardware versus unbounded computation models (§19.2.3), Instruction budget (fuel) (§2.32), Termination versus time bound (§2.33), Conservative refusal (§3.36), Soundness (§3.7), Completeness (§3.8), Verifier soundness and safe-program rejection (§17.5). Further reading: [158, 159].