CSC2501 Fall 2000 - Prolog Basics | ||||||||||||
Links:
CSC485 / 2501 Home Page |
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.
alias pl 'u/nlu/bin/pl \!*' Once you start Prolog, you should see:
For help, use ?- help(Topic). or ?- apropos(Word). 1 ?- To quit Prolog, enter
| ?- [filename]. If you use an extension on the end of the filename, you must put
the name in single quotes, like this:
To load a file in a different directory from the one Prolog starts
in, simply include the directory in the single quotes:
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:
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
This is a special command to tell the interpreter to enter the
next lines you type into program memory. For example, like this:
To finish entering facts and rules, type control-D.
| ?- mother(elizabeth, philip). no This query was false.
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.
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:
The following lists all procedures called listadd with any arity:
The following lists all procedures called listadd with arity 1
or 2:
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:
CALL is printed when Prolog starts matching a goal with the clauses
in a 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. |