Table of Contents

1 Introduction
1.1 SWI-Prolog
1.1.1 Books about Prolog
1.2 Status
1.3 Compliance to the ISO standard
1.4 Should you be using SWI-Prolog?
1.5 The XPCE GUI system for Prolog
1.6 Release Notes
1.6.1 Version 1.8 Release Notes
1.6.2 Version 1.9 Release Notes
1.6.3 Version 2.0 Release Notes
1.6.4 Version 2.5 Release Notes
1.6.5 Version 2.6 Release Notes
1.6.6 Version 2.7 Release Notes
1.6.7 Version 2.8 Release Notes
1.6.8 Version 2.9 Release Notes
1.6.9 Version 3.0 Release Notes
1.6.10 Version 3.1 Release Notes
1.6.11 Version 3.3 Release Notes
1.6.11.1 Incompatible changes
1.6.12 Version 3.4 Release Notes
1.6.13 Version 4.0 Release Notes
1.6.14 Version 5.0 Release Notes
1.6.15 Version 5.1 Release Notes
1.6.16 Version 5.2 Release Notes
1.6.17 Version 5.3 Release Notes
1.6.18 Version 5.4 Release Notes
1.6.19 Version 5.5 Release Notes
1.6.20 Version 5.6 Release Notes
1.7 Donate to the SWI-Prolog project
1.8 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.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 Infinite trees (cyclic terms)
2.17 Wide character support
2.17.1 Wide character encodings on streams
2.17.1.1 BOM: Byte Order Mark
2.18 System limits
2.18.1 Limits on memory areas
2.18.1.1 The heap
2.18.2 Other Limits
2.18.3 Reserved Names
2.19 SWI-Prolog and 64-bit machines
2.19.1 Supported platforms
2.19.2 Comparing 32- and 64-bits Prolog
2.19.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 Threads and reloading running code
4.3.3 Quick load files
4.4 Listing and Editor Interface
4.5 Verify Type of a Term
4.6 Comparison and Unification of Terms
4.6.1 Standard Order of Terms
4.6.2 Special unification and comparison predicates
4.7 Control Predicates
4.8 Meta-Call Predicates
4.9 ISO compliant Exception handling
4.9.1 Debugging and exceptions
4.9.2 The exception term
4.9.3 Printing messages
4.10 Handling signals
4.10.1 Notes on signal handling
4.11 The `block' control-structure
4.12 DCG Grammar rules
4.13 Database
4.13.1 Update view
4.13.2 Indexing databases
4.14 Declaring predicates 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 Adding Arithmetic Functions
4.28 Built-in list operations
4.29 Finding all Solutions to a Goal
4.30 Invoking Predicates on all Members of a List
4.31 Forall
4.32 Formatted Write
4.32.1 Writef
4.32.2 Format
4.32.3 Programming Format
4.33 Terminal Control
4.34 Operating System Interaction
4.34.1 Dealing with time and date
4.34.1.1 Time and date data-structures
4.34.1.2 Time and date predicates
4.34.2 Controlling the PLWIN.EXE console window
4.35 File System Interaction
4.36 User Top-level Manipulation
4.37 Creating a Protocol of the User Interaction
4.38 Debugging and Tracing Programs
4.39 Obtaining Runtime Statistics
4.40 Execution profiling
4.40.1 Profiling predicates
4.40.2 Visualizing profiling data
4.40.3 Information gathering
4.40.3.1 Profiling in the Windows Implementation
4.41 Memory Management
4.42 Windows DDE interface
4.42.1 DDE client interface
4.42.2 DDE server mode
4.43 Miscellaneous
5 Using Modules
5.1 Why Using Modules?
5.2 Name-based versus Predicate-based Modules
5.3 Defining a Module
5.4 Importing Predicates into a Module
5.4.1 Reserved Modules
5.5 Composing modules from other modules
5.6 Using the Module System
5.6.1 Object Oriented Programming
5.7 Meta-Predicates in Modules
5.7.1 Definition and Context Module
5.7.2 Overruling Module Boundaries
5.8 Dynamic Modules
5.9 Module Handling Predicates
5.10 Compatibility of the Module System
5.10.1 Emulating meta_predicate
6 Special Variables and Coroutining
6.1 Attributed variables
6.1.1 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
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.3 Dynamic Linking of shared libraries
9.4 Using the library shlib for .DLL and .so files
9.4.1 Static Linking
9.5 Interface Data types
9.5.1 Type term_t: a reference to a Prolog term
9.5.1.1 Interaction with the garbage collector and stack-shifter
9.5.2 Other foreign interface types
9.6 The Foreign Include File
9.6.1 Argument Passing and Control
9.6.1.1 Non-deterministic Foreign Predicates
9.6.2 Atoms and functors
9.6.2.1 Atoms and atom-garbage collection
9.6.3 Analysing Terms via the Foreign Interface
9.6.3.1 Testing the type of a term
9.6.3.2 Reading data from a term
9.6.3.3 Exchanging text using length and string
9.6.3.4 Wide character versions
9.6.3.5 Reading a list
9.6.3.6 An example: defining write/1 in C
9.6.4 Constructing Terms
9.6.5 Unifying data
9.6.6 BLOBS: Using atoms to store arbitrary binary data
9.6.6.1 Defining a BLOB type
9.6.6.2 Accessing blobs
9.6.7 Exchanging GMP numbers
9.6.8 Calling Prolog from C
9.6.8.1 Predicate references
9.6.8.2 Initiating a query from C
9.6.9 Discarding Data
9.6.10 Foreign Code and Modules
9.6.11 Prolog exceptions in foreign code
9.6.12 Catching Signals (Software Interrupts)
9.6.13 Miscellaneous
9.6.13.1 Term Comparison
9.6.13.2 Recorded database
9.6.13.3 Getting file names
9.6.14 Errors and warnings
9.6.15 Environment Control from Foreign Code
9.6.16 Querying Prolog
9.6.17 Registering Foreign Predicates
9.6.18 Foreign Code Hooks
9.6.19 Storing foreign data
9.6.19.1 Examples for storing foreign data
9.6.20 Embedding SWI-Prolog in other applications
9.6.20.1 Threading, Signals and embedded Prolog
9.7 Linking embedded applications using plld
9.7.1 A simple example
9.8 The Prolog `home' directory
9.9 Example of Using the Foreign Interface
9.10 Notes on Using Foreign Code
9.10.1 Memory Allocation
9.10.2 Compatibility between Prolog versions
9.10.3 Debugging and profiling foreign code (valgrind)
9.10.4 Name Conflicts in C modules
9.10.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 Predicates Definitions
10.3.2 The plrc program
10.4 Finding Application files
10.4.1 Passing a path to the application
10.5 The Runtime Environment
10.5.1 The Runtime Emulator
A The SWI-Prolog library
A.1 aggregate.pl -- Aggregation operators on backtrackable predicates
A.1.1 Acknowledgements
A.2 apply.pl -- Apply predicates on a list
A.3 library( assoc ): Association lists
A.4 library( broadcast ): Broadcast and receive event notifications
A.5 library( check ): Elementary completeness checks
A.6 library( clp/clp_distinct ): Weak arc consistent `all_distinct' constraint
A.6.1 Example 1
A.6.2 Example 2
A.6.3 Example 3
A.7 clpfd.pl -- 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.9 library( debug ): Some reusable code to help debugging applications
A.10 library( gensym ): Generate unique identifiers
A.11 library( lists ): List Manipulation
A.11.1 Set Manipulation
A.12 library( nbset ): Non-backtrackable set
A.13 library( www_browser ): Activating your Web-browser
A.14 option.pl -- Option list processing
A.15 library( ordsets ): Ordered Set Manipulation
A.16 pairs.pl -- Operations on key-value lists
A.17 library( pio ): Pure I/O
A.17.1 pure_input.pl -- Pure Input from files
A.18 library( prologxref ): Cross-reference data collection library
A.18.1 Extending the library
A.19 library( readutil ): Reading lines, streams and files
A.20 library( record ): Access named fields in a term
A.21 library( registry ): Manipulating the Windows registry
A.22 library( simplex ): Solve linear programming problems
A.22.1 Example 1
A.22.2 Example 2
A.22.3 Example 3
A.23 library( ugraphs ): Unweighted Graphs
A.24 url.pl -- Analysing and constructing URL
B Hackers corner
B.1 Examining the Environment Stack
B.2 Intercepting the Tracer
B.3 Adding context to errors: prolog_exception_hook
B.4 Hooks using the exception predicate
B.5 Hooks for integrating libraries
B.6 Hooks for loading files
B.7 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 Library predicates
E.4.1 library(aggregate)
E.4.2 library(apply)
E.4.3 library(assoc)
E.4.4 library(broadcast)
E.4.5 library(check)
E.4.6 library(lists)
E.4.7 library(option)
E.4.8 library(ordsets)
E.4.9 library(prologxref)
E.4.10 library(pairs)
E.4.11 library(pio)
E.4.11.1 library(pure_input)
E.4.12 library(readutil)
E.4.13 library(record)
E.4.14 library(registry)
E.4.15 library(ugraphs)
E.4.16 library(url)
E.4.17 library(www_browser)
E.4.18 library(clp/clp_distinct)
E.4.19 library(clp/clpfd)
E.4.20 library(clpqr)
E.4.21 library(clp/simplex)
F Bibliography