Prolog for CS 480/580

Our goal for this quarter is to learn to use Prolog for knowledge representation and for making logical inferences. There are many different Prolog compilers and interpreters. We have SWI-Prolog running on prime. To start Prolog, enter:

pl

A Prolog program, consisting of facts and rules, should be stored in a program file, named with the extension .pl. To load your program into Prolog, enter:

[program_name].

Note that the .pl extension is assumed. You do not write it as part of the program name when you load your program into Prolog. If you get any WARNING messages from Prolog about syntax errors, you should correct them in your program file and then reload the file into Prolog. (Exception: Warning messages about the clauses of predicates not being together in the source file or about singleton variables can safely be ignored.)

To see what you have loaded into Prolog, enter:

listing.

You may get a partial listing for a particular predicate by entering:

listing(predicate_name).

Now you can deduce the consequences of your program by entering queries at the ?- prompt. If your query can produce more than one correct answer, enter ; (the semicolon) after each answer to see the next. When no more answers can be deduced, the interpreter will answer No .

The trace is a useful Prolog feature that lets you see how Prolog is processing your query. To trace a query, enter:

trace.

Then, enter the query. Hit the return key to see each line of the trace. When the trace is done, SWI-Prolog leaves you in debug mode (whether or not anything needs debugging). My recommendation is to enter:

nodebug.

to get back to the ?- prompt. While you are welcome to experiment with the debugger if you like, learning a full Prolog software development environment is beyond the scope of this course.

To exit Prolog, enter:

halt.

Online Reference

The SWI-Prolog Reference Manual is available online.