Classic Computer Magazine Archive START VOL. 2 NO. 5 / SPRING 1988

A TASTE
OF PROLOG

Exploring AI on the ST

by Christopher F. Chabris and Gene Weiss

The Fifth Generation. It's Japan's concerted project to design computers that really understand their users. That takes Artificial Intelligence-and the language of the Fifth Generation Project is Prolog. Join AI expert Christopher Chabris and programmer Gene Weiss for a tour of the Prolog world on the ST-including a ready-to-run version of Prolog on your START Disk!

You'll find the program for this article in the file PROLOG.ARC on your START Disk.

Before you think about Prolog, think about the bad old days for a moment.

The first personal computers didn't use a mouse or drop-down menus. To do anything with them required often-cryptic commands that were easy to forget or mistype. Back then, computers were confusing and hard to use.

The current generation of more powerful machines -including the Atari ST-use the mouse-and-menu user interface that's opened up the personal computer for a whole generation of new users. It's an easier, friendlier way of using a computer that makes it much easier for you to understand your computer.

Now imagine what it would be like to have a computer that understands you. One that can comprehend ordinary English, instead of requiring commands or even mouse-clicks. Now that's user friendly.

And that's what Japan's Fifth Generation Computer Project is all about: making a truly intelligent computer. It's probably the largest artificial intelligence project in history- and the official language for the project is Prolog.

For a decade after it was first created in Europe, Prolog was little more than an obscure toy for computer scientists. But since the Fifth Generation project was launched in 1982, Prolog has grown into a full-scale applications development language used by thousands of programmers. It's now one of the hottest programming languages around.

Prolog is a hot item on the Atari ST, too. There are two major commercial implementations of Prolog for the ST:
XPro from Rational Visions and MProlog from Logicware. But if you're not sure whether Prolog is the language for you, you're in luck-you can try a limited version of Prolog on this issue's START Disk.

WHAT PROLOG IS
Modern computers have been around for more than 40 years now, and the pioneers who built the first machines would scarcely recognize today's computers, so great has the improvement in hardware been. But they would have no trouble recognizing our methods of programming- the process has changed very little since their days. There are three basic steps involved:

  • Reserve areas in memory to hold data structures whose contents will change an indefinite number of times during the progranis execution.
  • Create a program which will tell the machine in the most painstaking detail just how to manipulate the data in the data structures.
  • When the program fails to work properly (and it will), spend a great deal of time debugging it, getting very upset in the process.
This process has come to be seen as a natural, almost inevitable, way to tell a computer what to do, and it's difficult for us to imagine that there's any other way. But any programmer who's spent time involved in this process must come to the conclusion that it's simply unsatisfactory.

Not surprisingly, an enormous effort has been under way to find a better way to program. Prolog uses an approach called logic programming (in fact, its name is shortened from the phrase "Programming in Logic"), attempting to adapt some of the principles of logic to computer programming. Consider the following informal statement:

A man is wise if he is Greek and he's a philosopher.

If you think about it, there are two very different ways to read this sentence. Obviously it's a declaration of a fact about Greek philosophers. But at the same time, it can be seen as a procedure for solving a problem. The sentence can be paraphrased, "If you want to find out if a man is wise, first find out if he's Greek, and then find out if he's a philosopher"

This dual meaning- the declarative and the procedural-is one of the central insights of logic programming. What it means to the programmer is this: If you very carefully and logically declare what you know about a problem, you are at the same time implicitly defining a procedure for solving that problem.

A Prolog program consists of a database of facts and rules (called clauses), both expressed in a syntax borrowed from formal logic. The basic building block of clauses is the predicate, which expresses something about some elements of the world. For example,

philosopher(Plato)

simply states that Plato is a philosopher. In this example, "philosopher" is the predicate, and "Plato" is its argument. (Don't confuse either the syntax or the vocabulary with that of a function call in a conventional language like Pascal; although a predicate and a function call can often be thought of in similar ways, they're really not the same thing at all.)


If you
declare what you
know about a problem,
you are defining
a solution.

Predicates may also express relationships between two or more things: the predicate

teacher_of (Plato, Aristotle)

tells us something about the relationship between two philosophers (in this case, that Plato is the teacher of Aristotle).

You can even use variables in predicates. In

teacher(Plato,x)

the x is a variable which can be filled in with pretty much anything. Again, be careful with the similarities to conventional languages: a variable in Prolog is quite different from a variable in Pascal.

All the clauses introduced so far are facts, because they state unconditional truths. These facts can be combined to form conditional rules about the database. For example,

wise(x) :- Greek(x), philosopher(x)

is Prolog's way of stating the informal rule mentioned earlier: a man is wise if he is both Greek and a philosopher "Wise(x)" is called the head of the rule, and the part after the ':~' is known as the body. Notice that now "wise(x)" may or may not be true, depending upon the body of the clause. Facts and rules together constitute a database, like this small example:

wise(x) :- Greek(x), philosopher(x)
Greek(Plato)
philosopher(Plato).

If someone were to present this database to you and then ask you whether Plato was wise, you could, with a little logical thinking, answer yes-and so can Prolog. Although the database does not contain a single instruction for Prolog, the language itself knows enough about logic that it can deduce the answer. That's the ideal of logic programming: the programmer simply states the what of the problem, and Prolog provides the how.

If you're thinking that this sounds too good to be true, you're right. Prolog isn't a panacea for all programming problems. There are many jobs it is simply not suited for, and even when applied to the correct task, it doesn't work magic. If you believe Prolog will relieve you of the task of thinking, you're going to be disappointed. But if if you're willing to try a new way of thinking about problems, you'll find that Prolog can be a very elegant language. and it is enormously powerful - it's amazing what 25 lines of Prolog can accomplish.

PROLOG APPLICATIONS
Because of its powerful logic programming features, Prolog has found a place in both the world of artificial intelligence (AI) and in conventional computer applications.

AI applications typically use Prolog's built-in database for facts and rules to store information appropriate to a particular problem. Expert systems are a natural application -the expert knowledge can simply be encoded as a series of Prolog facts and rules. And Prolog can also be used to build an intelligent expert system user interface.

Natural language processing is an area where AI meets conventional needs. For example, most database management systems have arcane command interfaces that require menu selections or query languages. A Prolog database might let you simply ask, "What are the salaries of the female employees?" instead of requiring a complex command.

Symbolic mathematics is another promising area. Prolog is poor at arithmetic and numerical calculation, but it's much more likely to be able to handle symbolic math, which works at the level of equations and variables. The goal is to create an intelligent assistant that can help practicing scientists with differentiation and integration, vector analysis, matrix manipulation, and other areas of higher mathematics.

Outside AI, Prolog is ideally suited to rapid prototyping of programs-and that's indispensable as software systems get larger and larger. Other mainstream Prolog applications include compiler design, digital electronic circuit verification, proving program correctness and teaching programming to children.

Since Prolog is a general-purpose language like BASIC and C, it can be used to create all sorts of programs. But Prolog really excels at applications like database management, logical reasoning and natural language processing- many of the key fields targeted for future generations of computer research.

XPRO
There are two commercial versions of Prolog that bring its power to the ST: XPro Extended Prolog from Rational Visions and MProlog from Logicware. XPro is a straightforward Prolog interpreter based on the original version of Prolog developed at the University of Edinburgh in Scotland. (It's sometimes called the DEC-10 version, after the computer it was developed on.) XPro is supplied on one single-sided disk containing the interpreter, a special run-only version of the interpreter and several programming examples.

XPro offers all the standard features and syntax of DEC-10 Prolog, so it should run most programs found in books and magazines; when problems, arise they should be relatively easy to fix. Its many standard primitives support metalevel programming (assert, retract, = ... call, etc.), input/output, operator definition and definite clause grammar translation. Through an innovative extension to traditional Prolog, you can also create and manipulate arrays in memory using the peek and poke primitives. To cut down on annoying parentheses and make interactive programming more convenient, XPro predefines the built in single-argument predicates as prefix operators. in general, Xpro's implementation of Prolog is excellent.


The MProlog
manual is a joy.

Unfortunately, the XPro manual is not. It's only 20 pages and lacks an index-hardly adequate for documenting a complex programming language. System primitives get only a line or two each; you're referred to Prolog textbooks like Programming in Prolog for fuller explanations. Several BIOS, XBIOS and GEMDOS functions are available as primitives, but the manual just refers users to example files on the disk, admonishing "study them carefully." (XPro's sample programs are illustrative, and also well-written and useful.)

Likewise, the interface to the GEM AES and VDI, via the aes and vdi primitives, is discussed in only four paragraphs. What XPro sorely needs is a library of prewritten predicates that use these features. (You can, however, make any AES or VDI call if you have the patience to code the intricate parameter lists.)

XPro also has a built-in text editor that can handle only about 100 lines worth of text. Most Prolog program segments are smaller than that, but it's an annoying artificial limitation. For debugging, the trace primitive causes each goal to be printed out as it is called and either succeeds or fails, along with the depth of recursion at each step.

XPro earns kudos as the system that lets you distribute your ST Prolog applications. The save primitive dumps the current database contents into a file called KBASE that is automatically reloaded when the run-only interpreter is executed. That means you can simply rename the run-only interpreter and make sure KBASE is in the same directory, and you have a stand-alone application. According to the manual, you can distribute such applications "free of licensing restrictions."

XPro will appeal to several groups of users: programmers who have experience with DEC-10 or C-Prolog on larger machines and want a similar environment for their STs; students who prefer the more responsive environment of the ST to a timesharing system for their programming assignments; and developers who need to create a GEM interface for their Prolog programs.

MPROLOG
If XPro is the economy model of Prolog for the ST, MProlog is the luxury model. It was originally implemented for developing applications on mainframe computers, and it's currendy available for machines ranging from supercomputers to PCs. The same Prolog language syntax and features are used by all versions, making them source code compatible.


XPro
will appeal to
developers who need
GEM in their Prolog
programs.

But the feature that is unique to MProlog is modular programming. Modularity lets you break large programs into distinct modules. The code in one module is "invisible" to any other module, which makes the programs easier to understand and maintain. MProlog adds this idea to the DEC-10 standard, and MProlog's Logic-Lab and PDSS programming tools encourage modularity.

MProlog comes on two double-sided disks, with a large collection of manuals. The interpreter, pretranslator and consolidator programs are all GEM-based, with menus for commands and windows for log reports and interaction. To develop a Prolog program with MProlog, you first use a text editor to prepare the first draft of a program module, then load it into PDSS for debugging and modification. With PDSS you can enter facts and rules, read and write source files, and debug your program. You can control the debugging environment by setting the stack size. print level, and many other parameters.

Once you're satisfied with the module, you can pretranslate it: and once all your modules are working, you can consolidate them together and load the result into the interpreter for final use without PDSS. You can also consolidate PDSS into your program to have it available in case you need it.

MProlog provides a rich selection of built-in predicates. Every feature of DEC-10 Prolog is implemented with extensions, although the names of some predicates are different. If you only know DEC-10 Prolog, there's a DEC-10 compatibility module that alleviates the problem caused by the different predicate names. There's a selection of advanced utilities for manipulating lists and strings, controlling execution and mathematical functions in source code on the disk, along with the Eagle Graphics system (a sort of advanced turtle graphics).

The MProlog manual is a joy to read and work with. Its nearly 600 pages are divided into reference sections for the language and development system, a discussion of the differences between generic MProlog and the ST implementation, an installation guide, and references to the utilities and Eagle Graphics additions. The descriptions of individual predicates are very thorough.

(Logicware also sells A Primer for Logic Programming by Richard J. Young, a 500-page book/software combination that guides you through most aspects of the Prolog language. Between the manual and the primer, that's 1,100 pages of documentation!)

There are some major drawbacks to MProlog, though. Aside from the Eagle Graphics and a few functions for menus and dialog boxes, there is no comprehensive interface to GEM and TOS calls. However, the development system is GEM-based and can't use a command-line interface-perhaps the worst of both worlds, making the transition between text editor and PDSS slow and cumbersome. And despite its 25 pages of index, the gigantic manual can be daunting at times. All in all, though, MProlog is a superb language system; its documentation is splendid, and its modularity and portability should help bring Prolog applications from other systems to the ST.

MProlog, with its online tutorial, Eagle Graphics, and extensive documentation, is for programmers working on professional research and development projects that require superior Prolog features and performance but not much use of GEM. MProlog also offers portability across a wide range of environments, including IBM mainframes, Unix and MS-DOS computers.


MProlog's
modularity lets you
break large programs
into distinct
modules.

A TASTE OF PROLOG
XPro and MProlog are well-designed, complete implementations of Prolog. But if you want to get a taste of this language that's causing so much excitement, you don't have to look beyond this issue's START Disk. There you'll find G-Prolog- the G is for Gene Weiss, who programmed the language for START. G is also for generic; G-Prolog isn't as fancy as the name brands, but it's cheap and it does the job.

To get your taste of Prolog, first copy the file PROLOG.ARC and the program ARCX.TTP to a fresh disk. Double-click on ARCX.TTP, and in the box that comes on the screen, type the filename PROLOG.ARC and press the Return key. The disk will spin for a minute or so, the program will print messages on the screen and when it returns to the Desk-top you'll find G-Prolog and a set of documentation and sample files on the disk.
Be sure you read the file GPROLOG.DOC before you try to use GPROLOG.PRG; you can read it by double-clicking on GPROLOG.DOC from the Desktop, then select either Show (to read it on the screen) or Print (to print the file on paper).

RESOURCES
If you're interested in Prolog code, information, and tutorials, there are several places to find them.

Among the best books on the language are The Art of Prolog: Advanced Programming Techniques by Leon Sterling and Ehud Shapiro; Prolog Programming for Artificial Intelligence by I. Bratko; Programming in Prolog by W.F. Clocksin and CS. Mellish; and Prolog for Programmers by F. Kluzniak and S. Szpakowicz, which includes the source code for a Prolog interpreter called Toy-Prolog. AI Expert is a monthly magazine that covers Prolog and other kinds of artificial intelligence programming.

CompuServe's AI Expert Forum (type GO AIE) has a Prolog library, a Turbo Prolog library, and all the programs published in AI Expert magazine. BIX (the Byte magazine Information Exchange) offers an AI conference, a separate Prolog conference and a section of its Listings library for Prolog items. The Usenet network of Unix computers has a comp.lang.prolog newsgroup; you can get access to Usenet through systems such as Portal and the Well (the Whole Earth 'Lectronic Link). Finally, anyone with access to ARPAnet, the Defense Department-sponsored computer network, can subscribe to the electronically mailed Prolog Digest.

Prolog's not to every programmer's taste-and it isn't perfect for every application. The language's special features can make program development a dream or a nightmare. But if logic programming is what you want, the ST is the way to join AI's next generation.

If you'd like to see more articles like this, circle 190 on the Reader Service Card.

Christopher F Chabris, who lives in Cambridge. MA, is the author of Artificial Intelligence and Turbo Pascal. Gene Weiss is a programmer who lives in Saint Paul, MN.
 

PRODUCT INFORMATION

  • XPro, $39.95. Rational Visions, 7111 W Indian School Road, Suite 131, Phoenix, AZ 85033, (602) 846-0371.
    CIRCLE 191 ON READER SERVICE CARD
  • MProlog, $199; Prolog: A Primer for Logic Programming by Richard J. Young, $49. Logicware, Inc., 5915 Airport Road, Suite 200, Mississauga, Ontario L4V 1T1 Canada, (416) 672-0300.
    CIRCLE 192 ON READER SERVICE CARD
  • The Art of Prolog: Advanced Programming Techniques, by Leon Sterling and Ehud Shapiro (MIT Press), $32.95.
    CIRCLE 193 ON READER SERVICE CARD
  • Prolog Programming for Artificial Intelligence, by I. Bratko (Addison-Wesley), $26.95.
    CIRCLE 194 ON READER SERVICE CARD
  • Programming in Prolog. by W.F. Clocksin and CS. Mellish (Springer-Verlag) $19.95.
    CIRCLE 195 ON READER SERVICE CARD
  • Prolog for Programmers, by F Kluzniak and S. Szpakowicz (Academic Press), $67.50 hardcover, S24.95 paperback.
    CIRCLE 196 ON READER SERVICE CARD
  • AI Expert, $27 per year (monthly). Miller Freeman Publishing, 500 Howard St., San Francisco, CA 94105, (800) 341-7378.
    CIRCLE 197 ON READER SERVICE CARD
  • CompuServe, 5000 Arlington Centre Boulevard, P.O. Box 20212, Columbus, OH 43220, (800) 848-8190.
    CIRCLE 198 ON READER SERVICE CARD
  • BIX (Byte Information Exchange), 1 Phoenix Mill Lane, Peterborough, NH 03458, (800) 227-2983 or (603) 924-7681.
    CIRCLE 199 ON READER SERVICE CARD
  • Portal, 19720 Auburn Drive, Cupertino, CA 95014, (408) 973-9111.
    CIRCLE 200 ON READER SERVICE CARD
  • The Well (Whole Earth 'Lectronic Link), 27 Gate Five Road, Sausalito, CA 94965, (415) 332-4335.
    CIRCLE 201 ON READER SERVICE CARD