0x29A
"Dysfunctional programming": a stack of S and K combinators, where printing a byte is a side effect of a function call.
David Lewis, 2004 · Turing complete: yes · ●●● hard · esolangs.org
Tested with 3 known programs · Test method
Run it
The idea
David Lewis made 0x29A in 2004. He called its mix of a stack machine and combinator logic dysfunctional programming. The stack does not hold numbers. It holds functions built from s, k and four extra combinators. The only data is a one-byte register.
Read the story
After every command, the function on top of the stack is rewritten if one of the rules fits. k x y becomes x. The four extra combinators work like k but touch the register on the way: + and - change it, . prints it and , reads into it. So output happens when a function gets applied.
The specification shows how to compile brainfuck into it. The tape is two functions, one for each side of the pointer, and the current cell lives in the register.
The machine
A one-byte register, starting at 0, and a stack of terms. An empty stack pops the identity (sk)s.
| Command | Does |
|---|---|
s k + - . , | Push that combinator |
% | Swap the top two |
~ | Pop a (top) and b, push b applied to a |
[ | If the register is 0, go past the matching ] (halt if none) |
] | If the register is not 0, go back to the matching [ (or the start) |
Rewrite rules, tried on the top term only, at its root, until none fits:
| Term | Becomes |
|---|---|
s x y z | x z (y z) |
k x y | x |
+ x y, - x y | x, and the register goes up or down by one |
. x y | x, the register is printed and set to 0 |
, x y | x, a character is read into the register (0 at end of input) |
Example
This is brainfuck's cat ,[.,] run through the specification's translation rules.
This is brainfuck's cat ,[.,] run through the specification's translation rules. ,%~k~ builds , T k, which reads a byte and gives back T. Printing is harder, because . clears the register. The long middle part first counts the register into two copies of a function, then runs one copy to restore the value, prints, and keeps the other.
The specification's rule for brainfuck's > has ~ where - belongs, so its loop never ends. With that one character changed, the translated brainfuck Hello World runs.
Patterns it teaches
- Stack machines: a stack, but of functions instead of numbers.
- Code as data: a brainfuck tape half is stored as a function that replays its value when applied.
- Turing completeness: shown by compiling brainfuck, not by the combinators, since only the root of a term is ever rewritten.
Try this
- Print character 1 with
+,k,.and~only. - Read a character and print the one after it.
- Hard: work out why
ss+~~%~adds ones(s+)layer to the function under it.
Further
See also
Stack-based
Write your own in Run Convert a program from 0x29A
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: 0x29a.
Command line
shib run 0x29a -e ',%~k~ k%~ kk~ [ss+~~%~ % ss+~~%~ % -%~k~] k~ .%~k~ ~' --input 'A'shib manual 0x29aSource
- Module
langs/stack/ports.js- Interpreter
langs/stack/0x29a.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org