Section 19.2.2

Halting problem, Rice’s theorem, and restricted verifiers

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.

  1. Suppose an algorithm HH takes the text of any program PP and any input xx, always stops, and answers yes exactly when PP stops on xx.

  2. Build a program DD that reads the text of a program PP, runs HH on PP with the text of PP itself as the input, and then does the opposite of the answer: if HH says yes, DD loops forever; if HH says no, DD stops.

  3. Run DD on its own text. If DD stops, then HH answered yes about DD on its own text, so DD loops forever. If DD loops forever, then HH answered no, so DD stops. Both cases are contradictions.

  4. So HH 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 PP on xx 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.

  1. 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.

  2. Pick a program QQ that has the property; one exists because the property is non-trivial.

  3. Given any program MM and input xx, build a program P′P' that, on input yy, first runs MM on xx and then, if that run stops, runs QQ on yy.

  4. If MM stops on xx, then P′P' computes the same function as QQ and has the property. If MM never stops on xx, then P′P' runs forever on every input and lacks the property.

  5. A decider for the property would therefore decide whether MM stops on xx, 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 MM on xx and then writes outside an array is unsafe exactly when MM stops on xx, 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.

Fuel. The VM sets a counter to the budget NN 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 NN 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 xx, a whole number of at least 0. Its instructions are:

  1. set rr to xx;

  2. if r=0r = 0, go to instruction 5;

  3. subtract 1 from rr;

  4. go to instruction 2;

  5. stop.

Count the instructions executed. Instruction 1 runs once. Each of the xx passes through the loop runs instructions 2, 3 and 4, which is 3x3x instructions. The final pass runs instruction 2, finds r=0r = 0, and runs instruction 5, which is 2 more. The total is 1+3x+2=3x+31 + 3x + 2 = 3x + 3.

With a budget of N=100N = 100:

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: 3x+3≤1003x + 3 \le 100 holds exactly when x≤32x \le 32, 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 PP, let φP\varphi_P be the partial function it computes: φP(y)\varphi_P(y) is the output of PP on input yy, and it is undefined when PP runs forever on yy.

Let a verifier VV answer accept or reject for each program, for a property Π\Pi. The verifier is total when it stops on every program, sound when V(P)=acceptV(P) = \text{accept} implies that PP has Π\Pi, and complete when it accepts every program that has Π\Pi. For a non-trivial semantic property Π\Pi, 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].