comp106_1_logic
comp106_1_logic
artificial intelligence
computer programming and
algorithm development
design of computer circuits
etc
By the end of these lectures, you will be able
e.g.
Snow is white
Rain is wet
Propositions
1+1=2
2+3=7
Truth tables:
p q ¬p pq pq pq p→q p↔q
T T F T T F T T
T F F F T T F F
F T T F T T T F
F F T F F F T T
The Connective Or in English
p q pq pq
T T T F
T F T T
F T T T
F F F F
Implication (IF)
p q p→q
T T T
T F F
F T T
F F T
p→q
p: premise (hypothesis)
q: conclusion (consequence)
Understanding Implication
In p → q, there does not need to be any semantical connection between the premise
and the conclusion. The “truthness” of p → q depends only on the truth values of p
and q.
These implications are perfectly fine, but would not be used in ordinary English.
• “If the moon is made of green cheese, then I have more money than Bill Gates.”
• If this statement is T, it means that to get 100% in the final is sufficient to get an
A, but not necessary.
• So even if you don’t get 100% in the final, you may still get an A.
• Suppose that you get 100% in the final and do not get an A.
• This corresponds to the case where p is true and q is false
• Hence, you understand that p → q was false (so you can object to your letter grade)
Hence, we should consider all possible truth value assignments to analyze the “meaning”
of a compound statement such as an implication.
Understanding Implication
“If you are older than 18 years, then you have a driving license” (p → q)
• This statement does not tell anything about what happens when you are not older
than 18 years.
• So if you are not older than 18 years, you may or may not have a driving license
(we do not know).
• Hence, one cannot infer from the given statement the following: “if you have a
driving license, then you are older than 18 years”.
Common ways of expressing implication:
• if p then q
• q if p
• p implies q
• p only if q
• p is sufficient for q
• q whenever p
• q is necessary for p
• q follows from p
• q unless ¬p
p→q
Converse: q → p
Contrapositive: ¬q → ¬p
“If you are older than 18 years, then you can have a driving license”
Converse: “If you can have a driving license, then you are older than 18 years”
Contrapositive: “If you cannot have a driving license, then you are not older
than 18 years”
“If today is Thursday, then we have a quiz”
Converse: “If we have a quiz, then today is Thursday”
Contrapositive: “If we do not have a quiz today, then today is not Thursday”
Inverse: “If today is not Thursday, then we do not have a quiz”
Example:
“You cannot ride the roller coaster if you are less than 1.70m
tall unless you are older than 18 years.”
(r ¬s ) → ¬q
p ¬q
“It’s below freezing or it’s snowing; but it’s not snowing if it’s below
freezing.”
(p q) (p → ¬q)
and ≈ but
* exempli gratia
Logic and bit operations:
Logical equivalence
The propositions r and s are logically equivalent if r and s have the same truth values.
Notation: r s
e.g.
Show that ¬(p q) ¬p ¬q (De Morgan’s law)
p q pq ¬(pq) ¬p ¬q ¬p ¬q
T T T F F F F
T F T F F T F
F T T F T F F
F F F T T T T
Tautology: A compound proposition that is always true
e.g. p ¬p p ¬p
tautology contradiction
Logical Equivalences
pTp
pFp Identity laws
pTT
pFF Domination laws
ppp
ppp Idempotent laws
pqqp
pqqp Commutative laws
(p q) r p (q r)
(p q) r p (q r) Associate laws
p (q r) (p q) (p r)
p (q r) (p q) (p r) Distributive laws
¬(p q) ¬p ¬q
¬(p q) ¬p ¬q De Morgan’s laws
Some additional useful logical equivalences:
p ¬p T
p ¬p F
(p → q) (¬p q)
(p ↔ q) (p → q) (q → p)
(p → q) (¬q → ¬p)
You can show all these logical equivalences using truth tables.
e.g.
Show that ¬(p → q) p ¬q
e.g.
Show that ¬(p ↔ q) (p q) (¬p ¬q)
You can show these logical equivalences using truth tables or using
known equivalences.
Recall:
“If you are older than 18 years, then you can have a driving license” p → q
? “If you can have a driving license, then you are older than 18 years” q→p
or
✔ “If you cannot have a driving license, then you are not older than 18 years” ¬q → ¬p
️
since (p → q) (¬q → ¬p)
Recall:
“If you are older than 18 years, then you can have a driving license” p → q
? “If you can have a driving license, then you are older than 18 years” q→p
- But this doesn’t necessarily mean that q → p can not be concluded from p → q.
- Or equivalently: Is (p → q) → (q → p) a tautology?
e.g.
Is (p → q) → (q → p) a tautology?
We’ll see more on this while studying "rules of inference" (next lectures).
e.g.
Show that (p q) → (p q) is a tautology.
(p q) → (p q) ¬(p q) (p q)
(¬p ¬q) (p q) De Morgan
(¬p p) (¬q q) Assoc & Comm
TT
T
Consistency of Propositions
“Whenever the system software is being upgraded, users cannot access the file system.”
“If users can access the file system, then they can save new files.”
“If users cannot save new files, then the system software is not being upgraded.”
Logic puzzle 2: A father has two children, a boy and a girl. While playing, they both get mud on their
foreheads. The father says “at least one of you have mud on your forehead”. And then asks a yes/no
question: “Do you know if you have a muddy forehead?”. The father asks the question twice. The children
are honest, there is no mirror (they cannot see their own forehead) but they see each other’s forehead, and
they answer simultaneously.
What will the boy and girl’s answers to the question the first time?
What will the boy and girl’s answers to the question the second time?
1.4 Predicates and Quantifiers
“If you are older than 18 years, then you can have a driving license”
Subject Predicate
Notation: x P(x)
“for all x P(x)”
“for every x P(x)”
: Universal quantifier
e.g.
“Every student in this class has studied calculus.”
Then x P(x), where universe of discourse is all the students in the class.
Domain (universe of discourse) must always be specified!
xR P(x)
“for all x in the domain of Real Numbers P(x)”
OR
x (S(x) →P(x))
where
S(x) denotes “x is in the class”
Universe of discourse is the set of all students.
e.g.
What is the truth value of x P(x), where
P(x): “x2 > 0 ”
What about
x≠0 P(x)
DOMAIN IS CRUCIAL!!
Existential Quantification of P(x)
is the proposition “there exists an element x in the universe of discourse (domain)
such that P(x) is true”.
Notation:
x P(x)
“there exists an x s.t. P(x)”
“there is at least one x s.t. P(x)”
: Existential quantifier
e.g.
P(x): x2 > 10, x {1, 2, 3, 4}
e.g.
P(x): x = x +1, x R
x P(x) F
e.g.
P(x):x2 ≥ x
x P(x) if x Z ?
x P(x) if x R ?
Precedence of Quantifiers
The quantifiers and have higher precedence than all the logical operators.
Unfortunately, often people may write x P(x) Q(x) when they mean x (P(x) Q(x)).
DO NOT DO THIS !
Negation of Quantified Statements
e.g.
P(x): x2 > 10, x {1, 2, 3, 4}
Negation of x P(x) ?
e.g.
“Everyone has at least one best friend.”
x y B(x, y)
The Order of Quantifiers
e.g.
x y P(x,y) is not equivalent to yx P(x,y).
x y P(x,y) : “ For every real number x there is a real number y such that x+y=0”
yx P(x,y) : “ There is a real number y such that for every real number x, x+y=0”
“Some student in this class has not solved every exercise in the book.” ?
Not correct! (since it does not exclude the case where a student has solved every exercise)
Let S(x,y) be “student x has solved exercise y”, where the universe of discourse for x is the
set of students in this class, and for y, the set of exercises in the book.
“For every student x in this class, there is an exercise y that student x has not solved.”
Equivalently “No student in this class has solved every exercise in the book.”
e.g. Negate “Some student in this class has solved every exercise in the book.”
What if the universe of discourse for x is the set of all students, and for y, the set of
exercises in the book?
“For every student x, if x is in this class, then there is an exercise y that x has not solved.”
Equivalently “For every student x in this class, there is an exercise y that x has not solved.”
Equivalently “No student in this class has solved every exercise in the book.”
Replacing quantifiers
e.g.
Consider Universe of discourse for x is the set
P(x,y): “Student x has solved question y” of students in this class; for y the set
Q(x): “Student x passes the exam” of questions in the exam
“If a student in this class passes the exam, then that student has solved every question”
x (Q(x) → y P(x,y) )
x y ( Q(x) → P(x,y) )
since
y ( P(x,y) → Q(x) ) is not logically equivalent to (yP(x,y) ) → Q(x)
This is equivalent to say that a student x passes the exam How about this?
if x solves any of the questions.
Be careful when replacing quantifiers!
e.g.
Consider
P(x,y): “Student x has solved question y”
Q(x): “Student x passes the exam”
y ( P(x,y) →Q(x) ): “For every question y, if student x has solved y, x passes the exam.”
( y P(x,y) ) → Q(x): “If student x has solved every question y, x passes the exam.”
In the first case, a student x passes the exam if that student has solved any question;
In the second case, a student passes the exam if that student has solved all the questions.
So, they are not logically equivalent.
e.g. Negate x (yP(x,y) → Q(x) )
Consider
P(x,y): “Student x has solved question y”
Q(x): “Student x passes the exam”
“For every student x in this class, if x has solved every question y in the exam, x passes
the exam.”
x (yP(x,y) ¬Q(x))
“There exists a student in this class, who has solved every question but does not pass the
exam.”
Recall the example:
e.g.
“Everyone has at least one best friend.”
x y B(x, y)
How about “Everyone has exactly one best friend”?
“For every person x, there is a person y such that y is the best friend of x and
(and ??)
for every z, if z is a person other than y, then z is not the best friend of x.”
x y z ( B(x, y) ( (z y) → ¬B(x, z) ) ) This works out this time; but be careful while
replacing quantifiers in general.
e.g. Negate “Everyone has exactly one best friend.”
Universe of discourse is the set of all students in the class (for both x and y).
Let B(x,y): “y is the best friend of x”
x yz ( B(x, y) ((z y) → ¬B(x, z)))
Note that, for a given x, this statement is T when B(x, y) is F for all y, hence when x has no
best friend.
1.6/1.7 Mathematical Reasoning
We will learn:
Rules of reasoning
• used to draw conclusions from other assertions and tie the steps of a proof.
Valid does NOT mean TRUE. Valid argument means that if the premises are true, then the
conclusion is true. A valid argument may contain premises that are false:
e.g.,
“If we live on the Moon, then I am a carpenter. We live on the Moon. Therefore I am a
carpenter”
This is a valid argument, as long as we assume “We live on the Moon” is a true statement.
But the conclusion is not necessarily true since we know that one of the premises is false.
Example (Reasoning)
• “Socrates is a man.”
• “Socrates is mortal.”
(p (p → q)) → q
e.g.
“If it is sunny today, then we’ll play soccer. (given)
It is sunny today. (given)
Another notation:
p
p→q
q
Basic rule of inference: (Modus ponens)
(p (p → q)) → q : Tautology
e.g.
“If it is sunny today, then we’ll play soccer. It is sunny today. So we’ll play soccer”.
p q p→q (p (p → q)) → q
T T T T
T F F T
F T T T
F F T T
e.g.
It is sunny now. Therefore, it’s sunny or snowy now. Tautology
p Addition Rule
pq
e.g.
It is windy and raining now. Therefore, it is windy now. Tautology
pq Simplification Rule
p
Rule of
Inference Tautology Name
p p → (p q) Addition
pq
pq (p q ) → p Simplification
p
p ((p) (q)) → (p q) Conjunction
q
pq
p [p (p → q)] → q Modus Ponens
p→q
q
¬q [¬q (p → q)] → ¬p Modus Tollens
p→q
¬p
p→q [(p → q) (q → r)] → (p → r) Hypothetical
q→r Syllogism
p→r
pq [(p q ) ¬p] → q Disjunctive
¬p Syllogism
q
Fallacies: Types of incorrect reasoning:
e.g.
“If you are older than 18 years, then you can have a driving license. You can have a
driving license. Therefore, you are older than 18 years.”
“If you are older than 18 years, then you can have a driving license. You are not older
than 18 years, so you can’t have a driving license.”
Begging the question fallacy: When one or more steps of a proof are based on the truth
of the statement being proved (circular reasoning)
e.g. Show that the premises
“If you help me, then I will finish writing the program,” “If you do not help me, then I will go to
sleep early,” and “If I go to sleep early, then I will wake up feeling refreshed”
Let
p: “you help me”
q: “I’ll finish writing the program”
r: “I’ll go to sleep early”
s: “I’ll wake up feeling refreshed”
Step Reason
1. p → q premise
2. ¬q → ¬p contrapositive of step 1
3. ¬p → r premise
4. ¬q → r hypothetical syllogism using steps 2, 3
5. r → s premise
6. ¬q → s hypothesis syllogism using steps 4, 5
Rules of Inference for Quantified Statements
Existential instantiation:
Existential generalization:
P(x): “x is a lion”
Q(x): “x is fierce”
R(x): “x drinks coffee”
Universe of discourse is the set of all creatures.
i) x (P(x) → Q(x))
ii) x (P(x) ¬R(x))
i) ii) ? x (Q(x) ¬R(x))
(ii) implies that there is some x1 such that P(x1) ¬R(x1) by Existential Instantiation.
Hence by (i), Q(x1) is true (using Simplification and Modus Ponens rules).
¬R(x1) is also true by using again Simplification rule.
Then Q(x1) ¬R(x1) which implies that x (Q(x) ¬R(x)) by Existential Generalization.
1.8 Methods of Proof
To prove a theorem, you usually start from premises (hypothesis), and using known
theorems, axioms and definitions, you construct a sequence of steps that leads to the
conclusion (forward reasoning).
Direct proof: (forward reasoning)
Example:
nZ, if n is odd, then n2 is odd.
Many theorems are implications: n (P(n) → Q(n))
While doing so, we need to consider only the cases where P(n) is T
since otherwise, the implication will already be T due to the definition
of implication.
Recall:
n (P(n) → Q(n)) n (¬Q(n) → ¬P(n)) p → q ¬q → ¬p (contrapositive)
e.g.
Prove that, nZ, if 3n + 2 is odd, then n is odd.
3n + 2 is even too.
Formally, this needs to be written as
e.g. (iff) n (P(n) ↔ Q(n)); so parantheses are
taken granted.
Prove that, nZ, n is odd if and only if n2 is odd.
P(n): n is odd, Q(n): n2 is odd Prove n P(n) ↔ Q(n)
b2 is even b is even
2 | a 2 | b a and b have common factors
contradiction!
2 is irrational.
WHAT ABOUT 3
http://www.grc.nasa.gov/WWW/k-12/Numbers/Math/Mathematical_Thinking/irrationality_of_3.htm
Proof by cases:
e.g.
x P(x) ? Prove or disprove the existence of an a s.t. P(a) is true, without explicitly
finding it.
e.g., Show that there exists irrational numbers x and y such that xy is rational.
Consider x = 2 and y = 2 . Both are irrational.
xy = 2 2
Case 1: 2 2 is rational. DONE.
Case 2: 2 2 is irrational. Then let x = 2 2 and y = 2 . Thus xy = ( 2 2 ) 2 = 2
which is rational. DONE.
Non-constructive becase we didn’t find an example x and y exactly (we do not know which
case is true, but one of them must be).
Theorems and Quantifiers:
Counter-example:
x P(x)?
Find a for which P(a) is false; which Hence you can use counter-examples to
disprove a given universal quantification.
means (x P(x)) isfalse.
e.g.
Show that ‘all primes are odd’ is false.
x = 2 even and prime
it is false.
Uniqueness proof:
x (P(x) y (y≠x → ¬P(y)) )
e.g., “If a and b are real numbers and a≠0, then there exists a unique real number r such that
ar+b=0”
Part 2 – Uniqueness: Ley y be such that ay+b=0 but y≠r. We know ar+b = 0 = ay+b. Subtracting b
from both sides we get ar=ay. We know a≠0 hence dividing both sides by a we get r=y,
contradicting our assumption that y≠r. Therefore no such y can exist.
2.1 Sets
Set: A fundamental discrete structure upon which all other discrete structures are built.
Sets group objects together.
Definition: Set.
The objects (elements, members) are in a set. A set is said to contain its elements.
e.g. E = {2, 4, 6, 8}
E = {x | x is a positive even integer less than 10}
E = {x | x > 0, x < 10, x=2k, k Z}
Definition: Two sets are equal if and only if they have the same elements.
Definition:
Let S be a set.
If there are exactly n distinct elements in S where n is a nonnegative integer, we say S is a finite set
and n is the cardinality of S.
|S| = n
e.g.
Let S be the set of students in this class.
|S| = ?
e.g.
|| = ?
Definition:
A set is said to be infinite if it is not finite.
e.g.
Let A={1, 2}, B={a, b}
A × B = {(1, a), (1, b), (2, a), (2, b)}
Note: A×B≠B×A
unless A= B= so that A × B is equal to B × A =
or unless A = B
Remember example:
Let A={1, 2}, B={a, b}
A × B = {(1, a), (1, b), (2, a), (2, b)}
Equality of tuples:
(a,b) = (c,d) ↔ a = c b = d
Definition:
The Cartesian product of the sets A1, A2, …, An
denoted by A1 × A2 × … × An, is the set of ordered n-tuples (a1, a2, … , an), where ai belongs to Ai
for i = 1,2,… , n.
Equality of n-tuples:
(a1, a2, … , an) = (b1, b2, … , bn) ↔ i, 1 ≤ i ≤ n, ai = bi
2.2 Set Operations
Definition: Union
Let A and B sets.
A B is the set that contains those elements that are either in A or in B, or in both.
A B = {x | xA xB}
Definition: Intersection
Let A and B be sets.
A B is the set containing those elements that are both in A and B.
A B = {x | x A x B}
A
B
Definition: Complement
Let U be the universal set (domain from which x is coming).
The complement of the set A, denoted by Ā, is the complement of A with respect to U.
Ā = U – A = {x | x A}
Set Identities:
A=A
AU=A identity laws
AU=U
A= domination
AA=A
AA=A idempotent
( A) = A complementation
AB=BA
AB=BA commutative
A (B C) = (A B) C
A (B C) = (A B) C associative
A (B C) = (A B) (A C)
A (B C) = (A B) (A C) distributive property
(AB) = A B
(AB) = A B De Morgan’s rule
n
A1 A2 … An = Ai
i 1
Definition:
The intersection of a collection of sets is the set that contains those elements that are members
of all sets in the collection.
n
A1 A2 … An = Ai
i 1
2.3 Functions :
Definition: Function
Let A and B be sets.
A function from A to B is an assignment of each element of A to exactly one element of B.
ƒ: A → B; ƒ(a) = b
Definition:
Let ƒ be a function from A to B.
A: domain
B: codomain
Let ƒ(a) = b
b is image of a
a is a pre-image of b
ƒ
a b
A B
Definition:
A function ƒ is said to be one-to-one (or injective) iff x, yA (ƒ(x)=ƒ(y) → x=y).
e.g.
ƒ(x) = x2 ƒ(x) = x3 ƒ(x) = x + 2
not 1-to-1 1-to-1 1-to-1
Definition:
ƒ : A→ B A R, B R R: set of real numbers
x, y A,
ƒ is strictly increasing iff ƒ(x) < ƒ(y) for x < y
ƒ is strictly decreasing iff ƒ(x) > ƒ(y) for x < y
Definition:
Definition:
Let ƒ : A → B is a 1-to-1 correspondence.
-1
Inverse function ƒ : B → A such that
ƒ-1 (b) = a where ƒ(a) = b.
ƒ is invertible if and only if it is 1-to-1 and onto.
e.g.
ƒ : R→ R
ƒ(x) = x + 1 Is ƒ(x) invertible?
y = x +1
x=y–1
ƒ-1(y) = x – 1
ƒ(x) is invertible.
Definition: Composition of functions.
Let g be a function from the set A to the set B and let ƒ be a function from the set B to the set C.
composition ƒ○g is defined by
(ƒ○g)(a) = ƒ(g(a))
x x
Properties:
x–1 < x x x < x + 1
–x = – x
–x = – x
x + m = x + m where m is integer
x + m = x + m where m is integer
2.5 Carfinality :
Definition:
The sets A and B have the same cardinality if and only if there is 1-to-1 correspondence from
A to B.
Definition:
Countable set: either has finite cardinality or has the same cardinality as the set of natural
numbers.
e.g. The set of real numbers is not countable (uncountable).
e.g. The set of rational numbers is countable.
2.6 Matrices:
• Two matrices A and B are equal iff they have the same number of rows, the same number
of columns, and all corresponding elements are equal.
3 2 3 2 0
1 6 1 6 0
• A+B = [ai,j+bi,j]
2 6 9 3 11 9
0 8 11 3 11 5
• For an mk matrix A and a kn matrix B, the product AB is an mn matrix. Note that the
dimensions must match!
• Note: Matrix multiplication is not commutative! AB is not necessarily equal to BA (even
if both are defined)
k
AB C [ci , j ] ai ,b, j
1
0 1 1 0
0 1 1 1 0 5 1
2 0 3 2 0 2 0 3 2 11 3
1 0
3 1
More things to know about matrix multiplication:
- It is costly: O(n3) In many computer vision problems we use 1000x1000 images!
- AB are BA both defined only when A and B are of size nm and mn for some n,m.
- It is associative: ABC = (AB)C = A(BC)
- Order matters in terms of number of steps: If A:nm, B:mk, C:kt then (AB)C requires
nmk + nkt steps whereas A(BC) requires mkt + nmt steps!
2 0
t
2 1 3
0 1 2 1 1
3 2
• A square matrix A is symmetric iff A=At. i.e., i,jn: aij = aji .