160 likes | 292 Views
Prolog syntax + Unification. t.k.prasad@wright.edu http://www.knoesis.org/tkprasad/. Prolog Syntax. Terms (Data objects) Atoms (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
E N D
Prolog syntax + Unification t.k.prasad@wright.edu http://www.knoesis.org/tkprasad/ L2PrologUnify
Prolog Syntax • Terms (Data objects) • Atoms • (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 L2PrologUnify
Using variables 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. eq(X,X). ?-eq(a,b). No ?-eq(b,b). Yes ?-eq(A,B). A = B. p(X,Y). ?-p(a,b). Yes q(_,_). ?-q(a,b). L2PrologUnify
Anonymous Variable • p(X,Y).is equivalent to p(_,_). • Informally, it is equivalent to: For all x, for all y: p(x, y) holds. • ?- p(a,_). • Informally, it is equivalent to: There exists x, such that p(a, x) holds. L2PrologUnify
(cont’d) • eq(X,X). • ?- eq(1,Ans). Ans = 1 • answer extraction • q(_,_). • ?- q(1,Ans). Ans unbound • interpreted as: for all x, q(1,x) holds L2PrologUnify
Structures (~ records) arguments functor [name/arity] Structures encode labelled trees (or DAGs if they contain variables). add(5, mul(a,b)) L2PrologUnify
Matching • Basic operation (cf. assignment) • t1 matches t2if • t1 is (syntactically) identical to t2, or • if there exists bindings to variables such that the two terms become identical after substituition. L2PrologUnify
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)… L2PrologUnify
(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 match, recursively. • The resulting instantiation is determined by matching of the components. L2PrologUnify
Examples L2PrologUnify
Extended Example f(X, g(a)) f(Y, g(Y)) {X<-Y} {X<-a, Y<-a} Common instance: f(a,g(a)) L2PrologUnify
(cont’d) f(X, h(b), g(X)) f(a, h(Y), g(Y)) {X<-a}{Y<-b} Not unifiable Common instance: ?? L2PrologUnify
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. L2PrologUnify
(Cont’d) • ?-member(a,[f(a),a]). • Yes • ?-member(X,[f(X),X]). • Yes Infinite loop: X = f(f(f(…))) L2PrologUnify
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). L167AG
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). L167AG