Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

How a Turing machine works

After this lesson: I can trace a Turing machine on a tape.

60 min lesson3 commandsCompilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

Tape, head and a table of rules

A Turing machine is a very simple computer. It has an endless tape of cells, a head that reads and writes one cell at a time, and a table of rules. Alan Turing described it in 1936.

It matters because anything a real computer can compute, a Turing machine can compute too. So it lets us ask what computing can and cannot do.

By the end of this lesson

Note

I can trace a Turing machine on a tape.

Key words

WordWhat it meansExample
Tapea row of cells, each holding one symbol
Headthe part that reads and writes one cell at a time
Blankthe symbol in a cell nobody has written_
Rulein this state, on this symbol: write, move, change state
Haltto stop because the machine has finished or no rule fits
Turing machinea tape, a head, states and rules together

The cells students fill in are left blank in this preview.

A Turing machine writes as well as reads

A finite automaton only reads. A Turing machine also writes to its tape, and it can move its head left or right.

Each rule looks at two things: the current state and the symbol under the head. It then says what to write, which way to move, and which state comes next. The machine halts when it enters the state halt or no rule fits.

Worked example: rules that flip every bit

StateReadsWritesMovesNext state
go01Rightgo
go10Rightgo
go__Stayshalt

The cells students fill in are left blank in this preview.

Tracing the tape 101 step by step

StepTape, head in bracketsRule used
0[1]01Start in go
10[0]1Read 1, write 0, move right
201[1]Read 0, write 1, move right
3010[_]Read 1, write 0, move right
4010[_]Read blank, stay, halt

The cells students fill in are left blank in this preview.

The bit-flipper by hand

Code: python
# The bit-flipper by hand: read a cell, write the opposite, move right.
tape = list("10110")
head = 0
while head < len(tape):
    print("head at", head, "reads", tape[head])
    if tape[head] == "0":
        tape[head] = "1"
    else:
        tape[head] = "0"
    head = head + 1
print("".join(tape))
# Change the tape to "0011" and run again.

The shell flips bits the same way

Command
echo 10110 | tr 01 10

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Flip another tape

Command
echo 1011 | tr 01 10

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A second machine adds one in binary

This machine walks right to the end of the number, steps back, then carries leftwards. A 1 becomes 0 and the carry moves on. The first 0 it meets becomes 1, and the machine halts.

Leave one spare 0 on the left for a number of all ones. On the tape 011 it takes seven steps and leaves 100.

Rules for adding one in binary

StateReadsWritesMovesNext state
right0 or 1The sameRightright
right__Leftcarry
carry10Leftcarry
carry01Stayshalt

The cells students fill in are left blank in this preview.

Check the answer for 0111

Command
python3 -c "print(bin(int('0111', 2) + 1))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a Turing machine is weaker than a real computer because it is so simple. It can compute everything a real computer can. It is only slower. That is why we study it: if a problem is impossible for this machine, no ordinary computer can solve it either.

Why one simple machine matters

A finite automaton is a Turing machine that never writes and only moves right. Letting the head write and move both ways is what gives the machine unlimited memory.

A machine can also be built that reads another machine's rules from its tape and runs them. That is a universal Turing machine, and it is the idea of a stored program.

About this lab and its source: Compilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.