esolang

Binary lambda calculus

The lambda calculus written in bits, so that programs can be measured. Its self-interpreter fits in about 200 bits.

John Tromp, 2004 · Turing complete: yes · ●●● hard · esolangs.org

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

Run it

Expected: prints: hi

The idea

How much information is in a program? Kolmogorov complexity defines it as the length of the shortest program that produces it. But "length" depends on the language. John Tromp wanted a language so simple and so close to mathematics that the measure would feel natural.

Read the story

His answer is the plain lambda calculus with a binary code. Variables are written as de Bruijn indices: instead of a name, a variable says how many λs out its binder is. Then there are only three kinds of term, and each gets a prefix of bits.

The code is self-delimiting: the parser always knows where a term ends, so a program can be followed directly by its input, with no separator. That is what makes the length of a program a fair measure.

The machine

BitsTerm
00 Mλ M (an abstraction)
01 M NM N (an application)
1…1 0 (n ones)Variable n: 1 is the nearest λ, 2 the next one out, …

Input and output are lists of bits, built from pairs: cons a b = λf. f a b. The empty list and the bit 1 are both λλ1 (000010); the bit 0 is λλ2 (0000110). The program is applied to its input, and the result is the output.

This implementation packs input bytes into bits (most significant first) and packs the output back into bytes. --mode term prints a bit string as a term.

Example

The identity, λ1, is 0010.

The identity, λ1, is 0010. Applied to the input, it returns the input: cat in four bits.

Expected: prints: λλλ((3 1) (2 1))

The second line is S. Read the bits: 00 00 00 are three λs; 01 01 are two applications; 1110 is variable 3, 10 is variable 1; 01 110 10 is (2 1). So S = λλλ (3 1) (2 1), in 23 bits.

Drop the first bit of the input with λ (1 (λλ1)), "apply the list to false":

Expected: prints: [+7 bits: 1000001]

A is 01000001; with its first bit gone, 7 bits are left, which no longer make a byte.

Patterns it teaches

Try this

  1. Encode K = λλ2 and check it with -t.
  2. Encode the program that ignores its input and outputs the empty list.
  3. Hard: write a program that outputs every input bit flipped. (You need recursion, so a fixed-point combinator, in bits.)

Further

Functional paradigm

Write your own in Run Convert a program from Binary lambda calculus

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: binary-lambda-calculus.

Command line

shib run binary-lambda-calculus -e '0010' --input 'hi'
shib manual binary-lambda-calculus

Source

Module
langs/museum/ports.js
Interpreter
langs/museum/binary-lambda-calculus.js, langs/common.js

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

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