View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Salvador Fandiño García > Language-XSB > Language::XSB



Annotate this POD


Open  0
View/Report Bugs
Module Version: 0.14   Source  


Language::XSB - use XSB from Perl.


    use Language::XSB ':query';
    use Language::Prolog::Types::overload;
    use Language::Prolog::Sugar vars=>[qw(X Y Z)],
                                functors=>{equal => '='},
                                chains=>{plus => '+',
                                         orn => ';'};

    xsb_set_query( equal(X, 34),
                   equal(Y, -12),
                   is(Z, plus( X,
                               1000 )));

    while(xsb_next()) {
        printf("X=%d, Y=%d, Z=%d\n",
               xsb_var(X), xsb_var(Y), xsb_var(Z))

    print join("\n", xsb_find_all(orn(equal(X, 27),
                                      equal(X, 45)), X)), "\n";


Language::XSB provides a bidirectional interface to XSB (


From the XSB manual:

  XSB is a research-oriented Logic Programming and Deductive
  Database System developed at SUNY Stony Brook.  In addition to
  providing all the functionality of Prolog, it contains
  features not usually found in Logic Programming Systems such
  as evaluation according to the Well Founded Semantics through
  full SLG resolution, constraint handling for tabled programs,
  a compiled HiLog implementation, unification factoring and
  interfaces to other systems such as ODBC, C, Java, Perl, and

This package implements a bidirectional interface to XSB, thats means that Perl can call XSB that can call Perl back that can call XSB again, etc.:

  Perl -> XSB -> Perl -> XSB -> ...

(Unfortunately, you have to start from Perl, XSB->Perl->... is not possible.)

The interface to XSB is based on the objects created by the package Language::Prolog::Types. You can also use Language::Prolog::Sugar package, a front end for the types package to improve the look of your source (just some syntactic sugar).

To make queries to XSB you have to set first the query term with the function xsb_set_query, and then use xsb_next and xsb_result to iterate over it and get the results back.

Only one query can be open at any time, unless when Perl is called back from XSB, but then the old query is not visible.


In this versions there is only one tag to import all the soubrutines in your script or package:


sets the query term, if multiple terms are passed, then the are first chained with the ','/2 functor and the result stored as the query.

It returns the free variables found in the query.


Returns the value binded to $var in the current query/solution combination.


returns the current query, variables are bounded to their current values if xsb_next has been called with success.


iterates over the query and returns a true value if a new solution is found.


after calling xsb_next, this soubrutine returns the values assigned to the free variables in the query.


ends an unfinished query, similar to XSB (or Prolog) cut !. As the real cut in XSB, special care should be taken to not cut over tables.


a deprecated alias for xsb_cut.

xsb_find_all($query, @pattern)

iterates over $query and returns and array with @pattern binded to every solution. i.e:

  xsb_find_all(member(X, [1, 3, 7, 21]), X)

returns the array (1, 3, 7, 21) and

  xsb_find_all(member(X, [1, 3, 7, 21]), [X])

returns the array ([1], [3], [7], [21]).

More elaborate constructions can be used:

  %mothers=xsb_find_all(mother(X,Y), X, Y)
xsb_find_one($query, @pattern)

as xsb_find_all but only for the first solution.


runs the query once and return true if a solution was found or false otherwise.

xsb_assert($head => @body)

add new definitions at the botton of the database


commodity subroutine to add several facts (facts, doesn't have body) to the database in one call.


  use Language::Prolog::Sugar functors=>[qw(man woman)];

  xsb_facts( man('teodoro'),
             woman('mary') );


This is alpha software so there should be some of them.

clpr is not callable from Perl. A FPE signal will raise if you try to do so.

No threads support as XSB doesn't support them (take a look at Language::Prolog::Yaswi for an interface to SWI-Prolog with thread support).


Language::Prolog::Types, Language::Prolog::Types::overload and Language::Prolog::Sugar for instructions on creating Prolog (or XSB) terms from Perl.

For XSB and Prolog information see xsb(1), the XSB website at Sourceforge and the FAQ of comp.lang.prolog.

A good Prolog book would also help. I personally recommend you

- "PROLOG Programming for Artificial Intelligence" by Ivan Bratko.
- "The Art of Prolog" by Leon Sterling and Ehud Shapiro.

If you want to look at the inners details of this package then take a look at Language::XSB::Base and Language::XSB::Register.


Salvador Fandiño, <>


Copyright 2002, 2003 by Salvador Fandiño

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: