Hi,
What's the right way to query Ciao from C? Attached are two files which show what I'm trying (to get at some particular predicates) but, Ciao crashes:
Ciao-Prolog 1.9 #44: Thu May 22 22:21:28 MDT 2003 ?- use_module(library(show_preds)).
yes ?- show_preds(x). orig_var=2, ciao_is_variable? yes goal_var=4, ciao_is_variable? yes ciao_is_structure(goal): yes goal is: some_pred(_var_5) About to begin query goal=3 {ERROR: segmentation violation} { Execution aborted } ?-
(To re-create the above, put the attached two files into your ciao library directory and follow the session as shown.)
- Also, I have no success getting Ciao to accept a 0-arity foreign predicate. The following all fail with a parse error (so says assert anyway): :- true pred show_preds() :: foreign. :- true pred show_preds :: foreign. :- true pred show_preds/0 :: foreign. (where currently I have .. show_preds(in(X)) :: .. is that a real limit or is the syntax of the above wrong?
Thanks for any help,
Jeff
:- module(show_preds, [show_preds/1], [assertions, basicmodes, regtypes, foreign_interface ]). %% This doesn't seem to like /0 predicates; I have tried: %% show_preds :: .. show_preds/0 :: .. show_preds() :: %% So the argument is unused. :- true pred show_preds(in(X)) :: any_term + foreign.
:- use_foreign_source(show_preds_c).
:- impl_defined([show_preds/1]).
#include <stdio.h> #include "ciao_prolog.h"
static void print_ciao_term(ciao_term term);
void show_preds(ciao_term unused_term) { ciao_term orig_var = ciao_var(); ciao_term goal = ciao_structure("some_pred", 1, orig_var); ciao_term goal_var = ciao_structure_arg(goal, 1); ciao_query* query = 0;
printf("orig_var=%d, ciao_is_variable? %s\n", orig_var, ciao_is_variable(orig_var) ? "yes" : "no"); printf("goal_var=%d, ciao_is_variable? %s\n", goal_var, ciao_is_variable(goal_var) ? "yes" : "no");
printf("ciao_is_structure(goal): %s\n", ciao_is_structure(goal) ? "yes" : "no"); printf("goal is:\n"); print_ciao_term(goal); printf("\n");
printf("About to begin query goal=%d\n", goal); query = ciao_query_begin_term(goal); printf("Began ok...\n");
printf("First ciao_query_next query = %p\n", query); while (ciao_query_next(query)) { printf("print_ciao_term(goal)\n"); print_ciao_term(goal); printf("\n... 'nother ciao_query_next\n"); } /* ciao_bool ciao_query_ok(ciao_query *query); ciao_bool ciao_query_next(ciao_query *query); void ciao_query_end(ciao_query *query); */ ciao_query_end(query); }
static void print_ciao_term(ciao_term term) { if (ciao_is_atom(term)) { printf("%s", ciao_atom_name(term)); } else if (ciao_is_variable(term)) { /* most likely not the canonical form of the var.. */ printf("_var_%d", term); } else if (ciao_is_structure(term)) { int i; int a; a = ciao_structure_arity(term); printf("%s(", ciao_structure_name(term)); for (i = 1; i <= a; i++) { print_ciao_term(ciao_structure_arg(term, i)); if (i != a) printf(", "); } printf(")"); } else if (ciao_is_list(term)) { printf("["); print_ciao_term(term); printf("]"); } else if (ciao_is_empty_list(term)) { printf("[]"); } else if (ciao_is_integer(term)) { printf("%d", ciao_to_integer(term)); } else if (ciao_is_number(term)) { printf("%f", ciao_to_float(term)); } else { printf("<unknown/>"); } }