7.17 PROLOG

Copyright©  Department of Computer Science, University of Regina
Originally Written in 1996 by Zhiwei Wang 

7.17.1. OVERVIEW

Prolog, standing for PROgramming in LOGic, is a high-level programming language mainly used in artificial intelligence (AI) research and applications. It was developed by French computer scientist Alain Colmerauer and logician Philippe Roussel in the early 1970s. In 1981, it was selected by the Japanese government as the principal language for Japan's Fifth Generation Computer Systems project.

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]].

7.17.2. BASIC PROLOG CONCEPTS

7.17.2.1. CLAUSES

Prolog programs consist of statements called clauses which are analogous to sentences of natural language. A Prolog clause consists of a head and a body and generally has the following form:

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.

7.17.2.2. FACTS

A fact is a clause that has an empty body. It declares a relation that is unconditionally true. The general form of a fact is:

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.

7.17.2.3. RULES

A rule is a clause that has a head and a non-empty body. It is represented in the form:

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.

7.17.2.4. QUESTIONS

7.17.2.4.1. SIMPLE QUESTIONS

Once we have some facts, we can ask questions about them. Suppose we have stored all the facts in example 2 in a file. The file-name is example2. To open the file for consulting, we type the file-name enclosed by a pair of square bracket:

| ?- [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

7.17.2.4.2. QUESTIONS CONTAINING VARIABLES

A variable is a name which stands for objects to be determined. A variable name must begin with a capital letter. Suppose we want to know all the children of david. We can ask a question containing a variable:

| ?- 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.

7.17.2.4.3. COMPOUND QUESTIONS

Compound questions consist of conjunctively or disjunctively connected literals. Literals in a question are also called goals. In case of a conjunction, the answer is 'yes' if all the goals are true. In case of a disjunction, the answer is 'yes' if at least one of the goals is true.

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