\chapter{Overview} \label{sec:overview} \section{Getting started quickly} \label{sec:quickstart} \subsection{Starting SWI-Prolog} \label{sec:starting} \subsubsection{Starting SWI-Prolog on Unix} \label{sec:startunix} By default, SWI-Prolog is installed as `swipl'. The command line arguments of SWI-Prolog itself and its utility programs are documented using standard Unix \program{man} pages. SWI-Prolog is normally operated as an interactive application simply by starting the program: \begin{code} $ swipl Welcome to SWI-Prolog ... ... 1 ?- \end{code} After starting Prolog, one normally loads a program into it using consult/1, which may be abbreviated by putting the name of the program file between square brackets. The following goal loads the file \href{https://raw.githubusercontent.com/SWI-Prolog/swipl-devel/master/demo/likes.pl}{likes.pl} containing clauses for the predicates \nopredref{likes}{2}: \begin{code} ?- [likes]. true. ?- \end{code} Alternatively, the source file may be given as command line arguments: \begin{code} $ swipl likes.pl Welcome to SWI-Prolog ... ... 1 ?- \end{code} \begin{quote} Both the above assume \file{likes.pl} is in your \jargon{working directory}. If you use the command line version \program{swipl} the working directory is the same as the shell from which you started SWI-Prolog. If you started the GUI version (\program{swipl-win}) this depends largely on the OS. You can use pwd/0 and cd/0 to find and change the working directory. The utility ls/0 lists the contents of the working directory. \begin{code} ?- pwd. % /home/janw/src/swipl-devel/linux/ true. ?- cd('~/tmp'). true. ?- pwd. % /home/janw/tmp/ true. \end{code} The file \file{likes.pl} is also installed in a subdirectory \file{demo} insides SWI-Prolog's installation directory and may be loaded regardless of the working directory using the command below. See absolute_file_name/3 and file_search_path/2 for details on how SWI-Prolog specifies file locations. \begin{code} ?- [swi(demo/likes)]. true. \end{code} \end{quote} After this point, Unix and Windows users unite, so if you are using Unix please continue at \secref{consultuser}. \subsubsection{Starting SWI-Prolog on Windows} \label{sec:startwin} After SWI-Prolog has been installed on a Windows system, the following important new things are available to the user: \begin{itemize} \item A folder (called \jargon{directory} in the remainder of this document) called \file{swipl} containing the executables, libraries, etc., of the system. No files are installed outside this directory. \item A program \program{swipl-win.exe}, providing a window for interaction with Prolog. The program \program{swipl.exe} is a version of SWI-Prolog that runs in a console window. \item The file extension \fileext{pl} is associated with the program \program{swipl-win.exe}. Opening a \fileext{pl} file will cause \program{swipl-win.exe} to start, change directory to the directory in which the file to open resides, and load this file. \end{itemize} The normal way to start the \file{likes.pl} file mentioned in \secref{startunix} is by simply double-clicking this file in the Windows explorer. \subsection{Adding rules from the console} \label{sec:consultuser} Although we strongly advice to put your program in a file, optionally edit it and use make/0 to reload it (see \secref{viewprogram}), it is possible to manage facts and rules from the terminal. The most convenient way to add a few clauses is by consulting the pseudo file \const{user}. The input is ended using the system end-of-file character. \begin{code} ?- [user]. |: hello :- format('Hello world~n'). |: ^D true. ?- hello. Hello world true. \end{code} The predicates assertz/1 and retract/1 are alternatives to add and remove rules and facts. \subsection{Executing a query} \label{sec:execquery} 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 \mbox{\tt X = } if it can prove the goal for a certain \arg{X}. The user can type the semi-colon (;) or spacebar% \footnote{On most installations, single-character commands are executed without waiting for the {\sc return} key.} if (s)he wants another solution. Use the \textsc{return} key if you do not want to see more answers. Prolog completes the output with a full stop (.) if the user uses the \textsc{return} key or Prolog \emph{knows} there are no more answers. If Prolog cannot find (more) answers, it writes \textbf{false.} Finally, Prolog answers using an error message to indicate the query or program contains an error. \begin{code} ?- likes(sam, X). X = dahl ; X = tandoori ; ... X = chips. ?- \end{code} Note that the answer written by Prolog is a valid Prolog program that, when executed, produces the same set of answers as the original program.\footnote{The SWI-Prolog top level differs in several ways from traditional Prolog top level. The current top level was designed in cooperation with Ulrich Neumerkel.} \subsection{Examining and modifying your program} \label{sec:viewprogram} If properly configured, the predicate edit/1 starts the built-in or user configured editor on the argument. The argument can be anything that can be linked to a location: a file name, predicate name, module name, etc. If the argument resolves to only one location the editor is started on this location, otherwise the user is presented a choice. If a graphical user interface is available, the editor normally creates a new window and the system prompts for the next command. The user may edit the source file, save it and run make/0 to update any modified source file. If the editor cannot be opened in a window, it opens in the same console and leaving the editor runs make/0 to reload any source files that have been modified. \begin{code} ?- edit(likes). true. ?- make. % /home/jan/src/pl-devel/linux/likes compiled 0.00 sec, 0 clauses ?- likes(sam, X). ... \end{code} The program can also be \jargon{decompiled} using listing/1 as below. The argument of listing/1 is just a predicate name, a predicate \jargon{indicator} of the form \arg{Name/Arity}, e.g., \exam{?- listing(mild/1).} or a \jargon{head}, e.g., \exam{?- listing(likes(sam, _)).}, listing all \jargon{matching} clauses. The predicate listing/0, i.e., without arguments lists the entire program.\footnote{This lists several \jargon{hook} predicates that are defined by default and is typically not very informative.} \begin{code} ?- listing(mild). mild(dahl). mild(tandoori). mild(kurma). true. \end{code} \subsection{Stopping Prolog} \label{sec:halt} The interactive toplevel can be stopped in two ways: enter the system end-of-file character (typically \textit{Control-D}) or by executing the halt/0 predicate: \begin{code} ?- halt. $ \end{code} \section{The user's initialisation file} \label{sec:initfile} \index{startup file}% \index{user profile file}% \index{profile file}% After the system initialisation, the system consults (see consult/1) the user's \jargon{init} file. This file is searched using absolute_file_name/3 using the path alias (see file_search_path/2) \const{app_config}. This is a directory named \file{swi-prolog} below the OS default name for placing application configuration data: \begin{itemize} \item On Windows, the CSIDL folder \const{CSIDL_APPDATA}, typically \verb$C:\Documents and Settings\username\Application Data$. \item If the environment variable \env{XDG_DATA_HOME} is set, use this. This follows the \href{https://standards.freedesktop.org}{free desktop} standard. \item The expansion of \file{~/.config}. \end{itemize} The directory can be found using this call: \begin{code} ?- absolute_file_name(app_config(.), Dir, [file_type(directory)]). Dir = '/home/jan/.config/swi-prolog'. \end{code} After the first startup file is found it is loaded and Prolog stops looking for further startup files. The name of the startup file can be changed with the `\argoption{-f}{file}' option. If \arg{File} denotes an absolute path, this file is loaded, otherwise the file is searched for using the same conventions as for the default startup file. Finally, if \arg{file} is \const{none}, no file is loaded. The installation provides a file \file{customize/init.pl} with (commented) commands that are often used to customize the behaviour of Prolog, such as interfacing to the editor, color selection or history parameters. Many of the development tools provide menu entries for editing the startup file and starting a fresh startup file from the system skeleton. See also the \cmdlineoption{-s} (script) and \cmdlineoption{-F} (system-wide initialisation) in \secref{cmdline} and \secref{initgoal}. \section{Initialisation files and goals} \label{sec:initgoal} Using command line arguments (see \secref{cmdline}), SWI-Prolog can be forced to load files and execute queries for initialisation purposes or non-interactive operation. The most commonly used options are \argoption{-f}{file} or \argoption{-s}{file} to make Prolog load a file, \argoption{-g}{goal} to define initialisation goals and \argoption{-t}{goal} to define the \jargon{top-level goal}. The following is a typical example for starting an application directly from the command line. \begin{code} machine% swipl -s load.pl -g go -t halt \end{code} It tells SWI-Prolog to load \file{load.pl}, start the application using the \jargon{entry point} \nopredref{go}{0} and ---instead of entering the interactive top level--- exit after completing \nopredref{go}{0}. The command line may have multiple \argoption{-g}{goal} occurrences. The goals are executed in order. Possible choice points of individual goals are pruned. If a \arg{goal} fails execution stops with exit status \const{1}. If a \arg{goal} raises an exception, the exception is printed and the process stops with exit code \const{2}. The \cmdlineoption{-q} may be used to suppress all informational messages as well as the error message that is normally printed if an initialisation goal \emph{fails}. In MS-Windows, the same can be achieved using a short-cut with appropriately defined command line arguments. A typically seen alternative is to write a file \file{run.pl} with content as illustrated below. Double-clicking \file{run.pl} will start the application. \begin{code} :- [load]. % load program :- go. % run it :- halt. % and exit \end{code} \Secref{plscript} discusses further scripting options, and \chapref{runtime} discusses the generation of runtime executables. Runtime executables are a means to deliver executables that do not require the Prolog system. \section{Command line options} \label{sec:cmdline} SWI-Prolog can be executed in one of the following modes: \begin{description} \definition{\exam{swipl --help}} \nodescription \definition{\exam{swipl --version}} \nodescription \definition{\exam{swipl --arch}} \nodescription \definition{\exam{swipl --dump-runtime-variables}} These options must appear as only option. They cause Prolog to print an informational message and exit. See \secref{info-options}. \definition{\exam{swipl} [\arg{option} ...] \arg{script-file} [\arg{arg} ...]} These arguments are passed on Unix systems if file that starts with \mbox{\texttt{\#!/path/to/executable} [\arg{option} ...]} is executed. Arguments after the script file are made available in the Prolog flag \prologflag{argv}. \definition{\exam{swipl} [\arg{option} ...] \arg{prolog-file} ... [[\const{--}] \arg{arg} ...]} This is the normal way to start Prolog. The options are described in \secref{running-options}, \secref{stacksizes} and \secref{runoptions}. The Prolog flag \prologflag{argv} provides access to \arg{arg} ... If the \arg{options} are followed by one or more Prolog file names (i.e., names with extension \fileext{pl}, \fileext{prolog} or (on Windows) the user preferred extension registered during installation), these files are loaded. The first file is registered in the Prolog flag \prologflag{associated_file}. In addition, \program{pl-win[.exe]} switches to the directory in which this primary source file is located using working_directory/2. \definition{\exam{swipl} -o \arg{output} -c \arg{prolog-file} ...} The \cmdlineoption{-c} option is used to compile a set of Prolog files into an executable. See \secref{compoptions}. \definition{\exam{swipl} -o \arg{output} -b \arg{bootfile} \arg{prolog-file} ...} Bootstrap compilation. See \secref{maintoptions}. \end{description} \subsection{Informational command line options} \label{sec:info-options} \begin{description} \cmdlineoptionitem{--arch}{} When given as the only option, it prints the architecture identifier (see Prolog flag \prologflag{arch}) and exits. See also \cmdlineoption{--dump-runtime-variables}. \cmdlineoptionitem{--dump-runtime-variables}{[=format]} When given as the only option, it prints a sequence of variable settings that can be used in shell scripts to deal with Prolog parameters. This feature is also used by \program{swipl-ld} (see \secref{plld}). Below is a typical example of using this feature. \begin{code} eval `swipl --dump-runtime-variables` cc -I$PLBASE/include -L$PLBASE/lib/$PLARCH ... \end{code} The option can be followed by \const{=sh} to dump in POSIX shell format (default) or \const{=cmd} to dump in MS-Windows \program{cmd.exe} compatible format. \cmdlineoptionitem{--help}{} When given as the only option, it summarises the most important options. \cmdlineoptionitem{--version}{} When given as the only option, it summarises the version and the architecture identifier. \cmdlineoptionitem{--abi-version}{} Print a key (string) that represents the binary compatibility on a number of aspects. See \secref{abi-versions}. \end{description} \subsection{Command line options for running Prolog} \label{sec:running-options} Note that \jargon{boolean options} may be written as \exam{--name} (true), \exam{--noname} or \exam{--no-name} (false). They are written as \exam{--no-name} below as the default is `true'. \begin{description} \cmdlineoptionitem{--debug-on-interrupt}{} Enable debugging on an interrupt signal (Control-C, \const{SIGINT}) immediately. Normally debugging on interrupt is enabled when entering the interactive toplevel. This flag can be used to start the debugger on an interrupt while executing goals from \cmdlineoption{-g} or initialization/[1,2]. See also the Prolog flag \prologflag{debug_on_interrupt}. \cmdlineoptionitem{--home[=DIR]}{} Use \arg{DIR} as home directory. See \secref{findhome} for details. If \arg{DIR} is omitted, the found location is printed and the process exits. If the location cannot be found an error is printed and the process exits with status 1. \cmdlineoptionitem{--quiet}{} \index{verbose}\index{quiet}% Set the Prolog flag \prologflag{verbose} to \const{silent}, suppressing informational and banner messages. Also available as \cmdlineoption{-q}. \cmdlineoptionitem{--no-debug}{} Disable debugging. See the current_prolog_flag/2 flag \prologflag{generate_debug_info} for details. \cmdlineoptionitem{--no-signals}{} Inhibit any signal handling by Prolog, a property that is sometimes desirable for embedded applications. This option sets the flag \prologflag{signals} to \const{false}. See \secref{sigembedded} for details. Note that the handler to unblock system calls is still installed. This can be prevented using \exam{--sigalert=0} additionally. See \cmdlineoption{--sigalert}. \cmdlineoptionitem{--no-threads}{} Disable threading for the multi-threaded version at runtime. See also the flags \prologflag{threads} and \prologflag{gc_thread}. \cmdlineoptionitem{--no-packs}{} Do \emph{not} attach extension packages (add-ons). See also attach_packs/0 and the Prolog flag \prologflag{packs}. \cmdlineoptionitem{--no-pce}{} Enable/disable the xpce GUI subsystem. The default is to make it available as autoload component if it is installed and the system can access the graphics. Using \exam{--pce} load the xpce system in user space and \exam{--no-pce} makes it unavailable in the session. \cmdlineoptionitem{--on-error}{=style} How to handle on errors. See the Prolog flag \prologflag{on_error} for details. \cmdlineoptionitem{--on-warning}{=style} How to handle on warnings. See the Prolog flag \prologflag{on_warning} for details. \cmdlineoptionitem{--pldoc}{[=port]} Start the PlDoc documentation system on a free network port and launch the user's browser on \verb$http://localhost:$\arg{port}. If \arg{port} is specified, the server is started at the given port and the browser is \emph{not} launched. \cmdlineoptionitem{--sigalert=NUM}{} Use signal \arg{NUM} (1\ldots{}31) for alerting a thread. This is needed to make thread_signal/2, and derived Prolog signal handling act immediately when the target thread is blocked on an interruptible system call (e.g., sleep/1, read/write to most devices). The default is to use \const{SIGUSR2}. If \arg{NUM} is 0 (zero), this handler is not installed. See prolog_alert_signal/2 to query or modify this value at runtime. \cmdlineoptionitem{--no-tty}{} Unix only. Switches controlling the terminal for allowing single-character commands to the tracer and get_single_char/1. By default, manipulating the terminal is enabled unless the system detects it is not connected to a terminal or it is running as a GNU-Emacs inferior process. See also \prologflag{tty_control}. \cmdlineoptionitem{--win-app}{} This option is available only in \program{swipl-win.exe} and is used for the start-menu item. If causes \program{plwin} to start in the folder \verb$...\My Documents\Prolog$ or local equivalent thereof (see win_folder/2). The \file{Prolog} subdirectory is created if it does not exist. \cmdlineoptionitem{-O}{} Optimised compilation. See current_prolog_flag/2 flag \prologflag{optimise} for details. \cmdlineoptionitem{-l}{file} Load \arg{file}. This flag provides compatibility with some other Prolog systems.\footnote{YAP, SICStus} It is used in SWI-Prolog to skip the program initialization specified using initialization/2 directives. See also \secref{plscript}, and initialize/0. \cmdlineoptionitem{-s}{file} Use \arg{file} as a script file. The script file is loaded after the initialisation file specified with the \argoption{-f}{file} option. Unlike \argoption{-f}{file}, using \cmdlineoption{-s} does not stop Prolog from loading the personal initialisation file. \cmdlineoptionitem{-f}{file} Use \arg{file} as initialisation file instead of the default \file{init.pl}. `\argoption{-f}{none}' stops SWI-Prolog from searching for a startup file. This option can be used as an alternative to \argoption{-s}{file} that stops Prolog from loading the personal initialisation file. See also \secref{initfile}. \cmdlineoptionitem{-F}{script} Select a startup script from the SWI-Prolog home directory. The script file is named \file{