Logic: Computer Science Department Magomelo M
Logic: Computer Science Department Magomelo M
MAGOMELO M.
Introduction
• In developing a mathematical theory, assertions or
statements are made.
• These statements are made in the form of sentences using
words and mathematical symbols.
• When proving a theory, a mathematician uses a system of
logic.
• This is also the case when developing an algorithm for a
program or system of programs in computer science.
• The system of logic is applied to decide if a statement follows
from, or is a logical consequence of, one or more other
statements.
LOGIC
TRANSLATING FROM ENGLISH TO SYMBOLS
1
Conjunction ()/ “and”:
If p and q are statements, then the conjunction of p
and q is “p and q”, denoted as “p q”.
It is true when, and only when, both p and q are true.
If either p or q is false, or if both are false, pq is
false. Truth Table: p q
Disjunction () /“or”
I f p & q are statements, then the disjunction of p and
q is “p or q”, denoted as “p q”.
It is true when at least one of p or q is true and is false
only when both p and q are false.
Truth Table: p q
Exercise
Exclusive Disjunction
• The exclusive disjunction of p and q is symbolized
by p ⊕ q.
• This compound proposition is true when exactly
one (i.e. one or other, but not both) of its
components is true. The truth table for p q
p q pq
T T F
T F T
F T T
FF FF FF
Exclusive Cont’d……
• When two simple propositions are combined using
‘or’, context will provide the clue as to whether the
inclusive or exclusive sense is intended.
• “Tomorrow I will go swimming or play football”
seems to suggest that I will not do both and
therefore becomes exclusive or.
• ‘Applicants for this post must be over 25 or have at
least 3 years relevant experience’ suggests that
applicants who satisfy both criteria will be
considered, therefore or becomes inclusive
Disjunction cont’d…….
1
Other Ways of Expressing Implications
The implication p q could be expressed in many
alternative ways as:
•“if p then q” •“not p unless q”
•“p implies q” •“q follows from p”
•“if p, q” •“q if p”
•“p only if q” •“q whenever p”
•“p is sufficient for q” •“q is necessary for p”
More About Conditional
• Given the conditional proposition p →q, we
define the following:
• (a) the converse of p → q : q → p
• (b) the inverse of p →q : ~ p → ~ q
• (c) the contrapositive of p →q : ~ q → ~ p.
Conditionals cont’d…..
• The truth table gives conditional values for its
converse, inverse and contrapositive
•
p q pq q p ~p~q ~q~p
T T T T T T
T F F T T F
F T T F F T
F F T T T T
Exercise
• State the converse, inverse and contrapositive of
the proposition:
– If Jack plays his guitar then Sara will sing.
• State the converse, inverse and contrapositive of
the proposition:
– If it’s not Sunday then the supermarket is open until
midnight.
Possible Solution: First Proposition
• Solution
• We define: p: Jack plays his guitar
• q: Sara will sing
• so that: p →q: If Jack plays his guitar then Sara will
sing.
• Converse: q → p: If Sara will sing then Jack plays his
guitar.
• Inverse: ~ p → ~ q: If Jack doesn’t play his guitar
then Sara won’t sing.
• Contrapositive: ~ q → ~ p: If Sara won’t sing then
Jack doesn’t play his guitar.
Inverse of Conditional Statements
The inverse of the conditional statement p q is
~p ~q
A conditional and its inverse are not equivalent as
could be seen from the truth table.
Biconditional “If and only if”
• If p and q are statement variables, the biconditional
of p and q is “p if, and only if, q” and is denoted
pq. if and only if abbreviated iff.
• The double headed arrow " " is the biconditional
operator.
• Alternative wording for P⇔Q can be:
– P if, and only if Q.
– P iff Q.
– P implies and is implied by Q.
– P is equivalent to Q.
– P is a necessary and sufficient condition for Q.
Order of operation for logical operators
p ~p p ~p
T F F
F T F
REMARKS
REMARKS:
– Most statements are neither tautologies nor
contradictions.
– A proposition that is neither a tautology nor a
contradiction is called a contingency.
– The negation of a tautology is a contradiction and vice
versa.
– In common usage we sometimes say that two statement
are contradictory.
By this we mean that their conjunction is a
contradiction: they cannot both be true.
Logical Equivalence
• Two compound propositions are called logically equivalent if
and only if they have the same truth values no matter what truth
value their constituent propositions they have.
• For instance p → q and ~ p ∨ q are logically equivalent, and we
write it:
p→q~p∨q
• Note that if two propositions P and Q are logically equivalent
then P ↔Q is a tautology.
• To determine if two statements P and Q are logically equivalent,
construct a full truth table for each statement.
• If their truth values at the main connective are identical, the
statements are equivalent.
• Alternatively show P ↔ Q is a tautology and hence conclude P
≡Q.
Example
• Show that p → q ~ p ∨ q
Practice
• Using truth tables, show that the following propositions
are logically equivalent
1. p ↔ q ≡ (p → q) (q → p)
2. ~(p q) and p q
3.De Morgan’s Laws for Logic. :
i. ~ (p q) ≡ ~ p ~ q
ii. ~ (p q) ≡ ~ p ~ q
LAWS OF LOGIC
•
1.Commutative Law:
pqqp
(p q) (q p)
(p q) (q p)
2.Implication Laws:
p q ~p q ~(p ~q)
3.Exportation Law:
(p q)r p (q r)
4.Equivalence:
p q (p q)(q p)
5.Reductio ad absurdum:
p q (p ~q) c
6. De Morgan’s Laws:
i. ~ (p q) ≡ ~ p ~ q
ii. ~ (p q) ≡ ~ p ~ q
•
7. Associative Laws:
((p q) r) (p (q r))
((p q) r) (p (q r))
((p q) r) (p (q r))
8. Distributive Laws:
((p q) r) (p q) (q r))
((p q) r) (p q) (q r))
9. Double Negation (Involution) Law:
~~p p
10. Identity Laws:
(p F) p
(p T) p
11. Negation (Complement) Laws:
(p ~p) T
(p ~p) F
12. Idempotent Laws:
(p p) p
(p p) p
13. Absorption Laws:
p (p q) p
p (p q) p
LAWS OF LOGIC - APPLICATION
SOLUTION
iii. p~q r (p~q)r order of operations
~(p~q) r implication law
STATEMENT REASON
~p q r ~q Given statement form
(~p q) (r ~q) Order of operations
~[(~p q) ~ (r ~q)] Implication law
pq ~(p~ q)
~[~(p ~q) (~r q)] De Morgan’s law
Exercise
1. Let p be the statement “Mathematics is easy” and q be
the statement “I do not need to study”. Write down in
words the following statements, and simplify if
possible:
a. ~p q b. p q
2. Construct truth tables for the compound statements
(p ∨ ~p ) ∧(q ∨ r) and q ∨ r . What do you notice?
3. Using Laws of Logic, write the following expressions
using only ∧, ∨ and ~ . Further, write the expression in
the simplest form.