A riff on Cook-Levin Chapters

Chapter 34

The Cook-Levin machine

The decider of “The circuit, in full” is a checker: hand it a problem and a program, and it says true or false. Cook and Levin's idea is to run a checker backwards. Fix the problem, leave the program unknown, pin the output to true, and ask which programs fit. The circuit does the same work as before; only the question changes.

The circuit, turned around §

The want register is fixed at the problem's bits. The program register becomes twelve unknown wires, v1 to v12. The output wire is required to be 1. Everything in between is the same 24 steppers and four comparators, and every wire in them is now an unknown too, tied to the wires feeding it by the rule of its gate.

program: v1 .. v12 unknown the same circuit every wire an unknown want register fixed decides = 1 required
The question the circuit now asks

A setting of the twelve unknowns that satisfies every gate's rule and lights the output is a program that decides the problem. The circuit has a satisfying setting exactly when some program decides the problem.

The tableau §

Cook's proof draws the run of a checking machine as a grid, the tableau: one row for each moment of the run, each row the machine's whole state, and each row fixed by the row above it through a small local rule. Here the grid has seven rows, the start and the six steps, and four columns, one per core. Each cell is a state of four bits, hb, lb, halted and accepted. The top row is known, since core i starts with AX = i and both flags 0, and each cell below it is one stepper applied to the cell above and to that row's line of the unknown program. The local rule is the stepper, drawn from its parts in “The machine as a circuit”.

For a general checker the grid is as wide as its tape and as tall as its running time, and when the time is polynomial in the input's length, the grid and its rules are polynomial in size too. That is the whole of the theorem's construction: any question a fast checker can answer becomes the question of whether such a grid can be filled in consistently.

The gates, counted §

The stepper's gates as the SAT encoding builds them, in sat_step (src/sat.c): 34, where the stepper of “The machine as a circuit” has 59, or 43 counting AND and OR alone, and that chapter says how the two differ.

src/sat.c, an excerpt
    Lit is_3 = AND(hb, lb);
    Lit is_2 = AND(hb, NOT(lb));
    Lit is_1 = AND(NOT(hb), lb);
    Lit is_0 = AND(NOT(hb), NOT(lb));

    Lit is_pass = AND(NOT(op_hb), NOT(op_lb));
    Lit is_check = AND(NOT(op_hb), op_lb);
    Lit is_decr = AND(op_hb, NOT(op_lb));
    Lit is_mod2 = AND(op_hb, op_lb);

    Lit runs_pass = OR(is_pass, halted);
    Lit runs_check = AND(is_check, NOT(halted));
    Lit runs_decr = AND(is_decr, NOT(halted));
    Lit runs_mod2 = AND(is_mod2, NOT(halted));

    Lit pass1 = AND(is_1, runs_pass);
    Lit pass2 = AND(is_2, runs_pass);
    Lit pass3 = AND(is_3, runs_pass);
    Lit pass_accepts = FALSE;
    Lit pass_halts = FALSE;

    Lit check1 = AND(is_1, runs_check);
    Lit check2 = AND(is_2, runs_check);
    Lit check3 = AND(is_3, runs_check);
    Lit check_accepts = AND(is_0, runs_check);
    Lit check_halts = FALSE;

    Lit decr1 = AND(is_2, runs_decr);
    Lit decr2 = AND(is_3, runs_decr);
    Lit decr3 = FALSE;
    Lit decr_accepts = FALSE;
    Lit decr_halts = AND(is_0, runs_decr);

    Lit mod1 = AND(OR(is_3, is_1), runs_mod2);
    Lit mod2 = FALSE;
    Lit mod3 = FALSE;
    Lit mod_accepts = FALSE;
    Lit mod_halts = FALSE;

    Lit newly_accepted = sat_or4(c, pass_accepts, check_accepts, decr_accepts, mod_accepts);
    Lit accepted_out = OR(accepted, newly_accepted);

    Lit newly_halted = sat_or4(c, pass_halts, check_halts, decr_halts, mod_halts);
    Lit halted_out = OR(halted, newly_halted);

    Lit becomes_1 = sat_or4(c, pass1, check1, decr1, mod1);
    Lit becomes_2 = sat_or4(c, pass2, check2, decr2, mod2);
    Lit becomes_3 = sat_or4(c, pass3, check3, decr3, mod3);

    Lit hb_set = OR(becomes_3, becomes_2);
    Lit lb_set = OR(becomes_3, becomes_1);

tests/test_sat.c holds these gates to the machine's step on all 64 settings of the stepper's six input wires. A stepper is 34 gates here when all six inputs are unknown, so 24 steppers would be 816. The top row's constants fold many away, since each core's AX is known at the start and both flags are 0, and a gate already built from the same two inputs is reused. The whole circuit comes to 404 gates over the 12 unknown program bits.

“Cook-Levin as SAT” takes the last step: each gate becomes three clauses, the required output becomes four more, and the circuit becomes one formula for a SAT solver.