SWI-Prolog for MacOS X
Jan Wielemaker
Paulo Moura
Abstract
This document briefly explains the MacOS X specific issues for SWI-Prolog. This is by no means a manual or Prolog tutorial. The reference manual is available online or can be downloaded in HTML and PDF format from the SWI-Prolog website, which also provides links to books, online tutorials and other Prolog related material.

# Table of Contents

## 1 Introduction -- MacPorts

SWI-Prolog is by origin an Unix application, and not a native Macintosh application. It has been brought to the Mac using the MacPorts project, using XQuartz (Macintosh X11) for its graphical capabilities.

## 2 Installation

XQuartz should be installed prior to SWI-Prolog installation. The installer can be downloaded from the XQuartz web site.

### 2.1 Using MacPorts

Users of the MacPorts system can install the system just like any port using the command below. In addition to the port named `swi-prolog` providing the stable version, there is a port called `swi-prolog-devel` providing the development version.

    % sudo port -v selfupdate       # make sure we have the latest portfiles
    % sudo port install swi-prolog

The swi-prolog port depends on `readline`, `ncurses`, `ncursesw`, `gmp` `libmcrypt`, `zlib`, `expat`, and `jpeg`.

### 2.2 From the installer

Opening and installing the meta installer installs the required ports from the MacPorts system. The programs are installed in the directory `/opt/local/bin`. The main executable is named `swipl`.

## 3 Running SWI-Prolog

Not being a Macintosh Application, SWI-Prolog must be started from a terminal window. This can either be an X11 xterm or Terminal.app from Utilities. For comfortable usage it is necessary to setup some environment variables. The procedure depends on your shell of choice. On Tiger the default is bash. Add the following lines to the file `~/.bashrc` (or create this file if it does not yet exist).

    # This allows using Prolog graphics if you use Terminal.app
    if [ -z "$DISPLAY" ]; then export DISPLAY=:0; fi

    # This sets up the path
    PATH=$PATH:/opt/local/bin

Now start X11.App and configure it to autostart at login. Open Terminal.app or xterm and type

    % swipl
    Welcome to SWI-Prolog (Multi-threaded, Version 5.6.0)
    Copyright (c) 1990-2006 University of 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).

    1 ?-

If X11 is properly configured, the help system of the graphics subsystem XPCE can now be started using the command below.

    ?- manpce.

### 3.1 Loading a program

Prolog source files can be loaded by specifying their filename between `[]`. In addition to a plain filename, files may be searched on a named search-path1See file_search_path/2 in the manual for details. using the notation `SearchPath(File)`. Two defined paths are `library` for the Prolog library and `swi` for the Prolog installation directory. Below we load the file `likes.pl` from the `demo` directory in the installation directory, Be sure to get the quotes right and terminate the command with a full-stop (``.``).

    ?- [swi('demo/likes')].

### 3.2 Executing a query

After loading a program, one can ask Prolog queries about the program. The query below asks Prolog what food `sam' likes. The system responds with `X = <value>` if it can prove the goal for a certain X. The user can type the semi-colon (;)2On most installations, single-character commands are executed without waiting for the RETURN key. if (s)he wants another solution, or RETURN if (s)he is satisfied, after which Prolog will say Yes. If Prolog answers No, it indicates it cannot find any (more) answers to the query. Finally, Prolog can answer using an error message to indicate the query or program contains an error.

    ?- likes(sam, X).

    X = dahl ;

    X = tandoori ;

    ...

    X = chips ;

    No
    ?-

### 3.3 Editing Prolog programs

There are three options for editing. One is to run an editor of choice in a separate window and use the below described make/0 command to reload modified files. In addition to this option Prolog can be used to locate predicates, modules and loaded files by specifying the editor of choice for use with the edit/1 command described below. This is achieved by editing the personalisation file `/.plrc`. A commented template is in the directory `dotfiles` of the SWI-Prolog installation directory.

Finally, you may wish to use the built-in editor called PceEmacs. This editor provides colourisation support based on real-time parsing and cross-reference analysis of the program. It is started using the command `?- emacs.` or can be set as default editor in the personalisation file.

### 3.4 Some useful commands

This section provides a very brief overview of important or commonly used SWI-Prolog predicates to control the environment.

consult(+File)
  Load a source-file. A Prolog list ([ ... ]) can be used to abbreviate the consult command. The file-extension (`.pl` can be omitted. Here are some examples:

  `?- consult(likes).` | Load `likes.pl` from the current folder (see pwd/0).
  `?- ['/opt/local/lib/swipl-5.6.0/demo/likes']` | Load `likes.pl` using absolute path.

pwd
  Print working directory (folder).
ls
  List files in current directory.
edit
  If Prolog is started by opening a `.pl` file in the explorer, edit this file. Also available from the menu.
edit(+Spec)
  Edit file, predicate, module, etc. with the given name. If multiple items are named Spec it prompts for the desired alternative.
make
  Reload all files that have been changed since they where last loaded. Normally used after editing one or more files.
trace
  Start the interactive debugger. There are three ways to use this. Entered as a single goal at the top-level, the next query will be traced. Alternatively it can be used in conjunction with the goal to be debugged: `?- trace, run.` and finally you can include it in your program to start tracing at a particular point or under a particular condition:

              ...,
              (var(X) -> trace ; true),
              ...,

gtrace
  Same as trace, but forces the use of the graphical (source-level) debugger.
apropos(+Keyword)
  Search for all predicates that contain Keyword in their name or short description. If a GUI environment is available the results are hyperlinks. Otherwise use help/1 to get details on selected hits.
help(+Spec)
  Give help on Spec, which is normally the name of a predicate or C interface function.

## 4 Using SWI-Prolog with C/C++

To use SWI-Prolog with C or C++ code you must install Apples Xcode environment, providing gcc. If all paths are properly installed, programs can be linked using the plld command described in the manual.

## 5 Known problems

- XPCE and the meta-key
  By default, the `Meta' key is bound to X11 modifier 1 which is set by the `Alt' key. This doesn't work very well for the Mac as the Alt key is commonly used to create alternative characters, while the Command key, which is used for composing commands, is bound to the X11 modifier 2. For the Apple we therefore bind the XPCE meta modifier to the X11 modifier 2. Wherever the documentation mentions Alt-X or Meta-X this must be read as Command-X.

- Threaded XPCE programs
  XPCE normally initializes Xlib for multi-threaded access using XInitThreads(). It turns out the MacOS X X11 version (tested on Tiger) causes a deadlock on certain keys. We now do not call XInitThreads(). As Xlib access is synchronised using the currently global XPCE lock and XPCE/Prolog interfacing is designed to have only a single Prolog thread talking to XPCE this should not be a serious problem.

- XPCE programs do not open
  A problem has been reported where starting XPCE programs, including the IDE components such as help, PceEmacs, etc. causes the program to hang. In the end, it turned out the fontconfig/freetype library used by XPCE to render Unicode fonts builds a database on first invocation. This process can take several minutes. Ones created, XPCE applications start quickly.

- Font display issues
  If fonts do not display correctly (e.g. manpce pages not showing bold styles or editor text in `screen' font looking mis-spaced and misaligned) you may try to rebuild the X11 font cache by using the command sudo /usr/x11r6/bin/ fc-cache.

## 6 The SWI-Prolog community and foundation

### 6.1 Web-site and mailing lists

The SWI-Prolog web-site is located at http://www.swi-prolog.org/. Part of the web is powered by TWiki, a wiki web that can be edited by any registered user and used to exchange ideas, problems and solutions. The web also provides access to the archive of the mailing list and allows to (de)register the mailing list. Finally, it incorporates Bugzilla, the Mozilla project bugtracking system, for reporting bugs and tracking issues.

### 6.2 About license conditions

The SWI-Prolog license allows it to be used in a wide variety of environments, including closed-source commercial applications. In practice, redistribution and embedding is allowed, as long as modifications to the SWI-Prolog source are published following the Free Software rules.

The SWI-Prolog kernel and foreign libraries are licensed under the Lesser General Public License (LGPL). The Prolog files are licensed under the normal General Public License GPL with an additional statement that allows for embedding in proprietary software:

> As a special exception, if you link this library with other files, compiled with a Free Software compiler, to produce an executable, this library does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License.

This exception is a proven construct used for libgcc, the GNU C-compiler runtime library.

### 6.3 Supporting SWI-Prolog

There are several ways to support SWI-Prolog:

- Extend the system with contributions
- Improve the system by submitting bug reports and patches.
- Link to http://www.swi-prolog.org and refer to SWI-Prolog in publications.
- Ask for commercial development or support at http://www.cognitivetools.com/
- Support development financially at http://www.swi-prolog.org/donate.html.

# Index

A
apropos/1
C
consult/1
E
edit/0
edit/1
  3.3
F
file_search_path/2
  3.1
G
gtrace/0
H
help/1
  3.4
L
ls/0
M
make/0
  3.3
P
pwd/0
  3.4
T
trace/0