1\chapter{Introduction}			\label{sec:intro}
2
3This document is a \jargon{reference manual}. That means that it
4documents the system, but it does not explain the basics of the Prolog
5language and it leaves many details of the syntax, semantics and
6built-in primitives undefined where SWI-Prolog follows the standards.
7This manual is intended for people that are familiar with Prolog. For
8those not familiar with Prolog, we recommend to start with a Prolog
9textbook such as \cite{Bratko:86}, \cite{Sterling:86} or
10\cite{Clocksin:87}. For more advanced Prolog usage we recommend
11\cite{Keefe:90}.
12
13
14\section{Positioning SWI-Prolog}	\label{sec:swiprolog}
15
16Most implementations of the Prolog language are designed to serve a
17limited set of use cases. SWI-Prolog is no exception to this rule.
18SWI-Prolog positions itself primarily as a Prolog environment for
19`programming in the large' and use cases where it plays a central role
20in an application, i.e., where it acts as `glue' between components. At
21the same time, SWI-Prolog aims at providing a productive rapid
22prototyping environment. Its orientation towards programming in the
23large is backed up by scalability, compiler speed, program structuring
24(modules), support for multithreading to accommodate servers, Unicode
25and interfaces to a large number of document formats, protocols and
26programming languages. Prototyping is facilitated by good development
27tools, both for command line usage and for usage with graphical
28development tools. Demand loading of predicates from the library and a
29`make' facility avoids the \emph{requirement} for using declarations and
30reduces typing.
31
32SWI-Prolog is traditionally strong in education because it is free and
33portable, but also because of its compatibility with textbooks and its
34easy-to-use environment.
35
36Note that these positions do not imply that the system cannot be used
37with other scenarios. SWI-Prolog is used as an embedded language where
38it serves as a small rule subsystem in a large application. It is also
39used as a deductive database. In some cases this is the right choice
40because SWI-Prolog has features that are required in the application,
41such as threading or Unicode support. In general though, for example,
42GNU-Prolog is more suited for embedding because it is small and can
43compile to native code, XSB is better for deductive databases because it
44provides a mature implementation of \jargon{tabling} including support
45for incremental updates and \jargon{Well Founded
46Semantics}\footnote{Sponsored by Kyndi and with help from the XSB
47developers Theresa Swift and David S. Warren, SWI-Prolog now supports
48many of the XSB features.}, and ECLiPSe is better at constraint
49handling.
50
51The syntax and set of built-in predicates is based on the ISO standard
52\cite{stdprolog:98}. Most extensions follow the `Edinburgh tradition'
53(DEC10 Prolog and C-Prolog) and Quintus Prolog \cite{QUINTUS:manual}.
54The infrastructure for constraint programming is based on hProlog
55\cite{Demoen:CW350}. Some libraries are copied from the
56YAP\footnote{\url{http://www.dcc.fc.up.pt/\~{}vsc/Yap/}} system.
57Together with YAP we developed a portability framework (see
58\secref{dialect}). This framework has been filled for SICStus Prolog,
59YAP, IF/Prolog and Ciao. SWI-Prolog version~7 introduces various
60extensions to the Prolog language (see \secref{extensions}). The
61\jargon{string} data type and its supporting set of built-in predicates
62is compatible with ECLiPSe.
63
64\section{Status and releases}			\label{sec:status}
65
66This manual describes version \versionshort{} of SWI-Prolog. SWI-Prolog
67is widely considered to be a robust and scalable implementation of the
68Prolog language. It is widely used in education and research. In
69addition, it is in use for $24 \times 7$ mission critical commercial
70server processes. The site \url{http://www.swi-prolog.org} is hosted
71using the SWI-Prolog HTTP server infrastructure. It receives
72approximately 2.3 million hits and serves approximately 300 Gbytes on
73manual data and downloads each month.  SWI-Prolog applications range
74from student assignments to commercial applications that count more
75than one million lines of Prolog code.
76
77SWI-Prolog has two development tracks. \emph{Stable} releases have an
78even \emph{minor} version number (e.g., 6.2.1) and are released as a
79branch from the development version when the development
80version is considered stable and there is sufficient new functionality
81to justify a stable release. Stable releases often get a few patch
82updates to deal with installation issues or major flaws. A new
83\emph{Development} version is typically released every couple of weeks
84as a snapshot of the public git repository. `Extra editions' of the
85development version may be released after problems that severely hindered the
86user in their progress have been fixed.
87
88Known bugs that are not likely to be fixed soon are described as
89footnotes in this manual.
90
91
92\section{Should I be using SWI-Prolog?}		\label{sec:swiorother}
93
94There are a number of reasons why it might be better to choose a
95commercial, or another free, Prolog system:
96
97\begin{itemlist}
98    \item [SWI-Prolog comes with no warranties]
99Although the developers or the community often provide a work-around or
100a fix for a bug, there is no place you can go to for guaranteed support.
101However, the full source archive is available and can be used to compile
102and debug SWI-Prolog using free tools on all major platforms.
103Users requiring more support should ensure access to knowledgeable developers.
104
105    \item [Performance is your first concern]
106Various free and commercial systems have better performance. But,
107`standard' Prolog benchmarks disregard many factors that are often
108critical to the performance of large applications. SWI-Prolog is not
109good at fast calling of simple predicates, but it is fast with dynamic
110code, meta-calling and predicates that contain large numbers of clauses
111or require more advanced clauses indexing. Many of SWI-Prolog's built-in
112predicates are written in C and have excellent performance.
113\end{itemlist}
114
115On the other hand, SWI-Prolog offers some facilities that are widely
116appreciated by users:
117
118\begin{itemlist}
119    \item [Comprehensive support of Prolog extensions]
120Many modern Prolog implementations extend the standard SLD resolution
121mechanism with which Prolog started and that is described in the ISO
122standard. SWI-Prolog offers most popular extensions.
123
124\jargon{Attributed variables} provide \jargon{Constraint Logic
125Programming} and delayed execution based on instantiation
126(\jargon{coroutining}). \jargon{Tabling} or \jargon{SGL resolution}
127provides characteristics normally associated with \jargon{bottom up
128evaluation}: better termination, better predictable performance by
129avoiding recomputation and Well Founded Semantics for negation.
130\jargon{Delimited continuations} can be used to implement high level new
131control structures and \jargon{Engines} can be used to control multiple
132Prolog goals, achieving different control structures such as massive
133numbers of cooperating agents.
134
135    \item [Nice environment]
136SWI-Prolog provides a good command line environment, including `Do What I
137Mean', autocompletion, history and a tracer that operates on single
138key strokes.  The system automatically recompiles modified parts of the
139source code using the make/0 command.  The system can be instructed to
140open an arbitrary editor on the right file and line based on its source
141database. It ships with various graphical tools and can be combined with the
142SWI-Prolog editor, PDT (Eclipse plugin for Prolog), VScode or GNU-Emacs.
143
144    \item [Fast compiler]
145Even very large applications can be loaded in seconds on most machines.
146If this is not enough, there is the Quick Load Format.  See qcompile/1
147and qsave_program/2.
148
149    \item [Transparent compiled code]
150SWI-Prolog compiled code can be treated just as interpreted code: you
151can list it, trace it, etc. This implies you do not have to decide
152beforehand whether a module should be loaded for debugging or not, and
153the performance of debugged code is close to that of normal operation.
154
155    \item [Source level debugger]
156The source level debugger provides a good overview of your current
157location in the search tree, variable bindings, your source code and
158open choice points. Choice point inspection provides meaningful insight to
159both novices and experienced users. Avoiding unintended choice points
160often provides a huge increase in performance and a huge saving in
161memory usage.
162
163    \item [Profiling]
164SWI-Prolog offers an execution profiler with either textual output or
165graphical output.  Finding and improving hotspots in a Prolog program
166may result in huge speedups.
167
168    \item [Flexibility]
169SWI-Prolog can easily be integrated with C, supporting non-determinism
170in Prolog calling C as well as C calling Prolog (see \secref{foreign}).
171It can also be \jargon{embedded} in external programs (see
172\secref{plld}). System predicates can be redefined locally to provide
173compatibility with other Prolog systems.
174
175    \item [Threads]
176Robust support for multiple threads may improve performance and is a
177key enabling factor for deploying Prolog in server applications. Threads
178also facilitates debugging and maintenance of long running processes and
179embedded Prolog engines.  The native IDE tools run in a separate thread
180The \pllib{prolog_server} library provides \program{telnet} access and
181the pack \file{libssh} provides SSH login. With some restrictions
182regarding the compatibility of old and new code, code can be replaced
183while it is being executed in another thread.  This allows for injecting
184debug/3 statements as well as fixing bugs without downtime.
185
186    \item [Interfaces]
187SWI-Prolog ships with many extension packages that provide robust
188interfaces to processes, encryption, TCP/IP, TIPC, ODBC, SGML/XML/HTML,
189RDF, JSON, YAML, HTTP, graphics and much more.
190\end{itemlist}
191
192
193\section{Support the SWI-Prolog project}	\label{sec:sponsor}
194
195You can support the SWI-Prolog project in several ways. Academics are
196invited to cite one of the
197publications\footnote{\url{https://www.swi-prolog.org/Publications.html}}
198on SWI-Prolog. Users can help by identifying and/or fixing problems with
199the code or its
200documentation\footnote{\url{https://www.swi-prolog.org/howto/SubmitPatch.html}}.
201Users can contribute new features or, more lightweight, contribute
202packs\footnote{\url{https://www.swi-prolog.org/pack/list}}. Commercial
203users may consider contacting the
204developers\footnote{\url{mailto:info@swi-prolog.org}} to sponsor the
205development of new features or seek for opportunities to cooperate with
206the developers or other commercial users.
207
208
209\section{Implementation history}		\label{sec:implhistory}
210
211
212SWI-Prolog started back in 1986 with the requirement for a Prolog that
213could handle recursive interaction with the C-language: Prolog calling C
214and C calling Prolog recursively. In those days Prolog systems were not
215very aware of their environment and we needed such a system to support
216interactive applications. Since then, SWI-Prolog's development has been
217guided by requests from the user community, especially focussing on (in
218arbitrary order) interaction with the environment, scalability, (I/O)
219performance, standard compliance, teaching and the program development
220environment.
221
222SWI-Prolog is based on a simple Prolog virtual machine called ZIP
223\cite{Bowen:83,Neumerkel:93} which defines only 7 instructions. Prolog
224can easily be compiled into this language, and the abstract machine code
225is easily decompiled back into Prolog. As it is also possible to wire a
226standard 4-port debugger in the virtual machine, there is no need for a
227distinction between compiled and interpreted code. Besides simplifying
228the design of the Prolog system itself, this approach has advantages for
229program development: the compiler is simple and fast, the user does not
230have to decide in advance whether debugging is required, and the system
231only runs slightly slower in debug mode compared to normal execution.
232The price we have to pay is some performance degradation (taking out the
233debugger from the VM interpreter improves performance by about 20\%) and
234somewhat additional memory usage to help the decompiler and debugger.
235
236SWI-Prolog extends the minimal set of instructions described in
237\cite{Bowen:83} to improve performance. While extending this set, care
238has been taken to maintain the advantages of decompilation and tracing
239of compiled code. The extensions include specialised instructions for
240unification, predicate invocation, some frequently used built-in
241predicates, arithmetic, and control (\predref{;}{2}, \predref{|}{2}),
242if-then (\predref{->}{2}) and negation-by-failure (\predref{\+}{1}).
243
244SWI-Prolog implements \jargon{attributed variables} (constraints) and
245\jargon{delimited continuations} following the design in hProlog by Bart
246Demoen. The \jargon{engine} implementation follows the design proposed
247by Paul Tarau. Tabling was implemented by Benoit Desouter based on
248delimited continuations. Tabling has been extended with \jargon{answer
249subsumption} by Fabrizio Riguzzi. The implementation of \jargon{well
250founded semantics} and \jargon{incremental tabling} follows XSB and
251has been sponsored by Kyndi and mode possible by technical support from
252notably Theresa Swift and David S. Warren.
253
254
255\section{Acknowledgements}	\label{sec:acknowledge}
256
257Some small parts of the Prolog code of SWI-Prolog are modified
258versions of the corresponding Edinburgh C-Prolog code: grammar rule
259compilation and writef/2.  Also some of the C-code originates from
260C-Prolog: finding the path of the currently running executable and
261some of the code underlying absolute_file_name/2. Ideas on programming
262style and techniques originate from C-Prolog and Richard O'Keefe's {\em
263thief} editor. An important source of inspiration are the programming
264techniques introduced by Anjo Anjewierden in PCE version~1 and~2.
265
266Our special thanks go to those who had the fate of using the early
267versions of this system, suggested extensions or reported bugs. Among
268them are Anjo Anjewierden, Huub Knops, Bob Wielinga, Wouter Jansweijer,
269Luc Peerdeman, Eric Nombden, Frank van Harmelen, Bert Rengel.
270
271Martin Jansche (\email{jansche@novell1.gs.uni-heidelberg.de}) has been
272so kind to reorganise the sources for version 2.1.3 of this manual.
273Horst von Brand has been so kind to fix many typos in the 2.7.14 manual.
274Thanks!  Randy Sharp fixed many issues in the 6.0.x version of the
275manual.
276
277Bart Demoen and Tom Schrijvers have helped me adding coroutining,
278constraints, global variables and support for cyclic terms to the
279kernel. Tom Schrijvers has provided a first clp(fd) constraint solver,
280the CHR compiler and some of the coroutining predicates.  Markus Triska
281contributed the current clp(fd) implementation as well as the clp(b)
282implementation.
283
284Tom Schrijvers and Bart Demoen initiated the implementation of
285\jargon{delimited continuations} (\secref{delcont}), which was used by
286Benoit Desouter and Tom Schrijvers to implement \jargon{tabling}
287(\secref{tabling}) as a library. Fabrizio Riguzzi added a first
288implementation for \jargon{mode directed tabling}
289(\secref{tabling-mode-directed}).
290
291The SWI-Prolog~7 extensions (\secref{extensions}) are the result of a
292long heated discussion on the mailinglist. Nicos Angelopoulos' wish for
293a smooth integration with the R language triggered the overall intend of
294these extensions to enable a smoother integration of Prolog with other
295languages.  Michael Hendrix suggested and helped shaping SWI-Prolog
296\jargon{quasi quotations}.
297
298Paul Singleton has integrated Fred Dushin's Java-calls-Prolog side
299with his Prolog-calls-Java side into the current bidirectional JPL
300interface package.
301
302Richard O'Keefe is gratefully acknowledged for his efforts to educate
303beginners as well as valuable comments on proposed new developments.
304
305Scientific Software and Systems Limited, \url{www.sss.co.nz} has
306sponsored the development of the SSL library, unbounded integer and
307rational number arithmetic and many enhancements to the memory
308management of the system.
309
310Leslie de Koninck has made clp(QR) available to SWI-Prolog.
311
312Jeff Rosenwald contributed the TIPC networking library and Google's
313protocol buffer handling.
314
315Paulo Moura's great experience in maintaining Logtalk for many Prolog
316systems including SWI-Prolog has helped in many places fixing
317compatibility issues.  He also worked on the MacOS port and fixed many
318typos in the 5.6.9 release of the documentation.
319
320Kyndi (\url{https://kyndi.com/}) sponsored the development of the
321\jargon{engines} interface (\chapref{engines}). The final API was
322established after discussion with the founding father of engines, Paul
323Tarau and Paulo Moura. Kyndi also sponsored JIT indexing on multiple
324arguments as well as \jargon{deep indexing}. Kyndi currently supports
325the implementation of XSB compatible tabling, including well founded
326semantics and incremental tabling.  Theresa Swift, David S. Warren
327and Fabrizio Riguzzi provided input to realise advanced tabling.
328