A riff on Cook-Levin Chapters

Chapter 29

Six expressions, composed

“The Boolean view” ran the machine as four Boolean functions of six bits, composed over six steps, with the input and the program fixed. Cook and Levin's move is to stop fixing the program. Keep the same four functions and the same six steps, give the program's twelve bits names in place of values, v1 to v12, and what comes out of the sixth step is a formula where it was a bit. This chapter does that, turns the formula into the question a puzzle asks, and then names the two ways of composing that the rest of this part follows.

The program unknown §

Line k's opcode bits are v(2k − 1) and v(2k), so instruction k is 2·v(2k − 1) + v(2k), numbered as in “Cook-Levin as SAT”. The input stays fixed, so hb and lb start as constants and both flags as 0. python/steps.py composes the six steps with the same Python functions, now given formulas: a constant folds away, and an equal subformula is built once. The final accepted, for each input, has this many gates:

AX gates in accepted
0 41
1 91
2 145
3 148

For input 0 it is small enough to read:

   (NOT v1 AND v2)
OR (NOT (v1 AND NOT v2) AND NOT v3 AND v4)
OR (NOT ((v1 AND NOT v2) OR (v3 AND NOT v4)) AND NOT v5 AND v6)
OR (NOT ((v1 AND NOT v2) OR (v3 AND NOT v4) OR (v5 AND NOT v6)) AND NOT v7 AND v8)
OR (NOT ((v1 AND NOT v2) OR (v3 AND NOT v4) OR (v5 AND NOT v6) OR (v7 AND NOT v8)) AND NOT v9 AND v10)
OR (NOT ((v1 AND NOT v2) OR (v3 AND NOT v4) OR (v5 AND NOT v6) OR (v7 AND NOT v8) OR (v9 AND NOT v10)) AND NOT v11 AND v12)

Line k's bits 0 1 are zero, and 1 0 are decr. So the formula says: some line is zero, and every line before it is nada, zero or mod2. That is input 0's whole story, since AX starts at 0: zero accepts it, decr rejects it, and nada and mod2 leave it as it is. python/steps.py holds all four formulas to the interpreter on all 4,096 programs.

Composition §

Free the input's two bits as well. The first step reads them as hb and lb, both flags at 0, and line 1's two bits. Each later step reads the four bits the one before it wrote, and its own line's two bits. So the only bits that enter from outside the chain are the input's two and each line's two, six lines of them:

bits
the input, hb and lb at the start 2
the flags at the start 0 (both constant 0)
the program, two a line for six lines 12
the composed expression's variables 14

Everything else is internal: each step's gates, and of the 24 state bits the steps write, the 20 that a next step reads. After the sixth step, accepted is an expression accepted(n, v) of those 14 bits.

The question as one expression §

A puzzle asks whether a program decides L. That is one more expression, of the twelve program bits alone:

decides_L(v1, ..., v12) = AND over n in 0..3 of (accepted(n, v) == L(n))

L(n) is the constant 1 or 0, so each of the four comparisons is accepted(n, v) or its complement. The true points of decides_L are exactly the programs that decide L.

The payoff §

Expanded and simplified, decides_L is a sum of products over v1 to v12. src/boolean_main.c computes one for every problem: it finds the programs that decide L by running all 4,096, takes the prime implicants of that set, and covers it greedily, the product holding the most programs left taken first. Each product is written as its twelve bits, two a line of the program: 1 for v, 0 for its complement, and - for a bit the product leaves free. For {0, 2, 3} the expression is one product, the conjunction of 12 literals that spells its one program, zero, decr, decr, zero, decr, zero:

01 10 10 01 10 01

For {0, 3}, 12 programs take 8 products. The first is zero, decr, decr, decr, zero with line 6 free:

01 10 10 10 01 --
0- 01 10 10 10 01
01 10 10 00 10 01
01 10 00 10 10 01
01 0- 10 10 10 01
01 10 10 11 10 01
01 10 10 10 -1 01
01 10 10 10 0- 01

The counts for all 16, programs and products, from the same run:

code problem programs products
0 { } 844 87
1 {0} 681 41
2 {1} 303 32
3 {0, 1} 172 26
4 {2} 248 80
5 {0, 2} 883 121
6 {1, 2} 74 31
7 {0, 1, 2} 13 8
8 {3} 63 30
9 {0, 3} 12 8
10 {1, 3} 520 130
11 {0, 1, 3} 150 54
12 {2, 3} 13 9
13 {0, 2, 3} 1 1
14 {1, 2, 3} 15 10
15 {0, 1, 2, 3} 104 40

tests/test_boolean.c holds every problem's sum of products to run_progam, program by program, and the book's build holds the blocks here to boolean_main's output.

Two ways to compose §

The composing above ran all 4,096 programs, which a toy allows. For a real checker there are two ways to compose the step's expressions, and the rest of this part takes each in turn.

Naming is why Cook-Levin's formula stays polynomial in size: each step adds the same few clauses, whatever came before it.