Sub topic 9
Why some languages have no machine: counting and diagonalization
Prerequisites and goal
This chapter assumes you know what a Turing machine (TM) is and what recursive and recursively enumerable (RE) languages are. It also assumes basic set theory: countable and uncountable sets, and proof by contradiction. The goal is to show that some languages have no Turing machine at all, and to learn the diagonal argument that the halting problem proof reuses.
The counting argument
There are two sets to compare. One is the set of all Turing machines. The other is the set of all languages over an alphabet.
Turing machines are countable
Every TM has a finite description: finitely many states, a finite tape alphabet and a finite transition table. Write that description as a string over a fixed finite alphabet, such as {0, 1}. The set of all finite strings over a finite alphabet is countable. You can list them by length, and within one length in lexicographic order:
ε, 0, 1, 00, 01, 10, 11, 000, ...
Every string appears at a finite position. Some strings are not valid machine descriptions, so skip them. The remaining strings give a list M1, M2, M3, ... that contains every Turing machine. So the set of TMs is countably infinite.
Each TM accepts exactly one language. So the set of RE languages is also countable.
Languages are uncountable
Fix the alphabet Σ = {0, 1}. The set Σ of all strings is countably infinite, so list it as w1, w2, w3, ... A language is any subset of Σ. Encode a subset L as an infinite bit sequence. Bit i is 1 if wi is in L, and 0 otherwise.
So the set of all languages corresponds to the set of all infinite binary sequences. That set is uncountable, which Cantor's diagonal argument shows.
Conclusion
A countable set of machines cannot cover an uncountable set of languages. Almost all languages are not RE. Only countably many languages have a machine, and uncountably many do not.
This argument is non-constructive. It proves that unrecognizable languages exist, but it names none of them. To get a specific one, you need diagonalization applied to the machine list itself.
Cantor's diagonal argument
Suppose the infinite binary sequences could be listed as s1, s2, s3, ... Write them as rows of an infinite table, where entry (i, j) is the j-th bit of si.
Build a new sequence d. Its j-th bit is the opposite of the j-th bit of sj. This is the flip of the diagonal.
Then d differs from s1 in position 1, from s2 in position 2, and from every sk in position k. So d is not in the list. This contradicts the assumption that the list was complete, so no such list exists.
Diagonalization on machines: a language that is not RE
Use the list of machines M1, M2, M3, ... and the list of strings w1, w2, w3, ... For each pair (i, j), record whether Mi accepts wj. The table below shows an example. A means accept. A dash means reject or loop.
| Row | w1 | w2 | w3 | w4 |
|---|---|---|---|---|
| M1 | A | - | A | - |
| M2 | - | - | A | A |
| M3 | A | A | A | - |
| M4 | - | A | - | - |
Define the diagonal language:
L_d = { wi : Mi does not accept wi }
In the example table, w1 is not in Ld, because M1 accepts w1. The string w2 is in Ld, because M2 does not accept w2. The strings w3 and w4 follow the same rule: w3 is not in Ld, and w4 is in Ld (M4 does not accept w4).
Claim: L_d is not RE.
Proof. Suppose some TM Mk accepts exactly Ld. Ask whether wk is in Ld.
- If wk is in Ld, then by the definition of Ld, Mk does not accept wk. But Mk accepts exactly L_d, so it accepts wk. Contradiction.
- If wk is not in Ld, then Mk accepts wk by the definition of Ld. But Mk accepts only strings of L_d. Contradiction.
Both cases fail, so no Mk exists. L_d is a concrete language that is not RE.
Why the table argument works
The language Ld disagrees with every machine on at least one string. Machine Mi disagrees with Ld on wi. No machine can match L_d everywhere, because each machine is wrong on its own diagonal string.
The complement of L_d
The complement of L_d is:
L_d' = { wi : Mi accepts wi }
This language is RE. A machine can find the index i for a given string w, build Mi, and simulate Mi on w. It accepts if the simulation accepts. This relies on the universal TM, covered in the previous chapter.
The language Ld' is not recursive. If it were, its complement Ld would be recursive too, and so RE. But Ld is not RE. So Ld' is RE but not recursive.
This gives a concrete example that the class of recursive languages is a proper subset of the RE languages. The pair (Ld', Ld) also shows that a language can be RE while its complement is not.
Connection to the halting problem
The diagonal argument is the template for the halting problem proof. The program D in the next chapter's code sketch does the opposite of what the assumed decider predicts on D itself. This is the same flip. The table's rows are programs, its columns are inputs, and D flips the diagonal entry.
The difference is the starting point. Here we showed the existence of a non-RE language by counting and by direct diagonalization. The halting proof assumes a decider exists and derives a contradiction from it.
Common errors
- Saying "there are more languages than machines, so every unrecognized language is undecidable in a useful way." Counting shows only that non-RE languages exist. It does not tell you whether a particular language is RE.
- Confusing countable with finite. The set of TMs is infinite but countable. The set of languages is uncountable. Both are infinite, but they have different sizes.
- Claiming the set of recursive languages is uncountable. It is countable, because every recursive language is accepted by a TM, and there are countably many TMs.
- Treating the diagonal language as the halting problem. Ld is about machine Mi on its own index string wi. It is not the same set as ATM or HALT, although the proofs are related.
- Forgetting that the TM list must contain only valid encodings. The enumeration skips invalid strings. Without this step, the table rows would not all be machines.
- Assuming a machine that loops on wi is "accepting" it. In Ld, "does not accept" includes both rejecting and looping. That is why Ld is not RE, and why the complement L_d' is RE but not recursive.
A short code illustration
The code below shows the diagonal flip on a finite table. A finite table cannot prove the theorem, but it shows the construction.
table = [
[1, 0, 1, 0],
[0, 0, 1, 1],
[1, 1, 1, 0],
[0, 1, 0, 0],
]
d = [1 - table[i][i] for i in range(len(table))]
print("diagonal flipped:", d)
for i, row in enumerate(table):
assert row[i] != d[i]
print("d differs from row", i, "at position", i)The output is diagonal flipped: [0, 1, 0, 1], followed by four lines confirming that d differs from each row on its diagonal position. In the infinite case, the same flip produces a sequence that is missing from any claimed complete list.
Summary of the key facts
| Fact | Status |
|---|---|
| Set of all Turing machines | Countably infinite |
| Set of RE languages | Countably infinite |
| Set of all languages over Σ | Uncountable |
| L_d (diagonal language) | Not RE |
| Complement of L_d | RE, not recursive |
| Non-RE languages | Exist, uncountably many |
Exercise
The set of all Turing machines is countable and the set of all languages over {0, 1} is uncountable. Explain in two or three sentences why this proves that some language is not RE.
Show answer
Each TM accepts exactly one language, so the number of RE languages is at most the number of TMs, which is countable. The set of all languages is uncountable, so it is strictly larger. Therefore some language has no TM that accepts it, and that language is not RE.
Exercise
In the diagonal language Ld = { wi : Mi does not accept wi }, a machine Mk is assumed to accept exactly Ld. What contradiction arises when you ask whether wk is in L_d?
Show answer
If wk is in Ld, then Mk does not accept wk by the definition of Ld, yet Mk accepts exactly Ld and so accepts wk. If wk is not in Ld, then Mk accepts wk by the definition of Ld, yet Mk accepts only strings in Ld. Both cases contradict, so no such Mk exists.
Exercise
Is the complement of L_d recursive, RE but not recursive, or not RE? Give the reason.
Show answer
It is RE but not recursive. A machine can compute the index i of the input wi, simulate Mi on wi, and accept if Mi accepts, so the complement is RE. If it were recursive, then L_d would be recursive as well, and so RE, which contradicts the earlier result.