Table of Contents

1Introduction
1.1SWI-Prolog
1.1.1Books about Prolog
1.2Status
1.3Compliance to the ISO standard
1.4Should you be using SWI-Prolog?
1.5The XPCE GUI system for Prolog
1.6Release Notes
1.6.1Version 1.8 Release Notes
1.6.2Version 1.9 Release Notes
1.6.3Version 2.0 Release Notes
1.6.4Version 2.5 Release Notes
1.6.5Version 2.6 Release Notes
1.6.6Version 2.7 Release Notes
1.6.7Version 2.8 Release Notes
1.6.8Version 2.9 Release Notes
1.6.9Version 3.0 Release Notes
1.6.10Version 3.1 Release Notes
1.6.11Version 3.3 Release Notes
1.6.11.1Incompatible changes
1.6.12Version 3.4 Release Notes
1.6.13Version 4.0 Release Notes
1.6.14Version 5.0 Release Notes
1.6.15Version 5.1 Release Notes
1.6.16Version 5.2 Release Notes
1.6.17Version 5.3 Release Notes
1.6.18Version 5.4 Release Notes
1.6.19Version 5.5 Release Notes
1.6.20Version 5.6 Release Notes
1.7Donate to the SWI-Prolog project
1.8Acknowledgements
2Overview
2.1Getting started quickly
2.1.1Starting SWI-Prolog
2.1.1.1Starting SWI-Prolog on Unix
2.1.1.2Starting SWI-Prolog on Windows
2.1.2Executing a query
2.2The user's initialisation file
2.3Initialisation files and goals
2.4Command-line options
2.5GNU Emacs Interface
2.6Online Help
2.7Command-line history
2.8Reuse of top-level bindings
2.9Overview of the Debugger
2.10Compilation
2.10.1During program development
2.10.2For running the result
2.10.2.1Using PrologScript
2.10.2.2Creating a shell-script
2.10.2.3Creating a saved-state
2.10.2.4Compilation using the -c command-line option
2.11Environment Control (Prolog flags)
2.12An overview of hook predicates
2.13Automatic loading of libraries
2.14Garbage Collection
2.15Syntax Notes
2.15.1ISO Syntax Support
2.15.1.1Processor Character Set
2.15.1.2Character Escape Syntax
2.15.1.3Syntax for non-decimal numbers
2.15.1.4Unicode Prolog source
2.15.1.5Singleton variable checking
2.16Infinite trees (cyclic terms)
2.17Wide character support
2.17.1Wide character encodings on streams
2.17.1.1BOM: Byte Order Mark
2.18System limits
2.18.1Limits on memory areas
2.18.1.1The heap
2.18.2Other Limits
2.18.3Reserved Names
3Initialising and Managing a Prolog Project
3.1The project source-files
3.1.1File Names and Locations
3.1.1.1File Name Extensions
3.1.1.2Project Directories
3.1.1.3Sub-projects using search-paths
3.1.2Project Special Files
3.1.3International source files
3.2Using modules
3.3The test-edit-reload cycle
3.3.1Locating things to edit
3.3.2Editing and incremental compilation
3.4Using the PceEmacs built-in editor
3.4.1Activating PceEmacs
3.4.2Bluffing through PceEmacs
3.4.2.1Edit modes
3.4.2.2Frequently used editor commands
3.4.3Prolog Mode
3.4.3.1Finding your way around
3.5The Graphical Debugger
3.5.1Invoking the window-based debugger
3.6The Prolog Navigator
3.7Cross referencer
3.8Accessing the IDE from your program
3.9Summary of the iDE
4Built-in predicates
4.1Notation of Predicate Descriptions
4.2Character representation
4.3Loading Prolog source files
4.3.1Loading files, active code and threads
4.3.1.1Threads and reloading running code
4.3.2Quick load files
4.4Listing and Editor Interface
4.5Verify Type of a Term
4.6Comparison and Unification or Terms
4.6.1Standard Order of Terms
4.7Control Predicates
4.8Meta-Call Predicates
4.9ISO compliant Exception handling
4.9.1Debugging and exceptions
4.9.2The exception term
4.9.3Printing messages
4.10Handling signals
4.10.1Notes on signal handling
4.11The `block' control-structure
4.12DCG Grammar rules
4.13Database
4.13.1Update view
4.13.2Indexing databases
4.14Declaring predicates properties
4.15Examining the program
4.16Input and output
4.16.1ISO Input and Output Streams
4.16.2Edinburgh-style I/O
4.16.3Switching Between Edinburgh and ISO I/O
4.16.4Write onto atoms, code-lists, etc.
4.17Status of streams
4.18Primitive character I/O
4.19Term reading and writing
4.20Analysing and Constructing Terms
4.20.1Non-logical operations on terms
4.21Analysing and Constructing Atoms
4.22Classifying characters
4.22.1Case conversion
4.23Representing text in strings
4.24Operators
4.25Character Conversion
4.26Arithmetic
4.26.1Special purpose integer arithmetic
4.26.2General purpose arithmetic
4.26.2.1Arithmetic types
4.26.2.2Rational number examples
4.26.2.3Arithmetic Functions
4.27Adding Arithmetic Functions
4.28Built-in list operations
4.29Finding all Solutions to a Goal
4.30Invoking Predicates on all Members of a List
4.31Forall
4.32Formatted Write
4.32.1Writef
4.32.2Format
4.32.3Programming Format
4.33Terminal Control
4.34Operating System Interaction
4.34.1Dealing with time and date
4.34.1.1Time and date data-structures
4.34.1.2Time and date predicates
4.34.2Controlling the PLWIN.EXE console window
4.35File System Interaction
4.36User Top-level Manipulation
4.37Creating a Protocol of the User Interaction
4.38Debugging and Tracing Programs
4.39Obtaining Runtime Statistics
4.40Execution profiling
4.40.1Profiling predicates
4.40.2Visualizing profiling data
4.40.3Information gathering
4.40.3.1Profiling in the Windows Implementation
4.41Memory Management
4.42Windows DDE interface
4.42.1DDE client interface
4.42.2DDE server mode
4.43Miscellaneous
5Using Modules
5.1Why Using Modules?
5.2Name-based versus Predicate-based Modules
5.3Defining a Module
5.4Importing Predicates into a Module
5.4.1Reserved Modules
5.5Using the Module System
5.5.1Object Oriented Programming
5.6Meta-Predicates in Modules
5.6.1Definition and Context Module
5.6.2Overruling Module Boundaries
5.7Dynamic Modules
5.8Module Handling Predicates
5.9Compatibility of the Module System
5.9.1Emulating meta_predicate
6Special Variables and Coroutining
6.1Attributed variables
6.1.1Special purpose predicates for attributes
6.2Coroutining
6.3Global variables
6.3.1Compatibility of SWI-Prolog Global Variables
7CHR: Constraint Handling Rules
7.1Introduction
7.2Syntax and Semantics
7.2.1Syntax
7.2.2Semantics
7.3CHR in SWI-Prolog Programs
7.3.1Embedding in Prolog Programs
7.3.2Constraint declaration
7.3.3Compilation
7.4Debugging
7.4.1Ports
7.4.2Tracing
7.4.3CHR Debugging Predicates
7.5Examples
7.6Backwards Compatibility
7.7Programming Tips and Tricks
7.8Compiler Errors and Warnings
7.8.1CHR Compiler Errors
8Multi-threaded applications
8.1Creating and destroying Prolog threads
8.2Monitoring threads
8.2.1Linux: linuxthreads vs. NPTL
8.3Thread communication
8.3.1Message queues
8.3.2Signalling threads
8.3.3Threads and dynamic predicates
8.4Thread synchronisation
8.5Thread-support library(threadutil)
8.5.1Debugging threads
8.5.2Profiling threads
8.6Multi-threaded mixed C and Prolog applications
8.6.1A Prolog thread for each native thread (one-to-one)
8.6.2Pooling Prolog engines (many-to-many)
8.6.2.1Engines in single-threaded SWI-Prolog
8.7Multithreading and the XPCE graphics system
9Foreign Language Interface
9.1Overview of the Interface
9.2Linking Foreign Modules
9.2.1What linking is provided?
9.2.2What kind of loading should I be using?
9.3Dynamic Linking of shared libraries
9.4Using the library shlib for .DLL and .so files
9.4.1Static Linking
9.5Interface Data types
9.5.1Type term_t: a reference to a Prolog term
9.5.1.1Interaction with the garbage collector and stack-shifter
9.5.2Other foreign interface types
9.6The Foreign Include File
9.6.1Argument Passing and Control
9.6.1.1Non-deterministic Foreign Predicates
9.6.2Atoms and functors
9.6.2.1Atoms and atom-garbage collection
9.6.3Analysing Terms via the Foreign Interface
9.6.3.1Testing the type of a term
9.6.3.2Reading data from a term
9.6.3.3Exchanging text using length and string
9.6.3.4Wide character versions
9.6.3.5Reading a list
9.6.3.6An example: defining write/1 in C
9.6.4Constructing Terms
9.6.5Unifying data
9.6.6BLOBS: Using atoms to store arbitrary binary data
9.6.6.1Defining a BLOB type
9.6.6.2Accessing blobs
9.6.7Exchanging GMP numbers
9.6.8Calling Prolog from C
9.6.8.1Predicate references
9.6.8.2Initiating a query from C
9.6.9Discarding Data
9.6.10Foreign Code and Modules
9.6.11Prolog exceptions in foreign code
9.6.12Catching Signals (Software Interrupts)
9.6.13Miscellaneous
9.6.13.1Term Comparison
9.6.13.2Recorded database
9.6.13.3Getting file names
9.6.14Errors and warnings
9.6.15Environment Control from Foreign Code
9.6.16Querying Prolog
9.6.17Registering Foreign Predicates
9.6.18Foreign Code Hooks
9.6.19Storing foreign data
9.6.19.1Examples for storing foreign data
9.6.20Embedding SWI-Prolog in other applications
9.6.20.1Threading, Signals and embedded Prolog
9.7Linking embedded applications using plld
9.7.1A simple example
9.8The Prolog `home' directory
9.9Example of Using the Foreign Interface
9.10Notes on Using Foreign Code
9.10.1Memory Allocation
9.10.2Compatibility between Prolog versions
9.10.3Debugging Foreign Code
9.10.4Name Conflicts in C modules
9.10.5Compatibility of the Foreign Interface
10Generating Runtime Applications
10.1Limitations of qsave_program
10.2Runtimes and Foreign Code
10.3Using program resources
10.3.1Predicates Definitions
10.3.2The plrc program
10.4Finding Application files
10.4.1Passing a path to the application
10.5The Runtime Environment
10.5.1The Runtime Emulator
AThe SWI-Prolog library
A.1library( lists ): List Manipulation
A.1.1Set Manipulation
A.2library( ordsets ): Ordered Set Manipulation
A.3library( assoc ): Association lists
A.4library( ugraphs ): Unweighted Graphs
A.5library( nbset ): Non-backtrackable set
A.6library( gensym ): Generate unique identifiers
A.7library( check ): Elementary completeness checks
A.8library( debug ): Some reusable code to help debugging applications
A.9library( readutil ): Reading lines, streams and files
A.10library( netscape ): Activating your Web-browser
A.11library( registry ): Manipulating the Windows registry
A.12library( url ): Analysing and constructing URL
A.13library( clp/bounds ): Integer Bounds Constraint Solver
A.13.1Constraints
A.13.2Constraint Implication and Reified Constraints
A.13.3Example 1: Send+More=Money
A.13.4Example 2: Using tuples_in for a train schedule
A.13.5SICStus clp(FD) compatibility
A.14library( clpqr ): Constraint Logic Programming over Rationals and Reals
A.14.1Solver predicates
A.14.2Syntax of the predicate arguments
A.14.3Use of unification
A.14.4Non-linear constraints
A.15library( clp/clp_distinct ): Weak arc consistent `all_distinct' constraint
A.15.1Example 1
A.15.2Example 2
A.15.3Example 3
A.16library( simplex ): Solve linear programming problems
A.16.1Example 1
A.16.2Example 2
A.16.3Example 3
A.17library( prologxref ): Cross-reference data collection library
A.17.1Extending the library
BHackers corner
B.1Examining the Environment Stack
B.2Intercepting the Tracer
B.3Adding context to errors: prolog_exception_hook
B.4Hooks using the exception predicate
B.5Hooks for integrating libraries
B.6Hooks for loading files
B.7Readline Interaction
CGlossary of Terms
DSWI-Prolog License Conditions and Tools
D.1The SWI-Prolog kernel and foreign libraries
D.1.1The SWI-Prolog Prolog libraries
D.2Contributing to the SWI-Prolog project
D.3Software support to keep track of license conditions
D.4Library predicates
D.4.1library(check)
D.4.2library(lists)
D.4.3library(ordsets)
D.4.4library(ugraphs)
D.4.5library(www_browser)
D.4.6library(readutil)
D.4.7library(registry)
D.4.8library(url)
D.4.9library(clp/bounds)
D.4.10library(clp/clp_distinct)
D.4.11library(clp/simplex)
D.4.12library(clpqr)
D.4.13library(prologxref)
EBibliography