A reversible programming language

One program.
Both directions.

call transforms state. uncall runs the same Janus program backward—no separate decoder, unsorter, or undo implementation.

Watch information move instead of disappear. The array becomes sorted while six trace bits remember the swaps. uncall reads those bits backward to restore the input.

Reversible sort

Sort forward.
Restore backward.

An ordinary sort forgets where values came from. This one leaves six branch bits behind—just enough information to retrace every swap.

sort.janus · editable

change it and run again
sort.janus

Array and trace

input
uncall(sort, call(sort, state)) = state

Call sorts the array. Uncall follows the recorded decisions in reverse and reconstructs the input.

values[4]

edit initial state

trace[6]

branch history
  1. v0↔v10empty
  2. v1↔v20empty
  3. v2↔v30empty
  4. v0↔v10empty
  5. v1↔v20empty
  6. v0↔v10empty
initial
call sort →
← uncall sort

Ready. Call the program forward.

Sort

The array changes into ascending order.

Remember

Each trace bit says whether one comparison swapped.

Restore

Reading those bits backward recreates the original order.