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 /usr/local/swiprolog-centos6/bin/).
It may be more convenient for you to set up the paths to the executable files and the manual pages.
If you use the bash shell, you can do this by adding the following lines to your .bashrc file.
export PATH=/usr/local/swiprolog/bin/:$PATH
SWI-Prolog starts up as follows:
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 6.0.0) Copyright (c) 1990-2011 University of Amsterdam, VU Amsterdam SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details. For 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').).
Once you have familiarised yourself with the basic SWI-Prolog pl command line shell, you can quickly progress by taking advantage of SWI's integrated development environment (IDE). These notes will give you a few pointers to help you get up to speed with SWI-Prolog and its accompanying libraries (known as packages), documentation and tools.
Integrated Development Environment (IDE)
SWI-Prolog comes with a closely integrated X11-based windowing package called XPCE. As well as being a useful programming resource in its own right, XPCE is also used extensively by SWI to implement a collection of development tools that loosely constitute an IDE. These tools can be accessed from the command line shell by typing the name of the tool (followed by "." of course). Some important ones to know are described below.
IDE commands from pl
?-help.
Opens up a local copy of the SWI manual in a new window.
- Click on the tree control to browse around the manual.
Important: The chapter on Built-in Predicates is where you will find documentation of standard Prolog terms. - Type in a term (e.g. "bagof") and click Help to jump to the manual page for that term.
- Type in a keyword (e.g. "bag") and click Search for a list of approximate matches.
- Type in a term (e.g. assuming you have consulted labs1.pl, type "knows") and click Explain for a report about the use of this term. e.g.
Explanation for "connected" "connected" is an atom user:connected/3 is a predicate defined in /home/web/HTML/Teaching/Resources/COMS30106/labs/labs1.pl:4 Referenced from 1-th clause of user:friendOfAFriend/3 Referenced from 2-th clause of user:friendOfAFriend/3 Referenced from 1-th clause of user:friend/2 Referenced from 2-th clause of user:friend/2 - Choose "Package documentation" from the Help menu to open up a local HTML page with links to local copies of the package documentation.
Important: Some of SWI-Prolog's state of the art features, like RDF database and Semantic Web support are amongst these packages. Some of the packages have example code, like the http package's example web server in just a few lines of Prolog. - Choose "XPCE (GUI) manual" from the Help menu to access the windowing manual.
?-emacs. or ?-emacs(Filename).
Opens an XPCE implementation of the famous emacs editor that has built-in colour syntax highlighting, compilation, make and debugging.
?-manpce.
Opens the XPCE windowing manual. A good place to start learning about XPCE is by browsing the example programs reachable via the File menu. A completely separate set of examples, code snippets, can also be reached from the Browser menu. Make sure you read the XPCE FAQ and don't confuse XPCE with standard Prolog.
?-guitracer.
Switches on the graphical source level debugger for subsequent trace and spy commands. (This was covered in Labs 1 but is included here for reference).
Some other useful commands from pl
The following command terms can be typed into the pl command line shell.
?-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.
?-listing.
Display a listing of the current contents of the Prolog database.
?-make.
Looks for changes in file dates of the consulted Prolog program (or programs) and reconsults them if changes have occurred.
?-help(Topic).
Opens manual at the specified topic. e.g. help(bagof).
?-apropos(Word).
Opens manual and does a free text search for the word. e.g. apropos(append).
?-halt.
Close the SWI-Prolog shell - without confirmation!
Using the IDE tools on your own computer
The lab machines are configured to use X11 and so SWI-Prolog's XPCE-based IDE tools are available by default. If you download and install SWI-Prolog on your own personal machine you will need to ensure that you correctly configure it for X11 in order to use the IDE tools. For Unix and Linux, this is usually the norm. For the Windows PC a suitable alternative graphical front-end is always available in SWI-Prolog. However, for Mac OS-X, you must start pl from the X11 shell, not the normal terminal window. On any *nix platform, if you do not have X11 available, you will see this error message when you try to use XPCE.
[PCE fatal: @display/display: Failed to connect to X-server at `': no DISPLAY environment variable ********************************************************************* * You MUST be running the X11 Windowing environment. If you are, * * check the setting of your DISPLAY environment variable as well * * the access rights to your X11 server. See xauth(1) and xhost(1). * *********************************************************************
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.

