00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "ppl.hh"
00024 #include "pwl.hh"
00025 #include <ciao_prolog.h>
00026 #include <cassert>
00027 #include <sstream>
00028
00029 typedef ciao_term Prolog_term_ref;
00030 typedef const char* Prolog_atom;
00031 typedef ciao_bool Prolog_foreign_return_type;
00032
00033 namespace {
00034
00035 const Prolog_foreign_return_type PROLOG_SUCCESS = 1;
00036 const Prolog_foreign_return_type PROLOG_FAILURE = 0;
00037
00038 }
00039
00040 #include "../exceptions.hh"
00041
00042 namespace PPL = Parma_Polyhedra_Library;
00043
00044 namespace {
00045
00049 bool Prolog_has_unbounded_integers;
00050
00056 long Prolog_min_integer;
00057
00063 long Prolog_max_integer;
00064
00068 void
00069 ppl_Prolog_sysdep_init() {
00070 Prolog_has_unbounded_integers = true;
00071 Prolog_min_integer = 0;
00072 Prolog_max_integer = 0;
00073 }
00074
00078 void
00079 ppl_Prolog_sysdep_deinit() {
00080 }
00081
00085 inline Prolog_term_ref
00086 Prolog_new_term_ref() {
00087 return 0;
00088 }
00089
00094 inline int
00095 Prolog_put_term(Prolog_term_ref& t, Prolog_term_ref u) {
00096 t = u;
00097 return 1;
00098 }
00099
00103 inline int
00104 Prolog_put_long(Prolog_term_ref& t, long l) {
00105 t = ciao_integer(l);
00106 return 1;
00107 }
00108
00112 inline int
00113 Prolog_put_ulong(Prolog_term_ref& t, unsigned long ul) {
00114 if (ul < INT_MAX)
00115 t = ciao_integer(ul);
00116 else {
00117 std::ostringstream s;
00118 s << ul;
00119
00120 t = ciao_put_number_chars(const_cast<char*>(s.str().c_str()));
00121 }
00122 return 1;
00123 }
00124
00129 inline int
00130 Prolog_put_atom_chars(Prolog_term_ref& t, const char* s) {
00131 t = ciao_atom(s);
00132 return 1;
00133 }
00134
00138 inline int
00139 Prolog_put_atom(Prolog_term_ref& t, Prolog_atom a) {
00140 t = ciao_atom(a);
00141 return 1;
00142 }
00143
00147 inline int
00148 Prolog_put_address(Prolog_term_ref& t, void* p) {
00149 t = ciao_pointer_to_address(ciao_implicit_state, p);
00150 return 1;
00151 }
00152
00156 Prolog_atom
00157 Prolog_atom_from_string(const char* s) {
00158 return ciao_atom_name(ciao_atom(s));
00159 }
00160
00161 Prolog_term_ref args[4];
00162
00167 inline int
00168 Prolog_construct_compound(Prolog_term_ref& t, Prolog_atom f,
00169 Prolog_term_ref a1) {
00170 args[0] = a1;
00171 t = ciao_structure_a(f, 1, args);
00172 return 1;
00173 }
00174
00179 inline int
00180 Prolog_construct_compound(Prolog_term_ref& t, Prolog_atom f,
00181 Prolog_term_ref a1, Prolog_term_ref a2) {
00182 args[0] = a1;
00183 args[1] = a2;
00184 t = ciao_structure_a(f, 2, args);
00185 return 1;
00186 }
00187
00192 inline int
00193 Prolog_construct_compound(Prolog_term_ref& t, Prolog_atom f,
00194 Prolog_term_ref a1, Prolog_term_ref a2,
00195 Prolog_term_ref a3) {
00196 args[0] = a1;
00197 args[1] = a2;
00198 args[2] = a3;
00199 t = ciao_structure_a(f, 3, args);
00200 return 1;
00201 }
00202
00207 inline int
00208 Prolog_construct_compound(Prolog_term_ref& t, Prolog_atom f,
00209 Prolog_term_ref a1, Prolog_term_ref a2,
00210 Prolog_term_ref a3, Prolog_term_ref a4) {
00211 args[0] = a1;
00212 args[1] = a2;
00213 args[2] = a3;
00214 args[3] = a4;
00215 t = ciao_structure_a(f, 4, args);
00216 return 1;
00217 }
00218
00222 inline int
00223 Prolog_construct_cons(Prolog_term_ref& c,
00224 Prolog_term_ref h, Prolog_term_ref t) {
00225 c = ciao_list(h, t);
00226 return 1;
00227 }
00228
00232 inline void
00233 Prolog_raise_exception(Prolog_term_ref t) {
00234 ciao_raise_exception(t);
00235 }
00236
00240 inline int
00241 Prolog_is_variable(Prolog_term_ref t) {
00242 return ciao_is_variable(t);
00243 }
00244
00248 inline int
00249 Prolog_is_atom(Prolog_term_ref t) {
00250 return ciao_is_atom(t);
00251 }
00252
00256 inline int
00257 Prolog_is_integer(Prolog_term_ref t) {
00258 return ciao_is_integer(t);
00259 }
00260
00264 inline int
00265 Prolog_is_address(Prolog_term_ref t) {
00266 return ciao_is_address(ciao_implicit_state, t);
00267 }
00268
00272 inline int
00273 Prolog_is_compound(Prolog_term_ref t) {
00274 return ciao_is_structure(t);
00275 }
00276
00280 inline int
00281 Prolog_is_cons(Prolog_term_ref t) {
00282 return ciao_is_list(t);
00283 }
00284
00291 inline int
00292 Prolog_get_long(Prolog_term_ref t, long* lp) {
00293 assert(ciao_is_integer(t));
00294 if (ciao_fits_in_int(t)) {
00295 *lp = ciao_to_integer(t);
00296 return 1;
00297 }
00298 else {
00299 char* s = ciao_get_number_chars(t);
00300 mpz_class n(s);
00301 ciao_free(s);
00302 PPL::Result r = PPL::assign_r(*lp, n, PPL::ROUND_NOT_NEEDED);
00303 return r == PPL::V_EQ ? 1 : 0;
00304 }
00305 }
00306
00312 inline int
00313 Prolog_get_address(Prolog_term_ref t, void** vpp) {
00314 assert(Prolog_is_address(t));
00315 *vpp = ciao_address_to_pointer(ciao_implicit_state, t);
00316 return 1;
00317 }
00318
00323 inline int
00324 Prolog_get_atom_name(Prolog_term_ref t, Prolog_atom* ap) {
00325 assert(Prolog_is_atom(t));
00326 *ap = ciao_atom_name(t);
00327 return 1;
00328 }
00329
00335 inline int
00336 Prolog_get_compound_name_arity(Prolog_term_ref t, Prolog_atom* ap, int* ip) {
00337 assert(Prolog_is_compound(t));
00338 *ap = ciao_structure_name(t);
00339 *ip = ciao_structure_arity(t);
00340 return 1;
00341 }
00342
00349 inline int
00350 Prolog_get_arg(int i, Prolog_term_ref t, Prolog_term_ref& a) {
00351 assert(Prolog_is_compound(t));
00352 a = ciao_structure_arg(t, i);
00353 return 1;
00354 }
00355
00361 inline int
00362 Prolog_get_cons(Prolog_term_ref c, Prolog_term_ref& h, Prolog_term_ref& t) {
00363 assert(Prolog_is_cons(c));
00364 h = ciao_list_head(c);
00365 t = ciao_list_tail(c);
00366 return 1;
00367 }
00368
00373 inline int
00374 Prolog_unify(Prolog_term_ref t, Prolog_term_ref u) {
00375 return ciao_unify(t, u);
00376 }
00377
00378 PPL::Coefficient
00379 integer_term_to_Coefficient(Prolog_term_ref t) {
00380 assert(ciao_is_integer(t));
00381 if (ciao_fits_in_int(t))
00382 return PPL::Coefficient(ciao_to_integer(t));
00383 else {
00384 char* s;
00385 s = ciao_get_number_chars(t);
00386 PPL::Coefficient n(s);
00387 ciao_free(s);
00388 return n;
00389 }
00390 }
00391
00392 Prolog_term_ref
00393 Coefficient_to_integer_term(const PPL::Coefficient& n) {
00394 int i = 0;
00395 if (PPL::assign_r(i, n, PPL::ROUND_NOT_NEEDED) == PPL::V_EQ)
00396 return ciao_integer(i);
00397 else {
00398 std::ostringstream s;
00399 s << n;
00400
00401 return ciao_put_number_chars(const_cast<char*>(s.str().c_str()));
00402 }
00403 }
00404
00405 }
00406
00407 #include "../ppl_prolog.icc"
00408
00409 extern "C" void
00410 init() {
00411 ppl_initialize();
00412 }