Information Technology Reference
In-Depth Information
% The premises for an old tidbit of logical reasoning
man(socrates).
% Socrates is a man.
mortal(X) :- man(X).
% All men are mortal.
A still longer one is the family program in figure 3.1. Typically programs like these are
stored in computer files with names like family.pl , one per file. (All the programs
shown in the figures are available online. The name of the file appears at the top right
of each such figure. See appendix A for a complete list.)
3.2 Prolog queries
Once a legal Prolog program has been stored in a file, it is ready for use. But a Prolog
program by itself does not really do anything; it only acts in response to queries .So
here is the usual way of running a Prolog program:
1.
Prepare a file containing the Prolog program.
2.
Start the Prolog system, and ask it to load the program file.
3.
Repeatedly do the following:
a. Pose a query to the system.
b. Wait for Prolog to return an answer.
(Often the queries and answers are saved in a separate log file for later.)
4.
Exit the Prolog system.
The details of how these steps are actually carried out vary from system to system.
Figure 3.2 shows a typical run of SWI-Prolog under Linux. (Prolog under Windows
or Mac OS X is quite similar. See appendix B.) In this chapter, bold like this is
used to indicate what a user would type to a Prolog system, and gray italics indicate
comments. Anything else is what the Prolog system itself produces.
So in this case, the user starts Prolog, loads the family.pl file (figure 3.1), and then
asks two queries, one that returns success and one that returns failure . The name of
the file to be loaded appears within square brackets but without the final .pl part.
Here, success of the query is indicated by Yes and failure by No . (In some versions of
Prolog, true and false appear instead. See appendix D.)
Note that within Prolog, the ?- is its way of saying, “I am ready. Give me something
to do.” In this case, the user tells it (using the square brackets) to load a file, respond
to two queries about sam and jane , and then stop (using the special predicate halt ).
 
 
Search WWH ::




Custom Search