Skip to content

Getting started

Kacper Sokol edited this page Jan 23, 2018 · 7 revisions

Finding your way around SWI-Prolog

Getting started

Log on to a (Linux) lab machine and start up SWI-Prolog with the command swipl. (The location of the most recent swipl binary is /lib64/swipl-7.4.2/bin/x86_64-linux/swipl but it's linked to /bin/swipl)

SWI-Prolog starts up as follows:

Welcome to SWI-Prolog (threaded, 64 bits, version 7.4.2)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit http://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?-

'?-' is the Prolog prompt at which you can type a query, ended by a period. Prolog programs should be stored in a file and then read into the Prolog interpreter, by means of the query ?-consult(file). (which can be abbreviated ?-[file].). The default extension of a SWI-Prolog file is .pl, so the query ?-consult(file). will either load file or file.pl. If your filename contains non-alphanumeric characters you should use single quotes (e.g. ?-consult('file.txt'). or ?-['file.txt'].).

Some other useful commands from pl

The following command terms can be typed at the pl prompt ?-.

?-consult(user). or ?-[user].

Type straight into the database. Type <Ctrl-d> to stop. This is only useful for trying things out quickly; consulting a file is the normal way to edit a Prolog program.

?-consult(mycode). or ?-[mycode]. or ?-consult('mycode.pl').

Load the Prolog program in mycode.pl into the Prolog database.

?-listing.

Display a listing of the current contents of the Prolog database.

?-make.

Look for changes in time stamps of the consulted Prolog program (or programs) and reconsult them if changes have occurred.

?-help(Topic).

Display manual at the specified topic. e.g. help(bagof).

?-apropos(Word).

Display manual and do a free text search for the word. e.g. apropos(append).

?-halt.

Close the SWI-Prolog shell - without confirmation!

pl shell can also be closed with <Ctrl-d>.

?-debug. and ?-nodebug.

See this page for details.

?-trace. and ?-notrace.

See this page for details.

Using swipl on your own computer

Important: Please note that your assignments must work on the lab machines even if you develop them on your own computer. SWI-Prolog is highly portable between platforms provided that you do not use platform-specific features (e.g. such as calling MS Windows API). None of the assignments require the use of platform-specific features.