1 / 111

CPSC 121: Models of Computation 2013W2

CPSC 121: Models of Computation 2013W2. Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville and others. Outline. Prereqs , Learning Goals, and Quiz Notes Prelude: Scope and Predicate Definition Problems and Discussion Lis z t Etudes Sorted Lists

marged
Download Presentation

CPSC 121: Models of Computation 2013W2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPSC 121: Models of Computation2013W2 Describing the World with Predicate Logic Steve Wolfman, based on notes by Patrice Belleville and others

  2. Outline • Prereqs, Learning Goals, and Quiz Notes • Prelude: Scope and Predicate Definition • Problems and Discussion • Liszt Etudes • Sorted Lists • Comparing Algorithms • Next Lecture Notes

  3. 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). Discuss point of learning goals.

  4. 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. Discuss point of learning goals.

  5. Where We Are inThe Big Stories Theory Hardware How do we build devices to compute? Now: Still taking a bit of a vacation in lecture! How do we model computational systems? Now: Finally getting a tool that we can use to model the complex systems we really want to reason about!

  6. Outline • Prereqs, Learning Goals, and Quiz Notes • Prelude: Motivation, Scope & Defining Predicates • Problems and Discussion • Liszt Etudes • Sorted Lists • Comparing Algorithms • Next Lecture Notes

  7. Problem: Translate to Propositional Logic • Steve is married to Rachel. • Steve is married to Rachel and Rachel is married to Steve. • Steve is not married to anyone other than Rachel. NO predicates allowed, just things like p, q, r, etc.

  8. Limitations of Propositional Logic as a Model Which of the following can propositional logic model effectively? • Relationships among factory production lines like “wheel assembly and frame welding both connect to the undercarriage line”. • Defining what it means for a number to be prime. • Generalizing from examples to abstract patterns like “everyone takes off their shoes at airport security”. • Prop logic can model all of these effectively. • Prop logic cannot model any of these effectively.

  9. What Does Predicate Logic Model? • Relationships among real-world objects • Generalizations about patterns • Infinite (or even just big) domains array doubling pre/post conditions Problems where the properties of different concepts, ideas, parts, or entities depend on each other.

  10. But... Would You Ever Really Use Pred Logic? • Data Structures Example: “...every key is less than or equal to all of its children’s keys...” • AI example: “...let h' be a ‘heuristic’ function evaluating game states and h be the true value of the state. For all nodes n, h'(n)  h(n)...” • Java example: “...there is no path via references from any variable in scope to any memory location available for garbage collection...” • Economics/elections example: “...for any distinct pair of candidates c1 and c2, if all voters prefer c1 to c2, then society must rank c1 above c2...”

  11. Quantifier Syntax A quantifier in CPSC 121 is always of the form: _____ _____  ______, _______________  or  variable set subexpression that can mention the variable Together, this forms a single subexpression. For example, x  N, (x > 3)  (x + 4 < 12).

  12. Quantifier Syntax You cannot treat any part of this (like x  Z or x) as a subexpression. It just isn’t. So, no saying: (x  Z)  p.

  13. Quantifier Scope A quantifier applies to everything to its right until a closing parenthesis stops it.

  14. Quantifier Scope A quantifier applies to everything to its right until a closing parenthesis stops it. x  D, (y  E, Q(x,y)  z  F, R(y,z))  P(x). one subexpression (all under the universal)

  15. Quantifier Scope A quantifier applies to everything to its right until a closing parenthesis stops it. x  D, (y  E, Q(x,y)  z  F, R(y,z))  P(x). a smaller subexpression

  16. Quantifier Scope A quantifier applies to everything to its right until a closing parenthesis stops it. x  D, (y  E, Q(x,y)  z  F, R(y,z))  P(x). another smaller subexpression (all under the existential)

  17. Quantifier Scope A quantifier applies to everything to its right until a closing parenthesis stops it. x  D, (y  E, Q(x,y)  z  F, R(y,z))  P(x). another smaller subexpression

  18. A Bit of Syntax: Quantifier Scope Which of the following placements of parentheses yields the same meaning as: x  Z, y  Z, x < y  Even(y). • ()x  Z, y  Z, x < y  Even(y). • (x)  Z, y  Z, x < y  Even(y). • (x  Z), y  Z, x < y  Even(y). • (x  Z, y  Z, x < y)  Even(y). • (x  Z, y  Z, x < y  Even(y)). Reminder: Z is the set of integers.

  19. Aside: Where Can You “Cut” a Propositional Logic Statement? This doesn’t make sense: (x  w)  ( y). Why not? Every use of  looks just like every other use. They all look like p  q. The left side must make sense as an equivalent to p: p  x  w here. The right side must make sense as an equivalent to q. Does q   y make sense?

  20. Aside: Where Can You “Cut” a Predicate Logic Statement? This doesn’t make sense: x  Z  Tasty(y). Why not? Every use of  looks just like every other use. They all look like P(x, y, …)  Q(x, y, …). The RIGHT side must make sense as an equivalent to Q(…). Q(y)  Tasty(y) here. The left side must make sense as an equivalent to P(…): Does P(x)  x  Z make sense?

  21. Aside: Where Can You “Cut” a Predicate Logic Statement? So does this make sense: x  Z, Costs(y, x)  Tasty(y)? Does it look like P(x, y, …)  Q(x, y, …)? P(y)  x  Z, Costs(y, x)? Q(y)  Tasty(y)? No! The  is inside the universal quantifier. It applies to Costs(y, x) and Tasty(y). P(x, y)  Costs(y, x). Q(y)  Tasty(y). x  Z, P(x, y)  Q(y).

  22. Aside: Where Can You “Cut” a Predicate Logic Statement? How about: (x  Z, Costs(y, x))  (w  F, Salty(w))? P(y)  x  Z, Costs(y, x). q  w  F, Salty(w). Note: q alone is essentially a predicate Q() with no arguments.

  23. A Bit of Syntax: Negation Scope Which of the following placements of parentheses yields the same meaning as: ~x  Z+, y  Z+, x < y  Even(y). • (~)x  Z+, y  Z+, x < y  Even(y). • (~(x))  Z+, y  Z+, x < y  Even(y). • (~(x  Z+)), y  Z+, x < y  Even(y). • (~(x  Z+, y  Z+, x < y))  Even(y). • (~(x  Z+, y  Z+, x < y  Even(y))).

  24. A Bit of Semantics: Unbound Variables What is the truth value of the following formula? x  Z, x*x = y. (Warning: a bit of a trick question.) • True, because (for example) 5*5=25. • True, because every y=(sqrty)*(sqrt y) • False, because of counterexamples like no integer multiplied by itself equals 3. • It depends on y, but given a value for y, we could calculate a truth value. • None of the above. 5 5

  25. Defining a Predicate Using Expressions with “Unbound” Variables A pred. logic formula with only bound variables is a proposition, something that is either true or false: x  Z, x*x = 25. true x  Z, x*x = 3. false x  Z, x*x = robson. false (malformed?) y  Z, x  Z, x*x = y. false 5 5

  26. Defining a Predicate Using Expressions with “Unbound” Variables A pred. logic formula with unbound variables is itself a predicate, something whose truth depends on its unbound variables’ values: PerfectSquare(y): x  Z, x*x = y, where y  Z PerfectSquare(25). true PerfectSquare(3). false PerfectSquare(robson).malformed y  Z, PerfectSquare(y). false 5 5 Notice that this is much like defining a function in programming:(1) The meaning of the predicate/function depends on the argument given.(2) You can use the predicate/function over and over again.

  27. Unbound Variables Check Which variable(s) are unbound? (In other words, which variables does this formula’s truth depend on?) i  N, (i > n)  ~v  N, Elt(a, i, v). • i and v • a and n • n and v • i and n • None of these is correct. (DO NOT worry about what it means!)

  28. Outline • Prereqs, Learning Goals, and Quiz Notes • Prelude: Scope and Predicate Definition • Problems and Discussion • Liszt Etudes • Intermezzo on Idioms • Sorted Lists • Comparing Algorithms • Next Lecture Notes

  29. Modeling ListOfInteger Let Elt(a, i, v) be a predicate indicating that list a at index i has the integer value v, where indexes must be  0. 0 1 2 3 4 5 2 4 5 7 6 10 mylist Elt(mylist, 3, 7) is true. Elt(mylist, 2, 0) and Elt(mylist, 7, 2) are false.

  30. ListOfInteger ;; ListOfInteger is one of: ;; - empty ;; - (cons Integer ListOfInteger) Also: (where index 0 gets us the first element)

  31. List Element Warmup Elt(a, i, v)  list a has value v at index i. Which of the following should describe a valid list (assume all other Elt(x, y, z) are false)? • Elt(list, 0, 7), Elt(list, 1, 4), Elt(list, 2, 3) • Elt(list, 0, 7), Elt(list, 1, 4), Elt(list, 3, 3) • Elt(list, -1, 7), Elt(list, 0, 4), Elt(list, 1, 3) • Elt(list, 0, 7), Elt(list, 1, 7), Elt(list, 1, 3) • Nothing. (~i  N, v  Z, Elt(list, i, v).)

  32. List Element Exercise Define a predicate Contains(a, v) meaning “list a contains the value v”. Contains(a, v): • a  A, i  N,v  Z,Elt(a,i,v) • a  A, i  N,v  Z,Elt(a,i,v) • i  N,Elt(a,i,v) • i N,Elt(a,i,v) • None of these Hint: As with HtDP, design some tests. An empty list, a list with one element, a list with two, etc.

  33. List Length Properties We’ll agree on all of the following properties for a list: • Every list has a length. • No list has two different lengths. • No list has elements at any index as large or larger than (“beyond”) its length. • A list has exactly one element at every index up to (not including) its length.

  34. Outline • Prereqs, Learning Goals, and Quiz Notes • Prelude: Scope and Predicate Definition • Problems and Discussion • Liszt Etudes • Intermezzo on Idioms • Sorted Lists • Comparing Algorithms • Next Lecture Notes

  35. Idioms: “a form of expression natural to a language” To define lists, we need to say things like: • “No list has property P”, • “Some list has…”, • “Every list has…”, • “Some non-empty lists have…”, • “All non-empty lists have…” • “At most one element in a list has…” • “Exactly one element in a list has…” These are common predicate logic “idioms”. Let’s learn them.

  36. Idioms: “None, some, not every, all” Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” None of the animals likes honey: ~x  A, Honey(x). Some of the animals like honey: x  A, Honey(x). Not every animal likes honey: ~x  A, Honey(x). All animals like honey: x  A, Honey(x).

  37. Idioms: “None, some, not every, all” (put another way) Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” Every animals dislikes honey: x  A, ~Honey(x). Not every animal dislikes honey: ~x  A, ~Honey(x). Some animal dislikes honey: x  A, ~Honey(x). No animal dislikes honey: ~x  A, ~Honey(x).

  38. Idioms: “Restricting the Domain of an Existential” Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” Some animal likes honey: x  A, Honey(x).

  39. Idioms: “Restricting the Domain of an Existential” Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” Some bear likes honey. We’d like to say: x  SetOfBears, Honey(x). But we don’t have the set of bears. How do we “restrict” the set of animals to just bears for an existential? You did it on the quiz!

  40. Idioms: “Restricting the Domain of an Existential” Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” Some bear likes honey…Some animal is a bear and also likes honey…There is an animal that is a bear and likes honey. x  A, Bear(x)  Honey(x). This far, you did on the quiz!

  41. Idioms: “Restricting the Domain of an Existential” Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” But wait.. by the commutative law: x  A, Bear(x)  Honey(x)  x  A, Honey(x)  Bear(x) So that also means “Some honey-liker is a bear.” Yes, “some bear likes honey” is the same as “some honey-liker is a bear.”

  42. Idioms: “Restricting the Domain of an Existential” More generally: Given set D and predicate P(x), you want to say “Some <thing in D for which P is true> has the property Q.” You say: x  D, P(x)  Q(x). Some <thing in D for which P is true>…

  43. Idioms: “Restricting the Domain of a Universal” Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” Does x  A, Bear(x)  Honey(x) mean “Every bear likes honey”? (Try this test case: Steve is a wolf who dislikes honey; George is a bear who likes honey. Try to pick the best answer.) • Yes. • No, because then no bear could dislike honey. • No, because then all wolves would have to like honey. • No, because then there could be no wolves. • There’s not enough information to tell.

  44. Idioms: “Restricting the Domain of a Universal” Let A be the set of animals. Honey(x) means “x likes honey” Bear(x) means “x is a bear” “Every bear likes honey.” So, we must let wolves (and other non-bears) off the hook:“Every animal likes honey (or isn’t a bear so we don’t care).” x  A, Honey(x)  ~Bear(x). Every animal likes honey… or isn’t a bear (so we don’t care).

  45. Idioms: “Restricting the Domain of a Universal” Let’s play with logical equivalences… “Every bear likes honey.” x  A, Honey(x)  ~Bear(x)  x  A, ~Bear(x)  Honey(x) [by COM]  x  A, Bear(x)  Honey(x) [by IMP] For every animal: if it’s a bear, it must like honey. (If it’s not a bear, we don’t care whether it likes honey.)

  46. Idioms: “Restricting the Domain of a Universal” More generally: Given set D and predicate P(x), you want to say “All <things in D for which P is true> have the property Q.” You say: x  D, P(x)  Q(x). All <things in D for which P is true>…

  47. Idioms Questions Let P be the set of Racket Programs. Let Buggy(x) be true iff the program x contains errors. Let Terminate(x) be true iff the program x ever terminates (finishes running). Which of these means: “Every non-terminating program is buggy.” • p  P, ~Terminate(p)  Buggy(p). • p  P, ~Terminate(p)  Buggy(p). • p  P, ~Terminate(p)  Buggy(p). • p  P, ~Terminate(p)  Buggy(p). • None of these.

  48. Idioms Questions Let P be the set of Racket Programs. Let Buggy(x) be true iff the program x contains errors. Let Terminate(x) be true iff the program x ever terminates (finishes running). Which of these means: “Some buggy program does not terminate.” • p  P, Buggy(p)  ~Terminate(p). • p  P, Buggy(p)  ~Terminate(p). • p  P, Buggy(p)  ~Terminate(p). • p  P, Buggy(p)  ~Terminate(p). • None of these.

  49. Idioms: “At Least One”, “At Least Two” “At least one animal likes honey.” means the same as “Some animal likes honey.” x  A, Honey(x). “At least two animals like honey.” means “Some animal and some other animal like honey.” or“Some animal and some animal that is not the same animal like honey.” Just “restricting the domain”!

  50. Idioms: “At Least One”, “At Least Two” “At least one animal likes honey.” means the same as “Some animal likes honey.” x  A, Honey(x). “At least two animals like honey.” means “Some animal and some other animal like honey.” or“Some animal and some animal that is not the same animal like honey.” x  A, Honey(x)  y  A, x  y  Honey(y)  x  A, y  A, x  y  Honey(x)  Honey(y). Just “restricting the domain”! (We can move Honey(x) inside the  that doesn’t mention x,but beware of negations, (bi)conditionals, and XORs!)

More Related