UMBC CMSC 771 Knowledge representation and reasoning
offerings: 97 99 00 01 | resources | software | papers | hw | help | mail

Using XSB at UMBC

adapted from David Warren's notes from SUNY Stony Brook.

The XSB Prolog dialect uses the standard Edinburgh syntax for Prolog. It also extends the Prolog language with tabling, which allows more Prolog programs to run without looping. There is a homepage for the XSB project and Volume 1 and Volume 2 of the XSB manual, which you can access. (That documentation describes the entire XSB system and is much larger than you'll probably want to print out. There is a postscript version available through the XSB homepage, if you really need it.). We've collected some examples of simple XSB programs which you should look at.

The basics

You can run the XSB system on the CSEE departmental network's linux machines (e.g. linuxserver1.cs.umbc.edu) on a Sun putting /cs/bin in your path and then executing the command xsb . XSB is also avaliable in a version for windows and can be downloaded from the XSB project homepage.

When you execute it, it responds with something like:

  [xsb_configuration loaded]
  [sysinitrc loaded]
  [packaging loaded]

  XSB Version 2.01 (Gouden Carolus) of August 20, 1999
  [sparc-sun-solaris2.6; mode: optimal; engine: slg-wam; scheduling: batched]
  | ?- 

This is the top-level reader, and it is ready to evaluate a query (or command). The procedure is for you first to define predicates (i.e., relations) by consulting or compiling them, and then to call them.

To define a set of predicates, first create a file containing the rules that define them. The file name should have the suffix `.P'. For example, we might create the file (in the current directory) emp.P. Say it contains:

  % define the employee relation
  emp(e001,'Joe Smith','516-234-1358','Sales').
  emp(e002,'Larry Jones','516-234-1423','Support').
  emp(e003,'Dave Brown','516-234-4325','Development').
  emp(e004,'Jerry Gray','516-234-6423','Documentation').
  emp(e005,'Eugene Cheng','516-234-4357','Sales').
  emp(e006,'Tony Troy','516-234-8743','Development').
  emp(e007,'Joe Smith','516-234-6433','Support').
  emp(e008,'Clark Otasi','516-234-9437','Sales').

  % define a relation for the names of those in Sales.
  sales_emps(Name) :- emp(_,Name,_,'Sales').

To compile and load it into memory for evaluation, do:

  | ?- [emp].
  [Compiling ./emp]
  % Specialising partially instantiated calls to emp/4
  [emp compiled, cpu time used: 0.64 seconds]
  [emp loaded] 
  yes
  | ?-

You type what follows the | ?- . The rest is what the system responds. If the filename contains any special characters (such as a `/' or '.'!), it must be put in single quotes. Now you have compiled and loaded (sometimes known as ``consulted'') this file. Compiling it creates an object file (with the .O suffix). You can now enter goals involving those defined predicates for it to execute, such as:

  | ?- sales_emps(Name).

  Name = Joe Smith;

  Name = Eugene Cheng;

  Name = Clark Otasi;

  no

  | ?-

It printed the Name = Joe Smith and then waited for a response from me. I responded with a semicolon (;), which means I want more answers, if there are any. In this case there is another, so it responds with Name = Eugene Cheng. Again I entered a semecolon (;) and it continued with answers until there are no more, at which time it responds with `no'. My entering simply a <cr> (instead of the semicolon) would have terminated the search for answers, returning immediately to the top read-loop. A file can be changed and consulted again within the same execution. This will automatically recompile and reload the file.

To turn the stepper on, enter the query trace. To turn it off, enter notrace. The tracing facilities work primarily for Prolog code, and not so well for deductive database code (i.e. tabled code.) For tabled code, you should use only creep (<cr>).

There are demonstration programs in the directory ~sbprolog/XSB/examples. You might look at them to see larger example Prolog programs.

To exit from the Prolog system back to the unix prompt, enter the goal:

| ?- halt.

When you execute tabled code (which you will be doing when using the :- auto_table. or :- table pred/i. directives later in the course) you must empty the tables between executions. This is done with the predicate: abolish_all_tables. So a query done after a query involving tabled predicates should be preceded by abolishing all tables, as follows:

| ?- abolish_all_tables, sales_emps(Name).

Name = Joe Smith;

Name = Eugene Cheng;

Name = Clark Otasi;

no

| ?-

Also instead of having to hit ;<cr> to get all the answers, you can write a simple print statement to print them, as follows:

| ?- abolish_all_tables, emp(Id,Name,Tel,Dept), write([Id,Name,Tel,Dept]), nl, fail.
[e001,Joe Smith,516-234-1358,Sales]
[e002,Larry Jones,516-234-1423,Support]
[e003,Dave Brown,516-234-4325,Development]
[e004,Jerry Gray,516-234-6423,Documentation]
[e005,Eugene Cheng,516-234-4357,Sales]
[e006,Tony Troy,516-234-8743,Development]
[e007,Joe Smith,516-234-6433,Support]
[e008,Clark Otasi,516-234-9437,Sales]

no

| ?-

The write([Id,Name,Tel,Dept]) writes out the list of values, the nl prints a new-line, and the fail forces it to go back (as the hand-entered semicolon (;) did) and find another answer. You will probably want to use such a driver in your programs. You can create a testing pseudo-predicate test/0 to test a series of queries (q1, q2, q3) as the following example shows:

	test :- nl,nl,write('q1:'),nl,
		abolish_all_tables,q1(A,B),write([A,B]),nl,fail.
	test :- nl,nl,write('q2:'),nl,
		abolish_all_tables,q2(A,B,C),write([A,B,C]),nl,fail.
	test :- nl,nl,write('q3:'),nl,
		abolish_all_tables,q3(A),write([A]),nl,fail.
	test :- nl,nl,write('testing completed.'),nl.

Running XSB in Emacs

You can run XSB from within Emacs providing a programming environment which offers several advantages, including auto indentation, syntax highlighting, help on predefined predicates, consultation and compilation from inside Emacs, auto-fill mode, and more. In order to set this up you have to add some code to your .emacs file and make usre that the prolog.el package will be found. On the CSEE linux systems, we've installed a suitably modified prolog.el file in /cs/xsb/prolog.el. If you are running XSB on your own machine, you will want to copy this file.

Seting up your .emacs file

Put the following lines in your .emacs file.
    ;; make sure emacs can find prolog.el when it goes looking...
    (setq load-path (cons "/cs/xsb" load-path))
    ;; tell emacs where to find xsb-mode and prolog-mode
    (autoload `xsb-mode "prolog" "Major mode for editing XSB programs." t) 
    (autoload 'prolog-mode "prolog" "Major mode for editing Prolog programs." t) 
    ;; tuncomment one of these to select your prolog of the month. 
    (setq prolog-program-name "xsb") 
    ;; (setq prolog-program-name "sicstus") 
    ;; tell emacs to enter xsb-mode for a file ending in .P or .H and to
    ;; enter prolog-mode for a file ending in .pl
    (setq auto-mode-alist (cons '("\\.[PH]$" . xsb-mode) (cons '("\\.pl$" . prolog-mode) auto-mode-alist)))

Running XSB

A xsb process can be started within Emacs by choosing Run Prolog from the menu, by typing C-c RET (control-c followed by a return), or by typing M-x run-prolog (M-x is escape followed by x). It is however not strictly necessary to start a xsb process manually since it is automatically done when needed. The process can be restarted (i.e. the old one is killed and a new one is created) by typing C-u C-c RET. The process will appear in another window named *prolog* . You can always get back to the prolog window by typing C-x b *prolog* or even M-x run-prolog .

The Basic Prolog Commands

The most useful commands that the xsb mode provides are ones to consult and/or compile portions of one of your editing buffers. Consultation and compilation is either done via the menu or with the following key-bindings:
  • C-c C-b - Consult entire buffer.
  • C-c C-r - Consult current region.
  • C-c C-p - Consult predicate pointer is in.
  • C-c C-c b - Compile and load entire buffer.
  • C-c C-c r - Compile and load current region.
  • C-c C-c p - Compile and load predicate pointer is in.

For more information

The XSB manual, ( Volume 1 and Volume 2 ) is available online. There you can find other predicates which may help you. Other books on Prolog are:

  • Programming in Prolog, by W.F. Clocksin and C.S. Mellish, Springer-Verlag, New York, 1981.

  • The Art of Prolog, by L. Sterling and E. Shapiro, MIT Press, 1986.

  • Prolog and Artificial Intelligence, by I. Bratko.