Chapter 12
Checking a claim
A claim such as "program 1689 accepts input 2" can be checked by a tool that knows only formulas and clauses. This chapter turns f's graph into CNF and asks rocsat check about the claim (“The SAT solver”).
Naming each gate §
Expanding f into CNF would start from its half-million characters. Tseitin's encoding names instead: each AND and OR node gets a variable of its own, and three clauses say that variable equals its gate. A complement is a literal's minus sign.
| gate | its three clauses | what they say |
|---|---|---|
| g = a AND b | (¬g ∨ a), (¬g ∨ b), (g ∨ ¬a ∨ ¬b) | g gives a, g gives b, a and b give g |
| g = a OR b | (g ∨ ¬a), (g ∨ ¬b), (¬g ∨ a ∨ b) | a gives g, b gives g, g gives a or b |
def clauses_of(out):
lit, gates = numbered(out)
clauses = []
for g in gates:
a, b = lit(g.args[0]), lit(g.args[1])
v = lit(g)
if g.op == "and":
clauses += [[-v, a], [-v, b], [v, -a, -b]]
else:
clauses += [[v, -a], [v, -b], [-v, a, b]]
clauses.append([lit(out)])
return lit, gates, clauses
The gates are numbered from v15 up, inputs before the gates that use them. The last clause holds f's own literal alone: it states f = 1. Its head, in DIMACS, the format the tools read:
c f : {0,1}^14 -> {0,1}, the Nibble machine as one function
c v1 v2: the input's bits; v3 .. v14: the program's, line 1 first
c v15 .. v160: AND and OR gates; the last clause: f = 1
p cnf 160 439
-15 -3 0
-15 4 0
15 3 -4 0
-16 -1 0
-16 -2 0
16 1 2 0
-17 15 0
-17 16 0
17 -15 -16 0
-18 3 0
-18 -4 0
18 -3 4 0
-19 18 0
-19 16 0
19 -18 -16 0
Each group of three is one gate: v15 is NOT v3 AND v4, line 1 is zero; v16 is NOT v1 AND NOT v2, AX starts at 0; v17 is v15 AND v16, line 1 accepts; v18 is v3 AND NOT v4, line 1 is decr; v19 is v18 AND v16, line 1 halts. The formula has 160 variables and 439 clauses, three for each of the 146 gates and one for f = 1. It ends:
160 -130 0
160 -159 0
-160 130 159 0
160 0
v160 is f, the OR of v130, accepted by line 5, and v159, line 6 accepting.
A claim is an assignment §
A claim gives every variable a value. The 14 bits come from the claim; each gate's value comes from running the gates on them:
def values_at(gates, n, program):
env = {1: n >> 1, 2: n & 1}
for k in range(12):
env[3 + k] = (program >> (11 - k)) & 1
memo = {}
return [env[i] for i in range(1, 15)] + [g.value(env, memo) for g in gates]
For input 2 (10) and program 1689 (01 10 10 01 10 01), the 160 values, in the solvers' v format, a minus sign for false:
v 1 -2 -3 4 5 -6 7 -8 -9 10 11 -12 -13 14 15 -16 -17 -18 -19 -20 -21 -22 23 24 -25 -26 27 -28 -29 -30 -31 -32 -33 34 -35 -36 -37 -38 -39 -40 41 -42 -43 -44 -45 -46 -47 -48 -49 -50 -51 -52 -53 -54 -55 -56 -57 -58 -59 -60 -61 62 63 64 65 66 -67 -68 -69 -70 71 -72 -73 74 -75 -76 -77 -78 -79 -80 -81 -82 -83 -84 -85 -86 -87 -88 -89 -90 -91 -92 -93 -94 -95 -96 97 98 99 100 -101 -102 -103 -104 -105 -106 -107 -108 -109 -110 -111 -112 -113 -114 -115 -116 -117 -118 -119 -120 -121 -122 -123 -124 -125 -126 127 -128 -129 130 131 132 133 134 -135 -136 -137 -138 -139 -140 -141 -142 -143 -144 -145 -146 -147 -148 -149 -150 -151 -152 -153 -154 -155 -156 157 158 -159 160 0
rocsat checks it §
The question is rocsat check's: does this assignment satisfy the CNF? sat/fourteen.shale asks it for each of the four inputs, with program 1689. For inputs 0, 2 and 3, which the program accepts, the answer expected is yes; for input 1, which it rejects, the expected answer is that the assignment breaks the last clause, f = 1, and the script fails on any other answer:
input 0, program 1689: exit 0: c model ok
input 2, program 1689: exit 0: c model ok
input 3, program 1689: exit 0: c model ok
input 1, program 1689: exit 1: c model falsifies clause 438
Clause 438, counted from 0, is the last of the 439, 160 0: f = 1. So program 1689 accepts 0, 2 and 3 and rejects 1, the problem {0, 2, 3}, as Theorem 5 says.
The one complaint is a proof. The first 438 clauses say only that each gate equals its gate's AND or OR, so once the 14 bits are fixed, exactly one assignment satisfies them, the one the gates compute. At input 1 that assignment has v160 false, so every assignment that keeps the gates' clauses breaks f = 1. python/fourteen_cnf.py, run by build.shale, checks the same at all 16,384 points: the computed assignment keeps every gate's clauses, and it keeps f = 1 exactly where the machine accepts. The book's build holds the blocks here to its output, and rocsat's lines to sat/fourteen/verdicts.txt, which sat/fourteen.shale writes.
Every bit here was given; the check only computes. A solver that finds the bits when they are left unknown is where the second part, “Cook-Levin as SAT”, takes the same encoding.