Whenever
A language with no sense of urgency. The program is a to-do list, and the interpreter does the lines in any order it likes.
David Morgan-Mar, 2001 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 1 known program · Test method
Run it
The idea
David Morgan-Mar (also the author of Chef) designed Whenever around 2001. There is no sequence, no goto, no loop and no variable. The program is a numbered to-do list. The interpreter picks a line at random, does it, and crosses it off.
Read the story
All control comes from two things. A line can be put back on the list, or several copies of it can be added. And a line can say "not yet": defer keeps it waiting until a condition fails.
So the only memory is the list itself. How many copies of line 7 are waiting is a variable. Counting, looping and branching are all built from adding and removing copies of lines.
The machine
The to-do list is a multiset of line numbers. At the start it holds each line once. Then, until it is empty:
- Pick one entry at random (every copy is equally likely).
- If any
defer (b)on that line is true, do nothing; the entry stays. - Otherwise run the statement and remove that one entry, unless an
again (b)on the line is true.
| Statement | Does |
|---|---|
n | Add one copy of line n to the list |
-n | Remove one copy of line n (if there is one) |
n#k | Add k copies (or remove them, if k is negative) |
2,3#4,-5 | Several of the above at once |
print(x) | Print x and a newline. + joins strings |
read() | Read a number (or a character code) from input |
N(n) | How many copies of line n are on the list |
U(n) | The character with code n |
defer (b) s | Postpone s while b is true |
again (b) s | Run s, and keep the line on the list if b is true |
In a condition a plain number means "is that line still on the list?". So defer (1) means "wait until line 1 is done".
Example
A countdown that comes out right whatever order the interpreter picks:
A countdown that comes out right whatever order the interpreter picks:
1 2#4;
2 defer (1) print(N(2) + "...");
3 defer (1 || 2) print("Liftoff!");
Line 1 adds four more copies of line 2, so there are five. Line 2 waits until line 1 is done. Each time a copy runs, it prints how many copies are left (including itself), then is removed: 5..., 4..., 3..., 2..., 1.... Line 3 waits until both 1 and 2 are gone, then prints Liftoff!.
This implementation seeds its random choices from a hash of the program text. Add a line that is not a statement, such as x, to try another order. The output above is the same for every such line tried, while three plain print lines come out in other orders.
Patterns it teaches
- Nondeterminism: the interpreter chooses the order. Writing in Whenever means writing programs whose result does not depend on that choice. Every
deferis a little proof of order-independence. - Turing completeness: copy counts are unbounded counters, and
deferwithN(...)tests them for zero. That is a counter machine.
Try this
- Write a program that prints
a, thenb, thenc, in that order, every time. - Print the numbers 1 to 5 counting up. (Hint: count the copies of a second line.)
- Hard: add two numbers read from input, using only copy counts as storage.
Further
- esolangs.org/wiki/Whenever
- The original specification, with a 99 Bottles and a Fibonacci program: dangermouse.net/esoteric/whenever.html
- Compare Java2K and Entropy, two other languages that take control away.
See also
Probabilistic
Nondeterministic
Write your own in Run Convert a program from Whenever
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: whenever.
Command line
shib run whenever -e '1 2#4;
2 defer (1) print(N(2) + "...");
3 defer (1 || 2) print("Liftoff!");'shib manual wheneverSource
- Module
langs/museum/ports.js- Interpreter
langs/museum/whenever.js,langs/common.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org