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 Controlling the stack-sizes
2.4.2 Running goals from the commandline
2.4.3 Compiler options
2.4.4 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 Syntax Notes
2.15.1 ISO Syntax Support
2.15.1.1 Processor Character Set
2.15.1.2 Character Escape Syntax
2.15.1.3 Syntax for non-decimal numbers
2.15.1.4 Unicode Prolog source
2.15.1.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-bits 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 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 ISO Input and Output Streams
4.16.2 Edinburgh-style I/O
4.16.3 Switching between Edinburgh and ISO I/O
4.16.4 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 Character properties
4.22.1 Case conversion
4.22.2 White space normalization
4.22.3 Language-specific comparison
4.23 Representing text in strings
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 Dealing with time and date
4.33.1.1 Time and date data structures
4.33.1.2 Time and date predicates
4.33.2 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 Modules
5.1 Why Use Modules?
5.2 Defining a Module
5.3 Importing Predicates into a Module
5.4 Defining a meta-predicate
5.5 Overruling Module Boundaries
5.5.1 Explicit manipulation of the calling context
5.6 Interacting with modules from the toplevel
5.7 Composing modules from other modules
5.8 Operators and modules
5.9 Dynamic importing using import modules
5.10 Reserved Modules and using the `user' module
5.11 An alternative import/export interface
5.12 Dynamic Modules
5.13 Transparent predicates: definition and context module
5.14 Module properties
5.15 Compatibility of the Module System
6 Special Variables and Coroutining
6.1 Attributed variables
6.1.1 Attribute manipulation predicates
6.1.2 Attributed variable hooks
6.1.3 Operations on terms with attributed variables
6.1.4 Special purpose predicates for attributes
6.2 Coroutining
6.3 Global variables
6.3.1 Compatibility of SWI-Prolog Global Variables
7 CHR: Constraint Handling Rules
7.1 Introduction
7.2 Syntax and Semantics
7.2.1 Syntax of CHR rules
7.2.2 Semantics
7.3 CHR in SWI-Prolog Programs
7.3.1 Embedding in Prolog Programs
7.3.2 Constraint declaration
7.3.3 Compilation
7.4 Debugging
7.4.1 Ports
7.4.2 Tracing
7.4.3 CHR Debugging Predicates
7.5 Examples
7.6 Backwards Compatibility
7.6.1 The Old SICStus CHR implemenation
7.6.2 The Old ECLiPSe CHR implemenation
7.7 Programming Tips and Tricks
7.8 Compiler Errors and Warnings
7.8.1 CHR Compiler Errors
8 Multi-threaded applications
8.1 Creating and destroying Prolog threads
8.2 Monitoring threads
8.3 Thread communication
8.3.1 Message queues
8.3.2 Signalling threads
8.3.3 Threads and dynamic predicates
8.4 Thread synchronisation
8.5 Thread support library(threadutil)
8.5.1 Debugging threads
8.5.2 Profiling threads
8.6 Unbounded thread creation
8.7 Multi-threaded mixed C and Prolog applications
8.7.1 A Prolog thread for each native thread (one-to-one)
8.7.2 Pooling Prolog engines (many-to-many)
8.7.2.1 Engines in single-threaded SWI-Prolog
8.8 Multithreading and the XPCE graphics system
9 Foreign Language Interface
9.1 Overview of the Interface
9.2 Linking Foreign Modules
9.2.1 What linking is provided?
9.2.2 What kind of loading should I be using?
9.2.3 library(shlib): Utility library for loading foreign objects (DLLs, shared objects)
9.2.4 Low-level operations on shared libraries
9.2.5 Static Linking
9.3 Interface Data types
9.3.1 Type term_t: a reference to a Prolog term
9.3.1.1 Interaction with the garbage collector and stack-shifter
9.3.2 Other foreign interface types
9.4 The Foreign Include File
9.4.1 Argument Passing and Control
9.4.1.1 Non-deterministic Foreign Predicates
9.4.2 Atoms and functors
9.4.2.1 Atoms and atom garbage collection
9.4.3 Analysing Terms via the Foreign Interface
9.4.3.1 Testing the type of a term
9.4.3.2 Reading data from a term
9.4.3.3 Exchanging text using length and string
9.4.3.4 Wide-character versions
9.4.3.5 Reading a list
9.4.3.6 An example: defining write/1 in C
9.4.4 Constructing Terms
9.4.5 Unifying data
9.4.6 Convenient functions to generate Prolog exceptions
9.4.7 BLOBS: Using atoms to store arbitrary binary data
9.4.7.1 Defining a BLOB type
9.4.7.2 Accessing blobs
9.4.8 Exchanging GMP numbers
9.4.9 Calling Prolog from C
9.4.9.1 Predicate references
9.4.9.2 Initiating a query from C
9.4.10 Discarding Data
9.4.11 Foreign Code and Modules
9.4.12 Prolog exceptions in foreign code
9.4.13 Catching Signals (Software Interrupts)
9.4.14 Miscellaneous
9.4.14.1 Term Comparison
9.4.14.2 Recorded database
9.4.14.3 Getting file names
9.4.15 Errors and warnings
9.4.16 Environment Control from Foreign Code
9.4.17 Querying Prolog
9.4.18 Registering Foreign Predicates
9.4.19 Foreign Code Hooks
9.4.20 Storing foreign data
9.4.20.1 Examples for storing foreign data
9.4.21 Embedding SWI-Prolog in other applications
9.4.21.1 Threading, Signals and embedded Prolog
9.5 Linking embedded applications using swipl-ld
9.5.1 A simple example
9.6 The Prolog `home' directory
9.7 Example of Using the Foreign Interface
9.8 Notes on Using Foreign Code
9.8.1 Memory Allocation
9.8.1.1 Boehm-GC support
9.8.2 Compatibility between Prolog versions
9.8.3 Debugging and profiling foreign code (valgrind)
9.8.4 Name Conflicts in C modules
9.8.5 Compatibility of the Foreign Interface
10 Generating Runtime Applications
10.1 Limitations of qsave_program
10.2 Runtimes and Foreign Code
10.3 Using program resources
10.3.1 Predicate Definitions
10.3.2 The swipl-rc program
10.4 Finding Application files
10.4.1 Passing a path to the application
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): Elementary completeness checks
A.7 library(clpfd): Constraint Logic Programming over Finite Domains
A.8 library(clpqr): Constraint Logic Programming over Rationals and Reals
A.8.1 Solver predicates
A.8.2 Syntax of the predicate arguments
A.8.3 Use of unification
A.8.4 Non-linear constraints
A.8.5 Status and known problems
A.9 library(csv): Process CSV (Comma-Separated Values) data
A.10 library(debug): Print debug messages and test assertions
A.11 library(gensym): Generate unique identifiers
A.12 library(lists): List Manipulation
A.13 library(nb_set): Non-backtrackable set
A.14 library(www_browser): Activating your Web-browser
A.15 library(option): Option list processing
A.16 library(optparse): command line parsing
A.16.1 Notes and tips
A.17 library(ordsets): Ordered set manipulation
A.18 library(pairs): Operations on key-value lists
A.19 library(pio): Pure I/O
A.19.1 library(pure_input): Pure Input from files
A.20 library(predicate_options): Declare option-processing of predicates
A.20.1 The strength and weakness of predicate options
A.20.2 Options as arguments or environment?
A.20.3 Improving on the current situation
A.20.3.1 Options as types
A.20.3.2 Reflective access to options
A.21 library(prolog_pack): A package manager for Prolog
A.22 library(prolog_xref): Cross-reference data collection library
A.22.1 Extending the library
A.23 library(random): Random numbers
A.24 library(readutil): Reading lines, streams and files
A.25 library(record): Access named fields in a term
A.26 library(registry): Manipulating the Windows registry
A.27 library(simplex): Solve linear programming problems
A.27.1 Example 1
A.27.2 Example 2
A.27.3 Example 3
A.28 library(thread_pool): Resource bounded thread management
A.29 library(ugraphs): Unweighted Graphs
A.30 library(url): Analysing and constructing URL
A.31 library(varnumbers): Utilities for numbered terms
B Hackers corner
B.1 Examining the Environment Stack
B.2 Global cuts
B.3 Intercepting the Tracer
B.4 Adding context to errors: prolog_exception_hook
B.5 Hooks using the exception predicate
B.6 Hooks for integrating libraries
B.7 Hooks for loading files
B.8 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(option)
F.2.11 library(optparse)
F.2.12 library(ordsets)
F.2.13 library(predicate_options)
F.2.14 library(prologpack)
F.2.15 library(prologxref)
F.2.16 library(pairs)
F.2.17 library(pio)
F.2.17.1 library(pure_input)
F.2.18 library(random)
F.2.19 library(readutil)
F.2.20 library(record)
F.2.21 library(registry)
F.2.22 library(ugraphs)
F.2.23 library(url)
F.2.24 library(www_browser)
F.2.25 library(clp/clpfd)
F.2.26 library(clpqr)
F.2.27 library(clp/simplex)
F.2.28 library(thread_pool)
F.2.29 library(varnumbers)
F.3 Arithmetic Functions
F.4 Operators
G Bibliography