esolang

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

Expected: n=0 x1=5 x2=8

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).

StatementForwardsBackwards
x += e / x -= eAdd or subtract eThe other one
x ^= eXor with eThe same
x <=> ySwapThe same
if c then A else B fi dTest c, run A or B; then assert d is true exactly when c wasTest d, undo A or B, assert c
from c do A loop B until dc must be true on entry and false on every later lap; repeat A, then B, until dStart from d, run back until c
call p / uncall pRun p forwards / backwardsSwapped

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:

Expected: n=4 x1=0 x2=0

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

  1. Write a procedure that sets y to x * x without changing x. Then uncall it and check that y goes back to 0.
  2. Write a from … loop … until that sums 1 to 10 into s, then run it backwards from i=10 s=55.
  3. Hard: why does fi x1 = x2 work as the assertion in fib, and why does fi n = 0 fail? (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.

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 janus

Source

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