cs203 discrete mathematical structures logic proposition logic

59
CS203 Discrete Mathematical Structures Logic Proposition logic

Upload: brice-potter

Post on 17-Jan-2016

253 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS203 Discrete Mathematical Structures Logic Proposition logic

CS203Discrete Mathematical Structures

Logic Proposition logic

Page 2: CS203 Discrete Mathematical Structures Logic Proposition logic

What is logic?

The branch of philosophy concerned with analysing the patterns of reasoning by which a conclusion is drawn from a set of premises, without reference to meaning or context

(Collins English Dictionary)

Page 3: CS203 Discrete Mathematical Structures Logic Proposition logic

Digital Logic

Page 4: CS203 Discrete Mathematical Structures Logic Proposition logic

Propositional logic

• Simple types of statements, called propositions, are treated as atomic building blocks for more complex statements

1) Alexandria is a port or a holiday resort.

2) Alexandria is not a port.

Therefore, Alexandria is a holiday resort

Page 5: CS203 Discrete Mathematical Structures Logic Proposition logic

Basic connectives and truth tables

statements (propositions): declarative sentences that areeither true or false--but not both.

Eg. Ahmed Hassan wrote Gone with the Wind. 2+3=5.

not statements:

What a beautiful morning!Get up and do your exercises.

Page 6: CS203 Discrete Mathematical Structures Logic Proposition logic

Statement (Proposition)

A Statement is a sentence that is either True or False

Examples:

Non-examples:

x+y>0

x2+y2=z2

True

False2 + 2 = 4

3 x 3 = 8

787009911 is a prime

They are true for some values of x and y

but are false for some other values of x and y.

Today is Tuesday.

Page 7: CS203 Discrete Mathematical Structures Logic Proposition logic

Propositional connectives

• Propositional logic has four connectives

Name Read as Symbol

negation ‘not’

conjunction ‘and’

disjunction ‘or’

implication ‘if…then…’

Page 8: CS203 Discrete Mathematical Structures Logic Proposition logic

Logical Operators

• About a dozen logical operators– Similar to algebraic operators + * - /

• In the following examples,– p = “Today is Friday”

– q = “Today is my birthday”

Page 9: CS203 Discrete Mathematical Structures Logic Proposition logic

Logical operators: Not

• A “not” operation switches (negates) the

truth value

• Symbol: or ~

p = “Today is not Friday”

p p

T F

F T

Page 10: CS203 Discrete Mathematical Structures Logic Proposition logic

Logical operators: And

• An “and” operation is true if both operands are true

• Symbol: – It’s like the ‘A’ in And

• pq = “Today is Friday and today is my birthday”

p q pq

T T T

T F F

F T F

F F F

Page 11: CS203 Discrete Mathematical Structures Logic Proposition logic

Logical operators: Or

• An “or” operation is true if either

operands are true

• Symbol:

• pq = “Today is Friday or

today is my birthday (or

possibly both)”

p q pq

T T T

T F T

F T T

F F F

Page 12: CS203 Discrete Mathematical Structures Logic Proposition logic

Logical operators: Conditional

• A conditional means “if p then q”

• Symbol: • pq = “If today is

Friday, then today is my birthday”

• p→q=¬pq

p q pq

T T T

T F F

F T T

F F T

Page 13: CS203 Discrete Mathematical Structures Logic Proposition logic

Logical operators: Bi-conditional

• A bi-conditional means “p if and only if q”• Symbol: • Alternatively, it means

“(if p then q) and (if q then p)”

• Note that a bi-conditional has the opposite truth values of the exclusive or

p q pq

T T T

T F F

F T F

F F T

Page 14: CS203 Discrete Mathematical Structures Logic Proposition logic

Boolean operators summary

• Learn what they mean, don’t just memorize the table!

not not and or xor conditional Bi-conditional

p q p q pq pq pq pq pq

T T F F T T F T T

T F F T F T T F F

F T T F F T T T F

F F T T F F F T T

Page 15: CS203 Discrete Mathematical Structures Logic Proposition logic

Precedence of operators

• Just as in algebra, operators have precedence– 4+3*2 = 4+(3*2), not (4+3)*2

• Precedence order (from highest to lowest): ¬ → ↔– The first three are the most important

• This means that p q ¬r → s ↔ t yields: (p (q (¬r)) → s) ↔ (t)

• Not is always performed before any other operation

Page 16: CS203 Discrete Mathematical Structures Logic Proposition logic

Example

s: Aya goes out for a walk.t: The moon is out.u: It is snowing.

( )t u s : If the moon is out and it is not snowing, thenAya goes out for a walk.

If it is snowing and the moon is not out, then Aya will not go out for a walk. ( )u t s

Page 17: CS203 Discrete Mathematical Structures Logic Proposition logic

Translating English Sentences– p = “It is below freezing”– q = “It is snowing”

• It is below freezing and it is snowing• It is below freezing but not snowing• It is not below freezing and it is not snowing• It is either snowing or below freezing (or both)• If it is below freezing, it is also snowing• It is either below freezing or it is snowing,

but it is not snowing if it is below freezing• That it is below freezing is necessary and

sufficient for it to be snowing

pqp¬q¬p¬qpqp→q

((pq)¬(pq))(p→¬q)

p↔q

Page 18: CS203 Discrete Mathematical Structures Logic Proposition logic

Logical Equivalence

0011

0101

1100

1101

1101

p q p p q p q

s s1 2

Page 19: CS203 Discrete Mathematical Structures Logic Proposition logic

L3 19

Tables of Logical Equivalences

• Identity lawsLike adding 0

• Domination lawsLike multiplying by 0

• Idempotent lawsDelete redundancies

• Double negation“I don’t like you, not”

• Commutativity Like “x+y = y+x”

• AssociativityLike “(x+y)+z = y+(x+z)”

• DistributivityLike “(x+y)z = xz+yz”

• De Morgan

Page 20: CS203 Discrete Mathematical Structures Logic Proposition logic

Tables of Logical Equivalences

• Excluded middle• Negating creates opposite• Definition of implication in

terms of Not and Or

Page 21: CS203 Discrete Mathematical Structures Logic Proposition logic

Fundamentals of Logic

A compound statement is called a tautology(T0) if it is true for all truth value assignments for its component statements. If a compound statement is false for all such assignments, then it is called a contradiction(F0).

p p q

p p q

( )

( )

: tautology

: contradiction

Page 22: CS203 Discrete Mathematical Structures Logic Proposition logic

Propositional Logic - 2 more defn…A tautology is a proposition that’s always TRUE.

A contradiction is a proposition that’s always FALSE.

p p p p p p

T F

F T

T

T

F

F

Page 23: CS203 Discrete Mathematical Structures Logic Proposition logic

23

Tautology example

Demonstrate that

[¬p (p q )]q

is a tautology in two ways:

1. Using a truth table – show that [¬p (p q )]q is always true

2. Using a proof (will get to this later).

Page 24: CS203 Discrete Mathematical Structures Logic Proposition logic

Tautology by truth table

p q ¬p p q ¬p (p q ) [¬p (p q )]q

T T

T F

F T

F F

Page 25: CS203 Discrete Mathematical Structures Logic Proposition logic

Tautology by truth table

p q ¬p p q ¬p (p q ) [¬p (p q )]q

T T F

T F F

F T T

F F T

Page 26: CS203 Discrete Mathematical Structures Logic Proposition logic

Tautology by truth table

p q ¬p p q ¬p (p q ) [¬p (p q )]q

T T F T

T F F T

F T T T

F F T F

Page 27: CS203 Discrete Mathematical Structures Logic Proposition logic

Tautology by truth table

p q ¬p p q ¬p (p q ) [¬p (p q )]q

T T F T F

T F F T F

F T T T T

F F T F F

Page 28: CS203 Discrete Mathematical Structures Logic Proposition logic

Tautology by truth table

p q ¬p p q ¬p (p q ) [¬p (p q )]q

T T F T F T

T F F T F T

F T T T T T

F F T F F T

Page 29: CS203 Discrete Mathematical Structures Logic Proposition logic

29

Tautologies, contradictions and programming

Tautologies and contradictions in your code usually correspond to poor programming design. EG:

•while(x <= 3 || x > 3)

x++;•if(x > y)

if(x == y)

return “never got here”;

Page 30: CS203 Discrete Mathematical Structures Logic Proposition logic

dual

Let s be a statement. If s contains no logical connectivesother than and , then the dual of s, denoted sd, is thestatement obtained from s by replacing each occurrence ofand by and , respectively, and each occurrence of Tand F by F and T, respectively.

Eg. )()(:),()(: FrqpsTrqps d

The dual of is p q ( ) p q p qd

Page 31: CS203 Discrete Mathematical Structures Logic Proposition logic

The Principle of Duality

Theorem () Let s and t be statements.If , then .s t s td d

Ex. P p q p q: ( ) ( ) is a tautology. Replace

each occurrence of p by r s

P r s q r s q1: [( ) ] [ ( ) ] is also a tautology.

First Substitution Rule (replace each p by another statement q)

Page 32: CS203 Discrete Mathematical Structures Logic Proposition logic

32

Converse

The converse of a logical implication is the reversal of the implication. I.e. the converse of p q is q p.

EG: The converse of “If Donald is a duck then Donald is a bird.” is “If Donald is a bird then Donald is a duck.”

p q and q p are not logically equivalent.

Page 33: CS203 Discrete Mathematical Structures Logic Proposition logic

Converse

p q p q q p (p q) (q p)

Page 34: CS203 Discrete Mathematical Structures Logic Proposition logic

Converse

p q p q q p (p q) (q p)

TTFF

TFTF

Page 35: CS203 Discrete Mathematical Structures Logic Proposition logic

Logical Non-Equivalence of Conditional and Converse

p q p q q p (p q) (q p)

TTFF

TFTF

TFTT

Page 36: CS203 Discrete Mathematical Structures Logic Proposition logic

Logical Non-Equivalence of Conditional and Converse

p q p q q p (p q) (q p)

TTFF

TFTF

TFTT

TTFT

Page 37: CS203 Discrete Mathematical Structures Logic Proposition logic

Logical Non-Equivalence of Conditional and Converse

p q p q q p (p q) (q p)

TTFF

TFTF

TFTT

TTFT

TFFT

Page 38: CS203 Discrete Mathematical Structures Logic Proposition logic

Fundamentals of Logic

Compare the efficiency of two program segments.z:=4;for i:=1 to 10 do begin x:=z-1; y:=z+3*i; if ((x>0) and (y>0)) then writeln(‘The value of the sum x+y is’, x+y) end

.

.

.if x>0 then if y>0 then …

rqp )( )( rqp

Number of comparisons?20 vs. 10+3=13

logically equivalent

Page 39: CS203 Discrete Mathematical Structures Logic Proposition logic

Derivational Proof Techniques

When compound propositions involve more and more atomic components, the size of the truth table for the compound propositions increases

Q1: How many rows are required to construct the truth-table of:( (q(pr )) ((sr)t) ) (qr )

Q2: How many rows are required to construct the truth-table of a proposition involving n atomic components?

Page 40: CS203 Discrete Mathematical Structures Logic Proposition logic

Derivational Proof Techniques

A1: 32 rows, each additional variable doubles the number of rows

A2: In general, 2n rows

Therefore, as compound propositions grow in complexity, truth tables become more and more unwieldy. Checking for tautologies/logical equivalences of complex propositions can become a chore, especially if the problem is obvious.

Page 41: CS203 Discrete Mathematical Structures Logic Proposition logic

L3 41

Derivational Proof Techniques

EG: consider the compound proposition

(p p ) ((sr)t) ) (qr )

Q: Why is this a tautology?

Page 42: CS203 Discrete Mathematical Structures Logic Proposition logic

L3 42

Derivational Proof Techniques

A: Part of it is a tautology (p p ) and the disjunction of True with any other compound proposition is still True:

(p p ) ((sr)t )) (qr ) T ((sr)t )) (qr ) TDerivational techniques formalize the

intuition of this example.

Page 43: CS203 Discrete Mathematical Structures Logic Proposition logic

Propositional Logic - an unfamous

(p q) q p q

if NOT (blue AND NOT red) OR red then…

(p q) q

(p q) q(p q) qp (q q)p q

DeMorgan’s

Double negationAssociativity

Idempotent

Page 44: CS203 Discrete Mathematical Structures Logic Proposition logic

L3 44

Tautology by proof[¬p (p q )]q

Page 45: CS203 Discrete Mathematical Structures Logic Proposition logic

L3 45

Tautology by proof[¬p (p q )]q

[(¬p p)(¬p q)]q Distributive

Page 46: CS203 Discrete Mathematical Structures Logic Proposition logic

L3 46

Tautology by proof[¬p (p q )]q

[(¬p p)(¬p q)]q Distributive

[ F (¬p q)]q ULE

Page 47: CS203 Discrete Mathematical Structures Logic Proposition logic

L3 47

Tautology by proof[¬p (p q )]q

[(¬p p)(¬p q)]q Distributive

[ F (¬p q)]q ULE [¬p q ]q Identity

Page 48: CS203 Discrete Mathematical Structures Logic Proposition logic

L3 48

Tautology by proof[¬p (p q )]q

[(¬p p)(¬p q)]q Distributive

[ F (¬p q)]q ULE [¬p q ]q Identity

¬ [¬p q ] q ULE

Page 49: CS203 Discrete Mathematical Structures Logic Proposition logic

L3 49

Tautology by proof[¬p (p q )]q

[(¬p p)(¬p q)]q Distributive

[ F (¬p q)]q ULE [¬p q ]q Identity

¬ [¬p q ] q ULE

[¬(¬p) ¬q ] q DeMorgan

Page 50: CS203 Discrete Mathematical Structures Logic Proposition logic

L3 50

Tautology by proof[¬p (p q )]q

[(¬p p)(¬p q)]q Distributive

[ F (¬p q)]q ULE [¬p q ]q Identity

¬ [¬p q ] q ULE

[¬(¬p) ¬q ] q DeMorgan

[p ¬q ] q Double Negation

Page 51: CS203 Discrete Mathematical Structures Logic Proposition logic

L3 51

Tautology by proof[¬p (p q )]q

[(¬p p)(¬p q)]q Distributive

[ F (¬p q)]q ULE [¬p q ]q Identity

¬ [¬p q ] q ULE

[¬(¬p) ¬q ] q DeMorgan

[p ¬q ] q Double Negation

p [¬q q ] Associative

Page 52: CS203 Discrete Mathematical Structures Logic Proposition logic

L3 52

Tautology by proof[¬p (p q )]q

[(¬p p)(¬p q)]q Distributive

[ F (¬p q)]q ULE [¬p q ]q Identity

¬ [¬p q ] q ULE

[¬(¬p) ¬q ] q DeMorgan

[p ¬q ] q Double Negation

p [¬q q ] Associative

p [q ¬q ] Commutative

Page 53: CS203 Discrete Mathematical Structures Logic Proposition logic

L3 53

Tautology by proof[¬p (p q )]q

[(¬p p)(¬p q)]q Distributive

[ F (¬p q)]q ULE [¬p q ]q Identity

¬ [¬p q ] q ULE

[¬(¬p) ¬q ] q DeMorgan

[p ¬q ] q Double Negation

p [¬q q ] Associative

p [q ¬q ] Commutative p T ULE

Page 54: CS203 Discrete Mathematical Structures Logic Proposition logic

L3 54

Tautology by proof[¬p (p q )]q

[(¬p p)(¬p q)]q Distributive

[ F (¬p q)]q ULE [¬p q ]q Identity

¬ [¬p q ] q ULE

[¬(¬p) ¬q ] q DeMorgan

[p ¬q ] q Double Negation

p [¬q q ] Associative

p [q ¬q ] Commutative p T ULE T Domination

Page 55: CS203 Discrete Mathematical Structures Logic Proposition logic

Writing Logical Formula for a Truth Table

Digital logic:

Given a digital circuit, we can construct the truth table.

Now, suppose we are given only the truth table (i.e. the specification,

e.g. the specification of the majority function), how can

we construct a digital circuit (i.e. formula) using only

simple gates (such as AND, OR, NOT)

that has the same function?

Page 56: CS203 Discrete Mathematical Structures Logic Proposition logic

Examples

1. “I don’t study well and fail” is logically equivalent to “If I study well, then I don’t fail”

2. Write a C program that represents the compound proposition (pq)r

Page 57: CS203 Discrete Mathematical Structures Logic Proposition logic

Decide whether the following statements are tautologies or contradictions or neither

1. (p q) (q p).

2. (p q) (q ¬p).

3. p ¬p.

4. (p ¬q) (q ¬p).

Page 58: CS203 Discrete Mathematical Structures Logic Proposition logic

Use truth table to find

• - P Q P R• P -Q R P R - Q

• A B -C D E F

• (P ↔ Q) (P˅ Q)

• [P ˄ (P Q)] Q

Page 59: CS203 Discrete Mathematical Structures Logic Proposition logic

Which of the following equivalent to -(P˄Q)

• (-p) ˄ (-Q)

• P ˅ Q

• (-P) ˄ Q

• (-P) ˅ (-Q)