Chapter 31
Why composing blew up
A whole program is six steps. Composing them means substituting: step 1's four polynomials go in for the state variables of step 2, and so on, until the final state is four polynomials in 14 variables, the input's two bits and each instruction's two. One polynomial would then hold the whole machine.
The general ring §
In the general ring each substitution multiplies polynomials together, and every product raises powers: x·x is x², x²·x is x³. Terms of a state that will only ever see 0 and 1 are kept apart because their exponents differ, and products of sums of them multiply their coefficients. The Python harness measured it: step 1 in about 0.9 s, and step 2 past its 30 s cap (1a5f6fa's message). The general polynomial in this repository's C, src/poly.c, checks every coefficient's add and multiply in src/ring_int.c and stops the program on an int64 overflow, since Python's integers grow as large as they need and C's stop at 64 bits.
The Boolean ring §
The fix is one identity. Every variable here is only ever 0 or 1, and on those values x² = x. A ring where every x² is x is a Boolean ring: every polynomial in it is multilinear, each variable to the first power at most. A monomial is then a set of variables, kept as a bit mask, and the product of two monomials is the union of their sets, the OR of their masks.
roc/BoolPoly.roc is that ring. Its product, with like terms summed in a table indexed by mask:
## Every pair of terms, masks ORed and coefficients multiplied, summed
## by mask.
times : BoolPoly, BoolPoly -> BoolPoly
times = |BoolPoly.(p), BoolPoly.(q)| {
var $sums = List.repeat(0, union_of(p).bitwise_or(union_of(q)) + 1)
for a in p {
for b in q {
$sums = add_at($sums, { mask: a.mask.bitwise_or(b.mask), coeff: a.coeff * b.coeff })
}
}
from_sums($sums)
}
src/boolring.c is the C's version (0c233e3), with a 32-bit mask and checked int64 coefficients.
Why it is exact §
The Boolean ring keeps every value the machine can see. Two polynomials that agree on every 0/1 point reduce to the same multilinear polynomial, and a multilinear polynomial is fixed by its values on {0, 1}^n. So reducing by x² = x keeps every value the machine can see, and the canonical form makes == equality of functions (roc/BoolPoly.roc's header).
With the identity in place the step polynomials shrink from 786 and 2,979 terms to 7 and 13, and the composition of “Six steps composed” takes a fraction of a second. “The polynomials, in full” shows the step both ways, Python's 147,274 characters of lb' beside its 230 in the Boolean ring.