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
00026
00027
00028
00029 #include <gmp.h>
00030 #include <SWI-Prolog.h>
00031 #include <cassert>
00032 #ifdef HAVE_STDINT_H
00033 #include <stdint.h>
00034 #endif
00035 #ifdef HAVE_INTTYPES_H
00036 #include <inttypes.h>
00037 #endif
00038
00039 typedef term_t Prolog_term_ref;
00040 typedef atom_t Prolog_atom;
00041 typedef foreign_t Prolog_foreign_return_type;
00042
00043 namespace {
00044
00045 const Prolog_foreign_return_type PROLOG_SUCCESS = TRUE;
00046 const Prolog_foreign_return_type PROLOG_FAILURE = FALSE;
00047
00048 }
00049
00050 #include "../exceptions.hh"
00051
00052 namespace PPL = Parma_Polyhedra_Library;
00053
00054 namespace {
00055
00059 bool Prolog_has_unbounded_integers;
00060
00066 long Prolog_min_integer;
00067
00073 long Prolog_max_integer;
00074
00078 mpz_class tmp_mpz_class;
00079
00083 void
00084 ppl_Prolog_sysdep_init() {
00085 Prolog_has_unbounded_integers = true;
00086 Prolog_min_integer = 0;
00087 Prolog_max_integer = 0;
00088 }
00089
00093 void
00094 ppl_Prolog_sysdep_deinit() {
00095 }
00096
00100 inline Prolog_term_ref
00101 Prolog_new_term_ref() {
00102 return PL_new_term_ref();
00103 }
00104
00109 inline int
00110 Prolog_put_term(Prolog_term_ref t, Prolog_term_ref u) {
00111 PL_put_term(t, u);
00112 return 1;
00113 }
00114
00118 inline int
00119 Prolog_put_long(Prolog_term_ref t, long l) {
00120 PL_put_integer(t, l);
00121 return 1;
00122 }
00123
00127 inline int
00128 Prolog_put_ulong(Prolog_term_ref t, unsigned long ul) {
00129 if (ul <= LONG_MAX)
00130 PL_put_integer(t, ul);
00131 else if (ul <= static_cast<uint64_t>(std::numeric_limits<int64_t>::max()))
00132 PL_put_int64(t, static_cast<int64_t>(ul));
00133 else {
00134 PPL::assign_r(tmp_mpz_class, ul, PPL::ROUND_NOT_NEEDED);
00135 PL_unify_mpz(t, tmp_mpz_class.get_mpz_t());
00136 }
00137 return 1;
00138 }
00139
00144 inline int
00145 Prolog_put_atom_chars(Prolog_term_ref t, const char* s) {
00146 PL_put_atom_chars(t, s);
00147 return 1;
00148 }
00149
00153 inline int
00154 Prolog_put_atom(Prolog_term_ref t, Prolog_atom a) {
00155 PL_put_atom(t, a);
00156 return 1;
00157 }
00158
00162 inline int
00163 Prolog_put_address(Prolog_term_ref t, void* p) {
00164 PL_put_pointer(t, p);
00165 return 1;
00166 }
00167
00171 Prolog_atom
00172 Prolog_atom_from_string(const char* s) {
00173 return PL_new_atom(s);
00174 }
00175
00180 inline int
00181 Prolog_construct_compound(Prolog_term_ref t, Prolog_atom f,
00182 Prolog_term_ref a1) {
00183 PL_cons_functor(t, PL_new_functor(f, 1), a1);
00184 return 1;
00185 }
00186
00191 inline int
00192 Prolog_construct_compound(Prolog_term_ref t, Prolog_atom f,
00193 Prolog_term_ref a1, Prolog_term_ref a2) {
00194 PL_cons_functor(t, PL_new_functor(f, 2), a1, a2);
00195 return 1;
00196 }
00197
00202 inline int
00203 Prolog_construct_compound(Prolog_term_ref t, Prolog_atom f,
00204 Prolog_term_ref a1, Prolog_term_ref a2,
00205 Prolog_term_ref a3) {
00206 PL_cons_functor(t, PL_new_functor(f, 3), a1, a2, a3);
00207 return 1;
00208 }
00209
00214 inline int
00215 Prolog_construct_compound(Prolog_term_ref t, Prolog_atom f,
00216 Prolog_term_ref a1, Prolog_term_ref a2,
00217 Prolog_term_ref a3, Prolog_term_ref a4) {
00218 PL_cons_functor(t, PL_new_functor(f, 4), a1, a2, a3, a4);
00219 return 1;
00220 }
00221
00225 inline int
00226 Prolog_construct_cons(Prolog_term_ref c,
00227 Prolog_term_ref h, Prolog_term_ref t) {
00228 PL_cons_list(c, h, t);
00229 return 1;
00230 }
00231
00235 inline void
00236 Prolog_raise_exception(Prolog_term_ref t) {
00237 (void) PL_raise_exception(t);
00238 }
00239
00243 inline int
00244 Prolog_is_variable(Prolog_term_ref t) {
00245 return PL_is_variable(t);
00246 }
00247
00251 inline int
00252 Prolog_is_atom(Prolog_term_ref t) {
00253 return PL_is_atom(t);
00254 }
00255
00259 inline int
00260 Prolog_is_integer(Prolog_term_ref t) {
00261 return PL_is_integer(t);
00262 }
00263
00267 inline int
00268 Prolog_is_address(Prolog_term_ref t) {
00269 return PL_is_integer(t);
00270 }
00271
00275 inline int
00276 Prolog_is_compound(Prolog_term_ref t) {
00277 return PL_is_compound(t);
00278 }
00279
00283 inline int
00284 Prolog_is_cons(Prolog_term_ref t) {
00285 return !PL_is_atom(t) && PL_is_list(t);
00286 }
00287
00294 inline int
00295 Prolog_get_long(Prolog_term_ref t, long* lp) {
00296 assert(Prolog_is_integer(t));
00297 return PL_get_long(t, lp);
00298 }
00299
00305 inline int
00306 Prolog_get_address(Prolog_term_ref t, void** vpp) {
00307 assert(Prolog_is_address(t));
00308 return PL_get_pointer(t, vpp);
00309 }
00310
00315 inline int
00316 Prolog_get_atom_name(Prolog_term_ref t, Prolog_atom* ap) {
00317 assert(Prolog_is_atom(t));
00318 return PL_get_atom(t, ap);
00319 }
00320
00326 inline int
00327 Prolog_get_compound_name_arity(Prolog_term_ref t, Prolog_atom* ap, int* ip) {
00328 assert(Prolog_is_compound(t));
00329 return PL_get_name_arity(t, ap, ip);
00330 }
00331
00338 inline int
00339 Prolog_get_arg(int i, Prolog_term_ref t, Prolog_term_ref a) {
00340 assert(Prolog_is_compound(t));
00341 return PL_get_arg(i, t, a);
00342 }
00343
00349 inline int
00350 Prolog_get_cons(Prolog_term_ref c, Prolog_term_ref h, Prolog_term_ref t) {
00351 assert(Prolog_is_cons(c));
00352 return PL_get_list(c, h, t);
00353 }
00354
00359 inline int
00360 Prolog_unify(Prolog_term_ref t, Prolog_term_ref u) {
00361 return PL_unify(t, u);
00362 }
00363
00364 PPL::Coefficient
00365 integer_term_to_Coefficient(Prolog_term_ref t) {
00366 assert(Prolog_is_integer(t));
00367 PL_get_mpz(t, tmp_mpz_class.get_mpz_t());
00368 return PPL::Coefficient(tmp_mpz_class);
00369 }
00370
00371 Prolog_term_ref
00372 Coefficient_to_integer_term(const PPL::Coefficient& n) {
00373 PPL::assign_r(tmp_mpz_class, n, PPL::ROUND_NOT_NEEDED);
00374 Prolog_term_ref t = Prolog_new_term_ref();
00375 PL_unify_mpz(t, tmp_mpz_class.get_mpz_t());
00376 return t;
00377 }
00378
00379 }
00380
00381 #include "../ppl_prolog.icc"
00382
00383 #define PL_EXTENSION_ENTRY(name, arity) { #name, arity, (void*) name, 0 },
00384
00385 namespace {
00386
00387 PL_extension predicates[] = {
00388 PL_EXTENSION_ENTRY(ppl_version_major, 1)
00389 PL_EXTENSION_ENTRY(ppl_version_minor, 1)
00390 PL_EXTENSION_ENTRY(ppl_version_revision, 1)
00391 PL_EXTENSION_ENTRY(ppl_version_beta, 1)
00392 PL_EXTENSION_ENTRY(ppl_version, 1)
00393 PL_EXTENSION_ENTRY(ppl_banner, 1)
00394 PL_EXTENSION_ENTRY(ppl_max_space_dimension, 1)
00395 PL_EXTENSION_ENTRY(ppl_Coefficient_is_bounded, 0)
00396 PL_EXTENSION_ENTRY(ppl_Coefficient_max, 1)
00397 PL_EXTENSION_ENTRY(ppl_Coefficient_min, 1)
00398 PL_EXTENSION_ENTRY(ppl_initialize, 0)
00399 PL_EXTENSION_ENTRY(ppl_finalize, 0)
00400 PL_EXTENSION_ENTRY(ppl_set_timeout_exception_atom, 1)
00401 PL_EXTENSION_ENTRY(ppl_timeout_exception_atom, 1)
00402 PL_EXTENSION_ENTRY(ppl_set_timeout, 1)
00403 PL_EXTENSION_ENTRY(ppl_reset_timeout, 0)
00404 PL_EXTENSION_ENTRY(ppl_new_C_Polyhedron_from_space_dimension, 3)
00405 PL_EXTENSION_ENTRY(ppl_new_NNC_Polyhedron_from_space_dimension, 3)
00406 PL_EXTENSION_ENTRY(ppl_new_C_Polyhedron_from_C_Polyhedron, 2)
00407 PL_EXTENSION_ENTRY(ppl_new_C_Polyhedron_from_NNC_Polyhedron, 2)
00408 PL_EXTENSION_ENTRY(ppl_new_NNC_Polyhedron_from_C_Polyhedron, 2)
00409 PL_EXTENSION_ENTRY(ppl_new_NNC_Polyhedron_from_NNC_Polyhedron, 2)
00410 PL_EXTENSION_ENTRY(ppl_new_C_Polyhedron_from_constraints, 2)
00411 PL_EXTENSION_ENTRY(ppl_new_NNC_Polyhedron_from_constraints, 2)
00412 PL_EXTENSION_ENTRY(ppl_new_C_Polyhedron_from_generators, 2)
00413 PL_EXTENSION_ENTRY(ppl_new_NNC_Polyhedron_from_generators, 2)
00414 PL_EXTENSION_ENTRY(ppl_new_C_Polyhedron_from_bounding_box, 2)
00415 PL_EXTENSION_ENTRY(ppl_new_NNC_Polyhedron_from_bounding_box, 2)
00416 PL_EXTENSION_ENTRY(ppl_Polyhedron_swap, 2)
00417 PL_EXTENSION_ENTRY(ppl_delete_Polyhedron, 1)
00418 PL_EXTENSION_ENTRY(ppl_Polyhedron_space_dimension, 2)
00419 PL_EXTENSION_ENTRY(ppl_Polyhedron_affine_dimension, 2)
00420 PL_EXTENSION_ENTRY(ppl_Polyhedron_get_constraints, 2)
00421 PL_EXTENSION_ENTRY(ppl_Polyhedron_get_minimized_constraints, 2)
00422 PL_EXTENSION_ENTRY(ppl_Polyhedron_get_generators, 2)
00423 PL_EXTENSION_ENTRY(ppl_Polyhedron_get_minimized_generators, 2)
00424 PL_EXTENSION_ENTRY(ppl_Polyhedron_relation_with_constraint, 3)
00425 PL_EXTENSION_ENTRY(ppl_Polyhedron_relation_with_generator, 3)
00426 PL_EXTENSION_ENTRY(ppl_Polyhedron_get_bounding_box, 3)
00427 PL_EXTENSION_ENTRY(ppl_Polyhedron_is_empty, 1)
00428 PL_EXTENSION_ENTRY(ppl_Polyhedron_is_universe, 1)
00429 PL_EXTENSION_ENTRY(ppl_Polyhedron_is_bounded, 1)
00430 PL_EXTENSION_ENTRY(ppl_Polyhedron_bounds_from_above, 2)
00431 PL_EXTENSION_ENTRY(ppl_Polyhedron_bounds_from_below, 2)
00432 PL_EXTENSION_ENTRY(ppl_Polyhedron_maximize, 5)
00433 PL_EXTENSION_ENTRY(ppl_Polyhedron_maximize_with_point, 6)
00434 PL_EXTENSION_ENTRY(ppl_Polyhedron_minimize, 5)
00435 PL_EXTENSION_ENTRY(ppl_Polyhedron_minimize_with_point, 6)
00436 PL_EXTENSION_ENTRY(ppl_Polyhedron_is_topologically_closed, 1)
00437 PL_EXTENSION_ENTRY(ppl_Polyhedron_topological_closure_assign, 1)
00438 PL_EXTENSION_ENTRY(ppl_Polyhedron_contains_Polyhedron, 2)
00439 PL_EXTENSION_ENTRY(ppl_Polyhedron_strictly_contains_Polyhedron, 2)
00440 PL_EXTENSION_ENTRY(ppl_Polyhedron_is_disjoint_from_Polyhedron, 2)
00441 PL_EXTENSION_ENTRY(ppl_Polyhedron_equals_Polyhedron, 2)
00442 PL_EXTENSION_ENTRY(ppl_Polyhedron_OK, 1)
00443 PL_EXTENSION_ENTRY(ppl_Polyhedron_add_constraint, 2)
00444 PL_EXTENSION_ENTRY(ppl_Polyhedron_add_constraint_and_minimize, 2)
00445 PL_EXTENSION_ENTRY(ppl_Polyhedron_add_generator, 2)
00446 PL_EXTENSION_ENTRY(ppl_Polyhedron_add_generator_and_minimize, 2)
00447 PL_EXTENSION_ENTRY(ppl_Polyhedron_add_constraints, 2)
00448 PL_EXTENSION_ENTRY(ppl_Polyhedron_add_constraints_and_minimize, 2)
00449 PL_EXTENSION_ENTRY(ppl_Polyhedron_add_generators, 2)
00450 PL_EXTENSION_ENTRY(ppl_Polyhedron_add_generators_and_minimize, 2)
00451 PL_EXTENSION_ENTRY(ppl_Polyhedron_intersection_assign, 2)
00452 PL_EXTENSION_ENTRY(ppl_Polyhedron_intersection_assign_and_minimize, 2)
00453 PL_EXTENSION_ENTRY(ppl_Polyhedron_poly_hull_assign, 2)
00454 PL_EXTENSION_ENTRY(ppl_Polyhedron_poly_hull_assign_and_minimize, 2)
00455 PL_EXTENSION_ENTRY(ppl_Polyhedron_poly_difference_assign, 2)
00456 PL_EXTENSION_ENTRY(ppl_Polyhedron_affine_image, 4)
00457 PL_EXTENSION_ENTRY(ppl_Polyhedron_affine_preimage, 4)
00458 PL_EXTENSION_ENTRY(ppl_Polyhedron_bounded_affine_image, 5)
00459 PL_EXTENSION_ENTRY(ppl_Polyhedron_bounded_affine_preimage, 5)
00460 PL_EXTENSION_ENTRY(ppl_Polyhedron_generalized_affine_image, 5)
00461 PL_EXTENSION_ENTRY(ppl_Polyhedron_generalized_affine_preimage, 5)
00462 PL_EXTENSION_ENTRY(ppl_Polyhedron_generalized_affine_image_lhs_rhs, 4)
00463 PL_EXTENSION_ENTRY(ppl_Polyhedron_generalized_affine_preimage_lhs_rhs, 4)
00464 PL_EXTENSION_ENTRY(ppl_Polyhedron_time_elapse_assign, 2)
00465 PL_EXTENSION_ENTRY(ppl_Polyhedron_BHRZ03_widening_assign_with_tokens, 4)
00466 PL_EXTENSION_ENTRY(ppl_Polyhedron_BHRZ03_widening_assign, 2)
00467 PL_EXTENSION_ENTRY(
00468 ppl_Polyhedron_limited_BHRZ03_extrapolation_assign_with_tokens, 5)
00469 PL_EXTENSION_ENTRY(ppl_Polyhedron_limited_BHRZ03_extrapolation_assign, 3)
00470 PL_EXTENSION_ENTRY(
00471 ppl_Polyhedron_bounded_BHRZ03_extrapolation_assign_with_tokens, 5)
00472 PL_EXTENSION_ENTRY(ppl_Polyhedron_bounded_BHRZ03_extrapolation_assign, 3)
00473 PL_EXTENSION_ENTRY(ppl_Polyhedron_H79_widening_assign_with_tokens, 4)
00474 PL_EXTENSION_ENTRY(ppl_Polyhedron_H79_widening_assign, 2)
00475 PL_EXTENSION_ENTRY(
00476 ppl_Polyhedron_limited_H79_extrapolation_assign_with_tokens, 5)
00477 PL_EXTENSION_ENTRY(ppl_Polyhedron_limited_H79_extrapolation_assign, 3)
00478 PL_EXTENSION_ENTRY(
00479 ppl_Polyhedron_bounded_H79_extrapolation_assign_with_tokens, 5)
00480 PL_EXTENSION_ENTRY(ppl_Polyhedron_bounded_H79_extrapolation_assign, 3)
00481 PL_EXTENSION_ENTRY(ppl_Polyhedron_add_space_dimensions_and_project, 2)
00482 PL_EXTENSION_ENTRY(ppl_Polyhedron_add_space_dimensions_and_embed, 2)
00483 PL_EXTENSION_ENTRY(ppl_Polyhedron_concatenate_assign, 2)
00484 PL_EXTENSION_ENTRY(ppl_Polyhedron_remove_space_dimensions, 2)
00485 PL_EXTENSION_ENTRY(ppl_Polyhedron_remove_higher_space_dimensions, 2)
00486 PL_EXTENSION_ENTRY(ppl_Polyhedron_expand_space_dimension, 3)
00487 PL_EXTENSION_ENTRY(ppl_Polyhedron_fold_space_dimensions, 3)
00488 PL_EXTENSION_ENTRY(ppl_Polyhedron_map_space_dimensions, 2)
00489 PL_EXTENSION_ENTRY(ppl_new_LP_Problem_trivial, 1)
00490 PL_EXTENSION_ENTRY(ppl_new_LP_Problem, 4)
00491 PL_EXTENSION_ENTRY(ppl_new_LP_Problem_from_LP_Problem, 2)
00492 PL_EXTENSION_ENTRY(ppl_LP_Problem_swap, 2)
00493 PL_EXTENSION_ENTRY(ppl_delete_LP_Problem, 1)
00494 PL_EXTENSION_ENTRY(ppl_LP_Problem_space_dimension, 2)
00495 PL_EXTENSION_ENTRY(ppl_LP_Problem_constraints, 2)
00496 PL_EXTENSION_ENTRY(ppl_LP_Problem_objective_function, 2)
00497 PL_EXTENSION_ENTRY(ppl_LP_Problem_optimization_mode, 2)
00498 PL_EXTENSION_ENTRY(ppl_LP_Problem_clear, 1)
00499 PL_EXTENSION_ENTRY(ppl_LP_Problem_add_constraint, 2)
00500 PL_EXTENSION_ENTRY(ppl_LP_Problem_add_constraints, 2)
00501 PL_EXTENSION_ENTRY(ppl_LP_Problem_set_objective_function, 2)
00502 PL_EXTENSION_ENTRY(ppl_LP_Problem_set_optimization_mode, 2)
00503 PL_EXTENSION_ENTRY(ppl_LP_Problem_is_satisfiable, 1)
00504 PL_EXTENSION_ENTRY(ppl_LP_Problem_solve, 2)
00505 PL_EXTENSION_ENTRY(ppl_LP_Problem_feasible_point, 2)
00506 PL_EXTENSION_ENTRY(ppl_LP_Problem_optimizing_point, 2)
00507 PL_EXTENSION_ENTRY(ppl_LP_Problem_optimal_value, 3)
00508 PL_EXTENSION_ENTRY(ppl_LP_Problem_evaluate_objective_function, 4)
00509 PL_EXTENSION_ENTRY(ppl_LP_Problem_OK, 1)
00510 { NULL, 0, NULL, 0 }
00511 };
00512
00513 }
00514
00515 extern "C" install_t
00516 install() {
00517 ppl_initialize();
00518 PL_register_extensions(predicates);
00519 }
00520
00521 extern "C" install_t
00522 uninstall() {
00523 ppl_finalize();
00524 }