Local Copy of the SWI Prolog Manual, Version 7.2.3 (August 2015)
Total Page:16
File Type:pdf, Size:1020Kb
VU University Amsterdam University of Amsterdam De Boelelaan 1081a, 1081 HV Amsterdam Kruislaan 419, 1098 VA Amsterdam The Netherlands The Netherlands ˜ Reference Manual Updated for version 7.2.3, August 2015 Jan Wielemaker [email protected] http://www.swi-prolog.org SWI-Prolog is a comprehensive and portable implementation of the Prolog programming language. SWI-Prolog aims to be a robust and scalable implementation supporting a wide range of applications. In particular, it ships with a wide range of interface libraries, providing interfaces to other languages, databases, graphics and networking. It provides extensive support for managing HTML/SGML/XML and RDF documents. The system is particularly suited for server applications due to robust support for multithreading and HTTP server libraries. SWI-Prolog is designed in the ‘Edinburgh tradition’. In addition to the ISO Prolog stan- dard it is largely compatible to Quintus, SICStus and YAP Prolog. SWI-Prolog provides a compatibility framework developed in cooperation with YAP and instantiated for YAP, SICStus and IF/Prolog. SWI-Prolog aims at providing a good development environment, including extensive ed- itor support, graphical source-level debugger, autoloading and ‘make’ facility and much more. SWI-Prolog editor and the PDT plugin for Eclipse provide alternative environ- ments. This document gives an overview of the features, system limits and built-in predicates. ˜ This work is licensed under the Creative Commons Attribution- ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send a let- ter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. Contents 1 Introduction 10 1.1 Positioning SWI-Prolog................................. 10 1.2 Status and releases................................... 11 1.3 Should I be using SWI-Prolog?............................. 11 1.4 Support the SWI-Prolog project............................ 12 1.5 Implementation history................................. 13 1.6 Acknowledgements................................... 13 2 Overview 15 2.1 Getting started quickly................................. 15 2.1.1 Starting SWI-Prolog.............................. 15 2.1.2 Executing a query............................... 16 2.2 The user’s initialisation file............................... 16 2.3 Initialisation files and goals............................... 17 2.4 Command line options................................. 17 2.4.1 Informational command line options...................... 18 2.4.2 Command line options for running Prolog................... 18 2.4.3 Controlling the stack sizes........................... 20 2.4.4 Running goals from the command line..................... 21 2.4.5 Compilation options.............................. 21 2.4.6 Maintenance options.............................. 21 2.5 GNU Emacs Interface.................................. 21 2.6 Online Help....................................... 22 2.7 Command line history.................................. 23 2.8 Reuse of top-level bindings............................... 23 2.9 Overview of the Debugger............................... 24 2.10 Compilation....................................... 27 2.10.1 During program development......................... 27 2.10.2 For running the result.............................. 28 2.11 Environment Control (Prolog flags).......................... 30 2.12 An overview of hook predicates............................ 42 2.13 Automatic loading of libraries............................. 44 2.14 Garbage Collection................................... 45 2.15 The SWI-Prolog syntax................................. 45 2.15.1 ISO Syntax Support.............................. 46 2.15.2 Nested comments................................ 46 2.16 Rational trees (cyclic terms).............................. 51 2.17 Just-in-time clause indexing.............................. 51 2.17.1 Future directions................................ 52 2.17.2 Indexing and portability............................ 52 SWI-Prolog 7.2 Reference Manual 2 2.18 Wide character support................................. 53 2.18.1 Wide character encodings on streams..................... 53 2.19 System limits...................................... 54 2.19.1 Limits on memory areas............................ 54 2.19.2 Other Limits.................................. 55 2.19.3 Reserved Names................................ 57 2.20 SWI-Prolog and 64-bit machines............................ 57 2.20.1 Supported platforms.............................. 57 2.20.2 Comparing 32- and 64-bits Prolog....................... 57 2.20.3 Choosing between 32- and 64-bit Prolog.................... 58 3 Initialising and Managing a Prolog Project 59 3.1 The project source files................................. 59 3.1.1 File Names and Locations........................... 59 3.1.2 Project Special Files.............................. 60 3.1.3 International source files............................ 61 3.2 Using modules..................................... 61 3.3 The test-edit-reload cycle................................ 62 3.3.1 Locating things to edit............................. 62 3.3.2 Editing and incremental compilation...................... 63 3.4 Using the PceEmacs built-in editor........................... 63 3.4.1 Activating PceEmacs.............................. 63 3.4.2 Bluffing through PceEmacs.......................... 64 3.4.3 Prolog Mode.................................. 66 3.5 The Graphical Debugger................................ 68 3.5.1 Invoking the window-based debugger..................... 68 3.6 The Prolog Navigator.................................. 69 3.7 Cross-referencer..................................... 69 3.8 Accessing the IDE from your program......................... 71 3.9 Summary of the IDE.................................. 72 4 Built-in Predicates 73 4.1 Notation of Predicate Descriptions........................... 73 4.2 Character representation................................ 74 4.3 Loading Prolog source files............................... 75 4.3.1 Conditional compilation and program transformation............. 85 4.3.2 Loading files, active code and threads..................... 88 4.3.3 Quick load files................................. 90 4.4 Editor Interface..................................... 91 4.4.1 Customizing the editor interface........................ 91 4.5 List the program, predicates or clauses......................... 92 4.6 Verify Type of a Term.................................. 93 4.7 Comparison and Unification of Terms......................... 95 4.7.1 Standard Order of Terms............................ 95 4.7.2 Special unification and comparison predicates................. 96 4.8 Control Predicates.................................... 98 4.9 Meta-Call Predicates.................................. 100 SWI-Prolog 7.2 Reference Manual Contents 3 4.10 ISO compliant Exception handling........................... 104 4.10.1 Debugging and exceptions........................... 105 4.10.2 The exception term............................... 105 4.10.3 Printing messages............................... 105 4.11 Handling signals.................................... 110 4.11.1 Notes on signal handling............................ 111 4.12 DCG Grammar rules.................................. 111 4.13 Database......................................... 114 4.13.1 Update view.................................. 117 4.13.2 Indexing databases............................... 117 4.14 Declaring predicate properties............................. 118 4.15 Examining the program................................. 119 4.16 Input and output..................................... 124 4.16.1 Predefined stream aliases............................ 124 4.16.2 ISO Input and Output Streams......................... 125 4.16.3 Edinburgh-style I/O............................... 133 4.16.4 Switching between Edinburgh and ISO I/O.................. 135 4.16.5 Write onto atoms, code-lists, etc......................... 135 4.17 Status of streams.................................... 136 4.18 Primitive character I/O................................. 137 4.19 Term reading and writing................................ 140 4.20 Analysing and Constructing Terms........................... 148 4.20.1 Non-logical operations on terms........................ 151 4.21 Analysing and Constructing Atoms........................... 152 4.22 Localization (locale) support.............................. 155 4.23 Character properties................................... 157 4.23.1 Case conversion................................ 159 4.23.2 White space normalization........................... 159 4.23.3 Language-specific comparison......................... 159 4.24 Operators........................................ 160 4.25 Character Conversion.................................. 161 4.26 Arithmetic........................................ 162 4.26.1 Special purpose integer arithmetic....................... 162 4.26.2 General purpose arithmetic........................... 163 4.27 Misc arithmetic support predicates........................... 172 4.28 Built-in list operations................................. 173 4.29 Finding all Solutions to a Goal............................. 176 4.30 Forall.......................................... 177 4.31 Formatted Write..................................... 178 4.31.1 Writef...................................... 178 4.31.2 Format....................................