810 likes | 1.01k Views
CPSC 121: Models of Computation 2010 Winter Term 2. 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
E N D
CPSC 121: Models of Computation2010 Winter Term 2 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 • Liszt Etudes • Sorted Lists • Comparing Algorithms • Next Lecture Notes
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.
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.
Outline • Prereqs, Learning Goals, and Quiz Notes • Prelude: Motivation, Scope & Defining Predicates • Problems and Discussion • Liszt Etudes • Sorted Lists • Comparing Algorithms • Next Lecture Notes
Limitations of Propositional Logic as a Model Which of the following can propositional logic model effectively? • Relationships among factory production lines (e.g., “wheel assembly” and “frame welding” both feed 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.
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 • Generalizations about patterns • Infinite domains • Generally, problems where the properties of different concepts, ideas, parts, or entities depend on each other. array doubling pre/post conditions not x and y => not x or not y (TODO!!!??)
But... Would You Ever Really Use Pred Logic Like This? • 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...”
Quantifier Scope A quantifier applies to everything to its right until a closing parenthesis stops it.
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).
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).
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 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)).
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))).
A Bit of Semantics: Unbound Variables What is the truth value of the following formula? x Z, x*x = y. • True, because (for example) 5*5=25. • False, because (for example) no integer multiplied by itself equals 3. • It depends on y, but given a value for y, we could calculate a truth value. • It depends on y, but we may also need additional information. • None of the above. 5 5
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 y Z, x Z, x*x = y. false 5 5
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. PerfectSquare(25). true PerfectSquare(3). false y Z, PerfectSquare(y). false 5 5
Unbound Variables Check Which variable(s) does this formula’s truth depend on? i Z+, (i > n) ~v Z0, Elt(a, i, v). • i and v • a and n • n and v • i and n • None of these is correct.
Outline • Prereqs, Learning Goals, and Quiz Notes • Prelude: Scope and Predicate Definition • Problems and Discussion • Liszt Etudes • Sorted Lists • Comparing Algorithms • Next Lecture Notes
Defining 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 1. 1 2 3 4 5 6 2 4 5 7 6 10 mylist Elt(mylist, 3, 5) is true. Elt(mylist, 2, 1) and Elt(mylist, 7, 2) are false.
List Element Warmup Elt(a, i, v) list a has value v at index i. Which of the following could describe a valid list (assume all other Elt(x, y, z) are false)? • Elt(list, 1, 7), Elt(list, 2, 4), Elt(list, 1, 3) • Elt(list, 1, 7), Elt(list, 2, 4), Elt(list, 4, 3) • Elt(list, -1, 7), Elt(list, 0, 4), Elt(list, 1, 3) • Nothing. (i Z+, v Z, ~Elt(list, i, v).) • None of these is a valid list.
List Element Warmup Elt(a, i, v) a has value v at index i. Let A be the set of all lists. Which of these means: a list cannot have more than one element at any index. • a A, i Z+,v1 Z,Elt(a,i,v1) ~v2 Z,Elt(a,i,v2). • a A, i Z+,v1 Z,v2 Z,Elt(a,i,v1) Elt(a,i,v2). • a A, i Z+,v1 Z,v2 Z,Elt(a,i,v1) Elt(a,i,v2) v1 = v2. • Both a and c. • All of these We will assume henceforth that a list cannot have more than one element at any index.
List Length Warmup Elt(a, i, v) a has value v at index i. Let Length(a, n) mean list a is n items long. What does a A, n Z0, Length(a, n) mean? • Every list is n items long. • There are many lists that are n items long. • Every list has a length. • No list has more than one length. • None of these. We will assume henceforth that this statement is true.
List Length Properties Which of the following should not be true of the length of a list? • Every list should have a length. • No list should have more than one length. • No list should have elements at any index larger than its length. • A list should have an element at every index up to its length. • All of these should be true.
No More than One Length Which of the following means that a list should not have more than one length? • a A, n1 Z0, Length(a,n1) ~n2 Z0, n1 n2 Length(a,n2). • a A, n1 Z0, n2 Z0, n1 n2 Length(a,n1) Length(a,n2). • a A, n1 Z0, n2 Z0, Length(a,n1) Length(a,n2) n1 = n2. • Both a and c. • All of these
Defining Length(a, n) Consider: Length(a,n) i Z+, (v Z0, Elt(a, i, v)) (i n). In English: There’s an element at i if and only if i is a valid index (no greater than the list’s length). Why does this guarantee that no list has more than one length? • Because it states that no list has more than one length. • Because a list with two lengths would now both have and not have a value at some entry. • Because i now cannot be greater than the length of the list. • None of these, but it is guaranteed. • None of these, and it’s not guaranteed. We will assume henceforth that this statement is true.
Outline • Prereqs, Learning Goals, and Quiz Notes • Prelude: Scope and Predicate Definition • Problems and Discussion • Liszt Etudes • Sorted Lists • Comparing Algorithms • Next Lecture Notes
? Problem: Sorted Lists Problem: Give a definition for the predicate Sorted(a) in terms of Elt(a, i, v). Assume lists cannot have more than one element at an index, every list has a length, and lists are “filled” to their length.
? Problem: Sorted Lists Problem: Give a definition for the predicate Sorted(a) in terms of Elt(a, i, v). Which of the following is a problem with this definition? Sorted(a) i Z+, Elt(a,i,v1) < Elt(a,i+1,v2). • a isn’t quantified. • v1 and v2 aren’t quantified. • We can’t use < on Elt (or any other predicate) • a and b • b and c
? Problem: Sorted Lists Problem: Give a definition for the predicate Sorted(a) in terms of Elt(a, i, v). What’s wrong with the following definition? Sorted(a) i Z+, v1 Z, v2 Z, Elt(a,i,v1) Elt(a,i+1,v2) v1 < v2. • It’s missing quantifiers. • It’s too restrictive (e.g., for equal values). • It doesn’t handle the “boundary case” when i=length. • a and b • b and c
But... Would You Ever Really Use Pred Logic Like This? • 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...”
Predicate Logic Patterns “There exists” means “there’s at least one”. We often want “there’s exactly one” (e.g., lists have exactly one element at each valid index). Common problems like this lead to common patterns to solve them. • “At least one” plus “at most one” means “exactly one”. • “At most one” is: if any two items have this property, then they’re really the same item. • “Two distinct” or “at least two” is: exists one, exists another, such that the first is not equal to the second
Intuition ♥ Formality “...when we become comfortable with formal manipulations, we can use them to check our intuition, and then we can use our intuition to check our formal manipulations.” • Epp (3rd ed), p. 106-107 We’ll often use predicate logic informally in the future, but the ability to express and reason about ideas formally keeps us honest and helps us discover points we may overlook otherwise.
Outline • Prereqs, Learning Goals, and Quiz Notes • Prelude: Scope and Predicate Definition • Problems and Discussion • Liszt Etudes • Sorted Lists • Comparing Algorithms • Next Lecture Notes
Efficiency of Algorithms 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. 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.
Which algorithm is generally faster? (a)Ask each 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. (b) 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. (c) Neither.
Concrete Examples: 10 students Say checking if a class on a list is CPSC 121 takes 1 second and checking if a classmate is in your MUG takes 1 second. Algorithm (a) takes ~10*12*5 seconds = 10 minutes. Algorithm (b) takes ~10*10 seconds < 2 minutes.
Concrete Examples: 100 students Say checking if a class on a list is CPSC 121 takes 1 second and checking if a classmate is in your MUG takes 1 second. Algorithm (a) takes ~100*12*5 seconds = 100 minutes. Algorithm (b) takes ~100*100 seconds 167 minutes.
Concrete Examples: 400 students Say checking if a class on a list is CPSC 121 takes 1 second and checking if a classmate is in your MUG takes 1 second. Algorithm (a) takes ~400*12*5 seconds 7 hours. Algorithm (b) takes ~400*400 seconds 44 hours.
Which algorithm is generally faster? (a)Ask each 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. (b) 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. (c) Neither.
Comparing at One Input Size Let 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. time Alg A Alg B problem size We’ll assume the Faster predicate is given to us.
How Faster Works (1 of 3) Faster(a1,a2,n): a1 is faster than a2 at size n. Which of the following means “no algorithm is ever faster than itself”? • n Z+, a A, ~Faster(a,a,n). • n Z+, a1 A, a2 A, Faster(a1,a2,n) ~Faster(a2,a1,n). • n Z+, a1 A, a2 A, a3 A, Faster(a1,a2,n) Faster(a2,a3,n) Faster(a1,a3,n). • None of these. time Alg A Alg B We will assume this statement is true. problem size
How Faster Works (2 of 3) Faster(a1,a2,n): a1 is faster than a2 at size n. Which of the following means “two algorithms cannot be faster than each other”? • n Z+, a1 A, a2 A, Faster(a1,a2,n) ~Faster(a2,a1,n). • n Z+, a1 A, a2 A, Faster(a1,a2,n) ~Faster(a2,a1,n). • n Z+, a1 A, a2 A, Faster(a1,a2,n) Faster(a2,a1,n). • b and c. • All of these. time Alg A Alg B We will assume this statement is true. problem size
How Faster Works (3 of 3) Faster(a1,a2,n): a1 is faster than a2 at size n. What does the following statement mean? n Z+, a1 A, a2 A, a3 A, Faster(a1,a2,n) Faster(a2,a3,n) Faster(a1,a3,n). • Three algorithms cannot be faster than each other. • Three algorithms are the same “speed”. • Of any three algorithms, one is the fastest. • An algorithm cannot be faster than itself. • None of these. time Alg A Alg B We will assume this statement is true. problem size
How Faster Works (4 of 3, bonus!) Faster(a1,a2,n): a1 is faster than a2 at size n. We assume Faster is: • “Anti-reflexive”: No algorithm is faster than itself. • “Anti-symmetric”: No two algorithms are faster than each other. • “Transitive”: If one algorithm is faster than a second, which is faster than a third, then the first algorithm is faster than the third. (We can “chain” fasters together, analagously to the transitivity rule for propositional logic.) time Alg A Alg B problem size
General Efficiency of Algorithms Faster(a1,a2,n): a1 is faster than a2 at size n. Problem: Create a definition of GenerallyFaster(a1, a2) in terms of Faster(a1, a2, n) that you can live with. time Alg A Alg B problem size
Desirable Properties of Generally Faster Which of these properties should Generally Faster not share with Faster? • Anti-reflexivity • Anti-symmetry • Transitivity • Should share all of these. • Should share none of these. time Alg A Alg B problem size
English-Language Generally Faster Definitions Which one do you want? [Your definitions here.] • None of these. time Alg A Alg B problem size
FormalGenerally Faster Definitions Which one do you want? [Your definitions here.] • None of these. time Alg A Alg B problem size
Which algorithm is generally faster? (a)Ask each 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. (b) 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. (c) Neither. time Alg A Alg B problem size