A riff on Cook-Levin Chapters

Chapter 3

Formal languages and machines

In computer science a formal language is a set of strings. The book spends two words on it because it also has a programming language, Nibble, the notation its programs are written in, and the two are different things: a Nibble program decides a formal language. Pick an alphabet, say the digits 0 to 9, and any set of strings over it is a formal language: the decimal numerals of the primes, the palindromes, the strings that are valid Roc programs. A formal language is the cleanest way to state a decision question. "Is 91 prime?" becomes "is the string 91 in the formal language of primes?"

Machines that decide §

A machine decides a formal language when, given any string, it halts, accepting the strings in the language and rejecting the rest. A program that decides the primes is a primality test; a compiler's parser decides the formal language of well-formed programs, and rejects the rest with an error.

Turing's machine §

In 1936 Alan Turing described a machine with a finite set of states and an unbounded tape, in "On Computable Numbers, with an Application to the Entscheidungsproblem" (Proceedings of the London Mathematical Society, series 2, volume 42; a free scan). It reads and writes the tape one cell at a time, and every algorithm anyone has written since can be carried out by such a machine; that claim is the Church-Turing thesis. Turing also showed that some formal languages are undecidable: whatever machine one builds for them, some string defeats it. The best known is the halting problem, the formal language of programs that halt on their input. A machine can recognize it, accepting every member by running it, but for some strings outside the language it runs forever, still waiting to answer.

A simple example §

Take the alphabet {a, b} and the formal language of strings with an even count of a's: aba and bb are in it, ab and aaab are out. A Turing machine decides it with two working states, even and odd, and a head that reads the tape left to right. The input is written on the tape, with blank cells after it, and the machine starts in even on the first symbol.

state reads writes moves next state
even a a right odd
even b b right even
even blank blank stays accept
odd a a right even
odd b b right odd
odd blank blank stays reject

Each row is one rule: in this state, reading this symbol, write this, move, and go to that state. The state remembers the one fact that matters, whether the a's seen so far are even, and the blank after the input is where the machine answers.

A run on aba:

step state reading tape, head in brackets
1 even a [a] b a _
2 odd b a [b] a _
3 odd a a b [a] _
4 even blank a b a [_]
accept

And on ab: even reads a and moves to odd, odd reads b and stays odd, odd reads the blank and rejects. Every run takes one step per symbol and then halts, so the machine decides the formal language: it accepts exactly the strings with an even count of a's.

This machine only reads and moves right, so it is also a finite automaton; Turing's machines earn their tape on harder formal languages, such as the strings of a's followed by the same number of b's, where the machine writes marks on the tape to pair each a with a b. The idea carries over unchanged: states, a rule table, a run that ends in accept or reject, and a formal language made of the strings accepted.

This book's machine §

The machine here is Turing's idea at its smallest. Its tape is one 2-bit register, its inputs are the four strings 0, 1, 2 and 3, and every run halts within six steps, so every question about it is decidable by trying everything. Its formal languages are the 16 subsets of {0, 1, 2, 3}, the problems of the Introduction, which a program, a circuit and a SAT formula each decide alike. A program decides exactly one of them, the set of inputs it accepts. “Programs as formal languages” asks which programs decide which problems, by running all of them. The second part of the book asks what deciding costs when trying everything is out of reach, the question Cook and Levin answered.