Information Technology Reference
In-Depth Information
Figure 3.4.
Conjunctive queries after loading the family.pl program
?- female(jane), parent(sam,jane).
Yes
?- female(F), parent(sam,F).
F = jane
;
No
if there are any additional answers. It returns Sam and Jane as the next answer. (Same
father, different child.) The user asks for more with another ; and Prolog answers
George and Sue. The users asks for more with another ; and it answers No (meaning
it is unable to establish the query for any other values).
3.2.2 Conjunctive queries
So far, queries have been single atoms terminated with a period. Prolog also allows
conjunctive queries , which are sequences of atoms separated by commas and terminated
by a period. These queries are understood conjunctively , in the sense that Prolog is
asked to establish all the atoms in a single query (from left to right). So the comma in
a conjunctive query plays the role of and just as it does in the body of a conditional
sentence.
Assuming the family.pl file is loaded as before, consider the two queries in
figure 3.4. The first one asks if Jane is female and if Sam is a parent of Jane. The
answer is Yes . But this is not very useful, since the job could have been done with
two separate atomic queries. What makes conjunctive queries useful is when there
are variables that appear in more than one atom. So, for example, in the second query
in figure 3.4, the variable F appears in both parts. This query asks if there is an F that
satisfies two properties: F is female and Sam is a parent of F . Another way of saying
this: Is there a female that Sam is a parent of? The key point is that the user seeks
the same person for both atoms. The answer returned by Prolog is Jane, and after the
user types the ; command to ask if there are more, Prolog says No (Jane is the only
answer).
Chapter 2 did not deal with conjunctive queries like this, but they were there
implicitly. The back-chaining procedure (see figure 2.2) contained the following two
instructions:
2.
Otherwise, try to locate a conditional sentence of the form
If P 1 and . . . and P n then Q in the KB. If you cannot, return failure .
 
Search WWH ::




Custom Search