A riff on Cook-Levin Chapters

Chapter 11

The SAT solver

Several chapters from here on hand a formula to a SAT solver, rocsat, written in Roc by Matthieu Pizenberg. It is his project, and its README is the place to read about it: a CDCL solver, the kind that learns a new clause from each contradiction it meets, with a model checker and DRAT proofs beside it. This book uses it as an outside oracle. Its code is separate from the machine's, so when it agrees with the machine, two separate programs agree. This chapter says once what the book asks it, so that each later chapter needs only to name its question.

CNF, the solvers' format §

A literal is a variable or its complement: v3, or ¬v3. A clause is an OR of literals, and it holds when at least one of its literals is true. A formula in conjunctive normal form, CNF, is a list of clauses that must all hold: an AND of ORs. For example,

(v1 ∨ ¬v2) ∧ (v2 ∨ v3) ∧ (¬v1 ∨ ¬v3)

holds at v1 = 1, v2 = 1, v3 = 0, among other settings. In DIMACS, the text format SAT tools read, it is three lines, each clause's literals as signed numbers ending in 0: 1 -2 0, 2 3 0, -1 -3 0.

Solvers want CNF because its pieces can be reasoned about one at a time. A clause whose literals are all false but one forces that one to be true, and a contradiction a solver runs into can be kept as one more clause. Any formula can be written in CNF, but writing it out directly can multiply its size, the way writing out f's text did.

Two questions §

The book asks rocsat one of two questions.

UNSATISFIABLE is often the answer the book wants. To show that every program of five lines decides some problem other than {0, 2, 3}, say, the book writes a formula whose satisfying assignments would be five-line programs that decide it, and expects UNSATISFIABLE. Each chapter says which answer it expects.

The happy path §

sat/intro.shale writes the CNF above as example.cnf:

p cnf 3 3
1 -2 0
2 3 0
-1 -3 0

Our claim is that v1 = 1, v2 = 1, v3 = 0 satisfies it, the assignment v 1 2 -3 0 in ours.v. rocsat agrees:

rocsat check example.cnf ours.v: exit 0, c model ok
rocsat solve example.cnf: exit 10, s SATISFIABLE, v -1 -2 3 0
rocsat check example.cnf found.v: exit 0, c model ok

check confirms our assignment. solve finds one of its own, v3 alone true: a solver promises some satisfying assignment, whichever it reaches first. The script writes it to found.v, and check confirms that one too.

The unhappy paths §

An oracle that said yes to everything would pass the happy path. So the script also hands rocsat two false claims. The assignment v 1 2 3 0, in wrong.v, breaks the third clause, ¬v1 ∨ ¬v3. And forced.cnf is example.cnf with two unit clauses added, 1 0 and 3 0, asking for v1 and v3 both true, which the third clause forbids, so the answer expected is UNSATISFIABLE:

rocsat check example.cnf wrong.v: exit 1, c model falsifies clause 2
rocsat solve forced.cnf: exit 20, s UNSATISFIABLE

The script stops at the first answer other than the one it expects. Each script in this repository that runs rocsat holds it both ways: a claim that should hold must be confirmed, and a claim that should fail must be refused, at the clause or with the verdict the script names. The book's build holds each rocsat answer it shows to the file its script wrote, so the book builds from the committed files alone.

Where the book asks §

chapter the question the expected refusal
“Checking a claim” check: does program 1689's run on input n satisfy f's CNF with f = 1? input 1 breaks the clause f = 1
“From equations to CNF” solve, with every input fixed, so the search only evaluates the circuit program 1689 deciding {0, 2}: UNSATISFIABLE
“Cook-Levin as SAT” solve, with the program's bits free: find the programs UNSATISFIABLE once every program is found, and one line below each shortest length
“Solved by hand”, “The solver's work, in full” solve, with a DRAT proof the proofs behind those UNSATISFIABLE answers