Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call prolog predicate from python

I have some .pl file and I want to call predicate declared in it from python script. How can I do that?

For example, test.pl

rD( [], Ans, Ans ).
rD( [X|Xs], Ans, Acc ) :-
    member( X, Acc ),
    rD( Xs, Ans, Acc ), !.
rD( [X|Xs], Ans, Acc ) :-
    \+member( X, Acc ),
    append( Acc, [X], AccNew ),
    rD( Xs, Ans, AccNew ), !.

Working like

?- rD( [1,2,3,4,5,4], X ).
X = [1, 2, 3, 4, 5].

I want to call rD somehow from python script and get answer in result variable

result
[1, 2, 3, 4, 5]

ps: it is just an example, and I don't want to rewrite my current Prolog program.

like image 717
ДМИТРИЙ МАЛИКОВ Avatar asked May 23 '11 21:05

ДМИТРИЙ МАЛИКОВ


People also ask

How do you call a Prolog in Python?

TLDR: installing SWI-Prolog and pip install pyswip should do the job, for both Python 2 and 3. Show activity on this post. Since you don't want to "rewrite my current Prolog program," I think the natural approach is to make an external call from Python to SWI-Prolog, passing appropriate command line arguments.

What is Python & Prolog?

Prolog is a Logic-based Programming Language, while Python is an Interpreted Programming Language.


1 Answers

Not that I have a direct experience with it, but there is a project called PySWIP that provides a bridge between Python and SWI-Prolog. The wiki hosted on Google Code's project pages holds installation instructions and some usage examples.

EDIT (5th of July 2019)

PySWIP now seems to be maintained on Github, with its own installation instructions. TLDR: installing SWI-Prolog and pip install pyswip should do the job, for both Python 2 and 3.

like image 108
Giulio Piancastelli Avatar answered Sep 30 '22 03:09

Giulio Piancastelli