41
1 Predicates and Predicates and Quantifiers Quantifiers CS/APMA 202, Spring 2005 CS/APMA 202, Spring 2005 Rosen, section 1.3 Rosen, section 1.3 Aaron Bloomfield Aaron Bloomfield

1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

Embed Size (px)

Citation preview

Page 1: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

11

Predicates and Predicates and QuantifiersQuantifiers

CS/APMA 202, Spring 2005CS/APMA 202, Spring 2005

Rosen, section 1.3Rosen, section 1.3

Aaron BloomfieldAaron Bloomfield

Page 2: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

22

Terminology reviewTerminology review

Proposition: a statement that is either true Proposition: a statement that is either true or falseor false Must always be one or the other!Must always be one or the other! Example: “The sky is red”Example: “The sky is red” Not a proposition: x + 3 > 4Not a proposition: x + 3 > 4

Boolean variable: A variable (usually p, q, Boolean variable: A variable (usually p, q, r, etc.) that represents a propositionr, etc.) that represents a proposition

Page 3: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

33

Propositional functionsPropositional functions

Consider P(x) = x < 5Consider P(x) = x < 5 P(x) has no truth values (x is not given a P(x) has no truth values (x is not given a

value)value) P(1) is trueP(1) is true

The proposition 1<5 is trueThe proposition 1<5 is true P(10) is falseP(10) is false

The proposition 10<5 is falseThe proposition 10<5 is false

Thus, P(x) will create a proposition when Thus, P(x) will create a proposition when given a valuegiven a value

Page 4: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

44

Propositional functions 2Propositional functions 2

Let P(x) = “x is a multiple of 5”Let P(x) = “x is a multiple of 5” For what values of x is P(x) true?For what values of x is P(x) true?

Let P(x) = x+1 > xLet P(x) = x+1 > x For what values of x is P(x) true?For what values of x is P(x) true?

Let P(x) = x + 3Let P(x) = x + 3 For what values of x is P(x) true?For what values of x is P(x) true?

Page 5: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

55

Anatomy of a propositional functionAnatomy of a propositional function

P(x) = x + 5 > xP(x) = x + 5 > x

variable predicate

Page 6: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

66

Propositional functions 3Propositional functions 3

Functions with multiple variables:Functions with multiple variables:

P(x,y) = x + y == 0P(x,y) = x + y == 0P(1,2) is false, P(1,-1) is trueP(1,2) is false, P(1,-1) is true

P(x,y,z) = x + y == zP(x,y,z) = x + y == zP(3,4,5) is false, P(1,2,3) is trueP(3,4,5) is false, P(1,2,3) is true

P(xP(x11,x,x22,x,x33 … x … xnn) = …) = …

Page 7: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

88

So, why do we care about So, why do we care about quantifiers?quantifiers?

Many things (in this course and beyond) Many things (in this course and beyond) are specified using quantifiersare specified using quantifiers In some cases, it’s a more accurate way to In some cases, it’s a more accurate way to

describe things than Boolean propositionsdescribe things than Boolean propositions

Page 8: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

99

QuantifiersQuantifiers

A quantifier is “an operator that limits the A quantifier is “an operator that limits the variables of a proposition”variables of a proposition”

Two types:Two types: UniversalUniversal ExistentialExistential

Page 9: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

1010

Universal quantifiers 1Universal quantifiers 1

Represented by an upside-down A: Represented by an upside-down A: It means “for all”It means “for all” Let P(x) = x+1 > xLet P(x) = x+1 > x

We can state the following:We can state the following: x P(x)x P(x) English translation: “for all values of x, P(x) is English translation: “for all values of x, P(x) is

true”true” English translation: “for all values of x, x+1>x English translation: “for all values of x, x+1>x

is true”is true”

Page 10: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

1111

Universal quantifiers 2Universal quantifiers 2

But is that always true?But is that always true? x P(x)x P(x)

Let x = the character ‘a’Let x = the character ‘a’ Is ‘a’+1 > ‘a’?Is ‘a’+1 > ‘a’?

Let x = the state of VirginiaLet x = the state of Virginia Is Virginia+1 > Virginia?Is Virginia+1 > Virginia?

You need to specify your universe!You need to specify your universe! What values x can representWhat values x can represent Called the “domain” or “universe of discourse” Called the “domain” or “universe of discourse”

by the textbookby the textbook

Page 11: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

1212

Universal quantifiers 3Universal quantifiers 3

Let the universe be the real numbers.Let the universe be the real numbers. Then, Then, x P(x) is truex P(x) is true

Let P(x) = x/2 < xLet P(x) = x/2 < x Not true for the negative numbers!Not true for the negative numbers! Thus, Thus, x P(x) is falsex P(x) is false

When the domain is all the real numbersWhen the domain is all the real numbers

In order to prove that a universal quantification is In order to prove that a universal quantification is true, it must be shown for ALL casestrue, it must be shown for ALL casesIn order to prove that a universal quantification is In order to prove that a universal quantification is false, it must be shown to be false for only ONE false, it must be shown to be false for only ONE casecase

Page 12: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

1313

Universal quantification 4Universal quantification 4

Given some propositional function P(x)Given some propositional function P(x)

And values in the universe xAnd values in the universe x11 .. x .. xnn

The universal quantification The universal quantification x P(x) x P(x) implies:implies:

P(xP(x11) ) P(x P(x22) ) … … P(x P(xnn))

Page 13: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

1414

Universal quantification 5Universal quantification 5

Think of Think of as a for loop: as a for loop:

x P(x), where 1 x P(x), where 1 ≤≤ x x ≤≤ 10 10

… … can be translated as …can be translated as …

for ( x = 1; x <= 10; x++ )for ( x = 1; x <= 10; x++ )is P(x) true?is P(x) true?

If P(x) is true for all parts of the for loop, then If P(x) is true for all parts of the for loop, then x P(x)x P(x) Consequently, if P(x) is false for any one value of the for loop, Consequently, if P(x) is false for any one value of the for loop,

then then x P(x) is falsex P(x) is false

Page 14: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

15

Quick surveyQuick survey

I understand universal quantificationI understand universal quantification

a)a) Very wellVery well

b)b) With some review, I’ll be goodWith some review, I’ll be good

c)c) Not reallyNot really

d)d) Not at allNot at all

Page 15: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

1616

Chapter 2: Computer bugsChapter 2: Computer bugs

Page 16: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

1717

Existential quantification 1Existential quantification 1

Represented by an bacwards E: Represented by an bacwards E: It means “there exists”It means “there exists” Let P(x) = x+1 > xLet P(x) = x+1 > x

We can state the following:We can state the following: x P(x)x P(x) English translation: “there exists (a value of) x English translation: “there exists (a value of) x

such that P(x) is true”such that P(x) is true” English translation: “for at least one value of English translation: “for at least one value of

x, x+1>x is true”x, x+1>x is true”

Page 17: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

1818

Existential quantification 2Existential quantification 2

Note that you still have to specify your Note that you still have to specify your universeuniverse If the universe we are talking about is all the If the universe we are talking about is all the

states in the US, then states in the US, then x P(x) is not truex P(x) is not true

Let P(x) = x+1 < xLet P(x) = x+1 < x There is no numerical value x for which x+1<xThere is no numerical value x for which x+1<x Thus, Thus, x P(x) is falsex P(x) is false

Page 18: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

1919

Existential quantification 3Existential quantification 3

Let P(x) = x+1 > xLet P(x) = x+1 > x There is a numerical value for which x+1>xThere is a numerical value for which x+1>x

In fact, it’s true for all of the values of x!In fact, it’s true for all of the values of x! Thus, Thus, x P(x) is truex P(x) is true

In order to show an existential quantification is In order to show an existential quantification is true, you only have to find ONE valuetrue, you only have to find ONE value

In order to show an existential quantification is In order to show an existential quantification is false, you have to show it’s false for ALL valuesfalse, you have to show it’s false for ALL values

Page 19: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

2020

Existential quantification 4Existential quantification 4

Given some propositional function P(x)Given some propositional function P(x)

And values in the universe xAnd values in the universe x11 .. x .. xnn

The existential quantification The existential quantification x P(x) x P(x) implies:implies:

P(xP(x11) ) P(x P(x22) ) … … P(x P(xnn))

Page 20: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

21

Quick surveyQuick survey

I understand existential I understand existential quantificationquantification

a)a) Very wellVery well

b)b) With some review, I’ll be goodWith some review, I’ll be good

c)c) Not reallyNot really

d)d) Not at allNot at all

Page 21: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

2222

A note on quantifiersA note on quantifiers

Recall that P(x) is a propositional functionRecall that P(x) is a propositional function Let P(x) be “x == 0”Let P(x) be “x == 0”

Recall that a proposition is a statement that is Recall that a proposition is a statement that is either true or falseeither true or false P(x) is not a propositionP(x) is not a proposition

There are two ways to make a propositional There are two ways to make a propositional function into a proposition:function into a proposition: Supply it with a valueSupply it with a value

For example, P(5) is false, P(0) is trueFor example, P(5) is false, P(0) is true Provide a quantifiactionProvide a quantifiaction

For example, For example, x P(x) is false and x P(x) is false and x P(x) is truex P(x) is true Let the universe of discourse be the real numbersLet the universe of discourse be the real numbers

Page 22: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

2323

Binding variablesBinding variables

Let P(x,y) be x > yLet P(x,y) be x > y

Consider: Consider: x P(x,y)x P(x,y) This is not a proposition!This is not a proposition! What is y?What is y?

If it’s 5, then If it’s 5, then x P(x,y) is falsex P(x,y) is false

If it’s x-1, then If it’s x-1, then x P(x,y) is truex P(x,y) is true

Note that y is not “bound” by a quantifierNote that y is not “bound” by a quantifier

Page 23: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

2424

Binding variables 2Binding variables 2

((x P(x)) x P(x)) Q(x) Q(x) The x in Q(x) is not bound; thus not a propositionThe x in Q(x) is not bound; thus not a proposition

((x P(x)) x P(x)) ( (x Q(x))x Q(x)) Both x values are bound; thus it is a propositionBoth x values are bound; thus it is a proposition

((x P(x) x P(x) Q(x)) Q(x)) ( (y R(y))y R(y)) All variables are bound; thus it is a propositionAll variables are bound; thus it is a proposition

((x P(x) x P(x) Q(y)) Q(y)) ( (y R(y))y R(y)) The y in Q(y) is not bound; this not a propositionThe y in Q(y) is not bound; this not a proposition

Page 24: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

2525

Negating quantificationsNegating quantifications

Consider the statement:Consider the statement: All students in this class have red hairAll students in this class have red hair

What is required to show the statement is false?What is required to show the statement is false? There exists a student in this class that does NOT There exists a student in this class that does NOT

have red hairhave red hair

To negate a universal quantification:To negate a universal quantification: You negate the propositional functionYou negate the propositional function AND you change to an existential quantificationAND you change to an existential quantification ¬¬x P(x) = x P(x) = x ¬P(x)x ¬P(x)

Page 25: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

2626

Negating quantifications 2Negating quantifications 2

Consider the statement:Consider the statement: There is a student in this class with red hairThere is a student in this class with red hair

What is required to show the statement is What is required to show the statement is false?false? All students in this class do not have red hairAll students in this class do not have red hair

Thus, to negate an existential quantification:Thus, to negate an existential quantification: Tou negate the propositional functionTou negate the propositional function AND you change to a universal quantificationAND you change to a universal quantification ¬¬x P(x) = x P(x) = x ¬P(x)x ¬P(x)

Page 26: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

27

Quick surveyQuick survey

I understand about bound variables I understand about bound variables and negating quantifiersand negating quantifiers

a)a) Very wellVery well

b)b) With some review, I’ll be goodWith some review, I’ll be good

c)c) Not reallyNot really

d)d) Not at allNot at all

Page 27: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

2828

Translating from EnglishTranslating from English

This is example 17, page 36This is example 17, page 36Consider “For every student in this class, Consider “For every student in this class, that student has studied calculus”that student has studied calculus”Rephrased: “For every student x in this Rephrased: “For every student x in this class, x has studied calculus”class, x has studied calculus” Let C(x) be “x has studied calculus”Let C(x) be “x has studied calculus” Let S(x) be “x is a student”Let S(x) be “x is a student”

x C(x)x C(x) True if the universe of discourse is all True if the universe of discourse is all

students in this classstudents in this class

Page 28: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

2929

Translating from English 2Translating from English 2

What about if the unvierse of discourse is What about if the unvierse of discourse is all students (or all people?)all students (or all people?) x (S(x)x (S(x)C(x))C(x))

This is wrong! Why?This is wrong! Why? x (S(x)x (S(x)→→C(x))C(x))

Another option:Another option: Let Q(x,y) be “x has stuided y”Let Q(x,y) be “x has stuided y” x (S(x)x (S(x)→Q→Q(x, calculus))(x, calculus))

Page 29: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

3030

Translating from English 3Translating from English 3

This is example 18, page 36This is example 18, page 36

Consider:Consider: ““Some students have visited Mexico”Some students have visited Mexico” ““Every student in this class has visited Every student in this class has visited

Canada or Mexico”Canada or Mexico”

Let:Let: S(x) be “x is a student in this class”S(x) be “x is a student in this class” M(x) be “x has visited Mexico”M(x) be “x has visited Mexico” C(x) be “x has visited Canada”C(x) be “x has visited Canada”

Page 30: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

3131

Translating from English 4Translating from English 4

Consider: “Some students have visited Mexico”Consider: “Some students have visited Mexico” Rephrasing: “There exists a student who has visited Rephrasing: “There exists a student who has visited

Mexico”Mexico”

x M(x)x M(x) True if the universe of discourse is all studentsTrue if the universe of discourse is all students

What about if the unvierse of discourse is all What about if the unvierse of discourse is all people?people? x (S(x) x (S(x) →→ M(x)) M(x))

This is wrong! Why?This is wrong! Why? x (S(x) x (S(x) M M(x))(x))

Page 31: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

3232

Translating from English 5Translating from English 5

Consider: “Every student in this class has Consider: “Every student in this class has visited Canada or Mexico”visited Canada or Mexico”

x (M(x)x (M(x)C(x)C(x) When the universe of discourse is all studentsWhen the universe of discourse is all students

x (S(x)x (S(x)→→(M(x)(M(x)C(x))C(x)) When the universe of discourse is all peopleWhen the universe of discourse is all people

Why isn’t Why isn’t x (S(x)x (S(x)(M(x)(M(x)C(x))) correct?C(x))) correct?

Page 32: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

3333

Translating from English 6Translating from English 6

Note that it would be easier to define Note that it would be easier to define V(x, y) as “x has visited y”V(x, y) as “x has visited y” x (S(x) x (S(x) V V(x,Mexico))(x,Mexico)) x (S(x)x (S(x)→→(V(x,Mexico) (V(x,Mexico) V(x,Canada)) V(x,Canada))

Page 33: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

3434

Translating from English 7Translating from English 7

Translate the statements:Translate the statements: ““All hummingbirds are richly colored”All hummingbirds are richly colored” ““No large birds live on honey”No large birds live on honey” ““Birds that do not live on honey are dull in color”Birds that do not live on honey are dull in color” ““Hummingbirds are small”Hummingbirds are small”

Assign our propositional functionsAssign our propositional functions Let P(x) be “x is a hummingbird”Let P(x) be “x is a hummingbird” Let Q(x) be “x is large”Let Q(x) be “x is large” Let R(x) be “x lives on honey”Let R(x) be “x lives on honey” Let S(x) be “x is richly colored”Let S(x) be “x is richly colored”

Let our universe of discourse be all birdsLet our universe of discourse be all birds

Page 34: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

3535

Translating from English 8Translating from English 8

Our propositional functionsOur propositional functions Let P(x) be “x is a hummingbird”Let P(x) be “x is a hummingbird” Let Q(x) be “x is large”Let Q(x) be “x is large” Let R(x) be “x lives on honey”Let R(x) be “x lives on honey” Let S(x) be “x is richly colored”Let S(x) be “x is richly colored”

Translate the statements:Translate the statements: ““All hummingbirds are richly colored”All hummingbirds are richly colored”

x (P(x)x (P(x)→S(x))→S(x)) ““No large birds live on honey”No large birds live on honey”

¬¬x (Q(x) x (Q(x) R(x)) R(x))Alternatively: Alternatively: x (x (¬Q(x) ¬Q(x) ¬R(x)) ¬R(x))

““Birds that do not live on honey are dull in color”Birds that do not live on honey are dull in color”x (x (¬R(x) → ¬S(x))¬R(x) → ¬S(x))

““Hummingbirds are small”Hummingbirds are small”x (P(x) x (P(x) → ¬Q(x))→ ¬Q(x))

Page 35: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

36

Quick surveyQuick survey

I understand how to translate I understand how to translate between English and quantified between English and quantified statementsstatements

a)a) Very wellVery well

b)b) With some review, I’ll be goodWith some review, I’ll be good

c)c) Not reallyNot really

d)d) Not at allNot at all

Page 36: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

3737

Today’s demotivatorsToday’s demotivators

Page 37: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

3838

PrologProlog

A programming language using logic!A programming language using logic!Entering facts:Entering facts:

instructor (bloomfield, cs202)instructor (bloomfield, cs202)enrolled (alice, cs202)enrolled (alice, cs202)enrolled (bob, cs202)enrolled (bob, cs202)enrolled (claire, cs202)enrolled (claire, cs202)

Entering predicates:Entering predicates:teaches (P,S) :- instructor (P,C), enrolled (S,C)teaches (P,S) :- instructor (P,C), enrolled (S,C)

Extracting dataExtracting data?enrolled (alice, cs202)?enrolled (alice, cs202)

Result:Result:yesyes

Page 38: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

3939

Prolog 2Prolog 2

Extracting dataExtracting data?enrolled (X, cs202)?enrolled (X, cs202)

Result:Result:alicealice

bobbob

claireclaire

Extracting dataExtracting data?teaches (X, alice)?teaches (X, alice)

Result:Result:bloomfieldbloomfield

Page 39: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

40

Quick surveyQuick survey

I felt I understood the material in this I felt I understood the material in this slide set…slide set…

a)a) Very wellVery well

b)b) With some review, I’ll be goodWith some review, I’ll be good

c)c) Not reallyNot really

d)d) Not at allNot at all

Page 40: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

41

Quick surveyQuick survey

The pace of the lecture for this The pace of the lecture for this slide set was…slide set was…

a)a) FastFast

b)b) About rightAbout right

c)c) A little slowA little slow

d)d) Too slowToo slow

Page 41: 1 Predicates and Quantifiers CS/APMA 202, Spring 2005 Rosen, section 1.3 Aaron Bloomfield

42

Quick surveyQuick survey

How interesting was the material in How interesting was the material in this slide set? Be honest!this slide set? Be honest!

a)a) Wow! That was SOOOOOO cool!Wow! That was SOOOOOO cool!

b)b) Somewhat interestingSomewhat interesting

c)c) Rather bortingRather borting

d)d) ZzzzzzzzzzzZzzzzzzzzzz