View
2
Download
1
Embed Size (px)
University of Amsterdam Human-Computer Studies (HCS, formerly
SWI) Kruislaan 419, 1098 VA Amsterdam
The Netherlands Tel. (+31) 20 8884671
SWI-Prolog 5.6 Reference Manual
Updated for version 5.6.39, August 2007
Jan Wielemaker wielemak@science.uva.nl
http://www.swi-prolog.org
SWI-Prolog is a Prolog implementation based on a subset of the WAM (Warren Ab- stract Machine). SWI-Prolog was developed as an open Prolog environment, providing a powerful and bi-directional interface to C in an era this was unknown to other Prolog implementations. This environment is required to deal with XPCE, an object-oriented GUI system developed at SWI. XPCE is used at SWI for the development of knowledge- intensive graphical applications.
As SWI-Prolog became more popular, a large user-community provided requirements that guided its development. Compatibility, portability, scalability, stability and provid- ing a powerful development environment have been the most important requirements. Edinburgh, Quintus, SICStus and the ISO-standard guide the development of the SWI- Prolog primitives.
This document gives an overview of the features, system limits and built-in predicates.
Copyright c© 1990–2007, University of Amsterdam
Contents
1 Introduction 10 1.1 SWI-Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.1.1 Books about Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.2 Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.3 Compliance to the ISO standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.4 Should you be using SWI-Prolog? . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.5 The XPCE GUI system for Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.6 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.6.1 Version 1.8 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.6.2 Version 1.9 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.6.3 Version 2.0 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.6.4 Version 2.5 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.6.5 Version 2.6 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.6.6 Version 2.7 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.6.7 Version 2.8 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.6.8 Version 2.9 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.6.9 Version 3.0 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.6.10 Version 3.1 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.6.11 Version 3.3 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.6.12 Version 3.4 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.6.13 Version 4.0 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.6.14 Version 5.0 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.6.15 Version 5.1 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.6.16 Version 5.2 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.6.17 Version 5.3 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.6.18 Version 5.4 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.6.19 Version 5.5 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.6.20 Version 5.6 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.7 Donate to the SWI-Prolog project . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.8 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2 Overview 21 2.1 Getting started quickly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.1.1 Starting SWI-Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.1.2 Executing a query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.2 The user’s initialisation file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.3 Initialisation files and goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.4 Command-line options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.5 GNU Emacs Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.6 Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
SWI-Prolog 5.6 Reference Manual
Contents 3
2.7 Command-line history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.8 Reuse of top-level bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.9 Overview of the Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.10 Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.10.1 During program development . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.10.2 For running the result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.11 Environment Control (Prolog flags) . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2.12 An overview of hook predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2.13 Automatic loading of libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 2.14 Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 2.15 Syntax Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.15.1 ISO Syntax Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 2.16 Infinite trees (cyclic terms) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 2.17 Wide character support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.17.1 Wide character encodings on streams . . . . . . . . . . . . . . . . . . . . . 50 2.18 System limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
2.18.1 Limits on memory areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 2.18.2 Other Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 2.18.3 Reserved Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.19 SWI-Prolog and 64-bit machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 2.19.1 Supported platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 2.19.2 Comparing 32- and 64-bits Prolog . . . . . . . . . . . . . . . . . . . . . . . 54 2.19.3 Choosing between 32- and 64-bits Prolog . . . . . . . . . . . . . . . . . . . 55
3 Initialising and Managing a Prolog Project 56 3.1 The project source-files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
3.1.1 File Names and Locations . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 3.1.2 Project Special Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 3.1.3 International source files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
3.2 Using modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3.3 The test-edit-reload cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.3.1 Locating things to edit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 3.3.2 Editing and incremental compilation . . . . . . . . . . . . . . . . . . . . . . 60
3.4 Using the PceEmacs built-in editor . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 3.4.1 Activating PceEmacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 3.4.2 Bluffing through PceEmacs . . . . . . . . . . . . . . . . . . . . . . . . . . 60 3.4.3 Prolog Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
3.5 The Graphical Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 3.5.1 Invoking the window-based debugger . . . . . . . . . . . . . . . . . . . . . 65
3.6 The Prolog Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 3.7 Cross referencer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 3.8 Accessing the IDE from your program . . . . . . . . . . . . . . . . . . . . . . . . . 68 3.9 Summary of the iDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
SWI-Prolog 5.6 Reference Manual
4
4 Built-in predicates 70 4.1 Notation of Predicate Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 4.2 Character representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 4.3 Loading Prolog source files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4.3.1 Loading files, active code and threads . . . . . . . . . . . . . . . . . . . . . 78 4.3.2 Quick load files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
4.4 Listing and Editor Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 4.5 Verify Type of a Term . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 4.6 Comparison and Unification or Terms . . . . . . . . . . . . . . . . . . . . . . . . . 83
4.6.1 Standard Order of Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 4.7 Control Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 4.8 Meta-Call Predicates . . . . . . . . .