Sub topic 10
The halting problem and A_TM are undecidable
This chapter assumes you can trace a Turing machine (TM) by its configurations, and that you know the difference between a recursive (decidable) language and a recursively enumerable (RE, semi-decidable) language. It also assumes you are comfortable with countable and uncountable sets and with proof by contradiction. The chapter proves that two central languages have no deciding machine, shows where the proof breaks if you change its assumptions, and lists the facts GATE tests.
The two languages
A TM can be written as a string ⟨M⟩, so a machine can be given as input to another machine. Two languages are built from this idea.
- A_TM = { ⟨M, w⟩ : M is a TM and M accepts w }
- HALT_TM = { ⟨M, w⟩ : M is a TM and M halts on w }
M "halts" on w if it reaches an accepting or a rejecting state. M "loops" if it runs forever. The difference between the two languages is the treatment of rejection. In ATM, a machine that rejects w is not in the language. In HALTTM, it is.
| Language | Decidable? | Semi-decidable (RE)? | Complement RE? |
|---|---|---|---|
| A_TM | No | Yes | No |
| HALT_TM | No | Yes | No |
Why A_TM is semi-decidable
The universal Turing machine U takes ⟨M, w⟩ and simulates M on w step by step. If the simulation accepts, U accepts. If it rejects, U rejects. If M loops, U loops too.
So U accepts exactly ATM, and ATM is RE. U does not decide A_TM, because it never answers when M loops.
The Python simulator from the design chapter shows the same limit. Its max_steps parameter returns None when the budget runs out:
def run(delta, w, start='q0', accept='qa', blank='_', max_steps=10000):
tape = dict(enumerate(w))
head = 0
state = start
for _ in range(max_steps):
if state == accept:
return True
key = (state, tape.get(head, blank))
if key not in delta:
return False
state, sym, move = delta[key]
tape[head] = sym
head += 1 if move == 'R' else -1
return None # step limit reached: no verdict
# A machine that moves right forever on any input
loop_delta = {
('q0', 'a'): ('q0', 'a', 'R'),
('q0', '_'): ('q0', '_', 'R'),
}
print(run(loop_delta, 'aa')) # None
print(run(loop_delta, '')) # NoneNone does not mean "loops". It means "no verdict yet". Raising max_steps can change the answer for a slow machine, but no finite limit works for every machine. Telling a slow machine from a looping one is the whole difficulty.
The diagonal argument
The proof uses the same idea as Cantor's proof that the reals are uncountable. It builds an object that differs from every item on a list.
Suppose a TM H decides A_TM. On input ⟨M, w⟩, H accepts if M accepts w, and H rejects if M does not accept w. H always halts.
Build a new machine D that uses H as a subroutine:
- D receives ⟨M⟩, the description of a machine.
- D runs H on ⟨M, ⟨M⟩⟩. This asks whether M accepts its own description.
- D does the opposite of H's answer. If H accepts, D rejects. If H rejects, D accepts.
Because H always halts, D always halts. Now run D on its own description ⟨D⟩:
- If D accepts ⟨D⟩, then H said D does not accept ⟨D⟩, so H was wrong.
- If D rejects ⟨D⟩, then H said D accepts ⟨D⟩, so H was wrong.
Either case contradicts the claim that H is correct. So H cannot exist, and A_TM is undecidable.
The picture below shows the table the argument diagonalizes. Row M, column ⟨N⟩ holds the result of M on ⟨N⟩. D's row flips the diagonal entries, so D differs from every machine at one position.
The same argument as a program
The program form uses halts(program, data) instead of H. It is a sketch of the proof, not runnable code, because halts cannot be implemented.
def halts(program, data):
"""Hypothetical decider: True if program(data) halts, else False."""
raise NotImplementedError # assumed to exist only for the proof
def D(program):
if halts(program, program):
while True: # loop forever if program(program) halts
pass
else:
return # halt if program(program) loops
# Consider D(D):
# if halts(D, D) is True, D(D) loops forever, so halts was wrong;
# if halts(D, D) is False, D(D) halts, so halts was wrong.
# Either way the assumed decider fails, so no such halts exists.This version targets halting, not acceptance. The contradiction has the same shape. D loops exactly when the decider says it halts, and halts exactly when the decider says it loops.
From A_TM to HALT_TM
The proof above shows ATM is undecidable. HALTTM follows by a reduction, which the next chapter covers in general. The idea is short enough to state here.
Suppose R decides HALTTM. Build a decider S for ATM:
- On input ⟨M, w⟩, run R on ⟨M, w⟩.
- If R rejects, M loops on w, so S rejects.
- If R accepts, M halts on w. Simulate M on w, which now terminates. Accept if M accepts and reject if M rejects.
S always halts and decides A_TM. That contradicts what we proved, so R cannot exist.
The complement is not RE
A language L is recursive if and only if both L and its complement are RE. You can see this by running two recognizers in parallel, one for L and one for the complement. Exactly one of them eventually accepts, so the combined machine always halts.
Apply this to A_TM:
- A_TM is RE (the universal machine recognizes it).
- A_TM is not recursive (just proved).
- So the complement of ATM cannot be RE. If it were, ATM would be recursive.
The same holds for HALT_TM. This gives a language that is not even RE, which matters for GATE questions about the three classes.
Common errors
- Reading "undecidable" as "unsolvable for every input". A_TM is undecidable, yet you can answer it for many specific pairs. What does not exist is one machine that answers correctly and halts on all inputs.
- Believing a larger step limit fixes it. Any fixed bound is wrong for some machine that needs one more step. The simulator above returns
Nonebecause no bound is enough. - Mixing up A_TM and its complement. ATM is RE. The complement of ATM is not RE, so no TM, even one allowed to loop, can recognize it.
- Stating that the halting problem is undecidable "for TMs only". By the Church-Turing thesis, any general-purpose programming language has the same limit.
- Using the diagonal argument without totality. The contradiction depends on H always halting. If H may loop, D may loop on ⟨D⟩ and nothing contradicts. This is why A_TM is RE while not being decidable.
Quick reference for GATE
- ATM and HALTTM are RE and not recursive.
- Their complements are not RE.
- A language is recursive exactly when it and its complement are both RE.
- Undecidability proofs start from ATM or HALTTM and use a reduction.
- Diagonalization shows that some languages have no machine at all, because there are countably many TMs and uncountably many languages.
Exercise
Suppose a language L is RE and its complement is also RE. What can you conclude about L, and what does that tell you about the complement of A_TM?
Show answer
L is recursive (decidable). Run the two recognizers in parallel on the input. Exactly one accepts, so the combined machine always halts with the right answer. ATM is RE but not recursive, so its complement cannot be RE. If it were, ATM and its complement would both be RE and A_TM would be recursive, which contradicts the diagonal argument.
Exercise
In the diagonal proof, the machine D runs the assumed decider H and flips its answer. Which property of H is essential, and what goes wrong if H is allowed to loop?
Show answer
H must halt on every input. D then halts on every input too. If H could loop, D could loop on ⟨D⟩ whenever H loops on ⟨D, ⟨D⟩⟩, and looping is neither accepting nor rejecting. No contradiction follows. This is why the argument rules out deciders but not recognizers, and why A_TM is RE.
Exercise
A GATE statement says: "The complement of HALT_TM is recursively enumerable." Is it true? Give the reason in one chain of facts.
Show answer
False. HALTTM is RE (simulate M on w and accept if it halts). HALTTM is not recursive, by reduction from ATM. If its complement were also RE, HALTTM would be recursive. That is a contradiction, so the complement is not RE.