esolang

Referencement

Lambda calculus with closures and assignment, kept as numbers written on each function so that it can be done on paper.

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

Tested with 4 known programs · Test method

Run it

Expected: Hi

The idea

Hakerh400 made Referencement in 2019. In lambda calculus a function cannot change a variable, and closures need pointers. Referencement writes the pointers as numbers instead. An abstraction can carry one in front of its argument and one after it, as in 0-&a-1. a, and the rules hand them out while a program runs. The first number says which object a function is. The second says which reference it belongs to. An argument written &a is passed by reference and keeps its second number; a plain one gets a new number, so it becomes a copy.

Read the story

Five native functions do the rest, and a program never names them. They are passed in as its first five arguments. [0] assigns a new value to everything that shares a reference, [1] tells whether two functions are the same object, [2] reads a bit, and [3] and [4] write one.

The machine

WriteDoes
x. body &x. bodyA function whose argument is passed by value or by reference
f x (...)Apply; group
[0] X YX, and everything that shares its reference, becomes Y
[1] X Y ZZ Z if X and Y are the same object, else Z
[2] XRead a bit; X X for 1, X for 0
[3] X [4] XWrite a 0 or a 1, then X

Each step reduces the leftmost invocation whose two sides are not invocations, so arguments are worked out before they are passed, and nothing inside a function body runs before it is called. Input is bits. Each byte gives eight bits from the lowest, each after a 1, and zeros follow the last byte. Output bits make bytes the same way. The specification shows two runs step by step, 11 and 17 steps, and this implementation takes the same steps.

Example

The Hello World with other bits.

The Hello World with other bits. It binds the natives to 0 to 4, then makes 9 a function that takes a bit, writes it and returns itself for the next one. 7 writes a 0 and 8 a 1, each byte from its lowest bit.

The cat keeps its variables in 8 to c and sets them with 2, the assignment. 9 reads a bit, 8 writes one, and a loops while the bit in front of each input bit is a 1.

Expected: Lambda

The Invert bits and Reverse bits work as well. Reverse keeps every bit in a linked list of closures and writes them back from the end, so abc comes out as the bytes 198, 70 and 134.

Two choices were needed. The specification says a reference to X inside Y becomes {z}, but its own Hello World, cat and bit programs only run when it becomes {z} {z}, so this implementation does that. And a copy of a term is shared rather than written out again. On paper a list node that holds its tail twice doubles with every bit; shared, the same 24 bits reverse in 15,252 reductions.

Patterns it teaches

  • Lambda encoding: true and false are (0.1.0) and (0.1.1), and variables are closures.

Further

Also in Functional and concatenative

Write your own in Run Convert a program from Referencement

Implementation

JavaScript interpreter, source and command line

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

Command line

shib run referencement -e '(&0.&1.&2.(3.4.5.6.7.(8.(9.(a.(b.(c.1(0(2 c(d.e.e))(2 b(d.e.d))(2 a(f.g.f 1(h.1(0(g 1))(a f g))c 1))
(2 9(h.(i.1(0(2 i c)(5(h.2 i b)))i)1))(2 8(i.i 7 6 1)))(a 9(h.8(9 1))))1)1)1)1)1)2)((&d.d d)(&d.&e.d
d))(&d.&e.e)' --input 'abc'
shib manual referencement

Source

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

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

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