Logic and truth tables

Logic is the study of valid inference: when does a statement follow from other statements? From Aristotle’s syllogisms through George Boole’s algebra to Claude Shannon’s circuits, logic has become the language in which mathematical proofs are written and the machinery from which every computer is built. Here are the building blocks of propositional logic, truth tables you can make yourself, the connection to set theory and Venn diagrams – and a famous experiment that shows how hard we humans find it to think logically.

Propositions and connectives

A proposition is a sentence that is either true (T) or false (F): “7 is a prime”, “it is raining”, “$x^2 \ge 0$ for all real $x$”. Questions and commands are not propositions. From simple propositions $p$, $q$, $r$ compound ones are built with connectives:

SymbolNameReadTrue when …
¬pnegationnot pp is false
p ∧ qconjunctionp and qboth are true
p ∨ qdisjunctionp or qat least one is true
p ⊕ qexclusive oreither p or qexactly one is true
p → qimplicationif p, then qp is false, or q is true
p ↔ qbiconditionalp if and only if qthey have the same truth value

The logical “or” is inclusive: “coffee or tea?” in everyday speech often means “either–or”, but in logic $p \vee q$ is also true when both are true. Aristotle (4th century BC) studied inferences between statements about classes – the syllogisms – while the Stoics, especially Chrysippus, created the first propositional logic. Modern symbolic logic begins with George Boole, who in The Laws of Thought (1854) treated true and false as 1 and 0 and calculated with them like numbers.

Truth tables

A truth table runs through every combination of truth values of the propositions involved: with $n$ variables there are $2^n$ rows. From the table every compound proposition can be classified:

Try it: Type an expression with the variables p, q, r (up to five letters) – you can also type ->, <->, &, | and !. The table shows every sub-expression, and the last column is the whole expression. Notice that an implication and its contrapositive always have the same truth value, whereas the implication and its converse do not. Try a contingent proposition such as $p \to q$ too: the only false row is where $p$ is true and $q$ false.

Implication – the awkward one

$p \to q$ is false only when $p$ is true and $q$ is false. In particular “if $p$, then $q$” is true whenever $p$ is false – whatever $q$. That sounds strange (“if the moon is made of cheese, then 2 + 2 = 5” is true), but it is the only choice that makes mathematical theorems usable. The theorem “if $n$ is divisible by 4, then $n$ is even” must be true for all $n$, including $n = 3$ (false premise) and $n = 6$ (false premise, true conclusion). Implication says nothing about cause and effect; it merely rules out one case.

Three related statements belong to an implication: the converse $q \to p$, the inverse $\neg p \to \neg q$ and the contrapositive $\neg q \to \neg p$. Only the contrapositive is equivalent to the original. “All whales are mammals” also means “whatever is not a mammal is not a whale” – but not “all mammals are whales”.

Laws of logic

Boole’s algebra has rules resembling those for numbers, but with some surprises – for instance $p \vee (q \wedge r) = (p \vee q) \wedge (p \vee r)$, which does not hold for plus and times. The most important laws, all of which can be verified with a truth table:

$$\neg(p \wedge q) \equiv \neg p \vee \neg q,\qquad \neg(p \vee q) \equiv \neg p \wedge \neg q\qquad\text{(De Morgan)}$$
$$p \to q \;\equiv\; \neg p \vee q \;\equiv\; \neg q \to \neg p,\qquad \neg\neg p \equiv p,\qquad p \wedge (q \vee r) \equiv (p\wedge q) \vee (p \wedge r)$$

Augustus De Morgan formulated his laws in 1847, the same year Boole published his first book on logic. In everyday language De Morgan’s laws say: “not both rain and sun” is the same as “either no rain or no sun”.

Valid and invalid inferences

An inference is valid if the conclusion is true in every row where all the premises are true. Validity concerns form, not content: a valid inference from false premises may give a false conclusion.

Valid

Modus ponens: $p \to q$, $p$; therefore $q$.
Modus tollens: $p \to q$, $\neg q$; therefore $\neg p$.
Hypothetical syllogism: $p \to q$, $q \to r$; therefore $p \to r$.
Disjunctive syllogism: $p \vee q$, $\neg p$; therefore $q$.
Proof by contradiction: assume $\neg p$ and derive a contradiction; therefore $p$ – this is how Euclid proved that there are infinitely many primes.

Invalid (fallacies)

Affirming the consequent: $p \to q$, $q$; therefore $p$. “If it has rained, the street is wet. The street is wet. Therefore it has rained.” (It might be the street cleaners.)
Denying the antecedent: $p \to q$, $\neg p$; therefore $\neg q$. “If you are Danish, you are European. You are not Danish. Therefore you are not European.”

Logic gates – logic made of electricity

In 1937 Claude Shannon showed in his master’s thesis at MIT that Boole’s algebra exactly describes circuits of relays: switches in series are “and”, switches in parallel are “or”. Today a microprocessor is built from billions of transistors forming logic gates. The NAND gate alone is enough: every other logical function can be built from NAND gates. The half adder adds two binary digits with an XOR gate (the sum) and an AND gate (the carry), and chaining full adders together gives the arithmetic unit of a computer.

Try it: Click the switches A and B (or the buttons) and see when the lamp lights. The small truth table on the right highlights the current row. Choose the half adder: with A = B = 1 the sum is 0 and the carry 1 – binary 10, that is 2.

Logic and sets – Venn diagrams

To every statement about an element $x$ there corresponds a set: the set of those $x$ for which the statement is true. Then “and” becomes intersection ($A \cap B$), “or” becomes union ($A \cup B$), and “not” becomes the complement ($A^c$, everything in the universe $U$ outside $A$). John Venn introduced the diagrams of overlapping circles in 1880. With three sets the circles divide the universe into $2^3 = 8$ regions – exactly the 8 rows of a truth table with three variables.

Try it: Compare $(A \cup B)^c$ and $A^c \cap B^c$: the regions are the same – De Morgan’s law seen as sets. Type your own expressions with ∩, ∪, ¬ (or ' after a set), \ and →. Notice that $A \to B$ covers everything except the part of $A$ lying outside $B$: the implication is false exactly there.

Quantifiers – “for all” and “there exists”

Propositional logic cannot express “all men are mortal”. In 1879 Gottlob Frege introduced predicate logic with the quantifiers $\forall$ (for all) and $\exists$ (there exists). Negation swaps them – a version of De Morgan’s law:

$$\neg\,\forall x\, P(x) \;\equiv\; \exists x\, \neg P(x),\qquad \neg\,\exists x\, P(x) \;\equiv\; \forall x\, \neg P(x)$$

To disprove “all swans are white” one black swan is enough; to prove it a thousand white swans are not enough. The order of quantifiers matters: $\forall x\,\exists y: y > x$ (there is no largest number) is true for the real numbers, while $\exists y\,\forall x: y > x$ (there is a number larger than all) is false. This is the logical core of the definitions of limit and continuity.

Wason’s selection task – why logic is hard

In 1966 the psychologist Peter Wason devised an experiment that has since been repeated countless times. Each card has a letter on one side and a number on the other. Which cards must be turned over to decide whether the rule holds? Only about 10 % of participants answer correctly. Dress the same logical task in a social context, and most answer correctly.

Try it: Click the cards you want to turn over and press “Check my answer”. The rule is an implication $p \to q$, and it is broken only by a card with $p$ true and $q$ false. It is modus tollens that is hard: realising that the card showing ¬q must be checked.

The limits of logic

Around 1900 it was hoped that all of mathematics could be derived from logic. In 1901 Bertrand Russell found a paradox: the set of all sets that are not members of themselves – is it a member of itself? In Principia Mathematica (1910–13) Russell and Alfred North Whitehead built a system avoiding the paradox, but in 1931 Kurt Gödel showed that every consistent system strong enough to contain arithmetic contains true statements that cannot be proved within the system. In 1936 Alan Turing showed that there is no mechanical method that can decide whether an arbitrary program halts. Logic is an indispensable tool – but it cannot do everything.

Further reading