taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Volume P1 · Proof, induction and invariants

Logic and quantifiers

Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.

Read first
Contents
  1. Statements and connectives
  2. Logical equivalence and the laws of logic
  3. Predicates and quantifiers

Statements and connectives

Statement

A proof speaks about statements. This section says what a statement is, defines the five connectives that build new statements from old ones, and studies the implication, which is the connective that a proof uses most.

Definition P1.1.1 (statement). A statement is a sentence that is either true or false, and never both. Its truth value is T when it is true and F when it is false. A sentence with a free variable, such as "x+1=5x + 1 = 5", is not a statement until a value is given to xx.

Lean: the type Prop holds the statements of Lean, and True and False are the two constant statements (Lean core).

Example P1.1.2. "1717 is prime" is a statement, and its truth value is T. "210=10002^{10} = 1000" is a statement with truth value F, since 210=10242^{10} = 1024. Both are statements, and the second one is false: a false statement is still a statement.

Example P1.1.3. "Close the door" is not a statement, and neither is "nn is even" when nn has no value. For n=6n = 6 the sentence "nn is even" becomes the true statement "66 is even". Section 3 treats such sentences as predicates.

Definition P1.1.4 (connectives). Let PP and QQ be statements. The statement ¬P\neg P (not PP) is true when PP is false. The statement P∧QP \wedge Q (PP and QQ) is true when both are true. The statement P∨QP \vee Q (PP or QQ) is true when at least one is true. The statement P→QP \to Q (PP implies QQ, or if PP then QQ) is false exactly when PP is true and QQ is false. The statement P↔QP \leftrightarrow Q (PP if and only if QQ) is true exactly when PP and QQ have the same truth value. The table gives all five.

PP QQ ¬P\neg P P∧QP \wedge Q P∨QP \vee Q P→QP \to Q P↔QP \leftrightarrow Q
T T F T T T T
T F F F T F F
F T T F T T F
F F T F F T T

Lean: Not, And (written ∧), Or (∨), implication as the arrow →, and Iff (↔) (Lean core).

Example P1.1.5. Let PP be "5>35 > 3" (true) and QQ be "55 is even" (false). Then P∧QP \wedge Q is F, P∨QP \vee Q is T, P→QP \to Q is F and Q→PQ \to P is T. The statement ¬Q\neg Q is T, so P∧¬QP \wedge \neg Q is T.

Example P1.1.6. The word "or" of daily speech may mean "one or the other, but not both". The connective ∨\vee is the inclusive or: it is true also when both parts are true. So "3<53 < 5 or 4<64 < 6" is true. The exclusive or, written P⊕QP \oplus Q, is the statement (P∨Q)∧¬(P∧Q)(P \vee Q) \wedge \neg (P \wedge Q), and it is true exactly when PP and QQ have different truth values.

Definition P1.1.7 (converse, contrapositive, negation of an implication). For the implication P→QP \to Q, the converse is Q→PQ \to P, the contrapositive is ¬Q→¬P\neg Q \to \neg P, and the inverse is ¬P→¬Q\neg P \to \neg Q. In P→QP \to Q, the statement PP is the hypothesis and QQ is the conclusion.

Example P1.1.8. Take "if nn is divisible by 44, then nn is even". The converse is "if nn is even, then nn is divisible by 44". The contrapositive is "if nn is odd, then nn is not divisible by 44". The original and the contrapositive are true for every integer nn. The converse is false: n=6n = 6 is even and 4∤64 \nmid 6.

Example P1.1.9. Take "if x>2x > 2, then x2>4x^2 > 4" for real xx. The contrapositive is "if x2≤4x^2 \le 4, then x≤2x \le 2", which is true. The converse is "if x2>4x^2 > 4, then x>2x > 2", which is false for x=−3x = -3.

Theorem P1.1.10 (the implication). For all statements PP and QQ:

  1. P→QP \to Q has the same truth value as ¬P∨Q\neg P \vee Q;
  2. P→QP \to Q has the same truth value as its contrapositive ¬Q→¬P\neg Q \to \neg P;
  3. ¬(P→Q)\neg (P \to Q) has the same truth value as P∧¬QP \wedge \neg Q;
  4. P↔QP \leftrightarrow Q has the same truth value as (P→Q)∧(Q→P)(P \to Q) \wedge (Q \to P);
  5. P→QP \to Q and its converse Q→PQ \to P do not always have the same truth value.

Lean: imp_iff_not_or (rule 1), not_imp_not (rule 2), Classical.not_imp or not_imp (rule 3) and iff_def (rule 4); each is in Mathlib or Lean core, and the exact names are listed in Chapter 9.

Proof. A statement built from PP and QQ has its truth value fixed by the truth values of PP and QQ, so it is enough to check the four rows of the table. Rule 1: in the rows TT, TF, FT, FF the statement P→QP \to Q is T, F, T, T. The statement ¬P∨Q\neg P \vee Q is F or T, F or F, T or T, T or F, that is T, F, T, T. The two columns agree. Rule 2: the contrapositive in the four rows is ¬Q→¬P\neg Q \to \neg P with (¬Q,¬P)(\neg Q, \neg P) equal to (F, F), (T, F), (F, T), (T, T), so it is T, F, T, T. Again the columns agree. Rule 3: the statement ¬(P→Q)\neg(P \to Q) is F, T, F, F in the four rows, and P∧¬QP \wedge \neg Q is F, T, F, F. Rule 4: in the four rows (P→Q)∧(Q→P)(P \to Q) \wedge (Q \to P) is T, F, F, T, which is the column of P↔QP \leftrightarrow Q. Rule 5: for PP = F and QQ = T the implication P→QP \to Q is T and the converse Q→PQ \to P is F. □\square

Example P1.1.11. Rule 3 tells how to deny an implication. The denial of "if nn is prime, then nn is odd" is "nn is prime and nn is even". The number n=2n = 2 makes the denial true, so the implication is false.

Example P1.1.12. Rule 1 turns a hypothesis into a case. "If x≠0x \ne 0 then x2>0x^2 > 0" says the same as "x=0x = 0 or x2>0x^2 > 0". The second form is true for every real xx by direct inspection of the two cases.

Definition P1.1.13 (tautology and contradiction). A statement form is a tautology when it is true in every row of its truth table, and a contradiction when it is false in every row.

Example P1.1.14. P∨¬PP \vee \neg P is a tautology. The row with PP = T gives T, and the row with PP = F gives T. The form P∧¬PP \wedge \neg P is a contradiction.

Example P1.1.15. The form (P→Q)∧(Q→R)→(P→R)(P \to Q) \wedge (Q \to R) \to (P \to R) has three variables and so eight rows. It is a tautology. A row can make the whole form false only if P→RP \to R is false, that is PP = T and RR = F. Then QQ is T or F. If QQ = T, then Q→RQ \to R is F. If QQ = F, then P→QP \to Q is F. In both cases the hypothesis is F and the whole implication is T.

Proof

Every proof in this section is a check of all rows of a table. This method is complete: a statement form with nn variables has 2n2^n rows, and the form is a tautology exactly when the check finds no row with the value F. The method is also slow for large nn, so the next section gives laws that reduce one form to another by steps.

The implication has the rows that surprise a beginner: it is true when the hypothesis is false. The reason is the use that the implication has. A statement "if PP then QQ" is a promise about the case where PP holds. When PP does not hold, the promise is not broken. The statement "for all nn, if n>3n > 3 then n2>9n^2 > 9" must be true for n=1n = 1, where the hypothesis is false, and the table gives that value.

Examples

Example P1.1.16 (a puzzle with a table). Three people AA, BB and CC each say one sentence. AA says "BB lies". BB says "CC lies". CC says "AA and BB both lie". Let aa, bb and cc be T when the person tells the truth. The sentences give a↔¬ba \leftrightarrow \neg b, b↔¬cb \leftrightarrow \neg c and c↔(¬a∧¬b)c \leftrightarrow (\neg a \wedge \neg b). The second equation gives b=¬cb = \neg c and the first gives a=¬b=ca = \neg b = c. The third becomes c↔(¬c∧c)c \leftrightarrow (\neg c \wedge c), and the right side is F, so cc = F. Then bb = T and aa = F. A check of the three sentences with these values confirms them. BB tells the truth, and AA and CC lie.

Example P1.1.17 (counting rows). Two statements PP and QQ give four rows. A connective of two variables is a choice of T or F for each row, so there are 24=162^4 = 16 of them. The five connectives of the definition are five of the sixteen. Exactly (43)=4\binom{4}{3} = 4 of the sixteen are true in exactly three rows: they are P→QP \to Q, Q→PQ \to P, P∨QP \vee Q and ¬(P∧Q)\neg(P \wedge Q). For nn statements there are 22n2^{2^n} connectives, and for n=3n = 3 the number is 256256.

Example P1.1.18 (a chain of implications). Let PP be "n>5n > 5", QQ be "n>3n > 3" and RR be "n>0n > 0" for an integer nn. The statements P→QP \to Q and Q→RQ \to R are true for all integers nn. The tautology above says that P→RP \to R is true, that is "n>5n > 5 implies n>0n > 0". A proof often uses the pattern in this way, in a chain with many links.

Exercises

  1. [10] Write the truth table of P∧¬QP \wedge \neg Q and of ¬(P∨Q)\neg (P \vee Q).
  2. [10] Decide the truth value of each statement: (a) if 33 is even then 44 is odd; (b) if 44 is even then 33 is odd; (c) 7<57 < 5 or 5<75 < 7; (d) 2+2=52 + 2 = 5 if and only if 3+3=73 + 3 = 7.
  3. [12] Write the converse, the contrapositive and the denial of "if nn is prime and n>2n > 2, then nn is odd". Say which of the three are true for all integers n>1n > 1.
  4. [15] Show by a table that (P→Q)∧(Q→P)(P \to Q) \wedge (Q \to P) has the same truth value as P↔QP \leftrightarrow Q and that ¬(P↔Q)\neg(P \leftrightarrow Q) has the same truth value as P⊕QP \oplus Q.
  5. [M18] Show that every connective of two variables that is true in exactly one row is a conjunction of PP or ¬P\neg P with QQ or ¬Q\neg Q. Say how this gives each of the sixteen connectives from ¬\neg, ∧\wedge and ∨\vee.
  6. [M20] The Sheffer stroke P∣QP \mid Q is ¬(P∧Q)\neg (P \wedge Q). Show that ¬P\neg P has the same truth value as P∣PP \mid P, that P∧QP \wedge Q has the same value as (P∣Q)∣(P∣Q)(P \mid Q) \mid (P \mid Q), and find a form with ∣\mid alone for P∨QP \vee Q.
  7. [M22] A form with the implication as its only connective and with the constant F is written with →\to and ⊥\bot. Show that ¬P\neg P, P∨QP \vee Q and P∧QP \wedge Q can each be written with →\to and ⊥\bot alone.
  8. [M25] Three people each say one sentence: AA says "exactly one of BB and CC lies", BB says "AA lies", and CC says "BB lies or AA tells the truth". Find who tells the truth.
  9. [M25] Show that (P→Q)→P(P \to Q) \to P is true exactly when PP is true. Give a plain-language example, and say what the result tells about a proof of PP.

Lean

The two truth values are the Lean statements True and False. For a decidable statement, the tactic decide checks a truth table. For example, example : ∀ P Q : Bool, (P && Q) = (Q && P) := by decide runs through all four rows. For statements of type Prop, Mathlib proves the rules above as imp_iff_not_or, not_imp_not and not_imp; Chapter 9 shows how to prove them by hand with intro, by_cases and simp.

Notes and history

The truth table in its present form is due to Wittgenstein and to Post, who both wrote it in 1921 and 1922, but the idea of connectives with fixed truth values goes back to Frege and to Boole. The Sheffer stroke of Exercise 6 is named for Sheffer, who showed in 1913 that one connective is enough, and Peirce had found the same fact earlier in unpublished notes.

TAOCP Section 1.1 uses these connectives in the conditions of its steps, and the "if" of a step is an implication that the algorithm acts on only when the hypothesis is true. Velleman, How to Prove It, Chapter 1, and Lehman, Leighton and Meyer, Mathematics for Computer Science, Chapter 3, treat the same material with other examples.

Logical equivalence and the laws of logic

Statement

A proof often changes a statement into another statement that says the same and is easier to use. This section defines what "says the same" means for statement forms, lists the laws that allow the change, and shows how to use the laws in place of a table.

Definition P1.1.19 (logical equivalence). Two statement forms AA and BB in the variables P1,…,PnP_1, \dots, P_n are logically equivalent, written A≡BA \equiv B, when they have the same truth value in each of the 2n2^n rows of the table. Equivalently, A↔BA \leftrightarrow B is a tautology.

Lean: for Prop the relation Iff; a law is a theorem A ↔ B (Lean core).

Example P1.1.20. P→Q≡¬P∨QP \to Q \equiv \neg P \vee Q by Rule 1 of the theorem of Section 1. The forms P∧QP \wedge Q and P∨QP \vee Q are not equivalent: they differ in the rows TF and FT.

Example P1.1.21. The forms P→(Q→R)P \to (Q \to R) and (P∧Q)→R(P \wedge Q) \to R are equivalent. Both are F in exactly one row of the eight, the row with PP = T, QQ = T and RR = F, and they are T in the other seven.

Theorem P1.1.22 (the laws of logic). For all statement forms PP, QQ and RR:

  1. Double negation: ¬¬P≡P\neg\neg P \equiv P.
  2. Commutative laws: P∧Q≡Q∧PP \wedge Q \equiv Q \wedge P and P∨Q≡Q∨PP \vee Q \equiv Q \vee P.
  3. Associative laws: (P∧Q)∧R≡P∧(Q∧R)(P \wedge Q) \wedge R \equiv P \wedge (Q \wedge R) and the same for ∨\vee.
  4. Distributive laws: P∧(Q∨R)≡(P∧Q)∨(P∧R)P \wedge (Q \vee R) \equiv (P \wedge Q) \vee (P \wedge R) and P∨(Q∧R)≡(P∨Q)∧(P∨R)P \vee (Q \wedge R) \equiv (P \vee Q) \wedge (P \vee R).
  5. De Morgan laws: ¬(P∧Q)≡¬P∨¬Q\neg (P \wedge Q) \equiv \neg P \vee \neg Q and ¬(P∨Q)≡¬P∧¬Q\neg (P \vee Q) \equiv \neg P \wedge \neg Q.
  6. Idempotent laws: P∧P≡PP \wedge P \equiv P and P∨P≡PP \vee P \equiv P.
  7. Absorption: P∨(P∧Q)≡PP \vee (P \wedge Q) \equiv P and P∧(P∨Q)≡PP \wedge (P \vee Q) \equiv P.
  8. Identity and domination: P∧⊤≡PP \wedge \top \equiv P, P∨⊥≡PP \vee \bot \equiv P, P∧⊥≡⊥P \wedge \bot \equiv \bot and P∨⊤≡⊤P \vee \top \equiv \top, where ⊤\top is a true statement and ⊥\bot a false one.
  9. Complement: P∨¬P≡⊤P \vee \neg P \equiv \top and P∧¬P≡⊥P \wedge \neg P \equiv \bot.
  10. Exportation: (P∧Q)→R≡P→(Q→R)(P \wedge Q) \to R \equiv P \to (Q \to R).

Lean: not_not, and_comm, or_comm, and_assoc, or_assoc, and_or_left, or_and_left, not_and_or, not_or, and_self, or_self, and_imp (rule 10); each is in Mathlib or Lean core (Chapter 9 checks the list of names).

Proof. Each law has at most three variables. For each one, write the table and compare the two columns. This section proves in full the two laws that are not clear from the table of Section 1.

De Morgan, first law. The form ¬(P∧Q)\neg(P \wedge Q) has the values F, T, T, T in the rows TT, TF, FT, FF. The form ¬P∨¬Q\neg P \vee \neg Q has the values F or F, F or T, T or F, T or T, which are F, T, T, T. The columns agree.

Second distributive law. The form P∨(Q∧R)P \vee (Q \wedge R) is T when PP is T. When PP is F, it has the value of Q∧RQ \wedge R. The form (P∨Q)∧(P∨R)(P \vee Q) \wedge (P \vee R) is T when PP is T, since both parts are T. When PP is F, it equals Q∧RQ \wedge R. So the two forms agree in every row. The first distributive law has the same argument with the roles of T and F, and of ∧\wedge and ∨\vee, exchanged.

Absorption. When PP is T, P∨(P∧Q)P \vee (P \wedge Q) is T. When PP is F, then P∧QP \wedge Q is F, and the form is F. So the form has the value of PP. The second law is the same. Exportation: both forms are F in exactly the row PP = T, QQ = T, RR = F. In this row (P∧Q)→R(P \wedge Q) \to R has T hypothesis and F conclusion, and P→(Q→R)P \to (Q \to R) has T hypothesis and the conclusion T→FT \to F, which is F. In every other row both forms are T, since a false hypothesis anywhere in the chain gives T, and a true conclusion gives T. The remaining laws follow from the table in the same way. □\square

Example P1.1.23. Deny the statement "x>0x > 0 and x<10x < 10" with the first De Morgan law: the denial is "x≤0x \le 0 or x≥10x \ge 10". Deny "nn is even or n>100n > 100" with the second law: the denial is "nn is odd and n≤100n \le 100".

Example P1.1.24. Exportation says that a proof of "if AA and BB, then CC" may assume AA, then assume BB, and then prove CC. For AA: "nn is divisible by 44", BB: "n>0n > 0", CC: "n≥4n \ge 4", the proof assumes 4∣n4 \mid n and n>0n > 0 and shows n≥4n \ge 4, since nn is a positive multiple of 44.

Theorem P1.1.25 (replacement). Let A≡BA \equiv B. If FF is a statement form that contains AA as a part, and F′F' is the form that results when this part is replaced by BB, then F≡F′F \equiv F'.

Lean: the tactic rw with a theorem of the form A ↔ B replaces A by B in a goal (Lean core).

Proof. Take any row of the table of FF. In this row, AA and BB have the same truth value, and the truth value of FF depends on the truth values of its parts only through the values of its parts. So FF and F′F' have the same value in this row. □\square

Example P1.1.26. Simplify (P→Q)∧(P→¬Q)(P \to Q) \wedge (P \to \neg Q). The rule P→Q≡¬P∨QP \to Q \equiv \neg P \vee Q gives (¬P∨Q)∧(¬P∨¬Q)(\neg P \vee Q) \wedge (\neg P \vee \neg Q). The second distributive law, read from right to left with ¬P\neg P in the role of PP, gives ¬P∨(Q∧¬Q)\neg P \vee (Q \wedge \neg Q). The complement law gives ¬P∨⊥\neg P \vee \bot, and the identity law gives ¬P\neg P. A table with four rows confirms it: both sides are F in the rows TT and TF, and T in the rows FT and FF.

Example P1.1.27. The form ¬(P→Q)\neg (P \to Q) is ¬(¬P∨Q)\neg(\neg P \vee Q), which is ¬¬P∧¬Q\neg\neg P \wedge \neg Q by the second De Morgan law, and so P∧¬QP \wedge \neg Q. This is Rule 3 of the theorem in Section 1, now proved from the laws.

Definition P1.1.28 (disjunctive normal form). A literal is a variable or the negation of a variable. A conjunction of literals with each variable once is a minterm. A form is in disjunctive normal form (DNF) when it is a disjunction of minterms.

Lean: no name; the form is built by hand, or by Decidable evaluation of a Bool function.

Example P1.1.29. The form P⊕QP \oplus Q is true in the rows TF and FT. Its DNF is (P∧¬Q)∨(¬P∧Q)(P \wedge \neg Q) \vee (\neg P \wedge Q). The form P↔QP \leftrightarrow Q is true in the rows TT and FF, and its DNF is (P∧Q)∨(¬P∧¬Q)(P \wedge Q) \vee (\neg P \wedge \neg Q).

Example P1.1.30. The majority of three statements, true when at least two of PP, QQ and RR are true, holds in the rows TTF, TFT, FTT and TTT. Its DNF has four minterms: (P∧Q∧¬R)∨(P∧¬Q∧R)∨(¬P∧Q∧R)∨(P∧Q∧R)(P \wedge Q \wedge \neg R) \vee (P \wedge \neg Q \wedge R) \vee (\neg P \wedge Q \wedge R) \vee (P \wedge Q \wedge R).

Theorem P1.1.31 (normal form). Every statement form with at least one row of value T is equivalent to a form in DNF. Every statement form is equivalent to a form built from ¬\neg, ∧\wedge and ∨\vee alone.

Lean: no name; for a given form the tactic tauto proves the equivalence of the form and its DNF (Mathlib).

Proof. Let the form AA have the variables P1,…,PnP_1, \dots, P_n. For each row where AA is T, write the minterm that is T in this row and F in every other row: take the literal PiP_i when the row has T for PiP_i, and ¬Pi\neg P_i when it has F. The disjunction of these minterms is T exactly in the rows where AA is T. So it is equivalent to AA. For the second claim, a form with no row of value T is a contradiction, and P1∧¬P1P_1 \wedge \neg P_1 is equivalent to it. □\square

Proof

The laws of this section have all the same proof: a table with at most eight rows. The reader should not memorize the list. Learn three groups instead: the laws that do not change the structure (commutative, associative, idempotent, double negation), the laws of De Morgan and the distributive laws that move a negation or a connective inside a bracket, and the laws that remove an implication. A proof then reads as a calculation, each line equivalent to the line before, with the law named at each step.

A calculation by laws is exact and short for small forms. A table is more reliable when the form has a pattern that is not clear. The theorem of replacement is the reason that a calculation is allowed to change a part of a form without looking at the whole.

Examples

Example P1.1.32 (a calculation). Show that (P∨Q)∧(¬P∨R)→(Q∨R)(P \vee Q) \wedge (\neg P \vee R) \to (Q \vee R) is a tautology. The check of the eight rows gives T in every row. A short argument gives the same: if PP is T, then ¬P∨R\neg P \vee R gives RR, and Q∨RQ \vee R is T. If PP is F, then P∨QP \vee Q gives QQ, and Q∨RQ \vee R is T. This rule is the resolution rule, and it is the base of many programs that prove statements.

Example P1.1.33 (the number of rows). A DNF of a form with nn variables has at most 2n2^n minterms, one for each row. The form P1⊕P2⊕P3P_1 \oplus P_2 \oplus P_3 is T when an odd number of the three variables is T. This is the rows TFF, FTF, FFT and TTT, so 44 of 88 rows, and its DNF has four minterms.

Example P1.1.34 (a form that does not simplify as expected). The forms (P→Q)→R(P \to Q) \to R and P→(Q→R)P \to (Q \to R) are not equivalent: in the rows with PP = F, QQ = F, RR = F and with PP = F, QQ = T, RR = F the first form is F and the second is T. So an expression with two implications needs brackets. The convention of Mathlib and of Lean is that P→Q→RP \to Q \to R means P→(Q→R)P \to (Q \to R).

Exercises

  1. [10] Use the laws to show ¬(P→Q)≡P∧¬Q\neg(P \to Q) \equiv P \wedge \neg Q and ¬(P∧¬Q)≡P→Q\neg (P \wedge \neg Q) \equiv P \to Q.
  2. [12] Write the denial of each statement with no negation sign left in front of a bracket: (a) x≥3x \ge 3 and y<2y < 2; (b) nn is prime or nn is a square; (c) if a>0a > 0 then a2>0a^2 > 0.
  3. [15] Write the DNF of P→QP \to Q and of (P→Q)∧(Q→P)(P \to Q) \wedge (Q \to P).
  4. [M16] Show by a table that P∧(P→Q)→QP \wedge (P \to Q) \to Q is a tautology. Give a one-line argument that does not use a table.
  5. [M18] Show that (P∨Q)∧¬P→Q(P \vee Q) \wedge \neg P \to Q is a tautology (the rule of the disjunctive syllogism), and that (P→Q)∧¬Q→¬P(P \to Q) \wedge \neg Q \to \neg P is a tautology (the rule of the modus tollens).
  6. [M20] Prove the first distributive law and the second De Morgan law from the other laws of the theorem, with no table for them.
  7. [M22] Show that (P→Q)∧(R→Q)≡(P∨R)→Q(P \to Q) \wedge (R \to Q) \equiv (P \vee R) \to Q, and give an example with numbers.
  8. [M24] Show that P↔(Q↔R)≡(P↔Q)↔RP \leftrightarrow (Q \leftrightarrow R) \equiv (P \leftrightarrow Q) \leftrightarrow R, so the connective ↔\leftrightarrow is associative, and find in how many of the eight rows the form is T.
  9. [M28] Count the forms in the variables PP and QQ up to equivalence that are built only from ∧\wedge and ∨\vee with no negation and no constants, and give a list of them.

Lean

The laws of the theorem are theorems of Mathlib or Lean core. The names are not_not for ¬¬P↔P\neg\neg P \leftrightarrow P, and_comm and or_comm, and_assoc and or_assoc, and_or_left and or_and_left for the distributive laws, not_and_or and not_or for the two laws of De Morgan, and and_imp for the exportation. The theorem of replacement is the tactic rw. A table of two or three variables is checked by decide on Bool, and a statement of Prop is proved by tauto, which proves each propositional tautology. Chapter 9 shows these tools on the laws of this section.

Notes and history

De Morgan stated his two laws in 1847. The algebra of Boole, from 1847 and 1854, treats ∧\wedge and ∨\vee as multiplication and addition of a two-element system, and the distributive law of ∨\vee over ∧\wedge is the law that has no counterpart in the algebra of numbers. The rule of resolution of the example is due to Robinson, who used it in 1965 for a program that proves theorems.

The normal forms of this section appear in TAOCP in Volume 4, in the work on Boolean functions. This volume needs only the laws for the rewriting of conditions in a proof and in a program, such as the exit condition of a loop, which Chapter 8 negates.

Predicates and quantifiers

Statement

Most statements of mathematics speak about all the elements of a set or about some element of it. This section defines the two quantifiers that say it, gives the rule for the denial of a quantified statement, and explains why the order of two quantifiers matters.

Definition P1.1.35 (predicate). Let DD be a set. A predicate P(x)P(x) on DD is a sentence with a free variable xx such that P(a)P(a) is a statement for each a∈Da \in D. The set DD is the domain of the variable. A predicate with two variables P(x,y)P(x, y) is defined in the same way on a domain for each variable.

Lean: a predicate on α is a function α → Prop (Lean core).

Example P1.1.36. "xx is prime" is a predicate on the positive integers. P(7)P(7) is true and P(8)P(8) is false. The set {x∈D:P(x)}\{x \in D : P(x)\} of the values that make it true is the truth set, and for D={1,…,20}D = \{1, \dots, 20\} it is {2,3,5,7,11,13,17,19}\{2, 3, 5, 7, 11, 13, 17, 19\}.

Example P1.1.37. "x+y=10x + y = 10" is a predicate on pairs of integers. P(3,7)P(3, 7) is true, P(3,8)P(3, 8) is false, and for a fixed xx the sentence is a predicate in yy alone.

Definition P1.1.38 (quantifiers). The statement ∀x∈D P(x)\forall x \in D\ P(x) (for all xx in DD, P(x)P(x)) is true when P(a)P(a) is true for every a∈Da \in D. The statement ∃x∈D P(x)\exists x \in D\ P(x) (there exists xx in DD with P(x)P(x)) is true when P(a)P(a) is true for at least one a∈Da \in D. The symbol ∀\forall is the universal quantifier and ∃\exists is the existential quantifier. The statement ∃!x∈D P(x)\exists! x \in D\ P(x) (there is exactly one xx) is true when P(a)P(a) is true for exactly one a∈Da \in D.

Lean: ∀ x : α, P x, ∃ x : α, P x and ∃! x : α, P x (Lean core and Mathlib).

Example P1.1.39. Over the integers, ∀x (x2≥x)\forall x\ (x^2 \ge x) is true, since x2−x=x(x−1)x^2 - x = x(x-1) is the product of two consecutive integers, and it is never negative. The statement ∃x (x2<x)\exists x\ (x^2 < x) is false, for the same reason. Over the real numbers, x=1/2x = 1/2 has x2<xx^2 < x, so the second statement is true there and the first is false. The domain is part of the statement.

Example P1.1.40. The statement ∀n∈ℕ (1+3+⋯+(2n−1)=n2)\forall n \in \mathbb{N}\ (1 + 3 + \cdots + (2n - 1) = n^2), with the empty sum 00 for n=0n = 0, has a universal quantifier over an infinite domain. Its truth cannot be checked by a table. Chapter 4 proves it by induction. The statement ∃!x∈ℕ (x2=4)\exists! x \in \mathbb{N}\ (x^2 = 4) is true, since only x=2x = 2 works. Over ℤ\mathbb{Z} it is false, since x=−2x = -2 also works.

Definition P1.1.41 (bound and free variables). In ∀x P(x,y)\forall x\ P(x, y) the variable xx is bound by the quantifier, and yy is free. A statement has no free variable. The name of a bound variable does not matter: ∀x P(x)\forall x\ P(x) and ∀t P(t)\forall t\ P(t) say the same.

Example P1.1.42. In ∃k (n=2k)\exists k\ (n = 2k) over the integers, kk is bound and nn is free. For n=10n = 10 it is true and for n=7n = 7 it is false. So the sentence says "nn is even". It is a predicate in nn.

Example P1.1.43. In ∀x ∃y (y>x)\forall x\ \exists y\ (y > x) over ℕ\mathbb{N} there are two bound variables and no free one. It is a statement, and it is true.

Theorem P1.1.44 (denial of a quantified statement). For every predicate PP on a domain DD:

  1. ¬∀x∈D P(x)≡∃x∈D ¬P(x)\neg \forall x \in D\ P(x) \equiv \exists x \in D\ \neg P(x);
  2. ¬∃x∈D P(x)≡∀x∈D ¬P(x)\neg \exists x \in D\ P(x) \equiv \forall x \in D\ \neg P(x).

The same laws hold for each quantifier in front of a longer statement, so ¬ ∀x ∃y P(x,y)\neg\, \forall x\, \exists y\, P(x, y) is ∃x ∀y ¬P(x,y)\exists x\, \forall y\, \neg P(x, y).

Lean: not_forall and not_exists (Mathlib); not_forall needs classical logic, which Mathlib uses everywhere.

Proof. Rule 1. The statement ∀x P(x)\forall x\ P(x) is false exactly when P(a)P(a) is false for at least one a∈Da \in D. That is the same as saying that ¬P(a)\neg P(a) is true for at least one aa, which is ∃x ¬P(x)\exists x\ \neg P(x). Rule 2. The statement ∃x P(x)\exists x\ P(x) is false exactly when no element aa has P(a)P(a) true, so P(a)P(a) is false for every aa, which is ∀x ¬P(x)\forall x\ \neg P(x). For nested quantifiers apply the rule one quantifier at a time, from the outside. □\square

Example P1.1.45. The denial of "every integer n>1n > 1 has a divisor dd with 1<d<n1 < d < n" is "some integer n>1n > 1 has no such divisor". The integer n=5n = 5 shows that the original is false, and the denial is the statement that a prime exists.

Example P1.1.46. Write "∀x ∃y (x+y=0)\forall x\, \exists y\, (x + y = 0) over ℤ\mathbb{Z}" with its denial: "∃x ∀y (x+y≠0)\exists x\, \forall y\, (x + y \ne 0)". The first is true: take y=−xy = -x. So the denial is false. For ℕ\mathbb{N}, the first is false, since x=1x = 1 has no yy with 1+y=01 + y = 0, and the denial is true.

Theorem P1.1.47 (order of quantifiers). For a predicate P(x,y)P(x, y) on D×ED \times E:

  1. ∃y∈E ∀x∈D P(x,y)\exists y \in E\ \forall x \in D\ P(x, y) implies ∀x∈D ∃y∈E P(x,y)\forall x \in D\ \exists y \in E\ P(x, y);
  2. The converse implication is false in general;
  3. Two quantifiers of the same kind may be exchanged: ∀x ∀y P≡∀y ∀x P\forall x\, \forall y\, P \equiv \forall y\, \forall x\, P and ∃x ∃y P≡∃y ∃x P\exists x\, \exists y\, P \equiv \exists y\, \exists x\, P.

Lean: forall_comm and exists_comm for rule 3 (Mathlib); rule 1 is fun ⟨y, h⟩ x => ⟨y, h x⟩, which is a term of the type (Lean core).

Proof. Rule 1. Let b∈Eb \in E be such that P(a,b)P(a, b) holds for all a∈Da \in D. Take any a∈Da \in D. The element bb is a witness for ∃y P(a,y)\exists y\ P(a, y), and bb does not depend on aa. Rule 2. Take D=E={1,2}D = E = \{1, 2\} and P(x,y)P(x, y) equal to "x=yx = y". For each xx the element y=xy = x works, so ∀x ∃y P\forall x\, \exists y\, P is true. No single yy equals both 11 and 22, so ∃y ∀x P\exists y\, \forall x\, P is false. Rule 3. Both sides of each equivalence say that P(a,b)P(a, b) holds for all pairs, or for some pair, and the order of the pair is not part of the claim. □\square

Example P1.1.48. Let D=E={1,…,6}D = E = \{1, \dots, 6\} and P(x,y)P(x, y) be "x+y=7x + y = 7". For each xx the value y=7−xy = 7 - x lies in the domain, so ∀x ∃y P\forall x\, \exists y\, P is true. No single yy gives x+y=7x + y = 7 for every xx, so ∃y ∀x P\exists y\, \forall x\, P is false.

Example P1.1.49. On ℕ\mathbb{N}, "∀x ∃y (y>x)\forall x\, \exists y\, (y > x)" is true (take y=x+1y = x + 1) and "∃y ∀x (y>x)\exists y\, \forall x\, (y > x)" is false, since x=yx = y breaks it. The first statement says that no largest number exists. The second says that a largest number exists. The order of the two quantifiers is the whole difference.

Theorem P1.1.50 (quantifiers with connectives). For predicates PP and QQ on DD:

  1. ∀x (P(x)∧Q(x))≡∀x P(x)∧∀x Q(x)\forall x\ (P(x) \wedge Q(x)) \equiv \forall x\, P(x) \wedge \forall x\, Q(x);
  2. ∃x (P(x)∨Q(x))≡∃x P(x)∨∃x Q(x)\exists x\ (P(x) \vee Q(x)) \equiv \exists x\, P(x) \vee \exists x\, Q(x);
  3. ∀x P(x)∨∀x Q(x)\forall x\, P(x) \vee \forall x\, Q(x) implies ∀x (P(x)∨Q(x))\forall x\ (P(x) \vee Q(x)), and the converse is false in general;
  4. ∃x (P(x)∧Q(x))\exists x\ (P(x) \wedge Q(x)) implies ∃x P(x)∧∃x Q(x)\exists x\, P(x) \wedge \exists x\, Q(x), and the converse is false in general;
  5. if xx is not free in RR, then ∀x (P(x)→R)≡(∃x P(x))→R\forall x\ (P(x) \to R) \equiv (\exists x\, P(x)) \to R.

Lean: forall_and (rule 1), exists_or (rule 2) and exists_imp (rule 5), in Mathlib or Lean core.

Proof. Rule 1. Both sides say that for every aa both P(a)P(a) and Q(a)Q(a) hold. Rule 2. If P(a)∨Q(a)P(a) \vee Q(a) holds for some aa, then P(a)P(a) holds or Q(a)Q(a) holds, so one of the two existential statements is true. Conversely, the witness of a true existential statement on the right is a witness for the left side. Rule 3. If PP holds for all aa, then P(a)∨Q(a)P(a) \vee Q(a) holds for all aa. The same holds for QQ. For the converse, take D=ℤD = \mathbb{Z}, P(x)P(x) equal to "xx is even" and Q(x)Q(x) equal to "xx is odd": every integer is even or odd, but not every integer is even and not every integer is odd. Rule 4. A witness for the conjunction is a witness for each part. For the converse take P(x)P(x) equal to "xx is even", Q(x)Q(x) equal to "xx is odd": both parts of the right side hold, and no integer makes both true. Rule 5. If the left side holds and some aa has P(a)P(a), then RR holds, since P(a)→RP(a) \to R is true. Conversely, if (∃x P(x))→R(\exists x\, P(x)) \to R holds, then for each aa with P(a)P(a) the hypothesis of the implication is true, so RR is true, and so P(a)→RP(a) \to R is true for all aa. □\square

Example P1.1.51. Over the integers, let P(x)P(x) be "x2=9x^2 = 9" and RR be "99 is a perfect square". Both ∀x (x2=9→R)\forall x\, (x^2 = 9 \to R) and (∃x x2=9)→R(\exists x\, x^2 = 9) \to R are true. The rule says that a proof of "if some xx has P(x)P(x) then RR" may begin with "let xx be given with P(x)P(x)". This is how a proof uses an existential hypothesis.

Example P1.1.52. On D={1,…,10}D = \{1, \dots, 10\}, ∀x (x is even∨x is odd)\forall x\, (x \text{ is even} \vee x \text{ is odd}) is true, and each of ∀x (x is even)\forall x\, (x \text{ is even}) and ∀x (x is odd)\forall x\, (x \text{ is odd}) is false. This is the counterexample of rule 3 for a finite domain.

Proof

The proofs of this section come from the meaning of the two quantifiers, and tables do not help for a domain with infinitely many elements. The method is: a statement with ∀\forall is refuted by one element, and a statement with ∃\exists is proved by one element. These two facts decide most of the work in the chapters that follow.

A domain can be empty. Then ∀x∈∅ P(x)\forall x \in \emptyset\ P(x) is true, since no element can break it, and ∃x∈∅ P(x)\exists x \in \emptyset\ P(x) is false, since there is no element to be the witness. The laws of denial still hold in this case: ¬∀x∈∅ P(x)\neg \forall x \in \emptyset\ P(x) is false, and ∃x∈∅ ¬P(x)\exists x \in \emptyset\ \neg P(x) is false. A statement "for all xx in the empty set" is vacuously true. Section 3 of Chapter 2 uses this rule for the empty set and for the empty product.

Examples

Example P1.1.53 (a property of an algorithm). TAOCP Section 1.1 asks that an algorithm stop after a finite number of steps for each admissible input. In quantifiers, with II the set of inputs: ∀x∈I ∃k∈ℕ (the computation on x stops after k steps)\forall x \in I\ \exists k \in \mathbb{N}\ (\text{the computation on } x \text{ stops after } k \text{ steps}). The number kk may depend on xx, as the order of the two quantifiers allows. The denial is ∃x∈I ∀k∈ℕ (the computation on x is not finished after k steps)\exists x \in I\ \forall k \in \mathbb{N}\ (\text{the computation on } x \text{ is not finished after } k \text{ steps}): some input runs forever. The statement ∃k ∀x\exists k\, \forall x is much stronger, since it says that one bound works for all inputs. It is false for Euclid's algorithm on the natural numbers, since the number of steps is not bounded when nn grows.

Example P1.1.54 (the definition of a limit). A sequence a0,a1,…a_0, a_1, \dots of real numbers has the limit LL when ∀ε>0 ∃N∈ℕ ∀n≥N (∣an−L∣<ε)\forall \varepsilon > 0\ \exists N \in \mathbb{N}\ \forall n \ge N\ (|a_n - L| < \varepsilon). The denial is ∃ε>0 ∀N ∃n≥N (∣an−L∣≥ε)\exists \varepsilon > 0\ \forall N\ \exists n \ge N\ (|a_n - L| \ge \varepsilon). For an=(−1)na_n = (-1)^n and L=0L = 0 the value ε=1/2\varepsilon = 1/2 makes the denial true, since ∣an−0∣=1≥1/2|a_n - 0| = 1 \ge 1/2 for all nn. So the sequence (−1)n(-1)^n does not tend to 00.

Example P1.1.55 (counting relations). Let D=E={1,2,3}D = E = \{1, 2, 3\}. A predicate P(x,y)P(x, y) is a choice of T or F for each of the 99 pairs, so there are 29=5122^9 = 512 of them. For ∀x ∃y P(x,y)\forall x\, \exists y\, P(x, y) each of the three rows must have at least one T, and a row has 23−1=72^3 - 1 = 7 such choices, so there are 73=3437^3 = 343 predicates. For ∃y ∀x P(x,y)\exists y\, \forall x\, P(x, y) some column must be all T. The inclusion-exclusion count is 3⋅26−3⋅23+1=192−24+1=1693 \cdot 2^6 - 3 \cdot 2^3 + 1 = 192 - 24 + 1 = 169. Of the 343343 predicates with the weaker statement, 169169 have the stronger one, which is consistent with Rule 1 of the order of quantifiers: 169≤343169 \le 343.

Exercises

  1. [10] Decide the truth value of each statement over ℤ\mathbb{Z}: (a) ∀x ∃y (x+y=0)\forall x\, \exists y\, (x + y = 0); (b) ∃y ∀x (x+y=0)\exists y\, \forall x\, (x + y = 0); (c) ∀x ∃y (xy=1)\forall x\, \exists y\, (xy = 1); (d) ∃x ∀y (xy=y)\exists x\, \forall y\, (xy = y).
  2. [12] Write the denial of "every even integer greater than 22 is the sum of two primes", with a quantifier in front, and say which element would be a counterexample.
  3. [12] Write with quantifiers: (a) "mm divides nn", for integers; (b) "dd is the greatest common divisor of mm and nn", for positive integers.
  4. [15] Prove ∀x (P(x)∧Q(x))≡∀x P(x)∧∀x Q(x)\forall x\ (P(x) \wedge Q(x)) \equiv \forall x\, P(x) \wedge \forall x\, Q(x) in both directions.
  5. [M18] Prove that ∀x (P(x)→R)≡(∃x P(x))→R\forall x\ (P(x) \to R) \equiv (\exists x\, P(x)) \to R when xx is not free in RR, and give an example where the rule fails if xx is free in RR.
  6. [M20] Let the domain be empty. Give the truth values of ∀x P(x)\forall x\, P(x), ∃x P(x)\exists x\, P(x), ∀x (x≠x)\forall x\, (x \ne x) and ∃x (x=x)\exists x\, (x = x), and check that the laws of denial hold.
  7. [M22] Write ∃!x P(x)\exists! x\, P(x) with ∃\exists, ∀\forall and == only. Show that ∃!x P(x)\exists! x\, P(x) implies ∃x P(x)\exists x\, P(x), and that ∀x ∃!y (y=2x)\forall x\, \exists! y\, (y = 2x) is true over ℕ\mathbb{N}.
  8. [M25] Let D=E={1,2,3,4}D = E = \{1, 2, 3, 4\}. Find how many predicates P(x,y)P(x, y) make ∀x ∃y P(x,y)\forall x\, \exists y\, P(x, y) true and how many make ∃y ∀x P(x,y)\exists y\, \forall x\, P(x, y) true.
  9. [M25] Write the statement "the sequence ana_n does not tend to any limit" with quantifiers and no negation sign in front of a quantifier, and show that an=(−1)na_n = (-1)^n has this property.
  10. [M25] The statement "Algorithm 1.1E ends for all positive integers mm and nn" is a statement about a function T(m,n)T(m, n), the number of steps. Write it with quantifiers over mm, nn and the number of steps. Write the denial, and say how it differs from the statement that the number of steps is bounded by one number for all mm and nn.

Lean

A predicate is a function α → Prop, and the two quantifiers are ∀ x, P x and ∃ x, P x. A proof of ∀ x, P x starts with intro x. A proof of ∃ x, P x gives a witness: exact ⟨a, h⟩, or use a. The use of a hypothesis h : ∃ x, P x is obtain ⟨a, ha⟩ := h. The theorems of denial are not_forall and not_exists in Mathlib, and the order of two quantifiers is the lemma forall_comm and exists_comm. For a domain given as a finite set, decide evaluates a quantified statement over Fin n, for example example : ∀ x : Fin 6, ∃ y : Fin 6, x.val + y.val = 5 := by decide.

Notes and history

Frege introduced the quantifiers in 1879 in a notation that is hard to read, and Peirce and Mitchell gave the form with Σ\Sigma and Π\Pi in 1883. Peano wrote the symbol ∃\exists in 1897, and Gentzen wrote ∀\forall in 1935 by analogy with it. The definition of a limit with ε\varepsilon and NN was fixed by Weierstrass in the 1860s. It is the standard example of a statement with three nested quantifiers.

TAOCP Section 1.2.1 uses a statement with a single universal quantifier, "for all n≥1n \ge 1, P(n)P(n)", for every result that it proves by induction. TAOCP Section 1.1 states the finiteness property of an algorithm as the statement of the example in this section. The reader of the later chapters of TAOCP meets the order of quantifiers again in the definition of the O-notation in Section 1.2.11.1, where the constant MM may depend on the function but not on nn.

A proof is an argument that a statement is true. Before a reader can write or check a proof, the reader must know what a statement is and what it means for a statement to be true. This chapter gives the exact rules. They are few, and they are the same in every branch of mathematics.

Section 1 defines a statement and the five connectives: not, and, or, implies and if and only if. It gives their truth tables and treats the implication in detail, since most mistakes in proofs come from a wrong reading of "if ... then". Section 2 gives the laws of logic, the equivalences that let a writer change a statement into a form that is easier to prove, and it shows how to test a law with a truth table. Section 3 adds the quantifiers "for all" and "there exists". Almost every statement of TAOCP has them: "for all n≥1n \ge 1", "there exist integers aa and bb such that". The section teaches how to read a nested quantifier and how to write the negation of a quantified statement.

The chapter prepares TAOCP Section 1.1, where the five properties of an algorithm are statements with quantifiers over the inputs, and Section 1.2.1, where each assertion of a flow chart is a statement that is true or false at a given moment. Every later chapter of the volume uses the laws of Section 2 and the negation rule of Section 3 without a reference.

The chapter has no prerequisite besides P0. The spine texts are Velleman, How to Prove It, Chapters 1 and 2, and Lehman, Leighton and Meyer, Mathematics for Computer Science, Chapter 3. The handbook has its own examples and its own exercises. Each section ends with exercises, and the solutions are in the last chapter of the volume.

The source of this page. Something is wrong? Report it.