Chapter 7
Nibble, the puzzle page
Nibble is a puzzle page with one puzzle for each of the 16 problems: pick instructions until the program accepts exactly the puzzle's inputs and rejects the rest. The search of “Programs as formal languages” says every puzzle has an answer, and how many programs are answers.
What runs in the page §
The page runs the Roc port's machine. roc/NibbleWasm.roc exports three functions built on roc/Machine.roc, compiled to an 8.3 KB wasm module by the pinned Roc with a small C host (web/platform/host.c), and the puzzle table is printed by the C's web/puzzles.c, which files every program under the problem it decides and finds each puzzle's shortest program (README.md, "Playground"). The JavaScript in the page draws; the machine's verdicts are Roc's. The page's check, web/page_check.mjs, holds the Roc wasm to the C on all 4,096 programs and all four inputs, so every build of the page is also a cross-check of the two ports, then plays every puzzle in headless Chromium by clicks.
Playing a puzzle §
Every pick runs the program at once on all four inputs, so the trace always shows the program as it stands. A trace shows each input's AX after every step, then how each run ended, what the puzzle wants, and a row marking each input correct or wrong. A wrong program's verdict names every way it is wrong, as in "Your program falsely accepts 0. It falsely rejects 1 and 3." When the program is right, the page says so above the program, folds the instruction buttons away so the whole trace shows, and says how many of the 4,096 programs solve the puzzle. Beside the buttons, the program's machine code shows its twelve bits, two a line, and its number. Each opcode button's tip says what it does to each AX: decr is "0->R, 1->0, 2->1, 3->2". A reload starts fresh at puzzle 1.
The lessons §
The first four puzzles are lessons, one opcode each:
is_tiny({0}): one line,zero.is_one({1}): line 1 is given asdecr, and the player picks line 2.is_even({0, 2}): line 1 is given asmod2, and the player picks line 2.grumpy({ }), "Reject all inputs": one line that opens aszero. Any ofnada,decrormod2solves it, since onlyzeroever accepts.
A given line is drawn locked, in light blue, and the hint names it: "Line 1 is already fixed as decr. Your job: fix line 2."
The ramp §
The other twelve ramp by effort: the length of the shortest program, with each mod2 in it counted as one line more, since mod2 takes more thought. A tie goes to the puzzle free of mod2, then to fewer zeros, then to more solutions. The table that web/puzzles.c prints puts them in this order:
| # | puzzle | problem | shortest | programs |
|---|---|---|---|---|
| 5 | is_two |
{2} | 3 | 248 |
| 6 | is_small |
{0, 1} | 3 | 172 |
| 7 | is_huge |
{3} | 4 | 63 |
| 8 | is_middling |
{1, 2} | 4 | 74 |
| 9 | is_odd |
{1, 3} | 3 | 520 |
| 10 | is_big |
{2, 3} | 5 | 13 |
| 11 | is_extreme |
{0, 3} | 5 | 12 |
| 12 | is_not_huge |
{0, 1, 2} | 5 | 13 |
| 13 | is_not_two |
{0, 1, 3} | 4 | 150 |
| 14 | always_true |
{0, 1, 2, 3} | 4 | 104 |
| 15 | is_not_one |
{0, 2, 3} | 6 | 1 |
| 16 | is_not_tiny |
{1, 2, 3} | 5 | 15 |
A program's length is its instructions before its trailing nadas. Each puzzle says how short its best program is, and the lines past that are locked to nada. Past the lessons, every puzzle's last open line is fixed as zero, by this theorem:
Theorem 8. Every shortest program of a line or more ends in zero.
Proof. An instruction after a program's last zero can only reject, as running off the end does, so a program whose last instruction is decr or mod2 decides the same problem with that instruction cut, in fewer lines. tests/test_theorems.c checks it on all 4,096 programs: every shortest program of a line or more ends in zero, and longer programs that end otherwise exist.
The last puzzle, is_not_tiny, also gives its mod2 on line 2, the line where the solution folds 2 onto 0; the player still picks the decr on line 1.
Help for a newcomer §
The page's intro, behind a Help link after puzzle 1, says what a formal language is here (the page's word for a problem), how the machine runs a program four times to find the language it recognizes, and works decr, zero through to {1}; the page's check holds that example to the machine.
The sandbox §
A second page, the sandbox, drops the puzzles. All six lines are open, each starting as nada, and the page shows the trace on the four inputs and the problem the program decides, by its set and its name. A program can also be typed in by its number, from 0 to 4095. It runs on the same machine as the puzzles, and the page's check holds program 1689 to {0, 2, 3} there too.