The XSB System Version 3.3.X Volume 1: Programmer's Manual
Total Page:16
File Type:pdf, Size:1020Kb
The XSB System Version 3.3.x Volume 1: Programmer’s Manual xsb Terrance Swift David S. Warren Konstantinos Sagonas Juliana Freire Prasad Rao Baoqiu Cui Ernie Johnson Luis de Castro Rui F. Marques Diptikalyan Saha Steve Dawson Michael Kifer July 4, 2013 Credits Day-to-day care and feeding of XSB including bug fixes, ports, and configuration management is currently done by David Warren and Terrance Swift with the help of Michael Kifer. In the past Kostis Sagonas, Prasad Rao, Steve Dawson, Juliana Freire, Ernie Johnson, Baoqiu Cui, Bart Demoen and Luis F. Castro have provided tremendous help. In Version 3.3, the core engine development of the SLG-WAM has been mainly implemented by Terrance Swift, Kostis Sagonas, Prasad Rao, Juliana Freire, Ernie Johnson, Luis Castro and Rui Marques. The breakdown, very roughly, was that Terrance Swift wrote the initial tabling engine, the SLG-WAM, and its built-ins; and leads the current development of the tabling subsystem. Prasad Rao reimplemented the engine’s tabling subsystem to use tries for variant-based table access and Ernie Johnson extended and refactored these routines in a num- ber of ways, including adding call subsumption. Kostis Sagonas implemented most of tabled negation. Juliana Freire revised the table scheduling mechanism starting from Version 1.5.0 to create the batched and local scheduling that is cur- rently used. Baoqiu Cui revised the data structures used to maintain delay lists, and added attributed variables to the engine. Luis Castro rewrote the emulator to use jump tables and wrote a heap-garbage collector for the SLG-WAM. Rui Marques was responsible for the concurrency control algorithms used for shared tables, and mainly responsible for making the XSB engine multi-threaded. The incremental table maintenance subsystem was designed and implemented by Diptikalyan Saha. Other engine work includes the following. Memory expansion code for WAM stacks was written by Ernie Johnson, Bart Demoen and David S. Warren. Heap garbage collection was written by Luis de Castro, Kostis Sagonas and Bart Demoen. Atom space garbage collection was written by David Warren; table garbage collection was written by Terrance Swift based in part on space recla- mation code written by Prasad Rao. Rui Marques 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; it significantly revised by David S. Warren, who added alternative, multiple, and star indexing and by Terrance Swift who im- plemented dynamic clause garbage collection. Trie assert/retract code, and trie interning code was written by Prasad Rao, as was most code for reclaiming table space. The current version of findall/3 was re-written from scratch by Bart Demoen, as was XSB’s throw and catch mechanism. 64-bit floats were added by Charles Rojo. Walter Wilson has written several of XSB’s builtin predicates. In terms of core system Prolog code, Kostis Sagonas was responsible for HiLog compilation and associated built-ins as well as coding or revising many standard predicates. Steve Dawson implemented Unification Factoring. The revision of 2 XSB’s I/O into ISO-compatible streams was done by Michael Kifer and Terrance Swift. The auto_table and suppl_table directives were written by Kostis Sagonas. The DCG expansion module was written by Kostis Sagonas for non- tabled code and by Baoqiu Cui, Terrance Swift and David Warren for tabled code. The handling of the multifile directive was written by Baoqiu Cui. C.R. Ramakrishnan wrote the mode analyzer for XSB. Michael Kifer implemented the storage module. The multi-threaded API was written by Terrance Swift and Rui Marques. Michael Kifer has been in charge of XSB’s installation procedures, rewriting parts of the XSB code to make XSB configurable with GNU’s Autoconf, im- plementing XSB’s package system, and integrated GPP with XSB’s compiler. GPP, the source code preprocessor used by XSB, was written by Denis Auroux, who 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 on Prolog clause indexing for SB-Prolog, to Richard O’Keefe, who contributed the Prolog code for the Prolog reader and the C code for the tokenizer, and to Ciao Prolog whose write_term/[2,3] we use. ... Now what did I forget this time ? Contents 1 Introduction 1 1.1 Using This Manual ................................ 6 2 Getting Started with XSB 7 2.1 Installing XSB under UNIX ........................... 7 2.1.1 Possible Installation Problems ...................... 11 2.2 Installing XSB under Windows ......................... 12 2.2.1 Using Cygnus Software’s CygWin32 .................. 12 2.2.2 Using Microsoft Visual C++ ...................... 12 2.3 Invoking XSB ................................... 15 2.4 Compiling XSB programs ............................ 16 2.5 Sample XSB Programs .............................. 16 2.6 Exiting XSB ................................... 17 3 System Description 18 3.1 Entering and Exiting XSB from the Command Line ............. 18 3.2 The System and its Directories ......................... 19 3.3 How XSB Finds Files: Source File Designators ................ 20 3.4 The Module System of XSB ........................... 21 3.5 Standard Predicates in XSB ........................... 28 3.6 The Dynamic Loader and its Search Path ................... 28 3.6.1 Changing the Default Search Path and the Packaging System . 28 3.6.2 Dynamically loading predicates in the interpreter ........... 31 i CONTENTS ii 3.7 Command Line Arguments ........................... 31 3.8 Memory Management .............................. 36 3.9 Compiling, Consulting, and Loading ...................... 37 3.9.1 Static Code ................................ 37 3.9.2 Dynamic Code .............................. 39 3.9.3 The multifile directive .......................... 39 3.10 The Compiler ................................... 40 3.10.1 Invoking the Compiler .......................... 40 3.10.2 Compiler Options ............................ 42 3.10.3 Specialization ............................... 48 3.10.4 Compiler Directives ........................... 50 3.10.5 Inline Predicates ............................. 56 3.11 A Note on ISO Compatibility .......................... 57 4 Syntax 59 4.1 Terms ....................................... 59 4.1.1 Integers .................................. 59 4.1.2 Floating-point Numbers ......................... 60 4.1.3 Atoms ................................... 60 4.1.4 Variables ................................. 61 4.1.5 Compound Terms ............................ 61 4.1.6 Lists .................................... 63 4.2 From HiLog to Prolog .............................. 64 4.3 Operators ..................................... 66 5 Using Tabling in XSB: A Tutorial Introduction 70 5.1 Tabling in the Context of a Prolog System ................... 70 5.2 Definite Programs ................................ 71 5.2.1 Call Variance vs. Call Subsumption .................. 75 5.2.2 Table Scheduling Strategies ....................... 77 5.2.3 Interaction Between Prolog Constructs and Tabling ......... 79 CONTENTS iii 5.2.4 Potential Pitfalls in Tabling ....................... 82 5.3 Normal Programs ................................. 84 5.3.1 Stratified Normal Programs ....................... 84 5.3.2 Non-stratified Programs ......................... 87 5.3.3 On Beyond Zebra: Implementing Other Semantics for Non-stratified Programs 91 5.4 Answer Subsumption ............................... 93 5.4.1 Types of Answer Subsumption ..................... 93 5.4.2 Examples of Answer Subsumption ................... 95 5.4.3 Term-Sets ................................. 98 5.5 Tabling for Termination ............................. 101 5.5.1 Subgoal Abstraction ........................... 102 5.5.2 Bounded Rationality through Radial Restraint ............ 104 5.6 Incremental Table Maintenance ......................... 106 5.6.1 Examples ................................. 106 5.6.2 Incremental Tabling using Interned Tries ............... 111 5.6.3 View Consistency ............................. 112 5.6.4 Summary and Implementation Status ................. 113 5.6.5 Predicates for Incremental Table Maintenance ............. 113 5.7 Compatability of Tabling Modes and Predicate Attributes .......... 119 6 Standard and General Predicates 122 6.1 Input and Output ................................ 122 6.1.1 I/O Stream Implementation ....................... 123 6.1.2 ISO Streams ............................... 124 6.1.3 DEC-IO Style File Handling ....................... 131 6.1.4 Character I/O .............................. 133 6.1.5 Term I/O ................................. 137 6.1.6 Special I/O ................................ 145 6.2 Interactions with the Operating System .................... 151 6.2.1 The path_sysop/2 interface ....................... 153 CONTENTS iv 6.3 Evaluating Arithmetic Expressions through is/2 ............... 155 6.3.1 Evaluable Functors for Arithmetic Expressions ............ 156 6.4 Convenience .................................... 159 6.5 Negation and Control .............................. 160 6.6 Unification and Comparison of Terms ..................... 163 6.6.1 Sorting of Terms ............................. 169 6.7 Meta-Logical ................................... 170 6.8 Cyclic Terms ..................................