diff --git a/gmlmip.ml b/gmlmip.ml index 3243083b607c7487980beb5fc42b6964064db847..72b47f3fffb937ffa5ee84d754502627240fb424 100644 --- a/gmlmip.ml +++ b/gmlmip.ml @@ -4,4 +4,4 @@ to list of consequences (positive?,formula), here positive? means that the formula must be true in order to make the given conjunct of modalities true *) -external gml_rules : (bool*int*int) list -> (int*bool) list list = "gmlRules_stub" +external gml_rules : (bool*int*int) list -> (int*bool) list list list = "gmlRules_stub" diff --git a/gmlmip.mli b/gmlmip.mli index dca49b624d8b282335e9ae5e88a0f9e1887dbadc..4a911acccb4a7354376b31bff68e3a7f84598848 100644 --- a/gmlmip.mli +++ b/gmlmip.mli @@ -2,4 +2,4 @@ to list of consequences (positive?,formula), here positive? means that the formula must be true in order to make the given conjunct of modalities true *) -val gml_rules : (bool*int*int) list -> (int*bool) list list +val gml_rules : (bool*int*int) list -> (int*bool) list list list diff --git a/gmlmip_stub.cc b/gmlmip_stub.cc index 54ef0972c30f9bd74d390391547458fdf6458df7..8548e27aa3569b7c5d5fa9b5bd978bc025e1c624 100644 --- a/gmlmip_stub.cc +++ b/gmlmip_stub.cc @@ -3,6 +3,7 @@ #include <utility> // for pair #include <vector> +#include <set> extern "C" { #include <caml/mlvalues.h> @@ -21,15 +22,18 @@ extern "C" { } template<class T> -value vector2CamlList(const vector<T>& vec, value (*f)(const T&)) { +value set2CamlList(const set<T>& vec, value (*f)(const T&)) { CAMLlocal2( cli, cons ); cli = Val_emptylist; - for (int i = vec.size() - 1; i >= 0; i--) { + for (typename set<T>::const_reverse_iterator it = vec.rbegin(); + it != vec.rend(); + ++it) + { cons = caml_alloc(2, 0); - Store_field( cons, 0, f(vec[i]) ); // head - Store_field( cons, 1, cli ); // tail + Store_field( cons, 0, f(*it) ); // head + Store_field( cons, 1, cli ); // tail cli = cons; } @@ -39,18 +43,24 @@ value vector2CamlList(const vector<T>& vec, value (*f)(const T&)) { -static CAMLprim value pair2caml(const pair<int,bool>& t) { +static CAMLprim value pair2caml(const int& t) { CAMLlocal1( abc ); abc = caml_alloc(2, 0); - Store_field( abc, 0, Val_int(t.first)); - Store_field( abc, 1, Val_bool(t.second)); + bool neg = t < 0; + Store_field( abc, 0, Val_int(neg ?(-t) : t)); + Store_field( abc, 1, Val_bool(!neg)); return abc; } -static CAMLprim value vector2caml(const vector<pair<int,bool> >& vt) { - return vector2CamlList(vt, pair2caml); +static CAMLprim value set2caml(const set<int>& vt) { + return set2CamlList(vt, pair2caml); } +static CAMLprim value setset2caml(const set<set<int> >& vt) { + return set2CamlList(vt, set2caml); +} + + CAMLprim value gmlRules_stub(value modalities) { // parse caml-modalities to a C++ vector CAMLparam1( modalities ); @@ -70,7 +80,7 @@ CAMLprim value gmlRules_stub(value modalities) { GMLConclusion rulevec = gmlmip_onestep_gml(modvec); // convert rulevec into ocaml list of pairs CAMLlocal1( res ); - res = vector2CamlList(rulevec, vector2caml); + res = set2CamlList(rulevec, setset2caml); CAMLreturn( res ); }