Chapter 10
One formula for the whole machine
“The Boolean view” ran the machine as gates on known bits. Its four step functions never ask what their bits are, so they can be handed something else: formulas. The machine is then one function of 14 bits, f, the input's two and the program's twelve, true where the program accepts the input, and running the step functions on formulas writes f down. The Roc port states f directly, in roc/Fourteen.roc:
## f itself: true when the program accepts the input. A run that gets
## past line 6 still going rejects.
f : U64 -> Bool
f = |bits| {
input = bits // 4096
program = bits % 4096
# Each line's two bits, line 1 first: 4^5, 4^4, ... 4^0.
places = [1024, 256, 64, 16, 4, 1]
places.fold(Going(input), |run, place| Fourteen.step(run, (program // place) % 4)) == Accepted
}
Formulas as values §
python/steps.py keeps a formula as a graph of nodes, each a variable, a constant, or a complement, AND or OR of nodes before it. Its operators are the step functions' ~, & and |. They fold the plain cases away (a constant, a doubled complement, x AND x, and x OR (¬x AND y), which is x OR y), and a node already built is reused, so equal parts of a formula are one node:
class F:
table = {}
def __new__(cls, op, *args):
key = (op, args)
if key not in F.table:
f = object.__new__(cls)
f.op, f.args = op, args
F.table[key] = f
return F.table[key]
def __invert__(self):
if self is TRUE:
return FALSE
if self is FALSE:
return TRUE
if self.op == "not":
return self.args[0]
return F("not", self)
def __and__(self, other):
if self is FALSE or other is FALSE:
return FALSE
if self is TRUE:
return other
if other is TRUE or self is other:
return self
return F("and", self, other)
def __or__(self, other):
if self is TRUE or other is TRUE:
return TRUE
if self is FALSE:
return other
if other is FALSE or self is other:
return self
# Absorption: x OR (NOT x AND y) is x OR y.
if other.op == "and" and other.args[0] is ~self:
return self | other.args[1]
Six steps, every bit unknown §
Name the 14 bits v1 to v14: v1 and v2 the input's, high then low, and v3 to v14 the program's, two a line, line 1 first, so that v3 to v14 read as the program's number. Start from the input's two unknowns with both flags 0, and run the six steps, each on its line's two unknowns (python/fourteen_cnf.py):
def formula():
state = (var(1), var(2), FALSE, FALSE)
for k in range(1, 7):
state = step(var(2 * k + 1), var(2 * k + 2), state)
return state[3]
What comes back is f. After line 1 the four state bits are short enough to read:
hb' = ((NOT v3 AND NOT v4 AND v1) OR (NOT v3 AND v4 AND v1) OR (v3 AND NOT v4 AND v1 AND v2))
lb' = ((NOT v3 AND NOT v4 AND v2) OR (NOT v3 AND v4 AND v2) OR (v3 AND v4 AND v2) OR (v3 AND NOT v4 AND v1 AND NOT v2))
halted' = (v3 AND NOT v4 AND NOT v1 AND NOT v2)
accepted' = (NOT v3 AND v4 AND NOT v1 AND NOT v2)
accepted' is line 1 being zero (NOT v3 AND v4) on an AX of 0 (NOT v1 AND NOT v2). hb' is the high bit kept by nada, kept by zero, or kept by a decr from 3; mod2 clears it. After line 2, accepted has the line-1 bits pasted in wherever it uses them:
((NOT v3 AND v4 AND NOT v1 AND NOT v2) OR (NOT (v3 AND NOT v4 AND NOT v1 AND NOT v2) AND NOT v5 AND v6 AND NOT ((NOT v3 AND NOT v4 AND v1) OR (NOT v3 AND v4 AND v1) OR (v3 AND NOT v4 AND v1 AND v2)) AND NOT ((NOT v3 AND NOT v4 AND v2) OR (NOT v3 AND v4 AND v2) OR (v3 AND v4 AND v2) OR (v3 AND NOT v4 AND v1 AND NOT v2))))
It reads: line 1 accepted, or line 1 left the machine running, line 2 is zero, and AX after line 1 is 0.
Graph and tree §
Written out as text, each line's formula repeats the last line's state bits wherever it uses them, and the text grows about six-fold a line. Kept as a graph, the same formula grows by about 35 gates a line:
| after line | gates in the graph | written out, characters |
|---|---|---|
| 1 | 6 | 37 |
| 2 | 34 | 322 |
| 3 | 69 | 2,131 |
| 4 | 104 | 13,598 |
| 5 | 139 | 86,211 |
| 6 | 174 | 546,105 |
The last row is f: 174 gates, or more than half a million characters. “Checking a claim” works with the graph.