Predicate Calculus

Post on 03-Jan-2016

122 views 2 download

Tags:

description

Predicate Calculus. Calculus. What does calculus mean? Comes from the word “ stone ” Implies a process of calculating Lots of calculus studies … Differential calculus Integral calculus Relational calculus Propositional calculus Predicate calculus - PowerPoint PPT Presentation

Transcript of Predicate Calculus

Predicate Calculus

Calculus• What does calculus mean?– Comes from the word “stone”– Implies a process of calculating

• Lots of calculus studies …– Differential calculus– Integral calculus– Relational calculus– Propositional calculus– Predicate calculus

• Predicate calculus is a generalization of propositional calculus

Kidney stone

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 formulaExamples: Jane is the mother of Mary

isMother(Jane, Mary)M(j, m)

• Atomic formulas can be combined by logical connectivesExample: isMother(Jane,Mary)

isMother(Mary,Jane)• If all arguments of a predicate are individual

constants, the resulting atomic formula must either be true or falseExamples: Jane is the mother of Mary = T

isMother(Jane, Mary) = TisMother(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:

T

F

F

T

Sara

TFTSara

FFTZed

FFFSally

TFFJim

ZedSallyJimsibling

Of

UoD = {Jim, Sally, Sara, Zed}

siblingOf(x,y)

Predicate Evaluation (II)

• Sometimes we don’t know the “meaning” but we are “given” the assignments

• For example:

F

T

T

c

FTc

FFb

FFa

baP

UoD = {a, b, c}

P(x, y)Facts: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• Sx

t A is called an instantiation of A and t is said to be an instance of x

• Examples:Sx

3 P(x, y) = P(3, y)Sx

3+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 x (P(x, y) (Q(x) x R(x, y, z)))

y is bound

x is bound x is bound

z is freedifferent x’s

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 = Tx = 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 = Tx = 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) or P(1, 1)

1 1 P(1, 2) 1 2

• Then with the closed world assumption, this is simply shorthand for writing:

P(x, y) = ? or 1 1 T P(1, 1) = T 2 1 F P(2, 1) = F 1 2 T P(1, 2) = T 2 2 F 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)

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'). csg('EE200','22222','B+'). csg('EE200','33333','B'). csg('CS101','33333','A-'). csg('PH100','67890','C+'). cp('CS101','CS100'). cp('EE200','EE005'). cp('EE200','CS100'). cp('CS120','CS101'). cp('CS121','CS120'). cp('CS205','CS101').

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.').

Rules: WhoGradeCourse(N,G,C):-csg(C,S,G),snap(S,N,A,P). before(C1,C2):-cp(C2,C1). before(C1,C2):-cp(C3,C1),before(C3,C2).

Queries: WhoGradeCourse('Snoopy',G,C)? WhoGradeCourse(N,'A','CS100')? WhoGradeCourse(N,'A',C)? before('CS100','CS206')? 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('EE200','10AM') = Fcp('EE200','11AM') = Fcp('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 Facts: 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') = Fcsg('10AM','12345','10AM') cr('10AM','11AM') cdh('10AM','W','1PM') = Fcsg('10AM','12345','10AM') cr('10AM','12 Apple St.') cdh('10AM','W','1PM') = F…csg('10AM','12345','11AM') cr('10AM','10AM') cdh('10AM','W','1PM') = Fcsg('10AM','12345','11AM') cr('10AM','11AM') cdh('10AM','W','1PM') = Fcsg('10AM','12345','11AM') cr('10AM','12 Apple St.') cdh('10AM','W','1PM') = F…csg('11AM','12345','10AM') cr('11AM','10AM') cdh('11AM','W','1PM') = Fcsg('11AM','12345','10AM') cr('11AM','11AM') cdh('11AM','W','1PM') = Fcsg('11AM','12345','10AM') cr('11AM','12 Apple St.') cdh('11AM','W','1PM') = F…csg('11AM','12345','11AM') cr('11AM','10AM') cdh('11AM','W','1PM') = Fcsg('11AM','12345','11AM') cr('11AM','11AM') cdh('11AM','W','1PM') = Fcsg('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 xAxA xA

Proof: xP(x) (P(x1) P(x2) …) P(x1) P(x2) … xP(x)

RenamingAn 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 careful1. 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))

z z Doesn’t work w w Works

x(yP(y) Q(x,z)) xP(x) y y Works

y y Doesn’t work

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)

(xP(x, y) zQ(y, z)) wR(w, v)

freefree freesame

Derivations

Universal Instantiation (UI)

When A is true for every instantiation, it is certainly true for some particular instantiation

xA

SxtA

Example: from x Mortal(x), we can derive Mortal(Smith)

When A is true for one or more instantiations, we can let a variable, say b, designate any one of the true instantiations.

xA

SxbA

Existential Instantiation (EI)

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)

If we know A is true for some particular substitution t, we know there exists at least one substitution for which A is true.

SxtA

xA

If A holds with no restrictions on one of its variables x, it must hold for all substitutions for x.

A

xA

Universal Generalization (UG)

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. xyz(Q(x, y) P(z)) premise2. Q(a, b) P(c) 1, UI Sx

a, Sy

b, Szc

3. P(c) Q(a, b) 2, contrapositive

4. wP(w)premise

5. P(c)4, UI Sw

c

6. Q(a, b) 3&5, modus ponens

7. xyQ(x, y) 6, UG (Neither a nor b is free in a premise, nor were they introduced by EI)

8. xQ(x, 8)7, UI Sy

8

Note: in Step 2, we could have used Sxx, Sy

y, Szz: the names of the

variables don’t matter, so long as we follow the rules properly

UnificationThe 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. Q(x, y) P(z) premise2. P(z) Q(x, y) 1, contrapositive3. P(w) premise4. P(z) 3,

unification with 25. Q(x, y) 2&4, modus

ponens6. Q(x, 8) 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…

Prove that this rule of inference is valid.

P AP BA B

It is Valid

F

T

T

T

F

T

T

T

T

T

T

T

T

T

T

T

T

T

T

T

F

T

F

T

T

T

T

T

F

F

F

F

F

F

T

T

F

T

F

T

F

F

T

T

T

T

T

T

FFF

TFF

FTF

TTF

FFT

TFT

FTT

TTT

A B B)(P (P A)BAP

Using your new rule, rewrite

PP QQ

P FP QQ F

PQ PQ

P FQ PQ F

P QQ RP R

P QQ RP R

Modus ponens Modus tollens Hypothetical syllogism

We now have one rule to rule them all!

Resolution Rule

This says: In two disjunctive clauses, if we have complementary literals, we can discard them and “OR” the remaining clauses.

P AP BA B

literalsclauses: A and B will always be disjunctions of literals or just a literal or possibly missing.

Using Resolution

Uniform: Only disjunctions of literals in every ruleFewer Rules: Only one inference ruleHeuristic Guide: Reduce the number of literals with the goal

of reaching FalseAlgorithmic:

1. Negate the conclusion and add it as a premise.2. Convert the premises to CNF (conjunction of disjunction of

literals).3. Write each premise (which is a disjunction of literals) as a line

of the proof.4. Repeatedly apply resolution (the one inference rule) & simplify

as needed.5. Success iff F is reached.

Converting Expressionsto 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 #1If P Q, Q R, P then R.

1. Negate the conclusion (R becomes another premise).2. Convert to CNF: (P Q) (Q R) P R3. Write the premises as the first lines of the proof.4. Do resolution.

1. P Q premise2. Q R premise3. P premise4. R premise5. P R resolution 1,26. R resolution 3,57. resolution 4,6

} Sometimes calledthe support.

empty = F

Example #2If P (Q R), R Q then P.1. Negate conclusion: P P2. Convert to CNF: (P Q) (P R) R Q P

1. P Q premise (not used could discard)2. P R premise3. R premise4. Q premise (not used could discard)5. P premise6. R resolution 2,57. F 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. P Q premise2. Q P premise3. P Q premise4. P Q premise5. P resolution 1,2 (idemp. P P

P)6. Q resolution 3,57. Q resolution 4,58. F resolution 6,7

Example #4If (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 R1. P Q R premise2. P premise3. Q premise4. R premise5. Q R resolution 1,26. R resolution 3,57. 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. Remove and 2. Move in3. Rectify (standardize all variables apart)

4. Move quantifiers to the front

Prenex Normal Form – Example

y(xP(x) xQ(x, y)) y(xP(x) xQ(x, y)) implication

y(xP(x) xQ(x, y)) xA xA (de Morgan’s)

y(xP(x) xQ(x, y)) de Morgan’s, double neg.

y(xP(x) xQ(x, y)) xA xA (de Morgan’s)

y(xP(x) zQ(z, y)) rectification

yx(P(x) zQ(z, y)) xAB x(AB) (x not free in B)

yxz(P(x) Q(z, y)) AzB z(AB) (z not free in A)

Clauses in Our Language• We assume that all clauses are of the form

Premise Conclusionwhere:

Premise is a conjunction of predicatesConclusion 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

If R(x,y) P(x,z) Q(z,y)P(a,b)Q(b,j)Q(b,k)

Then R(a,j)

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) conclusion

Unif. 6. P(a,z) Q(z,j) R(a,j) UI, Sxa Sy

j

7. P(a,z) Q(z,j)res 5,6

Unif. 8. P(a,b) Q(b,j)Sz

b

9. Q(b,j) res 2,810. (succeed!)res 9,3

Domain = {a,b,j,k}

Example

If aunt(x,y) sister(x,z) parent(z,y)sister(ann,bob)parent(bob,jay)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)

conclusion6. sister(ann,z) parent(z,jay) aunt(ann,jay) UI, Sx

ann Syjay

7. sister(ann,z) parent(z,jay) res 5,6

8. sister(ann,bob) parent(bob,jay) Szbob

9. sister(ann,bob) res 3,8

10. (succeed!) res 2,9

Domain = {ann,bob,jay,kay}

Example: Same, but w/Meaning

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: In Our Language

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'). (using resolution)

1. aunt('ann','ann')conclusion

2. aunt('ann','ann') :- sister('ann',z), parent(z,'ann’) Sxann

Now observe that the resolution of 1 & 2 issister('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.

Example: with Variables

3a. sister('ann',z) conclusion 3b. sister('ann','ann') Sz

ann

3c. (fail!) 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') Sz

bob

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') Sz

bob

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'.

1. aunt('ann',x)conclusion

2. aunt('ann','ann')Sx

ann

3. aunt('ann','ann') :- sister('ann',z), parent(z,'ann’) Sxann Sy

ann

Continuing…

2. aunt('ann','bob') conclusion w/Sxbob

3. aunt('ann','bob') :- sister('ann',z), parent(z,'bob'). Sxann Sy

bob

3a. sister('ann',z) conclusion 3b. sister('ann','ann') Sz

ann

3c. (fail!) 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') Sz

bob

3c. (succeed!) res with fact: sister('ann','bob')

3d. parent(z,'bob') conclusion 3e. parent('bob','bob') Sz

bob

3f. (fail!) 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 Sy

jay

3a. sister('ann',z) conclusion 3b. sister('ann','ann') Sz

ann3c. (fail!) no fact:

sister('ann','ann')3a. sister('ann',z) conclusion 3b. sister('ann','bob') Sz

bob3c. (succeed!) res with fact:

sister('ann','bob')3d. parent(z,'jay') conclusion 3e. parent('bob','jay') Sz

bob3f. (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) conclusion 3b. sister('ann','jay') Sz

jay

3c. (fail!) no fact: sister('ann','jay')

3a. sister('ann',z) conclusion 3b. sister('ann','kay') Sz

kay

3c. (fail!) 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 Sy

kay

3a. sister('ann',z) conclusion 3b. sister('ann','ann') Sz

ann3c. (fail!) no fact:

sister('ann','ann')3a. sister('ann',z) conclusion 3b. sister('ann','bob') Sz

bob3c. (succeed!) res with fact:

sister('ann','bob')3d. parent(z,'kay') conclusion 3e. parent('bob','kay') Sz

bob3f. (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

Fail (sister)'kay'Fail (sister)'jay'Succeeds!'bob'

Fail (sister)'ann''kay'

Fail (sister)'kay'Fail (sister)'jay'Succeeds!'bob'

Fail (sister)'ann''jay'

Fail (sister)'kay'Fail (sister)'jay'Fail (parent)'bob'Fail (sister)'ann''bob'

Fail (sister)'kay'Fail (sister)'jay'Fail (parent)'bob'Fail (sister)'ann''ann''ann'

SzSx=y

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'ann'

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)?

1. aunt('ann','ann') goal2. aunt('ann','ann') :- sister('ann',z),parent(z,'ann').

2a. sister('ann','ann') subgoal Fail! Backtrack2a. sister('ann','bob') subgoal Succeed!2b. parent('bob','ann') subgoal Fail! Backtrack2a. sister('ann','jay') subgoal Fail! Backtrack2a. sister('ann','kay') subgoal Fail! Backtrack

1. aunt('ann','bob') goal2. aunt('ann','bob') :- sister('ann',z),parent(z,'bob').

2a. sister('ann','ann') subgoal Fail! Backtrack2a. sister('ann','bob') subgoal Succeed!2b. parent('bob','bob') subgoal Fail! Backtrack2a. sister('ann','jay') subgoal Fail! Backtrack2a. sister('ann',‘kay') subgoal 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)?

1. aunt('ann','jay') goal2. aunt('ann','jay') :- sister('ann',z),parent(z,'jay').

2a. sister('ann','ann') subgoal Fail! Backtrack2a. sister('ann','bob') subgoal Succeed!2b. parent('bob','jay') subgoal Succeed!Output “x='jay'”2a. sister('ann','jay') subgoal Fail! Backtrack2a. sister('ann','kay') subgoal Fail! Backtrack

1. aunt('ann','kay') goal2. aunt('ann','kay') :- sister('ann',z),parent(z,'kay').

2a. sister('ann','ann') subgoal Fail! Backtrack2a. sister('ann','bob') subgoal Succeed!2b. parent('bob','kay') subgoal Succeed!Output “x='kay'”2a. sister('ann','jay') subgoal Fail! Backtrack2a. sister('ann',‘kay') subgoal 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)?

Tree ViewFacts: sister('ann','bob'). parent('bob','jay'). parent('bob','kay').Rules: aunt(x,y) :- sister(x,z), parent(z,y).Queries: aunt('ann',x)?

aunt('ann',x)

sister('ann',z),parent(z,'ann'). sister('ann',z),parent(z,'jay').

sister('ann','ann') sister('ann','ann')

sister('ann','bob'),parent('bob‘,'jay').

x = 'ann'

z = 'ann'

x = 'bob' x = 'jay' x = 'kay'

z = 'bob'z = 'ann'

fail fail

succeed succeed

Note that we only need to keep track of one path from root to leaf at a time.

Potential Infinite Recursion1. 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)

f(1,3)fail

rule 4

f(1,z),b(z,3)

rule 5

f(1,1),b(1,3)succeed

z=1

f(1,3),b(3,3)fail

z=3

f(1,2),b(2,3)succeed

z=2

f(2,3)succeed

rule 4

Infinite Recursion!

fail

Infinite recursion!Keep current path stack if recursive call already in path, fail!

Domain = {1,2,3}