A riff on Cook-Levin Chapters

Chapter 2

The machine

The machine holds one register, AX, of 2 bits, so AX is 0, 1, 2 or 3. A run starts with AX set to the input. Its program is six instructions, each one of four opcodes, and the machine's programming language, an assembly language, is called Nibble.

Steve's spec §

The toy's plain-English spec is the docstring at the top of Steve's virtual_machine.py (lines 10 to 35, at commit 94c3d31), quoted as it stands:

    Every program is a sequence of these opcodes (aka instructions):

        00 (nada):
            (do nothing)

        01 (zero):
            AX = 3 -> ignore and continue
            AX = 2 -> ignore and continue
            AX = 1 -> ignore and continue
            AX = 0 -> halt and accept input

        10 (decr):
            AX = 3 -> AX = 2 and continue
            AX = 2 -> AX = 1 and continue
            AX = 1 -> AX = 0 and continue
            AX = 0 -> halt and reject input

        11 (mod2):
            AX = 3 -> AX = 1
            AX = 2 -> AX = 0
            AX = 1 -> AX = 1
            AX = 0 -> AX = 0

    Every program must include exactly six instructions.  If at the end
    of that program you have not accepted the input, then the input is
    implicitly rejected.

Every behaviour of the machine is in those 26 lines. The rest of this part only runs them.

Four opcodes §

src/vm.c holds the rule, as run_progam and its one-step helper:

src/vm.c, an excerpt
Machine vm_step(Machine m, Op op) {
    if (m.halted) return m;
    switch (op) {
    case OP_NADA:
        break;
    case OP_ZERO:
        if (m.ax == 0) {
            m.halted = 1;
            m.status = STATUS_ACCEPTED;
        }
        break;
    case OP_DECR:
        if (m.ax == 0) {
            m.halted = 1;
            m.status = STATUS_REJECTED;
        } else {
            m.ax -= 1;
        }
        break;
    case OP_MOD2:
        m.ax = m.ax % 2;

A run that reaches the end of its six instructions rejects. So a program accepts some of the four inputs and rejects the rest, and the set it accepts is the problem it decides.

One program, input by input §

Here is a program that decides {3}, the problem the puzzle page calls is_huge. In an ordinary programming language it is n == 3. In Nibble it is program 2704:

decr
decr
decr
zero
nada
nada

Read it once for each input:

So the program accepts {3} and rejects the rest. Every input's run ends by line 4, so lines 5 and 6 could hold any opcodes. That is one reason most problems have many programs.

Steve's own view of nada and of the design, in his words: "nada is kinda not a real instruction under my machine. It just makes it so that every program is exactly 12 bits. It's also worth saying that the language was designed to have some complexity. It's using an imperfect machine to solve the problem somewhat by design." “Which instructions are needed” makes both points exact: nada is redundant (Theorem 6), and two instructions of another design would decide every problem in four lines (Theorem 7).

Counting §

Each instruction is one of 4 opcodes, so there are 4^6 = 4,096 programs. Each problem is a subset of {0, 1, 2, 3}, so there are 2^4 = 16 problems. Each program is a 12-bit number, two bits an instruction, and assemble and disassemble in src/vm.c convert between the two forms.

Program numbers §

This book names a program by its number. Read the six lines as the digits of a base-4 number, line 1 first, with nada 0, zero 1, decr 2 and mod2 3: the number is op₁·4⁵ + op₂·4⁴ + op₃·4³ + op₄·4² + op₅·4 + op₆. So program 0 (nada, nada, nada, nada, nada, nada) is the first, program 4095 (mod2, mod2, mod2, mod2, mod2, mod2) is the last, and the number's twelve bits, line 1's two first, are the program's machine code as the puzzle page shows it. A program of fewer than six lines ends in nada, so its number ends in base-4 zeros. Steve's Python, and the C that matches it byte for byte, count through the programs in the other order, line 1 as the lowest digit. That order only decides which program the search finds first; the numbers in this book are all of the kind defined here, and the build checks each against the program it names.