A riff on Cook-Levin Chapters

Chapter 32

Six steps composed

src/compose.c substitutes the step polynomials into themselves six times (1146d43). The Roc port's step, from roc/Compose.roc:

roc/Compose.roc, the function after_step
## The state after step `index` (1 to 6), from the state before it: each
## step polynomial with its six variables replaced by the state's four
## polynomials and the instruction's two variables.
after_step : Stepper.State, U64 -> Stepper.State
after_step = |before, index| {
	replacements = [before.hb, before.lb, before.halted, before.accepted, BoolPoly.variable(2 * index), BoolPoly.variable(2 * index + 1)]
	step = Stepper.step_polynomials
	{
		hb: step.hb.substitute(replacements),
		lb: step.lb.substitute(replacements),
		halted: step.halted.substitute(replacements),
		accepted: step.accepted.substitute(replacements),
	}
}
input step 1 op1 step 2 op2 ... step 6 op6 input step 1 op1 step 2 op2 ... step 6 op6
Six steps, one substitution each

The sizes §

Each step's term counts, from roc/golden/composition.txt and out/tcc/compose_main run for this book:

step hb lb halted accepted
1 4 7 8 8
2 11 25 30 40
3 27 79 92 162
4 63 241 282 642
5 143 727 878 2,568
6 319 2,185 2,732 10,324

The final hb has degree 9 and its largest coefficient is 6; lb has degree 14 and 21; halted 14 and 20; accepted 14 and 63. Sixty-three is the largest coefficient of the four final polynomials, so int64 holds each with room to spare. “The polynomials, in full” prints the four. Built with gcc -O2 for this book, compose_main composed all six in 233 ms, 199 of them in step 6, at a peak of 4,224 KiB resident.

The proof §

Fourteen 0/1 variables make 2^14 = 16,384 points, and each point is one input with one program. This is the book's thesis, as a theorem:

Theorem 13. At every one of the 2^14 points, the composed polynomial accepted is 1 when the machine accepts that input with that program and 0 when it rejects it.

Proof. tests/test_compose.c evaluates the four final polynomials at every point and holds them to run_progam (its test the_composed_polynomials_agree_with_the_machine_on_all_2_to_the_14). The Roc port's proof prints the same verdict (roc/golden/composition.txt):

proof: 16384 points, 0 differ, 5596 accepted, 2891 accepted with no halt

The last count is the Python's own reading of halted, kept as it is: a zero that accepts leaves the stepper's halted at 0, so 2,891 accepting runs end with it unset. The verdict is accepted, and it agrees with the machine at every point.

The proof is exhaustive: it checks every input with every program. And since a multilinear polynomial is fixed by its values on {0, 1}^14, any implementation whose four polynomials pass it has exactly these four.