How to assert predicates from the external programs?

Hello, I want to build a system which use prolog as the backend reasoning and use other commands to generate some facts which are not available in the prolog facts. For example, I have the following rules:

fact1(a).
fact2(a).
result(X) :- fact1(X), fact2(X), fact3(X).

The above rules declare two facts fact1(a) and fact2(a). If I want to query result(a), prolog will verify whether fact1(a), fact2(a) and fact3(a) hold. For fact1(a) and fact2(a), they hold. But for fact3(a), I want to get the result from the output of another program, e.g. fact_program.

Thanks very much.

The main question is how Prolog communicates with the rest of the system if you already made that connection. If not, the question is how you plan to make that connection? If you have no idea, what is the language of the other part(s)? Are you using some microservice architecture? What about the amount of data that needs to be exchanged and the latency constraints?

Roughly the choice is between adding the fact, calling assertz/1 or friends or make Prolog call the other components to figure out whether some fact holds.

Thanks for your replies. I haven’t made the connection between Prolog and the rest of the system. I want to use python to build the other parts. The amount of data exchanged is not very large and the latency is not an issue.

I can add another rule :

fact3(X) :-
   write('what is true for fact? Please enter:'),
   read(X),
   assertz(fact3(X)).

Firstly I think I can write a python interpreter that reads from the prolog terminal, run the fact_program, parse the output and write the corresponding X into prolog.
But If I query fact3(a) again, prolog does not remember the predicate fact3, and will trigger the rule above again.

Is the above solution right? If it is ok, then how to let prolog remember the fact3?

Thanks very much again.

What you do reminds me a bit of the interactive expert systems that in part have already facts and in part ask the user to fill in missing facts – if needed.

I saw such an implementation in one of the standard Prolog books: The Art of Prolog, page 311 an enhanced meta-interpreter for expert systems.

I think you are looking for swiplserver.

You can also modify your code to check if fact3(X) already holds like this (so as to avoid redundant questions):

fact3(X) :-
   \+fact(X),
   write('what is true for fact? Please enter:'),
   read(X),
   assertz(fact3(X)).

Here \+fact(X) checks if fact(X) does not currently hold; in which case we want to ask the user.

Good luck :slight_smile:

I glanced at the documentation of swiplserver, and I don’t know if I understand it right: I feel that the main purpose of swiplserver is to provide prolog to python. My need feels like to provide python to prolog to use, i.e. python provides some facts to prolog. Please correct me.
Is there any sample code for using swiplserver?

See Google Colab.