next up previous contents
Next: Evaluation in Prolog Up: Programming Techniques and Previous: Programming Techniques and

The `Reversibility' of Prolog Programs

Consider the program:

This has the reading that the second argument is the square of the first argument. There are four kinds of query: we can ask what is the square of a specific number, what number has a specific square and what entities are related by the square relation. We can also ask whether two specific numbers are in the relation to each other of one being the square of the other. The queries would look like this:

 
?-  square(2,X).

?- square(X,5).

?- square(X,Y).

?- square(2,3).

Unlike many other programming languages, we do not need different procedures to calculate each of these results. This is a consequence of the declarative reading of Prolog. Sometimes we say that the program for square/2 is reversible.

This is a very desirable property for programs. For example, if we could write a program to determine that a given string of words was a legitimate sentence then we could use the same program to generate arbitrary grammatical sentences. Unfortunately, it not always possible to give a declarative reading to a Prolog program.





Paul Brna
Mon May 24 20:14:48 BST 1999