PROLOG on the Suns

PROLOG on the Suns

Complete information on our implementation of PROLOG, C-Prolog, can be found in the C-Prolog manual in the Sun lab. What follows is a very brief description of how to run programs.

To enter the prolog system, type:

        prolog
The system will print a message and come back to you with:
        ?-
To get out type: "halt." or ^D

To read in (consult) a file, type to the ?- prompt:

        ?- [filename1,filename2,...].
If a filename contains special characters (e.g., ".", "/", etc.) surround the whole name with single quotes (e.g., ['sort.p']).

If you make an error in a program, go back and fix it in your text editor and then "reconsult" the file. You reconsult by typing to the ?- prompt:

        ?- [-filename].
Notice that the only syntactic difference between consulting and reconsulting is the presence of the "-" before the file name. The difference semantically is that if you "consult" a file, all of its clauses are added to those already there (nothing is thrown away), while if you "reconsult" it adds all of the new clauses while throwing away all of those previously there with the same predicate. If you have an erroneous clause, fix the problem and then "consult" again, the erroneous clause will remain in the system (and in fact it will be found before your correction). As a result, always "reconsult" when fixing errors!

(Note that the system will always prompt you with "?-". Note also that you must terminate all commands with a ".".)

If you should find yourself in "break" (via an interrupt - usually by some sort of an error), you can exit by typing ^D (this brings you up one level in "break"). If you want to generate your own "break" (e.g., because your program is in an infinite loop), type ^C. If you are in "break" and the system wants an option, type one of:


        ?- listing(name).
will list all statements whose left hand side is "name". Unfortunately all variable names will be replaced by tokens of the form _nnn, where nnn is a number. This will make the output much less readable. However it can sometimes be helpful if you can't figure out what your program is doing.

It is possible to selectively trace the execution of your program by executing the following commands:

        ?- spy atom   or    ?- spy [atom1, atom2, ...].
This command tells the system to report to you whenever any instruction with any of these atoms on the left side is executed (these are called "spypoints").
        nospy atom  /* removes "spypoint" on atom */
        nodebug  /* removes all spypoints */
        trace  /* traces execution of program */
        notrace  /* turns off tracing */
The major error made by novices to PROLOG is to forget that PROLOG involves relational rather than functional programming. Thus you must write
        ?- sort (List, Sorted).
rather then just writing
        ?- sort(List).
and expect an answer to come popping out. Because PROLOG allows you to overload names, you can make this mistake and not even notice it. It presumes that if you write sort with one parameter then that is a different predicate than sort with two parameters.

The other common error is to neglect to terminate a command (query) by a period. If you leave this off, nothing will happen. To initiate execution just type a period on the next line.

The other thing to notice is that when you enter a query it will respond with a rather complex answer. If the query has any variables in it, it will see if it can find any sets of values which will make it true. If so, it will give you one set of values for the variables which will make it true. If you respond by typing ";" followed by a return it will try to find more values which make it correct. If you simply respond by hitting carriage return, it will tell you whether it succeeded at finding an anwer or not by giving you a yes or no answer. If there are no variables in the query, it will simply respond with a yes or no. Be careful here, it you get an unexpected "no" answer it may be that you simply mistyped the query.

The following sources are recommended for more information on PROLOG: