Chapter 30
Arithmetization
The stepper writes one step of the machine as polynomials, the first of the two ways to compose in “Six expressions, composed”: expand. Each bit of the state is a variable that is only ever 0 or 1, and logic becomes arithmetic:
- NOT x is 1 - x;
- x AND y is x·y;
- x OR y is x + y - x·y.
On 0/1 values each of these gives 0 or 1, and the right one. In Roc (roc/Stepper.roc) the first and third read as they are written:
## NOT, as an indicator: 1 - x.
complement : BoolPoly -> BoolPoly
complement = |x| BoolPoly.one - x
## OR, as an indicator: x + y - x·y.
either : BoolPoly, BoolPoly -> BoolPoly
either = |x, y| x + y - x * y
One step §
A step reads six bits: AX's high and low bits (hb, lb), the flags halted and accepted, and the instruction's two bits (op_hb, op_lb). It writes four: the next hb, lb, halted and accepted.
The construction names each fact about the state with an indicator, a polynomial that is 1 when the fact holds: AX is 0, the instruction is decr and the machine is still running, and so on. The new state is what any of the four instructions would make of the old one:
## The state after one step, from the state and the op's two bits.
## `pass`, `check`, `decr` and `mod2` say what each op does, as
## indicators of where AX goes and whether it accepts or halts; the
## new state is what any of them does.
next_state : { hb : BoolPoly, lb : BoolPoly, halted : BoolPoly, accepted : BoolPoly, op_hb : BoolPoly, op_lb : BoolPoly } -> Stepper.State
next_state = |{ hb, lb, halted, accepted, op_hb, op_lb }| {
is_0 = complement(hb) * complement(lb)
is_1 = complement(hb) * lb
is_2 = hb * complement(lb)
is_3 = hb * lb
runs_pass = either(complement(op_hb) * complement(op_lb), halted)
runs_check = complement(op_hb) * op_lb * complement(halted)
runs_decr = op_hb * complement(op_lb) * complement(halted)
runs_mod2 = op_hb * op_lb * complement(halted)
none = BoolPoly.zero
pass = { to_1: is_1 * runs_pass, to_2: is_2 * runs_pass, to_3: is_3 * runs_pass, accepts: none, halts: none }
check = { to_1: is_1 * runs_check, to_2: is_2 * runs_check, to_3: is_3 * runs_check, accepts: is_0 * runs_check, halts: none }
decr = { to_1: is_2 * runs_decr, to_2: is_3 * runs_decr, to_3: none, accepts: none, halts: is_0 * runs_decr }
mod2 = { to_1: either(is_3, is_1) * runs_mod2, to_2: none, to_3: none, accepts: none, halts: none }
ops = [pass, check, decr, mod2]
becomes_1 = any_of(ops.map(|op| op.to_1))
becomes_2 = any_of(ops.map(|op| op.to_2))
becomes_3 = any_of(ops.map(|op| op.to_3))
{
hb: either(becomes_3, becomes_2),
lb: either(becomes_3, becomes_1),
halted: either(halted, any_of(ops.map(|op| op.halts))),
accepted: either(accepted, any_of(ops.map(|op| op.accepts))),
}
}
The step's sizes §
In Python's general polynomial ring, where x·x is x², the four polynomials come out large: hb' has 786 terms and lb' 2,979 (README.md, its layout's src/stepper.c). The C port prints them exactly as stepper.py does, held to tests/golden/step_hb.txt and its three siblings. In the Boolean ring of “Why composing blew up” the same four have 7, 13, 17 and 33 terms, every coefficient 1 or -1, as an expect in roc/Stepper.roc holds. “The polynomials, in full” prints all eight whole.
Here is halted' in that ring. It says: halted already, or a decr at AX = 0 on a running machine.
halted+op_hb+(-1)*hb*op_hb+(-1)*lb*op_hb+(-1)*halted*op_hb+(-1)*op_hb*op_lb
+hb*lb*op_hb+hb*halted*op_hb+hb*op_hb*op_lb+lb*halted*op_hb+lb*op_hb*op_lb
+halted*op_hb*op_lb+(-1)*hb*lb*halted*op_hb+(-1)*hb*lb*op_hb*op_lb
+(-1)*hb*halted*op_hb*op_lb+(-1)*lb*halted*op_hb*op_lb+hb*lb*halted*op_hb*op_lb
The stepper as a second machine §
Evaluated at the bits of a state and an instruction, the four polynomials give the next state's bits. So the stepper is a second implementation of the machine, written as algebra.
Theorem 12. On every one of the 4,096 programs and each of the four inputs, the stepper, evaluating the step polynomials one step at a time, accepts exactly when run_progam does.
Proof. tests/test_cross_check.c runs all 16,384 pairs both ways and holds the differences to 0 and the accepting pairs to 5,596. The Python's find_solutions makes the same check on each problem's example program alone, 16 programs on 4 inputs.