Sub topic 13
Proving Undecidability by Reduction from the Halting Problem
This chapter assumes you know what a many-one reduction is and how to check both directions of its correctness. It also assumes you understand why hardness transfers backward along a reduction, and that you have seen the halting problem described as undecidable. You do not need any automata theory beyond the idea that a program is a finite string that can be run on an input.
The goal is a reliable recipe: to show a new problem L is undecidable, build a computable map from halting instances to L instances, and argue that a decider for L would yield a decider for halting.
The Starting Point: HALT
The halting problem as a decision problem is the language
HALT = { ⟨P, x⟩ : program P halts when run on input x }.
Here ⟨P, x⟩ is a string encoding of the pair. Turing proved by diagonalization that no program decides HALT. A decider is a program that always halts and answers yes or no correctly on every input.
We take that result as given. Every proof in this chapter uses it the same way. If L were decidable, the reduction would give a decider for HALT, which cannot exist.
The Proof Template
To prove L undecidable using a many-one reduction HALT ≤m L:
- Describe a function f that takes ⟨P, x⟩ and outputs an instance f(⟨P, x⟩) of L.
- Show that f is computable. A program computes it and always halts, even when P itself would run forever.
- Show the forward direction: if P halts on x, then f(⟨P, x⟩) ∈ L.
- Show the backward direction: if f(⟨P, x⟩) ∈ L, then P halts on x. Equivalently, if P loops on x, then f(⟨P, x⟩) ∉ L.
- Conclude: a decider D for L gives the decider "compute f, run D, return its answer" for HALT. That is a contradiction, so L is undecidable.
Worked Example: Halting on the Empty Input
Let HALT_ε = { ⟨Q⟩ : Q halts on the empty string }. This looks easier than HALT because there is only one input to consider. It is not.
The construction
Given ⟨P, x⟩, build a program Q that ignores its own input, runs P on the hard-coded x, and returns whatever P returns. Writing Q is a pure text operation. The reduction never runs P.
In Python, model a program as source text that defines main(inp):
def halt_to_halt_on_empty(p_source: str, x: str) -> str:
"""Map an instance (P, x) of HALT to an instance Q of HALT_on_empty.
Q ignores its input and runs P on the fixed input x.
This function only builds text, so it always terminates.
"""
return (
p_source
+ "\n\n_P_MAIN = main\n"
+ "def main(_ignored):\n"
+ f" return _P_MAIN({x!r})\n"
)
# Demonstration with a program P that halts.
p = "def main(inp):\n return len(inp)\n"
q = halt_to_halt_on_empty(p, "hello")
namespace = {}
exec(q, namespace)
assert namespace["main"]("") == 5 # Q on empty input behaves like P on "hello"
assert namespace["main"]("anything") == 5 # Q's own input is irrelevant
# A looping P still yields Q instantly; we just never call Q here.
looping = "def main(inp):\n while True:\n pass\n"
q_loop = halt_to_halt_on_empty(looping, "abc")
print(q_loop)The last part shows that building q_loop finishes immediately even though running it would never end. That property is what step 2 of the template requires.
Correctness
- Forward: if P halts on x, then Q on the empty string runs P on x, which halts. So Q ∈ HALT_ε.
- Backward: if Q halts on the empty string, the only thing Q did was run P on x, so P halts on x.
So ⟨P, x⟩ ∈ HALT if and only if f(⟨P, x⟩) ∈ HALTε. HALTε is undecidable.
Reusing the Trick: Programs That Accept Everything
The same "ignore your input" idea handles many properties of a program's whole behavior. Let TOTAL = { ⟨Q⟩ : Q halts on every input }.
Use the same f. If P halts on x, Q halts on every input, because its input is ignored. If P loops on x, Q loops on every input, so it is not total. Hence HALT ≤m TOTAL.
The construction makes Q's behavior all-or-nothing. Q either acts like a program that always halts or like one that never halts. When you need a reduction to a semantic property, look for a Q with one behavior when P halts on x and a clearly different behavior when it does not.
Using Complements: The Emptiness Problem
Let EMPTY = { ⟨Q⟩ : Q accepts no input }, where "accepts" means it halts and returns True. Modify the construction so Q runs P on x and then returns True.
- If P halts on x, Q accepts every input, so Q ∉ EMPTY.
- If P loops on x, Q accepts nothing, so Q ∈ EMPTY.
The answers are flipped. This f is a reduction from HALT to the complement of EMPTY. That is still enough. Decidable languages are closed under complement: if a decider for EMPTY existed, swapping its answers would decide the complement of EMPTY, and through f it would decide HALT.
A Turing Reduction Variant
Sometimes a many-one map is awkward and a Turing reduction is easier. A Turing reduction may query the hypothetical decider several times and post-process the answers. This is still valid for undecidability, because the argument only needs "decider for L implies decider for HALT." It does not distinguish between a decider and its complement, and it loses the recognizability information from the note above.
Rice's Theorem as a Shortcut
The constructions above share one pattern. Rice's theorem generalizes it: every nontrivial property of the language a program recognizes is undecidable. A property is nontrivial if some program has it and some program lacks it.
| Problem | Semantic and nontrivial? | Rice applies? |
|---|---|---|
| Q accepts no input (EMPTY) | Yes | Yes, undecidable |
| Q accepts exactly the even-length strings | Yes | Yes, undecidable |
| Q's source has more than 100 lines | No, it is syntactic | No, decidable by counting |
| Q halts within 1000 steps on the empty input | No, it depends on running time | No, decidable by simulation |
| Q and R accept the same language | Involves two programs | Not directly; reduce from EMPTY |
Rice's theorem covers only properties of the recognized language. Questions about source text, step counts, or the internal states a program visits need their own argument.
Common Errors
- Running P inside the reduction. If f simulates P to decide what to output, f may never halt and is not computable. Always embed P in Q's text instead.
- Proving only one direction. Showing "P halts implies Q ∈ L" without the converse lets a trivial f that always outputs a yes instance pass, and that map proves nothing.
- Reducing the wrong way. L ≤m HALT shows L is decidable relative to halting, which is an upper bound and not a hardness result.
- Bounded versions. "Halts within n steps" is decidable. The bound removes the source of undecidability.
- Forgetting that Q's behavior must depend only on whether P halts on x. If Q's acceptance also depends on what P returns, the backward direction usually breaks.
Exercise
Prove that EQ = { ⟨Q, R⟩ : Q and R accept the same language } is undecidable by reducing from EMPTY.
Show answer
Fix a program R0 that rejects every input. Map ⟨Q⟩ to ⟨Q, R0⟩, which is computable and always halts. Q accepts no input if and only if Q and R0 accept the same (empty) language. So EMPTY ≤m EQ. EMPTY is undecidable, so EQ is undecidable.
Exercise
A classmate argues: "Let ZERO = { ⟨Q⟩ : Q returns 0 on input 0 }. I can reduce ZERO to HALT by running Q on 0 and checking the output, so ZERO is undecidable." What is wrong, and how would you fix the proof?
Show answer
The direction is backwards, because ZERO ≤ HALT gives only an upper bound. The proposed procedure also runs Q, so it may not halt. Fix: reduce HALT to ZERO. Given ⟨P, x⟩, build Q that ignores its input, runs P on x, then returns 0. Q returns 0 on input 0 if and only if P halts on x.
Exercise
Is "Q ever writes to the variable y when run on the empty input" decidable? Justify with a reduction or an algorithm.
Show answer
It is undecidable. Given ⟨P, x⟩, rename every variable in P so none is called y, then build Q that runs the renamed P on x and afterwards executes y = 1. Q writes to y if and only if P halts on x. Rice's theorem does not apply because this is a property of behavior and not of the recognized language, which is why the direct reduction is needed.