Python prolog library

Is there any python library which is support prolog unification?

Thanks

Anyone can help?

My guess is: There isn’t. You’ll probably need to go the way over C:

Hope this helps,
Volker

The reason I did not answer yesterday when I first saw the question is because I don’t know of any that I would trust.

For example this code: bi/lib/bi_engine.py

Note: I found the reference from Implementing the Prolog Unification algorithm in Python? Backtracking which I answered many years ago.

bi_engine.py looks to have all of the correct pieces but I have never used it. From many years of seeking such code in the distant past of more than a decade, I have learned that one should have a very extensive set of test cases that the code should pass. Almost enviably the code will fail a test and then trying to fix the code is not worth effort. Many will not do cut correctly.

In the long run I have found that the better way to use a Prolog query from another programming language such as Python is to use a foreign language interface, e.g. for Python to SWI-Prolog see pyswip. Now having used such interfaces I have come to learn that it is much easier to just solve the problem in Prolog first than trying to work in the other language and force everything through the interface. In other words, a single call across the interface is much easier to debug than hundreds of calls to different predicates across the interface.

If you want to move into more of a modern way of doing this, then I would suggest starting up a Docker container that runs SWI-Prolog and implement a JSON interface. I have not done this but I think the SWISH image might.

HTH

3 Likes

You can also use Python pengines

It’s pretty straightforward to launch a prolog server (either with SWI installed, or via Docker) and have Python code communicate with it.