A levels Computer Science · Theory of computation
Turing machines
1The model
A Turing machine, described by Alan Turing in 1936, is a simple model of a computer:
- an infinite tape divided into cells, each holding one symbol (or a blank, □);
- a read/write head that looks at one cell at a time;
- a finite set of states, including a start state and one or more halting states;
- a transition function saying, for each state and symbol read, what to write, which way to move, and which state to go to next.
Unlike an FSM, a Turing machine has unlimited memory: the tape.
Check yourself
What gives a Turing machine more power than a finite state machine?
Show the answer
Its infinite tape, which it can read and write
Unlimited memory.
2Transition rules
A rule is written
$$\delta(\text{current state}, \text{symbol read}) = (\text{new state}, \text{symbol to write}, \text{move})$$
where the move is L (left) or R (right).
For example, δ(S0, 1) = (S0, 0, L) means: "in state S0, if you read 1, write 0, move left and stay in S0".
The rules can also be shown as a state transition diagram, with each arrow labelled read | write, move.
Check yourself
What does δ(S1, 0) = (S2, 1, R) mean?
Show the answer
In S1 reading 0: write 1, move right, go to S2
State, write, move.
3Worked example: adding 1 in binary
This machine adds 1 to a binary number. The head starts on the rightmost digit, in state S0.
| State | Read | Write | Move | Next state |
|---|---|---|---|---|
| S0 | 1 | 0 | L | S0 |
| S0 | 0 | 1 | L | Halt |
| S0 | □ | 1 | L | Halt |
The binary-increment Turing machine turning 1011 into 1100. The tape continues with blanks (□) in both directions.
1011 + 1
- Read 1 (rightmost): write 0, move left. Tape 1010.
- Read 1: write 0, move left. Tape 1000.
- Read 0: write 1, halt. Tape 1100.
- Check: 11 + 1 = 12 = 1100 in binary.
Check yourself
What does the machine produce from 111?
Show the answer
1000
The blank becomes 1.
4What can be computed
The Church–Turing thesis says that anything that can be computed by an algorithm can be computed by a Turing machine. Every real computer and programming language can do exactly the same things as a Turing machine (given enough memory and time).
So the Turing machine gives a precise meaning to "computable": a problem is computable if some Turing machine solves it and always halts.
Check yourself
What does the Church–Turing thesis state?
Show the answer
Anything computable by an algorithm can be computed by a Turing machine
Defines computable.
5The universal Turing machine
A universal Turing machine (UTM) can simulate any other Turing machine. The description of that machine (its transition rules) and its input are both written on the tape.
This is the key idea behind the stored-program computer: programs are just data held in memory, so one machine can run any program. Modern computers follow this von Neumann idea.
Check yourself
What is the significance of the universal Turing machine?
Show the answer
It shows one machine can run any program stored as data, the basis of stored-program computers
Programs as data.
6The halting problem
The halting problem asks: is there an algorithm that, given any program and its input, decides whether the program will eventually halt?
Turing proved there is not. The idea of the proof:
- Suppose a program H(P, I) always correctly says whether P halts on input I.
- Build a program Z(P) that runs H(P, P) and does the opposite: if H says "halts", Z loops forever; if H says "loops", Z halts.
- Ask what happens with Z(Z). If Z halts, H said it loops, so Z halts — contradiction. If Z loops, H said it halts, so Z loops — contradiction.
- So H cannot exist.
This shows there are problems no computer can ever solve, however powerful.
Check yourself
What does the halting problem show?
Show the answer
Some problems cannot be solved by any algorithm
Non-computable.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera