Negative integer passing from C to GNU-Prolog

Shuai Wang <[email protected]>
Newsgroups gmane.comp.gnu.prolog.general
Message-ID <CAEQMQomb0JYEKW1_G3StBvVxB65TzSdhBR35ur7-0M=tU+CA6A@mail.gmail.com>
Hello Prolog users,

I am trapped in passing negative integer value from C to Prolog using the
interface.

Here is the thing,
I have two files, one Prolog source code and a C source code.

Basically a variable defined in C has the value (*-2*), and the Prolog code
tries to read it in.

I use the standard interface like this in Prolog to get the value:

:- foreign(c_foo( -integer)).

However, even though the value defined in C code is *-2*, what I got in
Prolog (I write it out in Prolog after the above wrapper return) is
 4294967294


Follow the instruction
<http://www.gprolog.org/manual/html_node/gprolog068.html>,  I tried other
Foreign Type (positive, number ) which are like

:- foreign(c_foo( -positive)).

:- foreign(c_foo( -number)).

But it still gives me the wrong answer...

So I am wondering if it is possible to pass a negative integer from C to
GNU-Prolog..?

The attachments are the source code and Makefile, I have simplified it and
hope it could be understandable.

*The test is on Linux 14.04 64bit, GNU -Prolog 1.3.0*

Am I clear enough? Could anyone give me some help?

Best,
Shuai

_______________________________________________
Users-prolog mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/users-prolog
main.c (text/x-csrc, 602 B)
#include <gprolog.h>

int  babel_wrapper_foo(int  n)
      {
         int  return_value;

        int func;
        PlTerm arg[2];
        PlBool res;

        func = Pl_Find_Atom("foo"); 

        Pl_Query_Begin(PL_FALSE);

      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]);

        Pl_Query_End(PL_KEEP_FOR_PROLOG);

        return return_value;
        }
      
int main(int argc , char **argv ) 
{ 
  Pl_Start_Prolog(argc, argv);
  babel_wrapper_foo(1);
  Pl_Stop_Prolog();
  return (0);
}
test.pl (text/x-perl-script, 79 B)
:- foreign(c_foo( -integer)).

foo(N, A_SSA_2) :- 
c_foo(TMP),
write(TMP), nl.
test.c (text/x-csrc, 117 B)
#include <gprolog.h>
#include <stdio.h>
#include <stdlib.h>

PlBool c_foo(int * ret) 
{
*ret = -2;
return PL_TRUE;
}
Makefile (application/octet-stream, 54 B) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.