Stack Cats
Every program must be its own mirror image, and every command's mirror is its inverse. So the second half of each program is busy undoing the first.
Martin Ender and Sp3000, 2016 · Turing complete: yes · ●●● hard · esolangs.org
Tested with 6 known programs · Hello World on Try It Online: identical · Test method
Run it
The idea
Stack Cats was made in 2016 by Martin Ender and Sp3000, as an entry in a language-design challenge on Code Golf Stack Exchange. It is reversible, like Janus, but it gets there by a much stranger road.
Read the story
Each command has a mirror. ( mirrors ), [ mirrors ], < mirrors >, / mirrors \, and characters like -, !, : and | are their own mirrors. The rule of the language is that a program, reversed and with every character swapped for its mirror, must give the same program back. And each command's mirror is exactly its inverse.
So a program has the shape A c A⁻¹: some code, a centre command, and the undoing of the code. If the centre did nothing, the whole program would undo itself, and every input would come out unchanged. That is the joke in the name: an even-length program, if it stops, is always cat. All the work happens in how A sets things up so that the one centre command does something useful, and A⁻¹ cleans up around it.
One consequence is easy to miss. The centre command is its own mirror, so it is its own inverse. Then running the program twice gives A c A⁻¹ A c A⁻¹ = A c c A⁻¹, which is nothing at all. Every Stack Cats program that halts is its own inverse: run its output through it again and you get the input back.
The machine
An infinite tape of stacks, a head pointing at one of them, and in each stack an endless supply of zeros at the bottom. Values are integers of any size. The input bytes are pushed onto the starting stack above a -1, first byte on top. At the end, the current stack is printed top first, mod 256, ignoring a -1 at the very bottom.
| Char | Does | Mirror |
|---|---|---|
- | Negate the top | itself |
! | Bitwise not of the top (that is, −x − 1) | itself |
* | Flip the lowest bit of the top | itself |
_ ^ | Pop a, b; push b, then b − a (or b xor a) | itself |
: | Swap the top two | itself |
+ | Swap the top and third | itself |
= | Swap the tops of the two neighbouring stacks | itself |
\| | Reverse the stack down to the first zero | itself |
T | If the top is not zero, reverse the whole stack | itself |
I | Top < 0: carry it left and negate; top > 0: carry it right and negate | itself |
X | Swap the two neighbouring stacks | itself |
< > | Move the head left or right | each other |
[ ] | Move left or right, carrying the top along | each other |
/ \ | Swap this stack with its left or right neighbour, and move there | each other |
( … ) | Loop entered and left only when the top is positive | each other |
{ … } | Loop that repeats until the top is back to the value it had at { | each other |
Example
The smallest useful programs are a single self-mirrored command.
The smallest useful programs are a single self-mirrored command. : swaps the first two bytes:
:
Increment does not exist on its own, because it has no mirror twin. But !- is "not, then negate", which is −(−x − 1) = x + 1, and its mirror -! is minus one. So increment around something is legal:
!-*-!
This is A c A⁻¹ with A = !- (add one), c = * (flip the low bit), A⁻¹ = -! (subtract one). It swaps the first byte between pairs: a ↔ b, c ↔ d, and so on. On abc it prints bbc, and on bbc it prints abc again.
--mode mirror writes the mirror half for you. The README's example :>[(!)- expands to :>[(!)-(!)]<:, a longer cat.
Patterns it teaches
- Reversible computing: reversibility as a syntax rule. The program text itself carries the inverse of every step.
- Stack machines: a tape of stacks, which makes it a little like Brainfuck where each cell is a stack.
- Turing completeness: the specification shows how to translate Brainfuck (without its I/O) into Stack Cats, using big integers, and then restores the mirror rule.
Try this
- Predict what
+does on the inputabc, then run it. - Explain why no Stack Cats program can add 1 to the first byte and leave the rest alone. (Hint: what happens if you run it twice?)
- Hard: write a program that reverses its input. The -1 at the bottom of the stack is the obstacle. A seven-character solution exists.
Further
- esolangs.org/wiki/Stack_Cats
- The reference implementation and README (github.com/m-ender/stackcats), with Ruby and Python 3 versions.
- Related: Janus, Labyrinth, Hexagony.
See also
Reversible computing
Stack-based
Write your own in Run Convert a program from Stack Cats
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: stack-cats.
Command line
shib run stack-cats -e ':' --input 'abc'shib manual stack-catsSource
- Module
langs/museum/ports.js- Interpreter
langs/museum/stack-cats.js,langs/common.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org