Information Technology Reference
In-Depth Information
Figure 3.7.
Instantiated variables and negation
?- parent(X,john), \+ female(X).
X = sam
Yes
?- \+ female(X), parent(X,john).
No
A line that begins Fail: is printed when the atomic query has failed and Prolog
needs to look for alternatives.
A line that begins Exit: is printed when the atomic query has tentatively
succeeded, pending the remaining conjunctive query.
A line that begins Redo: is printed when Prolog has gone back to a choice point
to reconsider an atomic query.
The numbers that appear within parentheses in such traces are worth looking at. They
indicate the level at which Prolog is working. In figure 3.6, the lowest level happens to
be (8). This is the level at which Prolog finds the parent and \+ female queries. Once
Prolog uses the conditional sentence with parent in the head, and starts working on
a child query from the body, the number is incremented to (9). (If there had been
conditional sentences with the child predicate in the head, the atoms in the body
would have been at level (10), and so on.)
Although the trace generated by Prolog is succinct, with a bit of practice, you can
use it to reconstruct a more reader-friendly trace like the detailed one done in this
section.
3.2.5
Instantiated and uninstantiated variables
Now that you have seen how Prolog handles negated queries, there is one small quirk
to examine in the back-chaining procedure.
Consider the two queries in figure 3.7. The first one is familiar. The second one
simply changes the order in the conjunction. It appears to ask for an X such that X is
not female and X is a parent of John. But this time the query fails.
Why are the answers different? Here is a rough trace of the second query:
1.
The original query is \+ female(X), parent(X,john).
2.
Work on \+ female(X) . Since this is a negated query, remove the \+ and consider
the unnegated version.
 
 
Search WWH ::




Custom Search