III. Basics of Logic Programming A. Statements in logic programs 1. Facts = statements of relationships. -------------------- object(event1, paper). recipient(event1, sue). actor(event1, ron). action(event1, gave). -------------------- 2. queries = question about relationship ------------------- ?- object(event1,paper). %(was the object of event1 a paper?) yes ?- object(event1,quarterback). no ------------------- a. logical variables, ------------------- ?- object(event1,What). %(what was given in event1?) What = paper ------------------- b. terms c. substitution ---------------- example: s: {X, Y} -> {algol, father(me,dad)} where s[[X]] = algol s[[Y]] = father(me,dad). empty: {} -> {} counterexample: bad: {X} -> {X} ---------------- d. instance of a term ------------------ father(me,dad) is an instance of father(X,dad). ------------------ e. existential queries ---------------- ?- object(event1,X). % asks does there exist an X such that object(event1,X) holds? ---------------- ---------------------------------------------- To answer existential queries, use the deduction rule called "generalization": s[[P]] __________________ s a substitution, Xi in dom(s) exists X1...Xn . P ---------------------------------------------- f. conjunctive queries -------------- ?- actor(Event,Who), action(Event, gave). %(who gave in what event?) Event = event1 Who = ron ?- actor(X,Y), action(X,Z). X = event1 Y = ron Z = gave -------------- -------------------- exists Event, Who . (actor(Event,Who) and action(Event,gave))? -------------------- ------------------------------------ deduction rule "conjunction" s[[P1]], s[[P2]] ________________________ s a substitution, Xi in dom(s) exists X1...Xn . P and P2 ------------------------------------ 3. rules define new relationships in terms of old ---------------- son(X,Y) :- father(Y,X), male(X). daughter(X,Y) :- father(Y,X), female(X). ---------------- a. logical meaning is leftward implication ---------------------------------- forall X, Y . son(X,Y) <== father(Y,X) and male(X) forall X, Y . daughter(X,Y) <== father(Y,X) and female(X) ---------------------------------- -------------- grandfather(X,Z) :- father(X,Y), father(Y,Z). formal meaning is: forall X,Y,Z . grandfather(X,Z) <== father(X,Y) and father(Y,Z). = by model theory forall X,Z . forall Y. grandfather(X,Z) <== father(X,Y) and father(Y,Z). = by definition of <== forall X,Z . forall Y. grandfather(X,Z) or not(father(X,Y) and father(Y,Z)). = by model theory forall X,Z . grandfather(X,Z) or (forall Y. not(father(X,Y) and father(Y,Z))). = by deMorgan's laws forall X,Z . grandfather(X,Z) or not(exists Y. (father(X,Y) and father(Y,Z))). = by defintion of <== forall X,Z . grandfather(X,Z) <== (exists Y. (father(X,Y) and father(Y,Z))). -------------- -------------------------------------------------- deduction rule is "universal modus ponens": (forall X . (A <== B1,...,Bn)), s[[B1]], ..., s[[Bn]] _____________________________________________________ s a substitution s[[A]] -------------------------------------------------- b. universal facts ---------- likes(X,vanilla). plus(0,X,X). ---------- ------------ ?- likes(arch,vanilla). yes ?- plus(0,3,3). yes ------------ ---------------- plus(0,3,3) is common instance of plus(0,3,Y) and plus(0,X,X) ---------------- ---------------------- ?- plus(0,Z,2). plus(0,X,X) [axiom] ___________ [instantiation] plus(0,2,2) ___________ [generalization] plus(0,Z,2) ---------------------- B. meaning of a logic program -------------------- meaning of a logic program P, M[[P]], is set of ground terms deducible from P. def: a program P is correct with respect to its intended meaning, I, if M[[P]] is a subset of I def: a program P is complete with respect to its intended meaning, I, if I is a subset of M[[P]]. e.g., meaning of the son relation given above is correct but not complete. To complete it, need to add son(X,Y) :- mother(Y,X), male(Y). -------------------- IV. Unification You've seen this before... V. Resolution (logical basis for Prolog) A. An abstract interpreter ---------------- input: a logic program P, and a goal G output: a substitution s, if s[[G]] is deducible from P, or failure if such a deduction is not possible algorithm: (which may fail to terminate) initialize the resolvent to be G while the resolvent is not empty do choose a goal A from the resolvent and a (renamed clause) A' :- B1,...,Bn from P such that A and A' unify with mgu s (exit if no such goal and clause exist) remove A from the resolvent and add B1,...,Bn apply s to the resolvent and to G If the resolvent is empty, output G, else output failure ---------------- B. Why is the abstract interpreter correct? 1. simpler rules, resolution ------------- %facts scientist(sue). %1 scientist(ron). %2 spanish(sue). %3 american(ron). %4 %rule logician(X) :- scientist(X). %5 %goal ?- logician(Y), american(Y). % (is there) some Y is both a logician and an american? --------------- a. to eliminate quantifiers, use fresh variables (skolemize) ------------------------ rule 5 becomes logician(X) <== scientist(X) universal modus ponens becomes: (A <== B1,...,Bn), s[[B1]], ..., s[[Bn]] _________________________________________ s a substitution s[[A]] ------------------------- b. to remove implications, use formula: (A <== B) = (A or not B) ---------------------- rule 5 becomes logician(X) or not(scientist(X)) universal modus ponens becomes (A or not(B1) or ... or not(Bn)), s[[B1]], ..., s[[Bn]] ______________________________________________________ s a substitution s[[A]] ---------------------- c. to ask a query Q, see if not(Q) produces a contradiction ------------------------------------------------ query was exists Y . logician(Y) and american(Y) negation is forall Y . not (logician(Y) and american(Y)) = by deMorgan's laws forall Y . not(logician(Y)) or not(american(Y)) now skolemize this not(logician(Y)) or not(american(Y)) ------------------------------------------------ -------------------------------------- resolution: not(s[[A1]]) or ... or not(s[[Ai]]) or ... or not(s[[Am]]), (Ai or not(B1) or ... or not(Bn)) _________________________________________ s a substitution not(s[[A1]]) or ... or not(B1) or ... or not(Bn) or ...or not(s[[Am]]) simpler version: not(R) or not(P), (P or not(Q)) _______________________________ not(R) or not(Q) some consequences: not(R) or not(P), P ___________________ not(R) not(R), R _________ *poof* --------------------------------------- ------------------------ suppose clause A contains some term s, clause B contains negation of term t, s and t are unifiable by some subst sigma, then resolvent of A and B is generated by 1. combining the clauses to form A or B 2. removing s and t (from the resulting term) 3. applying substitution sigma to result ------------------------ C. Why Horn clauses? VI. Procedural model of Prolog A. practical questions in using resolution (and Prolog's answers) -------------------------------------- what substitution to use? (most general) no limitation here what clause to resolve? (goal or descendent of goal) backwards chaining what disjunct in goal to resolve? (leftmost = linear) what clause to resolve against? (from original set, top-down) search strategy breadth-first - sure to find solution (complete) depth first - less storage for backtracking (Prolog) -------------------------------------- 1. Top-down vs. bottom up control a. top-down control (backward chaining) as in Prolog b. bottom up control (forward chaining) as in OPS5 2. Depth-first search vs breadth-first search. ---------------------- p :- p, q. p. q. ---------------------- B. SL resolution C. Another way to see examples, time vs. goals stack graphs D. practical implications 1. order of rules matters 2. order of clauses in body matters 3. termination domain