Download presentation
Presentation is loading. Please wait.
1
Discussion #20 1/11 Discussion #20 Resolution in Datalog
2
Discussion #20 2/11 Topics Datalog –Prolog –Notation –Goals success and failure –Examples –Recursive Rules Termination Infinite recursion We are not trying to do all of Prolog (or even all of Datalog).
3
Discussion #20 3/11 236 Datalog 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.) The exact distinction is fuzzy (and doesn't matter). For the project, we'll do exactly the language we've defined, 236 Datalog. –Syntactically taken from a real Prolog –Is an actual subset of Datalog
4
Discussion #20 4/11 236 Datalog Prolog (Differences…) Constants –Prolog: numbers or alphanumeric beginning with lower case letter –236 Datalog: only strings Variables –Prolog: begin with a capital letter (anonymous variables, i.e. _ ) –236 Datalog: begin with a letter (no anonymous variables) Predicates –Prolog: begin with lower case letter –236 Datalog: begin with a letter Comments –Prolog: % … –236 Datalog: #-comments and #|…|#-comments Queries –Prolog: | ?- is the interactive prompt, so | ?- p(Y). is the query –236 Datalog: not interactive, no prompt, just P(Y)?
5
Discussion #20 5/11 236 Datalog Prolog (Similarities…) Same syntax for Facts and Rules –Comma means –No semicolon for ( ) use 2 rules. parent(x,y) :- mother(x,y); father(x,y). changes to: parent(x,y) :- mother(x,y). parent(x,y) :- father(x,y). Recursive rules the same Derivations the same –Instantiation –Unification –Resolution (derivations of empty clause) –Backtracking Results same: “can be derived from database”
6
Discussion #20 6/11 Prolog Derivations Although 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 We can use this same view in 236 Datalog. We’ll use the following database to explain these ideas. 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)?
7
Discussion #20 7/11 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…)
8
Discussion #20 8/11 236 Datalog aunt('ann',x)? 1. aunt('ann','ann') goal 2. aunt('ann','ann') :- sister('ann',z),parent(z,'ann'). 2a. sister('ann','ann') subgoalFail! Backtrack 2a. sister('ann','bob') subgoalSucceed! 2b. parent('bob','ann') subgoalFail! Backtrack 2a. sister('ann','jay') subgoalFail! Backtrack 2a. sister('ann','kay') subgoalFail! Backtrack 1. aunt('ann','bob') goal 2. aunt('ann','bob') :- sister('ann',z),parent(z,'bob'). 2a. sister('ann','ann') subgoalFail! Backtrack 2a. sister('ann','bob') subgoalSucceed! 2b. parent('bob','bob') subgoalFail! Backtrack 2a. sister('ann','jay') subgoalFail! Backtrack 2a. sister('ann',‘kay') subgoalFail! 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)?
9
Discussion #20 9/11 1. aunt('ann','jay') goal 2. aunt('ann','jay') :- sister('ann',z),parent(z,'jay'). 2a. sister('ann','ann') subgoalFail! Backtrack 2a. sister('ann','bob') subgoalSucceed! 2b. parent('bob','jay') subgoalSucceed! Output “x='jay'” 2a. sister('ann','jay') subgoalFail! Backtrack 2a. sister('ann','kay') subgoalFail! Backtrack 1. aunt('ann','kay') goal 2. aunt('ann','kay') :- sister('ann',z),parent(z,'kay'). 2a. sister('ann','ann') subgoalFail! Backtrack 2a. sister('ann','bob') subgoalSucceed! 2b. parent('bob','kay') subgoalSucceed! Output “x='kay'” 2a. sister('ann','jay') subgoalFail! Backtrack 2a. sister('ann',‘kay') subgoalFail! 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)?
10
Discussion #20 10/11 Tree View Facts: 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','bob'),parent('bob‘,'jay'). x = 'ann' z = 'ann' x = 'bob'x = 'jay'x = 'kay' z = 'bob' z = 'ann' fail succeed … … … … Note that we only need to keep track of one path from root to leaf at a time.
11
Discussion #20 11/11 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) 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}
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.