Sub topic 8
Encoding Machines and the Universal Turing Machine
Prerequisites and the main idea
This chapter assumes that you know the 7-tuple definition of a deterministic Turing machine, configurations, and basic string encodings. You should also be comfortable with proof by contradiction.
A Turing machine is a finite object: its states and transition rules can be written in a finite string. Therefore, one Turing machine can read the description of another machine as input. Such a machine is called a universal Turing machine.
If \(M\) is a Turing machine and \(w\) is an input string, write their encoded pair as \(\langle M,w\rangle\). The universal machine \(U\) performs this computation:
\[ U(\langle M,w\rangle) = M(w) \]
It accepts, rejects, or continues forever exactly as \(M\) does on \(w\).
Encoding a transition table
Suppose a machine has states \(q0,q1,\ldots\), tape symbols \(0,1,\blank\), and moves \(L,R\). Each transition can be written as
\[ (qi,a)\mapsto(qj,b,D) \]
An encoding assigns a number or binary string to every state, symbol, and move. A transition can then be represented as a sequence of numbers separated by delimiters. The complete machine encoding is the list of encoded transitions.
The encoding must be unambiguous. For example, separators must make it possible to distinguish state 12 from states 1 and 2. A standard method uses a self-delimiting code, such as blocks of 1s followed by a 0.
| Object | Example representation |
|---|---|
| State \(q_i\) | a code for integer \(i\) |
| Tape symbol \(a\) | a code for its position in the alphabet |
| Move \(R\) | a fixed code |
| Transition | codes joined in a fixed order |
The exact coding scheme is not important. What matters is that encoding and decoding are effective procedures.
How the universal machine simulates
The universal machine stores three pieces of simulated information:
- the encoded transition table,
- the simulated tape contents,
- the simulated state and head position.
It repeatedly decodes the rule matching the simulated state and scanned symbol. It applies that rule to the simulated tape and updates the simulated state and head. If no rule applies, it rejects or halts according to the chosen convention.
def simulate(delta, word, max_steps=1000):
tape = dict(enumerate(word))
state, head = "q0", 0
for _ in range(max_steps):
if state == "accept":
return "accept"
if state == "reject":
return "reject"
symbol = tape.get(head, "_")
rule = delta.get((state, symbol))
if rule is None:
return "reject"
state, new_symbol, move = rule
tape[head] = new_symbol
head += 1 if move == "R" else -1
return "unknown" # the simulation has not halted yetThe value "unknown" is essential. A simulator can report a result when the simulated machine halts, but reaching a step limit does not prove that the machine will never halt.
Why this construction matters
The universal machine makes programs into data. This allows a machine to receive, inspect, and simulate another machine. It also permits a machine to receive its own encoding, which is central to diagonalization and the proof that the halting problem is undecidable.
Exercise
What must U do on input ⟨M, w⟩ if M rejects w after 25 steps?
Show answer
U must reject after reproducing those 25 simulated steps, assuming the encoding is valid and U correctly simulates M.