Janus
An ordinary-looking imperative language in which every program can also run backwards. call runs a procedure; uncall un-runs it.
Christopher Lutz and Howard Derby, 1982 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 2 known programs · Test method
Run it
The idea
Janus was created by Christopher Lutz and Howard Derby at Caltech in 1982; Lutz described it in a 1986 letter to Rolf Landauer, the physicist who showed that erasing information is what costs energy. Tetsuo Yokoyama and Robert Glück revived and formalised it in 2007.
Read the story
It is named after the Roman god with two faces, one looking forward and one back. The goal is simple to state: no statement may throw information away. Then every run can be undone, step by step, and the interpreter can run any procedure in reverse.
That rules out the most common statement in programming. x = 5 forgets what x was. So Janus has no plain assignment, only updates that can be undone: x += e (undo with x -= e), x ^= e (its own undo) and x <=> y (swap). Branches and loops need a second condition, so that running backwards knows which way it came.
The machine
Integer variables, all starting at 0, and procedures without parameters (the classic version).
| Statement | Forwards | Backwards |
|---|---|---|
x += e / x -= e | Add or subtract e | The other one |
x ^= e | Xor with e | The same |
x <=> y | Swap | The same |
if c then A else B fi d | Test c, run A or B; then assert d is true exactly when c was | Test d, undo A or B, assert c |
from c do A loop B until d | c must be true on entry and false on every later lap; repeat A, then B, until d | Start from d, run back until c |
call p / uncall p | Run p forwards / backwards | Swapped |
One rule keeps updates reversible: in x += e, the variable x may not appear in e. Otherwise x += x would double x, and x -= x could not undo it. Assertions that fail are run-time errors, because a failed assertion means the step could not be reversed.
Example
Fibonacci pairs, after Yokoyama and Glück.
Fibonacci pairs, after Yokoyama and Glück. fib turns n into the pair (x1, x2) = (F(n+1), F(n+2)):
procedure main
call fib
procedure fib
if n = 0 then
x1 += 1
x2 += 1
else
n -= 1
call fib
x1 += x2
x1 <=> x2
fi x1 = x2
The fi x1 = x2 is the exit assertion. It holds after the then branch (both are 1) and fails after the else branch (x2 is always larger). So when running backwards, the interpreter can look at x1 and x2 and know which branch to undo.
It is saved as langs/museum/examples/fib.ja. Run it forwards with n = 4, then backwards from the result:
The backward run computes the inverse: given a pair of neighbouring Fibonacci numbers, it finds which n produced them. Give it a pair that is not Fibonacci, like 5 and 9, and it never reaches the base case.
This implementation handles integer variables only, with no arrays, stacks, parameters or local.
Patterns it teaches
- Reversible computing: usually called the first reversible programming language, and still the standard example. Every statement has an inverse, and so does every program.
- Metacircular interpreter: Yokoyama and Glück wrote a Janus interpreter in Janus, and since it is a Janus program, running it backwards gives an inverse interpreter for free.
- Turing completeness: with unbounded integers or arrays, Janus can simulate any reversible Turing machine; Yokoyama and Glück call this being reversibly Turing complete.
Try this
- Write a procedure that sets
ytox * xwithout changingx. Thenuncallit and check thatygoes back to 0. - Write a
from … loop … untilthat sums 1 to 10 intos, then run it backwards fromi=10 s=55. - Hard: why does
fi x1 = x2work as the assertion infib, and why doesfi n = 0fail? (Try n = 1.)
Further
- esolangs.org/wiki/Janus
- Tetsuo Yokoyama and Robert Glück, "A reversible programming language and its invertible self-interpreter" (PEPM 2007).
- Christopher Lutz, "Janus: a time-reversible language" (1986), a letter to Rolf Landauer.
- Related: Stack Cats, another reversible language.
See also
Reversible computing
Write your own in Run Convert a program from Janus
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: janus.
Command line
shib run janus -e 'procedure main
call fib
procedure fib
if n = 0 then
x1 += 1
x2 += 1
else
n -= 1
call fib
x1 += x2
x1 <=> x2
fi x1 = x2
' --input 'n=4'shib manual janusSource
- Module
langs/museum/ports.js- Interpreter
langs/museum/janus.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org