users-prolog
[Top][All Lists]
Advanced

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

Re: Calling Prolog from C


From: Daniel Diaz
Subject: Re: Calling Prolog from C
Date: Tue, 30 Oct 2001 21:50:26 +0100

Hi Ali,

If I understand well, what you want is a list of solutions (each solution being 
a list) like a
findall/3.

The problem of your version comes from the fact that Complex terms are created 
on the heap - which
is destroyed at backtracking (in the all_op example this does not occur since 
atoms are simple
elements not residing on the heap). The solution consists in keeping in a C 
data structure all
solutions (using a 2D array - since each solution is a list of simple terms - 
integers). After the
last solution is found, each intermediate list is created using 
Mk_Proper_List() and the result of
each call (a PlTerm) is stored in an array which is then used to unify using 
Un_Proper_List_Check().

Here is the corresponding C code.

Enjoy.


/* file p.c :*/

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

#define BIGGEST_SOL 5

typedef struct
{
  int nb_elem;  /* size of a solution */
  PlTerm list[BIGGEST_SOL];  /* the solution */
}OneSol;

Bool
wstat(PlTerm List, int P, int K, int Rmax)
{
  OneSol L[1024];  /* array of solutions */
  PlTerm res[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].nb_elem = i = Rd_Proper_List(args[0], L[nbr_sol].list);
      nbr_sol++;
      result = Pl_Query_Next_Solution();
    }
  Pl_Query_End(PL_RECOVER);
  
  for(i = 0; i < nbr_sol; i++)
      res[i] = Mk_Proper_List(L[i].nb_elem, L[i].list);

  printf("%d solutions\n", nbr_sol);

  return Un_Proper_List_Check(nbr_sol, res, List);
}



ED-DBALI AbdelAli wrote:
> 
> 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
> $
> 
> _______________________________________________
> Users-prolog mailing list
> address@hidden
> http://mail.gnu.org/mailman/listinfo/users-prolog

-- 
Daniel Diaz   address@hidden
http://pauillac.inria.fr/~diaz
gprolog --version 2>&1 | sed -n -e 's/By //p'



reply via email to

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