Nondeterminism
When the program does not fully decide what happens next. Sometimes a coin decides, sometimes the interpreter, sometimes nobody knows.
●●○ moderate · where to see it
The idea
A deterministic program does the same thing every time, given the same input. Many esolangs break that, and they break it in different ways. It helps to keep four words apart.
Read the story
- Random. A real coin is flipped. Befunge's
?sends the pointer in a random direction. - Arbitrary. The language says "one of these happens" and leaves the choice to the interpreter. Thue (John Colagioia, 2000) applies some matching rule, unspecified which. Whenever (David Morgan-Mar) runs the lines of its to-do list in whatever order the interpreter likes.
- Nondeterministic, in the theory sense. All choices are explored at once, and the machine succeeds if any branch does. This is the "N" in NP. It is a tool for proofs, not something you can run directly.
- Deterministic but unpredictable. No coin at all, yet a tiny change in the input changes everything. Chaos. Pseudo-random number generators are this.
Languages add it for jokes (Whenever), for hostility (Java2K, where functions only probably work), for decay (Entropy, where data gets fuzzier each time it is read), or because the underlying model is a choice (Thue is a string-rewriting system, and those are naturally nondeterministic).
How it works
A Thue coin.
A Thue coin. Rules come first, ::= ends them, then the starting string. A replacement that starts with ~ prints the rest and puts nothing back.
coin::=~heads
coin::=~tails
::=
coin
Both rules match coin. Thue says only that one is applied. Some interpreters take the first, some pick at random, so the output is heads or tails. After that the string is empty, no rule matches, and the program stops.
Some systems go the other way. They need randomness but want every run to be reproducible. So the random generator is seeded from a hash of the program's own text. One Whenever interpreter does this with the FNV-1a hash below.
function hash(text) {
let h = 0x811c9dc5;
for (let i = 0; i < text.length; i++) h = Math.imul(h ^ text.charCodeAt(i), 0x01000193);
return h >>> 0;
}
| Text | Hash |
|---|---|
1 print("hi"); + newline | 3dafeeca |
1 print("hi"); + two newlines | dbf4d840 |
1 print("ho"); + newline | cafcadbc |
One extra newline gives a completely different seed, so a completely different stream of "random" choices. But the same text always gives the same seed, on any machine.
Seen in
- Thue. Arbitrary choice between matching rules.
- Befunge.
?picks a random direction. - Fish.
xpicks a random direction too. - Whenever. No order at all; the program is a to-do list.
- INTERCAL. A statement can carry a percentage, such as
DO %50, and then runs only that often. - Java2K. Every function may or may not do what it says.
- Entropy. Values drift a little each time they are used.
Try this
- Add a third rule
coin::=~edgeto the Thue program. Run it in an interpreter several times. Does your interpreter pick at random, or always the first? - Hash two texts that differ in one character with the function above. How many of the 32 bits differ?
- Hard: a program seeded from its own text freezes once it copies itself perfectly. Propose a rule that keeps determinism but avoids freezing.