And here are two example runs:
% ./eval 1+2 3 % ./eval foo ERROR: is/2: Arithmetic: `foo/0' is not a function
Prolog script may be launched for debugging or inspection purposes
using the -l or -t. For example,
-l merely loads the script, ignoring
swipl -l eval 1+1 <banner> ?- main. 2 true. ?-
We can also force the program to enter the interactive toplevel after
the application is completed using
swipl -t prolog eval 1+1 2 ?-
The Windows version simply ignores the
versions extracted command line arguments from the HashBang
line. As of version 5.9 all relevant setup can be achieved using directives.
Due to the compatibility issues around HashBang line
processing, we decided to remove it completely.
With the introduction of PrologScript (see section 126.96.36.199), using shell scripts as explained in this section has become redundant for most applications.
Especially on Unix systems and not-too-large applications, writing a
shell script that simply loads your application and calls the entry
point is often a good choice. A skeleton for the script is given below,
followed by the Prolog code to obtain the program arguments. See library
#!/bin/sh base=<absolute-path-to-source> SWIPL=swipl exec $SWIPL "$base/load.pl" -- "$@"
:- use_module(library(main)). :- initialization(main,main). main(Argv) :- argv_options(Argv, Positional, Options), go(Positional, Options). go(Positional, Options) :- ...
On Windows systems, similar behaviour can be achieved by creating a
shortcut to Prolog, passing the proper options or writing a
For larger programs, as well as for programs that are required to run on systems that do not have the SWI-Prolog development system installed, creating a saved state is the best solution. A saved state is created using qsave_program/[1,2] or the -c command line option. A saved state is a file containing machine-independent14The saved state does not depend on the CPU instruction set or endianness. Saved states for 32- and 64-bits are not compatible. Typically, saved states only run on the same version of Prolog on which they have been created. intermediate code in a format dedicated for fast loading. Optionally, the emulator may be integrated in the saved state, creating a single file, but machine-dependent, executable. This process is described in chapter 14.
This mechanism loads a series of Prolog source files and then creates a saved state as qsave_program/2 does. The command syntax is:
% swipl [option ...] [-o output] -c file.pl ...
For example, to create a stand-alone executable that starts by
executing main/0 and for which the source is loaded through
load.pl, use the command
% swipl --goal=main --stand_alone=true -o myprog -c load.pl
This performs exactly the same as executing
% swipl <banner> ?- [load]. ?- qsave_program(myprog, [ goal(main), stand_alone(true) ]). ?- halt.
As of version 9.1.18, SWI-Prolog allows starting an application using the command below.
swipl [option ...] [path:]name [arg ...]
This command line first processes Prolog options described in
section 2.4. Note that
most standard Prolog commandline options are not relevant. The -f
none, which implies that the user init file is
by default not loaded. If an application wishes to load the user init
file, it should load
user_app_config(init) if this file exists (see
Next, it locates
path(name) using SWI-Prolog's file
search mechanism defined by absolute_file_name/3.
After loading this file it demands a to find the entry point registered
as shown in section 2.11.
By default, the application terminates after the entry point terminates.
The entry point may enable the interactive Prolog REPL loop by calling
All command line options after
accessible in the Prolog flag argv.
The optional path defaults to
default, apps are searched in the directories below. See file_search_path/2
appdirectory of the SWI-Prolog installation
- User and site configuration. On POSIX systems using the XDG file
name conventions, this is normally
appdirectory of a Prolog pack.
The following apps are provided by the installation
- Print information on installed apps. For example, to list all available
swipl app list
- Command line driven management of Prolog packs. This is a front-end to
the Prolog library
library(prolog_pack). For example, to find packages related to type, use the command below.
swipl pack find type