Information Technology Reference
In-Depth Information
Figure 11.5.
Making copies of terms
?- X=p(a), copy_term(X,T).
X = p(a), T = p(a)
?- X=p(a,Z,b), copy_term(X,T).
X = p(a,_G249,b), T = p(a,_G123,b)
?- X=[Y,W], W=a, copy_term(X,T), Y=a.
X = [a,a],
Y = a,
W = a,
T = [_G359,a]
11.1.3 Back-chaining with variables, negation, and equality
Now consider a final version of est that will correctly handle variables, negation, and
equality. As before, a knowledge base is represented by a list of the representations
of clauses, where a clause is represented by a nonempty list of the representations of
the literals. Recall from section 9.2.3 that an atom can be used as a Prolog term; this
means that an atom can be used to represent itself. Equalities are represented using a
predicate eq ; and negations, using a predicate not . For example, the clause
q(a,X) :- p(Y,Z), \+ r(Y), Z=Y, s(X,Y).
is represented as a Prolog term by the following list:
[q(a,X), p(Y,Z), not(r(Y)), eq(Z,Y), s(X,Y)]
So knowledge bases with variables can be represented, as in the following:
?- est([[p(a)],[p(b)],[q(X),p(X)]], [q(a)]).
X=a
Yes
The predicate est appears to work with variables. But consider this query:
?- est([[p(a)],[p(b)],[q(X),p(X)]], [q(a),q(b)]).
No
The trouble here is that once q(X) unifies with q(a) , the variable X is instantiated,
and so q(X) will not unify with q(b) .
A Prolog predicate that copies terms
The solution is a special built-in Prolog predicate copy_term (
, which holds if x
and y are terms that are identical except that the variables appearing in y are all new,
unrelated to those in x . Some examples of its use are shown in figure 11.5. As can be
x , y
)
Search WWH ::




Custom Search