users-prolog
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Calling Prolog from C


From: ED-DBALI AbdelAli
Subject: Calling Prolog from C
Date: Tue, 30 Oct 2001 18:49:00 -0500

Hi,

It is the first time I try to use this feature. I tried to adapt the example 
8.4.3 (recovering the list of all operators) to my problem... but with no 
success :(
I want to use a predicate called 'p' which returns a list of integers. I 
replaced the line 

op[n++] = Mk_Atom(Rd_Atom(args[2])); /* arg #2 is the name of the op */

by

L[nbr_sol++] = Mk_Proper_List(Rd_List_Check(args[0]));
          /* arg #0 is the solution list of p */

(1) How can I use the elements of L? (elements of L are PlTerm)
(2) return Un_Proper_List_Check(n, L, List);
    raises a Segmentation fault
(3) How can I use List inside the C program? (what is a PlTerm from C point of 
view)

Many thanks.                    A. ED-DBALI
-------------------------------------
PS : The entire program is:

/* file p.pl */

:- foreign(wstat(term,+positive,+positive,+positive)).

p([1,2,3],_,_,_).
p([4,5,6],_,_,_).
...

/* file p.c :*/

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

      Bool
      wstat(PlTerm List, int P, int K, int Rmax)
      { PlTerm L[1024];
        PlTerm args[4];
        int j, i, nbr_sol = 0;
        int result;

        Pl_Query_Begin(TRUE);
        args[0] = Mk_Variable();
        args[1] = Mk_Integer(P);
        args[2] = Mk_Integer(K);
        args[3] = Mk_Integer(Rmax);
        result = Pl_Query_Call(Find_Atom("p"), 4, args);
        while (result)
          {
            L[nbr_sol++] = Mk_Proper_List(P+1, Rd_List_Check(args[0])); /* arg 
#0 is the solution vector */
            result = Pl_Query_Next_Solution();
          }
        Pl_Query_End(PL_RECOVER);
        
        printf("%d solutions\n", nbr_sol);

        return Un_Proper_List_Check(nbr_sol, L, List);
      }


Compilation : $ gplc -o p p.pl p.c
Execution : 
$ p
GNU Prolog 1.2.8
By Daniel Diaz
Copyright (C) 1999-2001 Daniel Diaz
| ?- wstat(W,2,7,4).
2 solutions
Erreur de segmentation
$



reply via email to

[Prev in Thread] Current Thread [Next in Thread]