CS 541 Lecture -*- Outline -*- * Logical Basis of Logic Programming How to view a (Lambda)Prolog program as a logical theory. Reference: adapted from Sterling and Shapiro's book: "The Art of Prolog", chapters 1, 4 and 5. ** answering queries from ground facts decide queries based on logical consequences *** terms used as syntax of facts and queries. ------------------------------------------ TERMS AND GROUND TERMS def: a term is a in the grammar. Simple cases: either a constant (1, "a string", sue) variable (X, Xs) application ((f 3) Y), (f 3 Y) lambda abstraction (X : int \ X) def: a ground term has no free variables. examples: (foo a b) (X : int \ X) counterexamples: (foo X Y) (X : int \ Y) ------------------------------------------ Q: what is (X : int \ X) like in Haskell? def: a variable is free in a term iff it occurs in a positition where it is not bound by a lambda def: a variable is bound in a term iff it occurs in a lambda binding Q: can a variable be both free and bound in a term? *** without logical variables recall that a logical variable starts with a capital letter ------------------------------------------ ANSWERING QUERIES WITHOUT VARIABLES (WITH FACTS WITHOUT VARIABLES) in module semantic_net: object event1 paper. $ tjsim semantic_net [semantic_net] ?- object event1 paper. yes logical rule: identity G ____ G ------------------------------------------ simple rule "identity" suffices here: if G is in the program (DB), then G is satisfied, Operational semantics: Prog ?- Q => solved, if Q \in Prog ------------------------------------------ CLOSED WORLD ASSUMPTION module semantic_net. object event1 paper. recipeint event1 sue. actor event1 ron. action event1 gave. [semantic_net] ?- object event1 football. no (more) solutions [semantic_net] ?- not(object event1 football). yes logical rule: closed world assumption G is not provable ______________________ not G ------------------------------------------ Deducing that if query is not deducable from the database it is false is called the "closed world assumption". Prolog uses this. This is a fallacious if the theory or proof system isn't complete: like "I tried to prove Fermat's last theorem, but I couldn't, so it must be false". Only works if trying to prove Q fails in a finite amount of time... Operational semantics: Prog ?- not G => solved, if not (Prog ?- G => solved) *** with logical variables, Usually want to ask queries about unknown entities... ------------------------------------------ ANSWERING QUERIES WITH LOGICAL VARIABLES [semantic_net] ?- object event1 What. The answer substitution: What = paper logical meaning of query: logical rule: generalization ___________________________ exists X1...Xn . P(X1...Xn) def: a logical variable has a name that starts with an upper case letter and stands for an ------------------------------------------ ... exists What . (object event1 What) ... s[[P(X1...Xn)]] where s a substitution and Xi in dom(s) ... unspecified, single entity logical variables don't stand for locations in storage examples X, Who, What, ... notation means P(X1,...,Xn) is a query with free variables X1,...,Xn have to explain what all this means show the proof of object event1 What. Operational semantics: Prog ?- P(X1,...,Xn) => solved, if exists s . s[[P(X1,...,Xn)]] \in Prog **** substitution map from variables to terms, such that variables in domain do not occur in range. (some call this an idempotent substitution). *what prolog prints out is result of applying the substitution to all the free variables in the query. (You get part of the substitution back by taking out all the pairs that look like Z = Z.) ------------------------------------------ SUBSTITUTIONS def: a substitution is example: s: {X, Y} -> {(X \ X), (father me dad)} where s[[X]] = (X \ X) s[[Y]] = (father me dad). empty: {} -> {} counterexample: bad: {X} -> {X} application of a substitution to a term: s[[(t X Y)]] = ------------------------------------------ ... a map from variables to terms such that variables in the domain do not occur free in range. will sometimes write such a function as a set of mappings s = { X |-> (X \ X), Y |-> (father me dad) } ... (t (X \ X) (father me dad)) notational warning: some authors write substitutions *after* the term being substituted, e.g., P\theta. **** instances ------------------------------------------ INSTANCES def: A is an instance of B iff Examples: FOR YOU TO DO Give 3 instances of the term: (tree Left Right (f Node)) Can (tree Right Left (f 2)) be an instance? ------------------------------------------ ... there is a substitution s such that s[[B]] = A. example: (father me dad) is an instance of (father X dad). Q: can an instance have variables in it? **** recap of 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 What. % asks does there exist an What such that (object event1 What) holds? the answer Prolog prints What = paper. shows the substitution {What |-> paper} 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? the variable means the same thing in all parts of the answer i.e., scope of existential quantifier is outside conjunction ------------------------------------------ CONJUNCTIVE QUERIES [semantic_net] ?- (actor Event Who), (action Event gave). The answer substitution: Who = ron Event = event1 logical meaning: exists Event, Who . (actor Event Who) and (action Event gave) logical rule: conjunction ______________________________ exists X1...Xn . P1(X1...Xn) and P2(X1...Xn) ------------------------------------------ ... s[[P1(X1...Xn)]], s[[P2(X1...Xn)]] where s is a substitution, and Xi in dom(s) ** answering queries using rules recall that rules are 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. rules define new relationships in terms of old ones examples: ------------------------------------------ LOGICAL MEANING OF RULES sig patriarchical_family. kind person type. kind person type. type son person -> person -> o. type dad person -> person -> o. type male person -> o. type daughter person -> person -> o. type female person -> o. module patriarchical_family. son X Y :- (dad Y X), (male X). daughter X Y :- (dad Y X), (female X). logical meaning: ------------------------------------------ ...all X, Y . (son X Y) <== (dad Y X) and (male X) all X, Y . (daughter X Y) <== (dad Y X) and (female X) i.e., logical meaning of rule 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). ------------------------------------------ VARIABLES NOT IN HEAD ACT AS IF EXISTENTIALLLY QUANTIFIED (granddad X Z) :- (dad X Y), (dad Y Z). means: all X,Y,Z . (granddad X Z) <== (dad X Y) and (dad Y Z). = all X,Z . all Y. (granddad X Z) <== (dad X Y) and (dad Y Z). = all X,Z . all Y. (granddad X Z) or not((dad X Y) and (dad Y Z)). = all X,Z . (granddad X Z) or (all Y. not((dad X Y) and (dad Y Z))). = all X,Z . (granddad X Z) or not(exists Y. ((dad X Y) and (dad Y Z))). = all X,Z . (granddad X Z) <== (exists Y. ((dad X Y) and (dad Y Z))). ------------------------------------------ so can read this as for all X, Z: (granddad X Z) if there exists Y... *** logical basis for queries using rules ------------------------------------------ ANSWERING QUERIES USING RULES module my_family. accumulate patriarchical_family. dad pop gayle. dad pop me. dad pop glen. dad pop gina. female gayle. female gina. male me. male glen. $ tjsim my_family [my_family] ?- son me pop. yes logical rule: universal modus ponens: where s is a ________________________ A' = s[[A]] A' ------------------------------------------ ... (all X . (A <== B1,...,Bn)), B1', ..., Bn', where s is a substitution, A' = s[[A]], and B1' = s[[B1]], ..., Bn' = s[[Bn]] Q: Why does that make sense? What does it mean computationally? show the proof of son me pop: all X, Y . (son X Y) <== (dad Y X) and (male X) (dad pop me), (male me) ___________________________ [univ. mp.] (son me pop) ** answering queries using universal facts a special case of rules *** universal facts ------------------------------------------ ANSWERING QUERIES USING UNIVERSAL FACTS module universal_truths. plus 0 X X. likes X vanilla. $tjsim universal_truths [universal_truths] ?- likes arch vanilla. yes [universal_truths] ?- plus 0 3 3. yes logical rule: instantiation all X . A _____________ where s is a substitution A' and A' = s[[A]] ------------------------------------------ of course arch, vanilla, plus have to be declared in the module. *** universal facts and existential queries ------------------------------------------ ANSWERING EXISTENTIAL QUERIES WITH UNIVERSAL FACTS [universal_truths] ?- plus 0 Z 2. The answer substitution: Z = 2 logical proof: all X . (plus 0 X X) [axiom] _____________________ [instantiation] (plus 0 2 2) _____________________ [generalization] exists Z . (plus 0 Z 2) by subst. s = {X |-> 2, Z |-> 2} (note only print the user's part) ------------------------------------------ i.e., to answer an existential query with a universally quantified fact instantiate the fact, and then generalize it. answering an existential query with a universally quantified fact requires the existence of a common instance. ------------------------------------------ COMMON INSTANCES def: C is a common instance of A and B e.g. (plus 0 2 2) is common instance of (plus 0 Z 2) and (plus 0 X X) ------------------------------------------ ... if it is an instance of both A and B. i.e., there is a substitution s, such that s[[B]] = C = s[[A]] ** meaning of a logic program a logic program is a set of rules (some of which may be facts) Q: what does a logic program mean? Q: what could it mean for a logic program to be correct? if it doesn't mean anything, then how can I grade your homework? (have them derive these definitions) ------------------------------------------ SEMANTICS AND CORRECTNESS def: meaning of a logic program P, M[[P]], ------------------------------------------ ... is set of ground terms deducible from P. def: a program P is sound with respect to its intended meaning, I, iff M[[P]] is a subset of I def: a program P is complete with respect to its intended meaning, I, iff I is a subset of M[[P]]. some authors call soundness correctness, but that seems confusing Q: is the meaning of the son relation sound? complete? if it's not complete what could be done to complete it? To complete it, could add (son X Y) :- (mother Y X), (male X). Q: Is (son X Y) by itself complete? Sound?