prolog syntax + unification
Post on 21-Jan-2016
53 Views
Preview:
DESCRIPTION
TRANSCRIPT
cs774 (Prasad) L2PrologUnify 1
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
• Structures» E.g., f(X,23,g(‘Tom’,2)), etc
– Formulae (Predicate-logic clauses)
• Facts
• Rules
cs774 (Prasad) L2PrologUnify 2
Using variableseq(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).
• 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.
cs774 (Prasad) L2PrologUnify 3
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.
cs774 (Prasad) L2PrologUnify 4
(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
cs774 (Prasad) L2PrologUnify 5
Structures (~ records)
cs774 (Prasad) L2PrologUnify 6
add(5, mul(a,b))
functor[name/arity]
arguments
Structures encode labelled trees (or DAGs if they contain variables).
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 substituition.
cs774 (Prasad) L2PrologUnify 7
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)…
cs774 (Prasad) L2PrologUnify 8
(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.
cs774 (Prasad) L2PrologUnify 9
ExamplesTerm Term Matching Substituition
a b No match
a X { X <- a}
f(X) g(Y) No match
f(X,g(a)) f(b,g(Y)) {X <- b, Y <- a}
f(X,g(a)) f(a,g(X)) {X <- a}
f(X,g(a)) f(b,g(Y)) {X <- b, Y <- a}
f(X) f(Y) {X <- Y} or {Y <- X}Identical upto variable renaming
f(g(X,a), g(b,X))
f(Z,Z)No match
Conflicting substituition
cs774 (Prasad) L2PrologUnify 10
Extended Example
f(X, g(a))
f(Y, g(Y))
{X<-Y} {X<-a, Y<-a}
Common instance: f(a,g(a))
cs774 (Prasad) L2PrologUnify 11
(cont’d)
f(X, h(b), g(X))
f(a, h(Y), g(Y))
{X<-a} {Y<-b} Not unifiable
Common instance: ??
cs774 (Prasad) L2PrologUnify 12
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.
cs774 (Prasad) L2PrologUnify 13
(Cont’d)
• ?-member(a,[f(a),a]).• Yes• ?-member(X,[f(X),X]).• Yes
Infinite loop:
X = f(f(f(…)))
cs774 (Prasad) L2PrologUnify 14
CS784 (Prasad) L167AG 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).
CS784 (Prasad) 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).
top related