A riff on Cook-Levin Chapters

Chapter 22

Nibble++

The toy is small enough to settle by listing every program, 4,096 of them. Nibble++ is the case n = 4 of “Nibble, sized”, with programs of any length: inputs 0 to 15, and 2^16 = 65,536 formal languages, the sets of inputs a program can decide. It is too big to list, and small enough to settle exactly by searching the machine's states. src/plus.c is its interpreter and src/census.c its census.

The machine §

Nibble++ keeps zero, decr and mod2 with their rules, and gives nada's code, 0, to a new opcode, half, which sets AX to AX >> 1, half of it rounded down. The four codes fill two bits exactly, so a line is still two bits.

nada can go because decr is a safe pad: a decr added after a program's last line keeps the formal language it decides. An input that has halted stays halted. An input still running at the old end was rejected there, and after the pad it is rejected too, by a decr that finds AX at 0 or at the new end; decr's only verdict is to reject. tests/test_census.c pads the shortest program of every formal language with decr to 32 lines and checks that each still decides its formal language.

mod2 stays for two reasons: the four codes fill the two bits exactly, and the machine stays continuous with the toy.

Searching states §

The longest shortest program has 28 lines, and there are 4^28 = 2^56 programs of that length. The census searches states instead. A state is the whole machine on all 16 inputs at once: each input's AX, or accepted, or rejected. Two programs that reach the same state have the same futures, so the search keeps each state once, at the length that first reaches it, and grows the states a line at a time, breadth first. A state's formal language is the set of its accepted inputs, so a formal language's shortest length is the first level where a state with that language appears, and following a state back to its parents gives a witness program. tests/test_census.c runs every witness on the interpreter, and checks the census against every program of up to 9 lines. The four censuses below took 0.67 seconds together, run by src/plus_book_main.c while this chapter was written:

Opcodes States reached Mean shortest length Longest
{zero, decr} 163838 22.00 31
{zero, decr, mod2} 262136 21.06 30
{zero, decr, half} 240401 19.77 30
{zero, decr, mod2, half} 374003 19.07 28

Each set decides every formal language. With all four, every formal language has a program of 28 lines or fewer, so each has a 32-line program of 64 bits, padded with decr. The formal languages by shortest length, with all four:

Shortest length Formal languages
0 1
1 1
2 3
3 7
4 16
5 34
6 72
7 126
8 212
9 331
10 494
11 707
12 991
13 1376
14 1898
15 2612
16 3599
17 4928
18 6555
19 8171
20 9158
21 8870
22 7164
23 4667
24 2368
25 896
26 237
27 39
28 3

What half and mod2 buy §

Five formal languages' shortest lengths under each set:

Formal language {zero, decr} {zero, decr, mod2} {zero, decr, half} {zero, decr, mod2, half}
{15} 16 16 8 8
{8, ..., 15} 23 12 5 5
{2, 3, 6, 7, 10, 11, 14, 15} 23 23 12 4
all but 13 30 30 28 28
all 16 31 4 5 3

half buys the most on formal languages with bit structure, since it moves a high bit of AX down to where zero and mod2 test it. Beside {zero, decr, mod2}, it takes {15} from 16 lines to 8, the top half from 12 to 5, and the inputs with bit 1 set from 23 to 4. The census's witnesses, with all four:

{15}: decr, half, decr, half, decr, half, decr, zero
{8, ..., 15}: half, half, half, decr, zero
{2, 3, 6, 7, 10, 11, 14, 15}: half, decr, mod2, zero
all 16: mod2, half, zero

mod2 folds every input onto 0 or 1, so it buys the most on large sets: all 16 inputs cost 31 lines with {zero, decr} and 4 with mod2 added. Both opcodes together buy little on the hardest formal languages, near-full sets with an isolated gap: everything but 13 costs 30 lines with {zero, decr, mod2} and 28 with all four. A formal language's cost tracks its boundaries, the places where membership flips between neighbours, more than its size: all 16 inputs cost 3 lines.

The hardest §

Three formal languages need 28 lines, the census's longest, against the toy's {0, 2, 3} and its six-line program 1689 (“Programs as formal languages”):

The formal languages of 28 lines Left out
{0, ..., 10, 12, 15} 11, 13, 14
{0, ..., 13, 15} 14
{0, ..., 12, 14, 15} 13

The census's witness for the last, all but 13:

zero
decr
zero
decr
zero
decr
zero
decr
zero
decr
zero
decr
zero
decr
zero
decr
zero
decr
zero
decr
zero
decr
zero
decr
zero
half
decr
zero

Inputs 0 to 12 take a zero each, with a decr between, 25 lines, because half and mod2 merge inputs: run while 12, 13 and 14 are all live, either gives 13 the same AX as a member, and from then on the two share every verdict. With 13, 14 and 15 left, half sends 13 to 0 and 14 and 15 to 1, so one decr rejects 13 and one zero accepts the pair.