I have a program (C++) that embeds XSB Prolog to use as a constraint solver. I have already written code using the low-level C API to inject facts and run queries. But I am getting stuck on a particular side-problem.
I would like to (for debugging purposes) run a query and then output each term that the query unifies with to a stream. In order to ensure that the outputs are nice, I thought it would be nice to use the output of string:term_to_atom/2
to generate the strings.
So, I'd like to put the query term in register 1, run xsb_query()
, and then run string:term_to_atom/2
on the results. But running string:term_to_atom/2
is a query itself, and you can't run xsb_query()
when you are in the middle of a query.
I tried using xsb_query_save()
, hoping that I could then do a sub-query, followed by xsb_query_restore()
, but that doesn't appear to work. The call to my sub-query still bombs out because there is already a query in progress.
I thought about saving a vector of variables created with p2p_new()
that have been unified using p2p_unify()
with reg_term(1)
, but I have no idea how or when these terms might get garbage collected, as I see no way for XSB Prolog to know that my C program is using them. (Unless I am supposed to call the undocumented p2p_deref()
on them when I am done with them?)
Finally, I would like to do this in a single query (if possible) in order to avoid cluttering up the namespace with what would amount to a temporary rule. But maybe I am trying too hard and I should be using another approach entirely. Ideas?