Logic and proofs — theory with a home alarm, truth tables that never lie and step-by-step proofs (and the alarm that went off with the door shut)

One October night, at 3:10 a.m., our home alarm started beeping with the front door shut, and it took me a while to understand that the alarm had not made a mistake: I had. I thought the rule was “if the door opens, it rings”. But the installer had set it up differently: it rings if the door opens or the hallway sensor detects motion, and night mode is on. The living-room fan moved a curtain, the sensor saw it and, since night mode was on, the alarm rang. All correct. The only thing that was wrong was the way I read the rule.

That rule is a compound proposition, and working out when it is true is exactly what this topic teaches. We will see what a proposition is, the six connectives (not, and, or, exclusive or, implication and biconditional), how to build a truth table column by column (the thing that comes up most in the problems exam), what tautologies, contradictions and contingencies are, logical equivalences and their laws, predicates and quantifiers and, in the second half, how to prove things: rules of inference, direct proof, proof by contrapositive, proof by contradiction, counterexamples and induction. It is the first topic of Discrete Mathematics and the foundation of everything else: the sets in topic 2 are logic in different clothes. After this theory you have the guided practice with the classroom problems solved step by step.

In a nutshell: propositional logic studies sentences that are true or false and how they combine with connectives (¬, ∧, ∨, →, ↔); a truth table gives the value of the combination in every case. Proofs chain valid steps (direct, contrapositive, contradiction, induction) to show that something is always true.

What a proposition is

A proposition is a declarative sentence that is either true or false, but never both at once. Its value is called its truth value: true (T) or false (F). Simple propositions are named with lower-case letters: pp, qq, rr…

The home alarm has three simple propositions:

  • pp: the door is open.
  • qq: the sensor detects motion.
  • rr: night mode is on.

At any given moment each of them is T or F. And are these sentences propositions?

SentenceProposition?Why
“Las Palmas is in Gran Canaria”YesIt is true
“2+2=52 + 2 = 5“YesIt is false (a proposition can be false)
“Did you lock the door?”NoIt is a question: neither T nor F
“Lock the door”NoIt is an order
“x+1=3x + 1 = 3“NoIt depends on xx: it is a predicate (more on that below)

Thinking a proposition has to be true. “2+2=52 + 2 = 5” is a perfectly good proposition: it is false. What it cannot be is ambiguous or dependent on something that has not been fixed.

The six connectives

From simple propositions we build bigger ones with connectives (or logical operators). There are six, and each one has its truth table:

  • Negation, ¬p\neg p (“not pp“): flips the value. If the door is open, ¬p\neg p is F.
  • Conjunction, p∧qp \land q (“pp and qq“): T only if both are T.
  • Disjunction, p∨qp \lor q (“pp or qq“): T if at least one is T. It is the inclusive or: it also holds when both are T.
  • Exclusive or, p⊕qp \oplus q (XOR): T if exactly one is T. It fails when both are T.
  • Implication or conditional, p→qp \to q (“if pp, then qq“): F only when pp is T and qq is F. pp is called the hypothesis (or premise) and qq the conclusion.
  • Biconditional or double implication, p↔qp \leftrightarrow q (“pp if and only if qq“): T when both have the same value.
The six connectivesTruth tables of negation, conjunction, disjunction, exclusive or, implication and biconditional.p¬pTFFTnotandpqp ∧ qTTTTFFFTFFFForpqp ∨ qTTTTFTFTTFFFexclusive orpqp ⊕ qTTFTFTFTTFFFif… thenpqp → qTTTTFFFTTFFTif and only ifpqp ↔ qTTTTFFFTFFFTT = true, F = false. The highlighted column is the result of each connective.
Look at the implication: it is false in only one row, when p is true and q is false.

The diagram deserves a minute. The tables of ∧\land, ∨\lor and ⊕\oplus are symmetric: the order of pp and qq does not matter. The one for →\to is not: it has a single F, in the T–F row, and in both rows where pp is F the implication is T. That asymmetry is the source of almost every mistake in the topic, so let’s look at it calmly.

Why “false implies anything” is true

Think of an implication as a promise. The installer told me: “if there is motion in night mode, the alarm rings”. When has he lied to me?

There is motionIt ringsHas he kept the promise?
YesYesYes
YesNoNo: the only way to break it
NoYesYes: he promised nothing about when there is no motion
NoNoYes

When there is no motion, the promise says nothing, so it cannot be broken: that is why F→TF \to T and F→FF \to F are true. Such an implication is called vacuously true.

If you get stuck on an implication, look for the only row that makes it false: hypothesis T and conclusion F. All the others are T.

Ways of saying “if p, then q”

In everyday language the implication comes in disguise, and that is where most mistakes happen when formalising:

SentenceFormula
if pp, then qq · qq if pp · qq whenever ppp→qp \to q
pp only if qqp→qp \to q
pp is sufficient for qqp→qp \to q
qq is necessary for ppp→qp \to q
qq unless ¬p\neg pp→qp \to q
pp if and only if qq · pp is necessary and sufficient for qqp↔qp \leftrightarrow q

The trickiest one is “only if”. “The alarm rings only if night mode is on” does not say that in night mode it always rings; it says that, if it rings, night mode was on: s→rs \to r.

What is sufficient goes to the left of the arrow and what is necessary, to the right. “Having the key is necessary to open the door” → open→key\text{open} \to \text{key}.

The implication and its three variants

From p→qp \to q we build three more:

  • Converse: q→pq \to p.
  • Inverse: ¬p→¬q\neg p \to \neg q.
  • Contrapositive: ¬q→¬p\neg q \to \neg p.
The implication and its three variantsFour boxes: implication p → q, converse q → p, inverse ¬p → ¬q and contrapositive ¬q → ¬p; the diagonals join the equivalent ones.Implicationp → qif there is motion, it ringsConverseq → pif it rings, there is motionInverse¬p → ¬qif there is no motion, it does not ringContrapositive¬q → ¬pif it does not ring, there is no motionequivalentviolet with violet: same table · coral with coral: same table
An implication is only equivalent to its contrapositive; the converse and the inverse are equivalent to each other, but not to the original.

In the diagram, the diagonals join the ones with the same truth table. An implication is equivalent to its contrapositive (“if there is motion, it rings” says the same as “if it did not ring, there was no motion”), and the converse is equivalent to the inverse. But the converse does not say the same as the original: the alarm ringing does not mean there was motion (it also rings when the door opens). You will use this in proofs: proving ¬q→¬p\neg q \to \neg p is a valid way to prove p→qp \to q.

pqp → qq → p¬p → ¬q¬q → ¬p
TTTTTT
TFFTTF
FTTFFT
FFTTTT

Precedence and brackets

Just as multiplication goes before addition in arithmetic, the connectives have an order of priority:

Connective¬∧∨→↔
Priority1 (first)2345 (last)

So ¬p∧q→r\neg p \land q \to r reads ((¬p)∧q)→r((\neg p) \land q) \to r. Even so, in the exam and in your notes always write the brackets: they cost nothing and they prevent misreadings.

Truth tables, column by column

A truth table gives the value of a formula in every possible case. It is the main tool of the topic and what comes up most in the classroom problems, so here is the full method, using the real rule of my alarm:

s≡(p∨q)∧r s \equiv (p \lor q) \land r
The alarm ruleThree propositions p, q and r go into an or gate and an and gate; the output is the compound proposition (p ∨ q) ∧ r.pdoor openqmotionrnight mode∨ (or)∧ (and)rings(p ∨ q) ∧ rp ∨ q
The alarm is a compound proposition: it is worked out from the inside out, like a circuit.

As in the diagram, the formula is worked out from the inside out, like a circuit: first p∨qp \lor q and then the conjunction with rr. The method, in four steps:

  1. Rows: with nn variables there are 2n2^n rows. With pp, qq and rr, 23=82^3 = 8.
  2. Order of the rows: the first variable alternates every 2n−12^{n-1} rows (4 T and 4 F), the second every 2n−22^{n-2} (2 T, 2 F…) and the last one, row by row. That way no combination is repeated or forgotten.
  3. Columns: one for each subformula, from the innermost to the outermost. It is the formula’s tree read from the bottom up.
  4. Each cell is filled in with the table of that column’s connective, looking only at the columns it depends on.
Truth table of the alarmTruth table of (p ∨ q) ∧ r with eight rows and one column per subformula, next to its tree: first the variables, then p ∨ q and finally the conjunction.pqrp ∨ q(p ∨ q) ∧ rTTTTTTTFTFTFTTTTFFTFFTTTTFTFTFFFTFFFFFFFthe order of the columns∧∨rpq③ last column②① p, q, r: 2³ = 8 rowsmarked row: p = F, q = F, r = T
One column for each node of the tree, from the bottom up. The marked row is the night the door was closed and nobody was there.

The table in the diagram, written out in full:

pqrp ∨ q(p ∨ q) ∧ r
TTTTT
TTFTF
TFTTT
TFFTF
FTTTT
FTFTF
FFTFF
FFFFF

The fifth row is my night: door shut (pp F), the curtain moving (qq T), night mode on (rr T). The alarm rings. The row marked in the diagram is a quiet night: nothing open, nothing moving, night mode on. It does not ring. The rule works; what was wrong was my reading of it.

Skipping the intermediate columns to go faster. In the problems exam, one wrong cell in p∨qp \lor q drags the error into the whole last column, and the answer you choose is the whole last column: either it is all right or it scores nothing.

Before filling in the last column, look at which values make it false (in a conjunction, one F is enough; in an implication, only T → F). Fill in those rows first, and the rest are T.

Tautology, contradiction and contingency

With the last column in front of you, a formula falls into one of three groups:

  • Tautology: it is T in every row. For example, (p∧q)→p(p \land q) \to p: if both are true, the first one is.
  • Contradiction: it is F in every row. For example, p∧¬pp \land \neg p: the door cannot be open and shut at the same time.
  • Contingency: neither of the two; there are T rows and F rows. For example, p→qp \to q or the alarm rule itself.
Tautology, contradiction and contingencyThe last column of three formulas: all T in the tautology, all F in the contradiction and mixed in the contingency.Tautology(p ∧ q) → pTTTTalways TContradictionp ∧ ¬pFFFFalways FContingencyp → qTFTTsometimes T, sometimes F
You only need to look at the last column: the classification comes from there.

Two related ideas that come up in the classroom practicals:

  • A formula is satisfiable if there is at least one row in which it is T. Every tautology and every contingency is satisfiable; only contradictions are not.
  • A set of specifications is consistent if some assignment of values makes them all true at once. That is the same as saying that their conjunction is satisfiable. In the practice we check whether the alarm’s specifications are consistent.

The negation of a tautology is a contradiction, and vice versa. The negation of a contingency is still a contingency.

Logical equivalences

Two formulas are logically equivalent if they have the same truth value in every row. We write P≡QP \equiv Q, which is the same as saying that P↔QP \leftrightarrow Q is a tautology.

Using ≡\equiv as if it were one more connective. It is not: p↔qp \leftrightarrow q is a formula that can be T or F; P≡QP \equiv Q is a statement about two formulas (“they have the same table”), written outside them.

There are two ways to check an equivalence: build both tables and compare them column by column (it always works) or transform one into the other with the laws of logic, which are equivalences already proved. These are the ones in the course notes, with T\mathbf{T} as “any tautology” and F\mathbf{F} as “any contradiction”:

EquivalenceName
p∧T≡pp \land \mathbf{T} \equiv p · p∨F≡pp \lor \mathbf{F} \equiv pIdentity
p∨T≡Tp \lor \mathbf{T} \equiv \mathbf{T} · p∧F≡Fp \land \mathbf{F} \equiv \mathbf{F}Domination
p∨p≡pp \lor p \equiv p · p∧p≡pp \land p \equiv pIdempotent
¬(¬p)≡p\neg(\neg p) \equiv pDouble negation
p∨q≡q∨pp \lor q \equiv q \lor p · p∧q≡q∧pp \land q \equiv q \land pCommutative
(p∨q)∨r≡p∨(q∨r)(p \lor q) \lor r \equiv p \lor (q \lor r) · same with ∧\landAssociative
p∨(q∧r)≡(p∨q)∧(p∨r)p \lor (q \land r) \equiv (p \lor q) \land (p \lor r)Distributive (∨\lor over ∧\land)
p∧(q∨r)≡(p∧q)∨(p∧r)p \land (q \lor r) \equiv (p \land q) \lor (p \land r)Distributive (∧\land over ∨\lor)
¬(p∧q)≡¬p∨¬q\neg(p \land q) \equiv \neg p \lor \neg q · ¬(p∨q)≡¬p∧¬q\neg(p \lor q) \equiv \neg p \land \neg qDe Morgan
p∨(p∧q)≡pp \lor (p \land q) \equiv p · p∧(p∨q)≡pp \land (p \lor q) \equiv pAbsorption
p∨¬p≡Tp \lor \neg p \equiv \mathbf{T} · p∧¬p≡Fp \land \neg p \equiv \mathbf{F}Negation

And the ones with implications, which are the most useful for simplifying:

EquivalenceWhat it is for
p→q≡¬p∨qp \to q \equiv \neg p \lor qRemoving arrows: the most important one
p→q≡¬q→¬pp \to q \equiv \neg q \to \neg pContrapositive
¬(p→q)≡p∧¬q\neg(p \to q) \equiv p \land \neg qNegating an implication
(p→q)∧(p→r)≡p→(q∧r)(p \to q) \land (p \to r) \equiv p \to (q \land r)Joining conclusions
(p→r)∧(q→r)≡(p∨q)→r(p \to r) \land (q \to r) \equiv (p \lor q) \to rJoining hypotheses
p↔q≡(p→q)∧(q→p)p \leftrightarrow q \equiv (p \to q) \land (q \to p)Removing the double arrow
p↔q≡(p∧q)∨(¬p∧¬q)p \leftrightarrow q \equiv (p \land q) \lor (\neg p \land \neg q)“Same value”, case by case
¬(p↔q)≡p↔¬q≡p⊕q\neg(p \leftrightarrow q) \equiv p \leftrightarrow \neg q \equiv p \oplus qNegating a biconditional

An example with laws: negating the alarm rule

When does the alarm not ring? We have to negate (p∨q)∧r(p \lor q) \land r and leave it with no negation in front of brackets:

¬((p∨q)∧r)≡¬(p∨q)∨¬r(De Morgan) \neg\big((p \lor q) \land r\big) \equiv \neg(p \lor q) \lor \neg r \quad \text{(De Morgan)}
≡(¬p∧¬q)∨¬r(De Morgan again) \equiv (\neg p \land \neg q) \lor \neg r \quad \text{(De Morgan again)}

It reads: it does not ring when the door is shut and there is no motion, or when night mode is off. Each step carries the name of the law that justifies it: that is how an equivalence proof is written.

Another classic, simplifying (p∧q)∨(p∧¬q)(p \land q) \lor (p \land \neg q):

(p∧q)∨(p∧¬q)≡p∧(q∨¬q)(distributive) (p \land q) \lor (p \land \neg q) \equiv p \land (q \lor \neg q) \quad \text{(distributive)}
≡p∧T(negation)≡p(identity) \equiv p \land \mathbf{T} \quad \text{(negation)} \qquad \equiv p \quad \text{(identity)}

It makes sense: “the door is open and there is motion, or the door is open and there is none” is just “the door is open”.

To simplify, the order that almost always works is: remove arrows (p→q≡¬p∨qp \to q \equiv \neg p \lor q), push the negations in with De Morgan until they only apply to letters, and then distributive, absorption and negation.

Connectives on bits

In a computer, T is 1 and F is 0, and the connectives are applied bitwise to strings of the same length: AND (∧\land), OR (∨\lor) and XOR (⊕\oplus). That is what the &, | and ^ operators do in Python, C or Java. For example, with the sensors of a house with five zones:

Zone 12345
Motion10110
Armed zones11010
AND: alerts10010
OR11110
XOR01100

The AND says which zones have motion and are armed: 1 and 4. It is exactly the alarm rule, applied in five places at once.

For the curious. From any truth table you can get an equivalent formula: take each T row, write the conjunction of its letters (negating the ones that are F) and join them all with ∨\lor. That is the disjunctive normal form (DNF). Doing the opposite with the F rows gives the conjunctive one (CNF). They are not in the course notes, but this topic’s simulator gives them to you, and they explain why ¬\neg, ∧\land and ∨\lor are enough to write any formula.

Predicates and quantifiers

Sentences like “x>5x > 5” or “sensor xx detects motion” are not propositions: their value depends on xx. They are predicates. We write P(x)P(x): PP is the predicate (“detects motion”) and xx the variable (“the sensor”). When xx is given a value from the domain (the set of possible values), P(x)P(x) becomes a proposition. The part of logic that works with predicates is called predicate logic or first-order logic.

There is another way to turn a predicate into a proposition: quantifying it.

  • Universal quantifier, ∀x P(x)\forall x\, P(x): “for every xx in the domain, P(x)P(x)“. It is T if P(x)P(x) is T for all of them; one failure is enough to make it F.
  • Existential quantifier, ∃x P(x)\exists x\, P(x): “there is at least one xx with P(x)P(x)“. It is T if there is one; it is F only if P(x)P(x) is F for all of them.

If the domain is finite, {x1,…,xn}\{x_1, \dots, x_n\}, the quantifiers are long conjunctions and disjunctions:

∀x P(x)≡P(x1)∧P(x2)∧⋯∧P(xn) \forall x\, P(x) \equiv P(x_1) \land P(x_2) \land \dots \land P(x_n)
∃x P(x)≡P(x1)∨P(x2)∨⋯∨P(xn) \exists x\, P(x) \equiv P(x_1) \lor P(x_2) \lor \dots \lor P(x_n)

With the five sensors in the house, S1 to S5, and W(x)W(x): “sensor xx works”, “all the sensors work” is ∀x W(x)\forall x\, W(x) and “some sensor works” is ∃x W(x)\exists x\, W(x).

Bound and free variables

In ∀x P(x,y)\forall x\, P(x, y), xx is bound by the quantifier and yy is free. While any variable is free, the expression is not a proposition: it is missing a value.

Negating a quantifier

This is the rule that is asked about most. What does “it is not true that all the sensors work” mean? That at least one fails. And “there is no sensor that works” means that they all fail:

¬∀x P(x)≡∃x ¬P(x)¬∃x P(x)≡∀x ¬P(x) \neg \forall x\, P(x) \equiv \exists x\, \neg P(x) \qquad \neg \exists x\, P(x) \equiv \forall x\, \neg P(x)
Negating a quantifierFive sensors; on the left one fails and on the right all fail, to illustrate the two laws for negating quantifiers.¬∀x P(x) ≡ ∃x ¬P(x)¬∃x P(x) ≡ ∀x ¬P(x)S1okS2okS3failsS4okS5okS1failsS2failsS3failsS4failsS5failsit is not true that all of them work= at least one fails (S3)there is none that works= all of them fail
When you negate, the quantifier flips (∀ to ∃ and back) and the negation moves inside to the predicate.

These are De Morgan’s laws for quantifiers: the diagram shows that, with a finite domain, they are the usual ones applied to the long conjunction and disjunction.

Negating “all the sensors work” as “no sensor works”. That is going too far: one failure is enough. The negation of “all” is “not all”, that is, “some do not”.

Nested quantifiers

One quantifier can sit inside another: ∀x ∃y (x+y=0)\forall x\, \exists y\, (x + y = 0) says that every number has an opposite. Here the order matters:

  • ∀x ∃y (x+y=0)\forall x\, \exists y\, (x + y = 0) over the integers: T. For each xx, yy can be different (y=−xy = -x).
  • ∃y ∀x (x+y=0)\exists y\, \forall x\, (x + y = 0): F. It would say that there is a single yy that works for every xx at once.

To negate several, the negation goes through them one by one, flipping each quantifier: ¬∀x ∃y P(x,y)≡∃x ∀y ¬P(x,y)\neg \forall x\, \exists y\, P(x, y) \equiv \exists x\, \forall y\, \neg P(x, y).

Read ∀x ∃y\forall x\, \exists y as “you pick xx and I answer with a yy“, and ∃y ∀x\exists y\, \forall x as “I pick yy first and it has to work against anything you say”. The second is far more demanding.

Rules of inference: the valid steps

An argument is a list of hypotheses p1,p2,…,pnp_1, p_2, \dots, p_n and a conclusion qq. It is valid (or correct) if, whenever all the hypotheses are true, the conclusion is true as well; in other words, if

(p1∧p2∧⋯∧pn)→q (p_1 \land p_2 \land \dots \land p_n) \to q

is a tautology. Checking it with a 2n2^n-row table is slow, so we chain rules of inference, which are small arguments already proved:

RuleFrom…we deduce…Tautology
Modus ponenspp, p→qp \to qqq[p∧(p→q)]→q[p \land (p \to q)] \to q
Modus tollens¬q\neg q, p→qp \to q¬p\neg p[¬q∧(p→q)]→¬p[\neg q \land (p \to q)] \to \neg p
Hypothetical syllogismp→qp \to q, q→rq \to rp→rp \to r[(p→q)∧(q→r)]→(p→r)[(p \to q) \land (q \to r)] \to (p \to r)
Disjunctive syllogismp∨qp \lor q, ¬p\neg pqq[(p∨q)∧¬p]→q[(p \lor q) \land \neg p] \to q
Additionppp∨qp \lor qp→(p∨q)p \to (p \lor q)
Simplificationp∧qp \land qpp(p∧q)→p(p \land q) \to p
Conjunctionpp, qqp∧qp \land q(p∧q)→(p∧q)(p \land q) \to (p \land q)
Resolutionp∨qp \lor q, ¬p∨r\neg p \lor rq∨rq \lor r[(p∨q)∧(¬p∨r)]→(q∨r)[(p \lor q) \land (\neg p \lor r)] \to (q \lor r)

An example with the alarm. Hypotheses: “if the alarm rings, I wake up” (s→ws \to w), “if I wake up, I go down to the living room” (w→dw \to d) and “the alarm has rung” (ss). By hypothetical syllogism, s→ds \to d; by modus ponens with ss, dd: I go downstairs.

And the versions with quantifiers, used to go from “all” to “this one” and back:

RuleFrom…we deduce…
Universal instantiation∀x P(x)\forall x\, P(x)P(c)P(c) for a particular cc
Universal generalisationP(c)P(c) for an arbitrary cc∀x P(x)\forall x\, P(x)
Existential instantiation∃x P(x)\exists x\, P(x)P(c)P(c) for some cc
Existential generalisationP(c)P(c) for some cc∃x P(x)\exists x\, P(x)

The two classic fallacies look like rules but are not. Affirming the consequent: from p→qp \to q and qq concluding pp (“if there is motion it rings; it rang; so there was motion”: false, it could have been the door). Denying the antecedent: from p→qp \to q and ¬p\neg p concluding ¬q\neg q. Both fail for the same reason: mixing up an implication with its converse or its inverse.

Proof methods

A theorem is a statement that has been proved true; a proof is the chain of valid steps that establishes it from axioms, definitions and earlier theorems. Along the way you meet a lemma (a simpler auxiliary result), a corollary (something that follows directly from a theorem) and a conjecture (something believed true but not yet proved).

Almost every theorem has the form p→qp \to q (or ∀x (P(x)→Q(x))\forall x\, (P(x) \to Q(x))), and there are several ways to attack it:

Which proof method to chooseFour methods for an implication: direct, contrapositive, contradiction and a counterexample to refute, plus induction for statements about all integers.I want to prove p → qDirectassume p, reach qContrapositiveassume ¬q, reach ¬pContradictionassume p and ¬q: clashCounterexampleto knock down a ∀: one caseFor ∀n ≥ 1 over integers: induction (base + step P(k) → P(k+1))
If the hypothesis gives you nothing to work with, try the contrapositive or a contradiction.

In the examples I use the usual definitions: nn is even if n=2kn = 2k for some integer kk, and odd if n=2k+1n = 2k + 1.

Direct proof

Assume pp is true and, with definitions and valid steps, reach qq. Since p→qp \to q is only false when pp is T and qq is F, if pp always leads to qq, that row cannot happen.

Theorem. If nn is odd, n2n^2 is odd.

Proof. If nn is odd, n=2k+1n = 2k + 1. Then n2=4k2+4k+1=2(2k2+2k)+1n^2 = 4k^2 + 4k + 1 = 2(2k^2 + 2k) + 1, which has the form 2m+12m + 1 with m=2k2+2km = 2k^2 + 2k an integer. So n2n^2 is odd. ∎

By contrapositive (indirect proof)

Since p→q≡¬q→¬pp \to q \equiv \neg q \to \neg p, you can prove the contrapositive. It is used when the hypothesis pp gives little to work with and its negation gives more.

Theorem. If n2n^2 is even, nn is even.

Going straight at it is awkward: from n2=2kn^2 = 2k you cannot see what nn looks like. The contrapositive is “if nn is not even (it is odd), n2n^2 is not even (it is odd)”, which is exactly the previous theorem. ∎

By contradiction

Assume the opposite of what you want to prove and reach a contradiction (something both T and F). For p→qp \to q, assume p∧¬qp \land \neg q and look for the clash.

Theorem. 2\sqrt{2} is not rational.

Proof. Suppose it is: 2=a/b\sqrt{2} = a/b with aa and bb integers with no common factors. Squaring, a2=2b2a^2 = 2b^2, so a2a^2 is even and, by the previous theorem, aa is even: a=2ca = 2c. Substituting, 4c2=2b24c^2 = 2b^2, that is, b2=2c2b^2 = 2c^2: bb is even too. But then aa and bb have the factor 2 in common, against our assumption. Contradiction: 2\sqrt{2} is not rational. ∎

Vacuous, trivial and case-by-case proofs

  • Vacuous: if you show that the hypothesis pp is false, p→qp \to q is true without looking at qq (rows F→F \to anything). Example: “if nn is an integer with n2<0n^2 < 0, then n=7n = 7” is true, because no integer meets the hypothesis.
  • Trivial: if you show that the conclusion qq is true, p→qp \to q is true without looking at pp.
  • By cases: if the hypothesis is p1∨p2∨…p_1 \lor p_2 \lor \dots, prove each pi→qp_i \to q separately, because (p1∨p2)→q≡(p1→q)∧(p2→q)(p_1 \lor p_2) \to q \equiv (p_1 \to q) \land (p_2 \to q). Example: “n2+nn^2 + n is even” is proved with nn even and with nn odd.

Existence proofs and counterexamples

For a statement ∃x P(x)\exists x\, P(x):

  • Constructive: give a concrete xx that satisfies P(x)P(x). “There is an integer that is a sum of two squares in two different ways”: 50=12+72=52+5250 = 1^2 + 7^2 = 5^2 + 5^2.
  • Non-constructive: show that one exists without saying which, usually by contradiction.

And to knock down a ∀x P(x)\forall x\, P(x) one counterexample is enough: a single xx in the domain with P(x)P(x) false, because ¬∀x P(x)≡∃x ¬P(x)\neg \forall x\, P(x) \equiv \exists x\, \neg P(x). “Every number of the form n2+n+41n^2 + n + 41 is prime” sounds fine until n=41n = 41: 412+41+41=41⋅4341^2 + 41 + 41 = 41 \cdot 43.

“proving” a ∀\forall with examples. That n2+n+41n^2 + n + 41 is prime for n=0,1,2,…,39n = 0, 1, 2, \dots, 39 proves nothing about every nn. Examples are only good for refuting a ∀\forall (counterexample) or for proving an ∃\exists.

Induction

To prove that a property P(n)P(n) holds for every integer n≥1n \ge 1, examples do not work (there are infinitely many) and a direct proof often gets stuck. Mathematical induction solves it in two steps:

  1. Base case: prove P(1)P(1) (or the first value of the domain).
  2. Inductive step: prove that, for any k≥1k \ge 1, P(k)→P(k+1)P(k) \to P(k+1). P(k)P(k) is called the induction hypothesis.
Induction as dominoesA row of dominoes: the first falls (base case) and each one knocks over the next (inductive step).P(1)P(2)P(3)P(4)P(5)P(6)…base case: P(1) fallsinductive step: if P(k) falls, so does P(k+1)with both, all of them fall: ∀n ≥ 1, P(n)
Without a base case none fall; without an inductive step only the first one does.

It is like the diagram: if the first domino falls and each domino knocks over the next, they all fall. What holds it up is the well-ordering property: every non-empty set of non-negative integers has a smallest element. If P(n)P(n) failed for some nn, there would be a first failure mm, and since P(m−1)P(m-1) does hold, the inductive step would give P(m)P(m): a contradiction.

Theorem. For every n≥1n \ge 1, 1+2+⋯+n=n(n+1)21 + 2 + \dots + n = \dfrac{n(n+1)}{2}.

Base case (n=1n = 1): on the left, 1; on the right, 1⋅2/2=11 \cdot 2 / 2 = 1. They match.

Inductive step. Assume P(k)P(k): 1+⋯+k=k(k+1)21 + \dots + k = \frac{k(k+1)}{2}. We want to reach P(k+1)P(k+1), which is 1+⋯+k+(k+1)=(k+1)(k+2)21 + \dots + k + (k+1) = \frac{(k+1)(k+2)}{2}. Start from the left and use the hypothesis on the first kk terms:

(1+⋯+k)+(k+1)=k(k+1)2+(k+1)=k(k+1)+2(k+1)2=(k+1)(k+2)2 (1 + \dots + k) + (k + 1) = \frac{k(k+1)}{2} + (k+1) = \frac{k(k+1) + 2(k+1)}{2} = \frac{(k+1)(k+2)}{2}

which is exactly P(k+1)P(k+1). By induction, it holds for every n≥1n \ge 1. ∎

Quick check with n=4n = 4: 1+2+3+4=101 + 2 + 3 + 4 = 10 and 4⋅5/2=104 \cdot 5 / 2 = 10.

In the inductive step, first write what you want to get (P(k+1)P(k+1) with the formula) and then look, inside the expression for k+1k+1, for the piece that is P(k)P(k). That is the moment to use the hypothesis; if you do not use it anywhere, the proof is not by induction.

Skipping the base case because “it is obvious”. Without it you can “prove” anything: “n=n+1n = n + 1” passes the inductive step (if k=k+1k = k + 1, adding 1 gives k+1=k+2k + 1 = k + 2) and is false for every nn.

Check it with Python

A truth table is a loop over the 2n2^n combinations. With itertools.product they come out in the same order as on paper (starting with T), and the implication is written as (not a) or b, which is exactly the equivalence p→q≡¬p∨qp \to q \equiv \neg p \lor q. This program prints the alarm’s table, classifies it and checks one of the equivalences from the theory:

from itertools import product

def implies(a, b):            # p -> q is the same as (not p) or q
    return (not a) or b

def alarm(p, q, r):           # the installer's rule
    return (p or q) and r

def tf(x):                    # True/False -> T/F, as on paper
    return "T" if x else "F"

print(" p q r | p∨q | (p∨q)∧r")
last = []
for p, q, r in product([True, False], repeat=3):   # 2^3 = 8 rows
    s = alarm(p, q, r)
    last.append(s)
    print(f" {tf(p)} {tf(q)} {tf(r)} |  {tf(p or q)}  |    {tf(s)}")

if all(last):
    print("tautology")
elif not any(last):
    print("contradiction")
else:
    print("contingency:", last.count(True), "T rows out of", len(last))

# is not((p∨q)∧r) equivalent to (¬p∧¬q)∨¬r? Compare the 8 rows
eq = all((not alarm(p, q, r)) == ((not p and not q) or not r)
         for p, q, r in product([True, False], repeat=3))
print("De Morgan checked:", eq)

Output:

 p q r | p∨q | (p∨q)∧r
 T T T |  T  |    T
 T T F |  T  |    F
 T F T |  T  |    T
 T F F |  T  |    F
 F T T |  T  |    T
 F T F |  T  |    F
 F F T |  F  |    F
 F F F |  F  |    F
contingency: 3 T rows out of 8
De Morgan checked: True

What to look at:

  1. The rows come out in paper order: product([True, False], repeat=3) changes the last variable first, just like the table above.
  2. The last column has 3 T out of 8: the alarm is a contingency. The three T rows are the ones with rr T and at least one of pp or qq T.
  3. all(...) and any(...) are Python’s quantifiers: all is ∀\forall and any is ∃\exists over the rows.
  4. The final equivalence is the negation with De Morgan we did by hand: comparing the eight rows is exactly the definition of ≡\equiv.

In the “Truth table and equivalences” simulator you type any proposition (with the symbols or with the keyboard) and it builds the table column by column, subformula by subformula, tells you whether it is a tautology, a contradiction or a contingency, and checks whether two formulas are equivalent. It is the best way to check your tables from the exercises.

Summary

ConceptWhat it isHow to recognise or use it
PropositionSentence that is T or F, not bothNo free variables, no questions or orders
p→qp \to qF only if pp T and qq FSufficient on the left, necessary on the right
Contrapositive¬q→¬p\neg q \to \neg pEquivalent to p→qp \to q; the converse is not
Truth tableValue in the 2n2^n rowsOne column per subformula, inside out
Tautology / contradiction / contingencyAll T / all F / mixedLook at the last column
P≡QP \equiv QSame tableTables or laws (De Morgan, distributive…)
¬∀x P(x)\neg \forall x\, P(x)∃x ¬P(x)\exists x\, \neg P(x)One counterexample is enough
Valid argument(p1∧⋯∧pn)→q(p_1 \land \dots \land p_n) \to q a tautologyModus ponens, tollens, syllogisms
ProofDirect, contrapositive, contradiction, casesPick according to what the hypothesis gives
InductionBase case + P(k)→P(k+1)P(k) \to P(k+1)For ∀n≥1\forall n \ge 1 over the integers

Three ideas to take away:

  1. An implication is false in only one row (T → F). Almost every mistake in the topic, from “only if” to the fallacies, comes from forgetting it or from mixing it up with its converse.
  2. A well-built truth table is mechanical: 2n2^n rows in the usual order and one column per subformula. The last column decides whether it is a tautology, a contradiction or a contingency, and two formulas are equivalent if their last columns match.
  3. Proving is chaining valid steps. If the hypothesis gives you nothing, try the contrapositive or a contradiction; if the statement is about every nn, induction, and if it is false, one counterexample is enough.

Now it is your turn: in the logic and proofs guided practice we solve the classroom problems step by step (formalising, truth tables, equivalences, quantifiers, proofs and a table in the exam’s format), each one checked with Python. And to check you have got it, the logic and proofs exercises go from easy to hard and come with commented solutions and a theory quiz like the ones on the Campus.

Next step: practiceLogic and proofs — guided practice with the alarm’s rules, eight-row truth tables and an induction that falls like dominoes (and the table I handed in with a row missing)

Did this article help you? Here is how you can support SergioLearns →