Information Technology Reference
In-Depth Information
a. Work on the query female(X) ; this query succeeds. (The value of X will not
matter.)
Since the unnegated version succeeds, \+ female(X) fails. The back-chaining
does not go on to the second atom; it simply returns failure .
So the second query fails because its first part, \+ female(X) , fails. And that fails
because its unnegated version, female(X) , succeeds. So the trace never makes it to
the step “and X is a parent of John.”
The difference between the two queries is this. In the second query, the variable
X is uninstantiated (does not yet have a tentative value) when the negated portion of
the query is handled. In the first query, the variable X is instantiated (has a tentative
value) before the negation step. Back-chaining with negation handles these two cases
differently.
While there may be cases when something like the second query is wanted, they
will be very rare. One is much more likely to want to know about the nonfemale
parents of John (and to require something like the first query).
When variables appear in a negated query, make sure that they are already
instantiated at an earlier stage of the back-chaining.
The most common way to ensure that a variable is instantiated is by using another
atom in a query before the negated part, as is done in the first query of figure 3.7.
3.2.6 Equality in queries
The final refinement to the language of queries concerns equality queries . Prolog allows
elements in a query of the form
term 1 = term 2
where the two terms are either constants or variables. This query succeeds when the
two terms can be made equal by instantiating any variables, and fails otherwise. Some
examples are presented in figure 3.8.
The first and second queries succeed by instantiating both X and Y to sam . The third
query fails, since there is no way to instantiate X and Y to satisfy all three requirements.
Finally, in the last query, there is a new form of answer to a query with variables: the
query succeeds but there is no single constant value for X and Y . Prolog indicates this
by putting a variable as the value for X and for Y . The fact that the same variable is used
for both X and Y is Prolog's way of saying that they can be instantiated to anything,
but the two values must be the same.
 
 
Search WWH ::




Custom Search