Study on Voice Controlled Computing
PROLOG Language Basics
facts True/False Query wh Query Variables comments rules write
 

PROLOG is, in some ways, very different from the languages most computer scientists use most of the time. It is known as a nonprocedural language. That is, the programmer tells the PROLOG compiler / interpreter what he wants done, not how to do it.

The closest thing to a nonprocedural language that most computer programmers are familiar with, is SQL. In SQL, the programmer does not say how to search through all the records to select the right ones. Rather, he describes the charactaristics of the records he is interested in (think of the WHERE clause of the SELECT statement).

In PROLOG, the programmer supplies the data as well as the queries in the same language. PROLOG data statements are known as facts. Some example facts follow:

man(adam). 
man(peter). 
man(paul).
man(john).
man(mark).
woman(marry). 
woman(eve).
This says adam is a man, peter is a man, and so on. Note that everything is lowercase. Also note that each fact ends with a period. Sorry, C++ lovers, it's not a semicolon!

This example of facts is of little usefulness by itself. After compiling or loading into the interpreter, it does permits us to write two kinds of queries, however. 

First is the True/False or Yes/No query. This query returns a boolean value -- Yes or No. An example would be man(john). or woman(marry). or woman(sarah). The first two would return True because the facts say that john is a man and marry is a woman. The third would return false, because the facts do not give us that sarah is a woman. 

Dr. Dougherty calls the second kind of query a wh query. This query more closely approximates a SELECT statement in SQL. It returns all the records for which the query is true. woman(X). is an example. It would return that X = marry and X = eve. 

Note the use of a capital letter. Capitals, in PROLOG, are variables. The query is best understood as "For what values of X is X a woman?"

As in relational databases, facts (records) can contain multiple fields. Also, records must be unique, again as in a relational databases. The following code illustrates this.

parent(adam,peter). /* means adam is parent of peter */
parent(eve,peter). 
parent(adam,paul). 
parent(marry,paul).
parent(paul,john).
parent(john,mark).
Note that comments are enclosed in /* ... */ as in C/C++. This form works for single-line comments, but I cannot vouch for comment blocks. 

Prolog allows the programmer to write rules as well as facts.  Rules do not compare directly to anything in the realm of relational databases, so that analogy breaks down at this point.  Rules put data from multiple facts together.  Some examples will help explain this.

descendent(D,A):-parent(A,D).
descendent(D,A):-parent(P,D),descendent(P,A). /*RECURSION*/
The first rule says that D is a descendent of A if A is a parent of D.  The second rule introduces recursion to allow grandchildren, great grandchildren, and so on to be descendents.  We would read that second rule as, "D is a descendent of A if there exists a P such that P is a parent of D and P is a descendent of A".  Note that the comma in the rule serves essentially as an and.

One more thing that should be introduced as part of basic PROLOG is the write statement. This simply outputs a string. We could use write to trace the progress of the descendent rule this way:

    descendent(D,A):-parent(P,D),write(P),write(' is a parent of '),write(D),nl,descendent(P,A).
The nl command simply outputs a newline character.

Once these ideas make sense, please read about more PROLOG and about pitfalls to avoid in PROLOG.

Hosted by www.Geocities.ws

1