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 => '='}, functors=>[qw(is)], chains=>{plus => '+', orn => ';'}; xsb_set_query( equal(X, 34), equal(Y, -12), is(Z, plus( X, Y, 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 (http://xsb.sourceforge.net/).
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 Oracle
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.)
XSB->Perl->...
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.
xsb_set_query
xsb_next
xsb_result
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:
:query
xsb_set_query(@terms)
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.
xsb_var($var)
Returns the value binded to $var in the current query/solution combination.
$var
xsb_query()
returns the current query, variables are bounded to their current values if xsb_next has been called with success.
xsb_next()
iterates over the query and returns a true value if a new solution is found.
xsb_result()
after calling xsb_next, this soubrutine returns the values assigned to the free variables in the query.
xsb_cut()
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.
!
xsb_clear_query()
a deprecated alias for xsb_cut.
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
(1, 3, 7, 21)
xsb_find_all(member(X, [1, 3, 7, 21]), [X])
returns the array ([1], [3], [7], [21]).
([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.
xsb_find_all
xsb_call(@query)
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
xsb_facts(@facts)
commodity subroutine to add several facts (facts, doesn't have body) to the database in one call.
i.e.:
use Language::Prolog::Sugar functors=>[qw(man woman)]; xsb_facts( man('teodoro'), man('socrates'), woman('teresa'), 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
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, <sfandino@yahoo.com>
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.
1 POD Error
The following errors were encountered while parsing the POD:
Non-ASCII character seen before =encoding in 'Fandiño,'. Assuming CP1252
To install Language::XSB, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Language::XSB
CPAN shell
perl -MCPAN -e shell install Language::XSB
For more information on module installation, please visit the detailed CPAN module installation guide.