# How to assert predicates from the external programs?

**URL:** <https://swi-prolog.discourse.group/t/how-to-assert-predicates-from-the-external-programs/5045>\
**Category:** Help!\
**Created:** [February 22, 2022, 9:08am UTC](https://swi-prolog.discourse.group/t/how-to-assert-predicates-from-the-external-programs/5045 "2022-02-22T09:08:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vancaho](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/vancaho/32/4243_2.png) [@vancaho](https://swi-prolog.discourse.group/u/vancaho)\
**Post date:** [February 22, 2022, 9:08am UTC](https://swi-prolog.discourse.group/t/how-to-assert-predicates-from-the-external-programs/5045/1 "2022-02-22T09:08:08Z")

</div>

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:

```prolog
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.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 22, 2022, 10:36am UTC](https://swi-prolog.discourse.group/t/how-to-assert-predicates-from-the-external-programs/5045/3 "2022-02-22T10:36:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vancaho](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/vancaho/32/4243_2.png) [@vancaho](https://swi-prolog.discourse.group/u/vancaho)\
**Post date:** [February 22, 2022, 11:38am UTC](https://swi-prolog.discourse.group/t/how-to-assert-predicates-from-the-external-programs/5045/4 "2022-02-22T11:38:43Z")

</div>

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 :

```prolog
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.

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [February 22, 2022, 12:26pm UTC](https://swi-prolog.discourse.group/t/how-to-assert-predicates-from-the-external-programs/5045/5 "2022-02-22T12:26:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JasonMorris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jasonmorris/32/3824_2.png) [@JasonMorris](https://swi-prolog.discourse.group/u/JasonMorris)\
**Post date:** [February 22, 2022, 7:53pm UTC](https://swi-prolog.discourse.group/t/how-to-assert-predicates-from-the-external-programs/5045/6 "2022-02-22T19:53:39Z")

</div>

I think you are looking for swiplserver.

---

<div class="post-metadata">

**Author:** ![JCR](https://avatars.discourse-cdn.com/v4/letter/j/258eb7/32.png) [@JCR](https://swi-prolog.discourse.group/u/JCR)\
**Post date:** [February 22, 2022, 10:11pm UTC](https://swi-prolog.discourse.group/t/how-to-assert-predicates-from-the-external-programs/5045/7 "2022-02-22T22:11:40Z")

</div>

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

```prolog
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 🙂

---

<div class="post-metadata">

**Author:** ![vancaho](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/vancaho/32/4243_2.png) [@vancaho](https://swi-prolog.discourse.group/u/vancaho)\
**Post date:** [February 23, 2022, 9:49am UTC](https://swi-prolog.discourse.group/t/how-to-assert-predicates-from-the-external-programs/5045/8 "2022-02-23T09:49:19Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![JasonMorris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jasonmorris/32/3824_2.png) [@JasonMorris](https://swi-prolog.discourse.group/u/JasonMorris)\
**Post date:** [March 3, 2022, 12:39am UTC](https://swi-prolog.discourse.group/t/how-to-assert-predicates-from-the-external-programs/5045/9 "2022-03-03T00:39:22Z")

</div>

See [Google Colab](https://colab.research.google.com/gist/JasonMorrisSC/781feb7f8e9bf5404637ccb7d8ae9a73/swi-prolog-inside-python.ipynb).
