Turing completeness
"It can compute anything", as long as you never run out of paper. What the claim means.
●●○ moderate · where to see it
The idea
A language is Turing complete (or universal) if it can compute everything a Turing machine can. Alan Turing described his machine in 1936. The Church–Turing thesis says that this is the same as "everything that can be computed by any mechanical procedure". It is a thesis, not a theorem, but no counterexample has ever been found.
Read the story
"Is it TC?" is the first question asked of every esolang. Many of the strangest languages (FRACTRAN, Rule 110, the Game of Life) are famous because the answer is a surprising yes.
But the word hides a catch. No real computer is Turing complete.
How it works
To be universal, a language needs two things.
What you need
To be universal, a language needs two things.
- Unbounded memory. Not huge, but unbounded. There is always room for one more cell.
- A way to choose and repeat, a loop or jump that depends on the data.
The usual proof is a simulation, showing that your language can imitate a language already known to be universal. Brainfuck simulates P′′; FRACTRAN simulates counter machines; Rule 110 simulates cyclic tag systems.
Universal versus bounded
- Universal. The spec gives unbounded memory. Brainfuck as an idea, with an endless tape.
- Bounded. The same machine with a fixed amount of memory. Classic Brainfuck has 30,000 byte cells. That is a finite machine; it has 256^30000 possible tapes. Huge, but finite.
A finite machine is a finite-state machine. It cannot do some simple things a universal one can. One example is checking that a string of brackets is balanced, for any depth. With N states you cannot count deeper than about N, so a string of N+1 ( followed by N+1 ) will fool it.
So every real computer is bounded. We still call Python "Turing complete" because the language puts no limit on memory; only the machine does. A language like that is often marked bounded, meaning it would be universal given unbounded memory.
The price: the halting problem
Universality has a cost. Turing proved that no program can decide, for every program, whether it will ever stop. So a universal language always allows programs that run forever, and you cannot always tell which ones in advance.
A language can buy guaranteed halting by giving up universality. Many deliberately do.
Not Turing complete, on purpose
What it means
A computer can run any program you can write for it, if it has enough memory.
A computer can run any program you can write for it, if it has enough memory. A language is Turing complete if it can do the same, that is, simulate any other computer given unlimited memory and time.
Two things are roughly enough. Memory that can grow without limit, and a way to repeat something depending on what is in memory. brainfuck has both, an endless tape and a loop that tests a cell.
Tiny but complete
An esolang with very few commands that is still Turing complete is called a Turing tarpit.
An esolang with very few commands that is still Turing complete is called a Turing tarpit. Everything is possible and nothing is easy. brainfuck has 8 commands. Bitwise Cyclic Tag has 2.
Bitwise Cyclic Tag: two commands
A program of bits runs in a cycle over a string of data bits. 0 deletes the first data bit; 1x appends x if the first data bit is 1. It stops when the data is empty. The output is every bit deleted.
Expected output
10
Output
Not complete
Deadfish has one number and no loop.
Deadfish cannot loop
Deadfish has one number and no loop. Every program runs once from left to right. It cannot be Turing complete, because no program can run longer than its own length.
Expected output
4
Output
Smallfuck has a fixed tape of bits (16 in this implementation), so it is a finite machine. Brainfuck with a tape that cannot grow is also finite.
Every run stops
The kernel goes the other way on purpose. It gives every run a budget of 100,000 steps, 1,024 bytes of output, a stack of 256 and calls 32 deep. So every text is a valid program, and every run stops.
That makes the kernel not Turing complete in practice. Evolution tries millions of random programs, and none of them may hang.
A loop that never ends, stopped
The cell is 1 and the loop never changes it. The kernel stops it when its 100,000 steps are spent.
Expected output
(nothing)
Output
Try this
- Why is Brainfuck with an unbounded tape but cells that hold only 0 or 1 still universal? (Hint: Boolfuck.)
- Show that a Brainfuck program with no
[]always halts. How many steps can it take at most? - Hard: a deterministic sequence of texts up to 4,096 characters over 100 symbols must eventually repeat. Give an upper bound on when.
Seen in
- Brainfuck. Universal in theory, bounded in practice. The textbook case.
- P′′. Proven universal by Böhm in 1964 with only loops and moves.
- FRACTRAN. Universal because a number's prime factors can act as counters.
- Rule 110 and Game of Life. Universal with no instructions at all, only a grid and a rule.
- Deadfish, HQ9+, Smallfuck. Not universal, each for a different reason.
Further
- esolangs.org/wiki/Turing-complete
- Alan Turing, "On Computable Numbers, with an Application to the Entscheidungsproblem" (1936).
- See also Turing tarpit, counter machines, fixed points.