Table of Contents
1
Introduction
1.1
Positioning SWI-Prolog
1.2
Status and releases
1.3
Should I be using SWI-Prolog?
1.4
Support the SWI-Prolog project
1.5
Implementation history
1.6
Acknowledgements
2
Overview
2.1
Getting started quickly
2.1.1
Starting SWI-Prolog
2.1.1.1
Starting SWI-Prolog on Unix
2.1.1.2
Starting SWI-Prolog on Windows
2.1.2
Executing a query
2.2
The user's initialisation file
2.3
Initialisation files and goals
2.4
Command line options
2.4.1
Informational command line options
2.4.2
Command line options for running Prolog
2.4.3
Controlling the stack sizes
2.4.4
Running goals from the command line
2.4.5
Compilation options
2.4.6
Maintenance options
2.5
GNU Emacs Interface
2.6
Online Help
2.7
Command line history
2.8
Reuse of top-level bindings
2.9
Overview of the Debugger
2.10
Compilation
2.10.1
During program development
2.10.2
For running the result
2.10.2.1
Using PrologScript
2.10.2.2
Creating a shell script
2.10.2.3
Creating a saved state
2.10.2.4
Compilation using the -c command line option
2.11
Environment Control (Prolog flags)
2.12
An overview of hook predicates
2.13
Automatic loading of libraries
2.14
Packs: community add-ons
2.15
Garbage Collection
2.16
The SWI-Prolog syntax
2.16.1
ISO Syntax Support
2.16.1.1
Processor Character Set
2.16.1.2
Nested comments
2.16.1.3
Character Escape Syntax
2.16.1.4
Syntax for non-decimal numbers
2.16.1.5
Using digit groups in large integers
2.16.1.6
NaN and Infinity floats and their syntax
2.16.1.7
Force only underscore to introduce a variable
2.16.1.8
Unicode Prolog source
2.16.1.9
Singleton variable checking
2.17
Rational trees (cyclic terms)
2.18
Just-in-time clause indexing
2.18.1
Future directions
2.18.2
Indexing and portability
2.19
Wide character support
2.19.1
Wide character encodings on streams
2.19.1.1
BOM: Byte Order Mark
2.20
System limits
2.20.1
Limits on memory areas
2.20.1.1
The heap
2.20.2
Other Limits
2.20.3
Reserved Names
2.21
SWI-Prolog and 64-bit machines
2.21.1
Supported platforms
2.21.2
Comparing 32- and 64-bits Prolog
2.21.3
Choosing between 32- and 64-bit Prolog
3
Initialising and Managing a Prolog Project
3.1
The project source files
3.1.1
File Names and Locations
3.1.1.1
File Name Extensions
3.1.1.2
Project Directories
3.1.1.3
Sub-projects using search paths
3.1.2
Project Special Files
3.1.3
International source files
3.2
Using modules
3.3
The test-edit-reload cycle
3.3.1
Locating things to edit
3.3.2
Editing and incremental compilation
3.4
Using the PceEmacs built-in editor
3.4.1
Activating PceEmacs
3.4.2
Bluffing through PceEmacs
3.4.2.1
Edit modes
3.4.2.2
Frequently used editor commands
3.4.3
Prolog Mode
3.4.3.1
Finding your way around
3.5
The Graphical Debugger
3.5.1
Invoking the window-based debugger
3.6
The Prolog Navigator
3.7
Cross-referencer
3.8
Accessing the IDE from your program
3.9
Summary of the IDE
4
Built-in Predicates
4.1
Notation of Predicate Descriptions
4.2
Character representation
4.3
Loading Prolog source files
4.3.1
Conditional compilation and program transformation
4.3.1.1
Program transformation with source layout info
4.3.1.2
Conditional compilation
4.3.2
Reloading files, active code and threads
4.3.2.1
Compilation of mutually dependent code
4.3.2.2
Compilation with multiple threads
4.3.3
Quick load files
4.4
Editor Interface
4.4.1
Customizing the editor interface
4.5
List the program, predicates or clauses
4.6
Verify Type of a Term
4.7
Comparison and Unification of Terms
4.7.1
Standard Order of Terms
4.7.2
Special unification and comparison predicates
4.8
Control Predicates
4.9
Meta-Call Predicates
4.10
Delimited continuations
4.11
Exception handling
4.11.1
Urgency of exceptions
4.11.2
Debugging and exceptions
4.11.3
The exception term
4.11.4
Printing messages
4.11.4.1
Printing from libraries
4.12
Handling signals
4.12.1
Notes on signal handling
4.13
DCG Grammar rules
4.14
Database
4.14.1
Managing (dynamic) predicates
4.14.2
The recorded database
4.14.3
Flags
4.14.4
Tries
4.14.5
Update view
4.14.6
Indexing databases
4.15
Declaring predicate properties
4.16
Examining the program
4.17
Input and output
4.17.1
Predefined stream aliases
4.17.2
ISO Input and Output Streams
4.17.3
Edinburgh-style I/O
4.17.4
Switching between Edinburgh and ISO I/O
4.17.5
Write onto atoms, code-lists, etc.
4.17.6
Fast binary term I/O
4.18
Status of streams
4.19
Primitive character I/O
4.20
Term reading and writing
4.21
Analysing and Constructing Terms
4.21.1
Non-logical operations on terms
4.22
Analysing and Constructing Atoms
4.23
Localization (locale) support
4.24
Character properties
4.24.1
Case conversion
4.24.2
White space normalization
4.24.3
Language-specific comparison
4.25
Operators
4.26
Character Conversion
4.27
Arithmetic
4.27.1
Special purpose integer arithmetic
4.27.2
General purpose arithmetic
4.27.2.1
Arithmetic types
4.27.2.2
Rational number examples
4.27.2.3
Arithmetic Functions
4.28
Misc arithmetic support predicates
4.29
Built-in list operations
4.30
Finding all Solutions to a Goal
4.31
Forall
4.32
Formatted Write
4.32.1
Writef
4.32.2
Format
4.32.3
Programming Format
4.33
Global variables
4.33.1
Compatibility of SWI-Prolog Global Variables
4.34
Terminal Control
4.35
Operating System Interaction
4.35.1
Windows-specific Operating System Interaction
4.35.2
Dealing with time and date
4.35.2.1
Time and date data structures
4.35.2.2
Time and date predicates
4.35.3
Controlling the
swipl-win.exe
console window
4.36
File System Interaction
4.37
User Top-level Manipulation
4.38
Creating a Protocol of the User Interaction
4.39
Debugging and Tracing Programs
4.40
Obtaining Runtime Statistics
4.41
Execution profiling
4.41.1
Profiling predicates
4.41.2
Visualizing profiling data
4.41.3
Information gathering
4.41.3.1
Profiling in the Windows Implementation
4.42
Memory Management
4.43
Windows DDE interface
4.43.1
DDE client interface
4.43.2
DDE server mode
4.44
Miscellaneous
5
SWI-Prolog extensions
5.1
Lists are special
5.1.1
Motivating '
[|]
' and
[]
for lists
5.2
The string type and its double quoted syntax
5.2.1
Predicates that operate on strings
5.2.2
Representing text: strings, atoms and code lists
5.2.3
Adapting code for double quoted strings
5.2.4
Why has the representation of double quoted text changed?
5.3
Syntax changes
5.3.1
Operators and quoted atoms
5.3.2
Compound terms with zero arguments
5.3.3
Block operators
5.4
Dicts: structures with named arguments
5.4.1
Functions on dicts
5.4.1.1
User defined functions on dicts
5.4.1.2
Predefined functions on dicts
5.4.2
Predicates for managing dicts
5.4.2.1
Destructive assignment in dicts
5.4.3
When to use dicts?
5.4.4
A motivation for dicts as primary citizens
5.4.5
Implementation notes about dicts
5.5
Integration of strings and dicts in the libraries
5.5.1
Dicts and option processing
5.5.2
Dicts in core data structures
5.5.3
Dicts, strings and XML
5.5.4
Dicts, strings and JSON
5.5.5
Dicts, strings and HTTP
5.6
Remaining issues
6
Modules
6.1
Why Use Modules?
6.2
Defining a Module
6.3
Importing Predicates into a Module
6.4
Defining a meta-predicate
6.5
Overruling Module Boundaries
6.5.1
Explicit manipulation of the calling context
6.6
Interacting with modules from the top level
6.7
Composing modules from other modules
6.8
Operators and modules
6.9
Dynamic importing using import modules
6.10
Reserved Modules and using the `user' module
6.11
An alternative import/export interface
6.12
Dynamic Modules
6.13
Transparent predicates: definition and context module
6.14
Module properties
6.15
Compatibility of the Module System
7
Constraint Logic Programming
7.1
Attributed variables
7.1.1
Attribute manipulation predicates
7.1.2
Attributed variable hooks
7.1.3
Operations on terms with attributed variables
7.1.4
Special purpose predicates for attributes
7.2
Coroutining
8
CHR: Constraint Handling Rules
8.1
Introduction
8.2
Syntax and Semantics
8.2.1
Syntax of CHR rules
8.2.2
Semantics
8.3
CHR in SWI-Prolog Programs
8.3.1
Embedding in Prolog Programs
8.3.2
Constraint declaration
8.3.3
Compilation
8.4
Debugging
8.4.1
Ports
8.4.2
Tracing
8.4.3
CHR Debugging Predicates
8.5
Examples
8.6
Backwards Compatibility
8.6.1
The Old SICStus CHR implemenation
8.6.2
The Old ECLiPSe CHR implemenation
8.7
Programming Tips and Tricks
8.8
Compiler Errors and Warnings
8.8.1
CHR Compiler Errors
9
Multithreaded applications
9.1
Creating and destroying Prolog threads
9.2
Monitoring threads
9.3
Thread communication
9.3.1
Message queues
9.3.2
Signalling threads
9.3.3
Threads and dynamic predicates
9.4
Thread synchronisation
9.5
Thread support library(threadutil)
9.5.1
Debugging threads
9.5.2
Profiling threads
9.6
Multithreaded mixed C and Prolog applications
9.6.1
A Prolog thread for each native thread (one-to-one)
9.6.2
Pooling Prolog engines (many-to-many)
9.7
Multithreading and the XPCE graphics system
10
Coroutining using Prolog engines
10.1
Examples using engines
10.1.1
Aggregation using engines
10.1.2
State accumulation using engines
10.1.3
Scalable many-agent applications
10.2
Engine resource usage
10.3
Engine predicate reference
11
Foreign Language Interface
11.1
Overview of the Interface
11.2
Linking Foreign Modules
11.2.1
What linking is provided?
11.2.2
What kind of loading should I be using?
11.2.3
library(shlib): Utility library for loading foreign objects (DLLs, shared objects)
11.2.4
Low-level operations on shared libraries
11.2.5
Static Linking
11.3
Interface Data Types
11.3.1
Type
term_t
: a reference to a Prolog term
11.3.1.1
Interaction with the garbage collector and stack-shifter
11.3.2
Other foreign interface types
11.3.2.1
PL_ARITY_AS_SIZE
11.4
The Foreign Include File
11.4.1
Argument Passing and Control
11.4.1.1
Non-deterministic Foreign Predicates
11.4.2
Atoms and functors
11.4.2.1
Atoms and atom garbage collection
11.4.3
Analysing Terms via the Foreign Interface
11.4.3.1
Testing the type of a term
11.4.3.2
Reading data from a term
11.4.3.3
Exchanging text using length and string
11.4.3.4
Wide-character versions
11.4.3.5
Reading a list
11.4.3.6
An example: defining write/1 in C
11.4.4
Constructing Terms
11.4.5
Unifying data
11.4.6
Convenient functions to generate Prolog exceptions
11.4.7
BLOBS: Using atoms to store arbitrary binary data
11.4.7.1
Defining a BLOB type
11.4.7.2
Accessing blobs
11.4.8
Exchanging GMP numbers
11.4.9
Calling Prolog from C
11.4.9.1
Predicate references
11.4.9.2
Initiating a query from C
11.4.10
Discarding Data
11.4.11
Foreign Code and Modules
11.4.12
Prolog exceptions in foreign code
11.4.13
Catching Signals (Software Interrupts)
11.4.14
Miscellaneous
11.4.14.1
Term Comparison
11.4.14.2
Recorded database
11.4.14.3
Getting file names
11.4.14.4
Dealing with Prolog flags from C
11.4.15
Errors and warnings
11.4.16
Environment Control from Foreign Code
11.4.17
Querying Prolog
11.4.18
Registering Foreign Predicates
11.4.19
Foreign Code Hooks
11.4.20
Storing foreign data
11.4.20.1
Examples for storing foreign data
11.4.21
Embedding SWI-Prolog in other applications
11.4.21.1
Threading, Signals and embedded Prolog
11.5
Linking embedded applications using swipl-ld
11.5.1
A simple example
11.6
The Prolog `home' directory
11.7
Example of Using the Foreign Interface
11.8
Notes on Using Foreign Code
11.8.1
Foreign debugging functions
11.8.2
Memory Allocation
11.8.2.1
Boehm-GC support
11.8.3
Compatibility between Prolog versions
11.8.4
Debugging and profiling foreign code (valgrind)
11.8.5
Name Conflicts in C modules
11.8.6
Compatibility of the Foreign Interface
12
Generating Runtime Applications
12.1
Limitations of qsave_program
12.2
Runtimes and Foreign Code
12.3
Using program resources
12.3.1
Resource manipulation predicates
12.3.2
The
swipl-rc
program
12.4
Finding Application files
12.4.1
Specifying a file search path from the command line
A
The SWI-Prolog library
A.1
library(aggregate): Aggregation operators on backtrackable predicates
A.2
library(apply): Apply predicates on a list
A.3
library(assoc): Association lists
A.3.1
Introduction
A.3.2
Creating association lists
A.3.3
Querying association lists
A.3.4
Modifying association lists
A.3.5
Conversion predicates
A.3.6
Reasoning about association lists and their elements
A.4
library(broadcast): Broadcast and receive event notifications
A.5
library(charsio): I/O on Lists of Character Codes
A.6
library(check): Consistency checking
A.7
library(clpb): CLP(B): Constraint Logic Programming over Boolean Variables
A.7.1
Introduction
A.7.2
Boolean expressions
A.7.3
Interface predicates
A.7.4
Examples
A.7.5
Obtaining BDDs
A.7.6
Enabling monotonic CLP(B)
A.7.7
Example: Pigeons
A.7.8
Example: Boolean circuit
A.7.9
Acknowledgments
A.7.10
CLP(B) predicate index
A.8
library(clpfd): CLP(FD): Constraint Logic Programming over Finite Domains
A.8.1
Introduction
A.8.2
Arithmetic constraints
A.8.3
Declarative integer arithmetic
A.8.4
Example: Factorial relation
A.8.5
Combinatorial constraints
A.8.6
Domains
A.8.7
Example: Sudoku
A.8.8
Residual goals
A.8.9
Core relations and search
A.8.10
Example: Eight queens puzzle
A.8.11
Optimisation
A.8.12
Reification
A.8.13
Enabling monotonic CLP(FD)
A.8.14
Custom constraints
A.8.15
Applications
A.8.16
Acknowledgments
A.8.17
CLP(FD) predicate index
A.8.17.1
Arithmetic constraints
A.8.17.2
Membership constraints
A.8.17.3
Enumeration predicates
A.8.17.4
Global constraints
A.8.17.5
Reification predicates
A.8.17.6
Reflection predicates
A.8.18
Closing and opening words about CLP(FD)
A.9
library(clpqr): Constraint Logic Programming over Rationals and Reals
A.9.1
Solver predicates
A.9.2
Syntax of the predicate arguments
A.9.3
Use of unification
A.9.4
Non-linear constraints
A.9.5
Status and known problems
A.10
library(csv): Process CSV (Comma-Separated Values) data
A.11
library(debug): Print debug messages and test assertions
A.12
library(error): Error generating support
A.13
library(gensym): Generate unique identifiers
A.14
library(iostream): Utilities to deal with streams
A.15
library(lists): List Manipulation
A.16
library(main): Provide entry point for scripts
A.17
library(nb_set): Non-backtrackable set
A.18
library(www_browser): Activating your Web-browser
A.19
library(option): Option list processing
A.20
library(optparse): command line parsing
A.20.1
Notes and tips
A.21
library(ordsets): Ordered set manipulation
A.22
library(pairs): Operations on key-value lists
A.23
library(persistency): Provide persistent dynamic predicates
A.24
library(pio): Pure I/O
A.24.1
library(pure_input): Pure Input from files and streams
A.25
library(predicate_options): Declare option-processing of predicates
A.25.1
The strength and weakness of predicate options
A.25.2
Options as arguments or environment?
A.25.3
Improving on the current situation
A.25.3.1
Options as types
A.25.3.2
Reflective access to options
A.26
library(prolog_pack): A package manager for Prolog
A.27
library(prolog_xref): Cross-reference data collection library
A.27.1
Extending the library
A.28
library(quasi_quotations): Define Quasi Quotation syntax
A.29
library(random): Random numbers
A.30
library(readutil): Reading lines, streams and files
A.31
library(record): Access named fields in a term
A.32
library(registry): Manipulating the Windows registry
A.33
library(simplex): Solve linear programming problems
A.33.1
Introduction
A.33.2
Delayed column generation
A.33.3
Solving LPs with special structure
A.33.4
Examples
A.33.4.1
Example 1
A.33.4.2
Example 2
A.33.4.3
Example 3
A.34
library(solution_sequences): Modify solution sequences
A.35
library(tabling): Tabled execution (SLG)
A.35.1
Example 1: using tabling for memoizing
A.35.2
Example 2: avoiding non-termination
A.35.3
Mode directed tabling
A.35.1
Tabling predicate reference
A.35.1.1
About the tabling implementation
A.35.1.2
Status of tabling
A.36
library(thread_pool): Resource bounded thread management
A.37
library(ugraphs): Unweighted Graphs
A.38
library(url): Analysing and constructing URL
A.39
library(varnumbers): Utilities for numbered terms
A.40
library(yall): Lambda expressions
B
Hackers corner
B.1
Examining the Environment Stack
B.2
Ancestral cuts
B.3
Intercepting the Tracer
B.4
Breakpoint and watchpoint handling
B.5
Adding context to errors: prolog_exception_hook
B.6
Hooks using the exception predicate
B.7
Hooks for integrating libraries
B.8
Hooks for loading files
C
Compatibility with other Prolog dialects
C.1
Some considerations for writing portable code
D
Glossary of Terms
E
SWI-Prolog License Conditions and Tools
E.1
Contributing to the SWI-Prolog project
E.2
Software support to keep track of license conditions
E.3
License conditions inherited from used code
E.3.1
Cryptographic routines
F
Summary
F.1
Predicates
F.2
Library predicates
F.2.1
library(aggregate)
F.2.2
library(apply)
F.2.3
library(assoc)
F.2.4
library(broadcast)
F.2.5
library(charsio)
F.2.6
library(check)
F.2.7
library(clpb)
F.2.8
library(clpfd)
F.2.9
library(clpqr)
F.2.10
library(csv)
F.2.11
library(debug)
F.2.12
library(error)
F.2.13
library(iostream)
F.2.14
library(summaries.d/iostream/tex)
F.2.15
library(lists)
F.2.16
library(main)
F.2.17
library(option)
F.2.18
library(optparse)
F.2.19
library(ordsets)
F.2.20
library(persistency)
F.2.21
library(predicate_options)
F.2.22
library(prologpack)
F.2.23
library(prologxref)
F.2.24
library(pairs)
F.2.25
library(pio)
F.2.25.1
library(pure_input)
F.2.26
library(random)
F.2.27
library(readutil)
F.2.28
library(record)
F.2.29
library(registry)
F.2.30
library(simplex)
F.2.31
library(ugraphs)
F.2.32
library(url)
F.2.33
library(www_browser)
F.2.34
library(solution_sequences)
F.2.35
library(thread_pool)
F.2.36
library(varnumbers)
F.2.37
library(yall)
F.3
Arithmetic Functions
F.4
Operators
G
Bibliography