> > > > # SWI-Prolog for MS-Windows

> > > > Jan Wielemaker
> > > > HCS (formerly SWI),
> > > > University of Amsterdam
> > > > The Netherlands
> > > > E-mail: jan@swi-prolog.org

### Abstract

This document gets you started using SWI-Prolog on MS-Windows. It also describes the components and issues that are specific to MS-Windows. It 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 Using SWI-Prolog
  - 1.1 Starting Prolog and loading a program
  - 1.2 Executing a query
  - 1.3 Menu commands
  - 1.4 Editing Prolog programs
  - 1.5 Some useful commands
- 2 Using SWI-Prolog with C/C++
  - 2.1 Using DevStudio
  - 2.2 Using plld.exe
- 3 The installation
  - 3.1 Supported Windows versions
  - 3.2 Choosing the file-extension
  - 3.3 Installed programs
  - 3.4 Installed Registry keys and menus
- 4 The SWI-Prolog community and foundation
  - 4.1 Web-site and mailinglists
  - 4.2 About license conditions
  - 4.3 Supporting SWI-Prolog

## 1 Using SWI-Prolog

### 1.1 Starting Prolog and loading a program

The SWI-Prolog executable plwin.exe can be started from the StartMenu or by opening a `.pl` file holding Prolog program text from the Windows explorer. (1) The installation folder (by default `C:\Program Files\pl`) contains a subfolder `demo` with the file `likes.pl`. This file can be opened in Prolog from the StartMenu, by opening `likes.pl` in the Windows explorer or using the following command in the Prolog application. Be sure to get the quotes right and terminate the command with a full-stop (``.``).

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

### 1.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 (;) (2) 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
    ?-

### 1.3 Menu commands

The SWI-Prolog console provided by plwin.exe has a menu for accessing the most commonly used commands. We assume not all menu entries need to be explained in details. We make some exceptions:

File/Reload modified files
  This menu reloads all loaded source-files that have been modified using the make/0 command described in section ??.

File/Navigator ...
  Opens an explorer-like view on Prolog files and the predicates they contain.

Settings/Font ...
  Allows for changing the font of the console. On some installation the default font gives redraw and cursor dislocation problems. In this case you may wish to select an alternative. Some built-in commands assume non-proportional fonts.

Settings/User init file ...
  Edits the user personalisation file. If no such file exists it first installes a default file as `pl.ini` that contains commonly used settings in comments.

Settings/Stack sizes ...
  Allows to define the maximum size to which the various Prolog stacks are allowed to grow. The system defaults are chosen to make errornous programs fail quickly on modest hardware. Programs with large data structures or many choicepoints often need larger stacks. Note that an active Prolog process growing over the size of the physical memory of your computer can make the system extremely slow.

Run/Interrupt
  Try to interrupt the running Prolog process. This is the same as using Control-C. Sometimes interrupts are not honoured or take very long to process. Closing the window twice provides a way to force Prolog to stop.

Run/New thread
  Creates a new interactor window running in a separate thread of execution. This may be used to inspect the database or program while the main task continues.

Debug/Edit spy points ...
  Edit break-points on predicates. From the PceEmacs editor (see section 1.4) break-points can also be set on specific calls from specific clauses.

Debug/Graphical debugger ...
  Use the source-level debugger on the next spy- or break-point or other call that enables the debugger.

Help
  The help menu provides various starting point to related documents. Items flagged with (on www) open your default internet browser on a page of the SWI-Prolog website.

### 1.4 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 (see section 1.3) and follow the instructions in the comments.

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.

### 1.5 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. On Windows folders may be specified with the DOS/Windows ``\``, which must be escaped or using the POSIX standard ``/``. Especially when used in source-code ``/`` is to be preferred as it is portable. A Prolog list ([ ... ]) can be used to abbreviate the consult command. The file-extension (`.pl` as well as the selected alternative) can be omitted. Here are some examples:

  `?- consult(likes).` | Load `likes.pl` from the current folder (see pwd/0).
  `?- ['C:/Program Files/pl/demo/likes']` | Load `likes.pl` using absolute path.
  `?- ['C:`\``\`Program Files`\``\`pl`\``\`demo`\``\`likes']` | Same using Windows-style path-name

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 toplevel, 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.

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

Using MSVC or a compiler with compatible calling format you can write C or C++ code that can be loaded into SWI-Prolog and called as a predicate. You can also embed SWI-Prolog in C/C++ applications.

Details on how to interact with Prolog are in the SWI-Prolog reference manual. The mailing list archives and TWiki web provide problems and solutions to the many problems that may occur. Documentation of the `SWI-cpp.h` C++ include file is available from the package documentation. This section only discusses some Windows specific issues.

### 2.1 Using DevStudio

First of all, add the `include` folder of the installation to the search-path for headers and the `lib` folder to the search-path for libraries. Both DLLs (extensions) or embedded executables should link to `libpl.lib` and, if appropriate, to `msvcrt.lib`, the multi-threaded DLL version of the MSVC runtime library.

To create extensions, simply use the Win32 DLL project template. To embed Prolog, care should be taken that Prolog can find the Prolog installation. For development, the simplest way to ensure this is by adding the installation `bin` folder to the `%PATH%` environment and call PL_initialise() as illustrated below. PL_initialise() uses the path of the loaded `libpl.dll` module to find the Prolog installation folder. (3)

      { static char *av[] = { "libpl.dll", NULL };

        if ( !PL_initialise(1, av) )
        { <error>
        }
      }

To create an executable that does not rely on Prolog one must create a saved-state of the required Prolog code and attach this to the executable. Creating saved-states is described with qsave_program/2 in the reference manual. This can be attached to a state using the DOS command below to create `final.exe` from the executable produced by MSVC and the generated saved-state.

    > copy /b file.exe+file.state final.exe

### 2.2 Using plld.exe

The plld.exe automates most of the above complications and provides compatibility for common tasks on many platforms supported by SWI-Prolog. To use it with MSVC, set the PATH, INCLUDE and LIB environment to find the DevStudio tools, headers and libraries as well as the Prolog ones described above. Now, an extension `myext.dll` is created from the source `myext.c` using

    > plld -shared -o myext myext.c

An embedded executable is created from C, C++ and Prolog files using

    > plld -o myexe file.c ... file.pl ...

## 3 The installation

### 3.1 Supported Windows versions

SWI-Prolog 5.5.x and later are UNICODE applications, requiring Windows NT or later (2000, XP). Although Microsoft provides a library that allows such program to run under Windows 95/98/ME, this library is licenced such that it cannot be used with Open Source software. Users of these old operating systems are forced to stick with version 5.4.7.

### 3.2 Choosing the file-extension

By default, Prolog uses the `.pl` extension to indicate Prolog source-files. Unfortunately this extension conflicts with the Perl language. If you want to use both on the same Windows machine SWI-Prolog allows you to choose a different extension during the installation. The extension `.pro` is a commonly used alternative. If portability is an issue, it is advised to use the alternative extension only for the load file, the source-file that loads the entire program and use the normal `.pl` extension for libraries and files loaded from other files.

### 3.3 Installed programs

The following tools and programs are installed:

Programs
`bin\plwin.exe` | Default Windows application for interactive use.
`bin\plcon.exe` | Console-based version for scripting purposes.
Utilities
`bin\plld.exe` | Linker front-end to make single-file mixed Prolog/C/C++ executables.
`bin\plrc.exe` | Manipulate Prolog resource files.
Important directories
`bin` | Executables and DLL files
`library` | Prolog library
`boot` | Sources for system predicates
`include` | C/C++ header files for embedding or to create extensions
`xpce` | XPCE graphics system
`xpce\prolog\lib` | XPCE/Prolog library
DLLs and other supporting files
`boot32.prc` | Prolog resource file containing system predicates
`\bin\libpl.dll` | The Prolog kernel
`\bin\plterm.dll` | The window for plwin.exe
`\bin\pthreadVC.dll` | POSIX thread runtime library
`\bin\msvcrt.dll` | Microsoft C runtime library
Extension DLLs (plugins)
`\bin\xpce2pl.dll` | The XPCE graphics system
`\bin\socket.dll` | Prolog socket interface
`\bin\time.dll` | Timing and alarm library
`\bin\cgi.dll` | Gather CGI GET and POST arguments
`\bin\odbc4pl.dll` | ODBC interface
`\bin\sgml2pl.dll` | SGML/XML parser
`\bin\table.dll` | Access structured files as tables

### 3.4 Installed Registry keys and menus

The filetype `.pl` or chosen alternative (see section 3.2) is associated to plwin.exe. A chosen folder (default SWI-Prolog) is added to the start-menu holding shortcuts to Prolog and some related utilities. The following registry keys are in use:

`HKEY_LOCAL_MACHINE`\`Software`\`SWI`\`Prolog`
`fileExtension` | Extension used for Prolog files
`group` | Start-menu group
`cwd` | Default folder for shortcut
`home` | Installation directory
`HKEY_CURRENT_USER`\`Software`\`SWI`\`Prolog`
`localSize` | Default local stack size (KB)
`globalSize` | Default global stack size (KB)
`trailSize` | Default trail stack size (KB)
`argumentSize` | Default argument stack size (KB)
`HKEY_CURRENT_USER`\`Software`\`SWI`\`Plwin`\`Console`
Note: thread-windows store the same info in sub-keys
`Height` | Height of window in character units
`Width` | Width of window in character units
`X` | Left-edge of window in pixel units
`Y` | Top-edge of window in pixel units
`SaveLines` | Number of lines available for scrollback

## 4 The SWI-Prolog community and foundation

### 4.1 Web-site and mailinglists

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 mailinglist and allows to (de)register the mailinglist. Finally, it incorporates Bugzilla, the Mozilla project bugtracking system, for reporting bugs and tracking issues.

### 4.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.

### 4.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.

# Footnotes

note-1
  The .PL file extension can be changed during installation. See section 3.2.
note-2
  On most installations, single-character commands are executed without waiting for the RETURN key.
note-3
  When the C++ interface from `SWI-cpp.h`, these comments apply to the arguments for PlEngine().

# Index

A
apropos/1
C
consult/1
E
edit/0
edit/1
  1.4
G
gtrace/0
H
help/1
  1.5
L
ls/0
M
make/0
  1.3 1.4
P
pwd/0
  1.5
Q
qsave_program/2
  2.1
T
trace/0