CS 342 Lecture -*- Outline -*- * Logical variables and unification compare with Kamin's 8.2.2 ** logical variables used to abstract from specific (facts or queries): (infer? (object event1 X) (print X)) paper Satisfied stands for an unspecified, single entity not for a location in storage ground term: has no variables (object event1 paper) ** substitutions and instances *** substitution p. 360 def: map from variables to terms, such variables in domain do not occur in range. notation: set of mappings s = {X |-> algol, Y |-> (father me dad)} empty = {} counterexample: bad: {X |-> (cons X nil)} application of substitution to a term \hat{s}(g) (see def 8.2) \hat{s}[[(cons X (cons Y nil))]] = (cons algol (cons (father me dad) nil)) *** instance of a term def: a term A is an instance of a term B iff there is a substitution s such that s[[B]] = A e.g., (term instance) is an instance of (term X) by s = {X |-> instance} *** existential queries a query with a logical variable asks whether some individual exists (infer? (object event1 X) (print X)) paper Satisfied so substitution was s = {X |-> paper} ** comparing terms note: since scope of variables is limited to a clause, always comparing two terms with different variables (may look the same to you, but internally different). (plus 0 X X). (infer? (plus X 3 3) (print X)) 0 Satisfied this is the X of the question the one in the fact is bound to 3 *** common instance def: C is a common instance of A and B if C is an instance of both A and B e.g., (plus 0 3 3) is common instance of (plus Y 3 3) and (plus 0 X X) *** generality def: a term T is more general than a term V if V is an instance of T, but T is not an instance of V. (they are alphabetic variants if both instances of each other) *** unifier: a subsitution that makes two terms identical. determines a common instance (by applying the substitution). ---------------- (infer (star algol)) (infer? (star X) (print X)) algol Satisfied (infer (g X 3)) (infer? (g 4 Y) (print Y)) 3 Satisfied (infer (h X X)) (infer? (h Y Z) (print Y Z)) Z Z Satisfied ---------------- Note: in first one (star X) unifies with (star algol) by substituion s(X)=algol in second one (X = 4) is not shown **** composition of unifiers def: s1 <;> s2 is substitution s such that for all terms g \hat{s1 <;> s2}(g) = \hat{s2}(\hat{s1}(g)) note: composition in diagram order **** most general unifier (mgu): def: unifier that generates most general common instance efficiency: computation of mgu linear in size of the smallest term (in real Prolog, in our interpreters worse than this.) **** exercises For each of the following, compute the unifying substitution, if any ------- (member 3 (cons 3 nil)) (member X (cons X nil)) (member Y (cons 3 nil)) (member X (cons X nil)) (member 3 (cons 4 nil)) (member X (cons X L)) (length (cons 3 nil) N) (member X (cons X L)) (member X (cons X L)) (member Y (cons (mkTree Y nil nil) M)) ------- ** unification (def 8.4) def: process of finding most general unifier *** full procedural rule (p. 361) keeps a list of goals, called a resolvent ---------------- input: a Prolog program P, and a goal G output: a substitution s, if P => s[[G]], or failure algorithm: (not not terminate) initialize the resolvent to be (G) the subst to be {} i := 0 while the resolvent is not empty do let A be head of resolvent let (infer A' from B1 ... Bn) be the first clause in P s.t. A and A' unify with mgu s[i] (backtrack if no such exist); remove A from the resolvent; add B1 ... Bn to head of resolvent; apply s[i] to each term in resolvent If the resolvent is empty, output s[0] <> ... <> s[m] else output failure ---------------- to ensure that variables are local to a clause, rename variables each time the clause is chosen to effect a reduction. the new names are fresh