Beyond the WAM: Topics in Contemporary Logic Programming Implementation
Total Page:16
File Type:pdf, Size:1020Kb
Beyond the WAM: Topics in Contemporary Logic Programming Implementation Terrance Swift May 4, 2012 Terrance Swift Topics in Logic Programming Implementation Beyond the WAM: Topics in Contemporary Logic Programming Implementation Topic 1: Overview and Miscellaneous Topics Terrance Swift Topics in Logic Programming Implementation After the WAM There are a lot of Prologs around: Sicstus, Quintus, YAP, Chao, SWI, GNU Prolog, B-Prolog, Mercury, HAL, ALS, XSB and many others Nearly every Prolog implements the WAM The WAM has been known for over 20 years ... so why are there a lot of Prologs? Terrance Swift Topics in Logic Programming Implementation After the WAM There is a lot more to Prolog than just the WAM Builtins for I/O, term manipulation, DCGs etc. Module systems Debuggers Dynamic code Libraries, interfaces, and packages: e.g. Interprolog [Cal04], lpdoc (Ciao [HtCG]), pl-doc (SWI) jasper, database interfaces, and many libraries, interfaces, etc. APIs for embeddability Memory management (garbage collectors) Arbitrary arithmetic { rational numbers Unicode and other extensions to readers Still, the main ISO standard has been around for 10 years [ISO95] Terrance Swift Topics in Logic Programming Implementation After the WAM Much of what keeps many Prologs around is research: Speed optimizations faster WAM execution (e.g. YAP [SC99, dSC06]) native-code compilation [Tay01, van92] (e.g. GNU Prolog [DP01b]) global analysis (e.g Ciao [MCH04], XSB [DRSS96]), indexing (e.g. YAP [], XSB [RRS+99]) Annotations and analysis for correctness Constraint libraries: CLP(R), CLP(Q), CLP(FD), CLP(BN) [CC03], Various libraries based on Constraint Handling Rules. Parallelism, multi-threading, and \cluster" Prologs Changes to evaluation methods (e.g. tabling) Syntactic extensions: e.g. HiLog [CKW93, SW95] Semantic extensions: well-founded negation, Generalized Annotated Programs, Object-logics such as FLORA-2, preference logics, etc. Integration with other LP paradigms such as ASP (e.g. XASP [CSW, ARD08]),Terrance abduction, Swift Topics etc. in Logic Programming Implementation Focus of This Topic We'll discuss implementation issues arising from Making the WAM into a modern Prolog system Extending Prolog into new directions Focus is on the XSB system (xsb.sourceforge.net) It is a widely used Prolog with approximately 10,000 downloads from site a year since 2000, plus inclusions as a package in other systems { e.g. Debian Linux It has most of the advanced features that other Prologs have It has some advanced features of its own It is partly a UNL product I know it. Terrance Swift Topics in Logic Programming Implementation Tentative Overview 1 Overview: Exception Handling and a Taste of Speeding up the WAM 2 Tabling for Definite Programs: Variance and Subsumption; Scheduling Strategies 3 The SLG-WAM: Data Structures and Stack Changes; 4 The SLG-WAM: Completion, Registers and Instructions; Linear tabling 5 Tabling with Negation: Stratification Theories, SLG Resolution 6 The SLG-WAM: Implementation of Tabled Negation 7 Multi-threaded Prolog: ISO-multi-threading, the MT-SLG-WAM; Concurrent Completion and Shared Completed Tables, YAP Tabling. 8 Dynamic Rules and Facts: Incremental Tabling 9 Constraints and CHR; Tabled constraints; Answer Subsumption Terrance Swift Topics in Logic Programming Implementation XSB Background As a poor measure of system complexity XSB has: ~ 84,000 lines of C code in engine and support for builtins ~ 14,000 Lines of Prolog code in compiler ~ 37,000 Lines of Prolog code for builtins and standard libraries ~ 70,000 Lines of Prolog and C for packages (including constraint libraries) This is roughly the same as other leading open-source Prologs Terrance Swift Topics in Logic Programming Implementation XSB Background XSB is currently developed at Universidade Nova de Lisboa and State University of New York at Stony Brook. WAM has Code Space, Heap, Environment Stack, Trail and PDL Prolog systems typically contain other areas: Atom Tables, predicate/structure tables, findall buffers, and much else. XSB (and some other Prologs) splits Environment Stack into Environment Stack and Choice Point Stack The reason for the split is to make it easier to use the choice point stack for scheduling in tabled evaluations Terrance Swift Topics in Logic Programming Implementation XSB Background The code and algorithms discussed in these lectures were developed by (in alphabetical order) Luis de Castro, Baoqiu Cui, Steve Dawson, Ernie Johnson, Juliana Freire, Michael Kifer, Rui F. Marques, C.R. Ramakrishnan, I.V. Ramakrishnan, Prasad Rao, Konstantinos Sagonas, Diptikalyan Saha, Terrance Swift, David S. Warren and others. Terrance Swift Topics in Logic Programming Implementation Exceptions How do you handle exceptions in C? 1 Declare a C variable of type jmp buf 2 use setjmp(jmp buf env) to assign a forward continuation (i.e. environment) referred to by env For example, if you wanted to longjump to the WAM instruction dispatch loop: if (setjmp(xsb abort fallback environment)) Restore the default signal handling; set P register to saved value else P = first instruction of boot module goto *instr addr table[*P]; at beginning of instruction dispatch loop. 3 use longjmp(xsb abort fallback environment,val) to pop C stack to proper environment | the instruction goes back in stack to look for an environment with address matching that maintained in the jmp buf env. Terrance Swift Topics in Logic Programming Implementation Exceptions ISO-style exceptions are an elegant part of Prolog and are often under-appreciated1 To throw an exception, call throw(Ball): Ball is simply a Prolog term To handle an exception, call a goal through catch(Goal,Ball,Handler) This calls Goal and if Goal throws an exception (via throw(Ball)) that unifies with Ball, call Handler if Goal throws an exception that does not unify with Ball nothing happens, the exception is handled by an ancestor catch Thus, there are three continuations in Prolog: forward (success), failure, and exception. Note that continuations may be removed via !/0. catch/3 important when a Prolog is part of a system: e.g. called from Interprolog, from within a Ruby, Delphi, or C process. 1Exceptions in XSB wereTerrance originally Swift implementedTopics in Logic by Programming B. Demoen Implementation Exceptions How can exceptions be implemented? catch/3 is a little like a setjmp(), but relies on unification of Prolog terms, rather than matches of stack addresses. Go up Prolog's environment stack until you find an environment Env that looks like a catch. Recall that WAM environments contain: A pointer to their parent environment A pointer to the byte-code for their continuation Various permanent variables (+ perhaps another pointer for Tabling) See if the thrown Ball unifies with something in Env if so reset stacks and call Handler if not, keep going up stack Terrance Swift Topics in Logic Programming Implementation Exceptions Here's how we find the environment of a catch catch marker indicates that the current environment is that of catch/3 We aren't yet addressing the unification of the ball with the second argument of catch/3 Terrance Swift Topics in Logic Programming Implementation Exceptions find next catch() temp E = E find an environment that matches catch while (E and cp(E) != catch marker) temp E = parentEnv(E) if ( ! temp E) xsb exit("Throw failed because no catcher for throw") search through choice points until you find the choice point whose cp E value is older than temp E set B to this value search through choice points until you find the trail cell equal to cp TR(B) set TR to this value return(FALSE); Terrance Swift Topics in Logic Programming Implementation Exceptions After interning the error ball, throw/1 finds the next catch/3 environment (if any) Stacks are set to this new environment so that when we fail (which acts analogously to longjmp()), we'll take the failure continuation for catch/3 Recall from a few slides ago that xsb abort fallback environment puts you back to the beginning of the emulator. throw(error term) Intern error term as exception ball(thread,error term) if (find next catch()) xsb exit(”find next catch() failed in xsb throw internal!") /* Resume main emulator instruction loop */ longjmp(xsb abort fallback environment, fail inst) Terrance Swift Topics in Logic Programming Implementation Exceptions catch(Goal, Catcher, Handler) :- set variable catch marker to point to clean up block call(Goal), clean up block. catch( Goal,Catcher,Handler) :- xsb thread self(T), '$$exception ball'(T,Ball), % one fact per thread here so no CP ( Ball = Catcher -> retractall('exception ball'( )), call(Handler) ; find next catch() ). A call to catch/3 first makes sure that catch marker points to clean up block (this actually needs to be done at initialization). If find next catch() traverses the environment for a call to catch/3 it will set the environment and choice point stacks, the heap and trail to that environment, and succeeds. If the thrown Ball unifies with Catcher, Handler will be called; othewise find next catch() will be called again. When there are no choice points created by Goal, clean up block removes the choice point set up by catch/3. Terrance Swift Topics in Logic Programming Implementation Catching vs. Cutting catch/3 finds a previous enviroment and resets stack to that enviroment. After the environment is reset, it calls a handler and succeeds (if the handler succeeds); !/0 finds a previous choice point C, and resets stacks to the enviroment in C by failing. Once the enviroment is reset, a failure continuation is taken. Terrance Swift Topics in Logic Programming Implementation Cleaning Up after a Goal How do you guarentee that a given handler is always called after a goal { whether the goal succeeds (with no more choice points), fails, or throws an exception E.g. to close a db cursor, mutex, etc. As described, catch/3 handles exceptions This is usually done via call cleanup(#Goal,#Handler) Failure of Goal: ?- call_cleanup(fail,writeln(failed(Goal))).