Docsity

Logic Concepts and Truth Tables, Assignments of Algorithms and Programming

Toggle sidebar
bg1
1
1
An Introduction to Logic
By
Chuck Cusack
2
Logic: Basic Definitions
•Definition: A proposition is a statement that is either true
or false, but not both.
•Defintion: The value of a proposition is called its tru th
value. Denoted by T if it is true, F if it is false
Example 1: The statement “ John Cusack is the president of
the U.S.A.” is a propos ition with truth value false.
Example 2: The statement “ Do your homework” is not a
proposition because it is not a s tatement that can be true or
false.
3
Logical Connectives
•Connectives are used to create a proposition from
several other propositions.
• Such propositions ar e called compound
propositions
• The most common connectives are:
– NEGATION (¬or !)
– AND (∧)
–OR (∨)
–XOR (⊕)
– IMPLICATION (→)
– BICONDITIONAL or IF AND ONLY IF (↔)
4
Connective Examples
• Let p be the propositi on “The sky is clear.”
• Let q be the proposition “It is raining.”
• Some examples t hat combine these are:
– The sky is clear and it is raining. (p∧q)
– The sky is clear and it is not raining. (p∧¬q)
– It is raining if and only if the sky is not clear.
(q ↔¬p)
5
Truth Tables
•Truth Tables are used to show the
relationship between the truth values of
individual propositions and the compound
propositions based on them.
•Example:
FTF
FFF
FFT
TTT
p ∧q
qp
6
NEGATION
•If pis a proposition, the negation of p, denoted ¬p, is “it
is not the case that p.”
•Example: Let p be the st atement “this class has 30
students.” Then ¬p is the statement “this class does not
have 30 students.”
• It should be ob vious that the negation of a pr oposition has
the opposite truth value. In other words, if pis true, then
¬p is false.
• The truth table for ¬p is
TF
FT
¬p
p
pf3
pf4
pf5
pf8

Partial preview of the text

Download Logic Concepts and Truth Tables and more Assignments Algorithms and Programming in PDF only on Docsity!

1

An Introduction to Logic

By

Chuck Cusack

2

Logic: Basic Definitions

  • Definition: A proposition is a statement that is either true

or false , but not both.

  • Defintion: The value of a proposition is called its truth

value. Denoted by T if it is true , F if it is false

Example 1: The statement “John Cusack is the president of

the U.S.A.” is a proposition with truth value false.

Example 2: The statement “Do your homework” is not a

proposition because it is not a statement that can be true or

false.

3

Logical Connectives

• Connectives are used to create a proposition from

several other propositions.

• Such propositions are called compound

propositions

• The most common connectives are:

  • NEGATION (¬ or !)
  • AND (∧)
  • OR (∨)
  • XOR (⊕)
  • IMPLICATION (→)
  • BICONDITIONAL or IF AND ONLY IF (↔) 4

Connective Examples

• Let p be the proposition “The sky is clear.”

• Let q be the proposition “It is raining.”

• Some examples that combine these are:

– The sky is clear and it is raining. (p∧q)

– The sky is clear and it is not raining. (p∧¬q)

– It is raining if and only if the sky is not clear.

(q ↔ ¬ p)

5

Truth Tables

• Truth Tables are used to show the

relationship between the truth values of

individual propositions and the compound

propositions based on them.

• Example:

F T F

F F F

T F F

T T T

p q p ∧q

6

NEGATION

  • If p is a proposition, the negation of p, denoted ¬ p, is “it

is not the case that p.”

  • Example: Let p be the statement “this class has 30

students.” Then ¬ p is the statement “this class does not

have 30 students.”

  • It should be obvious that the negation of a proposition has

the opposite truth value. In other words, if p is true, then

¬ p is false.

  • The truth table for ¬ p is

F T

T F

p ¬p

7

AND

• Let p and q be propositions. The proposition “ p

and q ,” denoted by p ∧ q, is true if and only if both

p and q are true.

• p ∧ q is called the conjunction of p and q.

• The truth table for p ∧ q is

F T F

F F F

T F F

T T T

p q p ∧q

8

OR

• Let p and q be propositions. The proposition “ p or

q ,” denoted by p ∨ q, is false if and only if both p

and q are false. In other words, it is true if either p

or q is true, and false otherwise.

• p ∨ q is called the disjunction of p and q.

• The truth table for p ∨ q is

F T T

F F F

T F T

T T T

p q p∨q

9

XOR

• Let p and q be propositions. The proposition “ p

exclusive or q ,” denoted by p ⊕ q, is true if and

only if either p or q is true, but not both.

• When the term OR is used in conversation, often

the correct interpretation is XOR.

• The truth table for p ⊕ q is

F T T

F F F

T F T

T T F

p q p⊕q

10

IMPLICATION

• Let p and q be propositions. The proposition “ p

implies q ,” denoted by p → q, is false if and only if

p is true and q is false.

• p → q is called an implication.

• The truth table for p → q is

F T T

F F T

T F F

T T T

p q p→q

11

BICONDITIONAL

• Let p and q be propositions. The proposition “ p if

and only if q ,” denoted by p ↔ q, is true if and only

if p and q have the same truth value.

• p ↔ q is called a biconditional.

• The truth table for p ↔ q is

F T F

F F T

T F F

T T T

p q p↔q

12

Constructing Truth Tables

• Construct the truth table for the proposition

( (p ∧ q) ∨¬ q)

• We do this step by step as follows:

F T

F F

T F

T T

p q

F

F

F

T

p ∧q

T

F

T

F

¬q

T

F

T

T

( (p ∧ q) ∨¬ q)

19

Logic and e-Mail Filtering I

• Situation: You are tired of getting spam about

losing weight and making money on eBay.

• Problem: You tried the following filter:

If(subject contains weight and subject

contains eBay) Delete message

• Solution: You meant:

If(subject contains weight or subject

contains eBay) Delete message

20

Logic in Programming II

• Situation: Consider the following loop:

while(NOT(A[i]!= 0 AND NOT(A[i]>= 10)))

• Problem: You are convinced this is way too

complicated.

• Solution: Well, it’s hard to say at this point…

21

Logic in Programming III

• Situation: Consider the following loop:

while( (i<size AND A[i]>10) OR (i<size

AND A[i]<0) OR NOT (A[i]!= 0 AND

NOT (A[i]>= 10) ) )

• Problem: You are convinced this is way too

complicated.

• Solution: Yet another example we can’t solve.

22

Logic and e-mail Filtering II

  • Situation: You get so much spam , you decide to delete

any message not sent (or copied) to you (blah), or a group

to which you belong (foo). You try:

if( TO or CC does not contain blah OR TO or CC does not contain foo) Delete message

  • Problem: It seems like all of your e-mail is being deleted.
  • Solution: This one is confusing, and we will solve it later.

23

Logic and Medication

• Situation: Your new medication has the following label:

Take 1 or 2 pills every 4-6 hours until condition improves. Do not exceed 6 pills per day, or take for longer than 7 days unless directed by a doctor. Do not take this drug during the last 3 months of pregnancy, unless directed by a doctor. If you are taking XYZs and PDQs or have taken either an XYZ or a PDQ within the last 90 days, and the other within the last 30 days, or are taking an ABC or have taken an ABC within the last 60 days, you should not take this drug. Do not drink alcohol or smoke while on this drug. If you have a heart condition, asthma, diabetes, or have an IQ below 25, do not take this drug. Do not take this drug if you have a high fever, cold sweats, runny nose, headache, or sore throat, and discontinue use if you develop any of these symptoms, and dizziness, nervousness, or sleeplessness occur.

• Problem: Under what conditions can you take it?

• Solution: Well, this one may take more than

simple logic.

24

Why Logic?

• Hopefully the last several examples have

convinced you that knowing more logic is

important for computer science, and life in

general.

• If you are still not convinced, tough. You are

going to learn it anyway.

25

Propositional Equivalences

• Many problems, included the last several

examples, can be solved by understanding the

concept of propositional equivalences.

  • Example: The statement “I am not a student and I am not

living in the dorm” is equivalent to “I am not a student or

living in the dorm.”

  • Example: “You will pass this class or you will not be in

the J.D. Edwards program next year” is equivalent to “If

you are to be in the J.D. Edwards program next year, then

you must pass this class.”

26

Some Terminology

• Definition: A tautology is a proposition that

is always true.

• Definition: A contradiction is a proposition

that is always false.

• Definition: A proposition that is not a

tautology or a contradiction is a

contingency.

27

Propositional Equivalence

  • Definition #1: Propositions p and q are called logically

equivalent if p ↔ q is a tautology.

  • Definition #2: Propositions p and q are logically

equivalent if and only if they have the same truth table.

  • Notation: If p and q are equivalent, we write p ⇔ q
  • Example: The propositions ¬ p ∨ q and p → q are logically

equivalent. We can see this by constructing the truth

tables.

F T

F F

T F

T T

p q

T

T

F

T

¬p∨q

T

T

F

F

¬p

T

T

F

T

p→q

28

Another Example

• We show that p ∨ (q ∧ r) and ( p ∨ q) ∧ (p ∨ r) are

logically equivalent.

F

F

F

F

T

T

T

T

p

F F

F T

T F

T T

F F

F T

T F

T T

q r

F

F

F

T

T

T

T

T

(p∨q)∧(p∨r)

F

T

F

T

T

T

T

T

p∨r

F

F

T

T

T

T

T

T

p∨q

F

F

F

T

T

T

T

T

p∨(q∧r)

F

F

F

T

F

F

F

T

q∧r

29

Simple Logical Equivalences

Equivalences involving one proposition

Equivalence Name

p∧F ⇔ F

p∨T ⇔ T Domination laws

p∨F ⇔ p

p∧T ⇔ p Identity laws

p∧p ⇔p

p∨p ⇔ p Idempotent laws

¬(¬p) ⇔p Double negation law

p∧¬p ⇔ F (Not an offical name)

p∨¬p ⇔ T Cancellation laws

30

Logical Equivalences

Equivalences involving multiple propositions

Equivalence Name

p∧q ⇔ q∧p

p∨q ⇔ q∨p Commutative laws

(p∧q)∧r ⇔ p∧(q∧r)

(p∨q)∨r ⇔ p∨(q∨r) Associative 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

(p→q) ⇔ (¬p∨q) Implication law

37

Logic and e-Mail Filtering II

  • The statement becomes

if(NOT (p AND q)) Delete message

  • The truth table for NOT(p AND q)is:

F T F

F F F

T F F

T T T

p q p∧q

T

T

T

F

¬(p∧q)

  • So e-mail is deleted unless TO or CC contain both

blah and foo, which is clearly not what we wanted.

  • If TO or CC contains either blah or foo, we do not

want to delete. The truth table we want is…

T

F

F

F

F

T

T

T

p∨q

  • Applying negation, we now recognize this as:

38

Logic and e-Mail Filtering II

• Then what we really want is:

if(NOT(p OR q)) Delete message

F T F

F F T

T F F

T T F

p q ¬(p∨q)

•Using DeMorgan’s Law, it becomes:

if(NOT p AND NOT q)) Delete message

•Retranslating, we seem to have wanted

if( TO or CC does not contain blah AND TO or CC does not contain foo) Delete message

•Great. We did it.

•Or did we?

39

Logic and e-Mail Filtering II

  • We now think that the following filter should work: if( TO or CC does not contain blah AND TO or CC does not contain foo) Delete message
  • Unfortunately, all of your e-mail is still being deleted.
  • Let’s keep trying. Let

p = “TO contains blah” q = “CC contains blah”

r = “TO contains foo” s = “CC contains foo”

  • The filter is if( (NOT p OR NOT q ) AND (NOT r OR NOT s) ) Delete message
  • This is where we went wrong earlier…

40

Logic and e-Mail Filtering II

  • Continuing, we had if( (NOT p OR NOT q ) AND (NOT r OR NOT s) ) Delete message
  • Applying De Morgan’s Law, we have if( NOT (p AND q ) AND NOT( r AND s) ) Delete message
  • Applying De Morgan’s Law again, we have if( NOT( (p AND q ) OR ( r AND s) ) ) Delete message
  • In English, the condition is loosely translated

“If it is not the case that either

both TO AND CC contains blah OR

both TO AND CC contains foo”

41

Logic and e-Mail Filtering II

  • In other words, unless the person sending the message put

your address in both the TO and CC fields, it will be deleted.

  • The problem is that the filter parses “if TO OR CC (X)” as

“if TO (X) OR CC (X).”

  • This means that “if TO OR CC (NOT X)” is parsed as

“if TO (NOT X) OR CC (NOT X),”

not as

“if NOT ( TO (X) OR CC (X)),”

which is what we did earlier in the example.

  • Because of this, you should not use the “if TO OR CC…”

filter with the “does not contain” condition, since it is most

likely not what you intended.

42

Logic and e-Mail Filtering II

  • Now that we know the problem, we can fix it.
  • What we want is really: If(TO does not contain blah AND CC does not contain blah AND TO does not contain foo AND CC does not contain foo) Delete message
  • Recall the original filter was: if(TO or CC does not contain blah OR TO or CC does not contain foo) Delete message

43

Logic and e-Mail Filtering II

  • This example should help illustrate the following

1. Sometimes we can think a statement means one thing

when it actually means another.

2. Sometimes, we simply can’t figure out what a statement

means at all.

  • In these cases, we can use logic to assist us in determining the

true meaning of statements.

44

Logic and Medication

  • Situation: Your new medication has the following label: Take 1 or 2 pills every 4-6 hours until condition improves. Do not exceed 6 pills per day, or take for longer than 7 days unless directed by a doctor. Do not take this drug during the last 3 months of pregnancy, unless directed by a doctor. If you are taking XYZs and PDQs or have taken either an XYZ or a PDQ within the last 90 days, and the other within the last 30 days, or are taking an ABC or have taken an ABC within the last 60 days, you should not take this drug. Do not drink alcohol or smoke while on this drug. If you have a heart condition, asthma, diabetes, or have an IQ below 25, do not take this drug. Do not take this drug if you have a high fever, cold sweats, runny nose, headache, or sore throat, and discontinue use if you develop any of these symptoms, and dizziness, nervousness, or sleeplessness occur.
  • Problem: Under what conditions can you take it?
  • Solution: O.K., I give up. But seriously, there may be a

time when you really need to solve a similar problem.

45

Some Exercises

1. Construct the truth table for the following propositions

a) (p→q)∧p b) (¬p↔¬q) ↔(q↔r) c) p∧(q→¬ (r ∨ q))

2. I do not want any e-mail that contains the words puke,

ralph, or hurl, unless it was specifically sent to me (blah).

How do I do it?

3. Is ¬(p→q)→¬q a tautology? Give two different proofs.

4. Show that p↔q and (p∧q)∨(¬p∧¬q) are logically

equivalent.

5. Show that [(p∨q)∧(p→r)∧(q→r)]→r is a tautology.

Give a proof using equivalences and a truth table.

46

  1. Can you enter this contest? Can I? Can your friends? Rules: Must be at least 18 years old to enter. Must be enrolled in a computer science course at NU, but cannot be enrolled in a course in the J.D. Edwards Program. Full-time employees of UNL, UNO, and UNK are not allowed to enter, unless they are faculty of the computer science department, or work in information systems support.
  2. Your mom says “If you don’t eat your meat, you can’t have any pudding.” What should you do? Why?
  3. Your friend offers “Heads I win, tails you lose.” Do you take the bet?
  4. If you can’t understand this problem, then you didn’t learn the material from these notes. Did you learn the material from these notes?
  5. If you don’t want to not learn the material from this class, you should not fail to not skip doing problems. Or should you?
  6. In an episode of The Simpsons Bart said something similar to “All I know is that I’m getting straight A’s, and that ain’t not bad.” Does he deserve all A’s?