Information Technology Reference
In-Depth Information
Figure 3.8.
Queries using equality
?- X=sam, X=Y.
X = sam
Y = sam
Yes
?- X=Y, sam=X, \+ Y=jack.
X = sam
Y = sam
Yes
?- X=Y, sam=X, \+ Y=sam.
No
?- X=Y.
X = _G180
Y = _G180
Yes
As it turns out, inequality is more useful than equality in queries. In fact, it is never
necessary to use unnegated equality in a query. Instead of writing a query like
child(john,X), child(jane,Y), X=Y.
one should write
child(john,X), child(jane,X).
where the same variable is used more than once. However, negated equality does
come in handy, as seen in the queries of figure 3.9.
The first query asks if Sam is parent of someone other than John. In the back-
chaining required for this query, X will be instantiated by the parent query and the
first tentative value happens to be john . The negated equality fails for this value, and
so the parent query is reconsidered, and the second tentative value, jane , is found,
for which the rest of the query succeeds.
The second query is the first attempt to get the names of three males. Observe that
this does not work properly, since all three variables can have the same value. The
third query remedies this and insists that the three values must not be equal, using
negated equalities. In this case, Prolog reconsiders the male queries and comes up
with three distinct values, john , sam , and george .
Search WWH ::




Custom Search