Sub topic 13
Proving Undecidability by Reduction from the Halting Problem
This chapter assumes you know three things from earlier chapters. A many-one reduction from A to B is a computable function f where x is in A exactly when f(x) is in B. Hardness moves along a reduction from A to B. The third is Turing's theorem: the halting problem is undecidable.
The halting problem is HALT = { ⟨P, w⟩ : program P halts on input w }. It is the usual starting point for undecidability proofs. You prove a new problem B undecidable by reducing HALT to B. You never need to repeat Turing's diagonal argument.
The Shape of the Argument
The proof is by contradiction:
- Suppose some program D decides B.
- Give a computable function f with ⟨P, w⟩ ∈ HALT if and only if f(⟨P, w⟩) ∈ B.
- Then D(f(x)) decides HALT. That contradicts Turing's theorem, so D cannot exist.
In this setting f only has to be computable. It may take any amount of time, as long as it always finishes. The polynomial-time limit applies only to NP-completeness proofs.
Worked Example: Halting on Empty Input
Let HALT_ε = { Q : Q halts when given the empty input }. The reduction builds a wrapper program Q. Q ignores its own input, runs P on w, and halts if P does. The reduction never runs P. It only writes source code, so it always finishes.
def halt_to_halt_on_empty(program_src, w):
# program_src defines main(inp). The output defines wrapped_main(inp),
# which ignores inp and runs main on the fixed input w.
return (
program_src
+ "\n\ndef wrapped_main(_ignored):\n"
+ f" main({w!r})\n"
+ " return True\n"
)
src = "def main(inp):\n return len(inp)\n"
q_src = halt_to_halt_on_empty(src, "abc")
print(q_src)
namespace = {}
exec(q_src, namespace)
print(namespace["wrapped_main"]("")) # halts, because main halts on "abc"The correctness proof has two directions:
- If P halts on w, then Q runs P on w, finishes, and returns. So Q halts on the empty input.
- If P loops on w, then Q never gets past the call to main. So Q does not halt on the empty input, or on any other input.
Q acts the same way on every input, so the same f also shows that "Q halts on all inputs" is undecidable.
Common Targets
| Problem B | What the constructed Q does |
|---|---|
| Halts on empty input | Ignores its input and runs P on w |
| Halts on every input | Same construction as above |
| Accepts at least one string | Runs P on w, then accepts |
| Equivalent to a fixed program R | Runs P on w, then behaves like R on its input |
Rice's theorem generalizes this table. Any nontrivial property of the function a program computes is undecidable. Properties of the source text, such as "has more than 10 lines," are not covered by Rice's theorem, and many of them are decidable.
Exercise
Prove that ACCEPTS_HELLO = { Q : Q accepts the input "hello" } is undecidable. Give the reduction and both directions of correctness.
Show answer
Reduce HALT to ACCEPTSHELLO. Given (P, w), output a program Q that ignores its input, runs P on w, and then accepts. Writing Q is a mechanical text construction that always finishes, so f is computable. If P halts on w, Q accepts every input, including "hello". If P does not halt on w, Q never accepts anything, so it does not accept "hello". Hence (P, w) is in HALT exactly when Q is in ACCEPTSHELLO. A decider for ACCEPTS_HELLO would therefore decide HALT, which is impossible.
Exercise
A classmate shows that B reduces to HALT and concludes that B is undecidable. What is wrong?
Show answer
The reduction points the wrong way. B reducing to HALT only says B is no harder than HALT. Every decidable problem reduces to HALT, so this tells you nothing about B. To show B is undecidable, reduce HALT to B.