A riff on Cook-Levin Chapters

Chapter 5

Programs as formal languages

Steve settled which programs decide which problems twice in Python: first by construction, then by running everything.

The analytical solution, 2023 §

The first, find_solutions_analytically, came on the machine's first day, 2023-03-11, in d6bb78f ("Add analytical solution."), stamped 13:02, and the stepper followed at 15:47 (ba9388a). The machine was smaller then. That commit has two opcodes, check (today's zero) and decr, programs of up to 8 instructions, and a brute-force search over the program numbers 0 to 255. Its solve (virtual_machine.py, lines 185 to 195) builds a problem's program directly: decr down to the least member, check, and the same for the rest.

    def solve(lang):
        if not lang:
            # The program can terminate to reject all further numbers
            return []

        program = []
        while lang[0] > 0:
            program.append("decr")
            lang = [i-1 for i in lang]

        return program + ["check"] + solve(lang[1:])

For each of the 16 problems it asserted the program shorter than 8 and ran it on all four inputs (assert f(x) == y). Later commits removed it; it is absent at 94c3d31.

The complete calculation §

Today's find_solutions (virtual_machine.py, lines 148 to 180) runs every program on every input and groups the programs by the problem each decides. The runs are run_progam's (lines 76 to 105), the plain interpreter: a loop over the six instructions, an if/elif on each opcode and on AX, the spec above as code. That is the complete calculation, 4,096 programs on 4 inputs, 16,384 runs. It also re-runs each problem's first program, 16 programs on 4 inputs, through the step polynomials of the second part (lines 178 to 180); the theorems here rest on the interpreter alone.

The theorems §

Theorem 1. Every one of the 16 problems is decided by some six-instruction program.

Proof by construction. On today's machine solve builds a program of six lines or fewer for 15 problems, and each decides its problem. For {0, 1, 2, 3} it builds seven, zero, decr, zero, decr, zero, decr, zero, one past the limit, and Python's report gives mod2, zero, decr, zero for it. tests/test_theorems.c runs the construction on every problem.

Proof by exhaustion. find_solutions runs all 4,096 programs on run_progam and asserts len(solutions[y]) > 0 for every problem y (lines 159 to 160).

Theorem 2. With nada, zero and decr alone, exactly 15 of the 16 problems are decided by a six-instruction program; {0, 1, 2, 3} needs mod2.

Proof. tests/test_theorems.c runs the 3^6 = 729 programs over nada, zero and decr and finds 15 problems, {0, 1, 2, 3} the one missing.

Theorem 3. The number of programs deciding each problem is the table's programs column, and the 16 counts sum to 4,096.

Proof. Each program is filed under the one problem it decides, so the counts partition the 4,096 programs. Python's report gives each count, and tests/golden/find_solutions.txt holds that report.

In the table, a problem's code is its inputs as bits: input i accepted sets bit i. A program's length is its instructions before its trailing nadas, and the shortest column is the least length among a problem's programs.

code problem programs shortest
0 { } 844 0
1 {0} 681 1
2 {1} 303 2
3 {0, 1} 172 3
4 {2} 248 3
5 {0, 2} 883 2
6 {1, 2} 74 4
7 {0, 1, 2} 13 5
8 {3} 63 4
9 {0, 3} 12 5
10 {1, 3} 520 3
11 {0, 1, 3} 150 4
12 {2, 3} 13 5
13 {0, 2, 3} 1 6
14 {1, 2, 3} 15 5
15 {0, 1, 2, 3} 104 4

Theorem 4. Each problem's shortest program has the length in the table's shortest column.

Proof. tests/test_theorems.c measures the length of all 4,096 programs, takes each problem's least, and holds the 16 to the table. The second part proves it again a different way: a SAT solver shows each length one line shorter impossible, with a proof a checker replays (“The solver's work, in full”).

The rare one §

Theorem 5. {0, 2, 3} is decided by exactly one program, and that program uses all six lines. It is program 1689:

zero
decr
decr
zero
decr
zero

Proof. Python's report gives {0, 2, 3} a count of 1 and this program as its example, and Theorem 3's search is exhaustive. solve builds the same program.

Input 0 is accepted on line 1. Input 1 drops to 0 on line 2 and is rejected by the decr on line 3. Input 2 reaches 0 on line 3 and is accepted on line 4. Input 3 reaches 0 on line 5 and is accepted on line 6. The problem skips 1, and the only way past it is to count down past it.

The common ones §

The empty problem, { }, has 844 programs, since any program that rejects every input will do, and {0, 2} has 883: mod2 then zero is two lines, and many longer programs reach the same verdicts.