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
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
| Bits | Term |
|---|---|
00 M | λ M (an abstraction) |
01 M N | M 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.
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":
A is 01000001; with its first bit gone, 7 bits are left, which no longer make a byte.
Patterns it teaches
- Lambda encoding: bits, lists and bytes are all functions.
- Encoding tricks: de Bruijn indices remove names, and a prefix code removes separators.
- Metacircular interpreter: Tromp's BLC interpreter written in BLC is a couple of hundred bits, famously small.
- Turing completeness: it is the untyped lambda calculus.
Try this
- Encode K = λλ2 and check it with
-t. - Encode the program that ignores its input and outputs the empty list.
- Hard: write a program that outputs every input bit flipped. (You need recursion, so a fixed-point combinator, in bits.)
Further
- esolangs.org/wiki/Binary_lambda_calculus
- John Tromp's page on BLC and combinatory logic: tromp.github.io/cl/cl.html, with his paper "Binary Lambda Calculus and Combinatory Logic".
- Tromp's obfuscated BLC interpreter won "Most functional" at the 2012 IOCCC.
See also
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-calculusSource
- 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