Datalog & Resolution in Datalog. Prolog Derivations Although derivations are the same, there is a...

7
Datalog & Resolution in Datalog

Transcript of Datalog & Resolution in Datalog. Prolog Derivations Although derivations are the same, there is a...

Page 1: Datalog & Resolution in Datalog. Prolog Derivations Although derivations are the same, there is a different way of looking at the derivation in terms.

Datalog &Resolution in Datalog

Page 2: Datalog & Resolution in Datalog. Prolog Derivations Although derivations are the same, there is a different way of looking at the derivation in terms.

Prolog DerivationsAlthough derivations are the same, there is a different way of looking at

the derivation in terms of– Goals query and atomic formulas in the body of the rule.– Success can be derived– Failure cannot be derived

Facts:sister('ann','bob').parent('bob','jay').parent('bob','kay').

Rules:aunt(x,y) :- sister(x,z), parent(z,y).

Queries:aunt('ann','jay')?aunt('ann',x)?

Page 3: Datalog & Resolution in Datalog. Prolog Derivations Although derivations are the same, there is a different way of looking at the derivation in terms.

Resolution

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

parent(z,'jay').

2a. sister('ann','ann')2b. Fail! Backtrack

2a. sister('ann','bob')2b. res. with fact: sister('ann','bob')

2c. parent('bob','jay')2c. res. with fact: parent('bob','jay')Yes!

Prolog

1. aunt('ann','jay') goal

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

2a. sister('ann','ann')subgoal

2b. Fail! Backtrack

2a. sister('ann','bob')subgoal

2b. Matches a Fact (directly)

2c. parent('bob','jay')subgoal

2c. Matches a Fact (directly)Success!Yes!

Prolog Derivations (continued…)

Page 4: Datalog & Resolution in Datalog. Prolog Derivations Although derivations are the same, there is a different way of looking at the derivation in terms.

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

Page 5: Datalog & Resolution in Datalog. Prolog Derivations Although derivations are the same, there is a different way of looking at the derivation in terms.

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

Page 6: Datalog & Resolution in Datalog. Prolog Derivations Although derivations are the same, there is a different way of looking at the derivation in terms.

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.

Page 7: Datalog & Resolution in Datalog. Prolog Derivations Although derivations are the same, there is a different way of looking at the derivation in terms.

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}