Hat Trick
Write a value into one hat and it comes out of the other, back when you last looked inside.
cyanidesDuality, 2020 · Turing complete: unknown · ●●○ moderate · esolangs.org
Tested with 4 known programs · Test method
Run it
The idea
cyanidesDuality made Hat Trick in 2020, a language of assignments with a version of call/cc. Every line computes an RPN expression and stores it, 3 2 + => x. There are no jumps and no loops. Time travel is the only way back.
Read the story
A hat pair has an entrance and an exit. Reading the exit remembers the whole program at that moment. Writing a value to the entrance compares it with what the exit gave last time. If it is the same, the program goes on. If not, the program goes back to that read, and this time the exit holds the new value. A loop is a program that keeps sending a new value back until it sends the one it got.
The machine
| Line | Does |
|---|---|
expr => name | Store the value of the RPN expression |
expr => stdout | Print the value and a line feed |
=> [entrance|exit] | Make a hat pair |
Values are numbers, strings in either quotes, True, False and null, the empty expression. The operators are + - * / abs % ^ ?, the comparisons, ! coerce find slice len and stdin, which reads a line. + adds numbers, joins strings and ORs booleans. Dividing by 0 gives the string UNDEFINED, and 0 by 0 gives INDETERMINATE. The reference is written in Python 3, so numbers and equality follow Python, and 5 2 / is 2.5 and 4 2 / is 2.0.
Example
The hat pair.
The hat pair. Reading ext remembers the third line. Writing 3 to ent differs from the 2 the exit gave, so the program goes back to the third line with 3 in ext and prints again. The second time the values agree.
A count from 0 to 4. The last line sends back k + 1 while k is below 4 and k itself after that, which agrees with what the exit gave and ends the loop.
Patterns it teaches
- Fixed points: a loop stops at the value that comes back unchanged.
Further
See also
Also in Strange time
Write your own in Run Convert a program from Hat Trick
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: hat-trick.
Command line
shib run hat-trick -e '3 2 + => stdout'shib manual hat-trickSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/hat-trick.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org