This
tutorial covers the fundamental concepts behind Prolog programming.
Although most Prolog interpreters can execute the examples in this
tutorial, we use the Prolog Inference Engine (PIE) example included
in the Visual Prolog 6 distribution from the Prolog Development Center
(PDC - www.visual-prolog.com).
PIE is a classical Prolog interpreter that assists in learning and
experimenting with Prolog.
Horn Clause Logic
Horn Clause
logic, which forms the basis of Visual Prolog and other Prolog dialects,
is a formal system for reasoning between things and how they relate
to each other. For example, in english we could express the statement:
John is the father of Bill.
Here there are two things: John and Bill, and a relation
between them, namely that one is the father of the other. In Horn
Clause Logic, this statement formalizes as:
father(Bill, John).
where father is a predicate/relation taking two arguments,
with the second argument being the father of the first. Notice that
we define the second person as the father of the first. We could
have also chosen it the other way around: Although the order of
the arguments is the choice of the formalization designer, once
chosen, the logic must remain consistent. Therefore, in this formalization
the father is always the second person listed.
In these
examples, their names represent the people (which are string
literals). In a more complex world, this is insufficient since
many people have the same name. However, for now this simple formalization
will work for this tutorial. With formalizations such as the one
above it is possible to state any type of family relation. For this
to become at all interesting, however, we also have to formalize
rules such as:
X is the grandfather
of Z, if X is the father of Y and Y is the
father of Z
|
|
where X, Y and Z are people. Horn Clause Logic
formalizes this rule as follows:
grandFather(Person, GrandFather) :-
father(Person, Father), father(Father, GrandFather).
This example uses variable names to improve understanding as compared
to X, Y and Z and introduces a predicate for the grandfather relation.
To be consistent, the GrandFather is the second argument
and arguments of any other predicates should follow a similar principle.
When reading rules, interpret :- as if and the
comma that separates the relations as and.
Facts
are statements such as John is the father of Bill, while
rules are statements such as X is the grandfather of
Z, if X is the father of Y and Y is the father of Z. With
facts and rules, we are ready to formulate theories. A theory
is a collection of facts and rules such as:
father(Bill, John).
father(Pam, Bill).
grandFather(Person, GrandFather) :-
father(Person,
Father),
father(Father, GrandFather).
The purpose of the above theory is to answer questions such as:
Is John the father of Sue?
Who is the father of Pam?
Is John the grandfather of Pam?
...
Formalizing
these questions as goals (respectively):
?- father(Sue, John).
?- father(Pam, X).
?- grandFather(Pam, John).
|