
Hi there,
how can bignums be communicated to/from Ciao Prolog? More precisely, since we have
$ ciao Ciao-Prolog 1.9 #34: Sat Nov 30 19:17:43 CET 2002 ?- current_prolog_flag(bounded, X).
X = false ?
yes ?-
integers are unbounded. However, the function
ciao_term ciao_integer(int i)
allows to build only integers that fit in a machine word. The problem with the design of the function
int ciao_to_integer(ciao_term term)
appears to be more serious, as the C application seems to have no way of detecting whether the conversion was successful or not. It would seem better to define something like
int ciao_to_integer(ciao_term term, long* pl)
so that 0 is returned if the conversion failed (in which case *pl has not been touched), and a number different from 0 otherwise. Bignums could then be communicated with a pair of functions ciao_get_number_chars() and ciao_put_number_chars(). What do you think? Best wishes to you all
Roberto

Roberto Bagnara writes:
Hi there,
Hi Roberto (and all) -- have a happy new year!
how can bignums be communicated to/from Ciao Prolog? More precisely, since we have [...] It would seem better to define something like
int ciao_to_integer(ciao_term term, long* pl)
so that 0 is returned if the conversion failed (in which case *pl has not been touched), and a number different from 0 otherwise. Bignums could then be communicated with a pair of functions ciao_get_number_chars() and ciao_put_number_chars(). What do you think?
Completely agreed. I have implemented your suggestion, with some slight rewording and an extra function:
/* Check size */ ciao_bool ciao_fits_in_int(ciao_term term);
/* Try to perform conversion and return a success/failure code */ ciao_bool ciao_to_integer_check(ciao_term term, int *res);
/* Make conversion between arbitrary numbers and strings */ char *ciao_get_number_chars(ciao_term term); ciao_term ciao_put_number_chars(char *number_string);
(Comments on the interface, names, etc. are welcome. There are examples of use at /home/clip/Systems/ciao/lib/foreign_interface/examples/bignums).
I can send you the modified files or leave a complete .tgz for you to download; whatever you prefer.
MCL
___________________________________ "Eso" es mi amiga, y tampoco baila.

Manuel Carro wrote:
Hi Roberto (and all) -- have a happy new year!
You too!
Completely agreed. I have implemented your suggestion, with some
slight rewording and an extra function:
/* Check size */ ciao_bool ciao_fits_in_int(ciao_term term);
/* Try to perform conversion and return a success/failure code */ ciao_bool ciao_to_integer_check(ciao_term term, int *res);
Are these in addition to the old, unchecked
ciao_term ciao_integer(int i)
?
If so, I believe `ciao_to_integer_check(ciao_term term, int *res)' is redundant. I mean:
1) when the user _knows_ that the number fits, he/she can use `ciao_integer(int i)' for maximum speed; 2) when the user is not sure, he/she can use `ciao_fits_in_int(ciao_term term)' to cook his/her one checked version.
Another curiosity is: why `int' instead of `long' (the widest, standard integral type)?
/* Make conversion between arbitrary numbers and strings */ char *ciao_get_number_chars(ciao_term term); ciao_term ciao_put_number_chars(char *number_string);
If, as I suppose, the char* obtained with `ciao_get_number_chars(ciao_term term)' is under the responsibility of the Prolog engine, its lifetime should be documented.
I can send you the modified files or leave a complete .tgz for you to download; whatever you prefer.
If this is not too much, I would prefer obtaining a development snapshot of Ciao. I still have problems with the foreign language interface and I hope they are caused by bugs you have already fixed ;-) All the best
Roberto

Roberto Bagnara writes:
Are these in addition to the old, unchecked
ciao_term ciao_integer(int i) ? If so, I believe `ciao_to_integer_check(ciao_term term, int *res)' is redundant. I mean:
Yes, it is redundant. It is there only for convenience. It is defined as
ciao_bool ciao_to_integer_check(ciao_term term, int *res) { if (ciao_fits_in_int(term)) { *res = ciao_to_integer(term); return TRUE; } else return FALSE; }
I kept it only for the brevity of writing
if (!ciao_to_integer_check(term, &res)){ do_something_special_here() }
in a C library-like style.
Another curiosity is: why `int' instead of `long' (the widest, standard integral type)?
Good question. No special reason, in fact: it probably should have been 'long' since the beginning --- most likely we will change it. In fact, the size of long is the same as an int's right now in Intel machines and gcc. Actually, the widest standard data type now is the ISO C99 long long int, guaranteed to be at least 64 bits.
If, as I suppose, the char* obtained with `ciao_get_number_chars(ciao_term term)' is under the responsibility of the Prolog engine, its lifetime should be documented.
No, it is not: its management is passed on to the C side. The documentation reads:
@item @tt{char *ciao_get_number_chars(ciao_term term);}
It converts @tt{ciao_term} (which must be instantiated to a number) into a C string representing the number in the current radix. The string returned is a copy, to be explicitly deallocated by the user C code.
If this is not too much, I would prefer obtaining a development snapshot of Ciao. I still have problems with the foreign language interface and I hope they are caused by bugs you have already fixed ;-)
No problem at all.
Cheers,
MCL
_______________________________________________________________________ Internal error: Memory corruption [the program was trying to bribe gdb]

Manuel Carro wrote:
Roberto Bagnara writes:
If, as I suppose, the char* obtained with `ciao_get_number_chars(ciao_term term)' is under the responsibility of the Prolog engine, its lifetime should be documented.
No, it is not: its management is passed on to the C side. The
documentation reads:
@item @tt{char *ciao_get_number_chars(ciao_term term);}
It converts @tt{ciao_term} (which must be instantiated to a number) into a C string representing the number in the current radix. The string returned is a copy, to be explicitly deallocated by the user C code.
Right. Perhaps it is better to be clear about which deallocation function should be used. In principle, the Prolog engine may obtain memory in different ways and you may want not to commit yourself to some specific memory allocator. Would it be a good idea to abstract all this by adding macros like ciao_malloc() and ciao_free() to ciao_prolog.h and then instruct the user to use ciao_free() to release the memory returned by ciao_get_number_chars()?
If this is not too much, I would prefer obtaining a development snapshot of Ciao. I still have problems with the foreign language interface and I hope they are caused by bugs you have already fixed ;-)
No problem at all.
Thanks!
Roberto
participants (2)
-
Manuel Carro
-
Roberto Bagnara