Re: [SWIPL] Big integers and the foreign language interface

Jan Wielemaker wrote:
Roberto
On Friday 02 December 2005 19:55, Roberto Bagnara wrote:
I am interfacing the Parma Polyhedra Library (http://www.cs.unipr.it/ppl/) to the new version of SWI-Prolog. The library is already interfaced to versions of SWI-Prolog up to 5.4.7. The point is that big integers arise quite frequently in the kind of applications we are interested in. So we were really looking forward for your announcement that SWI-Prolog 5.6 is to be released very soon.
In order to do the job, all I need are functions of the kind PL_put_mpz_t(), PL_put_mpq_t(), PL_get_mpz_t() and PL_get_mpq_t() with the obvious semantics. A reasonably efficient text-based interface would also be OK: is there something like PL_put_number_chars() and PL_get_number_chars()?
By the way: Alan's idea of providing a separate header file for the GMP-dependent stuff would seem to address your concerns, right? An alternative would be to use the same header file but ask the user to activate the GMP-dependent stuff explicitly, e.g., by doing
Ok. I've added
int PL_get_mpz(term_t t, mpz_t mpz); int PL_get_mpq(term_t t, mpq_t mpq); int PL_unify_mpz(term_t t, mpz_t mpz); int PL_unify_mpq(term_t t, mpq_t mpq);
With pretty obvious (also documented) semantics. You get these if you do (in *this* order):
#include <gmp.h> #include <SWI-Prolog.h>
Now this works fine, except when SWI-Prolog is compiled with a diffferent version of GMP that uses a different representation. I think GMP is pretty stable, but there is an issue here.
Changes are on CVS. I only ran a few obvious tests. Please try it and report problems you may find.
Success --- Jan
Here is a complete example:
#include <gmp.h> #include <SWI-Prolog.h>
static foreign_t next_prime(term_t n, term_t prime) { mpz_t mpz; int rc;
mpz_init(mpz); if ( PL_get_mpz(n, mpz) ) { mpz_nextprime(mpz, mpz);
rc = PL_unify_mpz(prime, mpz);
} else rc = FALSE;
mpz_clear(mpz); return rc; }
install_t install() { PL_register_foreign("next_prime", 2, next_prime, 0); }
Dear Jan,
thanks a lot: the functions you have added to SWI-Prolog's foreign language interface do the job perfectly. I have made several tests with the PPL and everything seems to work as expected. Thanks again,
Roberto
participants (1)
-
Roberto Bagnara