There are various options if you want to make your program ready for real usage. The best choice depends on whether the program is to be used only on machines holding the SWI-Prolog development system, the size of the program, and the operating system (Unix vs. Windows).
A Prolog source file can be used directly as a Unix program using the
#! magic start. The same mechanism is useful for
specifying additional parameters for running a Prolog file on Windows.
#! magic is allowed because if the first letter of a Prolog
#, the first line is treated as a comment.11The
can be the legal start of a normal Prolog clause. In the unlikely case
this is required, leave the first line blank or add a header comment.
To create a Prolog script, make the first line start like this:
Prolog recognises this starting sequence and causes the interpreter to receive the following argument list:
Instead of -s, the user may use -f to stop Prolog from looking for a personal initialisation file.
Here is a simple script doing expression evaluation:
#!/usr/bin/swipl -q -t main -f eval :- current_prolog_flag(argv, Argv), append(_, [--|Args], Argv), concat_atom(Args, ' ', SingleArg), term_to_atom(Term, SingleArg), Val is Term, format('~w~n', [Val]). main :- catch(eval, E, (print_message(error, E), fail)), halt. main :- halt(1).
And here are two example runs:
% eval 1+2 3 % eval foo ERROR: Arithmetic: `foo/0' is not a function %
The Windows version supports the
too, but here it serves a rather different role. The Windows shell
already allows the user to start Prolog source files directly through
the Windows file-type association. However, Windows makes it rather
complicated to provide additional parameters for opening an individual
Prolog file. If the file starts with
#!, the first line is
analysed to obtain additional command line arguments. The example below
runs the system in `quiet' mode.
#!/usr/bin/swipl -q -s ....
Note the use of
/usr/bin/swipl, which specifies the
interpreter. This argument is ignored in the Windows version, but must
be present to ensure best cross-platform compatibility.
With the introduction of PrologScript (see section 22.214.171.124), 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.
#!/bin/sh base=<absolute-path-to-source> PL=swipl exec $PL -q -f '$base/load -t go -- **
go :- current_prolog_flag(argv, Arguments), append(_SytemArgs, [--|Args], Arguments), !, go(Args). go(Args) :- ...
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-independent12The 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 11.
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.