1 / 16

Prolog syntax + Unification

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

carney
Download Presentation

Prolog syntax + Unification

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Prolog syntax + Unification t.k.prasad@wright.edu http://www.knoesis.org/tkprasad/ L2PrologUnify

  2. 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

  3. 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

  4. 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

  5. (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

  6. Structures (~ records) arguments functor [name/arity] Structures encode labelled trees (or DAGs if they contain variables). add(5, mul(a,b)) L2PrologUnify

  7. 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

  8. 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

  9. (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

  10. Examples L2PrologUnify

  11. Extended Example f(X, g(a)) f(Y, g(Y)) {X<-Y} {X<-a, Y<-a} Common instance: f(a,g(a)) L2PrologUnify

  12. (cont’d) f(X, h(b), g(X)) f(a, h(Y), g(Y)) {X<-a}{Y<-b} Not unifiable Common instance: ?? L2PrologUnify

  13. 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

  14. (Cont’d) • ?-member(a,[f(a),a]). • Yes • ?-member(X,[f(X),X]). • Yes Infinite loop: X = f(f(f(…))) L2PrologUnify

  15. 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

  16. 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

More Related