XSB Tutorial: Basic Command Line Examples

This tutorial shows how to run a basic program on XSB (Which is "a research-oriented Logic Programming system", see xsb.sourceforge.net).  See the XSB Programmers Manual for more details.  You can use XSB to run Prolog programs, and it's free, so if you want a free Prolog interpreter/compiler, this is it.

Example 1 - Typing a program on the command line

unix% xsb  Start XSB, usually located in $XSB_DIR/bin
[xsb_configuration loaded]
[sysinitrc loaded]
[packaging loaded]

XSB Version 2.6 (Duff) of June 24, 2003
[i686-pc-linux-gnu; mode: optimal; engine: slg-wam; gc: indirection; scheduling: local]

| ?- [user].  Enter mode to type predicates & rules.
[Compiling user]
edge(1,2).
edge(2,3).
edge(2,4).

reachable(X,Y) :- edge(X,Y).
reachable(X,Y) :- edge(X,Z), reachable(Z, Y).
<Control-D>  Done with terminal mode
[user compiled, cpu time used: 0.0500 seconds]
[user loaded]

yes
| ?- reachable(X,Y).

X = 1
Y = 2;  Type a semi-colon repeatedly

X = 2
Y = 3;

X = 2
Y = 4;

X = 1
Y = 3;

X = 1
Y = 4;

no
| ?- halt.  Command to Exit XSB

End XSB (cputime 0.15 secs, elapsetime 96.23 secs)
unix%

The above program describes a graph of edges, and a rule to determine which nodes are reachable from other nodes, as in this diagram:
Diagram of Graphs
Example 2 - Loading a program from a file

You can also load a program in a file.  Name the file with the .P extension.  For example, if you have a file named sports.P, here's how to run it.

unix% cat sports.P  Here's what the file looks like
:- auto_table.

likes(ellen,tennis).
likes(john,football).
likes(tom,baseball).
likes(eric,swimming).
likes(mark,tennis).

likes(bill, X) :- likes(tom,X).
unix% xsb
[xsb_configuration loaded]
[sysinitrc loaded]
[packaging loaded]

XSB Version 2.6 (Duff) of June 24, 2003
[i686-pc-linux-gnu; mode: optimal; engine: slg-wam; gc: indirection; scheduling: local]

| ?- [sports].  Load and run sports.P
[sports loaded]

yes
| ?- likes(bill, baseball).

yes
| ?- likes(bill, tennis).

no
| ?- halt.

End XSB (cputime 0.09 secs, elapsetime 132.97 secs)
unix%End XSB (cputime 0.09 secs, elapsetime 132.97 secs)
unix%

To learn more about Prolog, check out the following sites:

--Matthew
[home]