CS 541 Lecture -*- Outline -*- * Basics of Logic Programming terms and statements come from logic only data structure is logical term ** Statements in logic programs 3 kinds, facts, rules and queries *** Facts = statements of relationships. -------------------- object(event1, paper). recipient(event1, sue). actor(event1, ron). action(event1, gave). -------------------- names of individuals are called atoms: event1, paper, sue, ron. relationship names called functors syntactic convention: atoms and functors start with lower case letter *** queries = question about relationship ------------------- ?- object(event1,paper). %(was the object of event1 a paper?) yes ?- object(event1,quarterback). no ------------------- decide queries based on logical consequences simple rule "identity" suffices here: if P is in the DB, then P is satisfied, P ____ P Deducing that if query is not deducable from the database it is false is called the "closed world assumption". Prolog uses this. Usually want to ask queries about unknown entities... **** logical variables, start with a capital letter or _ examples X, Who, What, _0, ... stand for an unspecified, single entity not for a location in storage ------------------- ?- object(event1,What). %(what was given in event1?) What = paper ------------------- **** terms a term is either a constant (1, 'a string') variable (X, Xs) a functor applied to a comma separated list of terms (f(X,1)) a term is a ground term if it has no variables foo(a,b) is ground bar(X) is nonground **** substitution map from variables to terms, such that variables in domain do not occur in range. *what prolog prints out (part of the substitution). ---------------- example: s: {X, Y} -> {algol, father(me,dad)} where s[[X]] = algol s[[Y]] = father(me,dad). empty: {} -> {} counterexample: bad: {X} -> {X} ---------------- will sometimes write such a function as a set of mappings s = { X |-> algol, Y |-> father(me,dad) } application of a substitution to a term: s[[t(X,Y)]] = t(algol, father(me,dad) **** instance of a term def: A is an instance of B if there is a substitution s such that s[[B]] = A. ------------------ father(me,dad) is an instance of father(X,dad). ------------------ **** existential queries query with logical variables asks whether some individual exists, not only that, but we want a constructive demonstration, hence we retrieve an instance... ---------------- ?- object(event1,X). % asks does there exist an X such that object(event1,X) holds? ---------------- the answer Prolog prints X = paper shows the substitution {X |-> paper} ---------------------------------------------- To answer existential queries, use the deduction rule called "generalization": s[[P]] __________________ s a substitution, Xi in dom(s) exists X1...Xn . P ---------------------------------------------- An existential query can have several solutions plus(X,Y,7). **** conjunctive queries query that asks are there X's such that this and that and ... hold? -------------- ?- 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 -------------- must be same event in both parts of the answer i.e., scope of existential quantifier is outside conjunction -------------------- 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 . P1 and P2 ------------------------------------ *** rules define new relationships in terms of old statements of the form A :- B1,...,Bn. A is head of rule Bi's are the body a fact is a special case of a rule, where n=0. examples: ---------------- son(X,Y) :- father(Y,X), male(X). daughter(X,Y) :- father(Y,X), female(X). ---------------- **** logical meaning is leftward implication variables in rules are universally quantified, scope is just the rule A :- B is read as "A if B", or "infer A from B", logically this is A <== B (i.e., B implies A). examples: ---------------------------------- forall X, Y . son(X,Y) <== father(Y,X) and male(X) forall X, Y . daughter(X,Y) <== father(Y,X) and female(X) ---------------------------------- variables that don't appear in the head can be *thought of* as universally quantified -------------- 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))). -------------- so can read this as for all X, Z, grandfather(X,Z) if there exists Y... -------------------------------------------------- deduction rule is "universal modus ponens": (forall X . (A <== B1,...,Bn)), s[[B1]], ..., s[[Bn]] _____________________________________________________ s a substitution s[[A]] -------------------------------------------------- **** universal facts a special case of rules ---------- likes(X,vanilla). plus(0,X,X). ---------- deduction rule specialization is called "instantiation": forall X . A _____________ s a substitution s[[A]] ------------ ?- likes(arch,vanilla). yes ?- plus(0,3,3). yes ------------ answering an existential query with a universally quantified fact requires the existence of a common instance. definition: C is a common instance of A and B if it is an instance of both A and B. there is a substitution s, such that s[[B]] = C = s[[A]] ---------------- plus(0,3,3) is common instance of plus(0,3,Y) and plus(0,X,X) ---------------- to answer an existential query with a universally quantified fact instantiate the fact, and then generalize it. ---------------------- ?- plus(0,Z,2). plus(0,X,X) [axiom] ___________ [instantiation] plus(0,2,2) ___________ [generalization] plus(0,Z,2) ---------------------- ** meaning of a logic program a logic program is a set of rules (some of which may be facts) -------------------- 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(X). --------------------