A riff on Cook-Levin Chapters

Chapter 25

Nibble++ meets rocsat

“Nibble++” settles every formal language's shortest program by searching the machine's states. This chapter puts the same questions to rocsat, as “The SAT solver” frames them, in a CNF that src/plus_sat.c builds on the toy's gates.

The machine as a circuit §

src/plus_sat.c writes the Nibble++ step on a 4-bit AX in the AND and OR gates of src/sat.c, a complement being a literal's sign: a test of AX for 0, a borrow chain for decr, and for each bit of AX a four-way multiplexer steered by the line's two bits. Unrolled for a k-line program, one run's circuit takes the 2k program bits and the input's four bits, and its output is accepted. At 28 lines, the longest shortest length, src/plus_sat_main.c counts:

CNF at 28 lines Gates Variables Clauses
one run, its input free 1340 1400 4020
the search for {0, ..., 13, 15} 19034 19090 57118

Each gate is one variable and three clauses. The second row is the search below, for the case at 28 lines.

The questions asked §

An evaluation claim fixes every input of one run with unit clauses, the program's bits and the input's, and a unit clause asks for accept. With every input fixed, solve only evaluates the circuit, as in “From equations to CNF”. The happy path comes first. The census witness of {15} accepts input 15, and the witness of {2, 3, 6, 7, 10, 11, 14, 15} accepts input 6. rocsat answers SATISFIABLE, and rocsat check accepts the model it found, with accept at 1:

program decr,half,decr,half,decr,half,decr,zero (8 lines) accepts input 15: SAT; rocsat check: c model ok; accept (399) = 1
program half,decr,mod2,zero (4 lines) accepts input 6: SAT; rocsat check: c model ok; accept (199) = 1

The unhappy path takes input 14, outside {15}. Left free, the run is SATISFIABLE with accept at 0. That model with accept turned to 1 breaks a clause, which check names. With accept forced, the answer is UNSATISFIABLE, the one expected, and rocsat's DRAT proof replays in src/drat.c:

program decr,half,decr,half,decr,half,decr,zero (8 lines) on input 14, accept left free: SAT, accept (399) = 0
program decr,half,decr,half,decr,half,decr,zero (8 lines) accepts input 14, the honest model with accept set to 1: rocsat check: exit 1, c model falsifies clause 1136
program decr,half,decr,half,decr,half,decr,zero (8 lines) accepts input 14: UNSAT; drat check: verified: 401 lemmas (0 by RAT), 0 deletions, over 400 variables and 1161 clauses; the empty clause at line 401

sat/plus.shale asks these, and build.shale runs it. It writes sat/plus/verdicts.txt with its times left out, so a changed answer shows in git status.

The search is the heart of the chapter. For a formal language W and a length k, plus_sat search W k writes one CNF. The 2k program bits are free, and 16 copies of the run share them, one per input, each with AX fixed to its input, so constant folding trims each copy. A unit clause holds each copy's output to W's want bit: accept for a member, reject for the rest.

A model is then a k-line program that decides W, so SATISFIABLE hands back a program. sat/plus_search.shale checks each one with rocsat check, with src/drat.c's model check and on the interpreter. One line below the census length L, UNSATISFIABLE says that every program of L − 1 lines decides some other formal language. Shorter programs are covered too: a decr added after a program's last line keeps its formal language (“Nibble++”), so any shorter program for W pads to one of L − 1 lines. The DRAT proof of that answer, replayed by src/drat.c, proves L minimal on its own, apart from the census.

The script ran 11 cases spread across the lengths, up to two of the hardest, at 27 and 28 lines. Its file, sat/plus/search.txt, also records each solve's time in the run of commit 9ff83c9; times vary from run to run, so the table leaves them out:

Formal language Census length L L lines L − 1 lines The proof at L − 1 Model and proof checked
{0} 1 SAT UNSAT 6 bytes yes
{0, 1} 2 SAT UNSAT 53 bytes yes
{2, 3, 6, 7, 10, 11, 14, 15} 4 SAT UNSAT 13,638 bytes yes
{8, ..., 15} 5 SAT UNSAT 34,723 bytes yes
{15} 8 SAT UNSAT 117,323 bytes yes
{0, ..., 4, 6} 12 SAT UNSAT 490,951 bytes yes
{0, ..., 6, 8} 16 SAT UNSAT 1,640,192 bytes yes
{0, ..., 8, 10} 20 SAT UNSAT 2,691,961 bytes yes
{0, ..., 10, 12} 24 SAT UNSAT 6,145,378 bytes yes
{1, ..., 10, 12, 15} 27 SAT UNSAT 11,954,817 bytes yes
{0, ..., 13, 15} 28 SAT UNSAT 17,192,870 bytes yes

Every case answered as expected: SATISFIABLE at the census length, UNSATISFIABLE one line shorter, each model and each proof checked. The proofs grow with the length, to about 17 MB at 28 lines.

Two observations §

rocsat's programs are its own. A solver hands back whichever model it reaches first, and here that model differs from the census witness in 2 of the 11 cases. At 8 lines, for {15}:

{15}, the census: decr, half, decr, half, decr, half, decr, zero
{15}, rocsat:     decr, half, decr, half, decr, decr, decr, zero

After the second half, input 15 holds 3 in both. The census brings it to 0 with decr, half, decr, and rocsat with three decrs, in the same four lines with zero.

Three methods agree on every case: the census, a breadth-first search over states in src/census.c; rocsat's search over assignments, in Roc; and src/drat.c's replay of each refutation. Each is its own code, so each shortest length in the table is confirmed three separate ways.