17-05-2012, 05:40 PM
Prolog Programming
prolog.pdf (Size: 488.37 KB / Downloads: 45)
The Basics
Prolog (programming in logic) is one of the most widely used programming languages
in artificial intelligence research. As opposed to imperative languages such as C or Java
(which also happens to be object-oriented) it is a declarative programming language.
That means, when implementing the solution to a problem, instead of specifying how
to achieve a certain goal in a certain situation, we specify what the situation (rules and
facts) and the goal (query) are and let the Prolog interpreter derive the solution for
us. Prolog is very useful in some problem areas, such as artificial intelligence, natural
language processing, databases, . . . , but pretty useless in others, such as graphics or
numerical algorithms.
By following this course, you will learn how to use Prolog as a programming language
to solve practical problems in computer science and artificial intelligence. You will also
learn how the Prolog interpreter actually works. The latter will include an introduction
to the logical foundations of the Prolog language.
These notes cover the most important Prolog concepts you need to know about, but
it is certainly worthwhile to also have a look at the literature. The following three are
well-known titles, but you may also consult any other textbook on Prolog.
• I. Bratko. Prolog Programming for Artificial Intelligence. 3rd edition, Addison-
Wesley Publishers, 2001.
• F. W. Clocksin and C. S. Mellish. Programming in Prolog. 5th edition, Springer-
Verlag, 2003.
• L. Sterling and E. Shapiro. The Art of Prolog. 2nd edition, MIT Press, 1994.
Getting Started: An Example
In the introduction it has been said that Prolog is a declarative (or descriptive) language.
Programming in Prolog means describing the world. Using such programs means asking
Prolog questions about the previously described world. The simplest way of describing
the world is by stating facts, like this one:
Terms
The central data structure in Prolog is that of a term. There are terms of four kinds:
atoms, numbers, variables, and compound terms. Atoms and numbers are sometimes
grouped together and called atomic terms.
Atoms. Atoms are usually strings made up of lower- and uppercase letters, digits, and
the underscore, starting with a lowercase letter. The following are all valid Prolog atoms:
Some Built-in Predicates
What we have seen so far is already enough to write simple programs by defining predicates
in terms of facts and rules, but Prolog also provides a range of useful built-in
predicates. Some of them will be introduced in this section; all of them should be explained
in the user manual of your Prolog system.
Built-ins can be used in a similar way as user-defined predicates. The important
difference between the two is that a built-in predicate is not allowed to appear as the
principal functor in a fact or the head of a rule. This must be so, because using them in
such a position would effectively mean changing their definition.