A riff on Cook-Levin Chapters

Chapter 42

The VM in Shale

shale/cook_levin.shale is the machine, the search over all 4,096 programs, the step polynomials and the six-step composition once more, in Shale (4942a49). It follows js/cook_levin.js closely, and it prints the same two reports, byte for byte: tests/golden/find_solutions.txt, then roc/golden/composition.txt. part=search prints the first alone and part=composition the second.

The machine §

A machine's state is a map of two fields, its AX and how its run has ended so far, the three endings named by string constants. One step is the rule of “The machine”:

shale/cook_levin.shale, the function machine_step
-- The machine after one op: `{ax, ending}`, ending RUNNING until it halts.
function machine_step(m, op)
  if m.ending ~= RUNNING then return m end
  if op == ZERO then
    if m.ax == 0 then return {ax = 0, ending = ACCEPTED} end
    return m
  end
  if op == DECR then
    if m.ax == 0 then return {ax = 0, ending = REJECTED} end
    return {ax = m.ax - 1, ending = RUNNING}
  end
  if op == MOD2 then return {ax = m.ax % 2, ending = RUNNING} end
  return m
end

A program is a list of six opcodes, the base-4 digits of its number, lowest first (disassemble). A run starts with AX at the input and folds machine_step over the list.

The search runs each of the 4,096 programs on the four inputs, makes its problem's code from the inputs it accepts, and files the program under that code. Lists count from 1, so problem y's programs are at slot y + 1:

shale/cook_levin.shale, an excerpt
function find_solutions()
  local programs = {}
  for y = 1, LANGUAGE_COUNT do push(programs, {}) end
  for n = 0, PROGRAM_COUNT - 1 do push(programs[accepted_code(disassemble(n)) + 1], n) end
  for y = 0, LANGUAGE_COUNT - 1 do
    if #programs[y + 1] == 0 then fail("no program accepts language " .. y) end

It then prints each problem's count and first program, and runs that program on the step polynomials too, failing the run where they disagree with the machine.

The ring's times §

A polynomial of the Boolean ring is a map of two lists, masks and coeffs, its terms in ascending order of mask. A monomial is a set of variables, bit i of its mask for variable i, and the product of two monomials is the OR of their masks. Shale refuses the bitwise operators, so the OR comes from a table. Every mask is below 2^14, so it splits into two 7-bit halves, and a table of the OR of every two 7-bit numbers, 16,384 entries, is made once:

shale/cook_levin.shale, the function half_table
-- The OR (or, with `both`, the AND) of every two 7-bit numbers a and b, at
-- slot a * 128 + b + 1.
function half_table(both)
  local out = {}
  for a = 0, HALF - 1 do
    for b = 0, HALF - 1 do
      local bits = 0
      local x = a
      local y = b
      for i = 1, 7 do
        local on = x % 2 == 1 or y % 2 == 1
        if both then on = x % 2 == 1 and y % 2 == 1 end
        if on then bits = bits + POW2[i] end
        x = x // 2
        y = y // 2
      end
      push(out, bits)
    end
  end
  return out
end
shale/cook_levin.shale, the function or14
function or14(a, b)
  return OR7[(a // HALF) * HALF + b // HALF + 1] * HALF + OR7[(a % HALF) * HALF + b % HALF + 1]
end

The product of two polynomials takes every pair of terms. Like terms are summed in SUMS, one dense list of 16,384 slots indexed by mask, the Roc port's table, and from_sums reads it back in order and puts each slot back to 0. q's halves are split once, before the pairs, so each pair costs two reads of OR7:

shale/cook_levin.shale, the function times
-- Every pair of terms, masks ORed and coefficients multiplied, summed by
-- mask. The OR of a pair is two reads of OR7, one a 7-bit half, so q's
-- halves are split once, before the pairs.
function times(p, q)
  local q_high = {}
  local q_low = {}
  for m in q.masks do
    push(q_high, m // HALF + 1)
    push(q_low, m % HALF + 1)
  end
  local q_coeffs = q.coeffs
  local n = #q_coeffs
  for i = 1, #p.masks do
    local a = p.masks[i]
    local high = (a // HALF) * HALF
    local low = (a % HALF) * HALF
    local c = p.coeffs[i]
    for j = 1, n do
      local k = OR7[high + q_high[j]] * HALF + OR7[low + q_low[j]] + 1
      SUMS[k] = SUMS[k] + c * q_coeffs[j]
    end
  end
  return from_sums(or14(union_of(p), union_of(q)))
end

The step polynomials §

The step is built in the ring as “Arithmetization” builds it: NOT x is 1 − x, x AND y is x·y and x OR y is x + y − x·y.

shale/cook_levin.shale, an excerpt
function negation(x) return minus(ONE_POLY, x) end
function either(x, y) return minus(plus(x, y), times(x, y)) end

next_state takes the six variables and builds the step from them, starting with which value AX holds and which opcode runs:

shale/cook_levin.shale, an excerpt
  local is0 = times(negation(hb), negation(lb))
  local is1 = times(negation(hb), lb)
  local is2 = times(hb, negation(lb))
  local is3 = times(hb, lb)

  local runs_pass = either(times(negation(op_hb), negation(op_lb)), halted)
  local runs_check = times(times(negation(op_hb), op_lb), negation(halted))
  local runs_decr = times(times(op_hb, negation(op_lb)), negation(halted))
  local runs_mod2 = times(times(op_hb, op_lb), negation(halted))

Its four answers, at variables 0 to 5, are the step polynomials. Each one's value at all 64 points is computed once, so the stepper's step is four reads of those tables.

The composition §

After step k the state is four polynomials in 14 variables. The next step substitutes them into the step polynomials. Each monomial of the step's six variables becomes the product of its variables' replacements, made from the product of the monomial less its highest variable and kept for the rest of the step. A list holding nil is refused, so PRODUCTS starts as 64 zeros and a second list, MADE, marks each slot that has been filled:

shale/cook_levin.shale, the function product_of
function product_of(mask, replacements)
  if MADE[mask + 1] == 1 then return PRODUCTS[mask + 1] end
  local product = ONE_POLY
  if mask > 0 then
    local top = 5
    while POW2[top + 1] > mask do top = top - 1 end
    product = times(product_of(mask - POW2[top + 1], replacements), replacements[top + 1])
  end
  PRODUCTS[mask + 1] = product
  MADE[mask + 1] = 1
  return product
end

substitute is a builtin already, a text replacement, and a function by a builtin's name is refused, so the substitution is substituted. Every product is made first, since a product is itself summed in SUMS, and then each term's coefficient times its product is summed in:

shale/cook_levin.shale, the function substituted
function substituted(p, replacements)
  local width = 0
  for r in replacements do width = or14(width, union_of(r)) end
  -- Every product first: a product is summed in SUMS too.
  for m in p.masks do product_of(m, replacements) end
  for i = 1, #p.masks do
    local product = product_of(p.masks[i], replacements)
    local c = p.coeffs[i]
    for j = 1, #product.masks do
      local k = product.masks[j] + 1
      SUMS[k] = SUMS[k] + c * product.coeffs[j]
    end
  end
  return from_sums(width)
end

The proof then takes each final polynomial's value at all 16,384 points by the zeta transform, as the Roc port does, and holds every point to the machine.

The check §

shale/check.shale (ac3ee9f) runs the port twice, each run a child shale with --verbose, niced, and holds each run's output byte for byte to its golden, leaving out the runner's own lines. Its run for this book, on 2026-11-25, printed:

By README.md's breakdown, of the composition's 397 million steps, the tables and the step polynomials take about 8 million, steps 1 to 5 about 22 million, step 6 about 193 million, and the proof about 174 million, at about 70 million steps a second.

What the port cost §