CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search Unification • From Knowledge Base 4: woman(mia). woman(jody). woman(yolanda). … • Querying Knowledge Base 4: 1 ?- woman(X). X = mia . 2 ?- • Prolog unifies woman(X) with woman(mia) , thereby instantiating the variable X to mia Types in Prolog – A Quick Review • Recall that there are three types of term: – Constants. These can either be atoms (such as vincent ) or numbers (such as 24 ). – Variables. (Such as X , Z3 , and List .) – Complex terms. These have the form: – functor(term_1,...,term_n) . Unification – A Definition • Two terms unify: – they are the same term or – if they contain variables that can be uniformly instantiated with terms in such a way that the resulting terms are equal. Unification – An Example • This means that – mia and mia unify, because they are the same atom. – 42 and 42 unify, because they are the same number. – X and X unify, because they are the same variable. – woman(mia) and woman(mia) unify, because they are the same complex term. Unification – An Example (continued) • This means that – woman(mia) and woman(vincent) do not unify, because they are not the same (and neither of them contains a variable that could be instantiated to make them the same). Unification and Instantiation • mia and X are not the same, but X can be instantiated to mia which makes them equal. Therefore, mia and X unify. • woman(X) and woman(mia) unify, because they can be made equal by instantiating X to mia . • loves(vincent,X) and loves(X,mia) do not unify because it is impossible to find an instantiation of X that makes the two terms equal. A Formal Definition of Unification • If term1 and term2 are constants, then term1 and term2 unify if and only if they are the same atom, or the same number. A Formal Definition of Unification (continued) • If term1 is a variable and term2 is any type of term, then term1 and term2 unify, and term1 is instantiated to term2 . • If term2 is a variable and term1 is any type of term, then term1 and term2 unify, and term2 is instantiated to term1 . • If they are both variables, they’re both instantiated to each other, and we say that they share values. A Formal Definition of Unification (continued) • If term1 and term2 are complex terms, then they unify if and only if: – They have the same functor and arity, and – all their corresponding arguments unify, and – the variable instantiations are compatible. A Formal Definition of Unification (continued) • E. g., it’s impossible to instantiate variable X to mia when unifying one pair of arguments, and to instantiate X to vincent when unifying another pair of arguments .) • Two terms unify if and only if it follows from the previous three clauses that they unify. Unification – Some Examples 1 ?- =(mia, mia). true. 2 ?- =(2, 2). true. 3 ?- mia = mia. true. 4 ?- 2 = 2. true. 5 ?- mia = vincent. false. 6 ?- 'mia' = mia. true. 7 ?- '2' = 2. false. 8 ?- 8 ?- mia = X. X = mia. 9 ?- mia = X. X = mia. 10 ?- X = Y. X = Y. 11 ?- X = _5067. X = _5067. 12 ?- Y = _5067. Y = _5067. 13 ?- X = mia, X = vincent. false. Unification – Another Example 2 ?- k(s(g), Y) = k(X, t(k)) . Y = t(k), X = s(g). 3 ?- • We use clause 3 because we are trying to unify two complex terms. Do both complex terms have the same functor and arity? Yes. • So do the first arguments, s(g) and X , unify? By clause 2, yes. • So do the second arguments, Y and t(k) , unify? By clause 2, yes. Unification – Other Examples 3 ?- k(s(g), t(k)) = k(X, t(Y)). X = s(g), Y = k. 4 ?- loves(X, X) = loves(marcellus, mia). false. 5 ?- • These terms do not unify because while they are both complex terms and have the same functor and arity, all corresponding arguments do not unify in this example Prolog and Unification Algorithm • Prolog does not use a standard unification algorithm when it performs its version of unification; it takes a shortcut. You need to know about this shortcut. • Consider the following query: ?- father(X) = X. Prolog and Unification Algorithm (continued) • Do these terms unify or not? No • Why is that? Pick any term and instantiate X to the term you picked. father(father(father(butch))) = father(father(butch)) and so on. Prolog and Unification Algorithm (continued) • Prolog unification won’t spot the problem and halt. Having instantiated X to father(X) , Prolog is committed to carrying out an unending sequence of expansions and will run until it runs out of memory. Prolog and Unification Algorithm (continued) • There are actually 3 responses to “does father(X) unify with X ”. – Standard unification algorithm (no) – Older Prolog implementations (run amok until they use up the available memory) – Sophisticated Prolog implementations (yes) • In short, there is no ‘right’ answer to this question. occurs check • A standard algorithm given two terms to unify carries out what is known as the occurs check. • This means that if it is asked to unify a variable with a term, it first checks whether the variable occurs in the term. • If it does, the standard algorithm declares that unification is impossible, for clearly it is the presence of the variable X in father(X) which leads to problems. • Only if the variable does not occur in the term do standard algorithms attempt to carry out the unification. Prolog and Unification Algorithm (continued) • Prolog comes with a built-in predicate that carries out standard unification (that is, unification with the occurs check). The predicate is unify_with_occurs_check/2. • So if we posed the query ?- unify_with_occurs_check (father(X),X). we would get the response no. Unification Proof Search f(a). f(b). g(a). g(b). h(b). k(X):- f(X),g(X),h(X). • try to match f (_1), g(_1), h(_1) • When f(a), then _1 = a. • Does it find f(a), g(a),h(a) – nope • When f(b) then _1 = b. • Does it find f(b), g(b), h(b) – yes so it stops and says true. • See with trace • Stop trace with notrace Crossword puzzle match • • • • • • • • • • • • • word(astante,a,s,t,a,n,t,e). word(astoria,a,s,t,o,r,i,a). word(baratto,b,a,r,a,t,t,o). word(cobalto,c,o,b,a,l,t,o). word(pistola,p,i,s,t,o,l,a). word(statale,s,t,a,t,a,l,e). crossword(X,Y, Z, A, B, C) :word(X,_,X1,_,X2,_,X3,_), word(Y,_,Y1,_,Y2,_,Y3,_), word(Z,_,Z1,_,Z2,_,Z3,_), word(C,_,X3,_,Y3,_,Z3,_), word(B,_,X2,_,Y2,_,Z2,_), word(A,_,X1,_,Y1,_,Z1,_). Summary • Prolog proves by instantiating variables and then checking truth • Stops at first complete matching truth • Will continue finding match when you press ; • Trace using trace / notrace • Can go into infinite checking unless you call with unify_with_occurs_check