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
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 "", is not a statement until a value is given to .
Lean: the type Prop holds the statements of Lean, and True and False are the two constant statements (Lean core).
Example P1.1.2. " is prime" is a statement, and its truth value is T. "" is a statement with truth value F, since . 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 " is even" when has no value. For the sentence " is even" becomes the true statement " is even". Section 3 treats such sentences as predicates.
Definition P1.1.4 (connectives). Let and be statements. The statement (not ) is true when is false. The statement ( and ) is true when both are true. The statement ( or ) is true when at least one is true. The statement ( implies , or if then ) is false exactly when is true and is false. The statement ( if and only if ) is true exactly when and have the same truth value. The table gives all five.
| 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 be "" (true) and be " is even" (false). Then is F, is T, is F and is T. The statement is T, so is T.
Example P1.1.6. The word "or" of daily speech may mean "one or the other, but not both". The connective is the inclusive or: it is true also when both parts are true. So " or " is true. The exclusive or, written , is the statement , and it is true exactly when and have different truth values.
Definition P1.1.7 (converse, contrapositive, negation of an implication). For the implication , the converse is , the contrapositive is , and the inverse is . In , the statement is the hypothesis and is the conclusion.
Example P1.1.8. Take "if is divisible by , then is even". The converse is "if is even, then is divisible by ". The contrapositive is "if is odd, then is not divisible by ". The original and the contrapositive are true for every integer . The converse is false: is even and .
Example P1.1.9. Take "if , then " for real . The contrapositive is "if , then ", which is true. The converse is "if , then ", which is false for .
Theorem P1.1.10 (the implication). For all statements and :
- has the same truth value as ;
- has the same truth value as its contrapositive ;
- has the same truth value as ;
- has the same truth value as ;
- and its converse 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 and has its truth value fixed by the truth values of and , so it is enough to check the four rows of the table. Rule 1: in the rows TT, TF, FT, FF the statement is T, F, T, T. The statement 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 with 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 is F, T, F, F in the four rows, and is F, T, F, F. Rule 4: in the four rows is T, F, F, T, which is the column of . Rule 5: for = F and = T the implication is T and the converse is F.
Example P1.1.11. Rule 3 tells how to deny an implication. The denial of "if is prime, then is odd" is " is prime and is even". The number makes the denial true, so the implication is false.
Example P1.1.12. Rule 1 turns a hypothesis into a case. "If then " says the same as " or ". The second form is true for every real 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. is a tautology. The row with = T gives T, and the row with = F gives T. The form is a contradiction.
Example P1.1.15. The form has three variables and so eight rows. It is a tautology. A row can make the whole form false only if is false, that is = T and = F. Then is T or F. If = T, then is F. If = F, then 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 variables has 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 , 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 then " is a promise about the case where holds. When does not hold, the promise is not broken. The statement "for all , if then " must be true for , where the hypothesis is false, and the table gives that value.
Examples
Example P1.1.16 (a puzzle with a table). Three people , and each say one sentence. says " lies". says " lies". says " and both lie". Let , and be T when the person tells the truth. The sentences give , and . The second equation gives and the first gives . The third becomes , and the right side is F, so = F. Then = T and = F. A check of the three sentences with these values confirms them. tells the truth, and and lie.
Example P1.1.17 (counting rows). Two statements and give four rows. A connective of two variables is a choice of T or F for each row, so there are of them. The five connectives of the definition are five of the sixteen. Exactly of the sixteen are true in exactly three rows: they are , , and . For statements there are connectives, and for the number is .
Example P1.1.18 (a chain of implications). Let be "", be "" and be "" for an integer . The statements and are true for all integers . The tautology above says that is true, that is " implies ". A proof often uses the pattern in this way, in a chain with many links.
Exercises
- [10] Write the truth table of and of .
- [10] Decide the truth value of each statement: (a) if is even then is odd; (b) if is even then is odd; (c) or ; (d) if and only if .
- [12] Write the converse, the contrapositive and the denial of "if is prime and , then is odd". Say which of the three are true for all integers .
- [15] Show by a table that has the same truth value as and that has the same truth value as .
- [M18] Show that every connective of two variables that is true in exactly one row is a conjunction of or with or . Say how this gives each of the sixteen connectives from , and .
- [M20] The Sheffer stroke is . Show that has the same truth value as , that has the same value as , and find a form with alone for .
- [M22] A form with the implication as its only connective and with the constant F is written with and . Show that , and can each be written with and alone.
- [M25] Three people each say one sentence: says "exactly one of and lies", says " lies", and says " lies or tells the truth". Find who tells the truth.
- [M25] Show that is true exactly when is true. Give a plain-language example, and say what the result tells about a proof of .
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 and in the variables are logically equivalent, written , when they have the same truth value in each of the rows of the table. Equivalently, is a tautology.
Lean: for Prop the relation Iff; a law is a theorem A ↔ B (Lean core).
Example P1.1.20. by Rule 1 of the theorem of Section 1. The forms and are not equivalent: they differ in the rows TF and FT.
Example P1.1.21. The forms and are equivalent. Both are F in exactly one row of the eight, the row with = T, = T and = F, and they are T in the other seven.
Theorem P1.1.22 (the laws of logic). For all statement forms , and :
- Double negation: .
- Commutative laws: and .
- Associative laws: and the same for .
- Distributive laws: and .
- De Morgan laws: and .
- Idempotent laws: and .
- Absorption: and .
- Identity and domination: , , and , where is a true statement and a false one.
- Complement: and .
- Exportation: .
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 has the values F, T, T, T in the rows TT, TF, FT, FF. The form 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 is T when is T. When is F, it has the value of . The form is T when is T, since both parts are T. When is F, it equals . 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 and , exchanged.
Absorption. When is T, is T. When is F, then is F, and the form is F. So the form has the value of . The second law is the same. Exportation: both forms are F in exactly the row = T, = T, = F. In this row has T hypothesis and F conclusion, and has T hypothesis and the conclusion , 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.
Example P1.1.23. Deny the statement " and " with the first De Morgan law: the denial is " or ". Deny " is even or " with the second law: the denial is " is odd and ".
Example P1.1.24. Exportation says that a proof of "if and , then " may assume , then assume , and then prove . For : " is divisible by ", : "", : "", the proof assumes and and shows , since is a positive multiple of .
Theorem P1.1.25 (replacement). Let . If is a statement form that contains as a part, and is the form that results when this part is replaced by , then .
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 . In this row, and have the same truth value, and the truth value of depends on the truth values of its parts only through the values of its parts. So and have the same value in this row.
Example P1.1.26. Simplify . The rule gives . The second distributive law, read from right to left with in the role of , gives . The complement law gives , and the identity law gives . 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 is , which is by the second De Morgan law, and so . 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 is true in the rows TF and FT. Its DNF is . The form is true in the rows TT and FF, and its DNF is .
Example P1.1.30. The majority of three statements, true when at least two of , and are true, holds in the rows TTF, TFT, FTT and TTT. Its DNF has four minterms: .
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 , and 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 have the variables . For each row where is T, write the minterm that is T in this row and F in every other row: take the literal when the row has T for , and when it has F. The disjunction of these minterms is T exactly in the rows where is T. So it is equivalent to . For the second claim, a form with no row of value T is a contradiction, and is equivalent to it.
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 is a tautology. The check of the eight rows gives T in every row. A short argument gives the same: if is T, then gives , and is T. If is F, then gives , and 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 variables has at most minterms, one for each row. The form is T when an odd number of the three variables is T. This is the rows TFF, FTF, FFT and TTT, so of rows, and its DNF has four minterms.
Example P1.1.34 (a form that does not simplify as expected). The forms and are not equivalent: in the rows with = F, = F, = F and with = F, = T, = 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 means .
Exercises
- [10] Use the laws to show and .
- [12] Write the denial of each statement with no negation sign left in front of a bracket: (a) and ; (b) is prime or is a square; (c) if then .
- [15] Write the DNF of and of .
- [M16] Show by a table that is a tautology. Give a one-line argument that does not use a table.
- [M18] Show that is a tautology (the rule of the disjunctive syllogism), and that is a tautology (the rule of the modus tollens).
- [M20] Prove the first distributive law and the second De Morgan law from the other laws of the theorem, with no table for them.
- [M22] Show that , and give an example with numbers.
- [M24] Show that , so the connective is associative, and find in how many of the eight rows the form is T.
- [M28] Count the forms in the variables and up to equivalence that are built only from and 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 , 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 and as multiplication and addition of a two-element system, and the distributive law of over 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 be a set. A predicate on is a sentence with a free variable such that is a statement for each . The set is the domain of the variable. A predicate with two variables 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. " is prime" is a predicate on the positive integers. is true and is false. The set of the values that make it true is the truth set, and for it is .
Example P1.1.37. "" is a predicate on pairs of integers. is true, is false, and for a fixed the sentence is a predicate in alone.
Definition P1.1.38 (quantifiers). The statement (for all in , ) is true when is true for every . The statement (there exists in with ) is true when is true for at least one . The symbol is the universal quantifier and is the existential quantifier. The statement (there is exactly one ) is true when is true for exactly one .
Lean: ∀ x : α, P x, ∃ x : α, P x and ∃! x : α, P x (Lean core and Mathlib).
Example P1.1.39. Over the integers, is true, since is the product of two consecutive integers, and it is never negative. The statement is false, for the same reason. Over the real numbers, has , 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 , with the empty sum for , has a universal quantifier over an infinite domain. Its truth cannot be checked by a table. Chapter 4 proves it by induction. The statement is true, since only works. Over it is false, since also works.
Definition P1.1.41 (bound and free variables). In the variable is bound by the quantifier, and is free. A statement has no free variable. The name of a bound variable does not matter: and say the same.
Example P1.1.42. In over the integers, is bound and is free. For it is true and for it is false. So the sentence says " is even". It is a predicate in .
Example P1.1.43. In over 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 on a domain :
- ;
- .
The same laws hold for each quantifier in front of a longer statement, so is .
Lean: not_forall and not_exists (Mathlib); not_forall needs classical logic, which Mathlib uses everywhere.
Proof. Rule 1. The statement is false exactly when is false for at least one . That is the same as saying that is true for at least one , which is . Rule 2. The statement is false exactly when no element has true, so is false for every , which is . For nested quantifiers apply the rule one quantifier at a time, from the outside.
Example P1.1.45. The denial of "every integer has a divisor with " is "some integer has no such divisor". The integer shows that the original is false, and the denial is the statement that a prime exists.
Example P1.1.46. Write " over " with its denial: "". The first is true: take . So the denial is false. For , the first is false, since has no with , and the denial is true.
Theorem P1.1.47 (order of quantifiers). For a predicate on :
- implies ;
- The converse implication is false in general;
- Two quantifiers of the same kind may be exchanged: and .
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 be such that holds for all . Take any . The element is a witness for , and does not depend on . Rule 2. Take and equal to "". For each the element works, so is true. No single equals both and , so is false. Rule 3. Both sides of each equivalence say that holds for all pairs, or for some pair, and the order of the pair is not part of the claim.
Example P1.1.48. Let and be "". For each the value lies in the domain, so is true. No single gives for every , so is false.
Example P1.1.49. On , "" is true (take ) and "" is false, since 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 and on :
- ;
- ;
- implies , and the converse is false in general;
- implies , and the converse is false in general;
- if is not free in , then .
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 both and hold. Rule 2. If holds for some , then holds or 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 holds for all , then holds for all . The same holds for . For the converse, take , equal to " is even" and equal to " 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 equal to " is even", equal to " is odd": both parts of the right side hold, and no integer makes both true. Rule 5. If the left side holds and some has , then holds, since is true. Conversely, if holds, then for each with the hypothesis of the implication is true, so is true, and so is true for all .
Example P1.1.51. Over the integers, let be "" and be " is a perfect square". Both and are true. The rule says that a proof of "if some has then " may begin with "let be given with ". This is how a proof uses an existential hypothesis.
Example P1.1.52. On , is true, and each of and 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 is refuted by one element, and a statement with is proved by one element. These two facts decide most of the work in the chapters that follow.
A domain can be empty. Then is true, since no element can break it, and is false, since there is no element to be the witness. The laws of denial still hold in this case: is false, and is false. A statement "for all 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 the set of inputs: . The number may depend on , as the order of the two quantifiers allows. The denial is : some input runs forever. The statement 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 grows.
Example P1.1.54 (the definition of a limit). A sequence of real numbers has the limit when . The denial is . For and the value makes the denial true, since for all . So the sequence does not tend to .
Example P1.1.55 (counting relations). Let . A predicate is a choice of T or F for each of the pairs, so there are of them. For each of the three rows must have at least one T, and a row has such choices, so there are predicates. For some column must be all T. The inclusion-exclusion count is . Of the predicates with the weaker statement, have the stronger one, which is consistent with Rule 1 of the order of quantifiers: .
Exercises
- [10] Decide the truth value of each statement over : (a) ; (b) ; (c) ; (d) .
- [12] Write the denial of "every even integer greater than is the sum of two primes", with a quantifier in front, and say which element would be a counterexample.
- [12] Write with quantifiers: (a) " divides ", for integers; (b) " is the greatest common divisor of and ", for positive integers.
- [15] Prove in both directions.
- [M18] Prove that when is not free in , and give an example where the rule fails if is free in .
- [M20] Let the domain be empty. Give the truth values of , , and , and check that the laws of denial hold.
- [M22] Write with , and only. Show that implies , and that is true over .
- [M25] Let . Find how many predicates make true and how many make true.
- [M25] Write the statement "the sequence does not tend to any limit" with quantifiers and no negation sign in front of a quantifier, and show that has this property.
- [M25] The statement "Algorithm 1.1E ends for all positive integers and " is a statement about a function , the number of steps. Write it with quantifiers over , 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 and .
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 and in 1883. Peano wrote the symbol in 1897, and Gentzen wrote in 1935 by analogy with it. The definition of a limit with and 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 , ", 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 may depend on the function but not on .
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 ", "there exist integers and 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.