The XSB System Version 2.4 Volume 1: Programmer's Manual
Total Page:16
File Type:pdf, Size:1020Kb
The XSB System Version 2.4 Volume 1: Programmer’s Manual Konstantinos Sagonas Terrance Swift David S. Warren Juliana Freire Prasad Rao Baoqiu Cui Ernie Johnson with contributions from Steve Dawson Michael Kifer July 13, 2001 Credits Day-to-day care and feeding of XSB including bug fixes, ports, and configuration man- agement has been done by Kostis Sagonas, David Warren, Terrance Swift, Prasad Rao, Steve Dawson, Juliana Freire, Ernie Johnson, Baoqiu Cui, Michael Kifer, and Bart Demoen. In Version 2.4, the core engine development of the SLG-WAM has been mainly imple- mented by Terrance Swift, Kostis Sagonas, Prasad Rao, and Juliana Freire. The break- down, roughly, was that Terrance Swift wrote the initial tabling engine and builtins. Prasad Rao reimplemented the engine’s tabling subsystem to use tries for variant-based table access while Kostis Sagonas implemented most of tabled negation. Juliana Freire revised the table scheduling mechanism starting from Version 1.5.0 to create a more efficient engine, and implemented the engine for local evaluation. Starting from XSB Version 2.0, XSB includes another tabling engine, CHAT, which was designed and developed by Kostis Sagonas and Bart Demoen. CHAT supports heap garbage collection (both based on a mark&slide and on a mark© algorithm) which was developed and implemented by Bart Demoen and Kostis Sagonas. Memory expansion code for WAM stacks was written by Ernie Johnson and Bart De- moen, while memory management code for CHAT areas was written by Bart Demoen and Kostis Sagonas. Rui Marques improved the trailing of the SLG-WAM and rewrote much of the engine to make it compliant with 64-bit architectures. Assert and retract code was based on code written by Jiyang Xu and significantly revised by David S. Warren and Rui Marques. Trie assert and retract code was written by Prasad Rao. The current version of findall/3 was re-written from scratch by Bart Demoen. In Version 2.3, the tabling subsystem and its interface to the engine have been extended for the support of subsumption-based tabling. This extension was developed and im- plemented by Ernie Johnson. In the XSB complier, Kostis Sagonas was responsible for HiLog compilation and associ- ated builtins. Steve Dawson implemented Unification Factoring. The auto table and suppl table directives were written by Kostis Sagonas. The DCG expansion module was written by Kostis Sagonas. The handling of the multifile directive was written by Baoqiu Cui. C.R. Ramakrishnan wrote the mode analyzer for XSB. The safety check for tabling within the scope of cuts was written by Kate Dvortsova. Michael Kifer rewrote parts of the XSB code to make XSB configurable with GNU’s Autoconf. Harald Schroepfer helped the XSB group with the Solaris port, and Yiorgos Adamopoulos suggested the bits to use for the HP-700 series port. Steven Dawson, Larry B. Daniel and Franklin Chen were responsible for the MkLinux and Solaris x86 ports. GPP, the source code preprocessor used by XSB, was written by Denis Auroux. He also wrote the GPP manual reproduced in Appendix A. The starting point of XSB (in 1990) was PSB-Prolog 2.0 by Jiyang Xu. PSB-Prolog in its turn was based on SB-Prolog, primarily designed and written by Saumya Debray, David S. Warren, and Jiyang Xu. Thanks are also due to Weidong Chen for his work 2 on Prolog clause indexing for SB-Prolog and to Richard O’Keefe, who contributed the Prolog code for the Prolog reader and the C code for the tokenizer. Contents 1 Introduction 1 2 Getting Started with XSB 5 2.1 Installing XSB under UNIX ................................ 5 2.1.1 Possible Installation Problems ........................... 7 2.2 Installing XSB under Windows .............................. 8 2.2.1 Using Cygnus Software’s CygWin32 ....................... 8 2.2.2 Using Microsoft Visual C++ ........................... 8 2.3 Invoking XSB ........................................ 9 2.4 Compiling XSB programs ................................. 10 2.5 Sample XSB Programs ................................... 10 2.6 Exiting XSB ........................................ 11 3 System Description 12 3.1 Entering and Exiting XSB ................................. 12 3.2 The System and its Directories .............................. 13 3.3 The Module System of XSB ................................ 13 3.4 The Dynamic Loader and its Search Path ........................ 16 3.4.1 Changing the Default Search Path and the Packaging System ......... 16 3.4.2 Dynamically loading predicates in the interpreter ................ 18 3.5 Command Line Arguments ................................ 18 3.6 Memory Management ................................... 22 3.7 Compiling and Consulting ................................. 22 3.8 The Compiler ........................................ 24 3.8.1 Invoking the Compiler ............................... 24 i CONTENTS ii 3.8.2 Compiler Options ................................. 25 3.8.3 Specialization .................................... 29 3.8.4 Compiler Directives ................................ 31 3.8.5 Inline Predicates .................................. 36 4 Syntax 37 4.1 Terms ............................................ 37 4.1.1 Integers ....................................... 37 4.1.2 Floating-point Numbers .............................. 38 4.1.3 Atoms ........................................ 38 4.1.4 Variables ...................................... 39 4.1.5 Compound Terms ................................. 39 4.1.6 Lists ......................................... 40 4.2 From HiLog to Prolog ................................... 42 4.3 Operators .......................................... 43 5 Using Tabling in XSB: A Tutorial Introduction 47 5.1 XSB as a Prolog System .................................. 47 5.2 Definite Programs ..................................... 48 5.2.1 Tabling Strategies ................................. 49 5.2.2 Tabling Directives and Declarations ....................... 50 5.2.3 Interaction Between Prolog Constructs and Tabling .............. 53 5.2.4 Potential Pitfalls in Tabling ............................ 55 5.3 Normal Programs ...................................... 56 5.3.1 Stratified Normal Programs ............................ 56 5.3.2 Non-stratified Programs .............................. 58 5.3.3 On Beyond Zebra: Implementing Other Semantics for Non-stratified Programs 62 5.4 Tabled Aggregation ..................................... 64 5.4.1 Local Evaluation .................................. 66 6 Standard Predicates 67 6.1 Input and Output ..................................... 67 6.1.1 File Handling .................................... 67 CONTENTS iii 6.1.2 Character I/O ................................... 69 6.1.3 Term I/O ...................................... 70 6.2 Convenience ......................................... 74 6.3 Negation and Control ................................... 74 6.4 Meta-Logical ........................................ 76 6.5 All Solutions and Aggregate Predicates .......................... 90 6.5.1 Tabling Aggregate Predicates ........................... 92 6.6 Comparison ......................................... 96 6.7 Meta-Predicates ...................................... 98 6.8 Information about the State of the Program ....................... 99 6.9 Modification of the Database ............................... 110 6.9.1 The storage Module: Associative Arrays and Backtrackable Updates .... 113 6.10 Execution State ....................................... 115 6.11 Exception Handling .................................... 119 6.12 Tabled Predicate Manipulations .............................. 120 6.12.1 Operators for Declaring and Modifying Tabled Predicates ........... 122 6.12.2 Predicates for Table Inspection .......................... 122 6.12.3 Deleting Tables and Table Components ..................... 130 7 Hooks 131 7.1 Adding and Removing Hooks ............................... 131 7.2 Hooks Supported by XSB ................................. 132 8 Debugging 135 8.1 High-Level Tracing ..................................... 135 8.2 Low-Level Tracing ..................................... 138 9 Definite Clause Grammars 140 9.1 General Description .................................... 140 9.2 Translation of Definite Clause Grammar rules ...................... 141 9.2.1 Definite Clause Grammars and Tabling ..................... 143 9.3 Definite Clause Grammar predicates ........................... 144 9.4 Two differences with other Prologs ............................ 146 CONTENTS iv 10 Restrictions and Current Known Bugs 148 10.1 Current Restrictions .................................... 148 10.2 Known Bugs ........................................ 149 A GPP - Generic Preprocessor 151 A.1 Description ......................................... 151 A.2 Syntax ............................................ 152 A.3 Options ........................................... 152 A.4 Syntax Specification .................................... 155 A.5 Evaluation Rules ...................................... 158 A.6 Meta-macros ........................................ 159 A.7 Examples .......................................... 162 A.8 Advanced Examples .................................... 167 A.9 Author ............................................ 169 Chapter 1 Introduction XSB is a research-oriented Logic Programming system for Unix and Windows-based systems. In addition to providing all the functionality of Prolog, XSB contains several features not usually found in Logic Programming systems, including • Evaluation according