Hello Dr. Diaz, Yes, I simplified the case I attached before.
Generally there are two functions in prolog, foo and foo1. In the main function of Simple.c, prolog's function foo is called (through foo's wrapper). Then in prolog foo, foo1's C wrapper is called. This wrapper would call foo1's implementation inside prolog . The problem is that there is a reentrancy from C to Prolog here " foo C wrapper --> foo " and " foo1 C wrapper --> foo1". and the problem is that when execution flow return back to foo, the TMP variable can not be binded (line 6 in babel.pl). I have tested other cases, and similar issues happened. Am I clear enough? Thank you a lot! Best, Shuai On Wed, Aug 13, 2014 at 3:24 AM, Daniel Diaz <daniel.d...@univ-paris1.fr> wrote: > Hi, > > sorry for the late reply. There are possibly reentrancy issues with > gprolog. However, your example is too complex as is to be used for > debugging purpose. Could you minimize it as most as possible so I could try > to understand what happens ? > > Thank you > > Daniel > > > Le 24 juil. 2014 à 20:24, Shuai Wang <wangshuai...@gmail.com> a écrit : > > Hello prolog users, > > > I just asked a question in this email list about > "why failed in process of Prolog call C call Prolog" > > Now after several test, I have a more general question which confused > me... > > I myself have some experience about using C call Prolog and > using Prolog to call C in GNU-Prolog, however, I have several > strange problems in recent test of procedure below: > > C -- call -- > Prolog -- call --> C -- call --> Prolog > -- return --> C -- return --> Prolog (fail..) > > *So basically, is the Prolog runtime reentrantable in Prolog --> C --> > Prolog* > *procedure..?* > > > I attached my test code in the email, in this code, the weird thing is > that return value of *TMP *in Prolog function *foo * can not be binded. > > BTW: Is it something related to the arguments of > Pl_Query_Begin; Pl_Query_End ? > > > Best, > Shuai > > > -- > Ce message a été vérifié par MailScanner <http://www.mailscanner.info/> > pour des virus ou des polluriels et rien de suspect n'a été trouvé. > <simple.cil.c><babel.c><babel.pl><Makefile> > _______________________________________________ > Users-prolog mailing list > Users-prolog@gnu.org > https://lists.gnu.org/mailman/listinfo/users-prolog > > > > -- > Ce message a été vérifié par MailScanner <http://www.mailscanner.info/> > pour des virus ou des polluriels et rien de suspect n'a été trouvé. >
:- foreign(babel_c_0(+integer, -integer)). :- foreign(babel_c_1(+string, +integer)). foo(__RETRES3) :- babel_c_0(23 ,TMP), write(TMP),nl, A is TMP, babel_c_1('%d\n', A), __RETRES3 is 0. foo1(N, T) :- BabelExp_0 is N + 1, T is BabelExp_0.
#include <gprolog.h> #include <stdio.h> #include <stdlib.h> PlBool babel_c_0(int arg_0, int * babel_ret) { *babel_ret = foo1(arg_0); printf("%d\n", *babel_ret); return PL_TRUE; } PlBool babel_c_1(char* arg_0, int arg_1) { printf(arg_0, arg_1); return PL_TRUE; }
Makefile
Description: Binary data
#include <gprolog.h> int babel_wrapper_foo1(int n) { int return_value; int func; PlTerm arg[2]; PlBool res; func = Pl_Find_Atom("foo1"); arg[0] = Pl_Mk_Integer(n); arg[1] = Pl_Mk_Variable(); res = Pl_Query_Call(func, 2, arg); return_value = Pl_Rd_Integer(arg[1]); return return_value; } int babel_wrapper_foo() { int return_value; int func; PlTerm arg[1]; PlBool res; func = Pl_Find_Atom("foo"); arg[0] = Pl_Mk_Variable(); res = Pl_Query_Call(func, 1, arg); return_value = Pl_Rd_Integer(arg[0]); return return_value; } int foo1(int n ) ; int foo(void) { int a ; int tmp ; int __retres3 ; return babel_wrapper_foo(); } int foo1(int n ) { return babel_wrapper_foo1(n); } int main(int argc , char **argv ) { Pl_Start_Prolog(argc, argv); foo(); Pl_Stop_Prolog(); return 0; }
_______________________________________________ Users-prolog mailing list Users-prolog@gnu.org https://lists.gnu.org/mailman/listinfo/users-prolog