RN L
RN L
RN L
Computation
Stefan Hugtenburg and Neil Yorke-Smith
Delftse Foundations of Computation
iv
Version TUD-2.0
ISBN 978-94-6366-083-9 | DOI 10.5074/t.isbn.9789463660839
v
To the students of CSE1300
vi
Contents
2 Logic 5
2.1 Propositional Logic . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.1 Propositions . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.2 Logical operators . . . . . . . . . . . . . . . . . . . . . 8
2.1.3 Precedence rules . . . . . . . . . . . . . . . . . . . . . 9
2.1.4 Logical equivalence . . . . . . . . . . . . . . . . . . . . 10
2.1.5 More logical operators . . . . . . . . . . . . . . . . . . 11
2.1.6 Implications in English . . . . . . . . . . . . . . . . . . 12
2.1.7 More forms of implication . . . . . . . . . . . . . . . . 14
2.1.8 Exclusive or . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1.9 Universal operators . . . . . . . . . . . . . . . . . . . . 15
2.1.10 Classifying propositions . . . . . . . . . . . . . . . . . 16
2.2 Boolean Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.2.1 Basics of Boolean Algebra . . . . . . . . . . . . . . . . 20
2.2.2 Substitution laws . . . . . . . . . . . . . . . . . . . . . 21
2.2.3 Simplifications . . . . . . . . . . . . . . . . . . . . . . 23
2.2.4 More rules of Boolean algebra . . . . . . . . . . . . . . 24
2.3 Application: Logic Circuits . . . . . . . . . . . . . . . . . . . 28
2.3.1 Logic gates * . . . . . . . . . . . . . . . . . . . . . . . . 29
2.3.2 Combining gates to create circuits * . . . . . . . . . . 30
2.3.3 From circuits to propositions * . . . . . . . . . . . . . 31
2.3.4 Disjunctive Normal Form . . . . . . . . . . . . . . . . 32
2.3.5 Binary addition * . . . . . . . . . . . . . . . . . . . . . 37
2.4 Predicate Logic . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.4.1 Predicates . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.4.2 Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.4.3 Operators . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.4.4 Tarski’s world and formal structures . . . . . . . . . . 47
2.4.5 Logical equivalence . . . . . . . . . . . . . . . . . . . . 48
vii
2.5 Deduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
2.5.1 Arguments . . . . . . . . . . . . . . . . . . . . . . . . 53
2.5.2 Valid arguments and proofs . . . . . . . . . . . . . . . 55
2.5.3 Proofs in predicate logic . . . . . . . . . . . . . . . . . 58
3 Proof 61
3.1 A Little Historical Background . . . . . . . . . . . . . . . . . 62
3.2 Mathematical Proof . . . . . . . . . . . . . . . . . . . . . . . . 63
3.2.1 How to write a proof . . . . . . . . . . . . . . . . . . . 66
3.2.2 Some terminology . . . . . . . . . . . . . . . . . . . . 69
3.2.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . 70
3.3 Proof by Contradiction . . . . . . . . . . . . . . . . . . . . . . 74
3.4 Mathematical Induction . . . . . . . . . . . . . . . . . . . . . 77
3.4.1 How to write a proof by induction . . . . . . . . . . . 78
3.4.2 Examples . . . . . . . . . . . . . . . . . . . . . . . . . 79
3.4.3 More examples . . . . . . . . . . . . . . . . . . . . . . 81
3.5 Strong Mathematical Induction . . . . . . . . . . . . . . . . . 83
3.6 Application: Recursion and Induction . . . . . . . . . . . . . 86
3.6.1 Recursive factorials . . . . . . . . . . . . . . . . . . . . 86
3.6.2 Towers of Hanoi * . . . . . . . . . . . . . . . . . . . . . 88
3.7 Recursive Definitions . . . . . . . . . . . . . . . . . . . . . . . 91
3.8 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
3.8.1 Nomenclature of trees . . . . . . . . . . . . . . . . . . 94
3.8.2 An application of trees . . . . . . . . . . . . . . . . . . 95
3.8.3 Binary trees in Java * . . . . . . . . . . . . . . . . . . . 96
3.9 Invariants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
viii
4.3.1 Graph nomenclature . . . . . . . . . . . . . . . . . . . 130
4.3.2 An application of graphs: Task ordering . . . . . . . . 130
4.4 Application: Programming with Sets * . . . . . . . . . . . . . 132
4.4.1 Representing sets . . . . . . . . . . . . . . . . . . . . . 132
4.4.2 Computing with sets . . . . . . . . . . . . . . . . . . . 134
4.5 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
4.5.1 Formalising the notion of functions . . . . . . . . . . 139
4.5.2 Operations on functions . . . . . . . . . . . . . . . . . 140
4.5.3 Properties of functions . . . . . . . . . . . . . . . . . . 142
4.5.4 Functions on trees . . . . . . . . . . . . . . . . . . . . 144
4.5.5 Functions on graphs . . . . . . . . . . . . . . . . . . . 145
4.5.6 First-class objects . . . . . . . . . . . . . . . . . . . . . 146
4.6 Application: Programming with Functions * . . . . . . . . . 148
4.6.1 Functions as first-class objects . . . . . . . . . . . . . . 150
4.7 Counting Past Infinity . . . . . . . . . . . . . . . . . . . . . . 153
4.7.1 Cardinality . . . . . . . . . . . . . . . . . . . . . . . . 154
4.7.2 Counting to infinity . . . . . . . . . . . . . . . . . . . 157
4.7.3 Uncountable sets * . . . . . . . . . . . . . . . . . . . . 159
4.7.4 A final note on infinities * . . . . . . . . . . . . . . . . 162
4.8 Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
4.8.1 Properties of relations . . . . . . . . . . . . . . . . . . 165
4.8.2 Equivalence relations . . . . . . . . . . . . . . . . . . . 167
4.9 Application: Relational Databases * . . . . . . . . . . . . . . . 172
Index 213
ix
Chapter 1
with the most general laws of truth. Why study this kind of reasoning in
computer science?
Logic is important because digital computers work with precision, and
because designing algorithms requires precision, and because comparing
algorithms requires precision.
Even when when a computer is, seemingly, computing with vague or
imprecise quantities, the underlying computation is precise.2 For example,
when a deep neural network is being trained to recognise cats, the algorithm
being used to train the network is specified precisely. More than this, the
criteria we use to assess whether the network has learned well enough are
also specified precisely. And any theoretical properties about the algorithm
have been proven precisely.
Reasoning, logic, and related mathematical concepts such as sets, are
foundational for computer science. One third of your first year TUDelft
CSE curriculum is mathematics: Reasoning & Logic, Calculus, Linear Algebra
and Probability Theory & Statistics.
As a computer scientist, you have to be capable of solving complex prob-
lems. One important aspect is to be able to come to the right conclusions.
On the basis of theorems and partial observations you can acquire more
knowledge and evidence to help prove that a specific conclusion is math-
1 In Ancient Greek, λογική, according to Wikipedia: “possessed of reason, intellectual, dia-
lectical, argumentative”.
2 You can take a course on quantum computing to learn whether they are an exception.
1
2 Chapter 1. Introduction and Learning Objectives
ematically and logically correct. You learn how to do this with the course
1 Reasoning & Logic.
The foundational mathematical skills you learn in Reasoning & Logic are
used in all the other mathematics courses you will take, and in Computer
Organisation, Algorithms & Data Structures, Information & Data Management,
Machine Learning, and many other courses. In fact, logic is studied and used
not only in mathematics and computer science, but also in philosophy (since
Ancient Greece) and today in fields such as linguistics and psychology.
This book is designed to help you achieve the learning goals of Reasoning
& Logic:
1. Translate a logically-precise claim to and from natural language.
2. Describe the operation of logical connectors and quantifiers.
3. Describe the notion of logical validity.
4. Explain and apply basic set and graph operations.
5. Define and perform computations with functions, relations and equi-
valence classes.
6. Construct and interpret recursive definitions, including recursive data
structures like trees.
7. Construct an appropriate function or relation given a description (in
natural language or formal notation).
8. Construct a direct or indirect proof (by contradiction, division into
cases, generalisation, or [structural] induction) or logical equivalence—
or counterexample for (in)valid arguments—in propositional logic,
predicate logic and set theory.
9. Identify what type of proof is appropriate for a given claim.
10. Solve simple Boolean Satisfiability (SAT) instances.
11. Develop specifications for verification tools like SAT or SMT solvers.
12. Interpret the response of verification tools like SAT or SMT solvers.
Á
We do not cover every topic at the same level of detail. Some
topics have extra podcast videos to accompany the book. Other
topics, such as SAT and SMT solvers, we do not cover at all. Fur-
ther, the lectures will not cover everything in the book. Some
topics in the lectures you will prepare for using other materials:
these will be announced.
3
®
Starred sections in the contents of this book are not included in
the syllabus for Reasoning & Logic. 1
®
We include solutions to some of the exercises, starting on
page 183. Exercises that have a solution are marked with a dag-
ger (†) symbol. You can contribute solutions to the other exer-
cises!
The theme of the book is about coming to the right conclusion: proving
the logical validity of arguments. What is a valid argument? When is an ar-
gument logically valid and when is it not? How can we determine whether
an argument is logically valid? How can we derive a logically valid conclu-
sion from the premises? Or how can we prove that a conclusion is not a
logical consequence of the premises? And how can we use these abilities in
computer science?
We will begin by talking further about logic.
Chapter 2
Logic
5
6 Chapter 2. Logic
®
Later in Reasoning & Logic, you will see some automated com-
putational techniques that can help us checking the deductions.
We don’t cover these in this edition of this book. In fact, there are
automated proof assistants that can even help us finding interest-
ing conclusions. One of the more famous is called Coq, a name
perhaps inspired by Socrates’s chicken.
thing about its subject. The subject of “Delft is a city” is Delft. The sentence
says something about Delft. The something that the sentence says about its
subject is the predicate. In the example, the predicate is the phrase ‘is a city’.
Once we start working with predicates, we can create propositions using
quantifiers like ‘all’, ‘some’ and ‘no’. For example, working with the predic-
ate ‘has a university’ we can move from simple propositions like “Delft has 2
a university” to “All cities have a university” or to “No city has a university”
or to the rather more realistic “Some cities have a university”.
Logical deduction usually deals with quantified statements, as shown
by the basic example of human mortality with which we began this chapter.
Logical deduction will be a major topic of this chapter; and under the name
of proof , it will be the topic of the next chapter and a major tool for the rest
of this book and indeed your computer science degree programme.
2.1.1 Propositions
ģ
Propositional variables are a little bit like variables in a program-
ming language such as Java. A basic Java variable such as int
x can take any integer value. There is ‘a little bit’ of similarity
between the two notions of variables—don’t take the analogy too
far at this point in your learning!
2
2.1.2 Logical operators
What we do with propositions is combine them with logical operators, also
referred to as logical connectives. A logical operator can be applied to one
or more propositions to produce a new proposition. The truth value of the
new proposition is completely determined by the operator and by the truth
values of the propositions to which it is applied.1 In English, logical oper-
ators are represented by words such as ‘and’, ‘or’ and ‘not’. For example,
the proposition “I wanted to leave and I left” is formed from two simpler
propositions joined by the word ‘and’. Adding the word ‘not’ to the pro-
position “I left” gives “I did not leave” (after a bit of necessary grammatical
adjustment).
But English is a little too rich for mathematical logic. When you read the
sentence “I wanted to leave and I left”, you probably see a connotation of
causality: I left because I wanted to leave. This implication does not follow
from the logical combination of the truth values of the two propositions “I
wanted to leave” and “I left”. Or consider the proposition “I wanted to leave
but I did not leave”. Here, the word ‘but’ has the same logical meaning as
the word ‘and’, but the connotation is very different. So, in mathematical
logic, we use symbols to represent logical operators. These symbols do not
carry any connotation beyond their defined logical meaning. The logical
operators corresponding to the English words ‘and’, ‘or’ and ‘not’ are ∧, ∨
and ¬.2
Definition 2.1. Let p and q be propositions. Then p ∨ q, p ∧ q, and ¬ p are
propositions, whose truth values are given by the rules:
• p ∧ q is true when both p is true and q is true, and in no other case
• p ∨ q is true when either p is true, or q is true, or both p and q are true,
and in no other case
1 It is not always true that the truth value of a sentence can be determined from the truth
values of its component parts. For example, if p is a proposition, then ‘Johan Cruyff believes p’
is also a proposition, so ‘Cruyff believes’ is some kind of operator. However, it does not count
as a logical operator because just from knowing whether or not p is true, we get no information
at all about whether ‘Johan Cruyff believes p’ is true.
2 Other textbooks might use different notations to represent a negation. For instance a bar
over the variable (x̄) or a ∼ symbol (∼ x). In Boolean algebra (and thus in your Computer
Organisation course) you will also often find the + symbol to represent an ‘or’ and a · (dot)
symbol to represent an ‘and’.
2.1. Propositional Logic 9
p q r p∧q q∧r ( p ∧ q) ∧ r p ∧ (q ∧ r )
0 0 0 0 0 0 0
0 0 1 0 0 0 0
0 1 0 0 0 0 0
2 0 1 1 0 1 0 0
1 0 0 0 0 0 0
1 0 1 0 0 0 0
1 1 0 1 0 0 0
1 1 1 1 1 1 1
Figure 2.1: A truth table that demonstrates the logical equivalence of ( p ∧
q) ∧ r and p ∧ (q ∧ r ). The fact that the last two columns of this table are
identical shows that these two expressions have the same value for all eight
possible combinations of values of p, q, and r.
3 In general, if there are n variables, then there are 2n different ways to assign truth values
to the variables, i.e., 2n situations. This might become clear to you if you try to come up with
a scheme for systematically listing all possible sets of values. As this should not satisfy you,
you’ll find a rigorous proof of the fact later in this chapter.
2.1. Propositional Logic 11
b
You can write the rows in a truth table in any order you like. We
suggest you write them in a sorted order, as in Table 2.1. This
helps you to be systematic in writing out the table. It also helps
us to provide feedback on your answers!
ģ
When writing a piece of code you will often have your code make
decisions. For instance in a bit of Java code—such as in your
Object-Oriented Programming course—you might encounter an if-
statement to check if the user has inputted the right type of data.
Since the input you expect can be rather difficult, the if-statement
is a complex combination of many simple checked chained to-
gether by &&’s and ||’s. After taking a look at the code, you be-
lieve it can be simplified to a much smaller expression. Using a
truth table you can prove that your simplified version is equival-
ent to the original.
0 0 1 1 0
0 1 1 0 1
1 0 0 0 1
1 1 1 1 0
®
When these operators are used in expressions, in the absence of
parentheses to indicate order of evaluation, we use the follow-
ing precedence rules: The exclusive or operator, ⊕, has the same
precedence as ∨. The conditional operator, →, has lower pre-
cedence than ∧, ∨, ¬, and ⊕, and is therefore evaluated after
them. Finally, the biconditional operator, ↔, has the lowest pre-
cedence and is therefore evaluated last. For example, the expres-
sion p → q ∧ r ↔ ¬ p ⊕ s is evaluated as if it were written
( p → (q ∧ r )) ↔ ((¬ p) ⊕ s). But again you should always in-
clude the parentheses!
In order to work effectively with the logical operators, you need to know
more about their meaning and how they relate to ordinary English expres-
sions. To that end we first consider the conditional operator in more detail
in the next section.
the child’s perfectly logical plea “But you never said what would happen if
I wasn’t good!”)
2.1.8 Exclusive or
Finally, we turn to the exclusive or operator. The English word ‘or’ is ac-
2
tually somewhat ambiguous. The two operators ⊕ and ∨ express the two
possible meanings of this word. The proposition p ∨ q can be expressed
unambiguously as “p or q, or both”, while p ⊕ q stands for “p or q, but not
both”. If a menu says that you can choose soup or salad, it doesn’t mean
that you can have both. In this case, ‘or’ is an exclusive or. On the other
hand, in “You are at risk of heart disease if you smoke or drink”, the or is
inclusive since you certainly don’t get off the hook if you both smoke and
drink. In theoretical computer science and mathematics, the word ‘or’ is
always taken in the inclusive sense of p ∨ q.
Consider for instance the set {¬, ∨}. As shown above the ∧, → and ↔-
operators can be expressed using only these operators. In fact all possible
operations can be expressed using only {¬, ∨}. To prove this you will show
in one of the exercises that all possible formulas in propositional logic can
be expressed using {¬, ∨, ∧, →, ↔}. So by showing that we do not need ∧,
→, and ↔ we can prove that {¬, ∨} is also functionally complete.
16 Chapter 2. Logic
Certain types of proposition will play a special role in our further work with
logic. In particular, we define tautologies, contradictions, and contingencies
as follows:
2
Definition 2.4. A compound proposition is said to be a tautology if and
only if it is true for all possible combinations of truth values of the proposi-
tional variables which it contains. A compound proposition is said to be a
contradiction if and only if it is false for all possible combinations of truth
values of the propositional variables which it contains. A compound pro-
position is said to be a contingency if and only if it is neither a tautology nor
a contradiction.
p q p∨q ¬q ( p ∨ q) ∧ ¬q (( p ∨ q) ∧ ¬q) → p
0 0 0 1 0 1
0 1 1 0 0 1
1 0 1 1 1 1
1 1 1 0 0 1
The fact that all entries in the last column are true tells us that this ex-
pression is a tautology. Note that for any compound proposition P, P is a
tautology if and only if ¬ P is a contradiction. (Here and moving forward,
we use uppercase letters to represent compound propositions. P stands for
any formula made up of simple propositions, propositional variables, and
logical operators.)
Logical equivalence can be defined in terms of tautology:
Exercises
b
Recall that solutions to some of the exercises start on page 183. Exercises
that have a solution are marked with a dagger (†) symbol. We suggest
you attempt the exercise first before looking at the solution!
†1. Give the three truth tables that define the logical operators ∧, ∨, and ¬.
†2. Some of the following compound propositions are tautologies, some are contra-
dictions, and some are neither (i.e., so are contingencies). In each case, use a
truth table to decide to which of these categories the proposition belongs:
a) ( p ∧ ( p → q)) → q b) (( p → q) ∧ (q → r )) → ( p → r )
c) p ∧ ¬ p d) ( p ∨ q) → ( p ∧ q)
e) p ∨ ¬ p f) ( p ∧ q) → ( p ∨ q)
†3. Use truth tables to show that each of the following propositions is logically equi-
valent to p ↔ q.
a) ( p → q) ∧ (q → p) b) ¬ p ↔ ¬q
c) ( p → q) ∧ (¬ p → ¬q) d) ¬( p ⊕ q)
†4. Is → an associative operation? This is, is ( p → q) → r logically equivalent to
p → ( q → r )?
†5. Let p represent the proposition “You leave” and let q represent the proposition
“I leave”. Express the following sentences as compound propositions using p
and q, and show that they are logically equivalent:
a) Either you leave or I do. (Or both!)
b) If you don’t leave, I will.
†6. Suppose that m represents the proposition “The Earth moves”, c represents “The
Earth is the centre of the universe”, and g represents “Galileo was falsely ac-
cused”. Translate each of the following compound propositions into English:
a) ¬ g ∧ c b) m → ¬c
c) m ↔ ¬c d) (m → g) ∧ (c → ¬ g)
†7. Give the converse and the contrapositive of each of the following English sen-
tences:
a) If you are good, Sinterklaas brings you toys.
b) If the package weighs more than one kilo, then you need extra postage.
18 Chapter 2. Logic
†8. In an ordinary deck of fifty-two playing cards, for how many cards is it true
†10. For our proof that {¬, ∨} is functionally complete, we need to show that all for-
mulas in propositional logic can be expressed in an equivalent form using only
{¬, ∧, ∨, →, ↔}.
a) How many unique truth tables exist for formulas containing two atoms?
b) Create a function for each of the possible truth tables that uses only the 5
operators listed above.
So far we have discussed how to write and interpret propositions. This sec-
tion deals with manipulating them. For this, we need algebra. Ordinary
algebra, of the sort taught in high school, is about manipulating numbers,
variables that represent numbers, and operators such as + and × that apply
to numbers. Now, we need an algebra that applies to logical values, propos-
itional variables, and logical operators. The first person to think of logic in
terms of algebra was the mathematician, George Boole, who introduced the
idea in a book that he published in 1854. The algebra of logic is now called
Boolean algebra in his honour.
2.2. Boolean Algebra 19
5 In ordinary algebra, it is easy to be confused by the equals sign, because it has two very
different roles. In an identity such as the distributive law, it means ‘is always equal to’. On
the other hand, an equation such as x2 + 3x = 4 is a statement that might or might not be
true, depending on the value of x. Boolean algebra has two operators, ≡ and ↔, that play
roles similar to the two roles of the equals sign. ≡ is used for identity, whereas ↔ is used in
equations that may or may not be true.
20 Chapter 2. Logic
“has the same value as, no matter what logical values p, q, and r have”.
tautology that uses only the operators ∧, ∨, and ¬, another tautology can be obtained from it by
interchanging ∧ with ∨ and T with F. We won’t attempt to prove this here, but we encourage
you to try it!
2.2. Boolean Algebra 21
atives make a positive. Although the way this rule applies to English is ques-
tionable, if you look at how it is used—no matter what the grammarian says,
“I can’t get no satisfaction” doesn’t really mean “I can get satisfaction”—the
validity of the rule in logic can be verified just by computing the two pos-
sible cases: when p is true and when p is false. When p is true, then by the
definition of the ¬ operator, ¬ p is false. But then, again by the definition of 2
¬, the value of ¬(¬ p) is true, which is the same as the value of p. Similarly,
in the case where p is false, ¬(¬ p) is also false. Organized into a truth table,
this argument takes the rather simple form
p ¬p ¬(¬ p)
0 1 0
1 0 1
The fact that the first and last columns are identical shows the logical
equivalence of p and ¬(¬ p). The point here is not just that ¬(¬ p) ≡ p, but
also that this logical equivalence is valid because it can be verified compu-
tationally based just on the relevant definitions. Its validity does not follow
from the fact that “it’s obvious” or “it’s a well-known rule of grammar”.
®
Students often ask “Why do I have to prove something when
it’s obvious?” The point is that logic—and mathematics more
generally—is its own little world with its own set of rules. Al-
though this world is related somehow to the real world, when
you say that something is obvious (in the real world), you aren’t
playing by the rules of the world of logic. The real magic of math-
ematics is that by playing by its rules, you can come up with
things that are decidedly not obvious, but that still say some-
thing about the real world or the computational world—often,
something interesting and important.
Each of the rules in Figure 2.2 can be verified in the same way, by making
a truth table to check all the possible cases. In one of the pencasts of this
course we further discuss how to check the equivalence of two propositions
using truth tables.
¬(¬ p) ≡ p. It is also true that ¬(¬q) ≡ q, that ¬(¬( p ∧ q)) ≡ ( p ∧ q), that
¬(¬( p → (q ∧ ¬ p))) ≡ ( p → (q ∧ ¬ p)), and an infinite number of other
statements of the same form. Here, a ‘statement of the same form’ is one
that can be obtained by substituting something for p in both places where
it occurs in ¬(¬ p) ≡ p. How can I be sure that all these infinitely many
2 statements are valid when all that I’ve checked is one little two-line truth
table? The answer is that any given proposition, Q, no matter how complic-
ated, has a particular truth value, either true or false. So, the question of
the validity of ¬(¬ Q) ≡ Q always reduces to one of the two cases I already
checked in the truth table. (Note that for this argument to be valid, the same
Q must be substituted for p in every position where it occurs.) While this
argument may be ‘obvious’, it is not exactly a proof, but for now we will just
accept the validity of the following theorem:
Theorem 2.1 (First Substitution Law). Suppose that Q is any proposition, and
that p is a propositional variable. Consider any tautology. If ( Q) is substituted for
p in all places where p occurs in the tautology, then the result is also a tautology.
Since logical equivalence is defined in terms of tautology, it is also true
that when ( Q) is substituted for p in a logical equivalence, the result is again
a logical equivalence.7
The First Substitution Law lets you do algebra! For example, you can
substitute p → q for p in the law of double negation, ¬(¬ p) ≡ p. This allows
you to ‘simplify’ the expression ¬(¬(r → q)) to r → q with confidence
that the resulting expression has the same logical value as the expression
you started with. (That’s what it means for ¬(¬(r → q)) and r → q to
be logically equivalent.) You can play similar tricks with all the laws in
Figure 2.2. Even more important is the Second Substitution Law, which says
that you can substitute an expression for a logically equivalent expression,
wherever it occurs. Once again, we will accept this as a theorem without
trying to prove it here. It is surprisingly hard to put this law into words:
Theorem 2.2 (Second Substitution Law). Suppose that P and Q are any pro-
positions such that P ≡ Q. Suppose that R is any compound proposition in which
( P) occurs as a sub-proposition. Let R′ be the proposition that is obtained by sub-
stituting ( Q) for that occurrence of ( P) in R. Then R ≡ R′ .
Note that in this case, the theorem does not require ( Q) to be substituted
for every occurrence of ( P) in R. You are free to substitute for one, two,
7 I’ve added parentheses around Q here for technical reasons. Sometimes, the parentheses
are necessary to make sure that Q is evaluated as a whole, so that its final value is used in place
of p. As an example of what can go wrong, consider q ∧ r. If this is substituted literally for p
in ¬(¬ p), without parentheses, the result is ¬(¬q ∧ r ). But this expression means ¬((¬q) ∧ r ),
which is not equivalent to q ∧ r. Did we say to always write parentheses if you’re in doubt? See
page 9.
2.2. Boolean Algebra 23
2.2.3 Simplifications
The final piece of algebra in Boolean algebra is the observation that we can
chain logical equivalences together. That is, from P ≡ Q and Q ≡ R, it
follows that P ≡ R. This is really just a consequence of the Second Substi-
tution Law. The equivalence Q ≡ R allows us to substitute R for Q in the
statement P ≡ Q, giving P ≡ R. (Remember that, by Definition 2.5, logical
equivalence is defined in terms of a proposition.) This means that we can
show that two compound propositions are logically equivalent by finding
a chain of logical equivalences that lead from one to the other.
Here is an example of such a chain of logical equivalences:
Each step in the chain has its own justification. In several cases, a substitu-
tion law is used without stating as much. In the first line, for example, the
definition of p → q is that p → q ≡ ¬ p ∨ q. The Second Substitution Law
allows us to substitute (¬ p ∨ q) for ( p → q). In the last line, we implicitly
applied the First Substitution Law to the Identity Law, F ∨ p ≡ p, to obtain
F ∨ ( p ∧ q ) ≡ ( p ∧ q ).
The chain of equivalences in the above example allows us to conclude
that p ∧ ( p → q) is logically equivalent to p ∧ q. This means that if you
were to make a truth table for these two expressions, the truth values in
the column for p ∧ ( p → q) would be identical to those in the column for
p ∧ q. We know this without actually making the table. Don’t believe it? Go
ahead and make the truth table. In this case, the table is only be four lines
long and easy enough to make. But Boolean algebra can be applied in cases
24 Chapter 2. Logic
where the number of propositional variables is too large for a truth table to
be practical.
Ď
Let’s do another example. Recall that a compound proposition
2 is a tautology if it is true for all possible combinations of truth
values of the propositional variables that it contains. But another
way of saying the same thing is that P is a tautology if P ≡ T. So,
we can prove that a compound proposition, P, is a tautology by
finding a chain of logical equivalences leading from P to T. For
example:
(( p ∨ q) ∧ ¬ p) → q
≡ (¬(( p ∨ q) ∧ ¬ p)) ∨ q definition of →
≡ (¬( p ∨ q) ∨ ¬(¬ p)) ∨ q DeMorgan’s Law, Theorem 2.2
≡ (¬( p ∨ q) ∨ p) ∨ q Double Negation, Theorem 2.2
≡ (¬( p ∨ q)) ∨ ( p ∨ q) Associative Law for ∨
≡T Law of Excluded Middle
Now, it takes some practice to look at an expression and see which rules
can be applied to it; to see (¬( p ∨ q)) ∨ ( p ∨ q) as an application of the law
of the excluded middle for example, you need to mentally substitute ( p ∨ q)
for p in the law as it is stated in Figure 2.2. Often, there are several rules that
apply, and there are no definite guidelines about which one you should try.
This is what makes algebra something of an art.
(( p ∨ q) ∨ r ) ∨ s
≡ ( p ∨ q ) ∨ (r ∨ s ) by the Associative Law for three terms
≡ p ∨ (q ∨ (r ∨ s)) by the Associative Law for three terms
b
One other thing that you should keep in mind is that rules can
be applied in either direction. The Distributive Law, for example,
allows you to distribute the p in p ∨ (q ∧ ¬ p) to get ( p ∨ q) ∧ ( p ∨
¬ p). But it can also be used in reverse to ‘factor out’ a term, as
when you start with (q ∨ ( p → q)) ∧ (q ∨ (q → p)) and factor
out the q to get q ∨ (( p → q) ∧ (q → p)).
So far in this section, we have been working with the laws of Boolean
algebra without saying much about what they mean or why they are reas-
onable. Of course, you can apply the laws in calculations without under-
standing them. But if you want to figure out which calculations to do, you
need some understanding. Most of the laws are clear enough with a little
thought. For example, if we already know that q is false, then p ∨ q will be
true when p is true and false when p is false. That is, p ∨ F has the same
logical value as p. But that’s just what the Identity Law for ∨ says. A few of
the laws need more discussion.
The Law of the Excluded Middle, p ∨ ¬ p ≡ T, says that given any pro-
position p, at least one of p or ¬ p must be true. Since ¬ p is true exactly
when p is false, this is the same as saying that p must be either true or false.
There is no middle ground. The Law of Contradiction, p ∧ ¬ p ≡ F, says
that it is not possible for both p and ¬ p to be true. Both of these rules are
obvious.
26 Chapter 2. Logic
i
There are some who set out to question the law of there being no
middle ground. Already in the 1920’s people like Tarski (who
we will meet later) talked about other forms of logic where
another value representing ‘unknown’ or ‘not proven’ also
exists. You can also see this in some programming languages
2 where they are referred to as ‘tri-state booleans’.
The Distributive Laws cannot be called obvious, but a few examples can
show that they are reasonable. Consider the statement, “This card is the
ace of spades or clubs.” Clearly, this is equivalent to “This card is the ace
of spaces or this card is the ace of clubs.” But this is just an example of the
first distributive law! For, let a represent the proposition “This card is an
ace”, let s represent “This card is a spade” and let c represent “This card
is a club”. Then “This card is the ace of spades or clubs” can be translated
into logic as a ∧ (s ∨ c), while “This card is the ace of spades or this card is
the ace of clubs” becomes ( a ∧ s) ∨ ( a ∧ c). And the distributive law assures
us that a ∧ (s ∨ c) ≡ ( a ∧ s) ∨ ( a ∧ c). The second distributive law tells us,
for example, that “This card is either a joker or is the ten of diamonds” is
logically equivalent to “This card is either a joker or a ten, and it is either a
joker or a diamond”. That is, j ∨ (t ∧ d) ≡ ( j ∨ t) ∧ ( j ∨ d). The distributive
laws are powerful tools and you should keep them in mind whenever you
are faced with a mixture of ∧ and ∨ operators.
DeMorgan’s Laws must also be less than obvious, since people often get
them wrong. Fortunately you get to practice them both in Reasoning & Logic,
as well as in Computer Organisation, so you will soon get them right. More
importantly perhaps they do also make sense. When considering ¬( p ∧ q),
you should ask yourself, how can ‘p and q’ fail to be true. It will fail to be
2.2. Boolean Algebra 27
¬( p → q) ≡ ¬((¬ p) ∨ q)
≡ (¬(¬ p)) ∧ (¬q)
≡ p ∧ ¬q
That is, p → q is false exactly when both p is true and q is false. For example,
the negation of “If you have an ace, you win” is “You have an ace, and you
don’t win”. Think of it this way: if you had an ace and you didn’t win, then
the statement “If you have an ace, you win” was not true.
Exercises
1. Construct truth tables to demonstrate the validity of each of the distributive
laws.
2. Construct the following truth tables:
a) Construct truth tables to demonstrate that ¬( p ∧ q) is not logically equi-
valent to (¬ p) ∧ (¬q).
b) Construct truth tables to demonstrate that ¬( p ∨ q) is not logically equi-
valent to (¬ p) ∨ (¬q).
c) Construct truth tables to demonstrate the validity of both DeMorgan’s
Laws.
3. Construct truth tables to demonstrate that ¬( p → q) is not logically equivalent
to any of the following.
a) (¬ p) → (¬q)
b) (¬ p) → q
c) p → (¬q)
Refer back to this section for a formula that is logically equivalent to ¬( p → q).
†4. Is ¬( p ↔ q) logically equivalent to (¬ p) ↔ (¬q)?
5. In the algebra of numbers, there is a distributive law of multiplication over ad-
dition: x (y + z) = xy + xz. What would a distributive law of addition over
multiplication look like? Is it a valid law in the algebra of numbers?
28 Chapter 2. Logic
6. The distributive laws given in Figure 2.2 are sometimes called the left distributive
laws. The right distributive laws say that ( p ∨ q) ∧ r ≡ ( p ∧ r ) ∨ (q ∧ r ) and that
( p ∧ q) ∨ r ≡ ( p ∨ r ) ∧ (q ∨ r ). Show that the right distributive laws are also
valid laws of Boolean algebra. (Note: In practice, both the left and the right
distributive laws are referred to simply as the distributive laws, and both can be
2 used freely in proofs.)
7. Show that p ∧ (q ∨ r ∨ s) ≡ ( p ∧ q) ∨ ( p ∧ r ) ∨ ( p ∧ s) for any propositions p,
q, r, and s. In words, we can say that conjunction distributes over a disjunction
of three terms. (Recall that the ∧ operator is called conjunction and ∨ is called
disjunction.) Translate into logic and verify the fact that conjunction distributes
over a disjunction of four terms. Argue that, in fact, conjunction distributes over
a disjunction of any number of terms.
8. There are two additional basic laws of logic, involving the two expression p ∧ F
and p ∨ T. What are the missing laws? Show that your answers are, in fact, laws.
9. For each of the following pairs of propositions, show that the two propositions
are logically equivalent by finding a chain of equivalences from one to the other.
State which definition or law of logic justifies each equivalence in the chain.
a) p ∧ (q ∧ p), p ∧ q b) (¬ p) → q, p ∨ q
c) ( p ∨ q) ∧ ¬q, p ∧ ¬q d) p → (q → r ), ( p ∧ q) → r
e) ( p → r ) ∧ (q → r ), ( p ∨ q) → r f) p → ( p ∧ q), p → q
†10. For each of the following compound propositions, find a simpler proposition
that is logically equivalent. Try to find a proposition that is as simple as possible.
a) ( p ∧ q) ∨ ¬q b) ¬( p ∨ q) ∧ p c) p → ¬ p
d) ¬ p ∧ ( p ∨ q) e) (q ∧ p) → q f) ( p → q) ∧ (¬ p → q)
†11. Express the negation of each of the following sentences in natural English:
a) It is sunny and cold.
b) I will have stroopwafel or I will have appeltaart.
c) If today is Tuesday, this is Belgium.
d) If you pass the final exam, you pass the course.
12. Apply one of the laws of logic to each of the following sentences, and rewrite it
as an equivalent sentence. State which law you are applying.
a) I will have coffee and stroopwafel or appeltaart.
b) He has neither talent nor ambition.
c) You can have oliebollen, or you can have oliebollen.
13. Suppose it is simultaneously true that “All lemons are yellow” and “Not all lem-
ons are yellow”. Derive the conclusion “Unicorns exist”. (If you get stuck, check
out en.wikipedia.org/wiki/Principle_of_explosion.)
Á
As we mentioned earlier, other textbooks might use different
notations to represent a negation. For instance a bar over the
variable x̄ or a ∼ symbol. In digital logic (and thus in your Com-
puter Organisation course) you will also often find the + symbol
to represent an ‘or’ and a · (dot) symbol to represent an ‘and’.
30 Chapter 2. Logic
2
A
output
B
C
Figure 2.3: The standard symbols for the three basic logic gates, and a
logic circuit that computes the value of the logical expression (¬ A) ∧ ( B ∨
¬( A ∧ C )). The input wires to each logic gate are on the left, with the
output wire on the right. Note that when wires cross each other in a diagram
such as this, the wires don’t actually intersect unless there is a black circle
at the point where they cross.
Other types of logic gates are, of course, possible. Gates could be made
to compute A → B or A ⊕ B, for example. However, any computation
that can be performed by logic gates can be done using only AND, OR, and
NOT gates, as we will see below. (In practice, however, NAND gates and NOR
gates, which compute the values of ¬( A ∧ B) and ¬( A ∨ B) respectively, are
often used because they are easier to build from transistors than AND and
OR gates.)
ition. The proposition itself is a blueprint for the circuit. As noted in Sec-
tion 2.1, every logical operator that we have encountered can be expressed
in terms of ∧, ∨, and ¬, so in fact every compound proposition that we know
how to write can be computed by a logic circuit.
Given a proposition constructed from ∧, ∨, and ¬ operators, it is easy
to build a circuit to compute it. First, identify the main operator in the
2
proposition—the one whose value will be computed last. Consider ( A ∨
B) ∧ ¬( A ∧ B). This circuit has two input values, A and B, which are rep-
resented by wires coming into the circuit. The circuit has an output wire
that represents the computed value of the proposition. The main operator
in ( A ∨ B) ∧ ¬( A ∧ B), is the first ∧, which computes the value of the ex-
pression as a whole by combining the values of the subexpressions A ∨ B
and ¬( A ∧ B). This ∧ operator corresponds to an AND gate in the circuit
that computes the final output of the circuit.
Once the main operator has been identified and represented as a logic
gate, you just have to build circuits to compute the input or inputs to that
operator. In the example, the inputs to the main AND gate come from two
subcircuits. One subcircuit computes the value of A ∨ B and the other com-
putes the value of ¬( A ∧ B). Building each subcircuit is a separate problem,
but smaller than the problem you started with. Eventually, you’ll come to
a gate whose input comes directly from one of the input wires—A or B in
this case—instead of from a subcircuit.
(A B) (A B)
Figure 2.4: Stages in the construction of a circuit that computes the com-
pound proposition ( A ∨ B) ∧ ¬( A ∧ B).
The key to understanding why this is true is to note that each wire in the
circuit—not just the final output wire—represents the value of some pro-
position. Furthermore, once you know which proposition is represented by
each input wire to a gate, it’s obvious what proposition is represented by
the output: You just combine the input propositions with the appropriate
∧, ∨, or ¬ operator, depending on what type of gate it is. To find the propos-
ition associated with the final output, you just have to start from the inputs
and move through the circuit, labeling the output wire of each gate with the
proposition that it represents. Figure 2.6 illustrates this process.
1 2
A A B (A B)
5
(A B)
B
(B C)
C B C
C
4
3
Figure 2.6: Finding the proposition whose value is computed by a combin-
atorial logic circuit. Each wire in the circuit is labeled with the proposition
that it represents. The numbering of the labels shows one of the orders in
which they can be associated with the wires. The circuit as a whole com-
putes the value of ¬( A ∧ B) ∧ ( B ∨ ¬C ).
Á
Normal forms are part of the syllabus for Reasoning & Logic.
These normal forms, such as Disjunctive Normal Form (this sub-
section) and Conjunctive Normal Form (see the exercises), are
important in propositional logic. There are normal forms for
other logics, too, such as for predicate logic which we’ll look at
in the next Section 2.4.
( p ∧ q ∧ r ) ∨ ( p ∧ ¬q ∧ r ∧ s) ∨ (¬ p ∧ ¬q)
( p ∧ ¬q)
( A ∧ ¬ B) ∨ (¬ A ∧ B)
p ∨ (¬ p ∧ q) ∨ (¬ p ∧ ¬q ∧ r ) ∨ (¬ p ∧ ¬q ∧ ¬r ∧ w)
Propositions in DNF are just what we need to deal with input/output tables
of the type that we have been discussing. Any such table can be computed
by a proposition in disjunctive normal form. It follows that it is possible to
build a circuit to compute that table using only AND, OR, and NOT gates.
Theorem 2.3. Consider a table that lists a logical output value for every combin-
ation of values of several propositional variables. Assume that at least one of the
output values is true. Then there is a proposition containing those variables such
that the value of the proposition for each possible combination of the values of the
2.3. Application: Logic Circuits 35
variables is precisely the value specified in the table. It is possible to choose the
proposition to be in disjunctive normal form.
Proof. Consider any row in the table for which the output value is T. Form a
conjunction of simple terms as follows: For each variable, p, whose value is
T in that row, include p itself in the conjunction; for each variable, q, whose 2
value is F in the row, include ¬q in the conjunction. The value of this con-
junction is T for the combination of variable values given in that row of the
table, since each of the terms in the conjuction is true for that combination of
variables. Furthermore, for any other possible combination of variable val-
ues, the value of the conjunction will be F, since at least one of the simple
terms in the conjunction will be false.
Take the disjunction of all such conjunctions constructed in this way, for
each row in the table where the output value is true. This disjunction has
the value T if and only if one of the conjunctions that make it up has the
value T—and that is precisely when the output value specified by the table
is T. So, this disjunction of conjunctions satisfies the requirements of the
theorem.
®
This is the first proof of a non-trivial claim that we’ve seen. You
will learn about theorems and proofs, and proof techniques, at
the end of this chapter and in Chapter 3.
p q r output
F F F F
F F T T (¬ p ∧ ¬q ∧ r )
2 F T F F
F T T T (¬ p ∧ q ∧ r )
T F F F
T F T F
T T F F
T T T T p∧q∧r
Figure 2.7: An input/output table specifying a desired output for each
combination of values of the propositional variables p, q, and r. Each row
where the output is T corresponds to a conjunction, shown next to that row
in the table. The disjunction of these conjunctions is a proposition whose
output values are precisely those specified by the table.
b
Our preference for simpler applies to compound propositions,
whether or not they correspond to circuits. We usually prefer
the equivalent form of the proposition that is simpler. Any pro-
position has an equivalent proposition in DNF. So when proving
a theorem about compound propositions, it is sufficient to con-
sider only DNF propositions. This can make the proof easier to
write.
but you can get by with ten gates in the circuit: Use a single NOT gate to compute ¬ p, and
connect the output of that gate to two different AND gates. Reusing the output of a logic gate is
an obvious way to simplify circuits that does not correspond to any operation on propositions.
38 Chapter 2. Logic
A B C output A B C output
0 0 0 0 0 0 0 0
0 0 1 1 0 0 1 0
2 0
0
1
1
0
1
1
0
0
0
1
1
0
1
0
1
1 0 0 1 1 0 0 0
1 0 1 0 1 0 1 1
1 1 0 0 1 1 0 1
1 1 1 1 1 1 1 1
Figure 2.8: Input/output tables for the addition of three binary digits, A,
B, and C.
numbers, you line them up one on top of the other, and add the digits in
each column. In each column, there might also be a carry from the previous
column. To add up a column, you only need to remember a small number
of rules, such as 7 + 6 + 1 = 14 and 3 + 5 + 0 = 8. For binary addition, it’s
even easier, since the only digits are 0 and 1. There are only eight rules:
0 + 0 + 0 = 00 1 + 0 + 0 = 01
0 + 0 + 1 = 01 1 + 0 + 1 = 10
0 + 1 + 0 = 01 1 + 1 + 0 = 10
0 + 1 + 1 = 10 1 + 1 + 1 = 11
Here, we’ve written each sum using two digits. In a multi-column addition,
one of these digits is carried over to the next column. Here, we have a calcu-
lation that has three inputs and two outputs. We can make an input/output
table for each of the two outputs. The tables are shown in Figure 2.8. We
know that these tables can be implemented as combinatorial circuits, so we
know that circuits can add binary numbers. To add multi-digit binary num-
bers, we just need one copy of the basic addition circuit for each column in
the sum.
Exercises
1. Using only AND, OR, and NOT gates, draw circuits that compute the value of each
of the propositions A → B, A ↔ B, and A ⊕ B.
2. For each of the following propositions, find a combinatorial logic circuit that
computes that proposition:
2.3. Application: Logic Circuits 39
a) A ∧ ( B ∨ ¬C ) b) ( p ∧ q) ∧ ¬( p ∧ ¬q)
c) ( p ∨ q ∨ r ) ∧ (¬ p ∨ ¬q ∨ ¬r ) d) ¬( A ∧ ( B ∨ C )) ∨ ( B ∧ ¬ A)
3. Find the compound proposition computed by each of the following circuits:
A
B
2
C
C
4. This section describes a method for finding the compound proposition com-
puted by any combinatorial logic circuit. This method fails if you try to apply it
to a circuit that contains a feedback loop. What goes wrong? Give an example.
5. Show that every compound proposition which is not a contradiction is equival-
ent to a proposition in disjunctive normal form. (Note: We can eliminate the re-
striction that the compound proposition is not a contradiction by agreeing that
‘F’ counts as a proposition in disjunctive normal form. F is logically equivalent
to any contradiction.)
6. A proposition in conjunctive normal form (CNF) is a conjunction of disjunctions
of simple terms (with the proviso, as in the definition of DNF that a single item
counts as a conjunction or disjunction). Show that every compound proposition
which is not a tautology is logically equivalent to a compound proposition in
conjunctive normal form. (Hint: What happens if you take the negation of a
DNF proposition and apply DeMorgan’s Laws?)
7. Use the laws of Boolean algebra to simplify each of the following circuits:
A
A
B
B
C
C
8. Design circuits to implement the input/output tables for addition, as given in
Figure 2.8. Try to make your circuits as simple as possible. (The circuits that are
40 Chapter 2. Logic
used in real computers for this purpose are more simplified than the ones you
will probably come up with, but the general approach of using logic to design
computer circuits is valid. If you are interested to learn more about this, the
second year variant course Digital Systems describes circuit design in more de-
tail.)
2
2.4 Predicate Logic
In propositional logic, we can let p stand for “Roses are red” and q stand
for “Violets are blue”. Then p ∧ q will stand for “Roses are red and violets
are blue”. But we lose a lot in the translation into logic. Since propositional
logic only deals with truth values, there’s nothing we can do with p and
q in propositional logic that has anything to do with roses, violets, or col-
our. To apply logic to such things, we need predicates. The type of logic
that uses predicates is called predicate logic or, when the emphasis is on
manipulating and reasoning with predicates, predicate calculus.
2.4.1 Predicates
A predicate is a kind of incomplete proposition, which becomes a propos-
ition when it is applied to some entity (or, as we’ll see later, to several en-
tities). In the proposition “the rose is red”, the predicate is is red. By itself,
‘is red’ is not a proposition. Think of it as having an empty slot, that needs
to be filled in to make a proposition: “— is red”. In the proposition “the
rose is red”, the slot is filled by the entity “the rose”, but it could just as well
be filled by other entities: “the barn is red”; “the wine is red”; “the banana
is red”. Each of these propositions uses the same predicate, but they are
different propositions and they can have different truth values.
If P is a predicate and a is an entity, then P( a) stands for the proposition
that is formed when P is applied to a. If P represents ‘is red’ and a stands
for ‘the rose’, then P( a) is ‘the rose is red’. If M is the predicate ‘is mortal’
and s is ‘Socrates’, then M (s) is the proposition “Socrates is mortal”.
Now, you might be asking, just what is an entity anyway? I am using the
term here to mean some specific, identifiable thing to which a predicate can
be applied. Generally, it doesn’t make sense to apply a given predicate to
every possible entity, but only to entities in a certain category. For example,
it probably doesn’t make sense to apply the predicate ‘is mortal’ to your
living room sofa. This predicate only applies to entities in the category of
living things, since there is no way something can be mortal unless it is alive.
This category is called the domain of discourse for the predicate.9
9 In the language of set theory, which will be introduced in Chapter 4, we would say that a
domain of discourse is a set, U, and a predicate is a function from U to the set of truth values.
2.4. Predicate Logic 41
2
Definition 2.7. A one-place predicate associates a proposition with each
entity in some collection of entities. This collection is called the domain of
discourse for the predicate. If P is a predicate and a is an entity in the do-
main of discourse for P, then P( a) denotes the proposition that is associated
with a by P. We say that P( a) is the result of applying P to a.
The definition should be clear enough without the formal language of set theory, and in fact
you should think of this definition—and many others—as motivation for that language.
42 Chapter 2. Logic
2.4.2 Quantifiers
Let’s go back to the proposition with which we started this section: “Roses
are red”. This sentence is more difficult to handle than it might appear. We
still can’t express it properly in logic. The problem is that this proposition
is not saying something about some particular entity. It really says that
all roses are red (which happens to be a false statement, but that’s what it
means). Predicates can only be applied to individual entities.
Many other sentences raise similar difficulties: “All persons are mortal.”
“Some roses are red, but no roses are black.” “All maths courses are inter-
esting.” “Every prime number greater than two is odd.” Words like all, no,
some, and every are called quantifiers. We need to be able to express similar
concepts in logic.
Suppose that P is a predicate, and we want to express the proposition
that P is true when applied to any entity in the domain of discourse. That is,
we want to say “for any entity x in the domain of discourse, P( x ) is true”. In
predicate logic, we write this in symbols as ∀ x ( P( x )). The ∀ symbol, which
looks like an upside-down ‘A’, is usually read ‘for all’, so that ∀ x ( P( x )) is
read as ‘for all x, P( x )’. (It is understood that this means for all x in the
domain of discourse for P.) For example, if R is the predicate ‘is red’ and
the domain of discourse consists of all roses, then ∀ x ( R( x )) expresses the
proposition “All roses are red”. Note that the same proposition could be
expressed in English as “Every rose is red” or “Any rose is red”.
Now, suppose we want to say that a predicate, P, is true for some entity
in its domain of discourse. This is expressed in predicate logic as ∃ x ( P( x )).
2.4. Predicate Logic 43
The ∃ symbol, which looks like a backwards ‘E’, is usually read ‘there exists’,
but a more exact reading would be ‘there is at least one’. Thus, ∃ x ( P( x )) is
read as ‘There exists an x such that P( x )’ , and it means “there is at least one
x in the domain of discourse for P for which P( x ) is true”. If, once again,
R stands for ‘is red’ and the domain of discourse is ‘roses’, then ∃ x ( R( x ))
could be expressed in English as “There is a red rose” or “At least one rose 2
is red” or “Some rose is red”. It might also be expressed as “Some roses are
red”, but the plural is a bit misleading since ∃ x ( R( x )) is true even if there
is only one red rose. We can now give the formal definitions:
and a is an entity. But that’s only because we said so. Any letter could be used in either role,
and you have to pay attention to the context to figure out what is going on. Usually, x, y, and
z will be variables.
44 Chapter 2. Logic
∀y( L( x, y)), giving ∀ x ∀y( L( x, y)) and ∃ x ∀y( L( x, y)) . Since all the vari-
ables are bound in these expressions, they are propositions. If L( x, y) repres-
ents ‘x loves y’,then ∀y( L( x, y)) is something like “x loves everyone”, and
∃ x ∀y( L( x, y)) is the proposition, “There is someone who loves everyone”.
Of course, we could also have started with ∃ x ( L( x, y)): “There is someone
2 who loves y”. Applying ∀y to this gives ∀y ∃ x ( L( x, y)) , which means “For
every person, there is someone who loves that person”. Note in particular
that ∃ x ∀y( L( x, y)) and ∀y ∃ x ( L( x, y)) do not mean the same thing. Al-
together, there are eight different propositions that can be obtained from
L( x, y) by applying quantifiers, with six distinct meanings among them.
®
From now on, we will leave out parentheses when there is no am-
biguity. For example, we will write ∀ x P( x ) instead of ∀ x ( P( x ))
and ∃ x ∃y L( x, y) instead of ∃ x ∃y( L( x, y)) . Make sure though
that when you leave out the parentheses you do so only when no
ambiguity exists. In one of the problems of this chapter, you will
see an example of two very similar statements where the paren-
theses do change the meaning significantly!
Further, we will sometimes give predicates and entities names
that are complete words instead of just letters, as in Red( x ) and
Loves( john, mary). This might help to make examples more read-
able.
2.4.3 Operators
In predicate logic, the operators and laws of Boolean algebra still apply. For
example, if P and Q are one-place predicates and a is an entity in the domain
of discourse, then P( a) → Q( a) is a proposition, and it is logically equival-
ent to ¬ P( a) ∨ Q( a). Further, if x is a variable, then P( x ) → Q( x ) is an open
statement, and ∀ x ( P( x ) → Q( x )) is a proposition. So are P( a) ∧ (∃ x Q( x ))
and (∀ x P( x )) → (∃ xP( x )). Obviously, predicate logic can be very express-
ive. Unfortunately, the translation between predicate logic and English sen-
tences is not always obvious.
Let’s look one more time at the proposition “Roses are red”. If the do-
main of discourse consists of roses, this translates into predicate logic as
∀ x Red( x ). However, the sentence makes more sense if the domain of dis-
course is larger—for example if it consists of all flowers. Then “Roses are
red” has to be read as “All flowers which are roses are red”, or “For any
flower, if that flower is a rose, then it is red”. The last form translates dir- 2
ectly into logic as ∀ x Rose( x ) → Red( x ) . Suppose we want to say that all
red roses are pretty. The phrase ‘red rose’ is saying both that the flower is a
rose and that it is red, and it must be translated as a conjunction, Rose( x ) ∧
Red( x ). So, “All red roses are pretty” can be rendered as ∀ x ( Rose( x ) ∧
Red( x )) → Pretty( x ) .
46 Chapter 2. Logic
Ď
Here are a few more examples of translations from predicate lo-
gic to English. Let H ( x ) represent ‘x is happy’, let C (y) represent
‘y is a computer’, and let O( x, y) represent ‘x owns y’. Then we
have the following translations:
2 • Jack owns a computer: ∃ x O( jack, x ) ∧ C ( x ) . (That is,
there is at least one thing such that Jack owns that thing
and that thing is a computer.)
• Everything Jack owns is a computer: ∀ x O( jack, x ) →
C(x) .
• If Jack owns a computer, then he’s
happy:
∃y(O( jack, y) ∧ C (y)) → H ( jack).
• Everyone who owns a computeris happy:
∀ x ∃y(O( x, y) ∧ C (y) → H ( x ) .
• Everyone owns a computer: ∀ x ∃y C (y) ∧ O( x, y) . (Note
that this allows each person to own a different computer.
The proposition ∃y ∀ x C (y) ∧ O( x, y) would mean that
there is a single computer which is owned by everyone.)
• Everyone is happy: ∀ xH ( x ).
• Everyone is unhappy: ∀ x (¬ H ( x )).
• Someone is unhappy: ∃ x (¬ H ( x )).
• At least two people are happy: ∃ x ∃y H ( x ) ∧ H (y) ∧ ( x ̸=
y) . (The stipulation that x ̸= y is necessary because two
different variables can refer to the same entity. The propos-
ition ∃ x ∃y( H ( x ) ∧ H (y)) is true even if there is only one
happy person.)
• There is exactly one happy person:
∃ xH ( x ) ) ∧ ∀y∀z(( H (y) ∧ H (z)) → (y = z)) .
(The first part of this conjunction says that there is at least
one happy person. The second part says that if y and z are
both happy people, then they are actually the same person.
That is, it’s not possible to find two different people who are
happy. The statement can be simplified a little however, to
get: ∃ x ( H ( x ) ∧ ∀y( H (y) → ( x = y))). Do you see why
this works as well?)
2.4. Predicate Logic 47
e d
a
2
b
Figure 2.9: An instance of a Tarski World.
Notice that for the one-place predicates we have a set of objects for which
this predicate is true (e.g., only b and c are blue) and such a set is denoted
using ‘{’ and ‘}’ symbols, called ‘curly braces’ or just ‘braces’.12 For the two-
place predicates we have a set of tuples that are denoted using ‘(’ and ‘)’
symbols, called ‘parentheses’ or ‘round brackets’. In this case, for instance,
the fact that ( a, b) is in the set LeftOf S means that LeftOf ( a, b) is true for this
structure, i.e., a is left of b.
Such formal structures can also be defined to disprove arguments writ-
ten in predicate logic, as we will see in Section 2.5.3.
¬ (∀ xP( x )) ≡ ∃ x (¬ P( x ))
¬ (∃ xP( x )) ≡ ∀ x (¬ P( x ))
∀ x ∀yQ( x, y) ≡ ∀y∀ xQ( x, y)
∃ x ∃yQ( x, y) ≡ ∃y∃ xQ( x, y) 2
Figure 2.10: Four important rules of predicate logic. P can be any one-
place predicate, and Q can be any two-place predicate. The first two rules
are called DeMorgan’s Laws for predicate logic.
two formulas are logically equivalent if they have the same truth value for
all possible predicates.
Consider ¬(∀ xP( x )) and ∃ x (¬ P( x )). These formulas make sense for
any predicate P, and for any predicate P they have the same truth value.
Unfortunately, we can’t—as we did in propositional logic—just check this
fact with a truth table: there are no subpropositions, connected by ∧, ∨, etc,
out of which to build a table. So, let’s reason it out: To say ¬(∀ xP( x )) is true
is just to say that it is not the case that P( x ) is true for all possible entities
x. So, there must be some entity a for which P( a) is false. Since P( a) is
false, ¬ P( a) is true. But saying that there is an a for which ¬ P( a) is true is
just saying that ∃ x (¬ P( x )) is true. So, the truth of ¬(∀ xP( x )) implies the
truth of ∃ x (¬ P( x )). On the other hand, if ¬(∀ xP( x )) is false, then ∀ xP( x )
is true. Since P( x ) is true for every x, ¬ P( x ) is false for every x; that is, there
is no entity a for which the statement ¬ P( a) is true. But this just means
that the statement ∃ x (¬ P( x )) is false. In any case, then, the truth values of
¬(∀ xP( x )) and ∃ x (¬ P( x )) are the same. Since this is true for any predicate
P, we will say that these two formulas are logically equivalent and write
¬(∀ xP( x )) ≡ ∃ x (¬ P( x )).
A similar argument would show that ¬(∃ xP( x )) ≡ ∀ x (¬ P( x )). These
two equivalences, which explicate the relation between negation and quan-
tification, are known as DeMorgan’s Laws for predicate logic. (They are
closely related to DeMorgan’s Laws for propositional logic; see the exer-
cises.) These laws can be used to help simplify expressions. For example,
It might not be clear exactly why this qualifies as a ‘simplification’, but it’s
generally considered simpler to have the negation operator applied to basic
propositions such as R(y), rather than to quantified expressions such as
50 Chapter 2. Logic
DeMorgan’s Laws are listed in Figure 2.10 along with two other laws of pre-
dicate logic. The other laws allow you to interchange the order of the vari-
ables when two quantifiers of the same type (both ∃ or ∀) occur together.
»
Notice however that we may not change the order of quanti-
fiers that are not the same! For instance: ∀ x ∃y( R( x, y)) ̸≡
∃y∀ x ( R( x, y)). If you are not convinced about this, try to draw
up a Tarski’s world that shows this unequivalence.
Exercises
†1. Simplify each of the following propositions. In your answer, the ¬ operator
should be applied only to individual predicates.
a) ¬ ∀ x (¬ P( x )) b) ¬ ∃ x ( P( x ) ∧ Q( x ))
2.4. Predicate Logic 51
c) ¬ ∀z( P(z) → Q(z)) d) ¬ (∀ xP( x )) ∧ (∀yQ(y))
e) ¬ ∀ x ∃yP( x, y) f) ¬ ∃ x ( R( x ) ∧ ∀yS( x, y))
g) ¬ ∃y( P(y) ↔ Q(y)) h) ¬ ∀ x ( P( x ) → (∃yQ( x, y)))
2. Give a careful argument to show that the second of DeMorgan’s laws for predic-
ate calculus, ¬(∀ xP( x )) ≡ ∃ x (¬ P( x )), is valid.
3. Find the negation of each of the following propositions. Simplify the result; in
2
your answer, the ¬ operator should be applied only to individual predicates.
a) ∃n(∀sC (s, n))
b) ∃n(∀s( L(s, n) → P(s)))
c) ∃n(∀s( L(s, n) → (∃ x ∃y∃zQ( x, y, z)))).
d) ∃n(∀s( L(s, n) → (∃ x ∃y∃z(s = xyz ∧ R( x, y) ∧ T (y) ∧ U ( x, y, z)))).
4. Suppose that the domain of discourse for a predicate P contains only two entities.
Show that ∀ xP( x ) is equivalent to a conjunction of two simple propositions, and
∃ xP( x ) is equivalent to a disjunction. Show that in this case, DeMorgan’s Laws
for propositional logic and DeMorgan’s Laws for predicate logic actually say
exactly the same thing. Extend the results to a domain of discourse that contains
exactly three entities.
5. Let H ( x ) stand for ‘x is happy’, where the domain of discourse consists of people.
Express the proposition “There are exactly three happy people” in predicate lo-
gic.
6. What is the difference between the following two statements?
∃ xRed( x ) ∧ ∃ xSquare( x ) and ∃ x (Red( x ) ∧ Square( x ))
7. Draw a Tarski world for the last exercise.
†8. Express Johan Cruyff’s statement “There is only one ball, so you need to have it”
in predicate logic.
9. Let T ( x, y) stand for ‘x has taken y’, where the domain of discourse for x con-
sists of students and the domain of discourse for y consists of CS courses (at
TUDelft). Translate each of the following propositions into an unambiguous
English sentence:
a) ∀ x ∀y T ( x, y) b) ∀ x ∃y T ( x, y) c) ∀y ∃ x T ( x, y)
d) ∃ x ∃y T ( x, y) e) ∃ x ∀y T ( x, y) f) ∃y ∀ x T ( x, y)
10. Let F ( x, t) stand for “You can fool person x at time t.” Translate the following
sentence into predicate logic: “You can fool some of the people all of the time,
and you can fool all of the people some of the time, but you can’t fool all of the
people all of the time.”
11. Translate each of the following sentences into a proposition using predicate logic.
Make up any predicates you need. State clearly what each predicate means.
a) All crows are black.
b) Any white bird is not a crow.
c) Not all politicians are honest.
d) All purple elephants have green feet.
e) There is no one who does not like pizza.
52 Chapter 2. Logic
f) Anyone who passes the final exam will pass the course.13
g) If x is any positive number, then there is a number y such that y2 = x.
12. Consider the following description of a Tarski World. Does an instance of a
Tarski World exist with these properties? If so, give one with a domain of at
most 5 elements. If no such instance exists, explain why not.
2 • ∀ x (Circle( x ) → ¬Blue( x ))
• ∃ x (Circle( x )) ∧ ∃ x (Blue( x ))
• RightOf ( a, b)
• LeftOf ( a, b) ∨ Square(c)
†13. The sentence “Someone has the answer to every question” is ambiguous. Give
two translations of this sentence into predicate logic, and explain the difference
in meaning.
14. The sentence “Jane is looking for a dog” is ambiguous. One meaning is that
there is some particular dog—maybe the one she lost—that Jane is looking for.
The other meaning is that Jane is looking for any old dog—maybe because she
wants to buy one. Express the first meaning in predicate logic. Explain why the
second meaning is not expressed by ∀ x ( Dog( x ) → LooksFor ( jane, x )). In fact,
the second meaning cannot be expressed in predicate logic. Philosophers of
language spend a lot of time thinking about things like this. They are especially
fond of the sentence “Jane is looking for a unicorn”, which is not ambiguous
when applied to the real world. Why is that?
2.5 Deduction
Logic can be applied to draw conclusions from a set of premises. A premise
is just a proposition that is known to be true or that has been accepted to be
true for the sake of argument, and a conclusion is a proposition that can be
deduced logically from the premises. The idea is that if you believe that the
premises are true, then logic forces you to accept that the conclusion is true.
An argument is a claim that a certain conclusion follows from a given set of
premises. Here is an argument laid out in a traditional format:
If today is Tuesday, then this is Belgium
Today is Tuesday
∴ This is Belgium
The premises of the argument are shown above the line, and the conclusion
below. The symbol ∴ is read ‘therefore’. The claim is that the conclusion,
“This is Belgium”, can be deduced logically from the two premises, “If today
is Tuesday, then this is Belgium” and “Today is Tuesday”. In fact, this claim
is true. Logic forces you to accept this argument. Why is that?
13 This is not true for Reasoning & Logic: see the syllabus.
2.5. Deduction 53
2.5.1 Arguments
Let p stand for the proposition “Today is Tuesday”, and let q stand for the
proposition “This is Belgium”. Then the above argument has the form
p→q
p
2
∴ q
Now, for any propositions p and q—not just the ones in this particular argument—
if p → q is true and p is true, then q must also be true. This is easy to check
in a truth table:
p q p→q
0 0 1
0 1 1
1 0 0
1 1 1
The only case where both p → q and p are true is on the last line of the
table, and in this case, q is also true. If you believe p → q and p, you have no
logical choice but to believe q. This applies no matter what p and q represent.
For example, if you believe “If Jill is breathing, then Jill pays taxes”, and
you believe that “Jill is breathing”, logic forces you to believe that “Jill pays
taxes”. Note that we can’t say for sure that the conclusion is true, only that
if the premises are true, then the conclusion must be true.
This fact can be rephrased by saying that ( p → q) ∧ p → q is a tau-
tology. More generally, for any compound propositions P and Q, saying
“P → Q is a tautology” is the same as saying that “in all cases where P
is true, Q is also true”.14 We will use the notation P =⇒ Q to mean that
P → Q is a tautology. Think of P as being the premise of an argument or
the conjunction of several premises. To say P =⇒ Q is to say that Q follows
logically from P. We will use the same notation in both propositional logic
and predicate logic. (Note that the relation of =⇒ to → is the same as the
relation of ≡ to ↔.)
Definition 2.10. Let P and Q be any formulas in either propositional logic
or predicate logic. The notation P =⇒ Q is used to mean that P → Q is a
tautology. That is, in all cases where P is true, Q is also true. We then say
that Q can be logically deduced from P or that P logically implies Q.
14 Here, “in all cases” means for all combinations of truth values of the propositional vari-
ables in P and Q, i.e., in every situation. Saying P → Q is a tautology means it is true in all
cases. But by definition of →, it is automatically true in cases where P is false. In cases where
P is true, P → Q will be true if and only if Q is true.
54 Chapter 2. Logic
p→q
¬q
∴ ¬p
To verify that this is a valid argument, just check that ( p → q) ∧ ¬q =⇒
¬ p, that is, that ( p → q) ∧ ¬q → ¬ p is a tautology. As an example, the
following argument has the form of modus tollens and is therefore a valid
argument:
You might remember this argument from page 13. You should note carefully
that the validity of this argument has nothing to do with whether or not
Feyenoord can play football well. The argument forces you to accept the
conclusion only if you accept the premises. You can logically believe that the
conclusion is false, as long as you believe that at least one of the premises is
false.15
Another named rule of deduction is the Law of Syllogism, which has
the form
p→q
q→r
∴ p→r
For example:
15 Unless the conclusion is a tautology. If that’s the case, then even when a premise is false
the conclusion will still be true. You do always know that if the conclusion is false then at least
one of the premises is false.
2.5. Deduction 55
There are many other rules. Here are a few that might prove useful.
Some of them might look trivial, but don’t underestimate the power of a
2
simple rule when it is combined with other rules.
p∨q p p∧q p
¬p q ∴ p ∴ p∨q
∴ q ∴ p∧q
( p ∧ r) → s
q→p
t→r
q
t
∴ s
Is this argument valid? Of course, you could use a truth table to check
whether the conjunction of the premises logically implies the conclusion.
But with five propositional variables, the table would have 32 lines, and the
size of the table grows quickly when more propositional variables are used.
So, in general, truth tables are not practical when we have a large number
of variables.
56 Chapter 2. Logic
The existence of such a proof shows that the conclusion follows logically
from the premises, and therefore that the argument is valid. Here is a formal
proof that the argument given above is valid. The propositions in the proof
are numbered, and each proposition has a justification.
Proof.
1. q→p premise
2. q premise
3. p from 1 and 2 (modus ponens)
4. t→r premise
5. t premise
6. r from 4 and 5 (modus ponens)
7. p∧r from 3 and 6
8. ( p ∧ r) → s premise
9. s from 7 and 8 (modus ponens)
2.5. Deduction 57
b
Once a formal proof has been constructed, it is convincing. Un-
fortunately, it’s not necessarily easy to come up with the proof.
Usually, the best method is a combination of working forward
(“Here’s what I know, what can I deduce from that?”) and work-
ing backwards (“Here’s what I need to prove, what other things
would imply that?”). For this proof, I might have thought: I want 2
to prove s. I know that p ∧ r implies s, so if I can prove p ∧ r, I’m
okay. But to prove p ∧ r, it’ll be enough to prove p and r separ-
ately….
Of course, not every argument is valid, so the question also arises, how
can we show that an argument is invalid? Let’s assume that the argument
has been put into general form, with all the specific propositions replaced
by propositional variables. The argument is valid if in all cases where all
the premises are true, the conclusion is also true. The argument is invalid
if there is even one case where all the premises are true and the conclusion
is false. We can prove that an argument is invalid by finding an assignment
of truth values to the propositional variables which makes all the premises
true but makes the conclusion false. We call such an assignment a counter-
example . To disprove the validity of an argument you should always provide
a counterexample. This holds in propositional logic, predicate logic, and
any other type of argument you may be asked to disprove.
For example, consider an argument of the form:
p→q
q → ( p ∧ r)
r
∴ p
In the case where p is false, q is false, and r is true, the three premises of
this argument are all true, but the conclusion is false. This counterexample
shows that the argument is invalid.
To apply all this to arguments stated in English, we have to introduce
propositional variables to represent all the propositions in the argument.
For example, consider:
John will be at the party if Mary is there and Bill is not there.
Mary will be at the party if it’s on Friday or Saturday. If Bill is
at the party, Tom will be there. Tom won’t be at the party if it’s
on Friday. The party is on Friday. Therefore, John will be at the
party.
Let j stand for “John will be at the party”, m for “Mary will be there”, b for
“Bill will be there”, t for “Tom will be there”, f for “The party is on Friday”,
and s for “The party is on Saturday”. Then this argument has the form
58 Chapter 2. Logic
(m ∧ ¬b) → j
( f ∨ s) → m
b→t
f → ¬t
f
2 ∴ j
Proof.
1. f → ¬t premise
2. f premise
3. ¬t from 1 and 2 (modus ponens)
4. b→t premise
5. ¬b from 4 and 3 (modus tollens)
6. f ∨s from 2
7. ( f ∨ s) → m premise
8. m from 6 and 7 (modus ponens)
9. m ∧ ¬b from 8 and 5
10. (m ∧ ¬b) → j premise
11. j from 10 and 9 (modus ponens)
®
You may have noticed that we start our proofs with the word
‘proof’ and end it with a little square. This is done to illustrate
clearly where our proof starts and ends. Historically different
symbols and expressions have been used to indicate that a proof
is done. You may have heard of the abbreviation Q.E.D. for
instance for ‘Quod Erat Demonstrandum’, which translates to:
‘what was to be shown’. Even in ancient Greece a Greek version
of Q.E.D. was used by Greek mathematicians like Euclid. You
are free to choose between Q.E.D. and the open square, so long
as you remember that no proof is complete if it does not have
either one of them.
One of the most basic rules of deduction in predicate logic says that
(∀ xP( x )) =⇒ P( a) for any entity a in the domain of discourse of the pre-
dicate P. That is, if a predicate is true of all entities, then it is true of any
given particular entity. This rule can be combined with rules of deduction
for propositional logic to give the following valid arguments:
2
∀ x ( P( x ) → Q( x )) ∀ x ( P( x ) → Q( x ))
P( a) ¬ Q( a)
∴ Q( a) ∴ ¬ P( a)
These valid arguments go by the names of modus ponens and modus tollens
for predicate logic. Note that from the premise ∀ x ( P( x ) → Q( x )) we can
deduce P( a) → Q( a). From this and from the premise that P( a), we can
deduce Q( a) by modus ponens. So the first argument above is valid. The
second argument is similar, using modus tollens.
The most famous logical deduction of them all is an application of modus
ponens for predicate logic:
All humans are mortal
Socrates is human
∴ Socrates is mortal
This has the form of modus ponens with P( x ) standing for “x is human”, Q( x )
standing for “x is mortal”, and a standing for the noted entity, Socrates.
To disprove validity of arguments in predicate logic, you again need to
provide a counterexample. These are most easily given in the form of a
mathematical structure. Consider for instance the following argument:
∃ xP( x )
∀ x ( P( x ) → Q( x ))
∴ ∀ xQ( x )
This argument is not valid and we can prove that using the following
structure A.
• D = { a, b}
• PA = { a}
• QA = { a}
As you can see, the first premise is true. There is an x such that P( x )
holds, namely x = a. The second premise is also true, as for all x for which
P( x ) holds (so only x = a), Q( x ) also holds (and indeed Q( a)) holds. How-
ever the conclusion is false, as Q(b) does not hold, so the Q( x ) does not hold
for all x.
There is a lot more to say about logical deduction and proof in predicate
logic, and we’ll spend the whole of the next chapter on the subject.
60 Chapter 2. Logic
Exercises
†1. Verify the validity of modus tollens and the Law of Syllogism.
†2. Each of the following is a valid rule of deduction. For each one, give an example
of a valid argument in English that uses that rule.
2 p∨q p p∧q p
¬p q ∴ p ∴ p∨q
∴ q ∴ p∧q
†3. There are two notorious invalid arguments that look deceptively like modus pon-
ens and modus tollens:
p→q p→q
q ¬p
∴ p ∴ ¬q
Show that each of these arguments is invalid. Give an English example that uses
each of these arguments.
†4. Decide whether each of the following arguments is valid. If it is valid, give a
formal proof. If it is invalid, show that it is invalid by finding an appropriate
assignment of truth values to propositional variables.
a) p → q b) p ∧ q c) p ∨ q
q→s q → (r ∨ s ) q → (r ∧ s )
s ¬r ¬p
∴ p ∴ s ∴ s
d) (¬ p) → t e) p f) q → t
q→s s→r p → (t → s)
r→q q∨r p
¬(q ∨ t) q → ¬p ∴ q→s
∴ p ∴ ¬s
†5. For each of the following English arguments, express the argument in terms of
propositional logic and determine whether the argument is valid or invalid.
a) If it is Sunday, it rains or snows. Today, it is Sunday and it’s not raining.
Therefore, it must be snowing.
b) If there is herring on the pizza, Jack won’t eat it. If Jack doesn’t eat pizza,
he gets angry. Jack is angry. Therefore, there was herring on the pizza.
c) At 8:00, Jane studies in the library or works at home. It’s 8:00 and Jane is
not studying in the library. So she must be working at home.
Chapter 3
Proof
In this chapter, we’ll look at some approaches and techniques that can
be used for proving mathematical results, including two important proof
techniques known as proof by contradiction and mathematical induction.
Along the way, we’ll encounter a few new definitions and notations. Hope-
fully, you will be left with a higher level of confidence for exploring the
mathematical world on your own.
61
62 Chapter 3. Proof
The mathematical world and the real world weren’t always quite so separ-
ate. Until around the middle of the nineteenth century, the statements of
mathematics were regarded as statements about the world. A proof was
simply a convincing argument, rather than a chain forged of absolute logic.
It was something closer to the original meaning of the word ‘proof’, as a test
3 or trial: you might have heard of the proverb, “The proof of the pudding is
in the eating.” So, to prove something was to test its truth by putting it to
the trial of logical argument.
Á
A commonly made mistake centres around the difference
between ‘proof‘ (a noun) and ‘to prove‘ (a verb). You can prove a
certain claim using a proof. But grammatically speaking you can-
not proof a certain claim using a prove. Historically, in the course
Reasoning & Logic this is one of the most common spelling/gram-
mar mistakes on exams. By including it in this book, we hope
that you will now know better.
Near the end of the nineteenth century came another shock, in the form
of cracks in the very foundation of mathematics. At that time, mathem-
atician Gottlob Frege was finishing a book on set theory that represented
his life’s work. In Frege’s set theory, a set could be defined by any prop-
erty. You could have, for example, the set consisting of all sets that contain
three objects. As he was finishing his book, Frege received a letter from
a young mathematician named Bertrand Russell which described what be-
came known as Russell’s Paradox. Russell pointed out that the set of all
sets—that is, the set that contains every entity that satisfies the property of
being a set—cannot logically exist. We’ll see Russell’s reasoning in the fol-
lowing chapter. Frege could only include a postscript in his book stating
that the basis of the work had been swept away.
3.2. Mathematical Proof 63
mathematicians are too picky about proving things that are ‘obvious’. But
the fact that something is obvious in the real world counts for very little
in the constructed world of mathematics. Too many obvious things have
turned out to be dead wrong. (For that matter, even things in the real world
that seem ‘obviously’ true are not necessarily true at all.)
Proof. This is a proposition of the form ∀n( P(n) → Q(n)) where P(n) is “n
is even” and Q(n) is “n2 is even.” We need to show that P(n) → Q(n) is true
for all values of n. Or alternatively we can phrase it as: ∀n( E(n) → E(n2 ))
where E( x ) is ‘x is even’.
In the language of Section 2.5, we need to show that for any n, E(n) lo-
gically implies E(n2 ); or, equivalently, that E(n2 ) can be logically deduced
from E(n); or, equivalently, that
n is even
∴ n2 is even
n is even
∴ n2 is even
1. n is even premise
2. if n is even, then n = 2k
for some integer k definition of even
3. n = 2k for some integer k from 1, 2 (modus ponens)
4. if n = 2k for some integer k,
then n2 = 4k2 for that integer k basic algebra
5. n = 4k2 for some integer k
2 from 3, 4 (modus ponens)
6. if n2 = 4k2 for some integer k,
then n2 = 2(2k2 ) for that k basic algebra
7. n = 2(2k2 ) for some integer k
2 from 5, 6 (modus ponens) 3
8. if n2 = 2(2k2 ) for some integer k,
then n2 = 2k′ for some integer k′ basic fact about integers
9. n = 2k′ for some integer k′
2 from 7, 8 (modus ponens)
10. if n2 = 2k′ for some integer k′ ,
then n2 is even definition of even
2
11. n is even from 9, 10 (modus ponens)
(The “basic fact about integers” referred to above is that the product of in-
tegers is again an integer.) Since n could be replaced by any integer through-
out this argument, we have proved the statement “if n is even then n2 is
even” is true for all integers n. (You might worry that the argument is only
valid for even n; see the disclaimer about Feyenoord’s football ability on
page 54, or remind yourself that P(n) → Q(n) is automatically true if P(n)
is false.)
Mathematical proofs are rarely presented with this degree of detail and
formality. A slightly less formal proof of our proposition might leave out the
explicit implications and instances of modus ponens and appear as follows:
1. n is even premise
2. n = 2k for some integer k definition of even
3. n2 = 4k2 for that integer k basic algebra
4. n2 = 2(2k2 ) for that k basic algebra
5. n2 = 2k′ for some integer k′ substituting k′ = 2k2
6. n2 is even definition of even
Since n was an arbitrary integer, the statement is true for all integers.
A more typical proof would take the argument above and present it in
prose rather than list form:
66 Chapter 3. Proof
Á
In the course Reasoning & Logic you are learning to write proper
formal proofs, and as a part of that we also need to evaluate your
performance. To this end we ask you to write proofs similar to
the second example given above (the list form rather than the
prose form) as this shows more clearly that you are aware of the
formalisms required in a proof.
Writing a proof is even more difficult than reading a proof. Every proof
involves a creative act of discovery, in which a chain of logic that leads from
assumptions to conclusion is discovered. It also involves a creative act of
expression, in which that logic is presented in a clear and convincing way.
There is no algorithm for producing correct, coherent proofs. There are,
however, some general guidelines for discovering and writing proofs. Let’s
look at some of these next.
hypotheses and conclusion in advance, which makes things quite a bit harder—and more in-
teresting.
68 Chapter 3. Proof
Note that we have now discussed how to prove and disprove universally
quantified statements, and how to prove existentially quantified statements.
How do you disprove ∃ x P( x )? Recall that ¬∃ x P( x ) is logically equivalent
to ∀ x (¬ P( x )), so to disprove ∃ x P( x ) you need to prove ∀ x (¬ P( x )).
Many statements, like that in our example above, have the logical form of
an implication, p → q. (More accurately, they are of the form “∀ x ( P( x ) →
Q( x ))”, but as discussed above the strategy for proving such a statement
is to prove P( x ) → Q( x ) for an arbitrary element x of the domain of dis-
course.) The statement might be “For all natural numbers n, if n is even
3 then n2 is even,” or “For all strings x, if x is in the language L then x is
generated by the grammar G,”2 or “For all elements s, if s ∈ A then s ∈ B.”
Sometimes the implication is implicit rather than explicit: for example, “The
sum of two rationals is rational” is really short for “For any numbers x and
y, if x and y are rational then x + y is rational.” A proof of such a statement
often begins something like this: “Assume that p. We want to show that q.”
In the rest of the proof, p is treated as an assumption that is known to be
true. As discussed above, the logical reasoning behind this is that you are
essentially proving that
p
∴ q
is a valid argument. Another way of thinking about it is to remember that
p → q is automatically true in the case where p is false, so there is no need
to handle that case explicitly. In the remaining case, when p is true, we can
show that p → q is true by showing that the truth of q follows from the
truth of p. So remember that proving an implication you should assume
the antecedent and prove the consequent (you can refresh your memory of
what those words mean on page 12).
A statement of the form p ∧ q can be proven by proving p and q separ-
ately. A statement of the form p ∨ q can be proved by proving the logically
equivalent statement (¬ p) → q: to prove p ∨ q, you can assume that p is
false and prove, under that assumption, that q is true. For example, the state-
ment “Every integer is even or odd” is equivalent to the statement “Every
integer that is not even is odd”.
Since p ↔ q is equivalent to ( p → q) ∧ (q → p), a statement of the
form p ↔ q is often proved by giving two proofs, one of p → q and one of
q → p. In English, p ↔ q can be stated in several forms such as “p if and
only if q”, “if p then q and conversely,” and “p is necessary and sufficient for
q”. The phrase ‘if and only if’ is so common in mathematics that it is often
abbreviated iff .
2 You will learn about this in the course Automata, Computability and Complexity in your
second year.
3.2. Mathematical Proof 69
You should also keep in mind that you can prove p → q by displaying a
chain of valid implications p → r → s → · · · → q. Similarly, p ↔ q can be
proved with a chain of valid biconditionals p ↔ r ↔ s ↔ · · · ↔ q.
• The integers (denoted Z) are the numbers 0, −1, 1, −2, 2, −3, 3, . . .. Note
that the sum, product, and difference of integers are integers.
• The rational numbers (denoted Q) are all numbers that can be written
−65
n where m and n are integers and n ̸ = 0. So 3 and 7
in the form m 1
√
are rationals; so, less obviously, are 6 and √27 since 6 = 61 (or, for that
√ q q 12
− 12
matter, 6 = −2 ), and √ = 27 27 9 3
12 12 = 4 = 2 . Note the restriction
that the number in the denominator cannot be 0: 03 is not a number at
all, rational or otherwise; it is an undefined quantity. Note also that
the sum, product, difference, and quotient of rational numbers are
rational numbers (provided you don’t attempt to divide by 0).
• The irrational numbers are real numbers that are not rational, i.e., that
√
cannot be written as a ratio of integers. Such numbers include 3
(which we will prove is not rational) and π (if anyone ever told you
that π = 22 22
7 , remember that 7 is only an approximation of the value
of π). Later you will learn that we can describe this set of irrational
numbers as R − Q, that is: it is all the numbers that are in R but are
not in Q.
3.2.3 Examples
Let’s look now at another example of a proof. We set out to prove that the
sum of any two rational numbers is rational.
Proof. We start by assuming that x and y are arbitrary rational numbers.
Here’s a formal proof that the inference rule
x is rational
y is rational
∴ x + y is rational
is a valid rule of inference:
1. x is rational premise
2. if x is rational, then x = ba
for some integers a and b ̸= 0 definition of rationals
3. x = ba for some integers a and b ̸= 0 from 1,2 (modus ponens)
4. y is rational premise
5. if y is rational, then y = dc for
some integers c and d ̸= 0 definition of rational
6. y = dc for some c and d ̸= 0 from 4,5 (modus ponens)
7. x = ba for some a and b ̸= 0 and
y = dc for some c and d ̸= 0 from 3,6
8. if x = ba for some a and b ̸= 0 and
y = dc for c and d ̸= 0 then
x + y = adbd+bc where a, b, c, d
where b, d ̸= 0 then x + y = m n
where m, n are integers and n ̸= 0 properties of integers
3.2. Mathematical Proof 71
So the rule of inference given above is valid. Since x and y are arbitrary
3
rationals, we have proved that the rule is valid for all rationals, and hence
the sum of any two rationals is rational.
Again, a more informal presentation that we expect from you during the
course would look like:
• Since x and y were arbitrary rationals, the sum of any two rationals is
rational.
And one more example: we will prove that any 4-digit number d1 d2 d3 d4
is divisible by 3 iff the sum of the four digits is divisible by 3.
Rearranging gives
d1 d2 d3 d4 = d1 × 1000 + d2 × 100 + d3 × 10 + d4
so
Exercises
1. Find a natural number n for which n2 + n + 41 is not prime.
74 Chapter 3. Proof
• But now we have (i) m and n have no common factors, and (ii) m
and n have a common factor, namely 3. It is impossible for both these
things to be true, yet our argument has been logically correct.
√
• Therefore our original assumption, namely that 3 is rational, must
be incorrect.
√
• Therefore 3 must be irrational.
76 Chapter 3. Proof
One of the oldest mathematical proofs, which goes all the way back to
Euclid, is a proof by contradiction. Recall that a prime number is an integer
n, greater than 1, such that the only positive integers that evenly divide n are
1 and n. We will show that there are infinitely many primes. Before we get
to the theorem, we need a lemma. (A lemma is a theorem that is introduced
only because it is needed in the proof of another theorem. Lemmas help to
organize the proof of a major theorem into manageable chunks.)
Lemma 3.2. If N is an integer and N > 1, then there is a prime number which
3 evenly divides N.
Proof. Let D be the smallest integer which is greater than 1 and which evenly
divides N. (D exists since there is at least one number, namely N itself,
which is greater than 1 and which evenly divides N. We use the fact that
any non-empty subset of N has a smallest member.) We claim that D is
prime, so that D is a prime number that evenly divides N.
Suppose that D is not prime. We show that this assumption leads to a
contradiction. Since D is not prime, then, by definition, there is a number
k between 2 and D − 1, inclusive, such that k evenly divides D. But since
D evenly divides N, we also have that k evenly divides N (by exercise 5
in the previous section). That is, k is an integer greater than one which
evenly divides N. But since k is less than D, this contradicts the fact that
D is the smallest such number. This contradiction proves that D is a prime
number.
Proof. Suppose that there are only finitely many prime numbers. We will
show that this assumption leads to a contradiction.
Let p1 , p2 , …, pn be a complete list of all prime numbers (which exists
under the assumption that there are only finitely many prime numbers).
Consider the number N obtained by multiplying all the prime numbers to-
gether and adding one. That is,
N = ( p1 · p2 · p3 · · · pn ) + 1.
Now, since N is larger than any of the prime numbers pi , and since p1 , p2 ,
…, pn is a complete list of prime numbers, we know that N cannot be prime.
By the lemma, there is a prime number p which evenly divides N. Now, p
must be one of the numbers p1 , p2 , …, pn . But in fact, none of these numbers
evenly divides N, since dividing N by any pi leaves a remainder of 1. This
contradiction proves that the assumption that there are only finitely many
primes is false.
3.4. Mathematical Induction 77
Exercises
1. Suppose that a1 , a2 , …, a10 are real numbers, and suppose that a1 + a2 + · · · +
a10 > 100. Use a proof by contradiction to conclude that at least one of the
numbers ai must be greater than 10.
2. Prove that each of the following statements is true. In each case, use a proof by
contradiction. Remember that the negation of p → q is p ∧ ¬q.
a) Let n be an integer. If n2 is an even integer, then n is an even integer.
√
b) 2 is irrational.
c) If r is a rational number and x is an irrational number, then r + x is an
irrational number. (That is, the sum of a rational number and an irrational
number is irrational.)
d) If r is a non-zero rational number and x is an irrational number, then rx is
an irrational number.
e) If r and r + x are both rational, then x is rational.
†3. The pigeonhole principle is the following obvious observation: If you have n
pigeons in k pigeonholes and if n > k, then there is at least one pigeonhole that
contains more than one pigeon. Even though this observation seems obvious,
it’s a good idea to prove it. Prove the pigeonhole principle using a proof by
contradiction.
rigorously in the next chapter (see Theorem 4.3), but for now we just state
it as a theorem:
Theorem 3.4. Let P be a one-place predicate whose domain of discourse includes
the natural numbers. Suppose that P(0) ∧ ∀k ∈ N ( P(k) → P(k + 1)) .4 Then
P(n) is true for all natural numbers n. (That is, the statement ∀n P(n) is true,
where the domain of discourse for P is the set of natural numbers.)
Mathematical induction can be applied in many situations: you can prove
things about strings of characters by doing induction on the length of the
string, things about graphs by doing induction on the number of nodes in
3
the graph, things about grammars by doing induction on the number of
productions in the grammar, and so on. We’ll be looking at applications
of induction for the rest of this chapter, and treat a form called structural
induction in the next chapter.
Although proofs by induction can be very different from one another,
they all follow just a few basic structures. A proof based on the preced-
ing theorem always has two parts. First, P(0) is proved. This is called the
base case of the induction. Then the statement ∀k ( P(k) → P(k + 1)) is
proved. This statement can be proved by letting k be an arbitrary element
of N and proving P(k) → P(k + 1). This in turn can be proved by assum-
ing that P(k) is true and proving that the truth of P(k + 1) follows from
that assumption. This case is called the inductive case, and P(k ) is called
the inductive hypothesis or the induction hypothesis. Note that the base
case is just as important as the inductive case. By itself, the truth of the
statement ∀k ( P(k) → P(k + 1)) says nothing at all about the truth of any
of the individual statements P(n). The chain of implications P(0) → P(1),
P(1) → P(2), …, P(n − 1) → P(n) says nothing about P(n) unless the chain
is anchored at the other end by the truth of P(0).
3.4.2 Examples
Let’s look at a few examples.
Theorem 3.5. The number 22n − 1 is divisible by 3 for all natural numbers n.
Proof. Here, P(n) is the statement that 22n − 1 is divisible by 3.
Base case: When n = 0, 22n − 1 = 20 − 1 = 1 − 1 = 0 and 0 is divisible
by 3 (since 0 = 3 · 0.) Therefore the statement holds when n = 0.
Inductive case: We want to show that if the statement is true for n = k
(where k is an arbitrary natural number), then it is true for n = k + 1 also.
That is, we must prove the implication P(k ) → P(k + 1). So we assume P(k),
4 We will encounter this notation of k ∈ N again in Chapter 4. For now you should just
that is, we assume that 22k is divisible by 3. This means that 22k − 1 = 3m
for some integer m. We want to prove P(k + 1), that is, that 22(k+1) − 1 is
also divisible by 3:
22(k+1) − 1 = 22k+2 − 1
= 22k · 22 − 1 properties of exponents
= 4·2 −1
2k
3 = 4 · 22k − 4 + 4 − 1
= 4(22k − 1) + 3 algebra
= 4(3m) + 3 the inductive hypothesis
= 3(4m + 1) algebra
and from the last line we see that 22k+1 is in fact divisible by 3. (The third
step—subtracting and adding 4—was done to enable us to use our inductive
hypothesis.)
Altogether, we have proved that P(0) holds and that, for all k, P(k ) →
P(k + 1) is true. Therefore, by the principle of induction, P(n) is true for all
n in N, i.e. 22n − 1 is divisible by 3 for all n in N.
Proof. Let P(n) be the statement “There are exactly 2n different ways of as-
signing truth values to n propositional variables.” We will use induction to
prove the P(n) is true for all n ≥ 1.
Base case: First, we prove the statement P(1). If there is exactly one
variable, then there are exactly two ways of assigning a truth value to that
variable. Namely, the variable can be either true or false. Since 2 = 21 , P(1)
is true.
Inductive case: Suppose that P(k) is already known to be true. We want
to prove that, under this assumption, P(k + 1) is also true. Suppose that p1 ,
p2 , …, pk+1 are k + 1 propositional variables. Since we are assuming that
3.4. Mathematical Induction 81
P(k ) is true, we know that there are 2k ways of assigning truth values to
p1 , p2 , …, pk . But each assignment of truth values to p1 , p2 , …, pk can be
extended to the complete list p1 , p2 , …, pk , pk+1 in two ways. Namely, pk+1
can be assigned the value true or the value false. It follows that there are
2 · 2k ways of assigning truth values to p1 , p2 , …, pk+1 . Since 2 · 2k = 2k+1 ,
this finishes the proof.
5
∑ i2 = 12 + 22 + 32 + 42 + 52
i =1
7
∑ a k = a3 + a4 + a5 + a6 + a7
k =3
N
1 1 1 1 1
∑n + 1
=
0 + 1
+
1 + 1
+
2 + 1
+···+
N +1
n =0
This notation for a sum, using the ∑ operator, is called summation notation.
A similar notation for products uses the symbol ∏. (This is the Greek letter
pi, which is equivalent to the Latin letter P and stands for ‘product’.) For
example,
5
∏ (3k + 2) = (3 · 2 + 2)(3 · 3 + 2)(3 · 4 + 2)(3 · 5 + 2)
k =2
n
1 1 1 1
∏i = · ··· ·
1 2 n
i =1
Induction can be used to prove many formulas that use these notations.
Here are two examples:
n
n ( n + 1)
Theorem 3.7. ∑ i=
2
for any integer n greater than zero.
i =1
n
Theorem 3.8. ∑ i2i−1 = (n − 1) · 2n + 1 for any natural number n > 0.
i =1
n
Proof. Let P(n) be the statement ∑ i2i−1 = (n − 1) · 2n + 1. We use induc-
i =1
tion to show that P(n) is true for all n > 0
1
Base case: Consider the case n = 1. P(1) is the statement that ∑ i2i−1 =
i =1
(1 − 1) · 21 + 1. Since each side of this equation is equal to one, this is true.
Inductive case: Let k > 1 be arbitrary, and assume that P(k) is true. We
k +1
want to show that P(k + 1) is true. P(k + 1) is the statement ∑ i2i−1 =
i =1
3.5. Strong Mathematical Induction 83
Exercises
1. Use induction to prove that n3 + 3n2 + 2n is divisible by 3 for all natural numbers
n.
2. Use induction to prove that
n
1 − r n +1
∑ ri = 1−r
i =0
3.5. Strong Mathematical Induction 85
for any natural number n and for any real number r such that r ̸= 0 ∧ r ̸= 1.
3. Use induction to prove that for any natural number n,
n
1 1
∑ 2i = 2−
2n
i =0
n
n(n + 1)(2n + 1)
∑ i2 = 6
i =1
n
∑ (2i − 1) = n2
i =1
7. Evaluate the following sums, using results proved in this section and in the pre-
vious exercises:
a) 1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19
1 1 1 1 1 1
b) 1 + + 2 + 3 + 4 + 5 + 6
3 3 3 3 3 3
c) 50 + 51 + 52 + 53 + · · · + 99 + 100
d) 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100
1 1 1
e) 2 + 3 + · · · + 99
2 2 2
8. Write each of the sums in the preceding problem using summation notation.
9. Rewrite the proof of Theorem 3.8 without using summation notation.
10. Use induction to prove the following generalized distributive laws for propos-
itional logic: For any natural number n > 1 and any propositions q, p1 , p2 , …,
pn ,
a) q ∧ ( p1 ∨ p2 ∨ · · · ∨ pn ) = (q ∧ p1 ) ∨ (q ∧ p2 ) ∨ · · · ∨ (q ∧ pn )
b) q ∨ ( p1 ∧ p2 ∧ · · · ∧ pn ) = (q ∨ p1 ) ∧ (q ∨ p2 ) ∧ · · · ∧ (q ∨ pn )
86 Chapter 3. Proof
ģ
In your course Computer Organisation you will be tasked to write
a small recursive program to compute the factorial in Assembly.
You can have a sneak-preview below of of what the pseudocode
for such a program might be. In future courses like Algorithms
& Data Structures and Algorithm Design you will also be tasked to
write and analyse recursive algorithms.
follows:
0! = 1
n
n! = ∏i for n > 0
i =1
ģ
All the programming examples in this section are written in the
Java programming language. We won’t put these blue boxes
around them.
function only gives the correct answer when the answer can be represented as a 32-bit binary
number. This is the kind of implementation issue that matters in practice, especially in lower-
level languages like Assembly.
88 Chapter 3. Proof
Base case: In the case n = 0, the if statement in the function assigns the
value 1 to the answer. Since 1 is the correct value of 0!, factorial(0) correctly
computes 0!.
Inductive case: Let k be an arbitrary natural number, and assume that
P(k ) is true. From this assumption, we must show that P(k + 1) is true. The
assumption is that factorial(k) correctly computes k!, and we want to show
that factorial(k + 1) correctly computes (k + 1)!.
3 When the function computes factorial(k + 1), the value of the parameter
n is k + 1. Since k + 1 > 0, the if statement in the function computes
the value of factorial(k + 1) by applying the computation factorial(k) ∗ (k +
1). We know, by the induction hypothesis, that the value computed by
factorial(k) is k!. It follows that the value computed by factorial(k + 1) is
(k!) · (k + 1). As we observed above, for any k + 1 > 0, (k!) · (k + 1) =
(k + 1)!. We see that factorial(k + 1) correctly computes (k + 1)!. This com-
pletes the induction.
In this proof, we see that the base case of the induction corresponds to
the base case of the recursion, while the inductive case corresponds to a
recursive subroutine call. A recursive subroutine call, like the inductive
case of an induction, reduces a problem to a ‘simpler’ or ‘smaller’ problem,
which is closer to the base case.
For example, if there are two discs, the problem can be solved by the
following sequence of moves:
Move disc 1 from pile 1 to pile 3
Move disc 2 from pile 1 to pile 2
Move disc 1 from pile 3 to pile 2
A simple recursive subroutine can be used to write out the list of moves
to solve the problem for any value of n. The recursion is based on the ob-
servation that for n > 1, the problem can be solved as follows: Move n − 1
discs from pile number 1 to pile number 3 (using pile number 2 as a spare).
Then move the largest disc, disc number n, from pile number 1 to pile num-
ber 2. Finally, move the n − 1 discs from pile number 3 to pile number 2,
putting them on top of the nth disc (using pile number 1 as a spare). In
both cases, the problem of moving n − 1 discs is a smaller version of the
original problem and so can be done by recursion. Here is the subroutine,
written in Java:
1 void Hanoi(int n, int A, int B, int C) {
// List the moves for moving n discs from
3 // pile number A to pile number B, using
// pile number C as a spare. Assume n > 0.
5 if (n == 1) {
System.out.println("Move disc 1 from pile " + A + " to pile " +
B);
7 }
else {
9 Hanoi(n-1, A, C, B);
System.out.println("Move disc " + n + " from pile " +
90 Chapter 3. Proof
®
This problem and its fame have led to implementations in a vari-
ety of languages, including a language called Brainf*ck.6 In the
Computer Organisation course, you can implement an interpreter
3 for this language and test it on the implementation of the Hanoi
algorithm.
We can use induction to prove that this subroutine does in fact solve the
Towers of Hanoi problem.
Theorem 3.12. The sequence of moves printed by the Hanoi subroutine as given
above correctly solves the Towers of Hanoi problem for any integer n ≥ 1.
Exercises
3.7. Recursive Definitions 91
1. The Hanoi subroutine given in this section does not just solve the Towers of Hanoi
problem. It solves the problem using the minimum possible number of moves.
Use induction to prove this fact.
2. Use induction to prove that the Hanoi subroutine uses 2n − 1 moves to solve the
Towers of Hanoi problem for n discs.
3. Consider the following recursive function:
Show that for any integer x and any non-negative integer n, the function power(x,n)
correctly computes the value of x n . (Assume that the int data type can represent
arbitrarily large integers.) Note that the test “if (n % 2 == 0)” tests whether
n is evenly divisible by 2. That is, the test is true if n is an even number. (This
function is actually a very efficient way to compute x n .)
f 2 , …, defined recursively by
f0 = 0
f1 = 1
f n = f n −1 + f n −2 for n > 1
f2 = f1 + f0 = 0 + 1 = 1
f3 = f2 + f1 = 1 + 1 = 2
f4 = f3 + f2 = 2 + 1 = 3
f5 = f4 + f3 = 3 + 2 = 5
f6 = f5 + f4 = 5 + 3 = 8
f 7 = f 6 + f 5 = 8 + 5 = 13
3.8. Trees 93
and so on. Based on this definition, we can use induction to prove facts
about the Fibonacci sequence. We can prove, for example, that f n grows
exponentially with n, even without finding an exact formula for f n :
n −1
Theorem 3.13. The Fibonacci sequence, f 0 , f 1 , f 2 , …, satisfies f n > 32 , for
n ≥ 6.
Proof. We prove this by induction on n. For n = 6, we have that f n = 8
while 1.5n−1 = 1.55 , which is about 7.6. So f n > 1.5n−1 for n = 6. Similarly,
for n = 7, we have f n = 13 and 1.5n−1 = 1.56 , which is about 11.4. So 3
f n > 1.5n−1 for n = 7.
Now suppose that k is an arbitrary integer with k > 7. Suppose that we
already know that f n > 1.5n−1 for n = k − 1 and for n = k − 2. We want to
show that the inequality then holds for n = k as well. But
f k = f k −1 + f k −2
> 1.5(k−1)−1 + 1.5(k−2)−1 (by the induction hypothesis)
= 1.5k−2 + 1.5k−3
= (1.5) · (1.5k−3 ) + (1.5k−3 )
= (2.5) · (1.5k−3 )
> (1.52 ) · (1.5k−3 ) (since 1.52 = 2.25)
= 1.5k−1
This string of equalities and inequalities shows that f k > 1.5k−1 . This com-
pletes the induction and proves the theorem.
Exercises
†1. Prove that the Fibonacci sequence, f 0 , f 1 , f 2 , …, satisfies f n < 2n for all natural
numbers n ≥ 1.
†2. Suppose that a1 , a2 , a3 , …, is a sequence of numbers which is defined recursively
by a1 = 1 and an = 2an−1 + 2n−1 for n > 1. Prove that an = n2n−1 for every
positive integer n.
3.8 Trees
Recursion is often used with linked data structures. A linked data structure
is a type of data structure constructed by linking several objects of the same
type together with pointers. In this section we will take a look at one of the
most common recursive data structures in computer science: trees.
94 Chapter 3. Proof
7 20
Nodes are represented by circles. The nodes of a tree can contain any
type of data, but we will in this example each node contains an integer. The
node containing 8 is the root of this tree, with 42 and 17 being its children.
Both 42 and 17 are root of subtrees. For example, the subtree rooted in 17
has three nodes: 17 as the root with its children 7 and 20. Nodes with no
children are called leaf nodes. In our example tree, the leaf nodes are 42, 7
and 20.
We will formalise this structure—trees—when we learn about sets and
relations in the next chapter, but for now we will settle for the following
informal definition:
Definition 3.1 (Tree node (informal)). A node in a tree has a) a value, b) a
list of nodes.
There are many more names used in a description of a tree, and some of
these might vary a bit between textbooks. In this book, we use the following
set of terminology. Notice how some of these are recursively defined!
Parent of x The node in whose list of nodes x occurs.
Siblings of x The other nodes in the list of children of x’s parent.
Descendants of x All children of x and the descendants of the children of
x.
Ancestors of x The parent of x and the ancestors of the parent of x.
Leaf A node that has no children.
Root The node that has no parent.
Binary tree A tree where every node has at most 2 children.
3.8. Trees 95
Height The largest number of vertices on a path from a leaf to the root,
excluding the leaf.
Using this terminology, we can now say that the example tree above has
a height of 2, contains 3 leaves, and is a binary tree. Further, the ancestors
of 20 are 17 and 8.
Trees are popular in computer science because they can be used for a large
number of different applications, from sorting, to memory management
and keeping track of family members (though the latter is not strictly speak-
ing a tree: do you see why?). During the course Algorithms & Data Structures
you will encounter many of these applications as well. Trees are a special
type of a more general linked data structure, graphs, which are also very
useful in computer science. We’ll look at graphs in the next chapter.
For now we will only take a look at one example, using trees to repres-
ent mathematical formulae. Consider for example the expression: (8 + 3) ∗
(10/5). We can represent this using a tree, by having every operator be the
root of a subtree, and the leaves of the tree be the different numbers:
+ /
8 3 10 5
Notice how this tree has different types of ‘data’ in its nodes: some are
numbers and some are operators.
You will find that for each of these trees there is exactly one expression
that matches it! For example, the following tree represents the statement:
− ( x+1x)(
+8
y −1)
−2
96 Chapter 3. Proof
− 2
3 + ∗
x 8 + −
x 1 y 1
In the exercises you will practice also with turning statements from pro-
positional and predicate logic into trees.
Á
If you don’t already know about objects and pointers, you will
not be able to follow the rest of this section. Time to read about
it on the internet?
Like any tree, a binary tree consists of nodes linked together in a tree-
like structure. A binary tree can be empty, or it can consist of a node (the
3.8. Trees 97
root of the tree) and two smaller binary trees (called the left subtree and the
right subtree of the tree). You can already see the recursive structure: a tree
can contain smaller trees. Notice that this recursive definition immediately
limits each node to have at most two children.
In the programming language Java, the nodes of a tree can be represen-
ted by objects belonging to this class
1 class BinaryTreeNode {
int item; // An integer value stored in the node.
3 BinaryTreeNode left; // Pointer to left subtree.
BinaryTreeNode right; // Pointer to right subtree. 3
5 }
An empty tree is represented by a pointer that has the special value null.
If root is a pointer to the root node of a tree, then root.left is a pointer to
the left subtree and root.right is a pointer to the right subtree. Of course,
both root.left and root.right can be null if the corresponding subtree is empty.
Similarly, root.item is a name for the integer in the root node.
Let’s say that we want a function that will find the sum of all the integers
in all the nodes of a binary tree. We can do this with a simple recursive
function. The base case of the recursion is an empty tree. Since there are no
integers in an empty tree, the sum of the integers in an empty tree is zero.
For a non-empty tree, we can use recursion to find the sums of the integers
in the left and right subtrees, and then add those sums to the integer in the
root node of the tree. In Java, this can be expressed as follows:
1 int TreeSum( BinaryTreeNode root ) {
// Find the sum of all the integers in the
3 // tree that has the given root.
int answer;
5 if (root == null) { // The tree is empty.
answer = 0;
7 } else {
answer = TreeSum(root.left);
9 answer += TreeSum(root.right);
answer += root.item;
11 }
return answer;
13 }
Theorem 3.14. The function TreeSum, defined above, correctly computes the sum
of all the integers in a binary tree.
Proof. We use induction on the number of nodes in the tree. Let P(n) be the
statement “TreeSum correctly computes the sum of the nodes in any binary
98 Chapter 3. Proof
tree that contains exactly n nodes”. We show that P(n) is true for every
natural number n.
Consider the case n = 0. A tree with zero nodes is empty, and an empty
tree is represented by a null pointer. In this case, the if statement in the
definition of TreeSum assigns the value 0 to the answer, and this is the correct
sum for an empty tree. So, P(0) is true.
Let k be an arbitrary natural number, with k > 0. Suppose we already
know P( x ) for each natural number x with 0 ≤ x < k. That is, TreeSum
3 correctly computes the sum of all the integers in any tree that has fewer
than k nodes. We must show that it follows that P(k ) is true, that is, that
TreeSum works for a tree with k nodes. Suppose that root is a pointer to the
root node of a tree that has a total of k nodes. Since the root node counts as a
node, that leaves a total of k − 1 nodes for the left and right subtrees, so each
subtree must contain fewer than k nodes. By the induction hypothesis, we
know that TreeSum(root.left) correctly computes the sum of all the integers
in the left subtree, and TreeSum(root.right) correctly computes the sum of
all the integers in the right subtree. The sum of all the integers in the tree is
root.item plus the sums of the integers in the subtrees, and this is the value
computed by TreeSum. So, TreeSum does work for a tree with k nodes. This
completes the induction.
Note how closely the structure of the inductive proof follows the struc-
ture of the recursive function. In particular, the second principle of math-
ematical induction is very natural here, since the size of subtree could be
anything up to one less than the size of the complete tree. It would be very
difficult to use the first principle of induction in a proof about binary trees.
Exercises
1. Draw a binary tree so that the root has a total of 8 descendants, and there is node
that has exactly 4 ancestors.
− 2
/ +
8 4 − ∗ 3
3 1 2 1
6. A binary search tree satisfies the following property: If node is a pointer to any
node in the tree, then all the integers in the left subtree of node are less than
node.item and all the integers in the right subtree of node are greater than or equal
to node.item. Prove that the following recursive subroutine prints all the integers
in a binary search tree in non-decreasing order:
void SortPrint(BinaryTreeNode root) {
2 // Assume that root is a pointer to the
// root node of a binary sort tree. This
4 // subroutine prints the integers in the
// tree in non -decreasing order.
6 if (root == null) {
// There is nothing to print.
100 Chapter 3. Proof
8 }
else {
10 SortPrint(root.left);
System.out.println(root.item);
12 SortPrint(root.right);
}
14 }
3
3.9 Invariants
Recursion is closely linked to iteration. In fact, a while loop can be written
as a recursive subroutine (and this how the programming language Pro-
log achieves ‘iteration’: see page 86). In computer science we would like
to prove correctness and other properties about algorithms. Proofs about
algorithms can be more difficult than the proofs about simple properties of
the integers that we often use as examples this book.
A tool that helps us to prove properties about algorithms is an invariant.
For example, a loop invariant is a property P of a loop such that:
2. P remains true after each execution of the body of the loop (but not
necessarily in-between the steps within the body).
1. Initialization or basis property. The invariant holds before the first iter-
ation of the loop.
Try to convince yourself that this code computes: ⌊n/a⌋. Don’t believe
us? We will prove it to you:
Proof. Invariant: r · a + b = n
1. Initialization or basis property. Before the loop runs, b = n and r = 0.
Thus r · a + b = b = n.
2. Maintenance or inductive property. Assume the invariant holds before
iteration k, thus: rold · a + bold = n.
Now we prove that it holds after iteration k, that is: rnew · a + bnew = n.
From line 4 we derive that: bnew = bold − a and rnew = rold + 1. Thus
rnew · a + bnew = (rold + 1) · a + bold − a = rold · a + a + bold − a = rold ·
IH
a + bold = n.
3. Termination and falsity of guard. Every iteration b decreases by a. Since
a > 0, this means that eventually b < a will hold.
4. Correctness of the post-condition. Since 0 ≤ b < a and r · a + b = n, we
know that: r · a ≤ n and n = r · a + b < r · a + a < (r + 1) · a. So we get:
r ≤ n/a and n/a < r + 1, thus n/a − 1 < r ≤ n/a. Since r is integer, this
means: f = ⌊n/a⌋.
102 Chapter 3. Proof
®
There is a form of logic, Floyd–Hoare logic, in which we can ex-
press invariants and can formally prove the (partial) correctness
of a program. Read about it on wikipedia: en.wikipedia.org/
wiki/Loop_invariant.
3
Chapter 4
E DEAL WITH THE COMPLEXITY of the world by putting things into categor-
W ies. There are not just hordes of individual creatures. There are dogs,
cats, elephants, and mice. There are mammals, insects, and fish. Animals,
vegetables, and minerals. Solids, liquids, and gases. Things that are red.
Big cities. Pleasant memories…. Categories build on categories. They are
the subject and the substance of thought.
In mathematics, which operates in its own abstract and rigorous world,
categories are modelled by sets. A set is just a collection of elements. Along
with logic, sets form the ‘foundation’ of mathematics, just as categories are
part of the foundation of day-to-day thought. In this chapter, we study sets
and relationships among sets. And, yes, that means we’ll prove theorems
about sets!
103
104 Chapter 4. Sets, Functions, and Relations
The most basic operation in set theory is forming a set from a given list
of specific entities. The set that is formed in this way is denoted by enclosing
the list of entities between a left brace, ‘{’, and a right brace, ‘}’. The entities
in the list are separated by commas. For example, the set denoted by
is the set that contains the entities 17, π, New York City, King Willem-Alexander,
and Euromast. These entities are the elements of the set. Since we assume
that a set is completely defined by the elements that it contains, the set is
well-defined. Of course, we still haven’t said what it means to be an ‘en-
tity’. Something as definite as ‘New York City’ should qualify, except that it
doesn’t seem like New York City really belongs in the world of mathematics.
The problem is that mathematics is supposed to be its own self-contained
world, but it is supposed to model the real world. When we use mathem-
atics to model the real world, we admit entities such as New York City and
even Euromast. But when we are doing mathematics per se, we’ll generally
stick to obviously mathematical entities such as the integer 17 or the real
number π. We will also use letters such as a and b to refer to entities. For
example, when we say something like “Let A be the set { a, b, c}”, we mean
a, b, and c to be particular, but unspecified, entities.
4.1. Basic Concepts 105
»
It’s important to understand that a set is defined by the ele-
ments that it contains, and not by the order in which those ele-
ments might be listed. For example, the notations { a, b, c, d}
and {b, c, a, d} define the same set. Furthermore, a set can
only contain one copy of a given element, even if the notation
that specifies the set lists the element twice. This means that
{ a, b, a, a, b, c, a} and { a, b, c} specify exactly the same set. Note
in particular that it’s incorrect to say that the set { a, b, a, a, b, c, a}
contains seven elements, since some of the elements in the list are
identical. The notation { a, b, c} can lead to some confusion, since
it might not be clear whether the letters a, b, and c are assumed to
refer to three different entities. A mathematician would generally
not make this assumption without stating it explicitly, so that the
set denoted by { a, b, c} could actually contain either one, two, or 4
three elements. When it is important that different letters refer
to different entities, we will say so explicitly, as in “Consider the
set { a, b, c}, where a, b, and c are distinct.”
b
As you may have noticed by now, it is convention for sets to be
denoted using capital letters (e.g. ‘A’) and elements within sets
to be denoted using lowercase letters (e.g. ‘a’). You should ad-
here to the same convention to prevent misunderstandings!
x ∈ B ).
»
Later, when proving theorems in set theory, we will find it can of-
ten help to use this predicate logic notation to simplify our proofs.
To avoid having to look them up later, make sure that you under-
stand why the predicate logic notation is equivalent to the set
notation.
Suppose now that A and B are sets such that every element of A is an
element of B. In that case, we say that A is a subset of B, i.e. A is a subset
of B if and only if ∀ x ( x ∈ A → x ∈ B). The fact that A is a subset of B is
denoted by A ⊆ B. Note that ∅ is a subset of every set B: x ∈ ∅ is false for
4 any x, and so given any B, ( x ∈ ∅ → x ∈ B) is true for all x.
If A = B, then it is automatically true that A ⊆ B and that B ⊆ A. The
converse is also true: If A ⊆ B and B ⊆ A, then A = B. This follows
from the fact that for any x, the statement ( x ∈ A ↔ x ∈ B) is logically
equivalent to the statement ( x ∈ A → x ∈ B) ∧ ( x ∈ B → x ∈ A). This fact
is important enough to state as a theorem.
Theorem 4.1. Let A and B be sets. Then A = B if and only if both A ⊆ B and
B ⊆ A.
This theorem expresses the following advice: If you want to check that
two sets, A and B, are equal, you can do so in two steps. First check that
every element of A is also an element of B, and then check that every ele-
ment of B is also an element of A.
If A ⊆ B but A ̸= B, we say that A is a proper subset of B. We use the
notation A ⊊ B to mean that A is a proper subset of B. That is, A ⊊ B if
and only if A ⊆ B ∧ A ̸= B. We will sometimes use A ⊇ B as an equivalent
notation for B ⊆ A, and A ⊋ B as an equivalent for B ⊊ A. Other text books
also sometimes use the ⊂ symbol to represent proper subsets, e.g., A ⊂ B ≡
A ⊊ B. Additionally, you may come across A ̸⊆ B which means that A is not
a subset of B. Notice that (especially in written text) the difference between
A ⊊ B and A ̸⊆ B can be small, so make sure to read properly and to write
clearly!
specified as
N = {0, 1, 2, 3, . . . }
However, this is an informal notation, which is not really well-defined, and
it should only be used in cases where it is clear what it means. It’s not very
useful to say that “the set of prime numbers is {2, 3, 5, 7, 11, 13, . . . }”, and it
is completely meaningless to talk about “the set {17, 42, 105, . . . }”. Clearly,
we need another way to specify sets besides listing their elements. The need
is fulfilled by predicates.
If P( x ) is a predicate, then we can form the set that contains all entities a
such that a is in the domain of discourse for P and P( a) is true. The notation
{ x | P( x )} is used to denote this set. This is the intensional definition of the
set. The name of the variable, x, is arbitrary, so the same set could equally
well be denoted as {z | P(z)} or {r | P(r )}. The notation { x | P( x )} can be 4
read “the set of x such that P( x )”. We call this the set-builder notation, as
you can think of the predicate as a building material for the elements of the
set. For example, if E( x ) is the predicate ‘x is an even number’, and if the
domain of discourse for E is the set N, then the notation { x | E( x )} specifies
the set of even natural numbers. That is,
{ x | E( x )} = {0, 2, 4, 6, 8, . . . }
®
It turns out, for deep and surprising reasons that we will discuss
later, that we have to be a little careful about what counts as a
predicate. In order for the notation { x | P( x )} to be valid, we
have to assume that the domain of discourse of P is in fact a set.
(You might wonder how it could be anything else. That’s the
surprise!)
{ x ∈ X | P( x )}
is the set that consists of all entities a that are members of the set X and for
which P( a) is true. In this notation, we don’t have to assume that the domain
of discourse for P is a set, since we are effectively limiting the domain of
discourse to the set X. The set denoted by { x ∈ X | P( x )} could also be
written as { x | x ∈ X ∧ P( x )}.
108 Chapter 4. Sets, Functions, and Relations
Ď
We can use this notation to define the set of prime numbers in
a rigorous way. A prime number is a natural number n which
is greater than 1 and which satisfies the property that for any
factorization n = xy, where x and y are natural numbers, either
x or y must be n. We can express this definition as a predicate
and define the set of prime numbers as
{ n ∈ N | ( n > 1) ∧
∀ x ∀y ( x ∈ N ∧ y ∈ N ∧ n = xy) → ( x = n ∨ y = n) }
A ∪ B = { a, b, c, d} A ∩ B = {b} A ∖ B = { a, c}
A ∪ C = { a, b, c, d, e, f } A∩C = ∅ A ∖ C = { a, b, c}
Ď
let L( x ) be the predicate ‘x is lucky’, and let W ( x ) be the predicate
‘x is wise’, where the domain of discourse for each predicate is
the set of people. Let X = { x | L( x )}, and let Y = { x | W ( x )}.
Then
Á
You have to be a little careful with the English word ‘and’. We
might say that the set X ∪ Y contains people who are lucky and
people who are wise. But what this means is that a person gets
into the set X ∪ Y either by being lucky or by being wise, so X ∪ Y
is defined using the logical ‘or’ operator, ∨.
Notation Definition
plane, and sets as regions inside closed curves. So a Venn diagram consists
of multiple overlapping closed curves, usually circles, each representing a
set. The points inside a curve (circle) labelled S represent elements of the
set S, while points outside the boundary represent elements not in the set
S. Figure 4.1 shows our example set which opened the section.
Venn diagrams help us to visualise sets and set operations. For example,
the set of all elements that are members of both sets S and T, S ∩ T, is repres-
ented visually by the area of overlap of the regions S and T: see Figure 4.3.
In Venn diagrams the curves are overlapped in every possible way, show-
ing all possible relations between the sets. You can find it useful to draw a
Venn diagram to gain intuition of what’s happening. On their own, Venn
diagrams do not offer a proof for theorems in set theory however.
the set {b} is a member of the set { a, {b}}, we have that {b} ∈ { a, {b}}.
4
On the other hand, provided that a ̸= b, the statement {b} ⊆ { a, {b}} is
false, since saying {b} ⊆ { a, {b}} is equivalent to saying that b ∈ { a, {b}},
and the entity b is not one of the two members of { a, {b}}. For the entity
a, it is true that { a} ⊆ { a, {b}} and for the set {b}, it is true that {{b}} ⊆
{ a, {b}}. Study these examples carefully before you continue, as many stu-
dents struggle with the notion and notation of putting sets in sets.
Given a set A, we can construct the set that contains all the subsets of
A. This set is called the power set of A, and is denoted P( A). Formally, we
define
P( A ) = { X | X ⊆ A }.
P( A) = { ∅, { a}, {b}, { a, b} }.
So the power set of of A has four elements. Does this surprise
you?
Note that since the empty set is a subset of any set, the empty set is an
element of the power set of any set. That is, for any set A, ∅ ⊆ A and ∅ ∈
P( A). Since the empty set is a subset of itself, and is its only subset, we
have that P(∅) = {∅}. The set {∅} is not empty. It contains one element,
namely ∅.
112 Chapter 4. Sets, Functions, and Relations
this condition means that R ̸∈ R. That is, from the fact that R ∈ R, we
deduce the contradiction that R ̸∈ R.
Now consider the remaining case, where R ̸∈ R. Since R ̸∈ R, R does
not satisfy the condition for membership in R. Since the condition for mem-
bership is that R ̸∈ R, and this condition is false, the statement R ̸∈ R must
be false. But this means that the statement R ∈ R is true. From the fact that
R ̸∈ R, we deduce the contradiction that R ∈ R.
Since both possible cases, R ∈ R and R ̸∈ R, lead to contradictions, we
see that it is not possible for R to exist. Since the existence of R follows from
the existence of V, we see that V also cannot exist.
b
This (in)famous contradiction has been adapted to natural lan-
guage to make it easier to convey the problem to laymen. Unfor- 4
tunately many of these translations are flawed. Can you think of
a solution for the following for instance? “The barber of Seville
shaves all men who do not shave themselves. Who shaves the
barber?”
b
In high school you would also have to write (x,y)-coordinates
using this ordered pair notation. For instance you would say that
the line y = ax + b intersects the y-axis at (0, b) and the x-axis at
b
(− , 0).
a
You can extend this concept to more than just pairs. With three ele-
ments we can create ordered triples ( a, b, c). The definition for four or more
coordinates is similar. The general term for such an ordered collection is
4 tuple (recall page 48) or, more specifically, ordered n-tuple. For example,
( a, b, c, d, e) is an ordered 5-tuple.
A × B = {( a, b) | a ∈ A and b ∈ B}.
This set is called the cross product or Cartesian product of the sets A and B.
The set A × B contains every ordered pair whose first coordinate is an ele-
ment of A and whose second coordinate is an element of B. For example, if
X = {c, d} and Y = {1, 2, 3}, then X × Y = {(c, 1), (c, 2), (c, 3), (d, 1), (d, 2), (d, 3)}.
It is possible to extend this idea to the cross product of more than two sets.
The cross product of the three sets A, B, and C is denoted A × B × C and
produced ordered triples ( a, b, c) where a ∈ A, b ∈ B, c ∈ C. Another ex-
ample can be found in the homework duos you have formed for this course.
Each of these pairs of students is a 2-tuple, from the set S × S where S is the
set of students currently taking Reasoning & Logic.
Proof. Suppose that P is a predicate that satisfies the hypotheses of the the-
orem, and suppose that the statement ∀n ∈ N, P(n) is false. We show that
this assumption leads to a contradiction.
Let X = {n ∈ N | ¬ P(n)}. Because of the assumption that ∀n ∈ N, P(n)
is false, X is non-empty. It follows that X has a smallest element. Let x be
the smallest element of X. The assumption that P(0) is true means that
0 ̸∈ X, so we must have x > 0. Since x is the smallest natural number
for which P( x ) is false, we know that P(0), P(1), …, and P( x − 1) are all
true. From this and the fact that ( P(0) ∧ P(1) ∧ · · · ∧ P( x − 1)) → P( x ), we
deduce that P( x ) is true. But this contradicts the fact that P( x ) is false. This
contradiction proves the theorem.
Basis: 0 ∈ N.
Succession: x ∈ N → x + 1 ∈ N.
4 Exclusivity: No other elements other than those outlined by the rules above
are in N.
This definition is similar to one we have seen before, first stating that
0 ∈ N and then saying that we can add 1 to an element in N to get another
element of N. The final clause is needed to ensure that other items are not
part of N. Without it, you and me, as well as π, ‘New York City’, and ‘King
Willem-Alexander’ might have been in the set. After all there was no reason
for those elements not to be in there.
Now compare that recursive definition, with the method for mathemat-
ical induction we have seen before:
Using this definition of the set PROP we can use structural induction to
prove certain claims about PROP. For instance we can prove that every for-
mula in PROP has equally many left parentheses ‘(’ and right parentheses
‘)’.
Proof. Let l (ϕ) denote the number of left parentheses in a formula ϕ. Simil-
arly let r (ϕ) denote the number of right parentheses. Let P(ϕ) be the state-
ment that l (ϕ) = r (ϕ). We need to prove that ∀ϕ ∈ PROP( P(ϕ)).
Base case: Consider the Atoms rule of the definition of PROP: l ( pi ) =
0 = r ( pi ). Therefore P( pi ) holds.
Inductive case: We want to show that if the statement is true for x, y ∈
PROP (where x and y are arbitrary formula), then it is true for ¬ x and
( x ∗ y) for all ∗ ∈ {∨, ∧, →, ↔}. That is, we must prove the implication
( P( x ) ∧ P(y)) → ( P(¬ x ) ∧ P(( x ∗ y))). So we assume P( x ) ∧ P(y), that is, 4
we assume that for both formula x and y: l ( x ) = r ( x ) and l (y) = r (y). We
want to prove P(¬ x ), that is, that for ¬ x l (¬ x ) = r (¬ x )
Secondly we prove that P(( x ∗ y)) holds for all ∗ ∈ {∨, ∧, →, ↔}:
Altogether, we have shown that P( pi ) holds and that, for all x, y ∈ PROP
and ∗ ∈ {∨, ∧, →, ↔}, ( P( x ) ∧ P(y)) → ( P(¬ x ) ∧ P(( x ∗ y)) is true. There-
fore, by the principle of structural induction, P(ϕ) is true for all ϕ ∈ PROP,
so for all propositional formula the number of left parentheses equals the
number of right parentheses. This completes the proof by structural induc-
tion.
(∗, ((+, (8, ∅), (3, ∅)), (/, (10, ∅), (5, ∅))))
4
You can probably see now why we often choose to represent trees visually!
∗
+ /
8 3 10 5
Note that we could alternatively have represented our leaf node 8 as
(8, (∅)). After all, one could argue this also describe a node without any
children containing values. However, this description describes a different
tree. After all it now says that the node 8 has a child (and thus is an internal
node), but that this child has no value (it is an empty tree). To visualise
this, other authors sometimes use squares as such:
∗
+ /
8 3 10 5
2 This set of rules also allows us to make invalid parse trees! For example where a leaf node
has a ∗ value or where operators have a wrong number of operands. Creating ‘proper’ parse
trees is commonly done with grammars which you will study in Automata, Computability, and
Complexity.
4.1. Basic Concepts 119
Notice also how we can now ‘easily’ define binary trees by simply lim-
iting k = 2 in the Internal Nodes rule. For example (8, (3, (∅, (2, ∅))), ∅)
would be a binary tree, represented like this:
8
2
But have you spotted an unfortunate side effect of this definition? In our
visualisation, both our squares and our node containing 2 are considered
leaves. After all they have no children! As a result, some books instead use 4
the following definition for binary trees. This removes the ambiguity on the
definition of a leaf, but as a downside does not allow any leaf to have a value.
It is a nice example of one of the many trade-offs in computer science.
Leaf Nodes ∅ ∈ BTREE if x ∈ D
Internal Nodes ( x, ( T1 , T2 )) ∈ BTREE if x ∈ D ∧ T1 , T2 ∈ BTREE
Exclusivity Nothing else is in BTREE
Now that we have formalised our definition of binary trees, we can also
start proving interesting properties about them. One such property is that
the number of leaves of a binary tree n L ≤ 2h where h is the height of the
tree. A proof for this claim can now follow our structural induction format
as outlined in the previous section:
Proof. Base case (Empty Tree): consider the Empty Tree rule of the defin-
ition of TREE. The empty tree has no nodes, so a height of 0 and also no
leaves. Hence n L = 0 ≤ 20 = 1 holds.
Base case (Leaf Nodes): consider the Leaf Nodes rule of the definition
of TREE. A leaf node has a height of 0 as longest path from a leaf to the root
excluding the leaf has no nodes (there are no nodes except the leaf!), hence
n L = 1 ≤ 20 = 1 holds.
Exercises
4 †1. If we don’t make the assumption that a, b, and c are distinct, then the set de-
noted by { a, b, c} might actually contain either 1, 2, or 3 elements. How many
different elements might the set { a, b, { a}, { a, c}, { a, b, c} } contain? Explain
your answer.
†2. Compute A ∪ B, A ∩ B, and A ∖ B for each of the following pairs of sets
a) A = { a, b, c}, B = ∅
b) A = {1, 2, 3, 4, 5}, B = {2, 4, 6, 8, 10}
c) A = { a, b}, B = { a, b, c, d}
d) A = { a, b, { a, b}}, B = {{ a}, { a, b}}
†3. Draw a Venn diagram for each of the four sets of the last exercise.
†4. Recall that N represents the set of natural numbers. That is, N = {0, 1, 2, 3, . . . }.
Let X = {n ∈ N | n ≥ 5}, let Y = {n ∈ N | n ≤ 10}, and let Z = {n ∈
N | n is an even number}. Find each of the following sets:
a) X ∩ Y b) X ∪ Y c) X ∖ Y d) N ∖ Z
e) X ∩ Z f) Y ∩ Z g) Y ∪ Z h) Z ∖ N
†5. Find P {1, 2, 3} . (Hint: It has eight elements.)
†6. Assume that a and b are entities and that a ̸= b. Let A and B be the sets defined
by A = { a, {b}, { a, b} } and B = { a, b, { a, {b}} }. Determine whether each of
the following statements is true or false. Explain your answers.
a) b ∈ A b) { a, b} ⊆ A c) { a, b} ⊆ B
d) { a, b} ∈ B e) { a, {b}} ∈ A f) { a, {b}} ∈ B
†7. Since P( A) is a set, it is possible to form the set P P( A) . What is P P(∅) ?
What is P P({ a, b}) ? (Hint: It has sixteen elements.)
†8. In the English sentence, “She likes dogs that are small, cuddly, and cute”, does
she like an intersection or a union of sets of dogs? How about in the sentence,
“She likes dogs that are small, dogs that are cuddly, and dogs that are cute”?
†9. If A is any set, what can you say about A ∪ A ? About A ∩ A ? About A ∖ A ?
Why?
†10. Suppose that A and B are sets such that A ⊆ B. What can you say about A ∪ B ?
About A ∩ B ? About A ∖ B ? Why?
4.2. The Boolean Algebra of Sets 121
†11. Suppose that A, B, and C are sets. Show that C ⊆ A ∩ B if and only if (C ⊆
A ) ∧ ( C ⊆ B ).
†12. Suppose that A, B, and C are sets, and that A ⊆ B and B ⊆ C. Show that A ⊆ C.
†13. Suppose that A and B are sets such that A ⊆ B. Is it necessarily true that P( A) ⊆
P( B) ? Why or why not?
†14. Let M be any natural number, and let P(n) be a predicate whose domain of
discourse includes all natural numbers greater than or equal to M. Suppose
that P( M) is true, and suppose that P(k) → P(k + 1) for all k ≥ M. Show that
P(n) is true for all n ≥ M.
†15. Prove that the number of propositional variables is always at most one more
than the number of connectives for every formula ϕ ∈ PROP.
16. A ternary tree is a tree in which every node has at most three children. Give
a formal definition a ternary trees, and prove a theorem about the number of
nodes in a ternary tree.
4
A ∪ ( B ∩ C ) = ( A ∪ B) ∩ ( A ∪ C )
122 Chapter 4. Sets, Functions, and Relations
and
A ∩ ( B ∪ C ) = ( A ∩ B) ∪ ( A ∩ C )
These rules are called the distributive laws for set theory. To verify the first
of these laws, we just have to note that for any x,
x ∈ A ∪ (B ∩ C)
↔ ( x ∈ A) ∨ (( x ∈ B) ∧ ( x ∈ C )) (definition of ∪, ∩)
↔ (( x ∈ A) ∨ ( x ∈ B)) ∧ (( x ∈ A) ∨ ( x ∈ C )) (distributivity of ∨)
↔ ( x ∈ A ∪ B) ∧ ( x ∈ A ∪ C ) (definition of ∪)
↔ x ∈ (( A ∪ B) ∩ ( A ∪ C )) (definition of ∩)
4
The second distributive law for sets follows in exactly the same way.
Definition 4.1. Let U be a given universal set, and let A be any subset of
U. We define the complement of A in U to be the set A that is defined by
A = { x ∈ U | x ̸ ∈ A }.
A ∩ A = { x ∈ U | ( x ∈ A) ∧ ( x ∈ A)} (definition of ∩)
= { x ∈ U | ( x ∈ A) ∧ ( x ̸∈ A)} (definition of complement)
= { x ∈ U | ( x ∈ A) ∧ ¬( x ∈ A)} (definition of ̸∈)
=∅
A∪B = A∩B
and
A∩B = A∪B
For example, we can verify the first of these laws with the calculation
A ∪ B ∪ C = ( A ∪ B) ∪ C
= ( A ∪ B) ∩ C (by DeMorgan’s Law for two sets)
= ( A ∩ B) ∩ C (by DeMorgan’s Law for two sets)
= A∩B∩C
124 Chapter 4. Sets, Functions, and Relations
From there, we can derive similar laws for four sets, five sets, and so on.
However, just saying ‘and so on’ is not a rigorous proof of this fact. Whereas
we may have excused ourselves about that in Chapter 2, we can now prove
this fact. Here is a rigorous inductive proof of a generalized DeMorgan’s
Law:
Theorem 4.5. For any natural number n ≥ 2 and for any sets X1 , X2 , …, Xn ,
X1 ∪ X2 ∪ · · · ∪ X n = X1 ∩ X2 ∩ · · · ∩ X n
Proof. We give a proof by induction. In the base case, n = 2, the statement
is that X1 ∪ X2 = X1 ∩ X2 . This is true since it is just an application of
DeMorgan’s law for two sets.
For the inductive case, suppose that the statement is true for n = k. We
want to show that it is true for n = k + 1. Let X1 , X2 , …, Xk+1 be any k + 1
4.2. The Boolean Algebra of Sets 125
X1 ∪ X2 ∪ · · · ∪ X k + 1 = ( X1 ∪ X2 ∪ · · · ∪ X k ) ∪ X k + 1
= ( X1 ∪ X2 ∪ · · · ∪ X k ) ∩ X k + 1
= ( X1 ∩ X2 ∩ · · · ∩ X k ) ∩ X k + 1
= X1 ∩ X2 ∩ · · · ∩ X k + 1
In this computation, the second step follows by DeMorgan’s Law for two
sets, while the third step follows from the induction hypothesis. Therefore
by the principle of induction we have proven the theorem.
T U
F ∅
p∧q A∩B
p∨q A∪B
¬p A
∅c
true
A↔A
Ac ∪ Bc A∪B
¬ A ∨ ¬B A∨B
A → ¬B ¬B → A
A ∪ Bc A∆Bc Ac ∪ B
A ∨ ¬B A⊕B ¬A ∨ B
B→A A ↔ ¬B A→B
Bc A Ac B
¬B A ¬A B
A∩ Bc ( A∆B)c Ac ∩B
A ∧ ¬B A↔B ¬A ∧ B
Ac ∩ Bc A∩B
¬ A ∧ ¬B A∧B
false
A ↔ ¬A
used in such expressions to specify the order in which the operations are
to be performed. In the absence of parentheses, we need precedence rules
to determine the order of operation. The precedence rules for the Boolean
algebra of sets are carried over directly from the Boolean algebra of proposi-
tions. When union and intersection are used together without parentheses,
intersection has precedence over union. Furthermore, when several oper-
ators of the same type are used without parentheses, then they are evalu-
ated in order from left to right. (Of course, since ∪ and ∩ are both asso-
ciative operations, it really doesn’t matter whether the order of evaluation
is left-to-right or right-to-left.) For example, A ∪ B ∩ C ∪ D is evaluated as
( A ∪ ( B ∩ C )) ∪ D. The complement operation is a special case. Since it is
denoted by drawing a line over its operand, there is never any ambiguity
about which part of a formula it applies to.
4
Á
Unfortunately in hand-written work this is not always true.
Make sure to write neatly and unambiguously when working
with complements. Also note that, similarly to parentheses in
propositional logic, although parentheses may not be necessary
I strongly encourage you to add them them to improve readabil-
ity of the order of operations.
The laws of set theory can be used to simplify complex expressions in-
volving sets. (As usual, of course, the meaning of ‘simplification’ is partly
in the eye of the beholder.) For example, for any sets X and Y,
( X ∪ Y ) ∩ (Y ∪ X ) = ( X ∪ Y ) ∩ ( X ∪ Y ) (Commutative Law)
= (X ∪ Y) (Idempotent Law)
where in the second step, the Idempotent Law, which says that A ∩ A =
A, is applied with A = X ∪ Y. For expressions that use the complement
operation, it is usually considered to be simpler to apply the operation to an
individual set, as in A, rather than to a formula, as in A ∩ B. DeMorgan’s
Laws can always be used to simplify an expression in which the complement
operation is applied to a formula. For example,
A ∩ B ∪ A = A ∩ ( B ∩ A) (DeMorgan’s Law)
= A ∩ ( B ∩ A) (Double Complement)
= A ∩ ( A ∩ B) (Commutative Law)
= ( A ∩ A) ∩ B) (Associative Law)
= A∩B (Idempotent Law)
128 Chapter 4. Sets, Functions, and Relations
As a final example of the relationship between set theory and logic, con-
sider the set-theoretical expression A ∩ ( A ∪ B) and the corresponding com-
pound proposition p ∧ ( p ∨ q). (These correspond since for any x, x ∈
A ∩ ( A ∪ B) ≡ ( x ∈ A) ∧ (( x ∈ A) ∨ ( x ∈ B)).) You might find it in-
tuitively clear that A ∩ ( A ∪ B) = A. Formally, this follows from the fact
that p ∧ ( p ∨ q) ≡ p, which might be less intuitively clear and is surprising
difficult to prove algebraically from the laws of logic. However, there is an-
other way to check that a logical equivalence is valid: Make a truth table.
Consider a truth table for p ∧ ( p ∨ q):
p q p∨q p ∧ ( p ∨ q)
0 0 0 0
4 0 1 1 0
1 0 1 1
1 1 1 1
The fact that the first column and the last column of this table are identical
shows that p ∧ ( p ∨ q) ≡ p. Taking p to be the proposition x ∈ A and q to
be the proposition x ∈ B, it follows that the sets A and A ∩ ( A ∪ B) have the
same members and therefore are equal.
Exercises
†1. Use the laws of logic to verify the associative laws for union and intersection.
That is, show that if A, B, and C are sets, then A ∪ ( B ∪ C ) = ( A ∪ B) ∪ C and
A ∩ ( B ∩ C ) = ( A ∩ B) ∩ C.
†2. Show that for any sets A and B, A ⊆ A ∪ B and A ∩ B ⊆ A.
†3. Recall that the symbol ⊕ denotes the logical exclusive or operation. If A and
B sets, define the set A △ B by A △ B = { x | ( x ∈ A) ⊕ ( x ∈ B)}. Show that
A △ B = ( A ∖ B) ∪ ( B ∖ A). (A △ B is known as the symmetric difference of A
and B.)
†4. Choose three non-empty sets A, B, C. Draw a Venn diagram of A △ B △ C.
†5. Let A be a subset of some given universal set U. Verify that A = A and that
A ∪ A = U.
4.3. Application: Graphs 129
†6. Verify the second of DeMorgan’s Laws for sets, A ∩ B = A ∪ B. For each step in
your verification, state why that step is valid.
†7. The subset operator, ⊆, is defined in terms of the logical implication operator, →.
However, ⊆ differs from the ∩ and ∪ operators in that A ∩ B and A ∪ B are sets,
while A ⊆ B is a statement. So the relationship between ⊆ and → isn’t quite the
same as the relationship between ∪ and ∨ or between ∩ and ∧. Nevertheless, ⊆
and → do share some similar properties. This problem shows one example.
a) Show that the following three compound propositions are logically equi-
valent: p → q, ( p ∧ q) ↔ p, and ( p ∨ q) ↔ q.
b) Show that for any sets A and B, the following three statements are equi-
valent: A ⊆ B, A ∩ B = A, and A ∪ B = B.
†8. DeMorgan’s Laws apply to subsets of some given universal set U. Show that for
a subset X of U, X = U ∖ X. It follows that DeMorgan’s Laws can be written as
U ∖ ( A ∪ B) = (U ∖ A) ∩ (U ∖ B) and U ∖ ( A ∩ B) = (U ∖ A) ∪ (U ∖ B). Show 4
that these laws hold whether or not A and B are subsets of U. That is, show that
for any sets A, B, and C, C ∖ ( A ∪ B) = (C ∖ A) ∩ (C ∖ B) and C ∖ ( A ∩ B) =
( C ∖ A ) ∪ ( C ∖ B ).
†9. Show that A ∪ ( A ∩ B) = A for any sets A and B.
†10. Let X and Y be sets. Simplify each of the following expressions. Justify each step
in the simplification with one of the rules of set theory.
a) X ∪ (Y ∪ X ) b) ( X ∩ Y ) ∩ X
c) ( X ∪ Y ) ∩ Y d) ( X ∪ Y ) ∪ ( X ∩ Y )
†11. Let A, B, and C be sets. Simplify each of the following expressions. In your
answer, the complement operator should only be applied to the individual sets
A, B, and C.
a) A ∪ B ∪ C b) A ∪ B ∩ C c) A ∪ B
d) B ∩ C e) A ∩ B ∩ C f) A ∩ A ∪ B
†12. Use induction to prove the following generalized DeMorgan’s Law for set theory:
For any natural number n ≥ 2 and for any sets X1 , X2 , …, Xn ,
X1 ∩ X2 ∩ · · · ∩ X n = X1 ∪ X2 ∪ · · · ∪ X n
†13. State and prove generalized distributive laws for set theory.
4
s a
This graph can also be expressed more formally as:
s a
This graph can be expressed as a 2-tuple as:
Exercises
1. Draw an undirected graph of 10 vertices, so that the longest path in the graph
consists of 6 edges.
2. Draw a directed acyclic graph of 8 vertices, and 13 edges.
3. Give a topological ordering of the DAG you drew in the previous exercise.
4. Prove the following claim: “Every DAG has at least one vertex without incoming
edges.” You may find a proof by contradiction to be useful here.
132 Chapter 4. Sets, Functions, and Relations
»
In this section we go into detail about representing and comput-
ing with sets. You won’t be examined on this in Reasoning & Lo-
gic. You will find that there is again quite some overlap with your
study materials from Computer Organisation however, as we dis-
cuss different numeric systems.
4
4.4.1 Representing sets
A set is determined by its elements. Given a set A and an entity x, the funda-
mental question is, does x belong to A or not? If we know the answer to this
question for each possible x, then we know the set. For a given x, the answer
to the question, “Is x a member of A”, is either yes or no. The answer can be
encoded by letting 1 stand for yes and 0 stand for no. The answer, then, is
a single bit, that is, a value that can be either zero or one. To represent the
set A as a string of zeros and ones, we could use one bit for each possible
member of A. If a possible member x is in the set, then the corresponding
bit has the value one. If x is not in the set, then the corresponding bit has
the value zero.
Now, in cases where the number of possible elements of the set is very
large or infinite, it is not practical to represent the set in this way. It would re-
quire too many bits, perhaps an infinite number. In such cases, some other
representation for the set can be used. However, suppose we are only inter-
ested in subsets of some specified small set. Since this set plays the role of a
universal set, let’s call it U. To represent a subset of U, we just need one bit
for each member of U. If the number of members of U is n, then a subset of
U is represented by a string of n zeros and ones. Furthermore, every string
of n zeros and ones determines a subset of U, namely that subset that con-
tains exactly the elements of U that correspond to ones in the string. You
know by now from Computer Organisation that a string of n zeros and ones
is called an n-bit binary number. So, we see that if U is a set with n elements,
then the subsets of U correspond to n-bit binary numbers.
To make things more definite, let U be the set {0, 1, 2, . . . , 31}. This set
consists of the 32 integers between 0 and 31, inclusive. Then each subset of U
can be represented by a 32-bit binary number. We use 32 bits because most
computer languages can work directly with 32-bit numbers. For example,
4.4. Application: Programming with Sets * 133
the programming languages Java has a data type named int. A value of
type int is a 32-bit binary number.3 Before we get a definite correspondence
between subsets of U and 32-bit numbers, we have to decide which bit in
the number will correspond to each member of U. Following tradition, we
assume that the bits are numbered from right to left. That is, the rightmost
bit corresponds to the element 0 in U, the second bit from the right corres-
ponds to 1, the third bit from the right to 2, and so on. For example, the
32-bit number
1000000000000000000001001110110
corresponds to the subset {1, 2, 4, 5, 6, 9, 31}. Since the leftmost bit of the
number is 1, the number 31 is in the set; since the next bit is 0, the number
30 is not in the set; and so on.
From now on, I will write binary numbers with a subscript of 2 to avoid
confusion with ordinary numbers. Further, we will often leave out leading
4
zeros. For example, 11012 is the binary number that would be written out
in full as
00000000000000000000000000001101
and which corresponds to the set {0, 2, 3}. On the other hand 1101 repres-
ents the ordinary number one thousand one hundred and one.
Even with this notation, it can be very annoying to write out long bin-
ary numbers—and almost impossible to read them. So binary numbers are
never written out as sequences of zeros and ones in computer programs.
An alternative is to use hexadecimal numbers. Hexadecimal numbers are
written using the sixteen symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E,
and F. These symbols are knows as the hexadecimal digits. Each hexa-
decimal digit corresponds to a 4-bit binary number, as shown in Figure 4.6.
To represent a longer binary number, several hexadecimal digits can be
strung together. For example, the hexadecimal number C7 represents the
binary number 110001112 . In Java and many related languages, a hexa-
decimal number is written with the prefix 0x. Thus, the hexadecimal num-
ber C7 would appear in the program as 0xC7. We’ll follow the same con-
vention here. Any 32-bit binary number can be written using eight hexa-
decimal digits (or fewer if leading zeros are omitted). Thus, subsets of
{0, 1, 2, . . . , 31} correspond to 8-digit hexadecimal numbers. For example,
the subset {1, 2, 4, 5, 6, 9, 31} corresponds to 0x80000276, which represents
the binary number 10000000000000000000010011101102 . Similarly, 0xFF cor-
responds to {0, 1, 2, 3, 4, 5, 6, 7} and 0x1101 corresponds to the binary num-
ber 00010001000000012 and to the set {0, 8, 12}.
Now, if you have worked with binary numbers or with hexadecimal
numbers, you know that they have another, more common interpretation.
3 If in a future version of Java, a value of type int is, for instance, a 64-bit number—which
can be used to represent a subset of the set {0, 1, 2, . . . , 63}—the principle remains the same.
134 Chapter 4. Sets, Functions, and Relations
0 00002 8 10002
1 00012 9 10012
2 00102 A 10102
3 00112 B 10112
4 01002 C 11002
5 01012 D 11012
6 01102 E 11102
7 01112 F 11112
4 Figure 4.6: The 16 hexadecimal digits and the corresponding binary num-
bers. Each hexadecimal digit corresponds to a 4-bit binary number. Longer
binary numbers can be written using two or more hexadecimal digits. For
example, 1010000111112 = 0xA1F.
They represent ordinary integers. Just as 342 represents the integer 3 · 102 +
4 · 101 + 2 · 100 , the binary number 11012 represents the integer 1 · 23 +
1 · 22 + 0 · 21 + 1 · 20 , or 13. When used in this way, binary numbers are
known as base-2 numbers, just as ordinary numbers are base-10 numbers.
Hexadecimal numbers can be interpreted as base-16 numbers. For example,
0x3C7 represents the integer 3 · 162 + 12 · 161 + 7 · 160 , or 874. So, does 11012
really represent the integer 13, or does it represent the set {0, 2, 3} ? The
answer is that to a person, 11012 can represent either. Both are valid inter-
pretations, and the only real question is which interpretation is useful in a
given circumstance. On the other hand, to the computer, 11012 doesn’t rep-
resent anything. It’s just a string of bits, and the computer manipulates the
bits according to its program, without regard to their interpretation.
Of course, we still have to answer the question of whether it is ever useful
to interpret strings of bits in a computer as representing sets.
tersection, and complement are written as | , &, and ~. For example, if x and
y are 32-bit integers representing two subsets, X and Y, of {0, 1, 2, . . . , 31},
then x | y is a 32-bit integer that represents the set X ∪ Y. Similarly, x & y
represents the set X ∩ Y, and ~x represents the complement, X.
The operators | , &, and ~ are called bitwise logical operators because of
the way they operate on the individual bits of the numbers to which they are
applied. If 0 and 1 are interpreted as the logical values false and true, then
the bitwise logical operators perform the logical operations ∨, ∧, and ¬ on
individual bits. To see why this is true, let’s look at the computations that
these operators have to perform.
Let k be one of the members of {0, 1, 2, . . . , 31}. In the binary numbers
x, y, x | y, x & y, and ~x, the number k corresponds to the bit in position k.
That is, k is in the set represented by a binary number if and only if the bit
in position k in that binary number is 1. Considered as sets, x & y is the 4
intersection of x and y, so k is a member of the set represented by x & y if
and only if k is a member of both of the sets represented by x and y. That
is, bit k is 1 in the binary number x & y if and only if bit k is 1 in x and bit
k is 1 in y. When we interpret 1 as true and 0 as false, we see that bit k of
x & y is computed by applying the logical ‘and’ operator, ∧, to bit k of x
and bit k of y. Similarly, bit k of x | y is computed by applying the logical
‘or’ operator, ∨, to bit k of x and bit k of y. And bit k of ~x is computed
by applying the logical ‘not’ operator, ¬, to bit k of x. In each case, the
logical operator is applied to each bit position separately. (Of course, this
discussion is just a translation to the language of bits of the definitions of the
set operations in terms of logical operators: A ∩ B = { x | x ∈ A ∧ x ∈ B},
A ∪ B = { x | x ∈ A ∨ x ∈ B}, and A = { x ∈ U | ¬( x ∈ A)}.)
For example, consider the binary numbers 10110102 and 101112 , which
represent the sets {1, 3, 4, 6} and {0, 1, 2, 4}. Then 10110102 & 101112 is
100102 . This binary number represents the set {1, 4}, which is the inter-
section {1, 3, 4, 6} ∩ {0, 1, 2, 4}. It’s easier to see what’s going on if we write
out the computation in columns, the way you probably first learned to do
addition:
1011010 { 6, 4, 3, 1 }
& 0010111 { 4, 2, 1, 0 }
0010010 { 4, 1 }
Note that in each column in the binary numbers, the bit in the bottom row is
computed as the logical ‘and’ of the two bits that lie above it in the column.
We have written out the sets that correspond to the binary numbers to show
how the bits in the numbers correspond to the presence or absence of ele-
ments in the sets. Similarly, we can see how the union of two sets is com-
puted as a bitwise ‘or’ of the corresponding binary numbers.
136 Chapter 4. Sets, Functions, and Relations
1011010 { 6, 4, 3, 1 }
| 0010111 { 4, 2, 1, 0 }
1011111 { 6, 4, 3, 2, 1, 0 }
The complement of a set is computed using a bitwise ‘not’ operation. Since
we are working with 32-bit binary numbers, the complement is taken with
respect to the universal set {0, 1, 2, . . . , 31}. So, for example,
~10110102 = 111111111111111111111111101001012
Of course, we can apply the operators &, | , and ~ to numbers written in
hexadecimal form, or even in ordinary, base-10 form. When doing such
calculations by hand, it is probably best to translate the numbers into binary
form. For example,
4
0xAB7 & 0x168E = 1010 1011 01112 & 1 0110 1000 11102
= 0 0010 1000 01102
= 0x286
When computing with sets, it is sometimes necessary to work with in-
dividual elements. Typical operations include adding an element to a set,
removing an element from a set, and testing whether an element is in a set.
However, instead of working with an element itself, it’s convenient to work
with the set that contains that element as its only member. For example,
testing whether 5 ∈ A is the same as testing whether {5} ∩ A ̸= ∅. The
set {5} is represented by the binary number 1000002 or by the hexadecimal
number 0x20. Suppose that the set A is represented by the number x. Then,
testing whether 5 ∈ A is equivalent to testing whether 0x20 & x ̸= 0. Simil-
arly, the set A ∪ {5}, which is obtained by adding 5 to A, can be computed
as x | 0x20. The set A ∖ {5}, which is the set obtained by removing 5 from
A if it occurs in A, is represented by x & ~0x20.
The sets {0}, {1}, {2}, {3}, {4}, {5}, {6}, …, {31} are represented by
the hexadecimal numbers 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, …, 0x80000000. In
typical computer applications, some of these numbers are given names, and
these names are thought of as names for the possible elements of a set (al-
though, properly speaking, they are names for sets containing those ele-
ments). Suppose, for example, that a, b, c, and d are names for four of the
numbers from the above list. Then a | c is the set that contains the two ele-
ments corresponding to the numbers a and c. If x is a set, then x & ~d is the
set obtained by removing d from x. And we can test whether b is in x by
testing if x & b ̸= 0.
Here is an actual example, which is used in the Apple Mac operating
systems (macOS). Characters can be printed or displayed on the screen in
various sizes and styles. A font is a collection of pictures of characters in
4.4. Application: Programming with Sets * 137
a particular size and style. On the Mac, a basic font can be modified by
specifying any of the following style attributes: bold, italic, underline, outline,
shadow, condense, and extend. The style of a font is a subset of this set of at-
tributes. A style set can be specified by or-ing together individual attributes.
For example, an underlined, bold, italic font has style set underline | bold |
italic. For a plain font, with none of the style attributes set, the style set is
the empty set, which is represented by the number zero.
The Java programming language uses a similar scheme to specify style
attributes for fonts, but currently there are only two basic attributes, Font.BOLD
and Font.ITALIC. A more interesting example in Java is provided by event
types. An event in Java represents some kind of user action, such as press-
ing a key on the keyboard. Events are associated with ‘components’ such as
windows, buttons and scroll bars. Components can be set to ignore a given
type of event. We then say that that event type is disabled for that compon- 4
ent. If a component is set to process events of a given type, then that event
type is said to be enabled. Each component keeps track of the set of event
types that are currently enabled. It will ignore any event whose type is not
in that set. Each event type has an associated constant with a name such as
AWTEvent.MOUSE_EVENT_MASK. These constants represent the possible ele-
ments of a set of event types. A set of event types can be specified by or-ing
together a number of such constants. If c is a component and x is a number
representing a set of event types, then the command ‘c.enableEvents(x)’ en-
ables the events in the set x for the component c. If y represents the set of
event types that were already enabled for c, then the effect of this command
is to replace y with the union, y | x. Another command, ‘c.disableEvents(x)’,
will disable the event types in x for the component c. It does this by repla-
cing the current set, y, with y & ~x.
Exercises
1. Suppose that the numbers x and y represent the sets A and B. Show that the set
A ∖ B is represented by x & (~y).
2. Write each of the following binary numbers in hexadecimal:
a) 101101102 b) 102 c) 1111000011112 d) 1010012
3. Write each of the following hexadecimal numbers in binary:
a) 0x123 b) 0xFADE c) 0x137F d) 0xFF11
4. Give the value of each of the following expressions as a hexadecimal number:
a) 0x73 | 0x56A b) ~0x3FF0A2FF
c) (0x44 | 0x95) & 0xE7 d) 0x5C35A7 & 0xFF00
e) 0x5C35A7 & ~0xFF00 f) ~(0x1234 & 0x4321)
5. Find a calculator (or a calculator program on a computer) that can work with
hexadecimal numbers. Write a short report explaining how to work with hexa-
138 Chapter 4. Sets, Functions, and Relations
decimal numbers on that calculator. You should explain, in particular, how the
calculator can be used to do the previous problem.
6. This question assumes that you know how to add binary numbers. Suppose x
and y are binary numbers. Under what circumstances will the binary numbers
x + y and x | y be the same?
7. In addition to hexadecimal numbers, the programming languages Java and C
(but not the latest version of C++) support octal numbers. Look up and report
on octal numbers in Java or C. Explain what octal numbers are, how they are
written, and how they are used.
8. In the Unix (and Linux, macOS, …) operating system, every file has an associ-
ated set of permissions, which determine who can use the file and how it can be
used. The set of permissions for a given file is represented by a nine-bit binary
number. This number is sometimes written as an octal number. Research and
4 report on the Unix systems of permissions. What set of permissions is represen-
ted by the octal number 752? by the octal number 622? Explain what is done by
the Unix commands chmod g+rw filename and chmod o-w filename in terms
of sets.
9. Most modern programming languages have a boolean data type that has the
values true and false. The usual logical and, or, and not operators on boolean
values are represented in Java, C and C++ by the operators &&, | |, and !. C and
C++ (but not Java) allow integer values to be used in places where boolean
values are expected. In this case, the integer zero represents the boolean value
false while any non-zero integer represents the boolean value true. This means
that if x and y are integers, then both x & y and x && y are valid expressions,
and both can be considered to represent boolean values. Do the expressions
x & y and x && y always represent the same boolean value, for any integers x
and y? Do the expressions x | y and x | | y always represent the same boolean
values? Explain your answers.
10. Suppose that you, as a programmer, want to write a subroutine that will open
a window on the computer’s screen. The window can have any of the following
options: a close box, a zoom box, a resize box, a minimize box, a vertical scroll
bar, a horizontal scroll bar. Design a scheme whereby the options for the win-
dow can be specified by a single parameter to the subroutine. The parameter
should represent a set of options. How would you use your subroutine to open
a window that has a close box and both scroll bars and no other options? Inside
your subroutine, how would you determine which options have been specified
for the window?
4.5 Functions
Both the real world and the world of mathematics are full of what are called,
in mathematics, ‘functional relationships’. A functional relationship is a
relationship between two sets, which associates exactly one element from
the second set to each element of the first set.
4.5. Functions 139
For example, each item for sale in a store has a price. The first set in
this relationship is the set of items in the store. For each item in the store,
there is an associated price, so the second set in the relationship is the set of
possible prices. The relationship is a functional relationship because each
item has a price. That is, the question “What is the price of this item?” has
a single, definite answer for each item in the store.
Similarly, the question “Who is the (biological) mother of this person?”
has a single, definite answer for each person.4 So, the relationship ‘mother
of’ defines a functional relationship. In this case, the two sets in the rela-
tionship are the same set, namely the set of people. On the other hand, the
relationship ‘child of’ is not a functional relationship. The question “Who
is the child of this person?” does not have a single, definite answer for each
person. A given person might not have any child at all. And a given per-
son might have more than one child. Either of these cases—a person with
4
no child or a person with more than one child—is enough to show that the
relationship ‘child of’ is not a functional relationship.
Or consider an ordinary map, such as a map of Zuid-Holland or a street
map of Rome. The whole point of the map, if it is accurate, is that there is a
functional relationship between points on the map and points on the surface
of the Earth. Perhaps because of this example, a functional relationship is
sometimes called a mapping.
There are also many natural examples of functional relationships in math-
ematics. For example, every rectangle has an associated area. This fact ex-
presses a functional relationship between the set of rectangles and the set
of numbers. Every natural number n has a square, n2 . The relationship
‘square of’ is a functional relationship from the set of natural numbers to
itself.
4 For the sake of the example, we leave aside possibilities of surrogacy, etc.
140 Chapter 4. Sets, Functions, and Relations
ģ
Mathematical functions are different to functions in a program-
ming language in Java. We’ll come back to this in the next sec-
tion.
4
Ď
For example, if I is the set of items for sale in a given store and
M is the set of possible prices, then there is function c : I → M
which is defined by the fact that for each x ∈ I, c( x ) is the price
of the item x. Similarly, if P is the set of people, then there is
a function m : P → P such that for each person p, m( p) is the
mother of p. And if N is the set of natural numbers, then the
formula s(n) = n2 specifies a function s : N → N.
5 This is a convenient definition for the mathematical world, but as is often the case in math-
ematics, it neglects much of the real world. Functional relationships in the real world are mean-
ingful, but we model them in mathematics with meaningless sets of ordered pairs. We do this
for the usual reason: to have something precise and rigorous enough that we can make logical
deductions and prove things about it.
142 Chapter 4. Sets, Functions, and Relations
100
80
a b c
60
40
20 2 3
1
0
−10 −5 0 5 10
(b) Graph for the function g : A → B
with A = { a, b, c} and B = {1, 2, 3},
(a) Graph for the formula f (n) = n2 such that g = {( a, 2), (b, 3), (c, 1)}.
for −10 ≤ n ≤ 10
Suppose that f : A → B is a function from the set A to the set B. We say that
A is the domain of the function f and that B is the range of the function. We
define the image of the function f to be the set {b ∈ B | ∃ a ∈ A (b = f ( a))}.
Put more simply, the image of f is the set { f ( a) | a ∈ A}. That is, the image
is the set of all values, f ( a), of the function, for all a ∈ A. For example, for
the function s : N → N that is specified by s(n) = n2 , both the domain and
the range are N, and the image is the set {n2 | n ∈ N }, or {0, 1, 4, 9, 16, . . . }.
4.5. Functions 143
®
In some cases—particularly in courses like Calculus—the term
‘range’ is used to refer to what we are calling the image.
Ď
The function that associates every person to his or her mother is
not one-to-one because it is possible for two different people to
have the same mother. The function s : N → N specified by
s(n) = n2 is one-to-one. However, we can define a function
r : Z → Z by the same formula: r (n) = n2 , for n ∈ Z. The
4 function r is not one-to-one since two different integers can have
the same square. For example, r (−2) = r (2).
length of the longest path from a leaf to the root. We can now use a recursive
function h : TREE → N to define this more formally:
0 if t = ∅
h(t) = 0 if t = ( x, ∅) for some x ∈ D
1 + max 1 ≤ i ≤ k(h( Ti )) else
You will find that since trees are recursive data structures, many func-
tions on trees are also recursively defined. Here we also see that our func-
tion closely follows our formal definition of TREE, with one outcome for
every rule from the definition.
Similarly, we can also define a function r : TREE × D × D → TREE
that replaces elements in a tree. For example r (t, 8, 42) would change all the
values 8 to 42 in a tree t. Again our function will follow the rules from the
4
definition of TREE, recursively calling the function on subtrees, as well as
replacing any value that needs to be replaced. In other words, we can define
r like this:
∅ if t = ∅
(d, ∅) if t = (s, ∅)
r (t, s, d) = ( x, ∅) if t = ( x, ∅) ∧ x ̸= s for some x ∈ D
(d, (r ( T1 ), . . . , r ( Tk ))) if t = (s, ( T1 , . . . , Tk ))
( x, (r ( T ), . . . , r ( T ))) else
1 k
®
In Algorithms & Data Structures you will be implementing these
functions in Java, as well as analyse their run time complexity!
weights of edges on the edges. The shortest path from s to t here has a total
weight of 8, and is ((s, b), (b, t)).
t 5 b
10 3 7
s a
®
In Algorithms & Data Structures as well as Algorithm Design you
will learn about a lot of different algorithms that operate on
graphs. From Dijkstra’s algorithm for shortest paths, to Ford-
4 Fulkerson to solve maximum flow problems.
ģ
One way that programming languages differ is by what they al-
low as first-class objects. For example, Java added a ‘lambda syn-
tax’ to help writing ‘closures’ in version 8.
Suppose that A and B are sets. There are, in general, many functions that
map A to B. We can gather all those functions into a set. This set, whose
elements are all the functions from A to B, is denoted B A . (We’ll see later
why this notation is reasonable.) Using this notation, saying f : A → B is
exactly the same as saying f ∈ B A . Both of these notations assert that f is a
function from A to B. Of course, we can also form an unlimited number of
other sets, such as the power set P B A , the cross product B A × A, or the
set A A× A , which contains all the functions from the set A × A to the set A.
And of course, any of these sets can be the domain or range of a function.
An example of this is the function E : B A × A → B defined by the formula
E( f , a) = f ( a). Let’s see if we can make sense of this notation. Since the
domain of E is B A × A, an element in the domain is an ordered pair in which
the first coordinate is a function from A to B and the second coordinate is
an element of A. Thus, E( f , a) is defined for a function f : A → B and an
4
element a ∈ A. Given such an f and a, the notation f ( a) specifies an element
of B, so the definition of E( f , a) as f ( a) makes sense. The function E is called
the ‘evaluation function’ since it captures the idea of evaluating a function
at an element of its domain.
Exercises
†1. Let A = {1, 2, 3, 4} and let B = { a, b, c}. Find the sets A × B and B × A.
†2. Let A be the set { a, b, c, d}. Let f be the function from A to A given by the set of
ordered pairs {( a, b), (b, b), (c, a), (d, c)}, and let g be the function given by the
set of ordered pairs {( a, b), (b, c), (c, d), (d, d)}. Find the set of ordered pairs for
the composition g ◦ f .
†3. Let A = { a, b, c} and let B = {0, 1}. Find all possible functions from A to B. Give
each function as a set of ordered pairs. (Hint: Every such function corresponds
to one of the subsets of A.)
†4. Consider the functions from Z to Z which are defined by the following formulas.
Decide whether each function is onto and whether it is one-to-one; prove your
answers.
a) f (n) = 2n b) g(n) = n + 1 c) h(n) = n2 + n + 1
n/2, if n is even
d) s(n) =
(n+1)/2, if n is odd
†5. Prove that composition of functions is an associative operation. That is, prove
that for functions f : A → B, g : B → C, and h : C → D, the compositions (h ◦
g) ◦ f and h ◦ ( g ◦ f ) are equal.
†6. Suppose that f : A → B and g : B → C are functions and that g ◦ f is one-to-one.
a) Prove that f is one-to-one. (Hint: use a proof by contradiction.)
b) Find a specific example that shows that g is not necessarily one-to-one.
†7. Suppose that f : A → B and g : B → C, and suppose that the composition g ◦ f
is an onto function.
148 Chapter 4. Sets, Functions, and Relations
»
In this section we go into detail about functions in computer pro-
gramming. You won’t be examined on this in Reasoning & Logic!
You will find that there is again quite some overlap with your
study materials from Object-Oriented Programming and later on
in your curriculum with Concepts of Programming Languages.
In Java, int is a data type. From the mathematical point of view, a data type
is a set. The data type int is the set of all integers that can be represented
as 32-bit binary numbers. Mathematically, then, int ⊆ Z. (You should get
used to the fact that sets and functions can have names that consist of more
than one character, since it’s done all the time in computer programming.)
The first line of the above function definition, int square(int n), says that
we are defining a function named square whose range is int and whose do-
main is int. In the usual notation for functions, we would express this as
4.6. Application: Programming with Functions * 149
square : int → int, or possibly as square ∈ intint , where intint is the set of all
functions that map the set int to the set int.
The first line of the function, int square(int n), is called the proto-
type of the function. The prototype specifies the name, the domain, and
the range of the function and so carries exactly the same information as the
notation f : A → B. The ‘n’ in the prototype int square(int n) is a name
for an arbitrary element of the data type int. We call n a parameter of the
function. The rest of the definition of square tells the computer to calculate
the value of square(n) for any n ∈ int by multiplying n times n. The state-
ment return n*n says that n ∗ n is the value that is computed, or ‘returned’,
by the function. (The ∗ stands for multiplication.)
Java has many data types in addition to int. There is a boolean data type
named boolean. The values of type boolean are true and false. Mathematically,
boolean is a name for the set {true, false}. The type double consists of real 4
numbers, which can include a decimal point. Of course, on a computer, it’s
not possible to represent the entire infinite set of real numbers, so double rep-
resents some subset of the mathematical set of real numbers. There is also
a data type whose values are strings of characters, such as “Hello world” or
“xyz152QQZ”. The name for this data type in Java is string. All these types,
and many others, can be used in functions. For example, in Java, m % n is
the remainder when the integer m is divided by the integer n. We can define
a function to test whether an integer is even as follows:
1 boolean even(int k) {
return k % 2 == 0;
3 }
You don’t need to worry about all the details here, but you should under-
stand that the prototype, boolean even(int k), says that even is a function
from the set int to the set boolean. That is, even : int → boolean. Given an in-
teger N, even( N ) has the value true if N is an even integer, and it has the
value false if N is an odd integer.
A function can have more than one parameter. For example, we might
define a function with prototype int index(string str, string sub). If
s and t are strings, then index(s, t) would be the int that is the value of the
function at the ordered pair (s, t). We see that the domain of index is the
cross product string × string, and we can write index : string × string → int
or, equivalently, index ∈ intstring×string .
Not every Java function is actually a function in the mathematical sense.
In mathematics, a function must associate a single value in its range to each
value in its domain. There are two things that can go wrong: the value of
the function might not be defined for every element of the domain, and the
function might associate several different values to the same element of the
domain. Both of these things can happen with Java functions.
150 Chapter 4. Sets, Functions, and Relations
The parameters to sum form an ordered triple in which the first coordinate is
a function and the second and third coordinates are integers. So, we could
write
sum : doubleint × int × int → double
It’s interesting that computer programmers deal routinely with such com-
plex objects.
Languages where functions are first-class objects are for example Python
and Scala. These languages support what is called functional program-
ming.
One of the most accessible languages that supports functional program-
ming is JavaScript, a language that is used on webpages. (Although the
names are similar, JavaScript and Java are only distantly related. You prob-
ably knew that.) In JavaScript, the function that computes the square of its 4
parameter could be defined as
1 function square(n) {
return n*n;
3 }
This is similar to the Java definition of the same function, but you’ll notice
that no type is specified for the parameter n or for the value computed by the
function. Given this definition of square, square(x) would be legal for any x
of any type. (Of course, the value of square(x) would be undefined for most
types, so square is a very partial function, like most functions in JavaScript.)
In effect, all possible data values in JavaScript are bundled together into one
set, which I will call data. We then have square : data → data.7
In JavaScript, a function really is a first-class object. We can begin to see
this by looking at an alternative definition of the function square:
1 square = function(n) { return n*n; }
a functional programming language, Haskell, for example, that is as strict about types as C++.
For information about Haskell, see www.haskell.org.
152 Chapter 4. Sets, Functions, and Relations
Here, the first parameter is the function that computes squares. We have
created and used this function without ever giving it a name.
It is even possible in JavaScript for a function to return another function
as its value. For example,
1 function monomial(a, n) {
return (function(x) { a*Math.pow(x,n); });
3 }
4 1 f = monomial (2,3);
would define f to be the function that satisfies f ( x ) = 2x3 , and if sum is the
function described above, then
1 sum( monomial (8,4), 3, 6 )
Exercises
1. For each of the following Java-like function prototypes, translate the prototype
into a standard mathematical function specification, such as func : float → int.
a) int strlen(string s)
b) double pythag(double x, double y)
c) int round(double x)
d) string sub(string s, int n, int m)
e) string unlikely(Function<String,Integer> f )
f) int h( Function<Integer,Integer> f, Function<Integer,Integer> g )
2. Write a Java-like function prototype for a function that belongs to each of the
following sets.
a) stringstring
b) booleanfloat×float
int
c) floatint
3. It is possible to define new types in Java by using classes. For example, the defin-
ition
4.7. Counting Past Infinity 153
1 Class point {
double x;
3 double y;
}
defines a new type named point. A value of type point contains two values of
type double. What mathematical operation corresponds to the construction of
this data type? Why?
4. Let square, sum and monomial be the JavaScript functions described in this section.
What is the value of each of the following?
a) sum(square, 2, 4)
b) sum(monomial(5,2), 1, 3)
c) monomial(square(2), 7)
d) sum(function(n) { return 2 ∗ n; }, 1, 5)
e) square(sum(monomial(2,3), 1, 2))
4
5. Write a JavaScript function named compose that computes the composition of
two functions. That is, compose( f ,g) is f ◦ g, where f and g are functions of one
parameter. Recall that f ◦ g is the function defined by ( f ◦ g)( x ) = f ( g( x )).
4.7.1 Cardinality
In counting, as it is learned in childhood, the set {1, 2, 3, . . . , n} is used as a
typical set that contains n elements. In mathematics and computer science,
it has become more common to start counting with zero instead of with one,
so we define the following sets to use as our basis for counting:
result holds for the cross product of more that two finite sets. For example,
| A × B × C | = | A | · | B | · | C |.
It’s also easy to compute | A ∪ B| in the case where A and B are disjoint
finite sets. (Recall that two sets A and B are said to be disjoint if they have
no members in common, that is, if A ∩ B = ∅.) Suppose | A| = n and
| B| = m. If we wanted to count the elements of A ∪ B, we could use the
n numbers from 0 to n − 1 to count the elements of A and then use the m
numbers from n to n + m − 1 to count the elements of B. This amounts to
a one-to-one correspondence between A ∪ B and the set Nn+m . We see that
| A ∪ B| = n + m. That is, for disjoint finite sets A and B, | A ∪ B| = | A| + | B|.
What about A ∪ B, where A and B are not disjoint? We have to be careful
not to count the elements of A ∩ B twice. After counting the elements of A,
there are only | B| − | A ∩ B| new elements in B that still need to be counted.
So we see that for any two finite sets A and B, | A ∪ B| = | A| + | B| − | A ∩ B|. 4
What about the number of subsets of a finite set A? What is the rela-
tionship between | A| and |P( A)|? The answer is provided by the following
theorem.
Proof. Let P(n) be the statement “Any set with cardinality n has 2n subsets”.
We will use induction to show that P(n) is true for all n ∈ N.
Base case: For n = 0, P(n) is the statement that a set with cardinality 0
has 20 subsets. The only set with 0 elements is the empty set. The empty set
has exactly 1 subset, namely itself. Since 20 = 1, P(0) is true.
Inductive case: Let k be an arbitrary element of N, and assume that P(k)
is true. That is, assume that any set with cardinality k has 2k elements. (This
is the induction hypothesis.) We must show that P(k + 1) follows from this
assumption. That is, using the assumption that any set with cardinality k
has 2k subsets, we must show that any set with cardinality k + 1 has 2k+1
subsets.
Let A be an arbitrary set with cardinality k + 1. We must show that
|P( A)| = 2k+1 . Since | A| > 0, A contains at least one element. Let x be
some element of A, and let B = A ∖ { x }. The cardinality of B is k, so we
have by the induction hypothesis that |P( B)| = 2k . Now, we can divide the
subsets of A into two classes: subsets of A that do not contain x and subsets
of A that do contain x. Let Y be the collection of subsets of A that do not
contain x, and let X be the collection of subsets of A that do contain x. X and
Y are disjoint, since it is impossible for a given subset of A both to contain
and to not contain x. It follows that |P( A)| = | X ∪ Y | = | X | + |Y |.
Now, a member of Y is a subset of A that does not contain x. But that
is exactly the same as saying that a member of Y is a subset of B. So Y =
P( B), which we know contains 2k members. As for X, there is a one-to-one
156 Chapter 4. Sets, Functions, and Relations
We have seen that the notation A B represents the set of all functions
from B to A. Suppose A and B are finite, and that | A| = n and | B| = m.
Then A B = nm = | A|| B| . (This fact is one of the reasons why the notation
A B is reasonable.) One way to see this is to note that there is a one-to-one
correspondence between A B and a cross product A × A × · · · A, where the
number of terms in the cross product is m. (This will be shown in one of the
exercises at the end of this section.) It follows that A B = | A| · | A| · · · | A| =
4 n · n · · · n, where the factor n occurs m times in the product. This product
is, by definition, nm .
This discussion about computing cardinalities is summarized in the fol-
lowing theorem:
• | A × B | = | A | · | B |.
• | A ∪ B | = | A | + | B | − | A ∩ B |.
• |P( A)| = 2| A| .
prizes have to go to four different people? This question takes us a little farther into combin-
atorics than we would like to go, but the answer is not hard. The first award can be given to
any of n people. The second prize goes to one of the remaining n − 1 people. There are n − 2
choices for the third prize and n − 3 for the fourth. The number of different ways of awarding
the prizes to four different people is the product n(n − 1)(n − 2)(n − 3). What about dividing
arbitrary objects between arbitrary numbers of people? That’s one topic of social choice theory.
158 Chapter 4. Sets, Functions, and Relations
0.9
90398937249879561297927654857945…
0.12
2349342094059875980239230834549… 4
400043298436234709323279989579…
0.224
0.500000000000000000000000000000000…
0.777433449234234876990120909480009…
0.777555555588888889498888980000111…
0.123456778888888888888888800000000…
0.348354400009848712712123940320577…
0.934732444447900498340999990948900…
.
.
.
This is only (a small part of) one possible list. How can we be certain
that every such list leaves out some real number between zero and one? The
trick is to look at the digits shown in bold face. We can use these digits to
build a number that is not in the list. Since the first number in the list has
a 9 in the first position after the decimal point, we know that this number
cannot equal any number of, for example, the form 0.4…. Since the second
number has a 2 in the second position after the decimal point, neither of the
first two numbers in the list is equal to any number that begins with 0.44….
Since the third number has a 4 in the third position after the decimal point,
none of the first three numbers in the list is equal to any number that begins
0.445…. We can continue to construct a number in this way, and we end
up with a number that is different from every number in the list. The nth
digit of the number we are building must differ from the nth digit of the
nth number in the list. These are the digits shown in bold face in the above
list. To be definite, we use a 5 when the corresponding boldface number is 4,
and otherwise we use a 4. For the list shown above, this gives a number that
begins 0.44544445…. The number constructed in this way is not in the given
list, so the list is incomplete. The same construction clearly works for any list
of real numbers between zero and one. No such list can be a complete listing
of the real numbers between zero and one, and so there can be no complete
160 Chapter 4. Sets, Functions, and Relations
»
Proofs by diagonalisation are not a part of Reasoning & Logic. You
should be able to prove a set is countably infinite (by finding a
bijection from N to the set), but you will not be asked to prove
a set is uncountable. We leave that for the course Automata, Com-
putability and Complexity later in your curriculum.
A ̸= f (y).9 To see why this is true, let y be any element of X. There are two
cases to consider. Either y ∈ f (y) or y ̸∈ f (y). We show that whichever case
holds, A ̸= f (y). If it is true that y ∈ f (y), then by the definition of A, y ̸∈ A.
Since y ∈ f (y) but y ̸∈ A, f (y) and A do not have the same elements and
therefore are not equal. On the other hand, suppose that y ̸∈ f (y). Again,
by the definition of A, this implies that y ∈ A. Since y ̸∈ f (y) but y ∈ A,
f (y) and A do not have the same elements and therefore are not equal. In
either case, A ̸= f (y). Since this is true for any y ∈ X, we conclude that A is
not in the image of f and therefore f is not a one-to-one correspondence.
‘real numbers’ exist in some sense in the real world. If all this sounds like
it’s a bit of a philosophical muddle, it is. That’s the state of things today at
the foundation of mathematics, and it has implications for the foundations
of computer science, as we’ll see in the next chapter.
Exercises
1. Suppose that A, B, and C are finite sets which are pairwise disjoint. (That is,
A ∩ B = A ∩ C = B ∩ C = ∅.) Express the cardinality of each of the following
sets in terms of | A|, | B|, and |C |. Which of your answers depend on the fact that
the sets are pairwise disjoint?
a) P( A ∪ B) b) A × ( BC ) c) P( A) × P(C )
d) A B×C e) ( A × B)C f) P( A B )
g) ( A ∪ B) C h) ( A ∪ B) × A i) A × A × B × B 4
2. Suppose that A and B are finite sets which are not necessarily disjoint. What are
all the possible values for | A ∪ B| ?
3. Let’s say that an ‘identifier’ consists of one or two characters. The fist character is
one of the twenty-six letters (A, B, …, C). The second character, if there is one, is
either a letter or one of the ten digits (0, 1, …, 9). How many different identifiers
are there? Explain your answer in terms of unions and cross products.
4. Suppose that there are five books that you might bring along to read on your
vacation. In how many different ways can you decide which books to bring,
assuming that you want to bring at least one? Why?
5. Show that the cardinality of a finite set is well-defined. That is, show that if f is
a bijective function from a set A to Nn , and if g is a bijective function from A to
Nm , then n = m.
6. Finish the proof of Theorem 4.7 by proving the following statement: Let A be
a non-empty set, and let x ∈ A. Let B = A ∖ { x }. Let X = {C ⊆ A | x ∈ C }.
Define f : P( B) → X by the formula f (C ) = C ∪ { x }. Show that f is a bijective
function.
7. Use induction on the cardinality of B to show that for any finite sets A and B,
B
A = | A|| B| . (Hint: For the case where B ̸= ∅, choose x ∈ B, and divide A B
into classes according to the value of f ( x ).)
8. Let A and B be finite sets with | A| = n and | B| = m. Let us list the elements of B
as B = {b0 , b1 , . . . , bm−1 }. Define the function F : A B → A × A × · · · × A, where
A occurs m times in the cross product, by F ( f ) = f (b0 ), f (b1 ), . . . , f (bm−1 ) .
Show that F is a one-to-one correspondence.
9. Show that Z, the set of integers, is countable by finding a one-to-one correspond-
ence between N and Z.
10. Show that the set N × N is countable.
11. Complete the proof of Theorem 2.9 as follows:
a) Suppose that A and B are countably infinite sets. Show that A ∪ B is count-
ably infinite.
164 Chapter 4. Sets, Functions, and Relations
12. Prove that each of the following statements is true. In each case, use a proof by
contradiction.
a) Let X be a countably infinite set, and let N be a finite subset of X. Then
X ∖ N is countably infinite.
b) Let A be an infinite set, and let X be a subset of A. Then at least one of the
sets X and A ∖ X is infinite.
c) Every subset of a finite set is finite.
13. Let A and B be sets and let ⊥ be an entity that is not a member of B. Show
that there is a one-to-one correspondence between the set of functions from A
to B ∪ {⊥} and the set of partial functions from A to B. (Partial functions were
4 defined in Section 4.6. The symbol ‘⊥’ is sometimes used in theoretical computer
science to represent the value ‘undefined.’)
4.8 Relations
Ď
For example, if P is the set of people and B is the set of books
owned by a library, then we can define a relation R on the sets P
and B to be the set R = {( p, b) ∈ P × B | p has b out on loan}.
The fact that a particular ( p, b) ∈ R is a fact about the world that
the library will certainly want to keep track of. When a collection
of facts about the world is stored on a computer, it is called a
database. We’ll see in the next section that relations are the most
common means of representing data in databases.
R is called transitive if ∀ a ∈ A, ∀b ∈ A, ∀c ∈ A ( a R b ∧ b R c) →
( a R c) . Transitivity allows us to ‘chain together’ two true statements a R b
and b R c, which are ‘linked’ by the b that occurs in each statement, to de-
duce that a R c. For example, suppose P is the set of people, and define the
relation C on P such that x C y if and only if x is a child of y. The relation C
is not transitive because the child of a child of a person is not a child of that
person. Suppose, on the other hand, that we define a relation D on P such
that x D y if and only if x is a descendent of y. Then D is a transitive relation
on the set of people, since a descendent of a descendent of a person is a des-
cendent of that person. That is, from the facts that Elizabeth is a descendent
of Victoria and Victoria is a descendent of James, we can deduce that Eliza-
beth is a descendent of James. In the mathematical world, the relations ≤
and < on the set N are both transitive.
4 R is said to be symmetric if ∀ a ∈ A, ∀b ∈ B ( a R b → b R a). That is,
whenever a is related to b, it follows that b is related to a. The relation ‘is a
first cousin of’ on the set of people is symmetric, since whenever x is a first
cousin of y, we have automatically that y is a first cousin of x. On the other
hand, the ‘child of’ relation is certainly not symmetric. The relation ≤ on N
is not symmetric. From the fact that n ≤ m, we cannot conclude that m ≤ n.
It is true for some n and m in N that n ≤ m → m ≤ n, but it is not true for
all n and m in N.
Finally, R is antisymmetric if ∀ a ∈ A, ∀b ∈ B ( a R b ∧ b R a) → a = b .
The relation R is antisymmetric if for any two distinct elements x and y of A,
we can’t have both x R y and y R x. The relation ≤ on N is antisymmetric
because from the facts that n ≤ m and m ≤ n, we can deduce that n =
m. The relation ‘child of’ on the set of people is antisymmetric since it’s
impossible to have both that x is a child of y and y is a child of x.
®
During lectures, we’ll think about how to draw relations graph-
ically. See the figure in Section 4.5.3 for one kind of graphical
depiction.
remark holds for each of the properties of relations that are discussed here.
4.8. Relations 167
members of the same subset in the partition. That is, two elements of A are
related if they are in the same category. It is clear that the relation defined
in this way is reflexive, symmetric, and transitive.
An equivalence relation is defined to be a binary relation that is reflexive,
symmetric, and transitive. Any relation defined, as above, from a partition
is an equivalence relation. Conversely, we can show that any equivalence
relation defines a partition. Suppose that R is an equivalence relation on a
set A. Let a ∈ A. We define the equivalence class of a under the equivalence
relation R to be the subset [ a]R defined as [ a]R = {b ∈ A | b R a}. That is,
the equivalence class of a is the set of all elements of A that are related to a.
In most cases, we’ll assume that the relation in question is understood, and
we’ll write [ a] instead of [ a]R . Note that each equivalence class is a subset of
A. The following theorem shows that the collection of equivalence classes
4 form a partition of A.
Theorem 4.12. Let A be a set and let R be an equivalence relation on A. Then the
collection of all equivalence classes under R is a partition of A.
The point of this theorem is that if we can find a binary relation that
satisfies certain properties, namely the properties of an equivalence rela-
tion, then we can classify things into categories, where the categories are
the equivalence classes.
11 Image: commons.wikimedia.org/wiki/File:Rubiks_cube_scrambled.jpg.
170 Chapter 4. Sets, Functions, and Relations
b
Fortunately if you buy the cube in the configuration you want it
to be in (i.e., with 6 faces all having just one colour), you can still
be successful. Since all configurations you can get to using legal
moves are in the same class, you can only move to another class
of configurations with an illegal move. So it is only by taking the
cube apart and incorrectly putting it back together that you can
really change this puzzle from difficult to impossible.
Since we’re talking about solving the Rubik’s Cube, could you
write a computer program to solve it? Given a cube in some
initial state, what is the shortest sequence of moves to get to
the solved state—or to prove it cannot be done? See Agostinelli,
McAleer, Shmakov & Baldi, Solving the Rubik’s cube with deep rein-
forcement learning and search, Nature Machine Intelligence (2019).
4
Suppose that R is a binary relation on a set A. Even though R might
not be transitive, it is always possible to construct a transitive relation from
R in a natural way. If we think of a R b as meaning that a is related by R
to b ‘in one step’, then we consider the relationship that holds between two
elements x and y when x is related by R to y ‘in one or more steps’. This
relationship defines a binary relation on A that is called the transitive clos-
ure of R. The transitive closure of R is denoted R∗ . Formally, R∗ is defined
as follows: For a and b in A, a R∗ b if there is a sequence x0 , x1 , . . . xn of ele-
ments of A, where n > 0 and x0 = a and xn = b, such that x0 R x1 , x1 R x2 ,
…, and xn−1 R xn .
®
You will revisit the notion of transitive closures in the course
Automata, Computability and Complexity.
Ď
For a practical example, suppose that C is the set of all cities and
let A be the binary relation on C such that for x and y in C, x A y
if there is a regularly scheduled airline flight from x to y. Then
the transitive closure A∗ has a natural interpretation: x A∗ y if it’s
possible to get from x to y by a sequence of one or more regularly-
scheduled airline flights. You’ll find a few more examples of
transitive closures in the exercises.
Exercises
1. For a finite set, it is possible to define a binary relation on the set by listing the
4.8. Relations 171
elements of the relation, considered as a set of ordered pairs. Let A be the set
{ a, b, c, d}, where a, b, c, and d are distinct. Consider each of the following binary
relations on A. Is the relation reflexive? Symmetric? Antisymmetric? Transit-
ive? Is it a partial order? An equivalence relation?
a) R = {( a, b), ( a, c), ( a, d)}.
b) S = {( a, a), (b, b), (c, c), (d, d), ( a, b), (b, a)}.
c) T = {(b, b), (c, c), (d, d)}.
d) C = {( a, b), (b, c), ( a, c), (d, d)}.
e) D = {( a, b), (b, a), (c, d), (d, c)}.
2. Let A be the set {1, 2, 3, 4, 5, 6}. Consider the partition of A into the subsets
{1, 4, 5}, {3}, and {2, 6}. Write out the associated equivalence relation on A
as a set of ordered pairs.
3. Consider each of the following relations on the set of people. Is the relation
reflexive? Symmetric? Transitive? Is it an equivalence relation? 4
a) x is related to y if x and y have the same biological parents.
b) x is related to y if x and y have at least one biological parent in common.
c) x is related to y if x and y were born in the same year.
d) x is related to y if x is taller than y.
e) x is related to y if x and y have both visited Indonesia.
f) x is related to y if x and y have both contracted the same disease.
4. It is possible for a relation to be both symmetric and antisymmetric. For example,
the equality relation, =, is a relation on any set which is both symmetric and
antisymmetric. Suppose that A is a set and R is a relation on A that is both
symmetric and antisymmetric. Show that R is a subset of = (when both relations
are considered as sets of ordered pairs). That is, show that for any a and b in A,
( a R b ) → ( a = b ).
5. Let ∼ be the relation on R, the set of real numbers,
√ √ for x and y in R,
such that
x ∼ y if and√only if x − √
y ∈ Z. For example, 2 − 1 ∼ 2 + 17 because the
difference, ( 2 − 1) − ( 2 + 17), is −18, which is an integer. Show that ∼ is
an equivalence relation. Show that each equivalence class [ x ]∼ contains exactly
one number a which satisfies 0 ≤ a < 1. (Thus, the set of equivalence classes
under ∼ is in one-to-one correspondence with the half-open interval [0, 1).)
6. Let A and B be any sets, and suppose f : A → B. Define a relation ∼ on B such
that for any x and y in A, x ∼ y if and only if f ( x ) = f (y). Show that ∼ is an
equivalence relation on A.
7. Let Z + be the set of positive integers {1, 2, 3, . . . }. Define a binary relation D
on Z + such that for n and m in Z + , n D m if n divides evenly into m, with no
remainder. Equivalently, n D m if n is a factor of m, that is, if there is a k in Z +
such that m = nk. Show that D is a partial order.
8. Consider the set N × N, which consists of all ordered pairs of natural numbers.
Since N × N is a set, it is possible to have binary relations on N × N. Such a
relation would be a subset of (N × N ) × (N × N ). Define a binary relation ⪯
on N × N such that for (m, n) and (k, ℓ) in N × N, (m, n) ⪯ (k, ℓ) if and only if
either m < k or ((m = k) ∧ (n ≤ ℓ)). Which of the following are true?
172 Chapter 4. Sets, Functions, and Relations
®
You’ll learn more about databases in the courses Web & Database
Technology and Information & Data Management.
sets. A relation that is a subset of the cross product int × int × string would
consist of ordered 3-tuples such as (17, 42, “hike”). In a relational database,
the data is stored in the form of one or more such relations. The relations
are called tables, and the tuples that they contain are called rows or records.
As an example, consider a lending library that wants to store data about
its members, the books that it owns, and which books the members have
out on loan. This data could be represented in three tables, as illustrated in
Figure 4.8. The relations are shown as tables rather than as sets of ordered
tuples, but each table is, in fact, a relation. The rows of the table are the
tuples. The Members table, for example, is a subset of int × string × string ×
string, and one of the tuples is (1782, “Smit, Johan”, “107 Main St”, “New
York, NY”). A table does have one thing that ordinary relations in mathem-
atics do not have. Each column in the table has a name. These names are
used in the query language to manipulate the data in the tables. 4
The data in the Members table is the basic information that the library
needs in order to keep track of its members, namely the name and address
of each member. A member also has a MemberID number, which is presum-
ably assigned by the library. Two different members can’t have the same
MemberID, even though they might have the same name or the same address.
The MemberID acts as a primary key for the Members table. A given value
of the primary key uniquely identifies one of the rows of the table. Simil-
arly, the BookID in the Books table is a primary key for that table. In the
Loans table, which holds information about which books are out on loan
to which members, a MemberID unambiguously identifies the member who
has a given book on loan, and the BookID says unambiguously which book
that is. Every table has a primary key, but the key can consist of more than
one column. The DBMS enforces the uniqueness of primary keys. That is,
it won’t let users make a modification to the table if it would result in two
rows having the same primary key.
The fact that a relation is a set—a set of tuples—means that it can’t con-
tain the same tuple more than once. In terms of tables, this means that a
table shouldn’t contain two identical rows. But since no two rows can con-
tain the same primary key, it’s impossible for two rows to be identical. So
tables are in fact relations in the mathematical sense.
The library must have a way to add and delete members and books and
to make a record when a book is borrowed or returned. It should also have
a way to change the address of a member or the due date of a borrowed
book. Operations such as these are performed using the DBMS’s query lan-
guage. SQL has commands named INSERT, DELETE, and UPDATE for perform-
ing these operations. The command for adding Barack Obama as a member
of the library with MemberID 999 would be
INSERT INTO Members
174 Chapter 4. Sets, Functions, and Relations
SELECT BookID
FROM Loans
WHERE MemberID IN (SELECT MemberID
FROM Members
WHERE City = "New York, NY")
More than one table can be listed in the FROM part of a query. The tables
that are listed are joined into one large table, which is then used for the
query. The large table is essentially the cross product of the joined tables,
when the tables are understood as sets of tuples. For example, suppose that
we want the titles of all the books that are out on loan to members who live
in New York City. The titles are in the Books table, while information about
loans is in the Loans table. To get the desired data, we can join the tables
and extract the answer from the joined table: 4
SELECT Title
FROM Books, Loans
WHERE MemberID IN (SELECT MemberID
FROM Members
WHERE City = "New York, NY")
In fact, we can do the same query without using the nested SELECT. We
need one more bit of notation: If two tables have columns that have the
same name, the columns can be named unambiguously by combining the
table name with the column name. For example, if the Members table and
Loans table are both under discussion, then the MemberID columns in the
two tables can be referred to as Members.MemberID and Loans.MemberID. So,
we can say:
SELECT Title
FROM Books, Loans
WHERE City ="New York, NY"
AND Members.MemberID = Loans.MemberID
This is just a sample of what can be done with SQL and relational data-
bases. The conditions in WHERE clauses can get very complicated, and there
are other operations besides the cross product for combining tables. The
database operations that are needed to complete a given query can be com-
plex and time-consuming. Before carrying out a query, the DBMS tries to
optimize it. That is, it manipulates the query into a form that can be carried
out most efficiently. The rules for manipulating and simplifying queries
form an algebra of relations, and the theoretical study of relational databases
is in large part the study of the algebra of relations.
176 Chapter 4. Sets, Functions, and Relations
Exercises
1. Using the library database from Figure 4.8, what is the result of each of the fol-
lowing SQL commands?
c) UPDATE Loans
SET DueDate = "20 November"
4 WHERE BookID = 221
d) SELECT Title
FROM Books, Loans
WHERE Books.BookID = Loans.BookID
2. Using the library database from Figure 4.8, write an SQL command to do each
of the following database manipulations:
a) Find the BookID of every book that is due on 1 November 2010.
b) Change the DueDate of the book with BookID 221 to 15 November 2010.
c) Change the DueDate of the book with title “Summer Lightning” to 14
November 2010. Use a nested SELECT.
d) Find the name of every member who has a book out on loan. Use joined
tables in the FROM clause of a SELECT command.
3. Suppose that a university colleges wants to use a database to store information
about its students, the courses that are offered in a given term, and which stu-
dents are taking which courses. Design tables that could be used in a relational
database for representing this data. Then write SQL commands to do each of
the following database manipulations. (You should design your tables so that
they can support all these commands.)
a) Enroll the student with ID number 1928882900 in “English 260”.
b) Remove “Johan Smit” from “Biology 110”.
c) Remove the student with ID number 2099299001 from every course in
which that student is enrolled.
d) Find the names and addresses of the students who are taking “Computer
Science 229”.
e) Cancel the course “History 101”.
4.9. Application: Relational Databases * 177
Members
MemberID Name Address City
1782 Smit, Johan 107 Main St New York, NY
2889 Jones, Mary 1515 Center Ave New York, NY
378 Lee, Joseph 90 Park Ave New York, NY
4277 Smit, Johan 2390 River St Newark, NJ
5704 De Jong, Sally 89 Main St New York, NY 4
Books
BookID Title Author
182 I, Robot Isaac Asimov
221 The Sound and the Fury William Faulkner
38 Summer Lightning P.G. Wodehouse
437 Pride and Prejudice Jane Austen
598 Left Hand of Darkness Ursula LeGuin
629 Foundation Trilogy Isaac Asimov
720 The Amber Spyglass Philip Pullman
Loans
MemberID BookID DueDate
378 221 8 October 2010
2889 182 1 November 2010
4277 221 1 November 2010
1782 38 30 October 2010
Figure 4.8: Tables that could be part of a relational database. Each table
has a name, shown above the table. Each column in the table also has a
name, shown in the top row of the table. The remaining rows hold the data.
Chapter 5
Looking Beyond
OMING TO THE RIGHT CONCLUSION has been the theme of this book. We
C learned how to represent statements formally in Chapter 2, and how to
prove or disprove statements in Chapter 3. We looked at more important
foundational parts of computer science in Chapter 4: sets, functions and
relations.
Last chapter, we said that the foundations of mathematics are in “a bit
of a philosophical muddle”. That was at the end of our discussion about
counting past infinity (Section 4.7). The questions from the work of Cantor,
Russell and others became more profound in the 1930s thanks to Kurt Gödel,
whom we mentioned briefly in Chapter 3. All this just before practical com-
puters were invented—yes, the theoretical study of computing began before
digital computers existed!
Since this book is about the foundations of computation, let’s say a little
more about Gõdel and his contemporary, Alan Turing.
Gödel published his two incompleteness theorems in 1931. The first in-
completeness theorem states that for any self-consistent recursive axiomatic
system powerful enough to describe the arithmetic of the natural numbers1 ,
there are true propositions about the naturals that cannot be proved from
the axioms. In other words, in any formal system of logic, there will always
be statements that you can never prove nor disprove: you don’t know.
These two theorems ended a half-century of attempts, beginning with
the work of Frege and culminating in the work of Russell and others, to
find a set of axioms sufficient for all mathematics. Game over: all axiomatic
systems are signposts in a void.
org/wiki/Peano_axioms.
179
180 Chapter 5. Looking Beyond *
5 Around the same time, one of the early models of computation was de-
veloped by the British mathematician, Alan Turing. Turing was interested
in studying the theoretical abilities and limitations of computation. (This
still before the first computers! Von Neumann knew Turing and emphas-
ized that “the fundamental conception [of the modern computer] is owing
to Turing” and not to himself.2 ) Turing’s model for computation is a very
simple, abstract computing machine which has come to be known as a Tur-
ing machine. While Turing machines are not very practical, their use as a
fundamental model for computation means that every computer scientist
should be familiar with them, at least in a general way.3 You’ll learn about
them in Automata, Computability and Complexity.
We can also use Turing machines to define ‘computable languages’. The
idea is that anything that is possible to compute, you can compute using a
Turing machine (just very slowly). Turing, with American mathematician
Alonzo Church, made a hypothesis about the nature of computable func-
tions.4 It states that a function on the natural numbers is computable by a
human being following an algorithm (ignoring resource limitations), if and
Neumann_architecture.
3 In fact, the Brainf*ck programming language we mentioned earlier is not much more than
a Turing machine.
4 It’s not a theorem because it is not proved, but all theoretical computer scientists believe it
to be true. Between them, Church and Turing did prove that a function is λ-computable if and
only if it is Turing computable if and only if it is general recursive. Another thesis that is widely
believed but not proved is: for the things we can compute, there is a difference between those
that need only polynomial time (in the size of the input) for the computation and those that
need more than polynomial time. More about that in Automata, Computability and Complexity.
181
b
Attempt the exercise first before looking at the solution!
Solutions 2.1
p q p∨q p q p∧q
p ¬p
0 0 0 0 0 0
1. 0 1
0 1 1 0 1 0
1 0 1 1 0 0 1 0
1 1 1 1 1 1
2.
B
A
z }| { z }| {
p q p→q p ∧ ( A) ( B) → q
a) 0 0 1 0 1
0 1 1 0 1
1 0 0 0 1
1 1 1 1 1
183
184 Selected Solutions
C
A
z }| {
B
z }| { z }| { D
z }| {
p q r p→q q→r ( A) ∧ ( B) p→r (C ) → ( D )
0 0 0 1 1 1 1 1
0 0 1 1 1 1 1 1
b) 0 1 0 1 0 0 1 1
0 1 1 1 1 1 1 1
1 0 0 0 1 0 0 1
1 0 1 0 1 0 1 1
1 1 0 1 0 0 0 1
1 1 1 1 1 1 1 1
p p ∧ ¬p
c) 0 0
1 0
5 Since the proposition is always false, the proposition is a contradiction.
A B
z }| { z }| {
p q p∨q p∧q ( A) → ( B)
d) 0 0 0 0 1
0 1 1 0 0
1 0 1 0 0
1 1 1 1 1
Since the proposition is sometimes true and sometimes false, the proposition is a
contingency.
p p ∨ ¬p
e) 0 1
1 1
f) 0 0 0 0 1
0 1 0 1 1
1 0 0 1 1
1 1 1 1 1
p↔q
0 0 1
3. We will compare the truth tables of the subquestions to that of p ↔ q:
0 1 0
1 0 0
1 1 1
A B
z }| { z }| {
p q p→q q→p A∧B
a) 0 0 1 1 1
0 1 1 0 0
1 0 0 1 0
1 1 1 1 1
5
A B
z}|{ z}|{
p q ¬p ¬q A↔B
b) 0 0 1 1 1
0 1 1 0 0
1 0 0 1 0
1 1 0 0 1
D
A
z }| { z}|{
B C
z}|{ z }| {
p q p→q ¬p ¬q B→C A∧D
c) 0 0 1 1 1 1 1
0 1 1 1 0 0 0
1 0 0 0 1 1 0
1 1 1 0 0 1 1
A
z }| {
p q p⊕q ¬A
d) 0 0 0 1
0 1 1 0
1 0 1 0
1 1 0 1
186 Selected Solutions
B D
A
z }| { z }| { C
z }| { z }| {
p q r p→q A→r q→r p→C
0 0 0 1 0 1 1
0 0 1 1 1 1 1
4. 0 1 0 1 0 0 1
0 1 1 1 1 1 1
1 0 0 0 1 1 1
1 0 1 0 1 1 1
1 1 0 1 0 0 0
1 1 1 1 1 1 1
Since the truth tables for the expressions (columns B and D) are different, they are
not equivalent. As our counterexample take for instance: p = q = r = 0. Thus → is
not associative.
What about ↔?
5.
a) p ∨ q
5 b) ¬ p → q
6. The four propositions are:
a) Galileo was not falsely accused and the Earth is the centre of the universe.
b) If the Earth moves then the Earth is not the centre of the universe.
c) The Earth moves if and only if the Earth is not the centre of the universe.
d) If the Earth moves the Galileo was falsely accused, but if the Earth is the centre
of the universe then Galileo was not falsely accused.
7.
a)
Converse If Sinterklaas brings you toys, you are good.
Contrapositive If Sinterklaas does not bring you toys, you are not good.
b)
Converse If you need extra postage, then the package weighs more than one kilo.
Contrapositive If you do not need extra postage, then the package does not weigh
more than one kilo.
c)
Converse If I don’t eat courgette, I have a choice.
Contrapositive If I eat courgette, I don’t have a choice.
8.
a) The only card that satisfies this is the ten of hearts.
b) An ordinary deck has 4 cards that satisfy the condition of being a ten,
and 13 cards that satisfy the condition of being a heart, the ten of hearts
187
has been counted twice. So the total the amount of cards that satisfy all
conditions is 16.
c) All cards that are not a ten will satisfy this condition, as well as the cards
that are a ten and a heart, which is only the ten of hearts. So only three
cards do not satisfy this condition, which are the ten of diamonds, ten of
spades, and ten of clubs.
d) It’s easier to reason about the cards that do not satisfy the condition. All
cards that are a ten and not a heart do not satisfy the condition, as well as
all cards that are a heart and not a ten. There are 3 cards that are a ten and
not a heart (ten of diamonds, ten of spades, and ten of clubs). There are
12 cards that are a heart and not a ten. So the total amount of cards that do
not satisfy this condition is 15, which means that there are 37 cards that
satisfy the condition.
9. A ∗ is used to indicate the step at which we can stop rewriting, as the equation
will use ↓ or other operators shown previously.
1.
¬ p ≡ (¬ p ∧ ¬ p)
≡ ¬( p ∨ p) 5
≡p↓p
2.
p ∧ q ≡ ¬(¬ p ∨ ¬q)
≡ ¬ p ↓ ¬q (∗)
≡ ( p ↓ p) ↓ (q ↓ q)
3.
p ∨ q ≡ ¬¬( p ∨ q)
≡ ¬( p ↓ q) (∗)
≡ ( p ↓ q) ↓ ( p ↓ q)
4.
p → q ≡ ¬p ∨ q (∗)
≡ (¬ p ↓ q) ↓ (¬ p ↓ q)
≡ (( p ↓ p) ↓ q) ↓ (( p ↓ p) ↓ q)
5.
p ↔ q ≡ ( p → q) ∧ (q → p) (∗)
≡ ((( p ↓ p) ↓ q) ↓ (( p ↓ p) ↓ q)) ∧ (((q ↓ q) ↓ p) ↓ ((q ↓ q) ↓ q))
≡ ((((( p ↓ p) ↓ q) ↓ (( p ↓ p) ↓ q)) ↓ ((( p ↓ p) ↓ q) ↓ (( p ↓ p) ↓ q)))
↓ ((((q ↓ q) ↓ p) ↓ ((q ↓ q) ↓ p)) ↓ (((q ↓ q) ↓ p) ↓ ((q ↓ q) ↓ p))))
188 Selected Solutions
6.
p ⊕ q ≡ ( p ∧ ¬q) ∨ (¬ p ∧ q) (∗)
≡ ( p ∧ (q ↓ q)) ∨ (( p ↓ p) ∧ q)
≡ (( p ↓ p) ↓ ((q ↓ q) ↓ (q ↓ q))) ∨ ((( p ↓ p) ↓ ( p ↓ p)) ↓ (q ↓ q))
≡ (((( p ↓ p) ↓ ((q ↓ q) ↓ (q ↓ q))) ↓ ((( p ↓ p) ↓ ( p ↓ p)) ↓ (q ↓ q)))
↓ ((( p ↓ p) ↓ ((q ↓ q) ↓ (q ↓ q))) ↓ ((( p ↓ p) ↓ ( p ↓ p)) ↓ (q ↓ q))))
10.
a) A truth table for a formula containing two unique atoms will have 4 rows.
Each of these 4 rows could have either a 0 or a 1 as result. Which means
that there will be 24 unique truth tables for a formula containing 2 unique
atoms.
p q p ∧ ¬ p p ∧ q p ∧ ¬q p ¬ p ∧ q q ¬( p ↔ q) p ∨ q
0 0 0 0 0 0 0 0 0 0
b)
0 1 0 0 0 0 1 1 1 1
5 1 0 0 0 1 1 0 0 1 1
1 1 0 1 0 1 0 1 0 1
0 0 1 1 1 1 1 1 1 1
0 1 0 0 0 0 1 1 1 1
1 0 0 0 1 1 0 0 1 1
1 1 0 1 0 1 0 1 0 1
c) Given that we can create a formula for each of the possible truth table
n
using these 5 operators. Furthermore, we know that there are only 22
possible truth tables, where n is the number of unique atoms. Given that
every one of these truth tables has a corresponding formula using only
n
the 5 operators, which we denote as f i , where 1 ≤ i ≤ 22 − 1. Now take
′
a formula, f , that uses the same unique atoms, but not necessarily the
same operators from the set of 5 operators stated in the description. The
truth table for the formula f ′ will be equal to one of the 22 possible truth
n
Solutions 2.2
a) q → p b) F c) ¬ p
d) ¬ p ∧ q e) T f) q
11. When translating try make the English sentences flow a bit without adding in
more constraints. For example by using the word ‘but’ rather than ‘and’ in two
of the examples below.
a) It is not sunny or it is not cold.
b) I will have neither stroopwafels nor appeltaart.
c) It is Tuesday today, but this is not Belgium.
d) You passed the final exam, but you did not pass the course.
Solutions 2.4
1.
a) ∃ x ( P( x )) b) ∀ x (¬ P( x ) ∨ ¬ Q( x ))
c) ∃z( P(z) ∧ ¬ Q(z)) d) (∃ x (¬ P( x ))) ∨ (∃y(¬ Q(y)))
e) ∃ x ∀y¬ P( x, y) f) ∀ x (¬ R( x ) ∨ ∃y¬S( x, y)) 5
g) ∀y((¬ P(y) ∧ Q(y)) ∨ ( P(y) ∧ ¬ Q(y))) h) ∃ x ( P( x ) ∧ (∀y¬ Q( x, y)))
8. We use the predicates Ball( x ) for x is a ball and Have( x, y) for x must have y. We
also use a constant you to represent you.
13. Using the predicates Person( x ) for x is a person, Question( x ) for x is a question,
Answer( x ) for x is an answer, and Has( x, y) for x has y. Two different interpret-
ations are:
In other words, there is a single person who has the answers to all questions.
In other words, every question has an answer and some person knows it (but differ-
ent people might know the answer to different questions).
Solutions 2.5
B
A
z }| { z }| {
p q p→q A ∧ ¬q B → ¬p
0 0 1 1 1
0 1 1 0 1
1 0 0 0 1
1 1 1 0 1
To verify the validity of the Law of Syllogism, construct another truth table to
show that (( p → q) ∧ (q → r )) → ( p → r ) is also a tautology.
2. Note that the following answers are example arguments, and that there exist
many other valid answers.
3. For both arguments when p is false and q is true the premises hold but the con-
clusion does not.
• When it rains the ground is wet. The ground is wet. Therefore it rains.
• When I am on a boat i am not on land. I am not on a boat. Therefore I am on
land.
4. For this set of solutions, remember that you a slightly less formal method would
be to use a truth table to prove the validity of arguments in propositional logic.
If you can show that in all rows where all premises are true, the conclusion is
also true, then the argument must be valid!
a) Invalid. A counterexample for this argument is when p and q are false
and s is true.
b) Valid.
c) Valid.
d) Valid.
p (premise) (5.1)
p → (t → s) (premise) (5.2)
t→s (from 1 and 2 by modus ponens) (5.3)
q→t (premise) (5.4)
q→s (from 3 and 4 by Law of Syllogism) (5.5)
5.
a) Let s = ”today is a Sunday”, r = ”it rains today” and s = ”it snows today”.
s → (r ∨ s )
s
¬r
∴s
This argument is valid.
b) Let h = ”there is herring on the pizza”, n = ”Jack doesn’t eat pizza”, a =
”Jack is angry”.
h→n
n→a
a
∴h
The argument is invalid since we can’t deduce anything from the fact that
Jack is angry, there might be many more reasons for Jack to get angry.
Note that this exercise becomes harder when we translate to predicate
192 Selected Solutions
logic instead of propositional logic. For predicate logic, ”a” pizza is trans-
lated differently than ”the” pizza.
This exercise in predicate logic would become:
Let P( x ) mean that x is a pizza, let H ( x ) mean that x is a herring, let
On( x, y) mean that x is on y, let E( x, y) mean x eats y and finally let A( x )
mean that x is angry. Let j = Jack.
P( a)
∀ x (( H ( x ) ∧ On( x, a)) → ¬ E( j, a))
¬∃ x (( P( x ) ∧ E( j, x )) → A( j))
A( j)
∴ ∃ x ( H ( x ) ∧ On( x, a))
As an exercise for yourself, try to translate part c to predicate logic as well
as propositional logic.
c) Let a = ”it is 8:00”, l = ”Jane studies at the library” and h = ”Jane works
at home”.
a → (l ∨ h)
a
¬l
5 ∴h
This argument is valid.
Solutions 3.2
Proof. We need to show something is true for all integers r, s, t, so take arbitrary
integers k, m, n such that: k | m, m | n.
Now we need to prove that k | n holds.
Since k | m, we know that m = ak for some integer a. Similarly n = bm for some
integer b.
Thus n = bm = bak = ck with integer c = ba.
Thus k | n.
Since k, m, n were arbitrary, this holds for all integers.
Solutions 3.3
1.
Proof. Assume that all of the numbers ai are smaller than or equal to 10. Since the
maximum value of each ai is 10, the maximum value of a1 + a2 + ... + a10 is now
100. However we asssumed that the summation was strictly larger than 100. This
contradiction means our assumption that all numbers are smaller than or equal to
10 must have been false and so at least one ai must be greater than 10.
193
2.
• a)
Proof. Assume that there exists an integer that is odd whose square is even,
i.e. ∃n ∈ Z ((2 | n2 ) ∧ (2 ∤ n)) ≡ ∃n ∈ Z ((2 ∤ n) ∧ (2 | n2 )). Take an arbitrary
odd integer m = 2p − 1, p ∈ Z. By taking its square, we get
This means that the square is again odd. Because the integer we have taken
was arbitrary, this is the case for all odd integers. From this follows a contra-
diction: in our assumptions, we stated that there must exist an integer that
is odd whose square is even, which can never happen. For this reason, the
assumption is false, and the original claim must be valid.
• b)
√ √
Proof. Assume 2 to be rational. This means that 2 can be written as
√
2=
a
, a, b ∈ Z
5
b
where a and b do not have any common divisors (= the fraction cannot be
simplified). Then:
a2
2= (5.6)
b2
2
2b = a2 (5.7)
This implies that a2 is even. From a), we know that if a2 is even, then a is also
even.
A can now be written as a = 2k, k ∈ Z.
Note: in this case, doing the proof by contradiction and taking the inverse of the state-
ment helped, because we switched from working with an irrational number to working
with fractions of integers.
• c)
194 Selected Solutions
Proof. Assume that the sum of a rational and an irrational number is rational.
Then:
a
r = , a, b ∈ Z, b ̸= 0 (5.11)
b
c
r + x = , c, d ∈ Z, d ̸= 0 (5.12)
d
Where x ∈ R − Z (i.e., is irrational). These fractions cannot be simplified.
Now:
a c
+x = (5.13)
b d
c a
x= − (5.14)
d b
cb − ad
= , db ̸= 0 (5.15)
db
The term cd − ab is an integer, and so is db. This would mean that x is a frac-
tion of 2 integers - in other words, x is rational. Because we assumed x to be
irrational, this is a contradiction. We can therefore conclude that the sum of a
rational and irrational number must be irrational.
5
Note: in this case, doing the proof by contradiction and taking the inverse of the state-
ment helped, because we switched from working with an irrational number to working
with fractions of integers.
• d)
Note: in this case, doing the proof by contradiction and taking the inverse of the state-
ment helped, because we switched from working with an irrational number to working
with fractions of integers.
195
• e)
a c
+x = (5.23)
b d
c a
x= − (5.24)
d b
cb − ad
x= (5.25)
db
The term cb − ad is a nonzero integer, and so is db. This would mean that x is
a fraction of 2 integers - in other words, x is rational. Because we assumed x
5
to be irrational, this is a contradiction. We can therefore conclude that if r and
r + x are both rational, then x is rational.
Note: here, the proof by contradiction did not help as much. Going from rational to
irrational does not bring any improvement, because we only know how not to write
an irrational number.
3.
Proof. Assume that every hole has at most one pigeon in it. This means that there
are < k pigeons in total. Since n > k this forms a contradiction. Therefore our
assumption that every hole has at most one pigeon is incorrect and there must be at
least one hole that has two or more pigeons.
(Take care to flip the quantifiers correctly when doing a proof by contradic-
tion! ¬∀ x (. . . ) becomes ∃ x (¬ . . . ).)
Solutions 3.7
1.
Proof: We prove this once again by induction.
• Base Case: When n = 0 and when n = 1, the statement is clearly true, since
f 0 = 0 < 1 = 20 and f 1 = 1 < 2 = 21 , so f 0 < 20 and f 1 < 21 .
196 Selected Solutions
f k = f k −1 + f k −2
< 2k −1 + 2k −2 (inductive hypothesis)
2k 2k
= +
2 4
3 k
= ·2
4
< 2k
5 • Base Case: When n = 1, a1 = 1 · 21−1 is true since both sides are equal to 1.
• Inductive Case: Let k ≥ 1 be an arbitrary integer. We asssume that an = n2n−1
holds for n = k − 1. To complete the induction, we need to show that the
equation also holds for n = k.
ak = 2ak−1 + 2k−1
= 2( k − 1)2k −1−1 + 2k −1 (inductive hypothesis)
k −1 k −1
= ( k − 1)2 +2
= ((k − 1) + 1)2k−1
= k2k−1
Which proves that the equation also holds for n = k. Thereby the induction is
completed.
Solutions 4.1
• 4: In the case that a ̸= b and a = c. Then the set can be written as:
.
2.
a) A∪B = { a, b, c}; A ∩ B = ∅; A ∖ B = { a, b, c}
b) A∪B = {1, 2, 3, 4, 5, 6, 8, 10}; A ∩ B = {2, 4}; A ∖ B = {1, 3, 5}
c) A∪B = { a, b, c, d}; A ∩ B = { a, b}; A ∖ B = ∅
d) A∪B = { a, b, { a}, { a, b}}; A ∩ B = {{ a, b}}; A ∖ B = { a, b}
3.
A c B
5
a
b
a)
1 10
A B
2
5 8
4
6
3
b)
A B
a
b
c
c)
198 Selected Solutions
A B
a {a,b} {a}
b
d)
4.
a) X ∩ Y = {5, 6, 7, 8, 9, 10}
b) X ∪ Y = N
c) X ∖ Y = {11, 12, 13, ...}
d) N ∖ Z = {1, 3, 5, ...}
e) X ∩ Z = {6, 8, 10, ...}
f) Y ∩ Z = {0, 2, 4, 6, 8, 10}
5 g) Y ∪ Z = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16...}
h) Z ∖ N = ∅
5. P({1, 2, 3}) = {∅, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}
6.
a) False. Although b is part of the two sets that are elements of A, namely
{b} and { a, b}, the element b itself is not a part of A.
b) False. In order for { a, b} to be a subset of A, both a and b should be ele-
ments of A. Like we specified in a), b ∈ / A, hence { a, b}¬ ⊆ A.
c) True. In order for { a, b} to be a subset of A, both a and b should be ele-
ments of A, which they are.
d) False. Although a and b are both individual elements of B, the combined
set { a, b} is not.
e) False. Although a and {b} are both individual elements of B, the com-
bined set { a, {b}} is not.
f) True. { a, {b}} is an element of B.
7. Yes, this is possible.
8. The sentence ”“She likes dogs that are small, cuddly, and cute” talks about dogs
that are both small, and cuddly, and cute. Hence, the dogs she likes has to be in
the set of small dogs, the set of cuddly dogs and the set of cute dogs. Therefore,
the set of dogs she likes is the intersection of the three sets.
On the other hand, the sentence ”She likes dogs that are small, dogs that are
cuddly, and dogs that are cute” talks about dogs that are either small, cuddly, or
cute. Hence, the set of liked dogs is the union of these three sets.
9. A ∪ A = A (remember that sets have no duplicates)
A ∩ A = A (after all everything in A that is also in A, is everything)
A ∖ A = ∅ (removing from A everything that is in A leaves us nothing)
10. We know that A ⊆ B. This means that each element of A is in B as well. There-
fore, if we take A ∪ B we have a set which is equal to B, as all elements of A are
in B. With the same reasoning, A ∩ B is equal to A, as all elements of A are in B.
Moreover, because of this, A ∖ B renders the empty set, as no elements are left
when removing all elements from A which are in B as well.
11.
12.
13.
200 Selected Solutions
Proof. Let us assume that A ⊆ B. Let us take an arbitrary element a in P( A). Since
a is in the power set of A, it is a subset of A. From a ⊆ A, A ⊆ B and the previous
question, we know that a ⊆ B. Because of this, a ∈ P( B). Because a is an arbitrary
element from P( A), this is true for all elements in P( A) and hence, P( A) ⊆ P( B).
14.
15.
Proof. Let C (ϕ) denote the number of connectives in ϕ, and V (ϕ) denote the number
of propositional variables. Let P(ϕ) be the statement that V (ϕ) ≤ C (ϕ) + 1, i.e. the
5 number of propositional variables is at most one more than the number of connect-
ives.
Base Case: V ( x ) = 1
By the Atoms rule by the definition of PROP, C (ϕ) = 0. Therefore, the number
of variables (1) is obviously at most one more than the number of connectives (0).
Therefore, P(ϕ) holds.
Inductive Case:
Assume that we have two formulas x, y ∈ PROP, for which P( x ) and P(y) are true,
i.e. V ( x ) ≤ C ( x ) + 1 and V (y) ≤ C (y) + 1. We want to show that P(¬ x ) and P( x ∗ y)
for ∗ ∈ {→, ∧, ∨} holds as well. We will split the prove into a proof for the negation
and the other connectives:
• ¬:
We want to show that P(¬ x ) holds.
IH
V (¬ x ) = V ( x ) ≤ C ( x ) + 1 ≤ C ( x ) + 2 = C (¬ x ) + 1
. From this, we see that V (¬ x ) ≤ C (¬ x ) + 1, hence P(¬ x ) holds.
• →, ∧, ∨:
In this case, we want to show that P( x ∗ y) holds for ∗ ∈ {→, ∧, ∨}.
IH
V ( x ∗ y) = V ( x ) + V (y) ≤ C ( x ) + 1 + C (y) + 1 = C ( x ) + C (y) + 2 = C ( x ∗ y) + 1
This shows that V ( x ∗ y) ≤ C ( x ∗ y) + 1, and hence, P( x ∗ y) is true for ∗ ∈
{→, ∧, ∨}.
Altogether, we have shown that P( x ) holds for an atom x and that for all x, y ∈
PROP and ∗ ∈ {→, ∧, ∨, ¬}, P( x ) ∧ P(y) → P(¬ x ) ∧ P( x ∗ y). Therefore, by the
principle of structural induction, P(ϕ) is true for all ϕ ∈ PROP, so for all proposi-
tional formula the number of propositional variables is at most one more than the
number of connectives. This completes the proof by structural induction.
201
Solutions 4.2
1.
x ∈ A ∪ (B ∪ C) ↔ x ∈ A ∨ (x ∈ B ∨ x ∈ C) (definition of ∪)
↔ ( x ∈ A ∨ x ∈ B) ∨ x ∈ C (associativity of ∨)
↔ x ∈ ( A ∪ B) ∪ C (definition of ∪)
x ∈ A ∩ (B ∩ C) ↔ x ∈ A ∧ (x ∈ B ∧ x ∈ C) (definition of ∩)
↔ ( x ∈ A ∧ x ∈ B) ∧ x ∈ C (associativity of ∧)
↔ x ∈ ( A ∩ B) ∩ C (definition of ∩)
2.
x ∈ A → x ∈ A∨x ∈ B 5
↔ x ∈ A∪B (definition of ∪)
3.
A △ B ↔ { x | ( x ∈ A) ⊕ ( x ∈ B)} (definition of △)
↔ { x | ( x ∈ A ∧ ¬( x ∈ B)) ∨ (¬( x ∈ A) ∧ x ∈ B)} (definition of ⊕)
↔ { x | ( x ∈ A ∧ x ̸∈ B) ∨ ( x ̸∈ A ∧ x ∈ B)} (definition of ̸∈)
↔ { x | ( x ∈ A ∖ B) ∨ ( x ∈ B ∖ A)} (definition of ∖)
↔ ( A ∖ B) ∪ ( B ∖ A) (definition of ∪)
A B
C
4.
202 Selected Solutions
5.
A ↔ { x ∈ U | x ̸∈ A} (definition of complement)
↔ {x ∈ U | ¬( x ∈ A)} (definition of ̸∈)
↔ {x ∈ U | ¬( x ̸∈ A)} (definition of complement)
↔ {x ∈ U | ¬¬( x ∈ A)} (definition of ̸∈)
↔ {x ∈ U | x ∈ A} (definition of double negation)
↔A
A ∪ A ↔ { x ∈ U | x ∈ A ∨ x ∈ A} (definition of ∪)
↔ { x ∈ U | x ∈ A ∨ x ̸∈ A} (definition of complement)
↔ { x ∈ U | x ∈ A ∨ ¬( x ∈ A)} (definition of ̸∈)
↔ {x ∈ U | T} (excluded middle (p ∨ ¬ p ≡ T))
↔U
5 6.
A ∩ B ↔ { x ∈ U | x ̸∈ A ∩ B} (definition of complement)
↔ {x ∈ U | ¬( x ∈ A ∩ B)} (definition of ̸∈)
↔ {x ∈ U | ¬( x ∈ A ∧ x ∈ B)} (definition of ∩)
↔ {x ∈ U | (¬( x ∈ A)) ∨ (¬( x ∈ B))} (DeMorgan’s Law for logic)
↔ {x ∈ U | ( x ̸∈ A) ∨ ( x ̸∈ B)} (definition of ̸∈)
↔ { x ∈ U | ( x ∈ A) ∨ ( x ∈ B)} (definition of complement)
↔ A∪B (definition of ∪)
7.
a)
( p ∧ q) ↔ p ≡ (( p ∧ q) → p) ∧ ( p → ( p ∧ q)) (definition of ↔)
≡ (¬( p ∧ q) ∨ p) ∧ (¬ p ∨ ( p ∧ q)) (definition of →)
≡ (¬ p ∨ ¬q ∨ p) ∧ ((¬ p ∨ p) ∧ (¬ p ∨ q)) (DeMorgan’s law and Distributive law)
≡ T ∧ (¬ p ∨ q) (p ∨ ¬ p ≡ T)
≡p→q (definition of →)
( p ∨ q) ↔ q ≡ (( p ∨ q) → q) ∧ (q → ( p ∨ q)) (definition of ↔)
≡ (¬( p ∨ q) ∨ q) ∧ (¬q ∨ ( p ∨ q)) (definition of →)
≡ (¬ p ∨ q) ∧ T (DeMorgan’s law and Distributive law)
≡p→q
b) In order to show that these three statements are equivalent, we show that
A ⊆ B → A ∩ B = A, A ∩ B = A → A ∪ B = B, and A ∪ B = B → A ⊆ B:
203
• A ⊆ B → A ∩ B = A:
We show this by contradiction, and therefore assume that A ⊆ B and that
A ∩ B ̸= A. Because of the latter, we know that there is an element in A which
is not in B. However, this contradicts our assumption that A ⊆ B, hence we
know that the original implication is true.
• A ∩ B = A → A ∪ B = B:
We show this by contradiction, and therefore we assume that A ∩ B = A and
A ∪ B ̸= B. From the latter, we know that there now exists an element in
A which is not in B, lets say x. However, this means that x should also be
excluded from A ∩ B, and hence A ∩ B ̸= A, contradicting our assumption.
Therefore, the original implication is true.
5
• A ∪ B = B → A ⊆ B:
We show this by contradiction, and therefore assume that A ∪ B = B and that
¬( A ⊆ B). Because of the latter, there exists at least one element, let say x,
such that x ∈ A and x ̸∈ B. This means that C = A ∖ B ̸= ∅. However,
this means that A ∪ B = B ∪ C. This contradicts our other assumption that
A ∪ B = B, which states that A ∪ B only contains elements of B, whereas
we have derived that this is not possible. Because of this contradiction, we
conclude that A ∪ B = B → A ⊆ B.
Because we have shown that all three implications hold, we have now
shown that the three statements are logically equivalent.
204 Selected Solutions
8.
A ↔ { x ∈ U | x ̸∈ A} (definition of A)
↔ { x | x ∈ U ∧ x ̸∈ A}
↔U∖A (definition of ∖)
C ∖ ( A ∪ B) ↔ { x | x ∈ C ∧ x ̸∈ ( A ∪ B)} (definition of ∖)
↔ { x | x ∈ C ∧ ¬( x ∈ ( A ∪ B))} (definition of ̸∈)
↔ { x | x ∈ C ∧ ¬( x ∈ A ∨ x ∈ B)} (definition of ∪)
↔ { x | x ∈ C ∧ ( x ̸∈ A ∧ x ̸∈ B)} (DeMorgan’s law)
↔ { x | ( x ∈ C ∧ x ̸∈ A) ∧ ( x ∈ C ∧ x ̸∈ B)} (Distributive law)
↔ { x | ( x ∈ C ∖ A) ∧ ( x ∈ C ∖ B)} (definition of ∖)
↔ { x | x ∈ (C ∖ A) ∩ (C ∖ B)} (definition of ∩)
↔ (C ∖ A) ∩ (C ∖ B)
5 C ∖ ( A ∩ B) ↔ { x | x ∈ C ∧ x ̸∈ ( A ∩ B)} (definition of ∖)
↔ { x | x ∈ C ∧ ¬( x ∈ ( A ∩ B))} (definition of ̸∈)
↔ { x | x ∈ C ∧ ¬( x ∈ A ∧ x ∈ B)} (definition of ∩)
↔ { x | x ∈ C ∧ ( x ̸∈ A ∨ x ̸∈ B)} (DeMorgan’s law)
↔ { x | ( x ∈ C ∧ x ̸∈ A) ∨ ( x ∈ C ∧ x ̸∈ B)} (Distributive law)
↔ { x | ( x ∈ C ∖ A) ∨ ( x ∈ C ∖ B)} (definition of ∖)
↔ { x | x ∈ (C ∖ A) ∪ (C ∖ B)} (definition of ∪)
↔ (C ∖ A) ∪ (C ∖ B)
9.
A ∪ ( A ∩ B) ↔ ( A ∪ A) ∩ ( A ∪ B) (Distributive law)
↔ A ∩ ( A ∪ B) (Idempotent law)
↔A (A ⊆ A ∪ B (see 2))
10.
a)
X ∪ (Y ∪ X ) ↔ ( X ∪ ( X ∪ Y )) (Commutative law)
↔ (X ∪ X) ∪ Y (Associative law)
↔ X∪Y (Idempotent law)
205
b)
( X ∩ Y ) ∩ X ↔ (Y ∩ X ) ∩ X (Commutative law)
↔ Y ∩ (X ∩ X) (Associative law)
↔Y∩∅ (Miscellaneous law A ∩ A = ∅)
↔∅ (Miscellaneous law A ∩ ∅ = ∅)
c)
( X ∪ Y ) ∩ Y ↔ ( X ∩ Y ) ∪ (Y ∩ Y ) (Distribution law)
↔ (X ∩ Y) ∪ ∅ (Miscellaneous law A ∩ A = ∅)
↔ X∩Y (Miscellaneous law A ∪ ∅ = A)
d)
( X ∪ Y ) ∪ ( X ∩ Y ) ↔ ( X ∪ ( X ∩ Y ) ∪ (Y ∪ ( X ∩ Y )) (Distributive law)
↔ (( X ∩ X ) ∪ ( X ∩ Y )) ∪ ((Y ∩ X ) ∪ (Y ∩ Y )) (Distributive law)
↔ ( X ∪ ( X ∩ Y )) ∪ ((Y ∩ X ) ∪ Y ) 5
(Idempotent law)
↔ X∪Y (A ⊆ A ∪ B (see 2))
11.
a)
b)
c)
d)
e)
f)
12.
Proof. We give a proof by induction. In the base case, n = 2, the statement is that
X1 ∩ X2 = X1 ∪ Xn . This is true since it is just an application of DeMorgan’s law for
two sets.
For the inductive case, suppose that the statement is true for n = k. Hence, we
assume the induction hypothesis: X1 ∩ X2 ∩ · · · ∩ Xk = X1 ∪ X2 ∪ · · · ∪ Xk , for X1 ,
X2 , …, Xk+1 being any k + 1 sets. Then we have:
X1 ∩ X2 ∩ · · · ∩ X k + 1 = ( X1 ∩ X2 ∩ · · · ∩ X k ) ∩ X k + 1
= ( X1 ∩ X2 ∩ · · · ∩ X k ) ∪ X k + 1
= ( X1 ∪ X2 ∪ · · · ∪ X k ) ∪ X k + 1 (IH)
5 = X1 ∪ X2 ∪ · · · ∪ X k + 1
In this computation, the second step follows by DeMorgan’s Law for two sets, while
the third step follows from the induction hypothesis. Therefore by the principle of
induction we have proven the theorem.
13.
• For any natural number n ≥ 2 and any sets Q, P1 , P2 , . . . , Pn : Q ∩ ( P1 ∪ P2 ∪
. . . ∪ Pn ) = ( Q ∩ P1 ) ∪ ( Q ∩ P2 ) ∪ . . . ∪ ( Q ∩ Pn )
Proof. We proof this by using induction. In the base case, n = 2, the statement
is that Q ∩ ( P1 ∪ P2 ) = ( Q ∩ P1 ) ∪ ( Q ∩ P2 ). This is true since this is just an
application of the Distributive law for three sets.
For the inductive case, suppose that the statement is true for n = k, where k
is an arbitrary integer bigger or equal to 2. Hence, we assume the induction
hypothesis: Q ∩ ( P1 ∪ P2 ∪ . . . ∪ Pk ) = ( Q ∩ P1 ) ∪ ( Q ∩ P2 ) ∪ . . . ∪ ( Q ∩ Pk ),
for Q, P1 , P2 , . . . Pk+1 being any k + 2 sets. Then we have:
Q ∩ ( P1 ∪ P2 ∪ . . . ∪ Pk+1 ) = ( Q ∩ ( P1 ∪ P2 ∪ . . . ∪ Pk )) ∪ ( Q ∩ Pk+1 )
= (( Q ∩ P1 ) ∪ ( Q ∩ P2 ) ∪ . . . ∪ ( Q ∩ Pk )) ∪ ( Q ∩ Pk+1 ) (IH)
= ( Q ∩ P1 ) ∪ ( Q ∩ P2 ) ∪ . . . ∪ ( Q ∩ Pk+1 )
In this computation, the second step follows by Distributive law for three sets,
while the third step follows from the induction hypothesis. Therefore by the
principle of induction we have proven the theorem.
Proof. We proof this by using induction. In the base case, n = 2, the statement
is that Q ∪ ( P1 ∩ P2 ) = ( Q ∪ P1 ) ∩ ( Q ∪ P2 ). This is true since this is just an
application of the Distributive law for three sets.
For the inductive case, suppose that the statement is true for n = k, where k is
an arbitrary integer bigger or equal to 2. Q ∪ ( P1 ∩ P2 ∩ . . . ∩ Pk ) = ( Q ∪ P1 ) ∩
( Q ∪ P2 ) ∩ . . . ∩ ( Q ∪ Pk ), for Q, P1 , P2 , . . . Pk+1 being any k + 2 sets. Then we
have:
Q ∪ ( P1 ∩ P2 ∩ . . . ∩ Pk+1 ) = ( Q ∪ ( P1 ∩ P2 ∩ . . . ∩ Pk )) ∩ ( Q ∪ Pk+1 )
= (( Q ∪ P1 ) ∩ ( Q ∪ P2 ) ∩ . . . ∩ ( Q ∪ Pk )) ∩ ( Q ∪ Pk+1 ) (IH)
= ( Q ∪ P1 ) ∩ ( Q ∪ P2 ) ∩ . . . ∩ ( Q ∪ Pk+1 )
In this computation, the second step follows by Distributive law for three sets,
while the third step follows from the induction hypothesis. Therefore by the
principle of induction we have proven the theorem.
Solutions 4.4
5
1.
A × B = {(1, a), (1, b), (1, c), (2, a), (2, b), (2, c), (3, a), (3, b), (3, c), (4, a), (4, b), (4, c)}
B × A = {( a, 1), ( a, 2), ( a, 3), ( a, 4), (b, 1), (b, 2), (b, 3), (b, 4), (c, 1), (c, 2), (c, 3), (c, 4)}
2.
3.
B A ={{( a, 0), (b, 0), (c, 0)}, {( a, 0), (b, 0), (c, 1)}, {( a, 0), (b, 1), (c, 0)}, {( a, 0), (b, 1), (c, 1)},
{( a, 1), (b, 0), (c, 0)}, {( a, 1), (b, 0), (c, 1)}, {( a, 1), (b, 1), (c, 0)}, {( a, 1), (b, 1), (c, 1)}}
4.
a) f is not onto, as there exists no element x in Z such that f ( x ) = 2x = 3,
because this means that x = 1.5, which is not an integer. However, it is
one-to-one. Take two arbitrary a and b such that f ( a) = f (b). Hence,
2a = 2b, which can only be true if a = b.
b) g is onto; take an arbitrary y in Z. Then there exists an x for which g( x ) =
y, namely x = y − 1 (g( x ) = g(y − 1) = y − 1 + 1 = y), which is integer
and thus in Z. Moreover, g is one-to-one as well. Take two arbitrary a and
b such that g( a) = g(b). Hence a + 1 = b + 1, which can only be true
when a = b.
c) h is not onto, as there exists no element x in Z such√
that h( x ) = x2 + x +
1 = 4. This is because x2 + x + 1 = 4 ↔ x = ± 13−1
2 , which is not an
208 Selected Solutions
6.
a) To prove: g ◦ f is one-to-one → f is one-to-one
b
Couldn’t find your answer here? Feel free to submit your own for
future editions of the book here: https://gitlab.ewi.tudelft.
nl/reasoning_and_logic/book_solutions. We will add your
name to the list of contributors for the book if we accept your
answers!
209
Contributors to Solutions
Max van Deursen,
Kevin Chong,
Julian Kuipers,
Pia Keukeleire,
Philippos Boon Alexaki,
Thijs Schipper
5
Further Reading
The books by Christian & Griffiths, Hofstadter and Smullyan are on recre-
ational maths. Hofstadter’s is a winner of the Pulitzer Prize. Besides Dowek,
the other titles are textbooks; Hammack’s book is an Open Textbook, just
like this book is.
[1] Christian, B. and Griffiths, T. (2016). Algorithms to Live By: The Com-
puter Science of Human Decisions. New York, NY: Henry Holt and Co.
[6] Hammack, R. (2018). Book of Proof, 3rd edition. Richmond, VA: Vir-
ginia Commonwealth University. www.people.vcu.edu/~rhammack/
BookofProof3/
[7] Hofstadter, D. (1979). Gödel, Escher, Bach: An Eternal Golden Braid. New
York, NY: Basic Books.
[8] Huth, M. and Ryan, M. (2004). Logic in Computer Science, 2nd edition.
Cambridge, UK: Cambridge University Press.
[9] Smullyan, R. M. (1979). What is the Name of This Book? Upper Saddle
River, NJ: Prentice-Hall.
211
Index
213
contradiction, 16 empty set, 105
contrapositive, 14 entity, 40
contributors, iv variable, 43
converse, 14 equals sign, 19
coordinates, 114 equivalence class, 168
Coq, 6 equivalence relation, 66, 168
corollary, 161 Euclid, 62, 76
countable set, 158 even, 70
countably infinite, 158 excluded middle, law of, 25
counterexample, 57, 67 exclusive or operator, 12
cross product, 114 existence proof, 67
Cruyff, Johan, 8 existential quantifier, 43
Explosion, Principle of, see Principle
DAG, see directed acyclic graph of Explosion
database, 165, 172 extensional definition, 106
Database Management System, 172
DBMS, see Database Management Sys-
false (logical value), 19
tem
feedback in circuits, 31
deduction, 5, 52, 53
Feijenoord, see Feyenoord
Delft, 6
Feyenoord, 13, 54
DeMorgan’s Laws, 26, 49, 123
Fibonacci sequence, 91
diagonalization, 160
finite set, 154
Digital Systems, 40
first-class citizen, see first-class objects
direct proofs, 73
first-class object, 146, 150
Directed Acyclic Graph, 131
font, 136
disjoint sets, 109
formal proof, see proof
disjunction, 9
formal structure, 48
disjunctive normal form, 34
free variable, 43
distributive law, 19, 26, 28, 122
divisible by m, 69 Frege, Gottlob, 62, 63, 179
DNF, see disjunctive normal form function, 130, 139, 141
domain, 118, 142 in computer programming, 148
domain of discourse, 41 on graphs, 145
dominos on trees, 144
analogy for mathematical induc- partial, 150
tion, 78 functional programming, 151
double negation, 20 functionally complete, 15
duality, 20 funtionally complete, 15
214
directed acyclic, see directed acyc- left subtree, 97
lic graph lemma, 76
undirected, 130 Linear Algebra, 1
linked data structures, see tree, 93
halting problem, 181 logic circuit, 30
hexadecimal number, 133 and compound propositions, 30
hypothesis, 12, 67 for an input/output table, 34
simplifying, 36
identity law, 25 logic gate, 28–30
if and only if, 14 logical connective, 8
iff, see also if and only if, 68 logical deduction, see deduction
image, 142 logical equivalence, 11, 16, 19, 22, 23
implication, 12 and logical deduction, 55
incompleteness theorems, 179 in predicate logic, 49, 50
indirect proof, 73 logical operator, 8
induction, 77 bitwise, 135
second form, 83 logically implies, 53
induction hypothesis, 79 loop invariant, see invariant
induction, structural, see structural in- Lucas, Édouard, 89
duction Luyben, Karel, 13
inductive case, 79
inductive hypothesis, 79 Machine Learning, 2
infinite set, 154 main connective, 10
Information & Data Management, 2, mapping, 139
172 mathematical generality, 7
injective, 143 mathematical induction, 77
integers, 69 mathematical structure, see formal struc-
intensional definition, 107 ture
intersection, 108, 121 member (of a set), 105
invalid argument, 57 modus ponens, 54, 59
invariant, 100, 116 modus tollens, 54, 59
inverse, 14
irrational numbers, 69 NAND gate, 30
iteration, 100 natural language, 7
natural numbers, 69
Java, 8, 11, 87, 97, 133, 134, 148 necessary, 13
JavaScript, 151 necessity, see necessary
negation, 9
Karnaugh-map, 37 of quantified statements, 49
Khan Academy, 78 neighbour, 130
node, 94, 130
laws of Boolean Algebra, 20 internal, 118
leaf, 94 leaf, 99, 118
215
non-Euclidean geometry, 62 Prolog, 86, 100
NOR gate, 30 Prometheus, 13
not (logical operator), 8 proof, 7, 21, 56, 62
NOT gate, 29 by contradiction, 74
null pointer, 97 by contrapositive, 73
by division into cases, 73
Object-Oriented Programming, 11, 148 by generalisation, 67
obviousness, 21, 64 examples, 70
octal number, 138 proof assistants, 6
odd, 70 proper subset, 106
one-place predicate, 41 proposition, 6, 7
one-to-one, 143 equivalent to one in DNF, 35
one-to-one correspondence, 153 for a logic circuit, 31
onto function, 143 propositional logic, 6, 7, 40
open statement, 43 propositional variable, 21
or (logical operator), 8, 121 prototype, 149
inclusive vs. exclusive, 15 Python, 151
OR gate, 29
ordered n-tuple, 114 quantifier, 7
ordered pair, 113 in English, 42
ordered triple, 114 on a two-place predicate, 43
quantifiers, 42
parameter, 149 query language, 172
parentheses, 9, 22, 48, 127 query, database, 172
partial function, 150
partial order, 166 range, 142
partition, 167 rational numbers, 69
path, 130 real numbers, 69
Peirce, Charles, 42, 63 Reasoning & Logic, 1–3, 6, 26, 29, 34,
pigeonhole principle, 77 37, 52, 62, 66, 69, 114, 130,
power set, 111 132, 148, 160
precedence rule, 9, 12, 127 recursion, 86
predicate, 6, 40, 41, 107 recursive definition, 91
predicate calculus, 40 recursive definition, see recursive
predicate logic, 40 definition
premise, 5, 52 recursive subroutine, 86
primary key, 173 reflexive relation, 165
prime, 70 relation, 164, 172
Principle of Explosion, 17, 28 relational database, 172
principle of mathematical induction right subtree, 97
see mathematical induction, 78 root, 94, 97
Probability Theory & Statistics, 1 round brackets, see parentheses
product (of sets), 114 Rubik’s Cube, 169
216
Russell’s Paradox, 62, 112 true (logical value), 19
Russell, Bertrand, 62, 112, 179 truth table, 10, 20, 23
and logic circuits, 33
search tree, 99 of a tautology, 16
set, 103 TU Delft, 1, 13, 51
of functions from A to B, 147 tuple, 48, 114
set difference, 108 tuples, 48
set theory, 103 Turing machine, 180
set-builder notation, 107 Turing, Alan, 179, 180
sets, 103
simple term, 33 uncountable set, 158
Sinterklaas, 14, 17 unicorn, 17, 28, 52
situation, 10 union, 108, 121
Socrates, 5 universal quantifier, 43
SQL, see Structured Query Language universal set, 122
strong induction, see induction, second
form valid argument, 54, 56
structural induction, 115 value (of a function), 140
Structured Query Language, 172 variable, 43, 50
subroutine, 86 propositional, 7
subset, 106 Venn diagram, 104, 109
substitution law, 22 vertex, 130
subtree, 94 vertices, 130
sufficiency, see sufficient von Neumann, John, 180
sufficient, 12
Web & Database Technology, 172
summation notation, 81
wires in computers, 29
surjective, 143
syllogism, 54 Zadeh, Lotfi, 26
symmetric difference, 128
symmetric relation, 166
Tarski’s world, 47
Tarski, Alfred, 26, 47
tautology, 16, 22, 24, 50
ternary relation, 165
topological ordering, 131
total function, 150
total order, 167
Towers of Hanoi, 88
transitive closure, 170
transitive relation, 166
tree, 93, 116, 117, 129
trees, 93
217
Delftse Foundations of Computation
Stefan Hugtenburg and Neil Yorke-Smith
Stefan Hugtenburg
TU Delft | Faculty of Electrical Engineering, Mathematics
and Computer Science: Software Technology, Distributed
Systems
Neil Yorke-Smith
TU Delft | Faculty of Electrical Engineering, Mathematics
and Computer Science: Software Technology,
Algorithmics