Information Technology Reference
In-Depth Information
Figure 3.3.
Two more queries after loading the family.pl program
?- father(sam,X).
% Who is Sam a father of?
X = john
Yes
?- father(U,V).
% Who is a father of whom?
U = sam
V = john
;
% Any more?
U = sam
V = jane
;
% Any more?
U = george
V = sue
;
% Any more?
No
For a query with variables, there will also be three possible outcomes:
Prolog answers No —this means that the atom cannot be established for any
values of the variables.
Prolog does not answer—this means that the atom cannot yet be established for
any values of the variables but that Prolog is continuing to try alternatives.
Prolog displays values for the variables for which it can establish the query. At
this point the user has some choices:
- If the user types a space or a return, Prolog answers Yes and the query
answering is complete.
- If the user types a semicolon ( ; ), Prolog tries to find new values for the
variables, again with the same three possible outcomes.
So if a query with a variable can be established for different values of the variable,
these values can be examined one at a time by using the ; command.
To see how this all works, restart Prolog and again load the family.pl program, as
in figure 3.2. Then pose some additional queries.
The first query shown in figure 3.3 uses a variable to ask who Sam is a father of.
Prolog responds that an answer is John. Then the user typed a space, indicating that
he did not want to see if there were any additional possible answers. The user is
finished with this query.
The second query asks Prolog who is a father of whom using two variables: U for
the father and V for the child. Prolog returns the pair Sam and John (meaning that it
can establish that Sam is a father of John). The user then types a ; asking Prolog to see
 
Search WWH ::




Custom Search