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 Whenever 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( Therefore, we can write: Son(John, Catherine) Example-1:Let 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 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 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
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 Eg., Consider the predicate In other words, we say that the predicate Starting from predicates, we can extend negations to formulae. What is the negation of: Answer: Note how the 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 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 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 The symbol 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 We usually write formulas as 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 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 A:
|