Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: Is there a way a swipl query results in value true not {}?

To call prolog programs in python there is the libary pyswip.

Is there a possibility to get information if a rule for a given value is true or false. If I call a fact in console from swipl I get e.g.

?- dad(a).
true.

If I try the same in python with:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from pyswip import Prolog, registerForeign

from pyswip import Prolog
prolog = Prolog()

prolog.assertz("dad(peter)")

for res in prolog.query("dad(peter)."):
    print(res)

It gives me only the output:

{}

and not True. Is there another function I can call to get True or False at this point or is the expected value of the query function in that case {}?

>Solution :

From Prolog’s point of view, {} is the expected result. When a Prolog query succeeds, Prolog prints a set of answer substitutions:

?- dad(X).
X = peter.

As a special case, if no variables from the query are bound, Prolog needs to indicate that (a) the query succeeded, and that (b) no variables were bound. Traditionally, this is done by printing something like yes or true. But this is just a shorthand for "success, and there were zero substitutions". And it’s only a printing convention, not a "return value" in any real sense. Prolog has no return values.

If you use a foreign library that represents success-and-a-set-of-answer-substitutions using the language’s set type, then it makes sense that success-and-zero-substitutions is represented as a zero-element set, i.e., {}.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading