Stream: show and tell

Topic: a riff on Cook-Levin


view this post on Zulip Steve Howell (Nov 25 2026 at 23:20):

This is gonna feel like something for #off topic at first, but there's gonna be a lot of show-and-tell coming up with very strong tie-ins to Roc coming up as well. Bear with me, please. First, let me highly recommend that following video about the Cook-Levin Theorem. The lecture is from professor Michael Sipser (MIT):

https://learn.mit.edu/video/7036/lecture-16-cook-levin-theorem?playlist=7021

view this post on Zulip Steve Howell (Nov 25 2026 at 23:24):

I watched this video in 2023, and it inspired me to write a little project in Python in which I explored some of the fundamental concepts of Cook-Levin, which draws on other great work (notably Turing) for its underpinning. In order to solidify the theorem in my brain, I devised a little language called Nibble that in the same spirit as a Turing machine but with everything finite. It has four opcodes:

view this post on Zulip Steve Howell (Nov 25 2026 at 23:26):

Every Nibble program has to be exactly six instructions. You feed it a value of 0, 1, 2, or 3, and there are three possible outcomes:

AX was the single register of the machine implemented in Python.

view this post on Zulip Steve Howell (Nov 25 2026 at 23:29):

Today I had Claude port my program to C, Roc, JS, and Shale (my personal invention). The JS gets compiled by my Roc-based JS compiler. But that's for later. I also had Claude write a little puzzle game for me. The language {0, 1, 2, 3} has sixteen "problems". For example {0, 1, 3} is the problem is_not_two. You need to write a Nibble program that rejects 2 but accepts 0, 1, and 3. I present that first:

https://roc.lynrummy.com/nibble/

view this post on Zulip Steve Howell (Nov 25 2026 at 23:31):

A much longer riff is coming later, but I challenge folks to do the 16 (2^4) puzzles to get your head around Nibble. Declare victory with a screenshot! I already gave this to my 22-year-old Zulip mentee, and he aced it. But I promise the last two problems are kinda challenging. The first four or so puzzles help you learn the language.

view this post on Zulip Steve Howell (Nov 26 2026 at 01:41):

Here's a quick illustration of how to play the game

Here's what I am presented with in puzzle 7:

image.png

view this post on Zulip Steve Howell (Nov 26 2026 at 01:42):

I have to accept the language {3}, which the puzzle calls is_huge. In a normal programming language, that's fn(input) return input == 3

view this post on Zulip Steve Howell (Nov 26 2026 at 01:43):

Here's the solution in Nibble:

image.png

view this post on Zulip Steve Howell (Nov 26 2026 at 01:46):

The program is this:

decr
decr
decr
zero
nada
nada

If you start with 0, the first command rejects.
If you start with 1, the second command rejects.
If you start with 2, the third command rejects.
If you start with 3, the fourth command accepts.

The program accepts {3}.

The program never executes the 5th or 6th lines (nada and nada), but the VM forces you to write six-line programs. The commands there could have been any of the four opcodes and been a correct program, but they never get reached. All but one of the 16 puzzles actually has multiple programs that could solve the problem.

view this post on Zulip Steve Howell (Nov 26 2026 at 01:48):

Puzzle 16 requires you to accept {1, 2, 3} and reject 0. It's a bit tricky to do it in five lines.

view this post on Zulip Steve Howell (Nov 26 2026 at 01:53):

/poll How familiar are people with Cook Levin?

view this post on Zulip Steve Howell (Nov 26 2026 at 02:35):

I'm watching Sipser again, and his metaphor for Cook Levin centers around the notion of a "computation history" for M on w on an accepting branch for a nondetermistic computation where M is the machine and w is the word. The nondetermism part is the tricky bit where lectures 1 though 15 help haha. (My forecoming riff is all deterministic code. I vaguely recall that one of the prior lectures kinda maps the nondeterministic frame to a more deterministic frame.)

view this post on Zulip Steve Howell (Nov 26 2026 at 02:41):

The essential notion is a tableau, and when you go through the Nibble exercise, you are essentially acting like the non-deterministic machine that captures every solution in finite time. With your mad programming skills, of course. But a monkey could win my puzzles with enough clicks.

view this post on Zulip Steve Howell (Nov 26 2026 at 02:47):

For example, puzzle 16 allows you to choose from 4 buttons on 3 rows to find the "winning" program, and you could monkey-click through all 64 combinations and find the answer without any knowledge of decr/zero/mod2 semantics or any insight into the problem. Which is the dilemma computers face.

view this post on Zulip Steve Howell (Nov 26 2026 at 02:53):

The nada choice is always dumb, because the puzzles are constructed to give the shortest solutions, and nada lives up to its name and does nothing. So it's really 27 monkey clicks to solve puzzle 16 once you accept that lemma.

view this post on Zulip Steve Howell (Nov 26 2026 at 02:55):

(or 81 I suppose. 27 attempts with 3 clicks each.)

view this post on Zulip Steve Howell (Nov 26 2026 at 03:02):

I should say that we're not precisely talking about a "Shakespeare monkey" when you talk about non-deterministic machines. Non-deterministic machines aren't random; they just fork super cheaply. The monkey could take 82 clicks.

view this post on Zulip Steve Howell (Nov 26 2026 at 03:03):

(at least that's my understanding)

view this post on Zulip Steve Howell (Nov 26 2026 at 03:05):

Any questions so far?

view this post on Zulip Steve Howell (Nov 26 2026 at 03:13):

The rest of the topic is predicated on understanding how the simple toy works.

view this post on Zulip Steve Howell (Nov 26 2026 at 03:16):

It's a deep topic. Cook-Levin wasn't "discovered" until 1971. I claim I can make it at least approachable.

view this post on Zulip Stuart Hungerford (Nov 26 2026 at 07:14):

This is so neat, thanks!

view this post on Zulip Steve Howell (Nov 26 2026 at 11:41):

The great thing about Nibble is that it's a completely understood language, because you can literally run every possible Nibble program and tabulate results. A Nibble program is six instructions over four opcodes, so 12 bits. 2 ^ 12 is 4096. So there are only 4096 programs, period. The AX register is two bits representing 0, 1, 2, or 3. There are only 2 ^ 4, or 16, languages over {0, 1, 2, 3).

view this post on Zulip Steve Howell (Nov 26 2026 at 12:00):

If you're a fan of Godel, you will probably recognize that there's a very straightforward numbering scheme for Nibble.

view this post on Zulip Steve Howell (Nov 26 2026 at 12:01):

Here's program 0:

nada
nada
nada
nada
nada
nada

This program rejects 0, 1, 2, and 3 implicity for each of the four runs. So it accepts the language {}.

view this post on Zulip Steve Howell (Nov 26 2026 at 12:03):

Here's program 4095:

mod2
mod2
mod2
mod2
mod2
mod2

It also rejects every input implicitly! So it too accepts the null language.

view this post on Zulip Steve Howell (Nov 26 2026 at 12:14):

The program that uniquely solves the hardest puzzle, {0, 2, 3} (is_not_one), is program 1689:

zero
decr
decr
zero
decr
zero

There are no other programs that accept {0, 2, 3}! Every other program either rejects 0, accepts 1, rejects 2, or rejects 3! Note that this program uses the two workhorse instructions: decr and zero. These are the only commands that explicitly accept or reject inputs.

view this post on Zulip Steve Howell (Nov 26 2026 at 12:42):

Before getting into Cook-Levin let's ask Claude for a precise mathematical description of the system we understand so far...

view this post on Zulip Steve Howell (Nov 26 2026 at 12:42):

from Claude:

The Nibble VM can be described as a mathematical object in three ways, depending on which bits you treat as inputs.

  1. Running a program is a Boolean function of 14 bits. The input is two bits (AX = 0 to 3), the program is twelve (six instructions, two bits each), and the output is accept or reject: f : {0,1}^14 → {0,1}. That's 2^14 = 16,384 points, one big truth table.

  2. Each program decides exactly one problem. Run a program on all four inputs and the four verdicts are one of the 16 subsets of {0, 1, 2, 3}. That's a function g from the 4,096 programs onto the 16 problems. It's a surjection, since every problem has a program. The programs that land on one problem form its fiber: 883 programs decide {0, 2}, and only program 1689 decides {0, 2, 3}.

  3. "Does this program decide this problem?" is a function of 16 bits. Add the problem's four bits to the program's twelve: V : {0,1}^16 → {0,1}. V is the characteristic function of a relation, the graph of g, so exactly 4,096 of its 65,536 points are 1. You can also call V a verifier: hand it a problem and a candidate program, and it checks the candidate by running it four times.

All of this is plain Boolean algebra: AND, OR and NOT on bits.

view this post on Zulip Steve Howell (Nov 26 2026 at 12:47):

And Roc finally makes its first appearance! (many more to come) What better language than Roc to express that view 1 is simply a pure function?

## The Nibble machine as one function, f : {0,1}^14 -> {0,1}. The 14 bits
## are a number below 2^14: the top 2 bits are the input, where AX starts,
## and the low 12 are the program, six lines of two bits, line 1 the highest
## pair (nada 00, zero 01, decr 10, mod2 11). So the program's 12 bits read
## as its number: 1689 is zero, decr, decr, zero, decr, zero.
Fourteen :: [].{

        ## Where a run stands: still going, with AX, or done.
        Run : [Going(U64), Accepted, Rejected]

        ## One line: `zero` accepts at 0, `decr` rejects at 0 or takes 1 from AX,
        ## `mod2` keeps AX's low bit, `nada` does nothing. A run that is done
        ## stays done.
        step : Fourteen.Run, U64 -> Fourteen.Run
        step = |run, op|
                match run {
                        Going(ax) =>
                                if (op == 1 and ax == 0) Accepted
                                else if (op == 2 and ax == 0) Rejected
                                else if (op == 2) Going(ax - 1)
                                else if (op == 3) Going(ax % 2)
                                else run
                        Accepted | Rejected => run
                }

        ## f itself: true when the program accepts the input. A run that gets
        ## past line 6 still going rejects.
        f : U64 -> Bool
        f = |bits| {
                input = bits // 4096
                program = bits % 4096
                # Each line's two bits, line 1 first: 4^5, 4^4, ... 4^0.
                places = [1024, 256, 64, 16, 4, 1]
                places.fold(Going(input), |run, place| Fourteen.step(run, (program // place) % 4)) == Accepted
        }

        ## The problem a program decides: the inputs f accepts.
        decided : U64 -> List(U64)
        decided = |program| [0, 1, 2, 3].keep_if(|input| Fourteen.f(input * 4096 + program))
}

# Program 1689 is the one program that decides {0, 2, 3}.
expect Fourteen.decided(1689) == [0, 2, 3]
# Program 2704 (decr, decr, decr, zero, nada, nada) decides {3}.
expect Fourteen.decided(2704) == [3]
# Programs 0 (all nada) and 4095 (all mod2) decide { }.
expect Fourteen.decided(0) == []
expect Fourteen.decided(4095) == []
# f is 1 at 5,596 of its 16,384 points, as the C's cross-check counts.
expect List.from_iter(U64.until(0, 16384)).keep_if(Fourteen.f).len() == 5596

view this post on Zulip Steve Howell (Nov 26 2026 at 12:49):

Any questions so far?

view this post on Zulip Steve Howell (Nov 26 2026 at 13:11):

nibble playground: https://roc.lynrummy.com/nibble/
Sipser lecture: https://learn.mit.edu/video/7036/lecture-16-cook-levin-theorem?playlist=7021

view this post on Zulip Steve Howell (Nov 26 2026 at 13:17):

I see from the poll that neither notramo, removewingman, nor Anton had ever heard of Cook-Levin. This is not a shocker to me. I'm a relatively curious person and I never heard of it until 2023. I had a lot of free time that year! I'm intentionally going slow to warm up to Cook-Levin.

view this post on Zulip Steve Howell (Nov 26 2026 at 13:33):

I assume most people have heard of Alan Turing. This is his classic paper on computability: Turing_Paper_1936

Do not attempt to read Turing's paper! It is extremely mathematical, covering things like Hilbert Function Calculus. Instead, most people get the distilled version of Turing, which is sufficient for us: a huge, useful set of computational problem maps to some kind of virtual machine (Turing called his machine a tape reader, more or less).

Nibble is obviously not a Turing complete language. It's tape is finite. The largest input to a Nibble machine is 11 (aka 3).

view this post on Zulip Steve Howell (Nov 26 2026 at 13:43):

Or really two inputs: AX 11 and program: 111101111011. But finite in every dimension. Every proof about Nibble is exhaustive just using ordinary hardware and mainstream programming languages. There's no guessing. There's no P ==? NP problem for Nibble. :smile:

view this post on Zulip Steve Howell (Nov 26 2026 at 14:12):

https://roc.lynrummy.com/nibble/sandbox.html is a sandbox for Nibble. Unlike the puzzles, here you just monkey-click and watch the patterns. I recommend mod2 for optimizations!

view this post on Zulip Steve Howell (Nov 26 2026 at 15:48):

My virtual machine is built on top of several programming languages, including Roc, but of course you could also design real circuits for it:

image.png

view this post on Zulip Steve Howell (Nov 26 2026 at 15:55):

A single stepper in the six-step VM is a little more complicated:

image.png

view this post on Zulip Steve Howell (Nov 26 2026 at 15:58):

Claude estimates the whole machine could be built with 1,439 gates.

image.png


Last updated: Nov 26 2026 at 20:35 UTC