From 79c5fa447b23bca54ac811efda2bcee8117d4a75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20Wi=C3=9Fmann?= <edu@thorsten-wissmann.de> Date: Wed, 29 Jan 2014 17:57:17 +0100 Subject: [PATCH] Translate GMLMIP formulas back --- gmlmip.ml | 2 +- gmlmip.mli | 2 +- gmlmip_stub.cc | 30 ++++++++++++++++++++---------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/gmlmip.ml b/gmlmip.ml index 3243083..72b47f3 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 dca49b6..4a911ac 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 54ef097..8548e27 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 ); } -- GitLab