You are on page 1of 5

What is Prolog?

Prolog is a programming language particularly well suited to logic and artificial intelligence programming. "Prolog" in
fact stands for "Programming in Logic." In this brief introduction we will try to give you a little taste of Prolog without
bogging you down with a great deal of technical jargon. By the end of this section you should be able to use Prolog
and write some little programs that give you a feel for the language. Feel free to consult the books and Web sites on
Prolog mentioned later should you want to go further.

A person uses a computer programming language to direct a computer to perform desired tasks. You might be
familiar already with the names of some common programming languages, such as C, Basic, Pascal, or Cobol.

Not all programming languages work the same way. In languages you might be familar with already, such as C, the
programmer commonly tells the computer that some letters or words are to be variables, for example that the letter
"X" is to be considered a variable that will stand for an integer number (such as 1, 2, etc.). The C program might then
involve a loop mechanism (a repeating set of commands) in which the computer assigns different integer values to X
every time it goes through the loop. In fact a large part of the program could consist of variable declarations,
"iterative" loops, calculations of values, and assignments of values to the variables. The programmer tells the
programmer not only what to do, but how to do it.

A Prolog program is likely to look a little different than a typical C program. The programmer tells the computer less
the "how" than the "what." For example, in Prolog the programmer might begin by telling the computer a bunch of
"facts." The facts can be about characteristics of people or things in the world ("Spot is frisky"), about relations of
such things ("John is the father of Susan"), and about "rules" pertaining to such facts ("Scott is the grandfather of
Susan" is true if "Scott is father the John" is true and "John is the father of Susan" is true).

The Prolog program could then be used to ask the computer about the facts already given and the computer would be
able to provide answers. Given the facts in the previous paragraph, if the computer is asked "Is John the father of
Susan?" it would reply "Yes." If asked, "Is John the father of James" it would reply "No" because it was not given that
fact or other facts from which it could be deduced.

Of course, Prolog, like any other common programming language, will not be able to process ordinary English
sentences such as those given above; rather it requires the programmer to write statements in a particular way . The
programmer must know how to phrase Prolog sentences properly. In a moment we will show you how to write some
simple Prolog statements, so that you can directly use the Prolog facility (PT-Prolog) within PT-Thinker.

But let's point out that you don't have to know Prolog in order to use its logical abilities - that is where PT-Thinker can
help you. PT-Thinker is a Prolog program that can take ordinary English sentences and translate them into a form
suitable for processing by Prolog. So you can see the kind of logical inferences PT-Thinker can make by telling PT-
Thinker facts and then seeing how it deduces conclusions from those facts. As already mentioned, should you decide
to do so you can also use the Prolog facility within PT-Thinker to give the computer such facts and questions phrased
in the Prolog language.

How to Write Prolog Statements Let's start off with some examples of Prolog statements. Note that Prolog is case
sensitive, that is, capital letters are considered different than lower-case letters, so don't just substitute an "A" for an
"a" and assume it will mean the same thing.

Let's tell the computer some facts about a family we know. We'll put the Prolog statement after the English statement.

English   Prolog
John is the father of Susan.   father(john,susan).
John is the husband of Martha.   husband(john,martha).
John eats pizza.   eats(john,pizza).
Susan eats pizza.   eats(susan,pizza).
Martha eats pizza.   eats(martha,pizza).
Susan eats oranges.   eats(susan,oranges).
John bought pizza for Martha.   bought(john,pizza,martha).
Susan is tired.   tired(susan).
Now let's talk about these statements. We can define a fact in the Prolog language by placing the verb or adjective
first and the relevant related nouns in parentheses. The period defines the end of the statement. (Notice that we
aren't using capital letters to start the names; capital letters or terms starting with them we reserve for variables.)

We would type the above facts and then load them into Prolog. The exact command here may vary with the specific
Prolog program/version with which one is working. (In PT-Thinker Prolog, one first types "assert" to enter the mode
for fact loading, types in the statements, and then enters F10 to load the facts.) Think of this loading process as
teaching Prolog the above facts. Prolog then responds with a "prompt," (such as "?-"), that is, the opportunity for the
user to ask questions.

Let's continue with examples, but this time with questions you can ask Prolog. Below are sample English questions,
their Prolog equivalent, and the answer from Prolog. Assume we have loaded the above facts into Prolog already.

English question Prolog (at prompt ?-) Prolog responds


Is John the father of Susan? father(john,susan). yes (or true)
Is Susan tired? tired(susan). yes
Is Martha tired? tired(martha). no (or false)
Who is tired? tired(X). X = susan
Who is the husband of Martha? husband(X,martha). X = john
Who eats pizza? eats(X,pizza). X = john
Who else eats pizza? merely hit spacebar X = martha
Who else eats pizza? hit spacebar again X = susan
Who else eats pizza? hit spacebar yet again no

When we ask Prolog above about John being the father of Susan, we do not have to change the form of the Prolog
statement from that which we used to tell Prolog that John is the father of Susan. This same statement may be used
to tell the computer the fact initially or inquire about it later. The computer knows it is an inquiry in the second
circumstance because we have typed it in response to the Prolog prompt, rather than loading it in as a fact.

Note that we use the variable letter "X" to ask the computer who is tired ("tired(X).") and who Martha's husband is
("husband(X,martha)."). Another way to translate these Prolog statements is as "Give me all those who are tired."
and "Give me all those who are a husband of Martha." We use the same strategy when asking about who eats pizza
("eats(X,pizza).". In such cases, Prolog's response ends by asking us in effect "Do you want me to look for another?"
In PT-Prolog, we signal yes by hitting our space bar, and Prolog attempts to find another. When there are no more to
find, it responds "no." (In other Prolog facilities, one may press the "return" key rather than the spacebar, and a semi-
colon may end the line rather than a question asking us if we want to find another.)

At this point you can go to the PT-Prolog facility within PT-Thinker, enter some facts, and ask some questions. For a
start you might put in facts about countries and capitals ("capital(london,england") and ask Prolog to give them back
to you ("capital(X,france)") or tell you the truth of statements ("capital(london,germany)"). Or type in some facts
about particular people and the foods they like or the colors they prefer.

http://www.mind.ilstu.edu/curriculum/protothinker/prolog_intro.php
An Introduction to
the Programming Language
PROLOG:
A Language for Logic Programming
and Symbolic Computation

There are primarily two computer languages used in artificial intelligence work, LISP and PROLOG. LISP,
which is short for List Processing, was created by John McCarthy of Stanford University. It looks klutzy
but it is based upon the lamba calculus and works quite well for computation associated with artificial
intelligence. PROLOG has an elegant formulation but it does not have the range of application that LISP
has. The Japanese when they formulated the Fifth Generation project chose PROLOG over LISP as the
programming language. This was perhaps one of the factors that contributed to the failure of the Fifth
Generation project. Nevertheless PROLOG is worth knowing for its power in solving questions about
relationships.

A PROLOG program consists of:

 Declaration of the facts of the relations involved.


 Declaration of rules concerning relations
 Formulation of questions to be answered.

Specifying Relationships

Relations can be defined in several different ways. In PROLOG they are defined in a functional form with
the name of the relation first and the object or objects involved in the relation being enclosed within
parentheses. For example,

 billionaire(bill_gates). 
aunt(jessica,liam).

The first one means that bill_gates is a billionaire and the second that jessica is the aunt of liam. In the
above the conventions of PROLOG have been adhered to; i.e.,

 1. all names of relations and objects are in lower case letters 


2. the objects are separated by commas 
3. the relationships end with a period (a full stop)

The first relation can be interpreted as a statement that the billionarity of bill_gates is true. Likewise the
second relation says that it is true that jessica is the aunt of liam.

A more complex relation is

 play(joe,mike,tennis).

which says that joe and mike play tennis together.

A prolog program is run by typing in questions in the form

 ?- aunt(jessica, liam). 
to which the computer would respond: Yes.

If the PROLOG program responds No to a question it does not mean that the statement involved in the
question is false. It only means that it cannot be proved true with the data shown.

A more sophisticated question can be formulated by means of Variables. Variables are unspecified objects
wose names start a with capital letter. For example, if the question is
 ?- aunt(jessica, N). 
the computer would answer
N=liam

If at this point the questioner presses return, the question is ended. If the questioner types semicolon (;)
and then return the computer will search for any more objects for which jessica is an aunt.

Consider this example:

 reads(jane, shakespeare).

reads(henry, hemingway).

reads(frank, falkner).

reads(jake, shakespeare).

The question

 ?- reads(Y,hemingway).

would get the response Y=henry. A semicolon and then return would be the response: no. The question

 ?-reads(Y, shakespeare).

would get the response Y=jane. A semicolon plus return would get the response Y=jake. Another
semicolon plus return would get the response no.

The question

 ?- reads(frank, X).

would get the response X=falkner.

Conjoining Questions

If we want to know if the answers to two questions correspond all that needs to done is to conjoin two
questions with a comma. Thus a coma in PROLOG is equivalent to a logical AND construction. For
example, the conjoined questions

 ?- reads(jane, X), reads(jake, X).

brings the response X=shakespeare.

Rules for Composing Relationships:


Using the PROLOG if Operation

Combinations of relations can be created by special PROLOG operations. Suppose the following relations
are defined:

 parent(joe,jane).

parent(harry,carl).

parent(meg,jane).

parent(jane,anne).
parent(carl,ralph).

parent(hazel,harry).

The grandparent relation is defined using the if operation:

 grandparent(X,Z) :- parent(X,Y), parent(Y,Z).

Thus a PROLOG program consists of the basic facts in terms of declarations and some rules for defining
more complex relations from the basic relations.

(To be continued.)

Symbolic Computation

A good example of symbolic computation and its problems is symbolic differentiation. Below is given a set
of basic rules of differentiation in a PROLOG format.

 deriv(C,X,0) :- constant(C).

deriv(X,X,1) :- !.

deriv(-F,X,-G) :- deriv(F,X,G).

deriv(F+G,X,H+I) :- deriv(F,X,H), deriv(G,X,I).

deriv(F*G,X,H*G+F*I) :- deriv(F,X,H), deriv(G,X,I).

deriv(F^C,X,c*F^(C-1)*G) :- const(C), deriv(F,X,G).

deriv(F/G,X,H/G - (F/G^2)*I) :- deriv(F,X,H), deriv(G,X,I).

deriv(log(F),X,H*(F^(-1)) :- deriv(F,X,H).

The rule that deriv(X,X,1) is always true. This is the meaning of ":- !." It could be read as "if anything."

The output of a program run on the basis of the above data is not simplified. For example, the question

 ?- deriv(X*X,X,A).
gets the response
A = 1*X+X*1

The derivative program needs to operate in conjunction with a simplification program. Some commands in
such a simplification program would be:

 simpl(1*F,F) :- !. simpl(F*1,F) :- !. simpl(F-F,0) :- !. simpl(F/F,1) :- F /=0. simpl(F+F,2*F) :- !.


simpl(C*F+F,(C+1)*F) :- !. simpl(C*F+D*F,(C+D)*F) : simpl(F^0,1) :- F /= 0. simpl(0^C,0) :-
C/=0.

There could of course be many more.

When confronted with the expression (1*X+X*1) the system would convert it successively to (X+X*1),
(X+X), (2*X).

http://www.sjsu.edu/faculty/watkins/prolog.htm

You might also like