In the previous post, I talked about how Python and Prolog are closer than one might think at a first glance. They are so close, that even both programming languages can be used from the same program.
Some days ago, I found Pyrolog, the Prolog interpreter that comes with the PyPy distribution. It is a Prolog interpreter written in Python. Its design is very clever and impressive. The recursion in Prolog is directly mapped to recursion in Python, making the design of the interpreter simpler. However, the Python stack is somewhat limited, and even a simple Prolog program will provoke a stack overflow. To avoid that, the interpreter keeps track of all the objects allocated in the stack, and moves thins around to heap and back to the stack when necessary. What is even more impressive is that all this work is just the Bachelor’s thesis of Carl Friedich. If that is the Bachelor’s thesis, what this guy will do in the PhD?
Reading that thesis I also finally understood what is the point of PyPy. When I first heard of PyPy I was not convinced at all about it. What’s the point of writing a Python interpreter in Python? But reading Friedich’s thesis I have understood that the point is writing interpreters or compilers for any language (including Python) in Python, which is a high level programming language, resulting in simpler designs and implementations of compilers. They already have a full functional Python 2.5 interpreter, and this Prolog interpreter is just another example of what can be done with it. So if you plan to write a compiler or an interpreter, have first a look at PyPy. It may save you a lot of work.
Pyrolog is not the only implementation of Prolog in Python: Python for fun includes some Prolog interpreters, from a very simple version to a more functional one. It also includes some other examples in Python, which shows two very interesting points. First, that Python is not only a scripting language (as it is often read in the web), and it can be used for many different purposes (including compiler writing). Second, it seems that Prolog is a good candidate if you want to try out how to write an interpreter or a compiler. The full ISO Prolog standard probably contains many details of low interest for learners. But implementing a minimal Prolog, with recursion, the cut, lists handling, etc can be an interesting problem for people learning how to write compilers.
The next impressive creation about Prolog and Python is Pythologic, that allows for Prolog syntax in Python programs. It is not the only solution for logic-like syntax in Python: Yield Prolog allows to write Prolog-like with Python syntax. I have read a little the documentation, and for me it is a little confusing. But if you want to express a problem logically in Python, it supposedly can be done with Yield Prolog.
And finally, IMHO, the most interesting option to combine Prolog and Python: Pyswip, an interface to call SWI Prolog from Python. Pyswip is a foreign language interface for SWI, that allows calling Prolog directly in your Python programs. Think for instance of a chess engine written in Prolog. You could perfectly write a web frontend in Python, that would use the engine calling it directly from Python, getting the best of both of the Python and Prolog worlds. Unfortunately, Pyswip has been orphaned, and it is still uncomplete.
Gracias Ixra! Muy interesante…
Renato.
Thanks for the kind words
.
Nice post. Have you looked at Pyke? I would interested in your thoughts about it?