Prolog syntax + Unification t.k.prasad@wright.edu http://www.knoesis.org/tkprasad/ cs7120 (Prasad) L15-PrologUnify
Prolog Syntax Terms (Data objects) Atoms Structures (symbolic) constants E.g., anna, x_25, ‘Tom_’, ::=, <--->, etc Numbers E.g., -25, 100.25e+5, etc Variables E.g., X, Result, _23, _, AnonymousVariable, etc Structures E.g., f(X,23,g(‘Tom’,2)), etc Formulae (Predicate-logic clauses) Facts Rules cs7120 (Prasad) L15-PrologUnify
Using variables Lexical scope of a variable is one clause (fact/rule). eq(X,X). ?-eq(a,b). No ?-eq(b,b). Yes ?-eq(A,B). A = B. p(X,Y). ?-p(a,b). q(_,_). ?-q(a,b). Lexical scope of a variable is one clause (fact/rule). Multiple named variable occurrences can be used to capture “equality” constraints. Bindings to distinct variables are independent (but can be same too). Multiple anonymous variable occurrences are considered distinct. Unique Name Hypothesis – one object cannot have two syntactically different names (soundness of unification) cs7120 (Prasad) L15-PrologUnify
Anonymous Variable p(X,Y). is equivalent to p(_,_). ?- p(a,_). Informally, it is equivalent to: For all x, for all y: p(x, y) holds. ?- p(a,_). There exists x, such that p(a, x) holds. cs7120 (Prasad) L15-PrologUnify
(cont’d) eq(X,X). ?- eq(1,Ans). Ans = 1 q(_,_). ?- q(1,Ans). answer extraction q(_,_). ?- q(1,Ans). Ans unbound interpreted as: for all x, q(1,x) holds cs7120 (Prasad) L15-PrologUnify
Structures (~ records) add(5, mul(a,b)) arguments functor [name/arity] Structures encode labeled trees (or DAGs if they contain variables). cs7120 (Prasad) L15-PrologUnify
Matching Basic operation (cf. assignment) t1 matches t2 if t1 is (syntactically) identical to t2, or if there exists bindings to variables such that the two terms become identical after substitution. declarative definition cf. Unification (LP) vs Term Matching (FP) (In the latter case, one of the terms is ground and the other cannot have repeated variables.) cs7120 (Prasad) L15-PrologUnify
Inductive Definition of Matching Terms If S and T are constants, then S and T match only if they are the same constants. If S is a variable and T is anything, then they match, and S is instantiated to T. (Similarly, if T is a variable.) … (cont’d)… constructive definition Robinson: In the context of ‘Resolution Theorem Proving’ introduced: unifier, most general unifier and proved the uniqueness of mgu (Any unifier is an instance of mgu.) cs7120 (Prasad) L15-PrologUnify
(cont’d) If S and T are structures, then S and T match only if: S and T have the same principal functor, and all their corresponding components (arguments) match, recursively. The resulting instantiation is determined by matching of the components (arguments). f(x,y) –> functor – name f , arity 2 cs7120 (Prasad) L15-PrologUnify
Matching Substituition Examples Term Matching Substituition a b No match X { X <- a} f(X) g(Y) f(X,g(a)) f(b,g(Y)) {X <- b, Y <- a} f(a,g(X)) {X <- a} f(X,g(X)) f(a,g(a)) f(Y) {X <- Y} or {Y <- X} Identical upto variable renaming f(g(X,a), g(b,X)) f(Z,Z) Conflicting substitution Unique name hypothesis implicitly assumed If two terms to be matched were independent, due to locality of variables, same variable appearing in two terms will be treated as different. Matching causes computation -> especially the trickling sort in the last one cs7120 (Prasad) L15-PrologUnify
Extended Example f(X, g(a)) f(Y, g(Y)) {X<-Y} {X<-a, Y<-a} Common instance: f(a,g(a)) cs7120 (Prasad) L15-PrologUnify
(cont’d) f(X, h(b), g(X)) f(a, h(Y), g(Y)) {X<-a} {Y<-b} Not unifiable Common instance: ?? cs7120 (Prasad) L15-PrologUnify
Occur’s Check Problem f(X, g(X)) f(Y, Y) These terms cannot be unified because there is no finite term substituition {X <- t} such that X = g(X). In practice, this can cause non-terminating computation, requiring special handling. unifying substituition: g(g(g(g(…t)] cs7120 (Prasad) L15-PrologUnify
(Cont’d) ?-member(a,[f(a),a]). Yes ?-member(X,[f(X),X]). Infinite loop: X = f(f(f(…))) SWI-Prolog 1 ?- member(X,[f(X)]). X = f(**) ; false. cs7120 (Prasad) L15-PrologUnify
Executable Specification in Prolog type(i,int). type(x,real). type(+(E,F),T) :- type(E,T), type(F,T). type(+(E,F),real) :- type(E,T1),type(F,T2), T1 \= T2. Type Checking ?- type(+(i,x),real). Type Inference ?- type(+(x,x),T). expression over +, i, and x cs7120 (Prasad) L15-PrologUnify
Alternative : with conditional type(i,int). type(x,real). type(+(E,F),T) :- type(E,TT), ( (TT == real) -> T = real ; type(F,T) ). Type Checking ?- type(+(i,x),real). Type Inference ?- type(+(x,x),T). = unify == syntactic identity if-then-else expression cs7120 (Prasad) L15-PrologUnify