Search code examples
cgnu-prolog

Examples for calling (GNU) Prolog from C as stand-alone


I want to build an executable based on a C-File in which Prolog predicates are used. I want to use GNU Prolog.

I succeed (based on the gnu prolog tutorial) to build the examp_c.c, examp.pl:

#include <string.h>
#include <gprolog.h>

PlBool
my_call(PlTerm goal)

{
  PlTerm *arg;
  int functor, arity;
  int result;

  arg = Pl_Rd_Callable_Check(goal, &functor, &arity);
  Pl_Query_Begin(PL_FALSE);
  result = Pl_Query_Call(functor, arity, arg);
  Pl_Query_End(PL_KEEP_FOR_PROLOG);
  return (result == PL_SUCCESS);
}

Compiled using

gplc examp.pl examp_c.c

When calling the executable examp the Prolog interpreter is started / emulated:

GNU Prolog 1.4.4 (64 bits)
Compiled Aug  3 2013, 20:06:22 with gcc
By Daniel Diaz
Copyright (C) 1999-2013 Daniel Diaz
| ?- 

But what I want is to have a C Program internally calling Prolog without communicating with the user via the Prolog interpreter. Either the Prolog predicates should be submitted as arguments of/within the C Program or are internally generated.

Do you know examples for this? Or did I misunderstood something, is it not possible?

You see, I just start as newbie... I would be happy to get some help :-) Many thanks in advance.


Solution

  • You can also directly write a main() in C and then inkove Prolog from C. See Defining a new C main function in the manual.