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
Garbage Collection
2.15
The SWI-Prolog syntax
2.15.1
ISO Syntax Support
2.15.1.1
Processor Character Set
2.15.2
Nested comments
2.15.2.1
Character Escape Syntax
2.15.2.2
Syntax for non-decimal numbers
2.15.2.3
Using digit groups in large integers
2.15.2.4
Unicode Prolog source
2.15.2.5
Singleton variable checking
2.16
Rational trees (cyclic terms)
2.17
Just-in-time clause indexing
2.17.1
Future directions
2.17.2
Indexing and portability
2.18
Wide character support
2.18.1
Wide character encodings on streams
2.18.1.1
BOM: Byte Order Mark
2.19
System limits
2.19.1
Limits on memory areas
2.19.1.1
The heap
2.19.2
Other Limits
2.19.3
Reserved Names
2.20
SWI-Prolog and 64-bit machines
2.20.1
Supported platforms
2.20.2
Comparing 32- and 64-bits Prolog
2.20.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
Loading files, active code and threads
4.3.2.1
Compilation of mutually dependent code
4.3.2.2
Compilation with multiple threads
4.3.2.3
Reloading running code
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
ISO compliant Exception handling
4.10.1
Debugging and exceptions
4.10.2
The exception term
4.10.3
Printing messages
4.10.3.1
Printing from libraries
4.11
Handling signals
4.11.1
Notes on signal handling
4.12
DCG Grammar rules
4.13
Database
4.13.1
Update view
4.13.2
Indexing databases
4.14
Declaring predicate properties
4.15
Examining the program
4.16
Input and output
4.16.1
Predefined stream aliases
4.16.2
ISO Input and Output Streams
4.16.3
Edinburgh-style I/O
4.16.4
Switching between Edinburgh and ISO I/O
4.16.5
Write onto atoms, code-lists, etc.
4.17
Status of streams
4.18
Primitive character I/O
4.19
Term reading and writing
4.20
Analysing and Constructing Terms
4.20.1
Non-logical operations on terms
4.21
Analysing and Constructing Atoms
4.22
Localization (locale) support
4.23
Character properties
4.23.1
Case conversion
4.23.2
White space normalization
4.23.3
Language-specific comparison
4.24
Operators
4.25
Character Conversion
4.26
Arithmetic
4.26.1
Special purpose integer arithmetic
4.26.2
General purpose arithmetic
4.26.2.1
Arithmetic types
4.26.2.2
Rational number examples
4.26.2.3
Arithmetic Functions
4.27
Misc arithmetic support predicates
4.28
Built-in list operations
4.29
Finding all Solutions to a Goal
4.30
Forall
4.31
Formatted Write
4.31.1
Writef
4.31.2
Format
4.31.3
Programming Format
4.32
Terminal Control
4.33
Operating System Interaction
4.33.1
Windows-specific Operating System Interaction
4.33.2
Dealing with time and date
4.33.2.1
Time and date data structures
4.33.2.2
Time and date predicates
4.33.3
Controlling the
swipl-win.exe
console window
4.34
File System Interaction
4.35
User Top-level Manipulation
4.36
Creating a Protocol of the User Interaction
4.37
Debugging and Tracing Programs
4.38
Obtaining Runtime Statistics
4.39
Execution profiling
4.39.1
Profiling predicates
4.39.2
Visualizing profiling data
4.39.3
Information gathering
4.39.3.1
Profiling in the Windows Implementation
4.40
Memory Management
4.41
Windows DDE interface
4.41.1
DDE client interface
4.41.2
DDE server mode
4.42
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
Special Variables and Coroutining
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
7.3
Global variables
7.3.1
Compatibility of SWI-Prolog Global Variables
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
Foreign Language Interface
10.1
Overview of the Interface
10.2
Linking Foreign Modules
10.2.1
What linking is provided?
10.2.2
What kind of loading should I be using?
10.2.3
library(shlib): Utility library for loading foreign objects (DLLs, shared objects)
10.2.4
Low-level operations on shared libraries
10.2.5
Static Linking
10.3
Interface Data Types
10.3.1
Type
term_t
: a reference to a Prolog term
10.3.1.1
Interaction with the garbage collector and stack-shifter
10.3.2
Other foreign interface types
10.4
The Foreign Include File
10.4.1
Argument Passing and Control
10.4.1.1
Non-deterministic Foreign Predicates
10.4.2
Atoms and functors
10.4.2.1
Atoms and atom garbage collection
10.4.3
Analysing Terms via the Foreign Interface
10.4.3.1
Testing the type of a term
10.4.3.2
Reading data from a term
10.4.3.3
Exchanging text using length and string
10.4.3.4
Wide-character versions
10.4.3.5
Reading a list
10.4.3.6
An example: defining write/1 in C
10.4.4
Constructing Terms
10.4.5
Unifying data
10.4.6
Convenient functions to generate Prolog exceptions
10.4.7
BLOBS: Using atoms to store arbitrary binary data
10.4.7.1
Defining a BLOB type
10.4.7.2
Accessing blobs
10.4.8
Exchanging GMP numbers
10.4.9
Calling Prolog from C
10.4.9.1
Predicate references
10.4.9.2
Initiating a query from C
10.4.10
Discarding Data
10.4.11
Foreign Code and Modules
10.4.12
Prolog exceptions in foreign code
10.4.13
Catching Signals (Software Interrupts)
10.4.14
Miscellaneous
10.4.14.1
Term Comparison
10.4.14.2
Recorded database
10.4.14.3
Getting file names
10.4.14.4
Dealing with Prolog flags from C
10.4.15
Errors and warnings
10.4.16
Environment Control from Foreign Code
10.4.17
Querying Prolog
10.4.18
Registering Foreign Predicates
10.4.19
Foreign Code Hooks
10.4.20
Storing foreign data
10.4.20.1
Examples for storing foreign data
10.4.21
Embedding SWI-Prolog in other applications
10.4.21.1
Threading, Signals and embedded Prolog
10.5
Linking embedded applications using swipl-ld
10.5.1
A simple example
10.6
The Prolog `home' directory
10.7
Example of Using the Foreign Interface
10.8
Notes on Using Foreign Code
10.8.1
Memory Allocation
10.8.1.1
Boehm-GC support
10.8.2
Compatibility between Prolog versions
10.8.3
Debugging and profiling foreign code (valgrind)
10.8.4
Name Conflicts in C modules
10.8.5
Compatibility of the Foreign Interface
11
Generating Runtime Applications
11.1
Limitations of qsave_program
11.2
Runtimes and Foreign Code
11.3
Using program resources
11.3.1
Resource manipulation predicates
11.3.2
The
swipl-rc
program
11.4
Finding Application files
11.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.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): 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.8
library(clpfd): Constraint Logic Programming over Finite Domains
A.8.1
Introduction
A.8.2
Arithmetic constraints
A.8.3
Declarative integer arithmetic
A.8.4
Reification
A.8.5
Domains
A.8.6
Examples
A.8.7
Enumeration predicates and search
A.8.8
Advanced topics
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(gensym): Generate unique identifiers
A.13
library(iostream): Utilities to deal with streams
A.14
library(lists): List Manipulation
A.15
library(nb_set): Non-backtrackable set
A.16
library(www_browser): Activating your Web-browser
A.17
library(option): Option list processing
A.18
library(optparse): command line parsing
A.18.1
Notes and tips
A.19
library(ordsets): Ordered set manipulation
A.20
library(pairs): Operations on key-value lists
A.21
library(persistency): Provide persistent dynamic predicates
A.22
library(pio): Pure I/O
A.22.1
library(pure_input): Pure Input from files
A.23
library(predicate_options): Declare option-processing of predicates
A.23.1
The strength and weakness of predicate options
A.23.2
Options as arguments or environment?
A.23.3
Improving on the current situation
A.23.3.1
Options as types
A.23.3.2
Reflective access to options
A.24
library(prolog_pack): A package manager for Prolog
A.25
library(prolog_xref): Cross-reference data collection library
A.25.1
Extending the library
A.26
library(quasi_quotations): Define Quasi Quotation syntax
A.27
library(random): Random numbers
A.28
library(readutil): Reading lines, streams and files
A.29
library(record): Access named fields in a term
A.30
library(registry): Manipulating the Windows registry
A.31
library(simplex): Solve linear programming problems
A.31.1
Example 1
A.31.2
Example 2
A.31.3
Example 3
A.32
library(solution_sequences): Modify solution sequences
A.33
library(thread_pool): Resource bounded thread management
A.34
library(ugraphs): Unweighted Graphs
A.35
library(url): Analysing and constructing URL
A.36
library(varnumbers): Utilities for numbered terms
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
B.9
Readline Interaction
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
The SWI-Prolog kernel and foreign libraries
E.1.1
The SWI-Prolog Prolog libraries
E.2
Contributing to the SWI-Prolog project
E.3
Software support to keep track of license conditions
E.4
License conditions inherited from used code
E.4.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(csv)
F.2.8
library(lists)
F.2.9
library(debug)
F.2.10
library(iostream)
F.2.11
library(summaries.d/iostream/tex)
F.2.12
library(option)
F.2.13
library(optparse)
F.2.14
library(ordsets)
F.2.15
library(persistency)
F.2.16
library(predicate_options)
F.2.17
library(prologpack)
F.2.18
library(prologxref)
F.2.19
library(pairs)
F.2.20
library(pio)
F.2.20.1
library(pure_input)
F.2.21
library(random)
F.2.22
library(readutil)
F.2.23
library(record)
F.2.24
library(registry)
F.2.25
library(ugraphs)
F.2.26
library(url)
F.2.27
library(www_browser)
F.2.28
library(clp/clpb)
F.2.29
library(clp/clpfd)
F.2.30
library(clpqr)
F.2.31
library(clp/simplex)
F.2.32
library(solution_sequences)
F.2.33
library(thread_pool)
F.2.34
library(varnumbers)
F.3
Arithmetic Functions
F.4
Operators
G
Bibliography