Muriel
There are no loops. To do something again, a program writes out a new copy of itself and runs that instead.
Matthew Westcott, 2001 · Turing complete: yes · ●●● hard · esolangs.org
Tested with 5 known programs · Hello World on Try It Online: identical · Test method
Run it
The idea
Matthew Westcott made Muriel in 2001. The name stands for Monumentally Useless Re-Iterative Execution Language. It has variables, numbers, strings and printing, but no if, no while and no jumps. What it has is @: give it a string, and the running program is thrown away and the string is run as a brand new program, with every variable forgotten.
Read the story
So a loop is a program that prints its own source text, slightly changed, and runs it. The counter goes into the text as a number; a program holding n:3 writes a copy holding n:2. That makes every Muriel loop a kind of quine. The | operator exists to make this bearable: it escapes quotes and backslashes so a string can be put back inside a string literal.
There is no if either. The trick is substrings: %S,0,&S*(n>1) is all of S when n>1 and the empty string otherwise. Running the empty program ends everything.
The machine
A program is instructions separated by ;. Lowercase letters hold numbers, uppercase letters hold strings.
| Code | Does |
|---|---|
a:expr A:expr | Set a number or a string variable |
.S | Print a string |
@S | Replace the whole running program with S; all variables are cleared |
12 a + - * | Numbers, number variables, arithmetic (* binds tighter) |
= > < | Compare, giving 1 or 0 |
#S &S | The number written in S; the length of S |
"text" A S+S | Strings with \" \n \\, string variables, joining |
$n | A number as a string, so $(1+1) is "2" |
%S,a,b | The part of S from position a up to b |
\|S | S with ", \ and newlines turned back into escapes |
~ | A line of input |
This implementation follows Chris Pressey's Perl interpreter, the one Try It Online runs, in its precedence and its quiet handling of odd cases: a substring past the end is cut short instead of being an error.
Example
The third program builds the text ."made at run time" and runs it.
The third program builds the text ."made at run time" and runs it. Below is a loop that counts down. A holds the second half of the program as text:
A:";.$n+\" \";@%(\"A:\\\"\"+|A+\"\\\";n:\"+$(n-1)+A),0,9999*(n>1)";n:3;.$n+" ";@%("A:\""+|A+"\";n:"+$(n-1)+A),0,9999*(n>1)
It prints n, then rebuilds itself: A:" plus A escaped, then ";n: with n-1, then A again. That is the same program with a smaller n. The %…,0,9999*(n>1) keeps the whole text while n>1 and nothing after, so the last round runs an empty program and stops.
Patterns it teaches
- Quine: every loop is a program reproducing itself, plus a change.
- Self-modifying code: the only way forward is to replace the program.
- Code as data: program text is an ordinary string, built with
+,$and|. - Turing completeness: esolangs.org has an interpreter for Bub, a brainfuck variant, written in Muriel, and a compiler from Minsky machines.
Try this
- Change the countdown to start at 10.
- Print
*,**,***: let the program grow a string instead of a number. - Hard: write a quine. The one on esolangs.org has two lines; the second prints the first.
Further
See also
String-rewriting paradigm
Write your own in Run Convert a program from Muriel
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: muriel.
Command line
shib run muriel -e '."Hello, World!"'shib manual murielSource
- Module
langs/tio/ports.js- Interpreter
langs/tio/muriel.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org