Getting list elements from Prolog in a C interface -
i try implement c interface using prolog script based on gnu prolog. problem single elements of nested prolog list.
actually c code looks like
... int func; plterm arg[10]; plterm *sol_gb; plbool res; int nmb; char *strhead; char *strtail; pllong nummero; plterm pl_nummero; pl_start_prolog(argc, argv); pl_query_begin(pl_true); arg[0] = pl_mk_string(strrname); arg[1] = pl_mk_variable(); arg[2] = pl_mk_variable(); arg[3] = pl_mk_string("true"); res = pl_query_call(func, 4, arg); sol_gb = pl_rd_list(arg[2]); nmb = pl_list_length(sol_gb[0]); strhead = pl_write_to_string(sol_gb[0]); printf("strhead = %s\n",strhead); strtail = pl_write_to_string(sol_gb[1]); printf("strtail = %s\n",strtail); ...
the prolog list returned in arg[2] looks like
[ [ spezial bolognese, [2, ,zwiebeln,300,gramm,hackfleisch,10, ,tomaten, 100,ml,sahne,500,gramm,spaghetti] ], [ spaghetti bolognese, [2, ,zwiebeln gehackt,300,gramm,hackfleisch,10, ,fleischtomaten, 100,ml,sahne,500,gramm,spaghetti] ] ]
the output of conversion string is
strhead = [spezial bolognese,[2, ,zwiebeln gehackt,300,gramm,hackfleisch, 10, ,fleischtomaten,100,ml,sahne,500,gramm,spaghetti]] strtail = [[spaghetti bolognese,[2, ,zwiebeln gehackt,300,gramm,hackfleisch, 10, ,fleischtomaten,100,ml,sahne,500,gramm,spaghetti]]]
so assume, "nearly there" have re-activate c knowledge not solution how enter in next level of list each element string ("spezial bolognese", next step: "2", "zwiebeln" etc.).
how can step through prolog list in c?
i happy every hint, thank again!
to content of list c-code can use 2 kind of functions.
first possibility (simple since list seen flat object requires more memory , needs proper list, i.e. not work lists not terminated [])
int pl_rd_proper_list_check(plterm the_prolog_list, plterm *the_array_receiving_arguments);
this functions receives array (it ensure large enough), stores each element of list in array , returns total number of elements. example:
plterm list = ...some prolog list... int nelem = pl_list_length(list); plterm *elem = (plterm *) calloc(nelem, sizeof(plterm)); pl_rd_proper_list_check(list, elem); int i; for(i = 0; < nelem; i++) { // here there argument in elem[i], let's print pl_write(elem[i]); }
second possibility (more general see list chained list, each cell contains head , tail (a list))
plterm *pl_rd_list(plterm the_prolog_list);
this function returns array of 2 elements corresponding head , tail of received list. function should called on each element of list; either know number of elements or test end of list (e.g. waiting end of list atom []). here code doing (it expected inside above loop since know second argument of list nested list.
plterm list = ... prolog list...; while(!pl_un_atom(pl_atom_nil(), list)) { plterm *lst_arg = pl_rd_list(list); // [0] = head element, [1] = tail list // here there argument in lst_arg[0], let's print pl_write(lst_arg[0]); list = lst_arg[1]; }
in example, first list looks like:
[ 'spezial bolognese', [2,' ','zwiebeln', 300,'gramm','hackfleisch', 10,' ','tomaten', 100,'ml','sahne', 500,'gramm','spaghetti'] ]
so second element nested list. following code uses first method above list (which has 2 elements), , second method nested list:
nelem = pl_list_length(sol_gb[0]); plterm *elem = (plterm *) calloc(nelem, sizeof(plterm)); pl_rd_proper_list_check(sol_gb[0], elem); int i; for(i = 0; < nmb; i++) { if (i != 1) { pl_write(elem[i]); printf("\n"); } else { // know list printf("("); plterm list = elem[i]; while(!pl_un_atom(pl_atom_nil(), list)) { plterm *lst_arg = pl_rd_list(list); // [0] = head element, [1] = tail list printf(" "); pl_write(lst_arg[0]); list = lst_arg[1]; } printf(" )\n"); } }
here should output
spezial bolognese ( 2 zwiebeln 300 gramm hackfleisch 10 tomaten 100 ml sahne 500 gramm spaghetti )
Comments
Post a Comment