Chapter 00 · Circuits and automata
How does a machine compute, deep down?
Underneath every program there are switches. Boolean algebra, a few logic gates and a little memory are enough to build an adder, a finite automaton and finally a processor. Along the way the first limit appears: there are patterns that no machine with finite memory can recognise.
In this chapter
A language model running on a graphics card performs trillions of operations a second, and each one, deep down, is made of switches that are either on or off. How do you get from a switch to a machine that adds, remembers and follows a program? The answer took a century to assemble, from George Boole's algebra (1854) to the first computers with a stored program (1948), and it is the foundation on which everything else in this site stands.
The key piece came from a master's thesis. In 1937 Claude Shannon, aged twenty-one, showed that the circuits of relays in telephone exchanges obey exactly Boole's algebra: a switch is a variable that is worth or , two switches in series are an (and) and two in parallel an (or). Designing a circuit became a matter of manipulating formulas, and reasoning about formulas became something a circuit could do.
One gate is enough
A Boolean function of inputs is any rule . There are many of them: each of the possible inputs can be sent to or , so there are functions, already 65,536 for . Yet a handful of building blocks can construct them all, and in fact a single one is enough: the NAND gate, “not and”.
Every Boolean function can be computed by a circuit of , and gates. NAND gates alone are also enough.
Proof
For each input with , form the conjunction , where if and if . By construction exactly when . So
which is exactly at the inputs where is (if there are none, ). This is the disjunctive normal form. For NAND it is enough to rebuild the three gates:
the last one by De Morgan's law, .
The proof builds circuits that can be huge, and Shannon showed in 1949 that, for most functions, that cannot be avoided: there are functions but far fewer small circuits, so almost every function of inputs needs on the order of gates. The functions we care about are the rare exceptions with small circuits. Addition is one of them.
From gates to arithmetic
To add two numbers in binary you do what you learned at school, column by column, carrying. In column three bits come in, , and the carry , and two go out: the result digit and the next carry . With for the exclusive or (which is when exactly one of its inputs is), the full adder is
Chaining full adders, with , computes the sum of two -bit numbers and :
Proof
Checking the eight possible inputs shows that the full adder writes the sum of its three bits in binary: . Multiplying by and adding over ,
and the last sum telescopes to .
Each full adder costs five gates (or nine NAND gates), so adding 64-bit numbers takes a few hundred gates. Multiplying, comparing or computing a is just a matter of more circuits of the same kind. What is still missing is memory.
Memory, and finite automata
A circuit whose output feeds back into its input can remember. Two NAND gates connected in a loop form a latch that keeps a bit until it is told to change it. With of those bits and a clock that ticks, a circuit has possible states and, at each tick, moves from one state to another depending on the state and on what comes in. That object, stripped of the electronics, is a finite automaton.
A DFA is a tuple with a finite set of states , an input alphabet , a transition function , an initial state and a set of accepting states . It reads a word one symbol at a time, , and accepts it if it ends in . The set of accepted words is the language .
The idea came from an unexpected place: the model of neurons by McCulloch and Pitts (1943), networks of threshold units that Stephen Kleene analysed in 1951 to ask which “events” they could detect. The answer, the languages that finite automata recognise, also has a very concrete description.
aaaabbbb: there is no state for “four”.A regular expression describes a set of words with three operations: union (, one or the other), concatenation (, one after the other) and the Kleene star (, any number of repetitions). For example, is “any word that ends in ”.
A language is described by a regular expression if and only if it is recognised by a finite automaton.
To go from expressions to automata it is convenient to let the automaton guess. A nondeterministic automaton (NFA) can have several transitions with the same symbol, or none, and accepts if some choice leads to an accepting state. Each operation of an expression then becomes a small piece of NFA (Thompson's construction, 1968, still at the heart of many regular-expression engines). Guessing seems to give extra power. It does not:
For every NFA with states there is a DFA that recognises the same language with at most states.
Proof
The DFA keeps track of all the states the NFA could be in. Its states are the subsets ; it starts at ; reading it moves to ; and it accepts if . By induction on the length of the word, after reading the DFA is in exactly the set of states the NFA can reach with , so it accepts if and only if some run of the NFA does. There are subsets.
The exponential is real: the language “the -th symbol from the end is a ” has an NFA with states, and every DFA for it needs at least . Rabin and Scott received the Turing Award in 1976 for this paper, which also introduced nondeterminism, an idea that a decade later would underlie the versus question.
What finite memory cannot do
The automaton in the figure cannot be extended to recognise for every , however many states we give it. It is not a lack of ingenuity: it is a theorem.
If is regular, there is a number such that every word with can be split as with and , so that for every .
Proof
Let be the number of states of a DFA for . Reading the first symbols of , the automaton goes through states, so by the pigeonhole principle it repeats one: there are positions with . Let be the first symbols, the following and the rest. Reading takes the automaton from back to the same state, so it can do it zero times or a hundred, and it will still end up where it ended with : in an accepting state.
Take and suppose it were regular, with constant . The word is in , and since , the piece consists only of 's. Pumping it, has more 's than 's, and it is not in . Contradiction. A finite automaton cannot count without limit: to check that there are as many 's as 's it would have to remember a number that can be arbitrarily large.
Chomsky's hierarchy
In 1956 the linguist Noam Chomsky asked which kind of grammar could describe a human language, and by 1959 he had ordered the formal languages into four levels. Each kind of grammar corresponds exactly to a kind of machine, which differ only in their memory:
| Type | Grammar | Machine | Memory | Example beyond the previous level |
|---|---|---|---|---|
| 3 | Regular | Finite automaton | Finite | |
| 2 | Context-free | Pushdown automaton | A stack | |
| 1 | Context-sensitive | Linear-bounded automaton | A tape as long as the input | |
| 0 | Unrestricted | Turing machine | An unbounded tape | The halting problem |
A single stack is enough for : push a token for each and pop one for each . Context-free grammars, written in the Backus–Naur notation of ALGOL 60, describe the syntax of almost every programming language, and so every compiler starts with a finite automaton that splits the text into tokens and a pushdown automaton that builds the syntax tree. Human languages turned out to be more slippery: a few constructions, such as the cross-serial dependencies of Swiss German, are beyond context-free grammars. Sixty years later the models that best predict language are not grammars at all, but that is another chapter.
The machine underneath
With adders, memory and a finite automaton to coordinate them, a computer can be built. In 1945 John von Neumann described the design of the EDVAC: a memory that holds both the data and the program, a unit that does arithmetic and a control unit that repeats the same cycle forever. On 21 June 1948 the Manchester “Baby” became the first computer to run a program stored in its own memory.
- Fetch: read from memory the instruction the program counter points to, and advance the counter.
- Decode: the control automaton works out which operation it is and which memory cell it refers to.
- Execute: the adder or the memory does the work, and the cycle starts again.
322 means “add the contents of cell 22”. Step through the fetch–decode–execute cycle and watch the program counter jump back to repeat a loop.A real computer is, strictly speaking, a finite automaton: with bits of memory it has at most states. But with 16 gigabytes, is about , and is a number that nothing in the universe can enumerate. It makes more sense to think of memory as a tape that can always be extended. That idealization, the infinite tape, is exactly Turing's machine.
References
- G. Boole (1854). An Investigation of the Laws of Thought. Walton and Maberly.
- C. E. Shannon (1938). “A Symbolic Analysis of Relay and Switching Circuits”. Transactions of the AIEE, 57(12).
- W. S. McCulloch and W. Pitts (1943). “A Logical Calculus of the Ideas Immanent in Nervous Activity”. Bulletin of Mathematical Biophysics, 5.
- J. von Neumann (1945). First Draft of a Report on the EDVAC.
- C. E. Shannon (1949). “The Synthesis of Two-Terminal Switching Circuits”. Bell System Technical Journal, 28(1).
- S. C. Kleene (1956). “Representation of Events in Nerve Nets and Finite Automata”. In Automata Studies, Princeton University Press.
- N. Chomsky (1956). “Three Models for the Description of Language”. IRE Transactions on Information Theory, 2(3).
- N. Chomsky (1959). “On Certain Formal Properties of Grammars”. Information and Control, 2(2).
- M. O. Rabin and D. Scott (1959). “Finite Automata and Their Decision Problems”. IBM Journal of Research and Development, 3(2).
- Y. Bar-Hillel, M. Perles and E. Shamir (1961). “On Formal Properties of Simple Phrase Structure Grammars”. Zeitschrift für Phonetik, Sprachwissenschaft und Kommunikationsforschung, 14.
- K. Thompson (1968). “Regular Expression Search Algorithm”. Communications of the ACM, 11(6).
- J. E. Hopcroft, R. Motwani and J. D. Ullman (2006). Introduction to Automata Theory, Languages, and Computation, 3rd ed. Addison-Wesley.