3
votes

Some background: I am writing a tool that parses code, produces some constraints to be satisfied, and tries to solve them by finding satisfying assignments for the variables in these constraints (the specifics are beyond the point).

I have written the constraint-solving logic in Prolog but the code parsing and constraint generation is in a C++ front-end. So I want to invoke Prolog from C++, which I am currently doing through a SWI-Prolog API. I need to provide the Prolog solver facts about the parsed code (e.g., variable names) and the generated constraints for it to solve them. Right now, I am providing these facts by invoking assert(fact) through the C++ API (having first declared those predicates as dynamic in the Prolog solver) and then by querying the solver. I am weary of using assert that way, however, for facts that are not really dynamic.

Is there a preferred way of providing these non-dynamic facts to the Prolog solver? Or is asserting them my only/best option?

To be clear, I do not want to emit a list of facts in a .pl file, then fire up a separate process with the solver logic that consults this .pl file, because I'd like the results of the solving to be available in my C++ infrastructure from where I invoke the solver.

Sorry if this has been answered, I'm new to Prolog and having trouble coming up with relevant search terms!

Thanks in advance for any advice!

1

1 Answers

0
votes

Since you're using SWI-Prolog and C++, maybe you be interested in some code I wrote interfacing Qt and SWI-Prolog.

Specifically, a simple set of macros (you can find them all here) allows to invoke Prolog' predicates in this way

/** from :/prolog/syncol.pl */
predicate2(syncol_allfile)

..
    try {
        T results, f;
        int rc = syncol_allfile(A(file), results);

        pqTextAttributes ta;

        for (L scanres(results); scanres.next(f); ) {
            psd->add_element_sorted(t2w(f[3]), f[1], f[2], ta[f[4]]);
        }
...

The prolog code:

%% process entire file in Prolog, get back results list
%
:- thread_local frag/4.

syncol_allfile(F, L) :-
    load_source(F),
    xref_source(F),
    open(F, read, S),
    prolog_colourise_stream(S, F, callback_allfile),
    close(S),
    setof(frag(A, B, C, D), frag(A, B, C, D), L).

Well, details can be complicated, some more user friendly info is here... I'm not sure it's still up-to-date, take it easy...