Underload
A stack of strings and eight commands. Code and data are the same thing, so quines are ten characters long.
ais523, 2006 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 6 examples and 1 known program · Hello World on Try It Online: identical · Test method
Run it
Hello
Push the text and print it.
Expected output
Hello, world!
Output
The idea
Most stack languages keep numbers on the stack. Underload keeps programs. A pair of parentheses pushes a piece of code as a string; ^ pops one and runs it. There are no numbers, no variables and no conditionals. Everything is built by copying, gluing and running strings of code.
Read the story
That sounds too weak to be universal, but it is not. There is a known translation from Unlambda into Underload, so it is Turing complete. Gluing code (*) plays the role of function composition; running it (^) plays the role of application.
Underload is the gentlest door into concatenative languages, and the best place to see a quine with nothing hidden.
The machine
A stack of strings, and the program text itself, which is consumed from the left.
| Command | Stack before → after | Does |
|---|---|---|
(x) | → (x) | Push the text between matching parentheses |
~ | (x)(y) → (y)(x) | Swap |
: | (x) → (x)(x) | Duplicate |
! | (x) → | Drop |
* | (x)(y) → (xy) | Concatenate |
a | (x) → ((x)) | Wrap in parentheses |
^ | (x) → | Run x: put it in front of the rest of the program |
S | (x) → | Print x |
Example
A number in Underload is a program that makes n copies of the top of the stack and glues them.
A number in Underload is a program that makes n copies of the top of the stack and glues them. ::** is 3:
(ab)::**S prints ababab
The famous quine:
(:aSS):aSS
Trace it:
(:aSS)pushes the string:aSS. Stack:(:aSS).:duplicates. Stack:(:aSS) (:aSS).awraps the top. Stack:(:aSS) ((:aSS)).Sprints(:aSS).Sprints:aSS. Output:(:aSS):aSS.
The data half (:aSS) is the code half :aSS in brackets. a adds the brackets back. That is the whole idea of a quine in four commands.
And the shortest infinite loop, a fixed point: (:^):^ duplicates a program that duplicates and runs itself.
Commands
| Command | What it does | Kernel tokens |
|---|---|---|
( | start a text: push everything up to the matching ) | [ |
) | end a text | ] |
: | copy the top of the stack | DUP |
! | drop the top of the stack | DROP |
~ | swap the top two | SWAP |
* | join the top two: the lower one first | CAT |
a | wrap the top in parentheses | [ ( ] SWAP CAT [ ) ] CAT |
^ | take the top and run it as a program | CALL |
S | take the top and print it | EMIT |
Two kernel features exist for Underload. When the two brackets of a quotation come from different rules (( and )), the kernel quotes the program text between them. And a CALL at the very end of a called text is a tail call. It reuses the frame, so a loop like (:^):^ runs in constant depth until the step budget ends it.
Step by step
Push x, then push the program :*.
Run a text twice
Push x, then push the program :*. ^ runs that program on the stack below it; : copies x, * joins the two copies.
Expected output
xx
Output
- The program
:*is data until^runs it. That is the whole idea of Underload.
Wrap and print
a puts parentheses around the top value. Printing shows them.
Expected output
(hi)
Output
The quine
This program prints itself. It pushes :aSS, copies it, wraps one copy in parentheses, and prints both, (:aSS) then :aSS.
Expected output
(:aSS):aSS
Output
- A search of every
(P)Pwith P up to 5 commands finds it. See the quotation quine.
A loop that never ends
(:^) copies itself and runs the copy, which copies itself again. Nothing is printed. The kernel stops it when its 100,000 steps are spent.
Expected output
(nothing)
Output
- Because the call is a tail call, the stack of calls never grows.
Two machines, one language
A second, standalone Underload interpreter was written from the specification.
A second, standalone Underload interpreter was written from the specification. The kernel dictionary agrees with it on 600 random programs.
The quine, run by the standalone interpreter
Expected output
(:aSS):aSS
Output
Quirks
- Errors. Popping an empty stack is an error in Underload. The standalone interpreter stops with nothing; the kernel pushes an empty value instead and goes on.
- Numbers are made, not given. A number n is usually written as a program that copies its input n times, like the Church numerals of lambda calculus.
- Unknown characters. The specification says any other character is an error. The kernel skips it.
Try this
- Write the numeral 4 and use it to print
xxxx. - Predict what
(a)(b)~*Sprints, then run it. - Hard: write a program that prints its own source twice. (Hint: start from
(:aSS):aSSand duplicate before printing.)
Patterns it teaches
- Quine: the clearest quine anywhere. Data and code differ by one pair of brackets.
- Concatenative:
*composes programs;^runs them. - Stack machines: one stack, no registers, no memory.
- Code as data: the only data type is code.
- Fixed points:
(:^):^is the Y-combinator idea in six characters.
Family and nearest relatives
Underload is a concatenative language like Joy and Forth.
Underload is a concatenative language like Joy and Forth. A program is a sequence of operations on a stack, and joining two programs runs one after the other. Its nearest relatives are FALSE (a stack language with lambdas) and Unlambda (combinators, where code is also the only data). The kernel itself is close. It has quotations [ … ] and CALL, and the list words build map and filter by writing code.
Further
- esolangs.org/wiki/Underload, with the Unlambda translation.
- Emmental was shown universal by writing an Underload interpreter in it.
- Joy and Forth for concatenative languages with numbers.
See also
Stack-based
Write your own in Run Convert a program from Underload
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: underload.
Command line
shib run underload -e '(Hello, world!)S'shib run shib:underload -e '(Hello, world!)S'shib manual underloadKernel translation
| Command | Kernel instructions |
|---|---|
( | [ |
) | ] |
: | DUP |
! | DROP |
~ | SWAP |
* | CAT |
a | [ ( ] SWAP CAT [ ) ] CAT |
^ | CALL |
S | EMIT |
Kernel definition
#! shibboleth 3 seed=underload syllables= plain=off
Underload as a front end: parentheses quote text, ^ reads and runs a text, S prints it
"(" => [
")" => ]
":" => DUP
"!" => DROP
"~" => SWAP
"*" => CAT
"a" => [ ( ] SWAP CAT [ ) ] CAT
"^" => CALL
"S" => EMITSource
- Module
langs/classic.js- Kernel definition
shibboleth/witness/underload.shibonshibboleth/kernel.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org