A riff on Cook-Levin Chapters

Chapter 40

The Roc port

roc/ is the same machine and the same mathematics in Roc, at the compiler that roc/roc-version pins (6206236). Its point is that the mathematics reads like mathematics.

The ring as operators §

BoolPoly is an opaque type over a canonical list of terms, and its plus, minus, times and negate are what Roc's +, -, * and unary - call. So OR is written x + y - x * y, as the algebra writes it. Roc's I64 arithmetic crashes on overflow, so every coefficient is exact or the program stops.

Substitution §

A substitution replaces each variable by a polynomial. The product of the replacements over each monomial is made once, from the product of its monomial less its highest variable, which other terms often share:

roc/BoolPoly.roc, the function substitute
## Composition: variable i replaced by `replacements[i]`. A variable with no
## value is a caller's bug. The product of the values over each of p's
## monomials is made once, from the product of its monomial without
## its highest variable, which p's own terms or their prefixes share.
substitute : BoolPoly, List(BoolPoly) -> BoolPoly
substitute = |BoolPoly.(p), replacements| {
	if union_of(p) >= bit(replacements.len()) {
		crash "BoolPoly.substitute: a variable of the polynomial has no value"
	} else {
		products = monomial_products(p, replacements)
		width = replacements.fold(0, |u, BoolPoly.(v)| u.bitwise_or(union_of(v)))
		var $sums = List.repeat(0, width + 1)
		for t in p {
			product =
				match products.get(t.mask) {
					Ok(BoolPoly.(found)) => found
					Err(KeyNotFound) => crash "BoolPoly.substitute: every monomial's product is made"
				}
			for u in product {
				$sums = add_at($sums, { mask: u.mask, coeff: t.coeff * u.coeff })
			}
		}
		from_sums($sums)
	}
}

Every point at once §

The proof wants each final polynomial's value at all 16,384 points. values gets them all at once by the zeta transform: each coefficient is summed into every point above its mask, one variable at a time, so the cost is 2^14 × 14 additions, whatever the terms:

roc/BoolPoly.roc, the function values
## The value at every point of `count` variables, by point: the
## coefficients summed over subsets (the zeta transform), one variable
## at a time, so 2^count × count additions, not 2^count × the terms.
values : BoolPoly, U64 -> List(I64)
values = |BoolPoly.(p), count| {
	size = bit(count)
	if union_of(p) >= size {
		crash "BoolPoly.values: the polynomial has a variable past the ${count.to_str()} asked for"
	} else {
		var $sums = p.fold(List.repeat(0, size), add_at)
		for index in 0..<count {
			with_it = bit(index)
			for point in 0..<size {
				if point.bitwise_and(with_it) != 0 {
					without = value_at($sums, point - with_it)
					$sums = add_at($sums, { mask: point, coeff: without })
				}
			}
		}
		$sums
	}
}

The speed, honestly §

On this box, the release build of composition 6 composes all six steps and runs the proof in about 30 ms, and the C's compose_main at -O2, which only composes, takes about 260 ms (README.md, "The Roc port"). The gap is the data structure, and the programming language has little to do with it: the C sums like terms in a hash table it grows and then sorts, while the Roc sums them in a dense table indexed by mask, a straight array write that reads back already in order. The C could use the same table, and it has yet to be tried (9e0c86c).