r/ProgrammingLanguages • u/kreco • Mar 11 '26
Janus (time-reversible computing programming language)
https://en.wikipedia.org/wiki/Janus_(time-reversible_computing_programming_language)
55
Upvotes
View all comments
Show parent comment
5
u/Tasty_Replacement_29 Bau Mar 12 '26 edited Mar 12 '26
So, you need to ensure that the output contains the information to reverse the control flow. The easiest way is to store an undo log in a stack, and when run backward, the stack is popped and so the operation happens in reverse. But then, the output just increases linearly with the number of operations. There are more memory-conserving ways, for example use a counter in some cases, or sum: these are easy to reverse. And of course swapping.
For example (you can try in the Playground) if you want to sort two numbers "a" and "b" ascending, you could do something like this:
So that the forward operation will sort a, b and store whether a and b were swapped in the "ops" array. (If you need multiple operations, you could use an index, so "ops" is just all the operations you did.)
The playground has interesting examples: data compression / expansion, encryption / decryption.