Tandem
One rewrite rule, applied once. The choices and the loops are inside the rule, and the rules form a Kleene algebra.
Chris Pressey, 2020 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 11 known programs · Test method
Run it
The idea
Chris Pressey designed Tandem in June 2020. A program rewrites a collection of labelled stacks, strings that are only ever changed at their left end. A usual rewriting system keeps picking rules from a list until none applies. A Tandem program is a single rule, built with three operators. | picks whichever of two rules matches, & applies one rule and then the other, and * repeats a rule while it matches. Together with 0, the rule that never matches, and 1, the rule that always matches and changes nothing, they form a Kleene algebra.
Read the story
The choice has to be deterministic. When both sides of a | match and give different results, the run stops with "multiple rewrite choices encountered".
The machine
| Rule | Does |
|---|---|
Ls → t | If stack L is s exactly, make it t |
Ls… → t | If L starts with s, make it t |
Ls… → t… | If L starts with s, put t in place of that s |
0 1 | Never match; always match and change nothing |
R & S | R, then S on the result; matches if both do |
R \| S | Whichever of R and S matches |
R* | R as long as it matches; always matches |
A label is a capital or a quoted string, and every stack starts empty. A % in front of a rule writes the stack with its top on the right. -> and ... may stand for → and …. With the pragma {B:i,o} the input is put on stack i, first character on top, and stack o is printed at the end with its top last.
Example
The binary cat moves one bit at a time from I to O while Q stays 0.
The binary cat moves one bit at a time from I to O while Q stays 0. When I is empty, Q becomes 1 and nothing matches any more.
The push down automaton for nested parentheses keeps its state in Q and its stack in K, with $ at the bottom. O starts as N and becomes Y only when the input runs out in state 0.
The reverse cat needs only the rule 1. The input goes onto B with its first character on top, and B is printed with its top last.
Without the B pragma this implementation prints every stack as label=contents. The Turing machine keeps the tape left of the head in L and the rest in R. On 111110 it ends in state 1, because R runs out and no rule matches.
Patterns it teaches
- String rewriting: always at the top of a named stack, so nothing is searched.
- Nondeterminism: two rewrites that could both happen are an error.
- Turing completeness: Turing machines and Minsky machines translate rule by rule.
Further
See also
String-rewriting paradigm
Write your own in Run Convert a program from Tandem
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: tandem.
Command line
shib run tandem -e '{B:B,B}1' --input 'Hello'shib manual tandemSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/tandem.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org