Information Technology Reference
In-Depth Information
Figure 3.1.
The family example in Prolog
family.pl
% This is the Prolog version of the family example
child(john,sue).
child(john,sam).
child(jane,sue).
child(jane,sam).
child(sue,george).
child(sue,gina).
male(john).
male(sam).
male(george).
female(sue).
female(jane).
female(june).
parent(Y,X) :- child(X,Y).
father(Y,X) :- child(X,Y), male(Y).
opp_sex(X,Y) :- male(X), female(Y).
opp_sex(Y,X) :- male(X), female(Y).
grand_father(X,Z) :- father(X,Y), parent(Y,Z).
The bad news is that in contrast to natural languages, Prolog usage is extremely
strict. The rules of English are quite forgiving. If you write, “I recieved the package,”
people will still understand what is meant despite the spelling mistake. You can even
say, “accident car passenger hospital,” and still be understood, more or less.
In Prolog, there is no such flexibility. Every single character matters! A missing
comma or a misplaced parenthesis can make the difference between a Prolog pro-
gram that behaves exactly as intended or a program that does nothing even close. So
you will need to be very meticulous and precise about all the details of the Prolog
notation. If you are ever going to write your own programs, there is really no choice
but to get those details just right. If you already know how to program well in Prolog,
you can safely skip this chapter and the next. If you don't, you will learn how to write
Prolog programs here.
This chapter is divided into three sections. The first section examines the makeup of
Prolog programs in detail. The second section does the same for Prolog queries, which
are used to run Prolog programs. The third section reexamines the back-chaining
procedure that Prolog uses.
3.1 Prolog programs
Prolog programs are simply knowledge bases of atomic and conditional sentences
like those of the previous chapter, but with a slightly different notation. The easiest
way to get a sense of that notation is to look at figure 3.1, which contains the entire
family example from figure 2.1 written as a Prolog program. (Prolog programs are
 
 
Search WWH ::




Custom Search