Sheet 22 of 22
From Boole to logic gates
The screen you are reading this on works by evaluating a very large number of truth tables, billions of times a second. It’s an odd fate for a branch of philosophy, and it took about a century.
22.1 An algebra of thought
In 1847 George Boole, a self-taught mathematician from Lincoln, published The Mathematical Analysis of Logic. His larger book An Investigation of the Laws of Thought followed in 1854. His central idea was that logic could be done as algebra. Let a letter stand for a class, multiplication for “and” (the things in both classes), and 1 for everything.
Then a law appears that ordinary algebra lacks. The class of things that are x and x is just x:
Rule
x · x = x
As an equation in numbers, this has only two solutions: 0 and 1. So logic is the algebra of a world with just two values. Rearranged, the equation says x(1 − x) = 0, which reads: nothing is both x and not x. Boole derived the law of non-contradiction from it by factorising.
Algebraists after Boole, such as William Stanley Jevons, Ernst Schröder and Charles Sanders Peirce, tidied this up into what’s now called Boolean algebra. Jevons also built a physical logic machine, the “logic piano”, which he showed to the Royal Society in 1870. It could work out which combinations of terms were consistent with some premises, as a mechanical truth table.
22.2 Shannon’s master’s thesis
Boolean algebra stayed the concern of logicians until the 1930s. Then Claude Shannon, a graduate student at MIT, noticed that it described telephone relay circuits. His master’s thesis of 1937, “A Symbolic Analysis of Relay and Switching Circuits”, was published in 1938. It is often called one of the most influential master’s theses ever written.
Let a closed switch be 1 and an open one 0.
| Wiring | Current flows when | Connective |
|---|---|---|
| Two switches in series | both are closed | P ∧ Q |
| Two switches in parallel | at least one is closed | P ∨ Q |
| A relay that opens when energised | the input is off | ¬P |
So any formula is a circuit, and circuit design becomes algebra. Simplifying a formula means saving hardware. Modern transistors replaced relays, but the logic is the same.
22.3 Gates
Definition
Logic gate. A small circuit that computes a connective, with one or two input wires and one output wire.
| P | Q | P ∧ Q | P ∨ Q | ¬(P ∧ Q) | ¬(P ∨ Q) | P ⊕ Q |
|---|---|---|---|---|---|---|
| T | T | T | T | F | F | F |
| T | F | F | T | T | F | T |
| F | T | F | T | T | F | T |
| F | F | F | F | T | T | F |
Definition
Functionally complete. A set of connectives is functionally complete if every truth table can be expressed with them alone.
The recipe promised on sheet 05 proves {¬, ∧, ∨} is functionally complete. For each row where the desired output is T, write the conjunction describing that row. Then join these conjunctions with ∨. The result is true on just those rows. This is called disjunctive normal form.
Example
Suppose you want a formula true just when exactly one of P and Q holds. Rows 2 and 3 are the ones you want: (P ∧ ¬Q) ∨ (¬P ∧ Q).
| P | Q | (P ∧ ¬Q) ∨ (¬P ∧ Q) | P ⊕ Q |
|---|---|---|---|
| T | T | F | F |
| T | F | T | T |
| F | T | T | T |
| F | F | F | F |
The surprise is that one connective is enough. Writing P ↑ Q for NAND, you have:
| Want | Build |
|---|---|
¬P | P ↑ P |
P ∧ Q | (P ↑ Q) ↑ (P ↑ Q) |
P ∨ Q | (P ↑ P) ↑ (Q ↑ Q) |
| P | ¬(P ∧ P) | ¬P |
|---|---|---|
| T | F | F |
| F | T | T |
Henry Sheffer published this in 1913 (hence “Sheffer stroke”), and Peirce had noted it decades earlier without publishing. It has practical importance: a chip can be built from a large number of one identical cell. NOR works too. No other two-place connective does this alone. AND and OR cannot, for instance, since neither can ever turn all-true inputs into a false output.
22.4 Adding by truth table
Binary addition of two one-digit numbers has four cases.
| P | Q | P ⊕ Q | P ∧ Q |
|---|---|---|---|
| T | T | F | T |
| T | F | T | F |
| F | T | T | F |
| F | F | F | F |
With T as 1 and F as 0, 1 + 1 gives a sum digit of 0 and a carry of 1, which is binary for 2. The two output columns are XOR and AND. So two gates add two bits. Add a third input for a carry coming from the previous column, and you have a full adder, made of two half adders and an OR gate:
| Output | Formula |
|---|---|
| Sum | (P ⊕ Q) ⊕ C |
| Carry out | (P ∧ Q) ∨ (C ∧ (P ⊕ Q)) |
Chain 64 of those, and you can add 64-bit numbers. Real processors use faster ways of handling the carry, but they are built from the same gates, and nothing here goes beyond sheet 04 and sheet 05. Add a feedback loop, so a gate’s output feeds back to its own input, and you get memory. Everything else in the machine follows from these pieces, by way of a great deal of engineering.
22.5 Logic in everyday code
Anyone who writes programs uses this course daily, and most bugs of the form “that condition is wrong” come from an earlier page.
Example
De Morgan. The negation of age >= 18 && hasId is age < 18 || !hasId. Programmers who write age < 18 && !hasId have made the mistake on sheet 04. The rule “Refuse entry if not (adult and has ID)” should refuse a child with an ID.
Counterexample
English “or”. In a language like C or JavaScript, x == 0 || 1 doesn’t test whether x is 0 or 1. It computes (x == 0) || 1, and 1 counts as true, so the condition holds always. The English phrase has a shared subject that the code doesn’t. It’s a parsing problem, the same kind that sheet 15 calls structural ambiguity.
Example
Short-circuiting. Most languages evaluate a && b from the left and stop if a is false. So p != null && p.name == "Sam" is safe, and the reverse order crashes. Here ∧ stops being symmetrical, which it always was in logic. Order counts when evaluation has effects. This is the “and then” reading of and from sheet 04 coming back.
Counterexample
Three values. In SQL, a missing value is NULL, and a comparison with it yields unknown, not true or false. So NULL = NULL isn’t true. The logic follows a three-valued table of the kind studied by Stephen Kleene. A query with WHERE x <> 5 silently omits rows in which x is missing. The law of excluded middle does not hold in your database.
22.6 SAT
Definition
Satisfiability. A formula is satisfiable if some row of its truth table makes it true. SAT is the problem of deciding this.
Tables solve SAT in 2ⁿ steps. Stephen Cook proved in 1971 that SAT is NP-complete, and Leonid Levin reached the same result independently. A fast general method for SAT would give a fast method for a huge family of search problems, from scheduling to protein folding models. Whether such a method exists is the P versus NP problem, which is one of the great open questions in mathematics.
In practice, modern SAT solvers handle formulas with millions of variables, by using clever search strategies, and they are used to check chip designs. The worst case remains frightening. Typical cases are often not. That’s a useful gap to know about: a theorem about the hardest instances does not say how hard yours is.
22.7 Proofs as programs
The other great link is more recent. Haskell Curry and William Howard noticed a correspondence between proofs and programs: a formula corresponds to a type, and a proof of the formula corresponds to a program of that type. A → B is the type of functions from A to B, and modus ponens is function application. Under this reading, the intuitionistic logic of sheet 09 is the natural one, because a proof has to be something you can run.
This underlies proof assistants such as Coq (now renamed Rocq) and Lean, in which a computer checks every step. Others, such as Isabelle, reach the same goal by a different design. Georges Gonthier and colleagues formally verified the four colour theorem in Coq, finishing in 2005, and it removes a lot of the old worry about computer proofs (sheet 13). Hilbert’s dream of mechanical checking survived Gödel in this form. A machine can’t decide all of mathematics, but a machine can check what we prove.
22.8 End of the sequence
That is what this course set out to do. It started on sheet 01 with people disagreeing over a fence. You’ve come through truth tables, through Aristotle, Frege and Russell, and arrived in a chip. The same rules work at each level. What was common ground for two neighbours is the thing keeping your phone working.
You now have the tools to look for a counterexample, and that habit is worth keeping. So: is the previous paragraph a sound argument?
Try to break this
Build XOR from NAND alone, using the fewest gates you can. (It can be done in four.) Then check your design in the truth-table tool. Now think about why it takes more than one gate, and what the tool would have to do if you asked for the smallest possible circuit for an arbitrary table.
22.9 Exercises
Using the table in this page, write
P → Qusing only NAND.Answer
P → Qis the same as¬(P ∧ ¬Q), which isP ↑ ¬Q, and¬QisQ ↑ Q. So:P ↑ (Q ↑ Q).Write the disjunctive normal form for a formula true in exactly two rows:
PandQboth true, andPandQboth false.Answer
(P ∧ Q) ∨ (¬P ∧ ¬Q). This is equivalent toP ↔ Q.Why can’t
∧and∨alone produce¬P?Answer
Set every input to true. Any formula built only from
∧and∨then comes out true, whereas¬Pmust be false in that row.Add binary 11 and 01 by hand, using half and full adders.
Answer
Right column: 1 + 1 gives sum 0, carry 1. Next column: 1 + 0 + carry 1 gives sum 0, carry 1. So the result is 100, which is 3 + 1 = 4.
Simplify the code condition
!(a || !b).Answer
By De Morgan,
!a && !!b, which is!a && b.A database table has rows with x values 1, 2 and
NULL. How many rows doWHERE x = 1andWHERE x <> 1return, in total?Answer
1 and 1, which makes 2, not 3. The
NULLrow is unknown for both conditions, and only rows that are true are returned.Is the formula
(P ∨ Q) ∧ (¬P ∨ Q) ∧ (P ∨ ¬Q) ∧ (¬P ∨ ¬Q)satisfiable?Answer
No. Each clause rules out one row of the table. Together they rule out all four.