CSCI 2824: Lecture 4In this lecture, we will cover the following topic:
PredicatesIn the previous lecture, we looked at propositions. Propositions are statements for which we can give true or false valuation. We will now generalize propositions to predicates. Predicates should be familiar from english grammar. Thus, the predicate can be informally expressed as: x is going to school, where x is a variable that can stand for a person. In logic, we represent the predicate as: isGoingToSchool(x) isGoingToSchool is the predicate and x is a variable. If we fill in values for x, such as Tom, Susan, My Fish and so on, we obtain propositions that can be given true or false valuations:
A one argument predicate has a single variable . Whenever is substituted by an object, then it results in a proposition that can be given true or false valuaton. A common example of a single argument predicate over numbers is the isPrime(x) predicate stands for a proposition of the form number x is prime.
Multiple Argument PredicatesThere can be multiple argument predicates. Let us imagine a predicate with two arguments: John is the son of Catherine. Let us imagine a two argument predicate Son(,) that denotes that whatever object is substituted for is the son of the object that is substituted for . Therefore, we can write: Son(John, Catherine) Example-1:Let stand for the statement: __<x>__ is mortal . Q: What is ? A: The proposition Socrates is mortal. We can use predicates with the variables substituted by objects inside propositional formulas. Socrates is mortal AND he NOT is the son of Zeus. Example-2:Predicates can be arithmetic in nature. Here are some predicates over numbers:
Note that we can plug in various numbers and find out if the resulting statement is true of false.
Example-3:Let stand for the statement: __<x>__ is the father of __<y>__ . Q: Using the predicate above, how do we express Darth is the father of Luke? A: . And the same applies to predicates over numbers:
Example-4:We can now bring in all the propositional connectives and combine them with predicates. Q: How do we say that ( DarthV is the father of Luke OR Anakin is the father of Luke) AND (Luke is NOT the father of Darth) using the Father predicate from the previous example? A: . Example-5:Let be a predicate over numbers that stands for is prime. Which of the following formulae are true over numbers?
Answers:
Redo the example with the following interpretation of : . Predicates in ProgramsIn general, predicates appear all over the place in programs. A predicate is a function that returns a Boolean. Here is an example predicate over strings in Python. Example Predicate
import re def stringPredicate(x): # Predicate is true only if string has the pattern # a followed by a string of bs and then an a, somewhere in it. m = re.search('ab+a',x) # A Predicate returns a Boolean if (m == None): return False else: return True #Take a list of words lst=['abba','maama mia','abbby', 'abbbbba','abbacadabra','dancing queen'] #Filter the list through a predicate newList = filter(stringPredicate,lst) Some common reasons to define predicates include:
Rather than write special routine for every operation, you will learn to use in-built filtering, search or sorting routines that can use any predicate. Languages like C (STL) and Java support these operations for a wide variety of data structures based on predicates that users can define. A function that defines a predicate must be free of side effects, or in other words, a pure function. You will learn more about side effects in your future programming languages or software engineering classes. Predicates and SetsWe can use predicates to select elements from a set. Let us fix a set . We will present this in detail when we discuss sets.
Application Mention SQL in databases. Here is what a query looks like (syntax may be a little off). SQL Query
select (name, age) from db where ( name[1]=="z" && age < 20) Negation of Predicates and FormulasThe negation of predicate is written as . However, this can be simplified further by applying DeMorgan's Laws. Eg., Consider the predicate . It is cumbersome to write everywhere. Instead, we write . In other words, we say that the predicate represents the negation of . Starting from predicates, we can extend negations to formulae. What is the negation of: Answer: Note how the changes to a while negation. Rules for negation:
ExampleWrite down the negation of the following formulae: Q1: . A: . Q2: . A: . QuantifiersThe power of predicates arise from our ability to quantify. Let us illustrate quantification with an example. How do we express the statement: For all natural numbers , is divisible by OR is divisible by ? If we forget the for all in the first place, we can write This is a formula involving a predicate that allows us to plug-in values for . But the original statement was meant to hold for all possible values of . We use the universal or forall quantifier to say:
The other quantifier used is the existential or the exists quantifier. As an example, we wish to say: There is a number that is even and prime. The symbol stands for exists. Notice that it is an inverted . Quantified FormulaWhenever there is a quantifier in front of a formula, it is called a quantified formula. As an example consider the formula: In this formula the variable is called a bound or a quantified variable. The variable y is called a free variable. We usually write formulas as to denote that is a free variable in the formula. For example, which of the following are true for the formula above:
Formulas can have multiple quantifiers in front of them: Q Paraphrase this formula in plain english. A For every number there exists a number such that and is prime. Q Express this sentence in Logic: “ Forall persons x,y,z, If (x is the parent of y) and ( y is the parent of z) then (x is the grandparent of z) ”. Use predicates “P(x,y)” for parent and “GP(x,y)”, grand parent. Q Define the formula to express that is a sibling of using the parent relation, where two persons are siblings if they have a parent in common. A: |