CS195, Class 11: Predicates Overview: * Expanding our worldview: Predicates vs. Propositions * Evaluating Predicates + Side note: Implmenting numeric compararators in hardware. * Three-valued Boolean logic. * Short-circuit (conditional) and and or. * Quantification: Exists * Quantification: For all * Counting: Number of * Bound and unbound variables * Substitution Notes: * Andy Cook: "CS Majors are *BAD CITIZENS*!" * Sorry about yesterday's class * Questions on homework 4? * Read IEEE reals * Lots of fancy notation today; fairly straightforward concepts ---------------------------------------- Right now, we can * Write propositions that include only Boolean values and variables * Evaluate those propositions given a "state": Assignments of true and false to variables. * Transform/simplify them. * Prove them to be tautologies. Proving things in terms of 0's and 1's is a pain, so let's expand our world view. "Add integers" * Integers * Natural numbers (non-negative integers) * Sets of integers "There's only so much formality we can handle." * We all "know" what the normal comparative operations on these values are: + a < b + a <= b + a subset b + a memberof b + ... "But this is CS195" * How would you implement a < b for integers a and b? (In hardware!) * Likarish method: Repeatedly decrement b until you hit 0. If a is positive, return true; If a is negative, return false. * Stoltzfus: Subtract b from a. If the result is positive, return true. If the result is negative, return false. Once you have these comparison operations, we can make assertions about not just Booleans, but also numbers (a < b) AND (c < d) Formality: How do you define what kinds of assertions you can make? Can you write "a < b < c"? Can you write "(a < b) AND b"? Gries says: * Start with a proposition * Take as many "atomic comparisons" as you want (two values and an operation) * Substitute them for variables in the proposition * Typecheck * "PRECEDENCE IS OBVIOUS" These new things are called "Predicates" What can we do with predicates? a < b where a and b are integers a < b and a*a > c where a and b and c are integers Might start writing predicates in which we fill in some values. 0 < b 1 < b 2 < b 3 < b 4 < b Quantifiers allow us to combine lots of predicates into a new compound predicate For all (A 1 <= x < 100; Prop(x)) Prop(1) AND Prop(2) AND Prop(3) AND Prop(4) AND ... Prop(99) (A 1 <= a < 5; a < b) Exists "just like for all except you use OR" NumberOf: Counts how many times the predicate holds for (N 1 <= a < 100; a < b) Since it's a number you can use it in the various comparison operations. A predicate using NumberOf (N 1 <= a < 100; a < b) = 5 One of the more complicated ways to say "b is 6" No, I don't expect you to prove it. ---- a/b > 2 Assertions can now be true, false, or "die and crash the system" A third value: U (for undefined) How does this affect our various operations a b (a and b) (a or b) ... F F F T F U F U T F T T T U U F U T U U "For the standard operations, if either argument is undefined, the result is undefined" (verify this by checking Gries!) Two new operations: a b (a cand b) (a cor b) F F F F F T F T F U F U **** T F F T T T T T T U U T **** U F U U U T U U U U U U Question: Why not make it symmetric? (eg.t U AND F is F) Answer: "Undefined" can mean "non-terminating computation" END OF DETOUR Note that a key aspect of quantification is "substitute each number in this range for a variable": We need to define formally what it means to substitute Problem: a variable can be used in two different ways substitute 5 for i in "3 + i < a" : "3 + 5 < a" substitute 5 for i in "E 0 <= i < 10; 3 + i < a": "E 0 <= i < 10; 3 + i < a": We need something equivalent to "scoping rules" Two possible "statuses" for a variable: * bound: participates in an enclosing quantifier * unbound: anything else Logicians spent years talking about how you figure out where a variable is bound A 0 <= i < 100; (i + (N 0 <= i < 10; i > b)) > 5 Gries did not want to burden you with this confusion: "WE WON'T ALLOW IT!" Substitution is now pretty easy: To substitute x for y in exp If the expression has the form (exp1 OP exp2), substitute x for y in exp1 and substitute x for y in exp2 If the expresion has the form (OP exp1), substitute x for y in exp1 If the expression has the form (A whatever exp1) If y is bound in that thing, just use (A whatever exp1) If y is unbound, substitute x for y in exp1