5 Prolog Initialization and Cleanup

The embedded SWI-Prolog runtime must be initialized before it can start executing queries. Normally, Sweep takes care of initializing Prolog for you the first time you use a command that requires running some Prolog code. This section elaborates about Prolog initialization and its customization options in Sweep:

User Option: sweeprolog-init-args

List of strings used as initialization arguments for Prolog. Sweep uses these as the args argument of sweeprolog-initialize when it initializes Prolog on-demand.

Sweep loads and initializes Prolog on-demand at the first invocation of a command that requires the embedded Prolog. The user option sweeprolog-init-args says which arguments to pass to Prolog initialization. Its value is a list of strings that you can extend if you want to pass specific command line flags SWI-Prolog. For example, to limit the embedded Prolog stack to 512 MB, add the following to your Emacs configuration:

(with-eval-after-load 'sweeprolog
  (push "--stack-limit=512m" sweeprolog-init-args))

Sweep initializes Prolog from Elisp by calling function sweeprolog-initialize.

Function: sweeprolog-initialize prog &rest args

Initialize the embedded Prolog runtime. prog should be the path to the swipl executable, and args should be a list of command line arguments for swipl. Sweep initializes Prolog as if it was started from the command line as prog args.

The function sweeprolog-initialize takes one or more string arguments and initializes the embedded Prolog as if it were invoked externally in a command line with the given strings as command line arguments, where the first argument to sweeprolog-initialize corresponds to argv[0]. This function is implemented in C in sweep-module (see High-level Architecture).

The default value of sweeprolog-init-args is set to load the Prolog helper library sweep.pl and to create a boolean Prolog flag called sweep with value true. You can check for this flag in Prolog code to detect at runtime that you’re running under Sweep.

It is also possible to specify initialization arguments to SWI-Prolog by passing them as command line arguments to Emacs, which can be convenient when using Emacs and Sweep as an alternative for the common shell-based interaction with SWI-Prolog. This is achieved by adding the flag --swipl-args followed by any number of arguments intended for SWI-Prolog, with a single semicolon (;) argument marking the end of the SWI-Prolog arguments, after which further arguments are processed by Emacs as usual (see (emacs)Emacs Invocation for more information about Emacs’s command line options), for example:

emacs --some-emacs-option --swipl-args -l foobar.pl \; --more-emacs-options

In order for Sweep to be able to handle Emacs’s command line arguments, you must call sweeprolog-handle-command-line-args before Emacs processes the --swipl-args argument.

Function: sweeprolog-handle-command-line-args

Enable support for the Sweep-specific --swipl-args Emacs command line flag. This flag can be used to specify additional Prolog initialization arguments for Sweep to use when initializing Prolog on-demand, directly from Emacs’s command line invocation.

This function makes Emacs recognize the --swipl-args command line flag by adding a dedicated handler function to command-line-functions (see (elisp)Command-Line Arguments). If you want to use --swipl-args, you should arrange for command-line-functions to run before Emacs processes --swipl-args. To do that, either place a call sweeprolog-handle-command-line-args in your Emacs configuration, or call it from the command line right before --swipl-args:

emacs -f sweeprolog-handle-command-line-args --swipl-args -l foobar.pl \;

You can shut down or restart the embedded Prolog runtime using the following commands:

Command: sweeprolog-shutdown

Shut down the embedded Prolog runtime.

Command: sweeprolog-restart

Restart the embedded Prolog runtime.

The command sweeprolog-shutdown shuts down the Prolog runtime and frees up resources Prolog allocated. You cannot shut down Prolog with running top-levels (see The Prolog Top-level)—if you invoke sweeprolog-shutdown while you have running top-levels, this command suggests killing them, and if you refuse it complains and keeps Prolog running. The command sweeprolog-restart is similar to sweeprolog-shutdown, expect it starts the embedded Prolog runtime anew after shutting it down. When you invoke sweeprolog-restart with a prefix argument (C-u M-x sweeprolog-restart RET), this command prompts for additional initialization arguments to pass to the embedded Prolog runtime when restarting it.