2
votes

I am implementing a program which need to somehow add facts into a "Prolog" query. But I don't know how to do it. For example: In my "Prolog" database, I have a rule:

engineer(X) :-
    employee(X,department_a).

Now I want to check if jack is an engineer, so the query would be: engineer(jack).

To make this query success, there should be a fact as: employee(jack,department_a). in database. However in my program, I don't want to add (or assert) this fact into database, for reason that the program I am working on is a distributed system so that which department jack belongs to is unknown to database. In other words, I am looking for some way that can make prolog engine deduct based on its own database and given facts (the given fact is not stored in database but along with the query).

Is there any way how can I do it? Any suggestions is appreciated.

1
Can you give a full example, containing the facts that ARE in database and a query, that you would like to implement along with the desired result?Eugene Sh.
What you are asking for? What is Prolog's "own database and given facts"? What does it mean, "along with the query"?user1812457
@Eugene@Boris For example, there is a server and a client. In server side, there is a prolog knowledge database. Now client send a query AND a fact to server. The question I had was: How can server take advantage of the fact it gets from client to do the query? Now I guess I can use assert clause to achieve this goal by inserting the fact first and then doing the query based on knowledge database, then retracting the fact. Is this doable?Yunhe
@Yunhe Yes, it is doable by asertz`retract` on the server side. But I have a feeling, that you can do it easier and in a more making sense way. But since you are not explaining well what is your requirement, it's hard to point it out.Eugene Sh.
@EugeneSh. It is kinda hard to explain it clearly, but this project is designed to store facts in a distributed manner for some concerns. Anyways, I will try assert approach first then see what can happen. Thanks for your reply:)Yunhe

1 Answers

0
votes

You can put facts and rules in different Prolog text files if you want to. And freely mix different Prolog text files from the command line while loading.

For example you could do the following:

File 1: foo.p

engineer(X) :-
    employee(X,department_a).

File 2: bar.p

employee(jack,department_a)

Then you can do on the command line:

?- [foo, bar].
Yes
?- engineer(jack).
Yes
?- engineer(jill).
No

Of course if you load different facts with the same rules, you might get different results in your query.

If you really don't want to consult something in the command line, but instead temporarily assert facts during query execution, you should look into hypothetical reasoning.

Here is an example:

Bonner's Example
http://www.jekejeke.ch/idatab/doclet/prod/en/docs/15_min/10_docu/02_reference/04_examples/01_bonner.html

Bye