← Will Donnelly

Church Encoding is the mechanism by which literal data can be encoded as terms of the lambda calculus. This shows that it's possible to represent arbitrary literal data, but how compact can such a representation be?

Binary Lambda Calculus is a binary encoding of the lambda calculus. The number of bits required to encode some term serves as a pretty good measure of the complexity of that term. As a refresher, the encoding works as follows:

λM    = 00 M         (each abstraction 'costs' 2 bits)
(M N) = 01 M N       (application also 'costs' 2 bits)
0     = 10           (variable reference 'costs' 2+k bits)
1     = 110            (...where k is the de Brujin index)
k     = 1...0          (...of that variable's binding)

Given this cost model, how efficiently can literal data be encoded?

Representing Large Constants as K-Ary Numerals

The "standard" representation of the natural numbers is basically a higher-order encoding of Peano numerals:

0 = λfλx.x
1 = λfλx.(f x)
N = λfλx.(f (... (f x))

This is a unary encoding because it has only the one operation f to apply. It can be made K-ary in several ways, but the one that I like best moves the 'zero value' argument to the front and treats each 'constructor' as a multiplication by N and the addition of a new digit. Thus for binary we have:

0 = ''    = λzλfλt.z               -- 10 bits
1 = '1'   = λzλfλt.(t z)           -- 14 bits
2 = '10'  = λzλfλt.(f (t z))       -- 19 bits
3 = '11'  = λzλfλt.(t (t z))       -- 18 bits
4 = '100' = λzλfλt.(f (f (t z)))   -- 24 bits
5 = '101' = λzλfλt.(t (f (t z)))   -- 23 bits
6 = '110' = λzλfλt.(f (t (t z)))   -- 23 bits
7 = '111' = λzλfλt.(t (t (t z)))   -- 22 bits

The extension to ternary or quaternary numerals should be self- evident. Under the Binary Lambda Calculus cost model, the zero value encodes to 10 bits and each additional digit adds ~4.5 more bits (on average assuming uniform distribution), so a given natural number N encodes to 10 + 4.5*log2(N) bits on average. The same scheme can be extended to arbitrary bases:

Binary:      10 + 4.5*log(N)/log(2)
Ternary:     13 + 5.0*log(N)/log(3)
Quaternary:  16 + 5.5*log(N)/log(4)
Octal:       28 + 7.5*log(N)/log(8)
Decimal:     34 + 8.5*log(N)/log(10)
Hexadecimal: 52 + 11.5*log(N)/log(16)
K-Ary:       4 + 3*K + (3.5 + 0.5*K)*log(N)/log(K)

Suppose that we have 1kiB of data to encode. Thus log2(N) = 1024, and we have the following encoded lengths:

Binary:      10 + 4.5*1024           = 4618 bits (4.5x)
Ternary:     13 + 5.0*1024/log2(3)   = 3243 bits (3.2x)
Quaternary:  16 + 5.5*1024/log2(4)   = 2832 bits (2.8x)
7-Ary:       25 + 7.0*1024/log2(7)   = 2578 bits (2.5x)
Octal:       28 + 7.5*1024/log2(8)   = 2588 bits (2.5x)
Decimal:     34 + 8.5*1024/log2(10)  = 2654 bits (2.6x)
Hexadecimal: 52 + 11.5*1024/log2(16) = 2996 bits (2.9x)

In the limit as N becomes increasingly large a 7-ary encoding is the optimal balance between the unary cost of indexing more and more constructors and the 1/log2(K) factor reducing the length of the description.

However for practical purposes the octal encoding is only 0.2% worse and maps more cleanly to bits and bytes.

Caveats

Note that these specific numbers only holds for the Binary Lambda Calculus encoding, or more generally a cost model where the marginal cost of each additional variable is constant.

Even more importantly, this analysis all assumes that N is large enough that the constant factors become negligible. Specifying eight distinct constructor functions to convert an octal representation into any other form is going to take a fair number of bits no matter how clever you get. So what about the numbers small enough that those assumptions don't hold?

Encoding Small Naturals

Let's specifically consider the integers 0 to 256. How compactly can these exact values be represented?

In the "Large N" case we could be flexible about what constitutes a "representation" because converting from one data representation to another is generally a constant factor overhead. But here we'll be dealing with encoded lengths in the hundreds of bits at most, so we don't have that luxury. So specifically the question is: What is the most compact encoding of each integer from 0 to 256 as lambda calculus terms equivalent to a Church Numeral?

I've made a (non-exhaustive) list of strategies by which some numeral could be encoded:

I wrote an exhaustive search (modulo a couple obvious heuristics) to find the shortest way to express each number from 0 to 256 using any of these approaches to combine other integers.

I have not proven that my results are the shortest possible representations of each integer, or even that they're all correct in the first place (I could have made a dumb typo somewhere in the search code), but I don't expect anybody to need copy-pastable representations of small Church Numerals without being in a position to double- check the values anyway.

As a concrete example, the largest value is 239 (at 132 bits) using the binary shift-and-increment encoding atop a base value of 3:

((λiλs.(i (s (i (s (i (s (i (s (s (i (s λfλx.(f (f (f x))))))))))))))
  λnλfλx.(f ((n f) x)))
  λnλfλx.((n f) ((n f) x)))
If you're interested, here is the table of all integer encodings from 0 to 256
Value Size Strategy Encoding
0 6 bits naive λfλx.x
1 11 bits naive λfλx.(f x)
2 16 bits naive λfλx.(f (f x))
3 21 bits naive λfλx.(f (f (f x)))
4 26 bits naive λfλx.(f (f (f (f x))))
5 31 bits naive λfλx.(f (f (f (f (f x)))))
6 36 bits naive λfλx.(f (f (f (f (f (f x))))))
7 41 bits naive λfλx.(f (f (f (f (f (f (f x)))))))
8 39 bits exp (λfλx.(f (f (f x))) λfλx.(f (f x)))
9 39 bits exp (λfλx.(f (f x)) λfλx.(f (f (f x))))
10 55 bits mul λf.(λfλx.(f (f x)) (λfλx.(f (f (f (f (f x))))) f))
11 61 bits naive λfλx.(f (f (f (f (f (f (f (f (f (f (f x)))))))))))
12 55 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f x)))) f))
13 71 bits naive λfλx.(f (f (f (f (f (f (f (f (f (f (f (f (f x)))))))))))))
14 65 bits mul λf.(λfλx.(f (f x)) (λfλx.(f (f (f (f (f (f (f x))))))) f))
15 60 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f (f x))))) f))
16 44 bits exp (λfλx.(f (f (f (f x)))) λfλx.(f (f x)))
17 62 bits addconst λfλx.(f ((λfλx.(f (f (f (f x)))) λfλx.(f (f x))) f x))
18 63 bits mul λf.(λfλx.(f (f x)) ((λfλx.(f (f x)) λfλx.(f (f (f x)))) f))
19 72 bits addconst λfλx.(f (f (f ((λfλx.(f (f (f (f x)))) λfλx.(f (f x))) f x))))
20 65 bits mul λf.(λfλx.(f (f (f (f x)))) (λfλx.(f (f (f (f (f x))))) f))
21 70 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f (f (f (f x))))))) f))
22 85 bits mul λf.(λfλx.(f (f x)) (λfλx.(f (f (f (f (f (f (f (f (f (f (f x))))))))))) f))
23 92 bits addconst λfλx.(f (f (f (f (f (f (f ((λfλx.(f (f (f (f x)))) λfλx.(f (f x))) f x))))))))
24 68 bits mul λf.(λfλx.(f (f (f x))) ((λfλx.(f (f (f x))) λfλx.(f (f x))) f))
25 49 bits exp (λfλx.(f (f x)) λfλx.(f (f (f (f (f x))))))
26 67 bits addconst λfλx.(f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f x)))))) f x))
27 31 bits selfexp (λn.(n n) λfλx.(f (f (f x))))
28 49 bits addconst λfλx.(f ((λn.(n n) λfλx.(f (f (f x)))) f x))
29 54 bits addconst λfλx.(f (f ((λn.(n n) λfλx.(f (f (f x)))) f x)))
30 59 bits addconst λfλx.(f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))))
31 64 bits addconst λfλx.(f (f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x)))))
32 49 bits exp (λfλx.(f (f (f (f (f x))))) λfλx.(f (f x)))
33 67 bits addconst λfλx.(f ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f x))) f x))
34 72 bits addconst λfλx.(f (f ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f x))) f x)))
35 77 bits addconst λfλx.(f (f (f ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f x))) f x))))
36 54 bits exp (λfλx.(f (f x)) λfλx.(f (f (f (f (f (f x)))))))
37 72 bits addconst λfλx.(f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f x))))))) f x))
38 77 bits addconst λfλx.(f (f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f x))))))) f x)))
39 82 bits addconst λfλx.(f (f (f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f x))))))) f x))))
40 78 bits mul λf.(λfλx.(f (f (f (f (f x))))) ((λfλx.(f (f (f x))) λfλx.(f (f x))) f))
41 92 bits addconst λfλx.(f (f (f (f (f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f x))))))) f x))))))
42 85 bits mul λf.(λfλx.(f (f (f (f (f (f x)))))) (λfλx.(f (f (f (f (f (f (f x))))))) f))
43 95 bits add λfλx.(((λfλx.(f (f (f (f x)))) λfλx.(f (f x))) f) (((λn.(n n) λfλx.(f (f (f x)))) f) x))
44 95 bits mul λf.(λfλx.(f (f (f (f x)))) (λfλx.(f (f (f (f (f (f (f (f (f (f (f x))))))))))) f))
45 78 bits mul λf.(λfλx.(f (f (f (f (f x))))) ((λfλx.(f (f x)) λfλx.(f (f (f x)))) f))
46 96 bits addconst λfλx.(f (λf.(λfλx.(f (f (f (f (f x))))) ((λfλx.(f (f x)) λfλx.(f (f (f x)))) f)) f x))
47 101 bits addconst λfλx.(f (f (λf.(λfλx.(f (f (f (f (f x))))) ((λfλx.(f (f x)) λfλx.(f (f (f x)))) f)) f x)))
48 73 bits mul λf.(λfλx.(f (f (f x))) ((λfλx.(f (f (f (f x)))) λfλx.(f (f x))) f))
49 59 bits exp (λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f x))))))))
50 73 bits mul λf.(λfλx.(f (f x)) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f x)))))) f))
51 82 bits addconst λfλx.(f (f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f x)))))))) f x)))
52 87 bits addconst λfλx.(f (f (f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f x)))))))) f x))))
53 92 bits addconst λfλx.(f (f (f (f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f x)))))))) f x)))))
54 55 bits mul λf.(λfλx.(f (f x)) ((λn.(n n) λfλx.(f (f (f x)))) f))
55 73 bits addconst λfλx.(f (λf.(λfλx.(f (f x)) ((λn.(n n) λfλx.(f (f (f x)))) f)) f x))
56 73 bits mul λf.(λfλx.(f (f x)) (λfλx.(f ((λn.(n n) λfλx.(f (f (f x)))) f x)) f))
57 83 bits addconst λfλx.(f (f (f (λf.(λfλx.(f (f x)) ((λn.(n n) λfλx.(f (f (f x)))) f)) f x))))
58 78 bits mul λf.(λfλx.(f (f x)) (λfλx.(f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))) f))
59 93 bits addconst λfλx.(f (f (f (f (f (λf.(λfλx.(f (f x)) ((λn.(n n) λfλx.(f (f (f x)))) f)) f x))))))
60 83 bits mul λf.(λfλx.(f (f x)) (λfλx.(f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x)))) f))
61 101 bits addconst λfλx.(f (λf.(λfλx.(f (f x)) (λfλx.(f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x)))) f)) f x))
62 88 bits mul λf.(λfλx.(f (f x)) (λfλx.(f (f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))))) f))
63 88 bits mul λf.(λfλx.(f (f (f (f (f (f (f x))))))) ((λfλx.(f (f x)) λfλx.(f (f (f x)))) f))
64 49 bits exp (λfλx.(f (f (f x))) λfλx.(f (f (f (f x)))))
65 67 bits addconst λfλx.(f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f x))
66 72 bits addconst λfλx.(f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f x)))
67 77 bits addconst λfλx.(f (f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f x))))
68 82 bits addconst λfλx.(f (f (f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f x)))))
69 87 bits addconst λfλx.(f (f (f (f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f x))))))
70 92 bits addconst λfλx.(f (f (f (f (f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f x)))))))
71 97 bits addconst λfλx.(f (f (f (f (f (f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f x))))))))
72 78 bits mul λf.(λfλx.(f (f x)) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f x))))))) f))
73 96 bits addconst λfλx.(f (λf.(λfλx.(f (f x)) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f x))))))) f)) f x))
74 96 bits mul λf.(λfλx.(f (f x)) (λfλx.(f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f x))))))) f x)) f))
75 78 bits mul λf.(λfλx.(f (f (f x))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f x)))))) f))
76 96 bits addconst λfλx.(f (λf.(λfλx.(f (f (f x))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f x)))))) f)) f x))
77 101 bits addconst λfλx.(f (f (λf.(λfλx.(f (f (f x))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f x)))))) f)) f x)))
78 96 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f x)))))) f x)) f))
79 111 bits addconst λfλx.(f (f (f (f (λf.(λfλx.(f (f (f x))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f x)))))) f)) f x)))))
80 83 bits mul λf.(λfλx.(f (f (f (f (f x))))) ((λfλx.(f (f (f (f x)))) λfλx.(f (f x))) f))
81 49 bits exp (λfλx.(f (f (f (f x)))) λfλx.(f (f (f x))))
82 67 bits addconst λfλx.(f ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f x))
83 72 bits addconst λfλx.(f (f ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f x)))
84 77 bits addconst λfλx.(f (f (f ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f x))))
85 82 bits addconst λfλx.(f (f (f (f ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f x)))))
86 87 bits addconst λfλx.(f (f (f (f (f ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f x))))))
87 83 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))) f))
88 97 bits addconst λfλx.(f (f (f (f (f (f (f ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f x))))))))
89 102 bits addconst λfλx.(f (f (f (f (f (f (f (f ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f x)))))))))
90 88 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x)))) f))
91 100 bits add λfλx.(((λn.(n n) λfλx.(f (f (f x)))) f) (((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f) x))
92 111 bits addconst λfλx.(f (f (λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x)))) f)) f x)))
93 93 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))))) f))
94 111 bits addconst λfλx.(f (λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))))) f)) f x))
95 111 bits mul λf.(λfλx.(f (f (f (f (f x))))) (λfλx.(f (f (f ((λfλx.(f (f (f (f x)))) λfλx.(f (f x))) f x)))) f))
96 78 bits mul λf.(λfλx.(f (f (f x))) ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f x))) f))
97 96 bits addconst λfλx.(f (λf.(λfλx.(f (f (f x))) ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f x))) f)) f x))
98 83 bits mul λf.(λfλx.(f (f x)) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f x)))))))) f))
99 96 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f x))) f x)) f))
100 73 bits exp (λfλx.(f (f x)) λf.(λfλx.(f (f x)) (λfλx.(f (f (f (f (f x))))) f)))
101 91 bits addconst λfλx.(f ((λfλx.(f (f x)) λf.(λfλx.(f (f x)) (λfλx.(f (f (f (f (f x))))) f))) f x))
102 96 bits addconst λfλx.(f (f ((λfλx.(f (f x)) λf.(λfλx.(f (f x)) (λfλx.(f (f (f (f (f x))))) f))) f x)))
103 101 bits addconst λfλx.(f (f (f ((λfλx.(f (f x)) λf.(λfλx.(f (f x)) (λfλx.(f (f (f (f (f x))))) f))) f x))))
104 101 bits mul λf.(λfλx.(f (f (f (f x)))) (λfλx.(f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f x)))))) f x)) f))
105 106 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f x))) f x)))) f))
106 113 bits binary ((λiλs.(s (i (s (s (i (s (s λfλx.(f (f (f x)))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
107 118 bits binary ((λiλs.(i (s (i (s (s (i (s (s λfλx.(f (f (f x))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
108 65 bits mul λf.(λfλx.(f (f (f (f x)))) ((λn.(n n) λfλx.(f (f (f x)))) f))
109 83 bits addconst λfλx.(f (λf.(λfλx.(f (f (f (f x)))) ((λn.(n n) λfλx.(f (f (f x)))) f)) f x))
110 88 bits addconst λfλx.(f (f (λf.(λfλx.(f (f (f (f x)))) ((λn.(n n) λfλx.(f (f (f x)))) f)) f x)))
111 93 bits addconst λfλx.(f (f (f (λf.(λfλx.(f (f (f (f x)))) ((λn.(n n) λfλx.(f (f (f x)))) f)) f x))))
112 83 bits mul λf.(λfλx.(f (f (f (f x)))) (λfλx.(f ((λn.(n n) λfλx.(f (f (f x)))) f x)) f))
113 101 bits addconst λfλx.(f (λf.(λfλx.(f (f (f (f x)))) (λfλx.(f ((λn.(n n) λfλx.(f (f (f x)))) f x)) f)) f x))
114 106 bits addconst λfλx.(f (f (λf.(λfλx.(f (f (f (f x)))) (λfλx.(f ((λn.(n n) λfλx.(f (f (f x)))) f x)) f)) f x)))
115 111 bits addconst λfλx.(f (f (f (λf.(λfλx.(f (f (f (f x)))) (λfλx.(f ((λn.(n n) λfλx.(f (f (f x)))) f x)) f)) f x))))
116 88 bits mul λf.(λfλx.(f (f (f (f x)))) (λfλx.(f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))) f))
117 106 bits addconst λfλx.(f (λf.(λfλx.(f (f (f (f x)))) (λfλx.(f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))) f)) f x))
118 111 bits addconst λfλx.(f (f (λf.(λfλx.(f (f (f (f x)))) (λfλx.(f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))) f)) f x)))
119 111 bits mul λf.(λfλx.(f (f (f (f (f (f (f x))))))) (λfλx.(f ((λfλx.(f (f (f (f x)))) λfλx.(f (f x))) f x)) f))
120 93 bits mul λf.(λfλx.(f (f (f (f x)))) (λfλx.(f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x)))) f))
121 79 bits exp (λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f (f (f (f (f x))))))))))))
122 97 bits addconst λfλx.(f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f (f (f (f (f x)))))))))))) f x))
123 102 bits addconst λfλx.(f (f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f (f (f (f (f x)))))))))))) f x)))
124 98 bits mul λf.(λfλx.(f (f (f (f x)))) (λfλx.(f (f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))))) f))
125 54 bits exp (λfλx.(f (f (f x))) λfλx.(f (f (f (f (f x))))))
126 72 bits addconst λfλx.(f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f x)))))) f x))
127 77 bits addconst λfλx.(f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f x)))))) f x)))
128 59 bits exp (λfλx.(f (f (f (f (f (f (f x))))))) λfλx.(f (f x)))
129 77 bits addconst λfλx.(f ((λfλx.(f (f (f (f (f (f (f x))))))) λfλx.(f (f x))) f x))
130 82 bits addconst λfλx.(f (f ((λfλx.(f (f (f (f (f (f (f x))))))) λfλx.(f (f x))) f x)))
131 87 bits addconst λfλx.(f (f (f ((λfλx.(f (f (f (f (f (f (f x))))))) λfλx.(f (f x))) f x))))
132 92 bits addconst λfλx.(f (f (f (f ((λfλx.(f (f (f (f (f (f (f x))))))) λfλx.(f (f x))) f x)))))
133 97 bits addconst λfλx.(f (f (f (f (f ((λfλx.(f (f (f (f (f (f (f x))))))) λfλx.(f (f x))) f x))))))
134 101 bits mul λf.(λfλx.(f (f x)) (λfλx.(f (f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f x)))) f))
135 70 bits mul λf.(λfλx.(f (f (f (f (f x))))) ((λn.(n n) λfλx.(f (f (f x)))) f))
136 88 bits addconst λfλx.(f (λf.(λfλx.(f (f (f (f (f x))))) ((λn.(n n) λfλx.(f (f (f x)))) f)) f x))
137 93 bits addconst λfλx.(f (f (λf.(λfλx.(f (f (f (f (f x))))) ((λn.(n n) λfλx.(f (f (f x)))) f)) f x)))
138 98 bits addconst λfλx.(f (f (f (λf.(λfλx.(f (f (f (f (f x))))) ((λn.(n n) λfλx.(f (f (f x)))) f)) f x))))
139 103 bits addconst λfλx.(f (f (f (f (λf.(λfλx.(f (f (f (f (f x))))) ((λn.(n n) λfλx.(f (f (f x)))) f)) f x)))))
140 88 bits mul λf.(λfλx.(f (f (f (f (f x))))) (λfλx.(f ((λn.(n n) λfλx.(f (f (f x)))) f x)) f))
141 106 bits addconst λfλx.(f (λf.(λfλx.(f (f (f (f (f x))))) (λfλx.(f ((λn.(n n) λfλx.(f (f (f x)))) f x)) f)) f x))
142 111 bits addconst λfλx.(f (f (λf.(λfλx.(f (f (f (f (f x))))) (λfλx.(f ((λn.(n n) λfλx.(f (f (f x)))) f x)) f)) f x)))
143 116 bits addconst λfλx.(f (f (f (λf.(λfλx.(f (f (f (f (f x))))) (λfλx.(f ((λn.(n n) λfλx.(f (f (f x)))) f x)) f)) f x))))
144 73 bits exp (λfλx.(f (f x)) λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f x)))) f)))
145 91 bits addconst λfλx.(f ((λfλx.(f (f x)) λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f x)))) f))) f x))
146 96 bits addconst λfλx.(f (f ((λfλx.(f (f x)) λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f x)))) f))) f x)))
147 88 bits mul λf.(λfλx.(f (f (f x))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f x)))))))) f))
148 106 bits addconst λfλx.(f (λf.(λfλx.(f (f (f x))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f x)))))))) f)) f x))
149 111 bits addconst λfλx.(f (f (λf.(λfλx.(f (f (f x))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f x)))))))) f)) f x)))
150 93 bits mul λf.(λfλx.(f (f (f (f (f (f x)))))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f x)))))) f))
151 111 bits addconst λfλx.(f (λf.(λfλx.(f (f (f (f (f (f x)))))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f x)))))) f)) f x))
152 105 bits add λfλx.(((λn.(n n) λfλx.(f (f (f x)))) f) (((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f x)))))) f) x))
153 109 bits mul λf.((λfλx.(f (f x)) λfλx.(f (f (f x)))) (λfλx.(f ((λfλx.(f (f (f (f x)))) λfλx.(f (f x))) f x)) f))
154 117 bits binary ((λiλs.(s (i (s (s (i (s (i (s (s λfλx.(f (f x))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
155 103 bits mul λf.(λfλx.(f (f (f (f (f x))))) (λfλx.(f (f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))))) f))
156 111 bits mul λf.(λfλx.(f (f (f (f (f (f x)))))) (λfλx.(f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f x)))))) f x)) f))
157 122 bits binary ((λiλs.(i (s (s (i (s (i (s (i (s (s λfλx.(f (f x)))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
158 122 bits binary ((λiλs.(s (i (s (i (s (i (s (i (s (s λfλx.(f (f x)))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
159 121 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f x)))))))) f x))))) f))
160 88 bits mul λf.(λfλx.(f (f (f (f (f x))))) ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f x))) f))
161 106 bits addconst λfλx.(f (λf.(λfλx.(f (f (f (f (f x))))) ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f x))) f)) f x))
162 73 bits mul λf.(λfλx.(f (f x)) ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f))
163 91 bits addconst λfλx.(f (λf.(λfλx.(f (f x)) ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f)) f x))
164 91 bits mul λf.(λfλx.(f (f x)) (λfλx.(f ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f x)) f))
165 101 bits addconst λfλx.(f (f (f (λf.(λfλx.(f (f x)) ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f)) f x))))
166 96 bits mul λf.(λfλx.(f (f x)) (λfλx.(f (f ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f x))) f))
167 111 bits addconst λfλx.(f (f (f (f (f (λf.(λfλx.(f (f x)) ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f)) f x))))))
168 93 bits mul λf.(λfλx.(f (f (f (f (f (f x)))))) (λfλx.(f ((λn.(n n) λfλx.(f (f (f x)))) f x)) f))
169 89 bits exp (λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f (f (f (f (f (f (f x))))))))))))))
170 106 bits mul λf.(λfλx.(f (f x)) (λfλx.(f (f (f (f ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f x))))) f))
171 112 bits addconst λfλx.(f (f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f (f (f (f (f (f (f x)))))))))))))) f x)))
172 111 bits mul λf.(λfλx.(f (f x)) (λfλx.(f (f (f (f (f ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f x)))))) f))
173 122 bits addconst λfλx.(f (f (f (f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f (f (f (f (f (f (f x)))))))))))))) f x)))))
174 98 bits mul λf.(λfλx.(f (f (f (f (f (f x)))))) (λfλx.(f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))) f))
175 98 bits mul λf.(λfλx.(f (f (f (f (f (f (f x))))))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f x)))))) f))
176 112 bits binary ((λiλs.(s (s (s (s (i (s (i (s λfλx.(f (f x)))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
177 117 bits binary ((λiλs.(i (s (s (s (s (i (s (i (s λfλx.(f (f x))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
178 117 bits binary ((λiλs.(s (i (s (s (s (i (s (i (s λfλx.(f (f x))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
179 122 bits binary ((λiλs.(i (s (i (s (s (s (i (s (i (s λfλx.(f (f x)))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
180 93 bits mul λf.(λfλx.(f (f (f (f (f x))))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f x))))))) f))
181 111 bits addconst λfλx.(f (λf.(λfλx.(f (f (f (f (f x))))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f x))))))) f)) f x))
182 116 bits addconst λfλx.(f (f (λf.(λfλx.(f (f (f (f (f x))))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f x))))))) f)) f x)))
183 121 bits addconst λfλx.(f (f (f (λf.(λfλx.(f (f (f (f (f x))))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f x))))))) f)) f x))))
184 117 bits binary ((λiλs.(s (s (s (i (s (i (s (i (s λfλx.(f (f x))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
185 111 bits mul λf.(λfλx.(f (f (f (f (f x))))) (λfλx.(f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f x))))))) f x)) f))
186 108 bits mul λf.(λfλx.(f (f (f (f (f (f x)))))) (λfλx.(f (f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))))) f))
187 126 bits addconst λfλx.(f (λf.(λfλx.(f (f (f (f (f (f x)))))) (λfλx.(f (f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))))) f)) f x))
188 122 bits binary ((λiλs.(s (s (i (s (i (s (i (s (i (s λfλx.(f (f x)))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
189 80 bits mul λf.(λfλx.(f (f (f (f (f (f (f x))))))) ((λn.(n n) λfλx.(f (f (f x)))) f))
190 98 bits addconst λfλx.(f (λf.(λfλx.(f (f (f (f (f (f (f x))))))) ((λn.(n n) λfλx.(f (f (f x)))) f)) f x))
191 103 bits addconst λfλx.(f (f (λf.(λfλx.(f (f (f (f (f (f (f x))))))) ((λn.(n n) λfλx.(f (f (f x)))) f)) f x)))
192 78 bits mul λf.(λfλx.(f (f (f x))) ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f))
193 96 bits addconst λfλx.(f (λf.(λfλx.(f (f (f x))) ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f)) f x))
194 101 bits addconst λfλx.(f (f (λf.(λfλx.(f (f (f x))) ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f)) f x)))
195 96 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f x)) f))
196 83 bits exp (λfλx.(f (f x)) λf.(λfλx.(f (f x)) (λfλx.(f (f (f (f (f (f (f x))))))) f)))
197 101 bits addconst λfλx.(f ((λfλx.(f (f x)) λf.(λfλx.(f (f x)) (λfλx.(f (f (f (f (f (f (f x))))))) f))) f x))
198 101 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f x))) f))
199 111 bits addconst λfλx.(f (f (f ((λfλx.(f (f x)) λf.(λfλx.(f (f x)) (λfλx.(f (f (f (f (f (f (f x))))))) f))) f x))))
200 96 bits mul λf.((λfλx.(f (f (f x))) λfλx.(f (f x))) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f x)))))) f))
201 106 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f x)))) f))
202 115 bits mul λf.(λfλx.(f (f x)) (λfλx.(f ((λfλx.(f (f x)) λf.(λfλx.(f (f x)) (λfλx.(f (f (f (f (f x))))) f))) f x)) f))
203 103 bits mul λf.(λfλx.(f (f (f (f (f (f (f x))))))) (λfλx.(f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))) f))
204 111 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f x))))) f))
205 122 bits binary ((λiλs.(i (s (s (i (s (i (s (s (s λfλx.(f (f (f x)))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
206 122 bits binary ((λiλs.(s (i (s (i (s (i (s (s (s λfλx.(f (f (f x)))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
207 116 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f x))))) f x)))))) f))
208 112 bits binary ((λiλs.(s (s (s (s (i (s (s λfλx.(f (f (f x)))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
209 117 bits binary ((λiλs.(i (s (s (s (s (i (s (s λfλx.(f (f (f x))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
210 108 bits mul λf.(λfλx.(f (f (f (f (f (f (f x))))))) (λfλx.(f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x)))) f))
211 122 bits binary ((λiλs.(i (s (i (s (s (s (i (s (s λfλx.(f (f (f x)))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
212 117 bits binary ((λiλs.(s (s (i (s (s (i (s (s λfλx.(f (f (f x))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
213 122 bits binary ((λiλs.(i (s (s (i (s (s (i (s (s λfλx.(f (f (f x)))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
214 122 bits binary ((λiλs.(s (i (s (i (s (s (i (s (s λfλx.(f (f (f x)))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
215 127 bits binary ((λiλs.(i (s (i (s (i (s (s (i (s (s λfλx.(f (f (f x))))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
216 59 bits exp (λfλx.(f (f (f x))) λfλx.(f (f (f (f (f (f x)))))))
217 77 bits addconst λfλx.(f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f (f x))))))) f x))
218 82 bits addconst λfλx.(f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f (f x))))))) f x)))
219 87 bits addconst λfλx.(f (f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f (f x))))))) f x))))
220 92 bits addconst λfλx.(f (f (f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f (f x))))))) f x)))))
221 97 bits addconst λfλx.(f (f (f (f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f (f x))))))) f x))))))
222 102 bits addconst λfλx.(f (f (f (f (f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f (f x))))))) f x)))))))
223 107 bits addconst λfλx.(f (f (f (f (f (f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f (f x))))))) f x))))))))
224 96 bits mul λf.((λfλx.(f (f (f x))) λfλx.(f (f x))) (λfλx.(f ((λn.(n n) λfλx.(f (f (f x)))) f x)) f))
225 78 bits exp (λfλx.(f (f x)) λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f (f x))))) f)))
226 96 bits addconst λfλx.(f ((λfλx.(f (f x)) λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f (f x))))) f))) f x))
227 101 bits addconst λfλx.(f (f ((λfλx.(f (f x)) λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f (f x))))) f))) f x)))
228 106 bits addconst λfλx.(f (f (f ((λfλx.(f (f x)) λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f (f x))))) f))) f x))))
229 111 bits addconst λfλx.(f (f (f (f ((λfλx.(f (f x)) λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f (f x))))) f))) f x)))))
230 116 bits addconst λfλx.(f (f (f (f (f ((λfλx.(f (f x)) λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f (f x))))) f))) f x))))))
231 116 bits mul λf.(λfλx.(f (f (f (f (f (f (f x))))))) (λfλx.(f ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f x))) f x)) f))
232 101 bits mul λf.((λfλx.(f (f (f x))) λfλx.(f (f x))) (λfλx.(f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))) f))
233 119 bits addconst λfλx.(f (λf.((λfλx.(f (f (f x))) λfλx.(f (f x))) (λfλx.(f (f ((λn.(n n) λfλx.(f (f (f x)))) f x))) f)) f x))
234 114 bits mul λf.((λfλx.(f (f x)) λfλx.(f (f (f x)))) (λfλx.(f ((λfλx.(f (f x)) λfλx.(f (f (f (f (f x)))))) f x)) f))
235 127 bits binary ((λiλs.(i (s (i (s (s (i (s (s (i (s λfλx.(f (f (f x))))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
236 122 bits binary ((λiλs.(s (s (i (s (i (s (s (i (s λfλx.(f (f (f x)))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
237 127 bits binary ((λiλs.(i (s (s (i (s (i (s (s (i (s λfλx.(f (f (f x))))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
238 121 bits mul λf.(λfλx.(f (f (f (f (f (f (f x))))))) (λfλx.(f (f ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f x))) f x))) f))
239 132 bits binary ((λiλs.(i (s (i (s (i (s (i (s (s (i (s λfλx.(f (f (f x)))))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
240 106 bits mul λf.((λfλx.(f (f (f x))) λfλx.(f (f x))) (λfλx.(f (f (f ((λn.(n n) λfλx.(f (f (f x)))) f x)))) f))
241 122 bits binary ((λiλs.(i (s (s (s (s (i (s (i (s λfλx.(f (f (f x)))))))))))) λnλfλx.(f ((n f) x))) λnλfλx.((n f) ((n f) x)))
242 103 bits mul λf.(λfλx.(f (f x)) ((λfλx.(f (f x)) λfλx.(f (f (f (f (f (f (f (f (f (f (f x)))))))))))) f))
243 54 bits exp (λfλx.(f (f (f (f (f x))))) λfλx.(f (f (f x))))
244 72 bits addconst λfλx.(f ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f (f x)))) f x))
245 77 bits addconst λfλx.(f (f ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f (f x)))) f x)))
246 82 bits addconst λfλx.(f (f (f ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f (f x)))) f x))))
247 87 bits addconst λfλx.(f (f (f (f ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f (f x)))) f x)))))
248 92 bits addconst λfλx.(f (f (f (f (f ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f (f x)))) f x))))))
249 97 bits addconst λfλx.(f (f (f (f (f (f ((λfλx.(f (f (f (f (f x))))) λfλx.(f (f (f x)))) f x)))))))
250 78 bits mul λf.(λfλx.(f (f x)) ((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f x)))))) f))
251 96 bits addconst λfλx.(f (λf.(λfλx.(f (f x)) ((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f x)))))) f)) f x))
252 96 bits mul λf.(λfλx.(f (f x)) (λfλx.(f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f x)))))) f x)) f))
253 106 bits addconst λfλx.(f (f (f (λf.(λfλx.(f (f x)) ((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f x)))))) f)) f x))))
254 101 bits mul λf.(λfλx.(f (f x)) (λfλx.(f (f ((λfλx.(f (f (f x))) λfλx.(f (f (f (f (f x)))))) f x))) f))
255 111 bits mul λf.(λfλx.(f (f (f x))) (λfλx.(f (f (f (f ((λfλx.(f (f (f (f x)))) λfλx.(f (f (f x)))) f x))))) f))
256 36 bits selfexp (λn.(n n) λfλx.(f (f (f (f x)))))


A plot of the size in encoded bits of each integer from 0 to 256

Perhaps the most interesting thing about the table of encodings is that while the lengths generally follow a sort of "fractal sawtooth" pattern it isn't quite as regular as I would have expected. This may be an indicator that there are other strategies that I've overlooked which would have brought some of the outliers into line.

Conclusion

Large amounts of literal data can be encoded in the Binary Lambda Calculus with an overhead of 2.5 bits-per-bit, which is either pretty great or absolutely terrible depending on your expectations.

Smaller constants are much more challenging from an efficiency perspective because constant factors matter a lot more, but every byte value can be encoded as a Church Numeral in 132 bits at most, with the average being 90 bits.

If any of this ever actually matters though, your best option will likely be defining some other virtual machine using the Lambda Calculus and then representing program and data alike as a large constant data blob.