Sub topic 5
Decidable versus semi-decidable languages
This chapter assumes you can trace a deterministic Turing machine (TM) through configurations, as covered in the earlier chapters. It also assumes you know that a machine on an input can accept, reject, or loop. Here we turn those three outcomes into two language classes. Almost every GATE question on undecidability depends on the difference between them.
The three outcomes and two promises
Run a TM M on a string w. Exactly one of three things happens:
Mhalts in the accept state.Mhalts in the reject state, which includes having no transition for the current state and symbol.Mnever halts.
A language class is defined by what we promise about those outcomes.
| Row | Decider (total TM) | Recognizer (semi-decider) |
|---|---|---|
| If `w` is in L | always accepts | accepts |
| If `w` is not in L | always rejects | rejects or loops |
| Halts on every input? | yes | no guarantee |
| Language class | recursive, also called decidable or Turing-decidable | recursively enumerable (RE), also called semi-decidable or Turing-recognizable |
L(M) is the set of strings that M accepts. A language is recursive if some TM that halts on every input has L(M) = L. A language is recursively enumerable if some TM, possibly one that loops, has L(M) = L.
A "problem" is a language in disguise. "Does this DFA accept ε?" becomes the set of encodings ⟨D⟩ of DFAs that do. Deciding the problem means deciding that set.
Why a recognizer is weaker
Suppose M recognizes L, and you run it on w. If it accepts, you know w is in L. If it has not halted after a million steps, you know nothing. It might accept at step a million and one, or it might never halt. You cannot tell which.
So a recognizer gives a reliable "yes" and an unreliable "no". Think of searching for a proof, a witness, or a halting computation. If one exists, the search eventually finds it. If none exists, the search runs forever.
The code below shows both kinds of procedure. The decider always returns. The semi-decider simulates the Collatz process (halve even numbers, map odd n to 3n+1) and accepts if the value reaches 1. Nobody knows whether every positive integer reaches 1. This procedure is a recognizer for the set of starting values that do. When the budget runs out, it returns None, meaning "no verdict".
def decide_anbn(w):
"""Decider for { a^n b^n : n >= 0 }. Always halts."""
n = len(w) // 2
return len(w) % 2 == 0 and w == 'a' * n + 'b' * n
def semi_decide_collatz(n, budget):
"""Accepts if the Collatz sequence from n reaches 1 within budget steps.
Returns None when the budget is spent: this is not a rejection."""
for _ in range(budget):
if n == 1:
return True
n = n // 2 if n % 2 == 0 else 3 * n + 1
return None
print(decide_anbn('aabb'), decide_anbn('aab')) # True False
print(semi_decide_collatz(6, 100)) # True
print(semi_decide_collatz(27, 100)) # None: needs 111 steps
print(semi_decide_collatz(27, 200)) # TrueThe None for 27 with budget 100 is the whole point. A larger budget changed the answer, so the first None told us nothing. A real recognizer has no budget. It runs until it accepts, and that may be never.
The hierarchy
The classes nest:
regular ⊂ context-free ⊂ decidable ⊂ RE ⊂ all languages
- Every decidable language is RE, because a decider is a recognizer that happens to halt everywhere.
- Some RE languages are not decidable.
A_TM(does machineMacceptw?) is the standard example. A later chapter proves it. - Some languages are not even RE. A counting argument shows there are more languages than TMs, and a later chapter makes this precise.
- Every context-free language is decidable. A CYK parser or a PDA simulation always halts.
Decidable if and only if both sides are RE
The most useful theorem in this area is:
L is decidable exactly when both L and its complement L̄ are RE.
Forward direction: if D decides L, then D recognizes L. Swapping the accept and reject states of D gives a machine that recognizes L̄.
Reverse direction: let M₁ recognize L and M₂ recognize L̄. Build M that, on input w, alternates one step of M₁ and one step of M₂. Every string is in exactly one of L and L̄, so exactly one of the two machines eventually accepts. If M₁ accepts, M accepts. If M₂ accepts, M rejects. This halts on every input.
Interleaving is essential. Running M₁ to completion first could loop forever on a string that only M₂ accepts.
Two consequences matter for GATE:
- If
Lis RE but not decidable, thenL̄is not RE. OtherwiseLwould be decidable. A_TMis RE but not decidable, so its complement is not RE.
The next chapter develops closure properties systematically. This theorem is the basis for the complement rules.
Enumerators
There is an equivalent view of RE. A language is RE exactly when some machine can print all its members, in any order and possibly with repeats. Given an enumerator, you recognize w by running it and accepting if w ever appears. Given a recognizer, you enumerate by dovetailing: for k = 1, 2, 3, ..., run the recognizer for k steps on each of the first k strings, and print any that have accepted.
A language is decidable exactly when it can be enumerated in length-then-lexicographic order. In that order, once the enumerator has printed a string longer than w without printing w, you know w is out.
Common errors
- Treating "halts on some inputs" as "decider". A decider halts on all inputs. A machine that halts on
whas said nothing aboutw'. - Assuming a language is decidable because it has a decider for some other form. Deciding
Land decidingL̄are the same problem for deciders (swap the answers). They are not the same for recognizers. - Saying "semi-decidable means undecidable". Every decidable language is also semi-decidable. A problem is "semi-decidable but not decidable" only when you can show both parts.
- Concluding from a failed simulation. Running a machine for a long time and seeing no accept does not put the string outside the language.
- Mixing up classes of languages with classes of machines. "Recursive" describes a language. A machine is a decider, a recognizer, or neither.
- Assuming nondeterminism helps. A nondeterministic TM recognizes exactly the RE languages. It decides exactly the recursive languages when every branch halts on every input. The model does not change the classes.
Quick decision procedure
To place a language in this hierarchy:
- Try to write an algorithm that always halts. If you succeed, it is decidable.
- Otherwise, try to describe a search for a witness that stops when it finds one. If you succeed, it is RE.
- Ask whether the complement also has such a witness search. If both do, the language is decidable.
- If the language is RE and known to be undecidable, its complement is not RE.
Exercise
A language L is RE, and its complement L̄ is also RE. What can you conclude about L, and why?
Show answer
L is decidable. Build a machine that alternates single steps of a recognizer for L and a recognizer for L̄. Every string is in exactly one of the two languages, so exactly one recognizer eventually accepts. Accept if the recognizer for L accepts, and reject if the recognizer for L̄ accepts. The combined machine halts on every input.
Exercise
A_TM is RE but not decidable. Is its complement RE? Justify your answer.
Show answer
No. If the complement were RE, then ATM and its complement would both be RE, so ATM would be decidable by the interleaving construction. That contradicts the fact that A_TM is undecidable.
Exercise
True or false: if a TM M halts on every input in a finite set S and loops on every other input, then L(M) is decidable.
Show answer
False as a conclusion drawn from M. The machine M is not a decider, since it loops outside S. However, L(M) is a subset of the finite set S, and every finite language is decidable by a machine that hard-codes its members. The statement is true about the language but wrong as a claim about M.