Module 7 · 15 min read · Free chapter
Turing machines and undecidability
A Turing machine reads and writes an infinite tape and can carry out any algorithm. A language is recursive if some machine always halts with the answer, and RE if some machine accepts it. The halting problem is RE but undecidable, and Rice's theorem makes every non-trivial property of a machine's language undecidable.
Start from zero: a clerk with an endless register
Picture a clerk at a railway reservation counter with a very long paper register, a pencil and an eraser. He can look at only one box of the register at a time, but he can move his finger one box left or right, rub out what is written and write something new. He also carries a small card of rules, such as "if you are on rule 3 and see an , write , move right and switch to rule 4".
That clerk is a Turing machine. A finite automaton could only read once, from left to right, and a pushdown automaton could only use the top of a stack. The clerk can go back, reread and overwrite as often as he likes, and his register never runs out.
It turns out that this simple clerk can carry out every algorithm that any computer can. But it also turns out that there are perfectly clear yes-or-no questions that no clerk, and no computer, can ever answer correctly for every input. Such questions are called undecidable, and this chapter proves that they exist.
Why it matters. Undecidability is why no program can check, in general, whether another program will hang. GATE asks about it every year in the form "which of these problems is decidable?", and the answers follow from a few clean theorems.
The Turing machine and a worked example
A Turing machine (TM) is a 7-tuple . Here is the tape alphabet, which contains the input alphabet and the blank , and the tape is infinite. The transition function reads the symbol under the head, writes a symbol, moves one cell left or right, and changes state.
So means: in state reading , write , move right and enter . The machine halts when no move is defined, and it accepts if it enters a final state. On some inputs a TM may never halt at all, and this possibility is the source of everything interesting in this chapter.
Let us build a TM for . The plan is to cross off one and one matching in each round, writing over the and over the :
- In , an is replaced by and the machine moves right into ; a means all 's are done, so it moves right into .
- In , the machine skips 's and 's moving right, and the first is replaced by as it turns left into .
- In , the machine skips 's and 's moving left, and on reaching an it steps right into .
- In , the machine skips 's moving right, and a blank sends it into the accepting state .
On the tape goes , and then walks over to the blank and accepts. On the machine finds no for the second and halts in without accepting. Each round walks across the input, so the machine takes moves.
Why it matters. This marking trick is how a TM does what a PDA cannot, such as checking with a third mark . Writing on the tape gives it unlimited, rewritable memory.
Variants are equivalent, and the Church–Turing thesis
People have tried to make Turing machines stronger in many ways: several tapes, several tracks on one tape, a tape infinite in both directions, an option to stay in place, or nondeterministic choices. Every one of these accepts exactly the same languages as the basic machine. They may be faster or more convenient, but they never decide anything new.
The costs of simulation differ, and GATE sometimes asks about them. A single-tape machine simulates steps of a multi-tape machine in steps, by keeping all tapes side by side as tracks. A deterministic machine simulates a nondeterministic one by trying all branches in breadth-first order, which can take exponential time.
Even weak-looking machines reach the same power once they have enough memory. A finite automaton with two stacks can simulate a tape, using one stack for the cells to the left of the head and one for the cells to the right. So a two-stack PDA is as powerful as a Turing machine, while a one-stack PDA is not.
Because so many different models agree, we accept the Church–Turing thesis: every function that can be computed by an algorithm, in the intuitive sense, can be computed by a Turing machine. It is a thesis, not a theorem, because "algorithm" in the everyday sense has no formal definition to prove it against. Every real programming language is equivalent to a TM, given unlimited memory.
Why it matters. The thesis lets us say "no algorithm exists" when we prove "no Turing machine exists". That is what makes undecidability results statements about all computers, not about one model.
Recursive and recursively enumerable languages
Because a TM may loop, there are two levels of "solving" a language. A language is recursive, or decidable, if some TM halts on every input and accepts exactly the strings in . Such a machine is called a decider, and it always answers yes or no.
A language is recursively enumerable (RE) if some TM accepts exactly the strings in . On a string in the machine eventually says yes, but on a string outside it may say no or may run forever. Every recursive language is RE, but not the other way round.
The key link between them is the complement theorem: is recursive if and only if both and are RE. Given recognisers for both, run them side by side, one step each in turn; exactly one of them must eventually accept, and that tells you the answer. It follows that if is RE but not recursive, then is not even RE.
| Operation | Recursive | RE |
|---|---|---|
| Union and intersection | Yes | Yes |
| Complement | Yes | No |
| Concatenation and star | Yes | Yes |
| Difference | Yes | No |
For union of RE languages, the two recognisers must again be run in parallel. If you run the first one to completion, it may loop forever on a string that the second one would have accepted.
Why it matters. Recursive means "has an algorithm that always answers", and RE means "has a search that confirms yes-answers". Many GATE options turn on this difference, especially around complements.
The halting problem, proved by diagonalisation
Every TM can be written as a finite string, called its code , so a TM can receive another TM as input. The halting problem asks: given and an input , does halt on ? We now prove that no TM can decide it.
Suppose, for contradiction, that a decider exists, so that always halts and says "yes" if halts on and "no" otherwise. Build a new machine that takes a code , runs , and then does the opposite. If says "yes", loops forever, and if says "no", halts.
Now feed its own code. By the way was built:
This is impossible, so cannot exist, and the halting problem is undecidable. The argument is called diagonalisation because is built to differ from every machine on the diagonal entry, the input , so cannot be any row of the table.
The same idea settles the acceptance problem . It is RE, because a universal TM can simulate on and accept if does. It is not recursive, by the argument above, so by the complement theorem is not even RE.
There is also a counting reason why hard languages must exist. Every TM is a finite string, so there are only countably many TMs. But the languages over are the subsets of , and there are uncountably many of them, so most languages have no TM at all.
Why it matters. This proof is the root of every undecidability result you will meet. Descriptive papers ask for it in full, and GATE asks for its consequences, such as which languages are RE but not recursive.
Reductions and Rice's theorem
Once one problem is known to be undecidable, we prove others undecidable by reduction. Problem reduces to problem , written , if a computable function turns every instance of into an instance of with . Then a decider for would give a decider for .
So the rules are: if and is decidable, is decidable, and if and is undecidable, is undecidable. The same holds for RE: if is RE, so is . The direction matters, because hardness flows from to .
Example. Is "does halt on the blank tape?" decidable? Reduce the halting problem to it. Given , build a machine that erases its own input, writes and then runs . Then halts on the blank tape exactly when halts on , so a decider for the blank-tape problem would decide halting, which is impossible.
Doing such a reduction for every question is tiring, and Rice's theorem does them all at once. It says that every non-trivial property of the language of a TM is undecidable. A property is non-trivial if some RE languages have it and some do not.
So all of these are undecidable: is empty, is it finite, is it regular, is it context-free, does it contain , and is . But Rice's theorem says nothing about properties of the machine rather than its language. "Does have more than 5 states?" is decidable by reading the code, and "does move left within its first 10 steps on input ?" is decidable by simulating 10 steps.
Why it matters. Rice's theorem answers most "is this decidable?" questions in seconds. The only skill is to check whether the question is about the language or about the machine's own structure or a bounded number of steps.
The Chomsky hierarchy and the decidability table
All the machines of this module fit into one ladder, the Chomsky hierarchy. Each level has a kind of grammar and a kind of machine, and each level is strictly larger than the one below it. A linear bounded automaton (LBA) is a TM that may use only the tape cells holding its input.
| Type | Grammar | Production form | Machine |
|---|---|---|---|
| Type 0 | Unrestricted (RE) | Turing machine | |
| Type 1 | Context-sensitive | , | Linear bounded automaton |
| Type 2 | Context-free | Pushdown automaton | |
| Type 3 | Regular | or | Finite automaton |
Putting the deterministic and decidable classes in their places, the full chain of strict containments is:
The table below collects the decidability results of this whole module. Read a row to see how far up the ladder a question stays decidable.
| Problem | Regular | CFL | CSL | Recursive | RE |
|---|---|---|---|---|---|
| Membership | D | D | D | D | U |
| Emptiness | D | D | U | U | U |
| Finiteness | D | D | U | U | U |
| Equivalence | D | U | U | U | U |
| Universality, is | D | U | U | U | U |
| Is the intersection of two such languages empty | D | U | U | U | U |
| Closed under complement | Yes | No | Yes | Yes | No |
Here D means decidable and U means undecidable. A useful tool behind several U entries is the Post Correspondence Problem (PCP): given pairs of strings, can you pick a sequence of pairs whose top halves and bottom halves spell the same string? PCP is undecidable, and reducing it to grammars shows that CFG ambiguity and CFL intersection-emptiness are undecidable.
Why it matters. A GATE question on decidability almost always lands on one cell of this table. Remember the pattern that regular languages are decidable in every row and RE languages in almost none. Then you can answer without any derivation.
Descriptive lens: how written papers ask this
Written papers ask about Turing machines and undecidability in four ways:
- Design a Turing machine for a language, such as , and trace it on a short input.
- Define and contrast recursive and recursively enumerable languages, with their closure properties.
- Prove that the halting problem is undecidable, or use a reduction to prove another problem undecidable.
- Explain the Church–Turing thesis, Rice's theorem or the Chomsky hierarchy, with examples.
Practice questions (our own, not previous-year questions).
- Design a Turing machine that accepts , and trace its run on . (10 marks)
- Prove that a language is recursive if and only if both and are recursively enumerable. (10 marks)
- Prove that the halting problem for Turing machines is undecidable. (15 marks)
Answer plan for question 3.
- State the halting problem precisely: given the code and an input , decide whether halts on .
- Explain that every TM has a finite code, so a machine can take another machine, or itself, as input.
- Assume for contradiction that a decider exists that always halts with the correct answer.
- Construct , which runs and loops if the answer is yes but halts if the answer is no.
- Run on and show that it halts exactly when it does not halt, which is a contradiction.
- Conclude that cannot exist, and add that the halting problem is RE but not recursive, so its complement is not RE.
Quick revision
- A TM is with and an infinite tape.
- Multi-tape, nondeterministic and two-way tape machines, and two-stack PDAs, are all equivalent to the basic TM.
- A single tape simulates multi-tape steps in steps.
- The Church–Turing thesis says every algorithm is a TM, and it is a thesis, not a theorem.
- Recursive means a TM halts on every input, and RE means a TM accepts exactly the language.
- is recursive exactly when and are both RE.
- The halting problem and are RE but not recursive, and their complements are not RE.
- If and is undecidable, then is undecidable.
- Rice's theorem makes every non-trivial property of undecidable.
- The hierarchy is regular ⊂ DCFL ⊂ CFL ⊂ CSL ⊂ recursive ⊂ RE, with FA, PDA, LBA and TM as machines.
- Membership is decidable up to recursive, emptiness only up to CFL, and equivalence only for regular languages.