Logic
Logic
Logic
Niloufar Shafiei
Propositions
Proposition:
n It is a sentence that declares a fact.
n It is either true or false, but not both.
Examples:
2 + 1 = 3.
True Proposition
Toronto is the capital of Canada.
False Proposition
Read this carefully.
Not declarative
x + 1 = 2.
Neither true nor false
2
Propositions
Variables (letters) are used to represent propositions, denoted
propositional variables.
p: Today is sunny.
Example:
p: Today is Friday.
¬p: It is not the case that today is Friday.
Today is not Friday.
4
Examples (negation)
p: It is snowing.
¬p: It is not the case that it is snowing.
It is not snowing.
r: 2+3 = 5.
¬r: It is not the case that 2+3 = 5.
2+3 ≠ 5.
5
Negation
The truth value of the negation of p, ¬p, is the
opposite of the truth value of p.
Example:
p: Today is Friday. F
¬p: Today is not Friday.
T
p ¬p
T F
F T
6
Conjunction
Let p and q be propositions.
The conjunction of p and q, denoted by
p∧q, is the proposition “p and q.”
Example:
p: Today is Friday.
q: It is raining today.
p∧q: Today is Friday and it is raining today.
7
Conjunction(example)
p: Maria takes a discrete mathematics course.
q: Maria takes a statistics course.
p∧q: Maria takes a discrete mathematics course and
Maria takes a statistics course.
r: Henry is happy.
s: Fred is not happy.
r∧s: Henry is happy and Fred is not happy.
Henry is happy, but Fred is not happy.
t: It is below freezing.
u: It is not snowing.
t∧u: It is below freezing and it is not snowing.
It is below freezing, but it is not snowing.
8
Conjunction
The conjunction p∧q is true when both p and q are
true and is false otherwise.
Example:
p: Today is Friday. F
q: It is raining today. T
p∧q: Today is Friday and it is raining today.
F
p q p∧q
T T T
F T F
T F F
F F F
9
Disjunction
Let p and q be propositions.
The disjunction of p and q, denoted by
p∨q, is the proposition “p or q.”
Example:
p: Today is Friday.
q: Today is Monday.
p∨q: Today is Friday or today is Monday.
10
Disjunction(example)
p: I go out tonight.
q: I stay at home.
p∨q: I go out tonight or I stay at home.
11
Disjunction
The disjunction p∨q is false when both p and q are
false and is true otherwise.
Example:
p: Today is Friday. F
q: Today is Monday. T
p∨q: Today is Friday or today is Monday.
T
p q p∨q
T T T
F T T
T F T
F F F
12
Exclusive or
Let p and q be propositions.
The exclusive or of p and q, denoted by p⊕q, is
the proposition “p or q, but not both.”
Example:
p: She wants coffee.
q: She wants tea.
p⊕q: She wants coffee or tea, but not both.
13
Exclusive or
The exclusive or p⊕q is true when exactly one of p and q is
true and is false otherwise.
Example:
p: She wants coffee. F
q: She wants tea . T
p⊕q: She wants coffee or tea, but not both.
T
p q p⊕ q
T T F
F T T
T F T
F F F
14
Exclusive or (example)
exclusive or versus inclusive or (disjunction)
Coffee or tea comes with dinner.
Exclusive or
A password must have at least three digits or be
at least five characters long.
Inclusive or
Lunch includes soup or salad.
Exclusive or
Experience with C++ or Java is required.
Inclusive or
15
Conditional statement
Let p and q be propositions.
The conditional statement p→q is the
proposition “if p, then q”.
p→q
hypothesis conclusion
Example:
p: It snows.
q: I stay at home.
p→q: If it snows, then I stay at home.
16
Conditional statement
p→q
It snows. → I stay at home.
If p, then q.
If it snows, then I stay at home.
p is sufficient for q.
It snows is sufficient for me to stay at home.
q if p.
I stay at home if it snows.
q when p.
I stay at home when it snows.
A necessary condition for p is q.
A necessary condition for me to stay at home is that it snows.
17
Conditional statement
p→q
It snows. → I stay at home.
q unless ¬p.
I stay at home unless it does not snow.
p implies q.
It snows implies I stay at home.
p only if q.
It snows only if I stay at home.
q whenever p.
I stay at home whenever it snows.
q follows from p.
I stay at home follows from it snows.
18
Conditional statement (example)
p: You do every exercise in the book.
q: You get an A in this class.
p→q: If you do every exercise in the book,
then you get an A in this class.
r: It is dark.
s: Hiking is not safe.
r→s: If it is dark, then hiking is not safe.
19
Conditional statement
The conditional statement p→q is false when p is
true and q is false, and true otherwise.
Example:
p: It snows. F
q: I stay at home. F
p→q: If it snows, then I stay at home.
p q p→q T
T T T
T F F
F T T
F F T
20
Propositions
Conditional Statement
p→q
If it snows, then I stay at home.
Contrapositive of p→q
¬q→¬p
If I do not stay at home, then it does not snow.
Converse of p→q
q→p
If I stay at home, then it snows.
Inverse of p→q
¬p→¬q
If it does not snow, then I do not stay at home.
21
Equivalent propositions
When two compound propositions always have the
same truth value, they are called equivalent.
Conditional Statement
p→q
Contrapositive of p→q
¬q→¬p
p q p→q ¬q→¬p q→p ¬p→¬q
Converse of p→q T T
T T T T
q→p T F F F T T
Inverse of p→q F T T T F F
¬p→¬q F F T T T T
22
Biconditional statement
Let p and q be propositions.
The biconditional statement p↔q is the
proposition “p if and only if q”.
Example:
p: You can take the flight.
q: You buy a ticket.
p↔q: You can take a flight, if and only if
you buy a ticket.
You can take a flight, iff you buy a ticket.
23
Biconditional statement (example)
p: 1+1=3.
q: monkeys can fly.
p↔q: 1+1=3, if and only if monkeys can fly.
24
Biconditional statement
The biconditional statement p↔q is true when p and
q have the same truth values, and is false
otherwise.
Example:
p: You can take a flight. F
q: You buy a ticket. T
p↔q: You can take
p a flight,
q ifp↔
and
q only if you buy a ticket.
T T T F
T F F
F T F
F F T
25
Logical operators (review)
Negation
¬p “not p.”
Conjunction
p∧q “p and q.”
Disjunction
p∨q “p or q.”
Exclusive or
p⊕q “p or q, but not both.”
Conditional statement
p→q “If p, then q.”
Biconditional statement
p↔q “p if and only if q.”
26
Compound proposition (example)
Truth value table of
(¬p∧q)↔(p∨q)
27
Precedence of logical operators
Parentheses can be used to specify the order
(p∨q)→r
p∨(q→r)
28
Precedence of logical operators
Parentheses can be used to specify the order
Logical operators have precedence over each
others.
Precedence of logical operators:
o Negation
¬p∧q (¬p)∧q
o Conjunction and disjunction
p∧q→r (p∧q)→r
o Conditional and biconditional
p↔q∨s p↔(q∨s)
29
Precedence of logical operators
Operators Precedence
¬ 1
∧ 2
∨ 3
→ 4
↔ 5
30
Translating English sentences
Translate this English sentence into a logical expression.
You can access the Internet from campus only if
you are a computer science major or you are not a freshman.
Solution:
Determine individual propositions
p: You can access the Internet from campus.
q: You are a computer science major.
r: You are a freshman.
Translate the sentence to compound proposition
o p only if q or not r
o p → (q ∨¬r)
31
Example of system specifications
Are these system specifications consistent?
o The message is stored in the buffer or it is retransmitted.
o The message is not stored in the buffer.
o If the message is stored in the buffer, then it is retransmitted.
Solution:
Determine individual propositions
p: The message is stored in the buffer.
q: It is retransmitted.
Translate each specification to compound proposition
o p∨q
o ¬p
o p→q
Find an assignment of truth values that makes all specifications true. (start for
the simplest compound proposition)
¬p p: false
p∨q q: true
p→q p→q : true
32
Example (creating new propositions)
p: It is snowing.
q: It is cold.
r: It is sunny.
¬p:
It is not snowing.
p→q:
If it is snowing, then it is cold.
p∨r:
It is snowing or it is sunny.
p→¬r
If it is snowing, then it is not sunny.
33
Boolean search
In searches of large bodies of information, logical
connective are used. These searches are called
Boolean searches.
Example:
o Web page searching
o Search universities in New Mexico.
o Universitites AND New AND Mexico
o Search universities in Mexico.
o Universitites AND Mexico AND NOT New
(minus)
34
Bit operations
Computers represent information using bits.
A bit has two possible values, 0 and 1.
A bit can be used to represent truth value.
1 bit represents true.
0 bit represents false.
A variable is called a boolean variable if its value
is either true or false.
Bit operations correspond to logical connectives.
Logical connective ∧ > AND
Logical connective ∨ > OR
Logical connective ⊕ > XOR
35
Table for bit operations
x y x∧y x∨y x⊕ y
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
36
Recommended exercises
6,11,14,21,23,29,38,51,5559
37