View
213
Download
0
Embed Size (px)
University of AmsterdamHuman-Computer Studies (HCS, formerly
SWI)Kruislaan 419, 1098 VA Amsterdam
The NetherlandsTel. (+31) 20 8884671
SWI-Prolog 5.5Reference Manual
Updated for version 5.5.21, July 2005
Jan Wielemakerwielemak@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, providinga powerful and bi-directional interface to C in an era this was unknown to other Prologimplementations. This environment is required to deal with XPCE, an object-orientedGUI 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 requirementsthat 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 19902005, University of Amsterdam
Contents
1 Introduction 91.1 SWI-Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1.1 Books about Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91.2 Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101.3 Compliance to the ISO standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101.4 Should you be using SWI-Prolog? . . . . . . . . . . . . . . . . . . . . . . . . . . . 101.5 The XPCE GUI system for Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . 111.6 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.6.1 Version 1.8 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 121.6.2 Version 1.9 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 131.6.3 Version 2.0 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 131.6.4 Version 2.5 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 131.6.5 Version 2.6 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 141.6.6 Version 2.7 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 141.6.7 Version 2.8 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 141.6.8 Version 2.9 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 141.6.9 Version 3.0 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 151.6.10 Version 3.1 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 151.6.11 Version 3.3 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 151.6.12 Version 3.4 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 161.6.13 Version 4.0 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 171.6.14 Version 5.0 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 171.6.15 Version 5.1 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 171.6.16 Version 5.2 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 171.6.17 Version 5.3 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 171.6.18 Version 5.4 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 171.6.19 Version 5.5 Release Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.7 Donate to the SWI-Prolog project . . . . . . . . . . . . . . . . . . . . . . . . . . . 181.8 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2 Overview 192.1 Getting started quickly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.1.1 Starting SWI-Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192.1.2 Executing a query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.2 The users initialisation file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202.3 Initialisation files and goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212.4 Command line options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212.5 GNU Emacs Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242.6 Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242.7 Query Substitutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
SWI-Prolog 5.5 Reference Manual
Contents 3
2.7.1 Limitations of the History System . . . . . . . . . . . . . . . . . . . . . . . 262.8 Reuse of toplevel bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262.9 Overview of the Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272.10 Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.10.1 During program development . . . . . . . . . . . . . . . . . . . . . . . . . 302.10.2 For running the result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.11 Environment Control (Prolog flags) . . . . . . . . . . . . . . . . . . . . . . . . . . 332.12 An overview of hook predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402.13 Automatic loading of libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412.14 Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432.15 Syntax Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.15.1 ISO Syntax Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432.16 Infinite trees (cyclic terms) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452.17 Wide character support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.17.1 Wide character encodings on streams . . . . . . . . . . . . . . . . . . . . . 452.18 System limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.18.1 Limits on memory areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472.18.2 Other Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492.18.3 Reserved Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3 Initialising and Managing a Prolog Project 503.1 The project source-files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.1.1 File Names and Locations . . . . . . . . . . . . . . . . . . . . . . . . . . . 503.1.2 Project Special Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513.1.3 International Sourcefiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.2 Using modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523.3 The test-edit-reload cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.3.1 Locating things to edit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533.3.2 Editing and incremental compilation . . . . . . . . . . . . . . . . . . . . . . 54
3.4 Using the PceEmacs built-in editor . . . . . . . . . . . . . . . . . . . . . . . . . . . 543.4.1 Activating PceEmacs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543.4.2 Bluffing through PceEmacs . . . . . . . . . . . . . . . . . . . . . . . . . . 543.4.3 Prolog Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
3.5 The Graphical Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593.5.1 Invoking the window-based debugger . . . . . . . . . . . . . . . . . . . . . 59
3.6 The Prolog Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593.7 Accessing the IDE from your program . . . . . . . . . . . . . . . . . . . . . . . . . 593.8 Summary of the iDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4 Built-in predicates 614.1 Notation of Predicate Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . 614.2 Character representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614.3 Loading Prolog source files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.3.1 Quick load files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 684.4 Listing and Editor Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 694.5 Verify Type of a Term . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714.6 Comparison and Unification or Terms . . . . . . . . . . . . . . . . . . . . . . . . . 72
SWI-Prolog 5.5 Reference Manual
4
4.6.1 Standard Order of Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724.7 Control Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744.8 Meta-Call Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754.9 ISO compliant Exception handling . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
4.9.1 Debugging and exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . 794.9.2 The exception term . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794.9.3 Printing messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
4.10 Handling signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814.10.1 Notes on signal handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
4.11 The block control-structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834.12 DCG Grammar rules . . . . . . . . . . . . . .