Thue
A program is a bag of find-and-replace rules and a starting string. Rules fire in any order, at any place, until none can.
John Colagioia, 2000 · Turing complete: yes · ●○○ easy · esolangs.org
Tested with 2 examples and 2 known programs · Hello World on Try It Online: identical · Test method
Run it
Hello World
Expected output
Hello World!
Output
The idea
Thue is named after Axel Thue, the Norwegian mathematician who studied string rewriting systems in the early 1900s. A semi-Thue system is a set of rules "replace this substring with that one". John Colagioia turned it into a programming language by adding a starting string, a way to print and a way to read.
Read the story
The twist is that Thue is nondeterministic. At each step, any rule that matches anywhere may fire. The language does not say which. A well-written Thue program gives the same answer whatever the order; a careless one gives a different answer every run.
Since string rewriting can simulate a Turing machine (the tape is the string, the head is a marker symbol), Thue is universal.
The machine
The state is one string.
| Line | Means |
|---|---|
lhs::=rhs | A rule: replace one occurrence of lhs with rhs |
lhs::=~text | Delete the match and print text |
lhs::=::: | Replace the match with a line of input |
::= | End of the rules. Everything after it is the starting string |
Each step: pick any rule whose left side occurs in the string, pick any occurrence, rewrite it. When no rule matches, the program halts.
This implementation picks the rule and the place with a seeded random choice, so a run is repeatable and a different seed gives a different order. It does not support input. When a run ends, it shows the final string too.
Example
Hello World is a single rule:
Hello World is a single rule:
a::=~Hello World!
::=
a
Doubling in unary. Each a becomes bb, and each b prints a star. The rules can interleave in many ways, but the output is always six stars:
a::=bb
b::=~*
::=
aaa
Now a program where the order does matter. Each x prints a 0 or a 1, whichever rule is picked:
x::=~0
x::=~1
::=
xxxxxxxx
The last one is bubble sort in one rule: swap any ba until none are left.
Checked examples
Example
Expected output
0
Output
Quirks
- A run stops after 3,000,000 steps.
Try this
- Write a Thue program that turns
aaaaintob(halve it, twice). Make sure every order works. - Add a rule to the sorting program that prints
doneonly when the string is sorted. (Hint: a marker at the end that only moves pastbs.) - Hard: binary increment. Start from
_1011_and end with_1100_, with a carry marker that walks left.
Patterns it teaches
- String rewriting: the whole language is one rewriting system.
- Nondeterminism: the order is not specified, so correctness means "the same result for every order".
- Turing completeness: rewriting rules can simulate a Turing machine, with a marker for the head.
Further
- esolangs.org/wiki/Thue
- ///, which rewrites its own program text deterministically; Retina, rewriting with regular expressions; FRACTRAN, rewriting numbers.
See also
Nondeterministic
String-rewriting paradigm
Write your own in Run Convert a program from Thue
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: thue.
Command line
shib run thue -e 'a::=~Hello World!
::=
a'shib manual thueSource
- Module
langs/classic.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org