esolang

Alchemist

A program is a list of reactions. Whatever atoms are present react, until nothing can.

bforte · Turing complete: yes · ●●○ moderate

Tested with 4 known programs · Hello World on Try It Online: identical · Test method

Run it

Expected: Hello, World!

The idea

Alchemist, by bforte, models a pot of chemicals. The state is a multiset, a bag of atoms with a count for each kind. A rule says which atoms react and what they turn into. 2a+b->c takes two a and one b out of the bag and puts one c in.

Read the story

At the start the bag holds a single _. On every step, one rule whose left side is in the bag reacts. When no rule can, the program ends. Counts are the only data, so a number is that many atoms of one kind, and a loop is a rule that keeps reacting while its atoms last.

0a on the left side means "no a at all", which is how a program tests for zero.

The machine

TermWhereMeans
a 3abothOne or three atoms a
0aleftOnly when there is no a
Out_arightPrint how many a there are
Out_"text"rightPrint the text
In_arightRead a number and add that many a

When several rules could react, the interpreter on Try It Online picks one at random. This implementation picks the first in the program, so that every run replays. Careful programs make sure only one rule can react at a time, and those behave the same either way.

Example

The second program turns _ into three a and a b, which acts as a catalyst; it is taken and put back each…
Expected: ***
Expected: 42

The second program turns _ into three a and a b, which acts as a catalyst; it is taken and put back each time, while one a is used up per star. The third reads 21 into a atoms and turns each into two c; once no a is left, the last rule prints the count.

Patterns it teaches

  • String rewriting: rules applied until none matches, on a bag instead of a string.
  • Nondeterminism: the reference picks among the rules that could react at random.
  • Counter machines: atom counts are unbounded registers and 0a tests one for zero.

Further

Also in Rewriting

Write your own in Run Convert a program from Alchemist

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: alchemist.

Command line

shib run alchemist -e '_->Out_"OK\""'
shib manual alchemist

Source

Module
langs/tio/ports.js
Interpreter
langs/tio/alchemist.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language