esolang

Forest

Memory is one infinite binary tree of bits. The program compares and copies whole subtrees, sometimes into themselves.

Hakerh400, 2021 · Turing complete: yes · ●●● hard · esolangs.org

Tested with 4 known programs · Test method

Run it

Expected: tree

The idea

Hakerh400 made Forest in 2021. Every node of the memory holds one bit and has two children, so the memory never ends in any direction. An address is a path from the root, 0 for left and 1 for right, and the empty address is the root itself.

Read the story

There are only three instructions. Two of them compare or copy whole subtrees, and the third jumps to a label. Copying a subtree into a place inside itself repeats the copy again and again, so a single instruction can change infinitely many bits. The result is a tree that contains itself.

The machine

InstructionDoes
x?yGo on if the subtrees at x and y are equal, else skip the next instruction
x.yCopy the subtree at x to y
:nameJump to the label name:

At the start the root holds 1, its left subtree is all zeros and its right subtree is the input. A string of bits is a node holding 1, with the first bit in its left child and the rest of the string to its right. The empty string is all zeros. When the program ends, the output is read back from address 1. Each byte is eight bits, the least significant first, which is the order in which the Hello, World! spells its text.

Example

The cat is the empty program, because input and output share address 1.

The cat is the empty program, because input and output share address 1.

The Reverse bits. Its first six instructions set up a zero tree at 000 to compare with, a list cell at 001 and an empty list at 01. The loop moves one bit at a time from the input to the front of the new list. Reversing all the bits also mirrors the bits inside each byte. F and b are each other's mirror image, as are N and r, so rNbF comes out as bFrN.

Expected: bFrN

1.10 copies the input list into its own first cell. That cell becomes the list node itself, which holds 1, so the first bit of @ turns to 1 and it becomes A.

Expected: A

The Hello, World! is 367 copies and no jumps. After the same six setup instructions it pushes 104 bits one at a time, 48 of them turned to 1, and then moves the list to 1.

Patterns it teaches

  • Turing tarpit: compare, copy and jump on one tree.
  • Fixed points: a copy into itself leaves a tree that the copy no longer changes.

Further

Also in Turing tarpits

Write your own in Run Convert a program from Forest

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run forest -e 'init:
  .00
  01.000
  01.001
  00.0
  0.001
  01.0010

reverse:
  1?000
  :output

  01.0011
  001.01
  000.0011
  10.010
  11.1
  
  :reverse

output:
  01.1' --input 'rNbF'
shib manual forest

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/forest.js

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

Source esolangs.org (CC0) · Edit on esolangs.org