Predicates and Quantifiers
https://slides.com/georgelee/ics141-predicates-quantifiers/live
Predicates
Propositional logic sometimes isn't enough
- x > 5
- If the user is an admin, then let them edit
- When is this true? When is this false?
Propositional Functions
- We can express these as functions of a variable
- Consists of a subject and predicate
- P(x) = "x > 5"
- Q(x) = "If x is an admin, then they can edit"
Complex Functions
- Functions can have more than one variable
- P(x, y) = "x + y = 3"
- Q(x, y) = "User x is the author of post y"
- Functions of n values are n-place predicates or n-ary predicates
Preconditions/Postconditions
- Establish assumptions for your code
- Given the assumptions, your code does this
- Error handling, state change, etc.
// What are the preconditions and postconditions?
public void start() {
if (!this.isStarted()) {
this.started = true;
} else {
throw new Exception("Already started");
}
}
Quantifiers
We can assign values
But some expressions have multiple valid values
P(x) = "x * x >= 0"
When is this true? When is this false?
Domain of Discourse
Set of values which we can use to quantify our function/expression. Must be stated in order to use a quantifier. Examples include "real numbers", "imaginary numbers", "users in the database", etc.
Domains can be restricted, too. For example "Given all real numbers x where x > 3" or "Given all users in the database where role = 'admin'"
Universal Quantification
For all values in a given domain, P(x) is true. Denoted as "∀". If there is an x in the domain for which P(x) is false, that value is a counterexample of "∀x P(x)".
Another way to think about it: Given (x1, x2, x3, ... xn), the universal quantification ∀x P(x) is the same as:
P(x1) ^ P(x2) ^ P(x3) ^ ... ^ P(xn)
Existential Quantification
For at least one value in a given domain, P(x) is true. Denoted as "∃". If the statement "∃x P(x)" is false for all x in the domain, then the statement is false.
Another way to think about it: Given (x1, x2, x3, ... xn), the existential quantification ∃x P(x) is the same as:
P(x1) v P(x2) v P(x3) v ... v P(xn)
Uniqueness Quantifier
- Special case of the existential quantifier
- There exists a single value that satisfies the predicate
- For all real numbers, ∃!x such that x + 1 = 0
Logical Equivalence
- Statements can be logically equivalent (given the same domain)
- Are "∃x (P(x) v Q(x))" and "∃x P(x) v ∃x Q(x)" logically equivalent?
- To prove this true, determine if statement 1 is true, then statement 2 is true and vice versa.
- To prove this false, find a counterexample
Negating Quantifiers
- We can negate quantifiers
- ¬∃x P(x) is equivalent to ∀x ¬P(x)
- ¬∀x P(x) is equivalent to ∃x ¬P(x)
- What is the negation of "For all students in this class, every student plays video games"?
-
What is the negation of "For all students in this class, there is a student that is bored"?
Predicates and Quantifiers
By George Lee
Predicates and Quantifiers
For all slides, there exists one with an animated GIF.
- 1,260