SWI-Prolog 3 - UWA SWI-Prolog has been designed and implemented to get a Prolog implementation which

  • View
    1

  • Download
    0

Embed Size (px)

Text of SWI-Prolog 3 - UWA SWI-Prolog has been designed and implemented to get a Prolog implementation which

  • University of Amsterdam Dept. of Social Science Informatics (SWI)

    Roeterstraat 15, 1018 WB Amsterdam The Netherlands

    Tel. (+31) 20 5256121 SSIIWW SWI-Prolog 3.3

    Reference Manual Updated for version 3.3.7, June 2000

    Jan Wielemaker

    jan@swi.psy.uva.nl http://www.swi.psy.uva.nl/projects/SWI-Prolog/

    SWI-Prolog is a Prolog implementation based on a subset of the WAM (Warren Abstract 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–2000, University of Amsterdam

  • Contents

    1 Introduction 7 1.1 SWI-Prolog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

    1.1.1 Other books about Prolog. . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.2 Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.3 Compliance to the ISO standard. . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.4 Should you be using SWI-Prolog?. . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.5 The XPCE GUI system for Prolog. . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.6 Release Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    1.6.1 Version 1.8 Release Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.6.2 Version 1.9 Release Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.6.3 Version 2.0 Release Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.6.4 Version 2.5 Release Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.6.5 Version 2.6 Release Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.6.6 Version 2.7 Release Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.6.7 Version 2.8 Release Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.6.8 Version 2.9 Release Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.6.9 Version 3.0 Release Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.6.10 Version 3.1 Release Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.6.11 Version 3.3 Release Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . 13

    1.7 Acknowledgements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

    2 Overview 16 2.1 Getting started quickly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

    2.1.1 Starting SWI-Prolog. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.1.2 Executing a query. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

    2.2 The user’s initialisation file. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.3 Initialisation files and goals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2.4 Command line options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.5 GNU Emacs Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.6 Online Help. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.7 Query Substitutions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

    2.7.1 Limitations of the History System. . . . . . . . . . . . . . . . . . . . . . . 22 2.8 Reuse of toplevel bindings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.9 Overview of the Debugger. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.10 Compilation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

    2.10.1 During program development. . . . . . . . . . . . . . . . . . . . . . . . . 26 2.10.2 For running the result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

    2.11 Environment Control (Prolog flags). . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.12 An overview of hook predicates. . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 2.13 Automatic loading of libraries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

    SWI-Prolog 3.3 Reference Manual

  • Contents 3

    2.14 Garbage Collection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2.15 Syntax Notes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

    2.15.1 ISO Syntax Support. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2.16 System limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

    2.16.1 Limits on memory areas. . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2.16.2 Other Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2.16.3 Reserved Names. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

    3 Built-in predicates 39 3.1 Notation of Predicate Descriptions. . . . . . . . . . . . . . . . . . . . . . . . . . . 39 3.2 Character representation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 3.3 Loading Prolog source files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

    3.3.1 Quick load files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3.4 Listing and Editor Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3.5 Verify Type of a Term. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3.6 Comparison and Unification or Terms. . . . . . . . . . . . . . . . . . . . . . . . . 48

    3.6.1 Standard Order of Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 3.7 Control Predicates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 3.8 Meta-Call Predicates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.9 ISO compliant Exception handling. . . . . . . . . . . . . . . . . . . . . . . . . . . 53

    3.9.1 Debugging and exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . 53 3.9.2 The exception term. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 3.9.3 Printing messages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

    3.10 Handling signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 3.10.1 Notes on signal handling. . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

    3.11 The ‘block’ control-structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3.12 DCG Grammar rules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3.13 Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

    3.13.1 Update view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 3.13.2 Indexing databases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

    3.14 Declaring predicates properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 3.15 Examining the program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 3.16 Input and output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

    3.16.1 Input and output using implicit source and destination. . . . . . . . . . . . 66 3.16.2 Explicit Input and Output Streams. . . . . . . . . . . . . . . . . . . . . . . 67 3.16.3 Switching Between Implicit and Explicit I/O. . . . . . . . . . . . . . . . . 70

    3.17 Status of streams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 3.18 Primitive character I/O. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 3.19 Term reading and writing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 3.20 Analysing and Constructing Terms. . . . . . . . . . . . . . . . . . . . . . . . . . . 78 3.21 Analysing and constructing atoms. . . . . . . . . . . . . . . . . . . . . . . . . . . 79 3.22 Classifying characters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 3.23 Representing text in strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 3.24 Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 3.25 Character Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 3.26 Arithmetic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 3.27 Arithmetic Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

    SWI-Prolog 3.3 Reference Manual

  • 4

    3.28 Adding Arithmetic Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 3.29 List Manipulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 3.30 Set Manipulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 3.31 Sorting Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 3.32 Finding all Solutions to a Goal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 3.33 Invoking Predicates on all Members of a List. . . . . . . . . . . . . . . . . . . . . 94 3.34 Forall . . . . . . . . . . . . . . . .