Reversible computing
Every step can be run backwards. Nothing is ever forgotten, so nothing is ever lost.
●●○ moderate · where to see it
The idea
Ordinary programs throw information away all the time. After x = 0, you cannot tell what x was. After a + b = 7, you cannot tell which a and b it came from.
Read the story
A reversible program never does that. Every step is a one-to-one map from states to states, so you can always run it backwards and recover where you came from.
The first reason is physics. Rolf Landauer argued in 1961 that erasing a bit has a minimum energy cost, given off as heat. Charles Bennett showed in 1973 that any computation can be made reversible, so in principle computing need not erase anything. Reversible logic gates followed, such as the Toffoli gate (1980) and the Fredkin gate. Today the same ideas matter for quantum computing, where every step must be reversible, and for running programs backwards to debug or to compute gradients.
How it works
An update like x += y is reversible as long as x and y are different variables; its inverse is x -= y.
Steps that can be undone
An update like x += y is reversible as long as x and y are different variables; its inverse is x -= y. An update like x = 0 is not. A reversible language only lets you write the first kind.
| Reversible | Its inverse | Not reversible |
|---|---|---|
x += e | x -= e | x = e |
x ^= e (xor) | x ^= e (itself) | x = x & e |
swap x and y | swap again | y = x (the old y is lost) |
Swapping two numbers with no temporary variable takes xor three times.
x, y = 6, 10
x ^= y; y ^= x; x ^= y # forward: x, y = 10, 6
x ^= y; y ^= x; x ^= y # backward: the inverse runs the steps in reverse order;
# here that is the same three steps: x, y = 6, 10
Control flow that can be undone
if is the hard part. Going forward, the condition tells you which branch to take. Going backward, you need to know which branch you came from. Janus (Christopher Lutz and Howard Derby, 1982) solves it with an exit assertion.
if x > 0 then ... else ... fi y = 0
The fi condition must be true after the then-branch and false after the else-branch. Running backwards, Janus tests it to know which branch to undo. Loops have an entry and an exit assertion in the same way.
Bennett's trick
To keep the answer without the intermediate values, compute forward, copy the answer into a fresh variable (copying into a zero is reversible, out ^= result), then run the computation backwards to clean up. Only input and answer remain.
Seen in
- Janus. The first reversible programming language; every procedure can also be uncalled.
- Stack Cats. A reversible stack language where a program must also read the same as its mirror image.
Try this
- Write the inverse of
x += 3; y += x; swap x y. - Why is
x += xnot allowed in Janus, even thoughx -= xlooks like its inverse? - Hard: using Bennett's trick, describe a reversible program that computes
out = a * band leaves onlya,bandoutbehind.
Further
- esolangs.org/wiki/Janus
- Reversible computing on Wikipedia
- Charles Bennett, "Logical Reversibility of Computation" (1973).
- See also fixed points, cellular automata, model collapse.