从 python 调用 prolog 谓词

发布于 2024-11-08 22:10:28 字数 533 浏览 4 评论 0 原文

我有一些 .pl 文件,我想从 python 脚本调用其中声明的谓词。我怎样才能做到这一点?

例如, 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 ), !.

工作方式就像

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

我想从 python 脚本中以某种方式调用 rD 并在结果变量

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

ps 中获取答案:这只是一个示例,我不这样做想重写我当前的Prolog程序。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

就是爱搞怪 2024-11-15 22:10:28

并不是说我有直接的经验,但是有一个名为 PySWIP 的项目提供了Python 和 SWI-Prolog 之间的桥梁。 Google 代码项目页面上托管的 wiki 包含 安装说明和一些使用示例

编辑(2019 年 7 月 5 日)

PySWIP 现在似乎在 Github 上维护,有自己的安装说明。 TLDR:对于 Python 2 和 3,安装 SWI-Prolog 和 pip install pyswip 应该可以完成这项工作。

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.

私野 2024-11-15 22:10:28

由于您不想“重写我当前的 Prolog 程序”,我认为自然的方法是从 Python 到 SWI-Prolog 进行外部调用,并传递适当的命令行参数。

看一下这个 SO 讨论,How to call external command in Python,2008 年 9 月开始。 subprocess 模块允许将来自外部命令的标准输出通过管道传送到 Python 进程并在那里作为流读取。

这将问题减少到选择 SWI-Prolog 的命令行参数。可以通过类 Unix 系统上的 shell 脚本或 Windows 上的“DOS”批处理/cmd 文件来间接调用 SWI-Prolog,但我将省略对这种间接调用的进一步提及。

特别参见第 2 节中的讨论。 SWI-Prolog 手册(上面链接)的 2.4.2 中的 -g-t 选项。例如:

swipl --quiet -t rD( [1,2,3,4,5,4], X ),halt

可能会做你想做的事。 --quiet 选项会抑制横幅/欢迎消息,您可能希望简化对 Python 接收到的输出的解析。

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.

Take a look at this SO discussion, How to call external command in Python, from Sept. 2008. Using the subprocess module allows the stdout from an external command to be piped to the Python process and read as a stream there.

This reduces the problem to choosing command line arguments for SWI-Prolog. It is possible to invoke SWI-Prolog indirectly, through a shell-script on Unix-like systems or a "DOS" batch/cmd file on Windows, but I'll omit further mention of such an indirect call.

See especially the discussion in Sec. 2.4.2 of the SWI-Prolog manual (linked above) of -g and -t options. For example:

swipl --quiet -t rD( [1,2,3,4,5,4], X ),halt

will likely do what you want. The --quiet option suppresses the banner/welcome message, which you probably want to simplify parsing the output as received by Python.

寒冷纷飞旳雪 2024-11-15 22:10:28

在撰写本文时,PyPI 中的 Python3、PySwip 更新仅适用于旧版 Python,但 github 上的源代码< /a> 与 Python3 兼容。你可以 git clone 这个,运行 python3 setup.py install ,它会给你一个 Python3 版本。

要查阅现有的知识库,存储为knowledge_base.pl:

from pyswip import Prolog
prolog = Prolog()
prolog.consult("knowledge_base.pl")
for res in prolog.query("rD( [1,2,3,4,5,4], X )."):
    print(res)

# output:
# {'X': [1, 2, 3, 4, 5]}

An update for Python3, PySwip in PyPI at the time of writing is for legacy Python only, but the source code on github is Python3 compatible. You can git clone this, run python3 setup.py install and it'll give you a Python3 version.

To consult an existing knowledge base, stored as knowledge_base.pl:

from pyswip import Prolog
prolog = Prolog()
prolog.consult("knowledge_base.pl")
for res in prolog.query("rD( [1,2,3,4,5,4], X )."):
    print(res)

# output:
# {'X': [1, 2, 3, 4, 5]}
耳钉梦 2024-11-15 22:10:28
from subprocess import Popen, PIPE, STDOUT

p = Popen('/usr/local/sicstus4.2.3/bin/sicstus', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
cmd = open('/path/to/your/test.pl').read()
res = p.communicate(cmd)
for line in res:
    print line
from subprocess import Popen, PIPE, STDOUT

p = Popen('/usr/local/sicstus4.2.3/bin/sicstus', shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
cmd = open('/path/to/your/test.pl').read()
res = p.communicate(cmd)
for line in res:
    print line
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文