Prolog commands called and executed within Python using PySWIP

Thank you all.
All the previous exercises of Prolog with Raspberry were successfully worked out thanks to you all here. Now, this is my attempt to learn something combining Prolog commands with Python program, using PySWIP. I made something wrong since didn’t get the correct response as it shows below. Any small comment will be humbly appreciated.

pi@raspberrypi:~ $ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
[GCC 6.3.0 20170516] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

I think that I spelled it well, I don’t understand what the “invalid syntax” may be:

from pyswip import Prolog
prolog = Prolog() >>> prolog.assertz(“father(michael,john)”) >>> prolog.assertz(“father(michael,gina)”) >>> List(prolog.query(“father(michael,X)”)) [{‘x’: ‘john’}, {‘X’: ‘gina’}]
for soln in prolog.query(“father(X,Y)”):
… print soln[“X”], “is the father of”, soln[“Y”] …
^
SyntaxError: invalid syntax

You are showing Python code. I think Python is complaining about your code.

This is a forum about Prolog code.

Abaljeu,
Thanks for your assistance but this demonstration works with a bridge program between Prolog and Python called PySWIP, which allows Prolog commands to be called and executed within a python program in order to practice Artificial Intelligence with Raspberry Pi and some minimal electronics.
I am only a beginner and this is an experiment contained in the book “Beginning artificial Intelligence with the Raspberry Pi” whose author is professor Donald J. Norris from Southern North Hampshire University.

Have you considered directing your questions to professor Donald J. Norris directly? After all, he wrote the book, he should know best how he meant it.

1 Like

I stand by what I wrote. Yes PySwip invokes Prolog, but Python usage is the problem here. I don’t know python enough to answer, which is why I suggest asking a Python group.

The “...” is typically from the Python interpreter if you’re entering this interactively. Note that indentation is required but the amount of indentation is up to you.

The ">>>"s are also from the Python interpreter.

What you should do is either type the lines interactively or put everything into a file.
Here, I’ve put it into example.py (my shell prompt character is “$”):

$ cat example.py
from pyswip import Prolog
prolog = Prolog()
prolog.assertz("father(michael,john)")
prolog.assertz("father(michael,gina)")
for soln in prolog.query("father(X,Y)"):
    print(soln["X"], "is the father of", soln["Y"])

$ python3.7 example.py
michael is the father of john
michael is the father of gina

(I’ve also changed your program to be valid Python 3 (because Python 2 will soon be deprecated.)
Also, if you’re dong a copy and paste, watch out for editors that do “smart quotes” (changing the straight quotes "..." into “...”).

2 Likes

Peter, I am very grateful for your suggestion. I’ve tried this over and over with no success. Also notice that I am working this with Python3 and with two different queries according to two models that i received in this forum,
This is your model: ( maybe it is too short or I didn’t get your Idea. I am so neophyte in this)
Any idea from you will be greatly and humbly appreciated.

pi@raspberrypi:~ python3 -m venv pyswip_env pi@raspberrypi:~ source pyswip_env/bin/activate
(pyswip_env) pi@raspberrypi:~ $ python
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

from pyswip import Prolog >>>prolog = Prolog()
prolog.assertz(“father(michael,john)”) >>>prolog.assertz(“father(michael,gina)”) for soln in prolog.query(“fatherX,Y)”): …print(soln, “is the father of”, soln[“Y”]) …
File “”, line 1
from pyswip import Prolog >>>prolog = Prolog()
prolog.assertz(“father(michael,john)”) >>>prolog.assertz(“father(michael,gina)”) for soln in prolog.query(“fatherX,Y)”): …print(soln, “is the father of”, soln[“Y”]) …
^
SyntaxError: invalid syntax

Also this other one more elaborated, I think.

(pyswip_env) pi@raspberrypi:~ $ python
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

from pyswip import Prolog >>> prolog =Prolog()
prolog.assertz(“father(michael,john)”)
prolog.assertz(“father(michael,gina)”)
list(prolog.query(“father(michael,X)”)) [{‘X’: ‘john’}, {‘X’: ‘gina’}]
for soln in prolog.query(“fatherX,Y)”): … print(soln[“X”],“is the father of”, soln[“Y”]) …
File “”, line 1
from pyswip import Prolog >>> prolog = Prolog() >>> prolog.assertz(“father(michael,john)”) >>> prolog.assertz(“father(michael,gina)”) >>> list(prolog.query(“father(michael,X)”))
[{‘X’: ‘john’}, {‘X’: ‘gina’}]
for soln in prolog.query(“fatherX,Y)”): … print(soln[“X”], “is the father of”, soln[“Y”]) …
^
SyntaxError: invalid syntax

Try this.

Using whatever editor you prefer, create file example.py in your current directory and put this text into it:

from pyswip import Prolog
prolog = Prolog()
prolog.assertz("father(michael,john)")
prolog.assertz("father(michael,gina)")
for soln in prolog.query("father(X,Y)"):
    print(soln["X"], "is the father of", soln["Y"])

Then run this command:

python example.py

You should get this output:

michael is the father of john
michael is the father of gina

If you get an error message like ModuleNotFoundError: No module named 'pyswip', then you’ll need to install that package, e.g., by a command like this:

python -m pip install pyswip
1 Like

Hello Peter,

Thank you. This really was so simple. I’m grateful to all the people who have helped me with this obstacle, and thanks to you for helping to finish weeks of struggle.

This is what I’ve got now with Geany ( I couldn’t make it with any other editor):

from pyswip import Prolog

prolog = Prolog()

prolog.assertz(“father(michael,john)”)

prolog.assertz(“father(michael,gina)”)

for soln in prolog.query(“father(X,Y)”):

print(soln[“X”], “is the father of”, soln[“Y”])

Then I clicked on the paper plane icon and voila:

(‘michael’, ‘is the father of’, ‘john’)

(‘michael’, ‘is the father of’, ‘gina’)

I finally got the previous output. The only problem now is how to get rid of the parenthesis, commas and quotes. Your help continues being highly appreciated.

Victor

Hello Peter,
I pasted your query directly to the Raspberry terminal with Python 2.7.13 and worked!
I just typed just python (no iteration number).
My problem was that I insisted to use Python 3.
so this time I didn’t need editor or new folder. Just pasted your query and voila!
so it worked both ways.
Thank you.

pi@raspberrypi:~ $ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
[GCC 6.3.0 20170516] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

from pyswip import Prolog
prolog = Prolog()
prolog.assertz(“father(michael,john)”)
prolog.assertz(“father(michael,gina)”)
for soln in prolog.query(“father(X,Y)”):
… print(soln[“X”], “is the father of”, soln[“Y”])

(‘michael’, ‘is the father of’, ‘john’)
(‘michael’, ‘is the father of’, ‘gina’)

I don’t think so. Python3 is very similar to Python2. In this particular case, the only difference is that Python3 treats print as a function call but Python2 treats it as a reserved word … so, in Python3, the parentheses are required in print(soln[“X”], “is the father of”, soln[“Y”]) but Python2 allows print soln[“X”], “is the father of”, soln[“Y”]. However, Python2 allows the parentheses, so print(...) is allowed for both Python2 and Python3. In many cases, you can write code that works for both Python2 and Python3; and there’s a compatibility library.

If you’re going do any significant work with Prolog, I suggest that you use it directly (e.g., with SWI-Prolog); adding in the Python layer makes things more complicated and you don’t get some of the more advanced and useful features of Prolog. And if you’re going to do significant work with Python, I suggest you read some tutorials, such as The Python Tutorial — Python 3.12.0 documentation (and there are many others on the Internet).

1 Like

pi@raspberrypi:~ python example.py ('michael', 'is the father of', 'john') ('michael', 'is the father of', 'gina') pi@raspberrypi:~

I’m reading the tutorials. Thank you , again.