39
snick snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville and others

Snick snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Embed Size (px)

Citation preview

Page 1: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

snick

snack

CPSC 121: Models of Computation2008/9 Winter Term 2

Describing the World with Predicate Logic

Steve Wolfman, based on notes by Patrice Belleville and others

Page 2: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Quiz 5 Notes

Predicate translation went great!

A few other problems caused trouble...

Consider all variables in this problem to be drawn from the domain of 8-bit signed ints, not mathematical integers. Is the following statement true?

Page 3: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Quiz 5 Notes

A few other problems caused trouble...

Let P(x) be the predicate statement “x is prime” and O(x) be “x is odd”.

Is P(10) O(2) true?

Page 4: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Quiz 5 Notes

A few other problems caused trouble...

Page 5: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Quiz 5 Notes: Sorting

My wording focused many on “corresponding”, not “sorted”!

What is “corresponding”? Unchanged contents:– everything in the first list is in the second– in one if and only if it's in the other– only order (indexes) changes, not values

What is sorted?– need a definition for order (alphabetical?)– each element is >= the previous and <= the next– first element higher than all other elements; second is

higher than all but one; ... Lastly, a notion that “sorted” should mean “more

organized” somehow.

Page 6: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Lecture Prerequisites

Review Chapter 1 and be able to solve any Chapter 1 exercise.

Read Sections 2.1 and 2.3 (skipping the “Negation” sections in 2.3 on pages 102-104)

Solve problems like Exercise Set 2.1 #1-24 and Set 2.3 #1-12, part (a) of 14-19, 21-22, 30-31, part (a) of 32-38, 39, parts (a) and (b) of 45-52, and 53-56.

You should have completed the open-book, untimed quiz on Vista that was due before this class.

Page 7: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Learning Goals: Pre-Class

By the start of class, you should be able to:– Evaluate the truth of predicates applied to

particular values.– Show predicate logic statements are true by

enumerating examples (i.e., all examples in the domain for a universal or one for an existential).

– Show predicate logic statements are false by enumerating counterexamples (i.e., one counterexample for universals or all in the domain for existentials).

– Translate between statements in formal predicate logic notation and equivalent statements in closely matching informal language (i.e., informal statements with clear and explicitly stated quantifiers).

Page 8: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Learning Goals: In-Class

By the end of this unit, you should be able to:– Build statements about the relationships

between properties of various objects—which may be real-world like “every candidate got votes from at least two people in every province” or computing related like “on the ith repetition of this algorithm, the variable min contains the smallest element in the list between element 0 and element i”)—using predicate logic.

Page 9: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

What Does Predicate Logic Model?

Propositional logic is a good (but not perfect) model of combinational circuits.

What is predicate logic good for modeling?• Relationships among real-world objects• Programming constraints• Properties of computing systems• Generally, problems where the properties of

different concepts, ideas, parts, or entities depend on each other.

Page 10: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: Lists (aka Arrays)

Let Elt(a, i, v) be a predicate indicating that list a has the integer value v at index i, where indexes must be 0.

Problem: write a statement that means that some arrays contain duplicate values.

Page 11: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: Lists (aka Arrays)

Let’s assume that for any given list:(1)The list has a length. (We’ll call it len here

for simplicity.)(2)The list has exactly one value for every

index < len.(3)The list has no values for indexes >= len.Problem: Write logical statements that express

these constraints. You may need to invent a new predicate.

Predicates are clumsy for expressing these common ideas.Later, we’ll use functions to do a better job.

Page 12: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: Sorted Lists

Problem: Give a definition for the predicate Sorted(a) in terms of Elt(a, i, v).

Page 13: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: Voting Database

Consider a database that tracks the votes in an election. In the database, the predicate Tally(d, c, n) means that district d reported that candidate c received n votes, where n is an integer 0.

Problem: Define a predicate GotVote(c) in terms of Tally whose truth set is the set of all candidates who received at least one vote.

Page 14: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: Voting Database

Problem: Define a predicate whose truth set is the set of all candidates who won at least one district.

Why work so hard on defining predicates?This is essentially how we query databases.

Page 15: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: Voting Database

Let’s assume that every candidate has exactly one vote total for every district. That is, there’s no missing and no duplicate data.

Problem: Write a logical statement that describes this constraint.

Predicates are clumsy for expressing this common idea.Later, we’ll use functions to do a better job.

Page 16: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: Voting Database

Let Winner(c) indicate that candidate c is the winner of the election.

Problem: Write a logical statement that means that the winner of the election must have received at least one vote.

Page 17: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: Voting Database

Let D be the set of all districts and C be the set of all candidates.

Problem: Determine what the following statement means, whether it is necessarily true (no matter what the actual vote tallies are), and justify your stance:

c1C, dD, c2C, Winner(c1) c1c2 n1Z, n2Z, Tally(d,n1,c1) Tally(d,n2,c2) n1>n2

Page 18: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: Efficiency of Algorithms (1 of 3)

Let’s say each student is in a “MUG” (1st year orientation group). For each of their MUG-mates, each student has a list of all of their classes. For simplicity, we’ll assume each MUG has 13 students and each student is taking 5 classes.

I want to determine how many students in my class have a MUG-mate in my class.

Page 19: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: Efficiency of Algorithms (2 of 3)

I consider the following two algorithms:– MUG-lists algorithm: For each student, ask the

student for the list of their MUG-mates’ classes, and check for each class whether it is CPSC 121. If the answer is ever yes, include the student in my count. Let’s this takes one second for each class I check.

– Pairwise recognition algorithm: For each student s1 in the class, ask the student for each other student s2 in the class whether s2 is a MUG-mate. If the answer is ever yes, include s1 in my count. Let’s say a student takes one second to determine whether another student is a MUG-mate.

Page 20: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: Efficiency of Algorithms (3 of 3)

Problem: How long does each of these algorithms take in terms of the number of students?

Page 21: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: Detailed Efficiency of AlgorithmsLet the predicate Faster(a1, a2, n)

mean algorithm a1 is faster than algorithm a2 on a problem of size n, where n is a positive integer.

Problem: for which ns is Faster(MUG-list, pairwise, n) true?

Page 22: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: General Efficiency of AlgorithmsLet the predicate Faster(a1, a2, n)

mean algorithm a1 is faster than algorithm a2 on a problem of size n, where n is a positive integer.

Problem: create a definition of GenerallyFaster(a1, a2) in terms of Faster(a1, a2, n) that you can live with.

Page 23: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Learning Goals: In-Class

By the start of class, you should be able to:– Build statements about the relationships

between properties of various objects—which may be real-world like “every candidate got votes from at least two people in every province” or computing related like “on the ith repetition of this algorithm, the variable min contains the smallest element in the list between element 0 and element i”) using predicate logic.

Page 24: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Next Lecture Learning Goals: Pre-Class

By the start of class, you should be able to:– Determine the negation of any quantified statement.– Given a quantified statement and an equivalence rule,

apply the rule to create an equivalent statement (particularly the De Morgan’s and contrapositive rules).

– Prove and disprove quantified statements using the “challenge” method (Epp, 3d edition, page 99).

– Apply universal instantiation, universal modus ponens, and universal modus tollens to predicate logic statements that correspond to the rules’ premises to infer statements implied by the premises.

Page 25: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Next Lecture Prerequisites

Reread Sections 2.1 and 2.3 (including the negation part that we skipped previously).

Read Sections 2.2 and 2.4.Solve problems like Exercise Set 2.1 #25-28 and 30-

31; Set 2.2 #1-25 and 27-36; Set 2.3 #13-20, 23-29, 40-42, and 45-52; and Set 2.4 #2-19, and 28-34.

(You needn’t learn the “diagram” technique, but it may make more sense than other explanations!)

Complete the open-book, untimed quiz on Vista that’s due before the next class.

Page 26: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: Java Collections

Problem: Translate the following text from the Java 1.6.0 API page for the Collection interface into predicate logic.[T]he specification for the contains(Object o) method says: "returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e))."

c ? a : b acts essentially like a multiplexer.If c is true, it evaluates to a; otherwise, it evaluates to b.

Page 27: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Problem: Java Collections

Problem: The API goes on to say:This specification should not be construed to imply that invoking Collection.contains with a non-null argument o will cause o.equals(e) to be invoked for any element e.

Explain whether and how this is consistent with your definition.

Page 28: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

More Quantifier Examples

Someone is in charge.

Everyone except the person in charge reports to someone else.

Page 29: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

More Quantifier Examples

n is a prime number.

Note: we use x|y as a predicate meaning x divides y (i.e., x “goes into” y with no remainder).

Page 30: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

More Quantifier Examples

n is a prime number.

Let’s define a new predicate P(x) in terms of this “clause”. Then, let’s express…

There’s some prime number larger than 10.

There’s some prime number larger than every natural number.

Page 31: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Yet More Examples

Eating food causes Alice to grow or shrink.

F = set of all foods

E(x): Alice eats x

g: Alice grows

s: Alice shrinks

Page 32: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Yet More Examples

Alice shrank when she ate some food.

F = set of all foods

E(x): Alice eats x

g: Alice grows

s: Alice shrinks

Page 33: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Yet More Examples

All lions are fierce. F(x): x is a fierce creature

L(x): x is a lion

C(x): x drinks coffee

Domain for all is the set of all creatures.

Page 34: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Yet More Examples

Some lions do not drink coffee.

F(x): x is a fierce creature

L(x): x is a lion

C(x): x drinks coffee

Domain for all is the set of all creatures.

Page 35: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Yet More Examples

All fierce creatures are not lions.

F(x): x is a fierce creature

L(x): x is a lion

C(x): x drinks coffee

Domain for all is the set of all creatures.

Is that English sentence ambiguous?

Page 36: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Yet More Examples

Is x, K(x, y) a proposition?

Why or why not?

F(x): x is a fierce creatureL(x): x is a lionC(x): x drinks coffee

K(x, y): x has been in y’s kitchen

Domain for all is the set of all creatures (with kitchens?).

Page 37: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Yet More Examples

Every creature has been in its own kitchen.

Some creature has not been in its own kitchen.

F(x): x is a fierce creatureL(x): x is a lionC(x): x drinks coffee

K(x, y): x has been in y’s kitchen

Domain for all is the set of all creatures (with kitchens?).

Page 38: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Yet More Examples

There is a creature that has been in every creature’s kitchen.

Every creature’s kitchen has had some creature in it.

F(x): x is a fierce creatureL(x): x is a lionC(x): x drinks coffee

K(x, y): x has been in y’s kitchen

Domain for all is the set of all creatures (with kitchens?).

Are these the same?

Page 39: Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville

Yet More Examples

Every creature has been in every other creature’s kitchen.

Every creature’s kitchen has had every creature in it.

F(x): x is a fierce creatureL(x): x is a lionC(x): x drinks coffee

K(x, y): x has been in y’s kitchen

Domain for all is the set of all creatures (with kitchens?).

Are these the same?What if we removed the word “other”?