Predicate Calculus Calculus • What does calculus mean? – Comes from the word “stone” – Implies a process of calculating Kidney stone • Lots of calculus studies … – – – – – Differential calculus Integral calculus Relational calculus Propositional calculus Predicate calculus • Predicate calculus is a generalization of propositional calculus Predicate Calculus • Also called Predicate or First-Order Logic • Contains all of the components of propositional calculus… • Together with: – Predicates – A universe of discourse (UofD) – Terms – Quantifiers Predicates • A predicate is a statement that is either true or false and has zero or more arguments • A predicate has a name followed by a list of arguments enclosed in parentheses and is called an atomic formula Examples: Jane is the mother of Mary isMother(Jane, Mary) M(j, m) • Atomic formulas can be combined by logical connectives Example: isMother(Jane,Mary) isMother(Mary,Jane) • If all arguments of a predicate are individual constants, the resulting atomic formula must either be true or false Examples: Jane is the mother of Mary = T isMother(Jane, Mary) = T isMother(Mary, Jane) = F • The number and order of predicate arguments is significant • The number of elements in the predicate list is called the arity of the predicate UofD, Terms, Quantifiers • The Universe of Discorse (UofD) is a set of values – The UofD represents all values being considered – The UofD is sometimes called the domain of interest, or simply the domain • Arguments in predicates can be constants (values in the UofD), variables (whose value assignments come from the UofD), or terms (expressions that evaluate to values in the UofD) • Quantifiers give us a way to evaluate predicate calculus formulas with variables that range over the entire UofD Discussion #14 Chapter 2, Section 1 5/20 Predicate Evaluation (I) • Sometimes we know the “meaning” but we don’t know which assignments hold until we are told • For example: UoD = {Jim, Sally, Sara, Zed} siblingOf(x,y) siblingOf Jim Sally Sara Zed Jim F F T T Sally F F F F Sara T F F T Zed T F T F Predicate Evaluation (II) • Sometimes we don’t know the “meaning” but we are “given” the assignments • For example: UoD = {a, b, c} Facts: P(x, y) P a b c a F F T b F F T c T F F P(b, c) P(c, a) P(a, c) • Under a “closed world assumption,” we only need to list the facts (substitutions that evaluate to True). All others are False. Instantiation • Instantiation is the substitution of a constant for a variable (or in general, the substitution of a term, which is an expression that yields a constant) • Sxt A means substitute term t for all variables x in A • Sxt A is called an instantiation of A and t is said to be an instance of x • Examples: Sx3 P(x, y) = P(3, y) Sx3+1 P(x, y, z, x) = P(3+1, y, z, 3+1) = P(4, y, z, 4) Universal Quantification (I) • Let A be an expression, and let x be a variable. If we want to say that P(x) is true for all substitutions of values for x in the UofD, we write xP(x). • The symbol is pronounced “for all” and is called the universal quantifier. • Examples: – All cats have tails, x(cat(x) hasTail(x)). – For every integer x, x+1 > x, x(>(x+1, x)). Universal Quantification (II) • x P(x) is shorthand for: – x P(x) = P(a) P(b) P(c) with UoD = {a, b, c}. – x P(x) = P(0) P(1) … with UoD = non-negative integers. • x P(x) = T when P(x) = T for all substitutions from the UoD. (Only need one false predicate instantiation to make the formula false.) • Examples: – x red(x) = T for UoD = red apples – x red(x) = F for UoD = apples Existential Quantification (I) • Let A be an expression, and let x be a variable. If we want to say that P(x) is true for at least one value of x, we write xP(x). • The symbol is pronounced “there exists” and is called the existential quantifier. • Examples: – Some people like apples, x(likesApples(x)). – There is an integer larger than 10, x(>(x, 10)). Existential Quantification (II) • x is shorthand for: – x P(x) = P(a) P(b) P(c) with UoD = {a, b, c}. – x P(x) = P(1) P(2) … with UoD = non-negative integers. • x P(x) = T when P(x) = T for one or more substitutions from UoD. (Only need one true predicate instantiation to make the formula true.) • Examples: – x red(x) = T for UoD = all apples – x red(x) = F for UoD = golden delicious apples Expressions with Quantifiers • Quantifiers associate right to left. – Example with UoD = {Ann, Sue, Tim} xy loves(x, y) = (x(y(loves(x, y)))) = x(loves(x, Ann) loves(x, Sue) loves(x, Tim)) = (loves(Ann, Ann) loves(Ann, Sue) loves(Ann, Tim)) (loves(Sue, Ann) loves(Sue, Sue) loves(Sue, Tim)) (loves(Tim, Ann) loves(Tim, Sue) loves(Tim, Tim)) • We say, for every x, there exists a y such that x loves y. (Everybody loves somebody.) Examples • What about yx loves(x, y)? – There exists a y such that for every x, x loves y. – Somebody is loved by everybody. – Not the same as everybody loves somebody. • What about xy loves(x, y)? – There exists an x such that for every y, x loves y. – Somebody loves everybody. • What about yx loves(x, y)? – For every y there exists an x such that x loves y. – Everybody is loved by somebody. • Order matters. Precedence Quantifiers have the highest precedence: (unary operators) yx P(x, y) Q(x) x R(x, y, z) y (x P(x, y)) Q(x) (x R(x, y, z)) (y (x P(x, y))) Q(x) ((x R(x, y, z))) (y (x P(x, y))) (Q(x) ((x R(x, y, z)))) ((y (x P(x, y))) (Q(x) ((x R(x, y, z))))) Scope, Bound, and Free • Scope defines extent. Parentheses define scope, and precedence dictates how to insert parentheses. • Bound variables define “sameness”. – A variable is bound if it “is introduced by” a quantifier. – A variable remains bound throughout the scope of the quantifier unless rebound by another quantifier in a nested sub formula. • Any variable that is not bound is said to be free. • We can consider bound variables to be local to the scope of the quantifier, just as parameters and locally declared variables in procedures are local to the procedure in which they are declared. • If several quantifiers use the same bound variable for quantification, then all those variables are local to their scope and are distinct. Example Which variables are bound and which are free? y x (P(x, y) (Q(x) x R(x, y, z))) Example Which variables are bound and which are free? y is bound y x (P(x, y) (Q(x) x R(x, y, z))) x is bound different x’s x is bound z is free Interpretations Providing Meaning • Consider the problem of giving meaning to the expression: sibling(x, Lynn) married(x). – Can’t just assign T or F to a predicate expression with variables – Truth depends on the values assigned to the variables • E.g., assign Zed to x; then if Zed is indeed Lynn’s sibling and is married, we can say that this expression is true. • E.g., for x(sibling(x, Lynn) married(x)), we can look through the list of all possibilities (i.e. look through the domain) and see if at least one of them is a sibling of Lynn and is married; if so we can say that this expression is true. • To provide an interpretation, we need – A domain that provides values for the arguments of the predicate – A way to determine the truth value of all predicates for each possible assignment of domain values to the variables Interpretation (I) • An interpretation for an expression E – Specify a domain, D. – For each predicate of E, specify T or F for every possible substitution. – Select a value in D for each free variable, if any. • Example: yP(x, y) D = {1, 2} P(x, y) = ? 1 1 T 1 2 F 2 1 F 2 2 F x = 1: yP(x, y) = P(1, 1) P(1, 2) = T F = T x = 2: yP(x, y) = P(2, 1) P(2, 2) = F F = F Interpretation (II) • An interpretation for an expression E – Specify a domain, D. – For each predicate of E, specify T or F for every possible substitution. – Select a value in D for each free variable, if any. • Example: yP(x, y) D = {1, 2} P(x, y) = ? 1 1 T 1 2 F 2 1 T 2 2 F x = 1: yP(x, y) = P(1, 1) P(1, 2) = T F = T x = 2: yP(x, y) = P(2, 1) P(2, 2) = T F = T Observe that the truth of a statement depends on the interpretation. Interpretation (III) • An interpretation for an expression E – Specify a domain, D. – For each predicate of E, specify T or F for every possible substitution. – Select a value in D for each free variable, if any. • Example: xyP(x, y) D = {1, 2} P(x, y) = ? 1 1 T 1 2 F 2 1 F 2 2 F xyP(x, y) = x(P(x, 1) P(x, 2)) = (P(1, 1) P(1, 2)) (P(2, 1) P(2, 2)) = (T F) (F F) = T F = F Interpretation (IV) • An interpretation for an expression E – Specify a domain, D. – For each predicate of E, specify T or F for every possible substitution. – Select a value in D for each free variable, if any. • Example: xyP(x, y) D = {1, 2} P(x, y) = ? 1 1 T 1 2 F 2 1 T 2 2 F xyP(x, y) = x(P(x, 1) P(x, 2)) = (P(1, 1) P(1, 2)) (P(2, 1) P(2, 2)) = (T F) (T F) = T T = T Closed World Assumption • With the closed world assumption, we only give the substitutions that evaluate to true — all others are assumed to be false. • Let the domain, D = {1, 2}, then if we write: P(x, y) 1 1 1 2 or P(1, 1) P(1, 2) • Then with the closed world assumption, this is simply shorthand for writing: P(x, y) = ? or 1 1 T 2 1 F 1 2 T 2 2 F P(1, 1) = T P(2, 1) = F P(1, 2) = T P(2, 2) = F Closed World Assumption Notes • Our project uses the closed world assumption. – Only the substitutions that hold are given. – These are called facts. • Real-world databases use the closed world assumption only “true” facts are stored. • Contrary to the closed world assumption, the open world assumption says that if a fact is not stated, we do not know whether it is true or false. – The open world assumption is typical in everyday life. – It is harder to work with an open world assumption. Schemes: snap(S,N,A,P) csg(C,S,G) cp(C,Q) cdh(C,D,H) cr(C,R) cp('CS206','CS121'). cp('CS206','CS205'). cdh('CS101','M','9AM'). cdh('CS101','W','9AM'). cdh('CS101','F','9AM'). cdh('EE200','Tu','10AM'). cdh('EE200','W','1PM'). cdh('EE200','Th','10AM'). cdh('PH100','Tu','11AM'). cr('CS101','Turing Aud.'). cr('EE200','25 Ohm Hall'). cr('PH100','Newton Lab.'). Facts: snap('12345','C. Brown','12 Apple St.','555-1234'). snap('67890','L. Van Pelt','34 Pear Ave.','555-5678'). snap('22222','P. Patty','56 Grape Blvd.','555-9999'). snap('33333','Snoopy','12 Apple St.','555-1234'). csg('CS101','12345','A'). csg('CS101','67890','B'). csg('EE200','12345','C'). Rules: csg('EE200','22222','B+'). WhoGradeCourse(N,G,C):-csg(C,S,G),snap(S,N,A,P). csg('EE200','33333','B'). before(C1,C2):-cp(C2,C1). csg('CS101','33333','A-'). before(C1,C2):-cp(C3,C1),before(C3,C2). csg('PH100','67890','C+'). cp('CS101','CS100'). Queries: cp('EE200','EE005'). WhoGradeCourse('Snoopy',G,C)? cp('EE200','CS100'). WhoGradeCourse(N,'A','CS100')? cp('CS120','CS101'). WhoGradeCourse(N,'A',C)? cp('CS121','CS120'). before('CS100','CS206')? cp('CS205','CS101'). before('CS100',X)? Example #1 (Class Project) • Query: What are the prerequisites of EE200? • Translated to predicate logic, we are asking for: cp('EE200', x) where: cp(course, prerequisite) • We need to find the substitutions for the free variable x, if any, that make this true. • Interpretation for the project: – Domain = all constant strings in the Facts – Closed world assumption holds (if stated as a fact, then T; otherwise, F). Example #1 (cont’d) • Check all substitutions for cp('EE200', x) from the domain for x: cp Facts: cp('EE200','10AM') = F cp('EE200','11AM') = F cp('EE200','12 Apple St.') = F … cp('EE200','CS100') = T x = 'CS100' … cp('EE200','EE005') = T x = 'EE005' … • Also, – x cp('EE200', x) = T – x cp('EE200', x) = F – x cp('CS100', x) = F cp('CS101','CS100'). cp('EE200','EE005'). cp('EE200','CS100'). cp('CS120','CS101'). cp('CS121','CS120'). cp('CS205','CS101'). cp('CS206','CS121'). cp('CS206','CS205'). Note: Untyped Logic Example #2 (Class Project) • Query: Where am I likely to find Charlie Brown ('12345') on Wednesday ('W') at 1 PM ('1PM')? • Translated to predicate logic, we are asking for: xz(csg(x,'12345',z) cr(x,r) cdh(x,'W','1PM')) where: csg(course, studentID, grade) cr(course, room) cdh(course, day, hour) r is a free variable Example #2 (cont’d) Check substitutions for all combinations of values from the domain for x, z, and r: … csg('10AM','12345','10AM') cr('10AM','10AM') cdh('10AM','W','1PM') = F csg('10AM','12345','10AM') cr('10AM','11AM') cdh('10AM','W','1PM') = F csg('10AM','12345','10AM') cr('10AM','12 Apple St.') cdh('10AM','W','1PM') = F … csg('10AM','12345','11AM') cr('10AM','10AM') cdh('10AM','W','1PM') = F csg('10AM','12345','11AM') cr('10AM','11AM') cdh('10AM','W','1PM') = F csg('10AM','12345','11AM') cr('10AM','12 Apple St.') cdh('10AM','W','1PM') = F … csg('11AM','12345','10AM') cr('11AM','10AM') cdh('11AM','W','1PM') = F csg('11AM','12345','10AM') cr('11AM','11AM') cdh('11AM','W','1PM') = F csg('11AM','12345','10AM') cr('11AM','12 Apple St.') cdh('11AM','W','1PM') = F … csg('11AM','12345','11AM') cr('11AM','10AM') cdh('11AM','W','1PM') = F csg('11AM','12345','11AM') cr('11AM','11AM') cdh('11AM','W','1PM') = F csg('11AM','12345','11AM') cr('11AM','12 Apple St.') cdh('11AM','W','1PM') = F … Example #2 (cont’d) • Eventually, we check the substitution x = 'EE220', z = 'C', and r = '25 Ohm Hall'. csg('EE220','12345','C') cr('EE220','25 Ohm Hall') cdh('EE220','W','1PM') = T • Thus, Charlie Brown is likely to be in 25 Ohm Hall on Wednesday at 1 PM. csg, cdh, and cr Facts: csg('CS101','12345','A'). csg('CS101','67890','B'). csg('EE200','12345','C'). csg('EE200','22222','B+'). csg('EE200','33333','B'). csg('CS101','33333','A-'). csg('PH100','67890','C+'). cdh('CS101','M','9AM'). cdh('CS101','W','9AM'). cdh('CS101','F','9AM'). cdh('EE200','Tu','10AM'). cdh('EE200','W','1PM'). cdh('EE200','Th','10AM'). cdh('PH100','Tu','11AM'). cr('CS101','Turing Aud.'). cr('EE200','25 Ohm Hall'). cr('PH100','Newton Lab.'). Validity & Equivalences Validity • An expression that is true for all interpretations is said to be valid. (A valid expression is also call a tautology.) • An expression that is true for no interpretation is said to be contradictory. (A contradictory expression is also called a contradiction.) • If A is valid, A is contradictory. (a tautology) (a contradiction) • Examples: – P(x, y) P(x, y) P(x, y) P(x, y) is valid – P(x, y) P(x, y) is contradictory Laws are valid! DeMorgan’s Laws with Quantifiers deMorgan’s laws: xA xA xA xA Proof:xP(x) (P(x1) P(x2) …) P(x1) P(x2) … xP(x) Renaming An expression with the variable names changed is called a variant. Proper variants are equivalent, i.e., it doesn’t matter what variable name is used. Example: xA ySxyA But, we must be careful 1. We must substitute only for the x’s bound by x. 2. Further, variables must not “clash.” Strong rule: y must not be in A; weaker rule: no y in the scope of x can be free in the scope of x, and no x bound by x may be in the scope of a bound y. x(y(P(y) Q(x,z)) xP(x)) w w z z y y x(yP(y) Q(x,z)) xP(x) y y Works Doesn’t work Doesn’t work Works Rectification Standardizing variables apart, also called rectification we can rename variables to make distinct variables have distinct names. (xP(x, y) xQ(y, x)) yR(y, x) free same free free (xP(x, y) zQ(y, z)) wR(w, v) Derivations Universal Instantiation (UI) xA SxtA When A is true for every instantiation, it is certainly true for some particular instantiation Example: from x Mortal(x), we can derive Mortal(Smith) Existential Instantiation (EI) xA SxbA When A is true for one or more instantiations, we can let a variable, say b, designate any one of the true instantiations. Example: from x Mortal(x), we can derive Mortal(b), where b is a variable that stands for somebody who is mortal. Caution: Because we don’t know which one(s) in the domain make A hold, we must make sure b is a new variable not one already in use. For example, if we have x Succeeds(x) and x Fails(x), then we cannot conclude Succeeds(b) and Fails(b) the first b is OK, but not the second. Existential Generalization (EG) SxtA xA If we know A is true for some particular substitution t, we know there exists at least one substitution for which A is true. Universal Generalization (UG) A xA If A holds with no restrictions on one of its variables x, it must hold for all substitutions for x. Example: from x(P(x) Q(x)) and xP(x), we can conclude P(a) Q(a) and P(a) by UI and then Q(a) by modus ponens; and thus by UG we can conclude xQ(x). Careful: from x<100, we cannot infer x x>100 by UG Derivation Example If xyz(Q(x, y) P(z)), wP(w) then xQ(x, 8). 1. 2. 3. 4. 5. 6. 7. xyz(Q(x, y) P(z)) Q(a, b) P(c) P(c) Q(a, b) wP(w) P(c) Q(a, b) xyQ(x, y) premise 1, UI Sxa, Syb, Szc 2, contrapositive premise 4, UI Swc 3&5, modus ponens 6, UG (Neither a nor b is free in a premise, nor were they introduced by EI) 8. xQ(x, 8) 7, UI Sy8 Note: in Step 2, we could have used Sxx, Syy, Szz: the names of the variables don’t matter, so long as we follow the rules properly Unification The previous proof was basically about getting rid of universal quantifiers and then reintroducing them and about getting the variable names to match for modus ponens and for the conclusion. Since, we “play this game” over and over, we could (and should) simplify by doing two things: 1. Drop initial ’s, so long as we remember which variables are actually bound to these universal quantifiers so that we use them properly and which variables (if any) are fixed in the premises. 2. Use unification. Two expressions unify if there are legal instantiations that make them the same. Simplified Derivation Example If Q(x, y) P(z), P(w) then Q(x, 8). 1. 2. 3. 4. 5. 6. Q(x, y) P(z) P(z) Q(x, y) P(w) P(z) Q(x, y) Q(x, 8) premise 1, contrapositive premise 3, unification with 2 2&4, modus ponens 5, instantiation Informally, we can see that this works: If 2 & 3 are true for all substitutions, then we can always choose the w to be the same as z and be guaranteed that they will both be true (assuming the premises are true). Note that the UI and UG are “hidden” within the unification Resolution Programming a Computer to do Proofs Too much work to program all the possibilities we have considered. We need a better way. 1. Better = more uniform not so many cases (even though it may sometimes be longer). 2. Better = fewer rules of inference. 3. Better = a heuristic guide to lead us to the conclusion. 4. Better = easier to convert to an algorithm. Consider… PA P B AB Prove that this rule of inference is valid. It is Valid P A B (P A) (P B) AB T T T T T F T T T T T F T F F F T T T F T T T F T T T T F F T F F F T F F T T T T T T T T F T F T T T T T T F F T F F T T T T F F F F F T T T F Using your new rule, rewrite Modus ponens Modus tollens Hypothetical syllogism P PQ Q P QP Q PQ QR PR PF P Q QF P F Q P Q F P Q Q R P R We now have one rule to rule them all! Resolution Rule literals PA P B AB clauses: A and B will always be disjunctions of literals or just a literal or possibly missing. This says: In two disjunctive clauses, if we have complementary literals, we can discard them and “OR” the remaining clauses. Using Resolution Uniform: Only disjunctions of literals in every rule Fewer Rules: Only one inference rule Heuristic Guide: Reduce the number of literals with the goal of reaching False Algorithmic: 1. 2. 3. 4. 5. Negate the conclusion and add it as a premise. Convert the premises to CNF (conjunction of disjunction of literals). Write each premise (which is a disjunction of literals) as a line of the proof. Repeatedly apply resolution (the one inference rule) & simplify as needed. Success iff F is reached. Converting Expressions to DNF or CNF Recall from our earlier discussion that the following procedure converts an expression to DNF or CNF: 1. Remove all and . 2. Move inside. (Use De Morgan’s law.) 3. Use distributive laws to get proper form. Simplify as you go. (e.g. double-neg., idemp., comm., assoc.) Example #1 If P Q, Q R, P then R. 1. Negate the conclusion (R becomes another premise). 2. Convert to CNF: (P Q) (Q R) P R 3. Write the premises as the first lines of the proof. 4. Do resolution. 1. 2. 3. 4. 5. 6. 7. P Q Q R P R P R R premise premise premise premise resolution 1,2 resolution 3,5 resolution 4,6 empty = F } Sometimes called the support. Example #2 If P (Q R), R Q then P. 1. Negate conclusion: P P 2. Convert to CNF: (P Q) (P R) R Q P 1. 2. 3. 4. 5. 6. 7. P Q P R R Q P R F premise (not used could discard) premise premise premise (not used could discard) premise resolution 2,5 resolution 3,6 Also, resolution 1,5 yields Q, which need not be added to the derivation already there. Do we always need to use all the premises? If not, we can discard them from the statement to be proved. Example #3 If P Q, Q P, P Q then P Q. 1. Negate conclusion: (P Q) (P Q) 2. CNF: (P Q) (Q P) (P Q) (P Q) 1. 2. 3. 4. 5. 6. 7. 8. PQ Q P P Q P Q P Q Q F premise premise premise premise resolution 1,2 (idemp. P P P) resolution 3,5 resolution 4,5 resolution 6,7 Example #4 If (P Q) (P R), P then Q R. 1. Negate conclusion: (Q R) (Q R) 2. CNF: ((P Q) (P R)) P (Q R) (P Q R) P Q R 1. 2. 3. 4. 5. 6. 7. P Q R P Q R QR R premise premise premise premise resolution 1,2 resolution 3,5 resolution 4,6 Prenex Normal Form • Prenex Normal Form preparation to do resolution in predicate calculus – All quantifiers in front – More formally: No quantifier in the scope of any logical connector (, , , , ) • Algorithm to obtain prenex normal form: 1. 2. 3. 4. Remove and Move in Rectify (standardize all variables apart) Move quantifiers to the front Prenex Normal Form – Example y(xP(x) xQ(x, y)) y(xP(x) xQ(x, y)) y(xP(x) xQ(x, y)) y(xP(x) xQ(x, y)) y(xP(x) xQ(x, y)) y(xP(x) zQ(z, y)) yx(P(x) zQ(z, y)) yxz(P(x) Q(z, y)) implication xA xA (de Morgan’s) de Morgan’s, double neg. xA xA (de Morgan’s) rectification xAB x(AB) (x not free in B) AzB z(AB) (z not free in A) Clauses in Our Language • • • • • We assume that all clauses are of the form Premise Conclusion where: Premise is a conjunction of predicates Conclusion is a single predicate We assume that all variables are universally quantified We can then discard the universal quantifiers but don’t forget them. One consequence is that variables are local to their own clause (e.g., the x’s in P and Q are the same x, but the x’s in P and R are different). P(x,y) Q(x,z) R(z) (rule) T P(a,b) (P(a,b) must be true: fact) R(x) F (not sure about R(x): query) Finally, we modify the syntax so that it corresponds to our language. R(z) :- P(x,y),Q(x,z) P(a,b). R(x)? Use resolution to compute Example If R(x,y) P(x,z) Q(z,y) P(a,b) Q(b,j) Domain = {a,b,j,k} Q(b,k) Then R(a,j) Unif. Unif. 1. P(x,z) Q(z,y) R(x,y) 2. P(a,b) 3. Q(b,j) 4. Q(b,k) 5. R(a,j) 6. P(a,z) Q(z,j) R(a,j) 7. P(a,z) Q(z,j) 8. P(a,b) Q(b,j) 9. Q(b,j) 10. (succeed!) conclusion UI, Sxa Syj res 5,6 Szb res 2,8 res 9,3 Example: Same, but w/Meaning If aunt(x,y) sister(x,z) parent(z,y) sister(ann,bob) parent(bob,jay) Domain = {ann,bob,jay,kay} parent(bob,kay) Then aunt(ann,jay) 1. sister(x,z) parent(z,y) aunt(x,y) 2. sister(ann,bob) 3. parent(bob,jay) 4. parent(bob,kay) 5. aunt(ann,jay) conclusion 6. sister(ann,z) parent(z,jay) aunt(ann,jay) UI, Sxann Syjay 7. sister(ann,z) parent(z,jay) 8. sister(ann,bob) parent(bob,jay) 9. sister(ann,bob) 10. (succeed!) res 5,6 Szbob res 3,8 res 2,9 Example: In Our Language Facts: sister('ann','bob'). parent('bob','jay'). parent('bob','kay'). Rules: aunt(x,y) :- sister(x,z), parent(z,y). Queries: aunt('ann','jay')? Observe: for aunt('ann','jay') we have exactly the previous proof! Output: yes. Example: with Variables What do we do for aunt('ann',x)? – Try all proofs with all substitutions from the domain if a substituted value v for x works, output v. – What’s the domain? Usual choice is all constants in the facts = {'ann', 'bob', 'jay', 'kay'} Try first to prove aunt('ann','ann'). 1. 2. (using resolution) aunt('ann','ann') conclusion aunt('ann','ann') :- sister('ann',z), parent(z,'ann’) Sxann Now observe that the resolution of 1 & 2 is sister('ann',z) parent(z,'ann') (sp)a (sp)a And that this is only F if both sister('ann',z) and parent(z,'ann') are T. Thus, we can essentially start over and prove these using resolution one at a time as sub-proofs. 1. aunt('ann',x) 2. aunt('ann','ann') 3. aunt('ann','ann') :- sister('ann',z), parent(z,'ann’) 3a. sister('ann',z) 3b. sister('ann','ann') 3c. (fail!) conclusion Sxann Sxann Syann conclusion Szann can’t resolve, no fact: sister('ann','ann') So, we try the next element of the domain for z, namely, bob. 3a. sister('ann',z) conclusion 3b. sister('ann','bob') Szbob 3c. (succeed!) resolution with fact: sister('ann','bob') Great! Let’s try the 2nd half with bob (the z’s must be the same). 3d. parent(z,'ann') conclusion 3e. parent('bob','ann') Szbob 3f. (fail!) again, no fact: parent('bob','ann') In fact, z='jay', z='kay' all fail on both sister and parent. So, we backtrack to step 2 and try the next element of the domain for x, namely, 'bob'. Continuing… 2. aunt('ann','bob') conclusion w/Sxbob 3. aunt('ann','bob') :- sister('ann',z), parent(z,'bob'). Sxann Sybob 3a. sister('ann',z) 3b. sister('ann','ann') 3c. (fail!) conclusion Szann no fact: sister('ann','ann') So, we try the next element of the domain for z, namely, bob. 3a. sister('ann',z) 3b. sister('ann','bob') 3c. (succeed!) conclusion Szbob res with fact: sister('ann','bob') 3d. parent(z,'bob') 3e. parent('bob','bob') 3f. (fail!) conclusion Szbob no fact: parent('bob','bob') Further, z='jay' fails, z='kay' fails, so, we backtrack again. Continuing with next element in domain for x, namely 'jay' … 2. aunt('ann','jay') Sxjay 3. aunt('ann','jay') :- sister('ann',z), parent(z,'jay’) Sxann Syjay 3a. sister('ann',z) conclusion 3b. sister('ann','ann') Szann 3c. (fail!) no fact: sister('ann','ann') 3a. sister('ann',z) conclusion 3b. sister('ann','bob') Szbob 3c. (succeed!) res with fact: sister('ann','bob') 3d. parent(z,'jay') conclusion 3e. parent('bob','jay') Szbob 3f. (succeed!) res with fact: parent('bob','jay') So, both sub-proofs are satisfied and we output “x='jay'”. Are there any more? From what point do we continue? We can continue as if we had failed, choosing the next element in domain for z, namely 'jay' … 3a. sister('ann',z) 3b. sister('ann','jay') 3c. (fail!) conclusion Szjay no fact: sister('ann','jay') 3a. sister('ann',z) 3b. sister('ann','kay') 3c. (fail!) conclusion Szkay no fact: sister('ann','kay') So, we again backtrack to step 2 and the next element for x … (Alternatively, since we have succeeded, we can jump back to the query and try the next element of the domain at that level.) Try to resolve using last element of domain for x, namely, 'kay' … 2. aunt('ann','kay') Sxkay 3. aunt('ann','kay') :- sister('ann',z), parent(z,'kay’) Sxann Sykay 3a. sister('ann',z) conclusion 3b. sister('ann','ann') Szann 3c. (fail!) no fact: sister('ann','ann') 3a. sister('ann',z) conclusion 3b. sister('ann','bob') Szbob 3c. (succeed!) res with fact: sister('ann','bob') 3d. parent(z,'kay') conclusion 3e. parent('bob','kay') Szbob 3f. (succeed!) res with fact: parent('bob','kay') So, we output “x='kay'”. Continuing, as if we had failed, we try z = 'jay' and z = 'kay', which both fail. (Alternatively, since we succeeded, we can jump back to the query and try the next element of the domain at that level.) We’ve tried all possible substitutions, so we’re done. High-Level Overview Sx'ann' 'ann' Facts: sister('ann','bob'). parent('bob','jay'). parent('bob','kay'). Rules: aunt(x,y) :- sister(x,z), parent(z,y). Queries: aunt('ann',x)? Sx=y Sz 'ann' 'ann' 'bob' 'jay' 'kay' Fail (sister) Fail (parent) Fail (sister) Fail (sister) 'bob' 'ann' 'bob' 'jay' 'kay' Fail (sister) Fail (parent) Fail (sister) Fail (sister) 'jay' 'ann' 'bob' 'jay' 'kay' Fail (sister) Succeeds! Fail (sister) Fail (sister) 'kay' 'ann' 'bob' 'jay' 'kay' Fail (sister) Succeeds! Fail (sister) Fail (sister) Thank goodness computers can do this faster and better than we can 236 Language Datalog Prolog • Database oriented (i.e., query answering) • Does not include some features. (Prolog does have the power of procedural languages, but we won't do general I/O, arithmetic, and negation.) • For the project, we'll do exactly the language we've defined: – Syntactically taken from a real Prolog – Is an actual subset of Datalog 236 Datalog aunt('ann',x)? Facts: sister('ann','bob'). parent('bob','jay'). parent('bob','kay'). Rules: aunt(x,y) :- sister(x,z), parent(z,y). Queries: aunt('ann',x)? 1. aunt('ann','ann') goal 2. aunt('ann','ann') :- sister('ann',z),parent(z,'ann'). 2a. sister('ann','ann') 2a. sister('ann','bob') 2b. parent('bob','ann') 2a. sister('ann','jay') 2a. sister('ann','kay') subgoal subgoal subgoal subgoal subgoal Fail! Backtrack Succeed! Fail! Backtrack Fail! Backtrack Fail! Backtrack 1. aunt('ann','bob') goal 2. aunt('ann','bob') :- sister('ann',z),parent(z,'bob'). 2a. sister('ann','ann') 2a. sister('ann','bob') 2b. parent('bob','bob') 2a. sister('ann','jay') 2a. sister('ann',‘kay') subgoal subgoal subgoal subgoal subgoal Fail! Backtrack Succeed! Fail! Backtrack Fail! Backtrack Fail! Backtrack 1. aunt('ann','jay') goal 2. aunt('ann','jay') :- sister('ann',z),parent(z,'jay'). 2a. sister('ann','ann') 2a. sister('ann','bob') 2b. parent('bob','jay') Output “x='jay'” 2a. sister('ann','jay') 2a. sister('ann','kay') subgoal subgoal subgoal Fail! Backtrack Succeed! Succeed! subgoal subgoal Fail! Backtrack Fail! Backtrack 1. aunt('ann','kay') goal 2. aunt('ann','kay') :- sister('ann',z),parent(z,'kay'). 2a. sister('ann','ann') 2a. sister('ann','bob') 2b. parent('bob','kay') Output “x='kay'” 2a. sister('ann','jay') 2a. sister('ann',‘kay') subgoal subgoal subgoal Fail! Backtrack Succeed! Succeed! subgoal subgoal Fail! Backtrack Fail! Backtrack Facts: sister('ann','bob'). parent('bob','jay'). parent('bob','kay'). Rules: aunt(x,y) :- sister(x,z), parent(z,y). Queries: aunt('ann',x)? Facts: sister('ann','bob'). parent('bob','jay'). parent('bob','kay'). Rules: aunt(x,y) :- sister(x,z), parent(z,y). Queries: aunt('ann',x)? Tree View aunt('ann',x) x = 'ann' x = 'bob' x = 'jay' … sister('ann',z),parent(z,'ann'). z = 'ann' … sister('ann','ann') fail x = 'kay' … sister('ann',z),parent(z,'jay'). z = 'ann' z = 'bob' sister('ann','ann') … fail Note that we only need to keep track of one path from root to leaf at a time. sister('ann','bob'),parent('bob‘,'jay'). succeed succeed Potential Infinite Recursion 1. f(1,1). 2. f(1,2). 3. f(2,3). 4. b(x,y):-f(x,y). 5. b(x,y):-f(x,z),b(z,y). b(1,3)? b(1,3) rule 4 f(1,3) Domain = {1,2,3} rule 5 f(1,z),b(z,3) fail z=1 f(1,1),b(1,3) succeed z=2 z=3 f(1,2),b(2,3) succeed fail rule 4 Infinite recursion! Keep current path stack if recursive call already in path, fail! f(2,3) Infinite Recursion! fail f(1,3),b(3,3) succeed