Conventional languages, such as Pascal and C, are generally considered procedural languages. Programs written in these languages are a set of instructions telling the computer what to do step by step. Prolog, on the other hand, is a declarative language, which is more concerned with stating relationships between objects. A Prolog program is a set of 'facts' and 'rules' that can serve as a knowledge base. A user can send 'questions' to query the program for information that is either explicitly stated or is implied in the knowledge base.
Let us illustrate these through examples.
Example 1
This is a very simple Prolog program which contains only one fact. The fact is 'david loves mary'. Expressed in Prolog it is:
love(david, mary).
In this fact, there are two objects: 'david' and 'mary'. The relationship between them is 'love'. The relationship is called a predicate, the objects are called the arguments of the predicate. A predicate can have any number of arguments.
Example 2
This example contains rules. One of the rules is:
father(X,Y) :-
child(Y,X),
male(X).
which means:
If Y is a child of X and X is male then X is Y's father.
The symbol ':-' is used to represent 'if'. The following is a complete program.
female(ann).
female(mary).
female(jane).
female(ellen).
male(bob).
male(cory).
male(larry).
male(david).
male(peter).
male(tim).
child(ann, bob).
child(cory, bob).
child(ellen, jane).
child(bob, david).
child(jason, david).
child(mary,peter).
child(tim, peter).
child(david,larry).
child(peter,larry).
father(X,Y) :-
child(Y,X),
male(X).
mother(X,Y) :-
child(Y,X),
female(X).
grand-father(X,Y) :-
father(X,Z),
father(Z,Y).
grand-father(X,Y) :-
father(X,Z),
mother(Z,Y).
Let us write the preceding program into a text file and name it, say, 'example2'. Make sure you type the case of the letters correctly and end each clause with a period.
To start Prolog, you type 'prolog' at the shell prompt 'hercules%' and press the [return] key. After you press the [return] key, the prompt |?- will appear. This prompt indicates that the system is ready to receive your query.
To consult your program file, type the filename enclosed in a pair of square brackets:
|?- [filename].
or equivalently,
|?- consult(filename).
For example, to consult the file 'example2' we should type:
| ?- [example2].
or
| ?- consult(example).
Let us make some queries. To check whether a fact is true, type the fact. For example:
| ?- child(ann, bob).
Since this fact is stored in the database, the system will display the answer:
yes
If you type:
| ?- father(david, jason).
This fact is not stored explicitly but it can be deduced from the facts and rules, so the system will also answer
yes
If we want to know who jason's father is, we can type:
| ?- father(Who, jason).
The system will answer:
Who = david
For some questions there may be more than one answer. To obtain another answer, type ';' at the end of the current answer. For example, if we type:
| ?- father(david, Who).
The system will answer:
Who = bob
Type ';' and press the [return] you will get:
Who = jason
Type ';' and press the [return] again you will get:
no
because david only has two children.
To exit from Prolog, type 'halt.' or [ctrl [D]].
head :- body.
A clause should be terminated by a period.
Example:
parent(X,Y) :-
child(Y,X).
The head of this clause is 'parent(X, Y)', the body is 'child(Y,X)'. There are three basic types of clauses: facts, rules, and queries.
relationship(object1, object2).
For example, the fact that ann is bob's child would be written in Prolog as:
child(ann, bob).
In the example, 'child' is the name of the relationship, and 'ann' and 'bob' are both objects. A relationship is also called a predicate, and the objects are called arguments or parameters. A predicate can have any number of arguments. The number of arguments is called the arity of the predicate. For instance, 'child' is a predicate of arity 2. The fact that david is male can be expressed by a predicate of arity 1:
male(david).
Arguments can also be numbers. David Epp is 36 years old can be defined by:
age(david_epp, 36).
It is important to note that:
1. The names of predicates must begin with a lower-case letter.
2. A fact must be terminated with a full stop.
3. The character underscore '_' can be used in the middle of
a name to make the name more readable.
In the preceding examples, the arguments are constants which denote particular objects. To express properties that apply to objects in general, we use variables as arguments. Variables are names that stand for objects in general. A variable name should begin with a capital letter. For example, the fact:
is_mortal(Everyone).
means everybody is mortal.
Sometimes it is more readable to use infix notation in which predicate names are written between two arguments. For example:
3 < 5.
In this example, '<' is the predicate and '3' and '5' are its arguments.
Head :- Body.
which may be interpreted as "'Head' is true if 'Body' is true". The operator
':-' denotes logical implication. For example, the sentence:
If Tom is Mary's child then Marry is Tom's parent.
can be written as a rule:
parent(mary,tom) :-
child(tom,mary).
Rules may have variables. For example:
parent(X,Y) :-
child(Y,X).
The body of a rule may consist of more than one literal. A literal generally is constructed from a predicate name and arguments enclosed in brackets and separated by commas. For example, the body of the preceding rule consists of just one literal:
child(Y,X)
However, if a predicate is an infix operator, the literal has the form 'argument operator argument'. For example, in the following rule:
experienced :-
working_year(Y),
Y > 5.
the body has two literals:
working_year(Y)
and
Y > 5
The predicate in the second literal is '>' which is an infix operator,
so the literal
has the form 'argument operator argument'.
Literals in the body are called goals. They can be connected conjunctively (logical 'and') or disjunctively (logical 'or'). In case of a conjunction, goals are separated by commas and goals must all be true if the head is to be true. In case of a disjunction, goals are separated by semi-colons and head is true if at least one of the goals is true. Here are some examples:
The rule
X is Y's mother if Y is X's child and X is female.
can be expressed as:
mother(X,Y) :-
child(Y,X),
female(X).
Notice the comma denoting a conjunction.
The rule:
X is Y's if X is Y's son or X is Y's daughter.
can be expressed as:
child(X,Y) :-
son(X,Y);
daughter(X,Y).
Notice the semi-colon denoting a disjunction.
| ?- [example2].
Now we are ready to ask questions. The simplest form of a question is to check a fact. To do this, you just type in the fact.
For instance, if we want to check 'Is it true that Tim is male?', we type:
| ?- male(tim)
The Prolog system will look up its database (the program) to see if the fact is true. If the fact can be proved, the system will display yes else it will display no. Since this fact is listed in the database, the system will display:
yes
If we type:
| ?- child(tim, ann).
Since this fact can not be proved given the facts in the database, the system will display:
no
| ?- child(david, Who).
After press [return] key the system will display:
Who = bob
If we want to know more, we type a semicolon ';'. The system will answer:
Who = jane
Type ';' again. This time we will get:
no
because there are no more answers.
The following is a query using conjunction to check if david has a son:
| ?- child(Who, david), male(Who).
The system will answer:
who = bob
The following is a query using disjunction to check if jane is allen's parent:
| ?- mother(jane, allen); father(jane, allen).
The system will answer:
yes