esolang

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

Expected: 5

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

LineDoes
expr => nameStore the value of the RPN expression
expr => stdoutPrint 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.
Expected: Returning with: hats

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.

Expected: 2 3

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.

Expected: 0 1 2 3 4

Patterns it teaches

  • Fixed points: a loop stops at the value that comes back unchanged.

Further

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-trick

Source

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