CSC2501 Fall 2000 - Prolog Basics 
Links:
CSC485 / 2501 Home Page

 Introduction to Prolog Programming

  This document provides a few useful functions to get you started in Prolog. These are the things the text books tend to bury somewhere, when all you really want is to get started. For a fuller introduction to Prolog, see Introduction to Prolog Programming.
 
 
Starting and Stopping Prolog Top of screen
After logging into cdf, you can invoke Prolog by typing "pl" at the Unix prompt. The pl program is stored in the directory /u/nlu/bin/pl, which is not in the default path. So instead of typing just 'pl' you can type either the whole string, or you can make an alias for 'pl' in your .cshrc file:
   alias pl 'u/nlu/bin/pl \!*'

 Once you start Prolog, you should see:
Welcome to SWI-Prolog (Version 2.7.14)
Copyright (c) 1993-1996 University of Amsterdam. All rights reserved.

 For help, use ?- help(Topic). or ?- apropos(Word).

 1 ?- 

To quit Prolog, enter 
| ?- halt.
at the Prolog prompt. Don't forget the full stop at the end! If you don't put a full stop after a query or command at the command line, Prolog won't know you've finished typing.
 
 
To Load a File Top of screen
At the Prolog prompt, type:
| ?- [filename].

 If you use an extension on the end of the filename, you must put the name in single quotes, like this:
| ?- ['filename.pl'].

 To load a file in a different directory from the one Prolog starts in, simply include the directory in the single quotes:
| ?- ['lexicon/filename.pl'].

 It's very useful to create a single file which you load at the prompt, and which in turn loads all the other files you will want. Such a file is included with the Prolog files that start you off in your project. Its format might go like this:
:- ['gulp3mod.pl',
   'restrict.pl',
   'sentence.pl',
   'input.pl',
   'classes.pl',
   'parser_gulp.pl',
   'lexicon_gulp.pl'].

You can also enter commands directly into the Prolog interpreter, instead of writing them to a file and then loading the file. To do this, enter 
| ?- [user].

 This is a special command to tell the interpreter to enter the next lines you type into program memory. For example, like this:
| ?- [user].
| mother(elizabeth, charles).
| mother(elizabeth, andrew).
| mother(elizabeth, edward).
| brother(X, Y) :- mother(Mom, X), mother(Mom, Y).
| [^D] user consulted 260 bytes 0.0166667 sec.

To finish entering facts and rules, type control-D.
 
 
Performing a query Top of screen
At the prompt, simply enter a fact with zero or more variables. Prolog will return variable bindings, if any, and whether or not the query succeeded. Using the example under the "To Load a File" section:
| ?- mother(elizabeth, philip).

 no

This query was false.
| ?- mother(elizabeth, Son).

 Son = charles ;

 Son = andrew ;

 Son = edward ;

 no

In these cases, the semicolon is typed by the user. It tells Prolog to try to find more bindings. The "no" at the end is a response to the last semicolon. The semicolon asked for more solutions, Prolog found no more, so the request failed.

 Try pressing 'h' after a binding is returned. You will see some more debugging options. 
List What's in Memory Top of screen
To ask the Prolog environment to list all the rules and facts it currently has loaded, use the predicate:
listing.

 The listing shown may not be identical to the procedures in the program files when they were loaded because you can change the program file in memory with assert and retract predicates. The following example shows how to use listing with an argument. This example lists all procedures called listadd with an arity of 2:
listing(listadd/2).

 The following lists all procedures called listadd with any arity:
listing(listadd).

 The following lists all procedures called listadd with arity 1 or 2:
listing([listadd/1, listadd/2]).
 
 
Debugging Tools Top of screen
Spy allows you to watch exactly how Prolog works to satisfy a goal in your program. You can add a spypoint to a predicate by specifying the predicate's name and arity, like this:
spy(append/3).

 This makes Prolog print out a Spy message whenever a the predicate member/2 is called, succeeds, fails, or when it is reached by backtracking. Its tracking looks like this:
| ?- append([xy, yz], [a, b], Result).
(1) 0 CALL append([xy, yz], [a, b], _1)
(2) 1 CALL append([yz], [a, b], _6)
(3) 2 CALL append([], [a, b], _11)
(3) 2 EXIT append([], [a, b], [a, b])
(2) 1 EXIT append([yz], [a, b], [yz, a, b])
(1) 0 EXIT append([xy, yz], [a, b], [xy, yz, a, b])
Result = [xy, yz, a, b] 

CALL is printed when Prolog starts matching a goal with the clauses in a procedure.
EXIT is printed when a goal succeeds. For fact clauses this happens immediately, whle for rule clauses this onl happens after all the goals in the body have succeeded.
REDO is printed when deep backtracking reaches a procedure. Shallow backtracking is not reported, because it happens within a procedure.
FAIL is printed when there are no more clauses with which to match the goal. This occuurs when the current goal has come to the bottom of its procedure.

 The predicate nospy(X) turns off a spypoint.

 The predicate trace and its complementary predicate notrace sets Prolog into a debugging mode where it prints spy messages for all predicates.