Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pyswip中生成子句_Python_Prolog_Swi Prolog - Fatal编程技术网

Python 如何在pyswip中生成子句

Python 如何在pyswip中生成子句,python,prolog,swi-prolog,Python,Prolog,Swi Prolog,这里有人能帮我用pyswip在python中创建prolog子句吗 数据库: man(peter). woman(adam). man(jonathan). man(paul). woman(cloe). father(jonathan, peter). father(pierre, adam). brother(pierre, paul). father(pierre, cloe). 这些都是功能 child(X, Y) :- father(Y,X). son(X, Y) :- man(X)

这里有人能帮我用pyswip在python中创建prolog子句吗

数据库:

man(peter).
woman(adam).
man(jonathan).
man(paul).
woman(cloe).
father(jonathan, peter).
father(pierre, adam).
brother(pierre, paul).
father(pierre, cloe).
这些都是功能

child(X, Y) :- father(Y,X).
son(X, Y) :- man(X) , father(Y, X).
daughter(X, Y) :- woman(X), father(Y, X).
brother(X, Y) :- man(X), father(Z, Y), father(Z, X).
sister(X, Y) :- woman(X), father(Z, Y), father(Z, X).

如何通过pyswip在python中定义这些prolog函数我现在没有时间做详细的回答,稍后我会更新它,但这里有一个简单的python接口示例,我对一个玩reversi的prolog程序做了一个

#!/usr/bin/python

import sys
from pyswip import Prolog, Functor, Variable, Query

prolog = Prolog()
prolog.consult('./reversi_game.pl')
prolog.consult('./alphabeta.pl')

start_board = Functor("startBoard", 1)
b = Variable()
start_board_query = Query(start_board(b))
start_board_query.nextSolution()
print()
print_board(list(b.get_value())) # an 8*8 grid filled with 0 except at the 4 center squares that have x's and o's
start_board_query.closeQuery()

set_to_x = Functor("setToX", 1)
xp = Variable()
set_player_query = Query(set_to_x(xp))
set_player_query.nextSolution()
x_player = xp.get_value()
print()
print(x_player) # 'x'
set_player_query.closeQuery()
这是怎么回事?要定义谓词接口,您需要创建一个函子,给它一个字符串,该字符串是Prolog中谓词的名称及其arity,您可以根据需要创建任意多的变量,并将它们传递给函子,从而从中创建查询

然后,您可以继续调用查询对象上的
nextSolution()
,只要您愿意,这取决于您需要多少解决方案。如果我没记错的话,当它失败并且停止提供任何解决方案时,结果将是零。然后使用
get_value()
函数提取谓词变量的值

您还可以查看以下内容:

希望能有帮助

编辑:


我知道这个“承诺一个更详细的答案”现在有点晚了,但无论如何。

事实证明你可以做
prolog.assertz(“人(彼得)”)
来断言事实,你也可以做
prolog.assertz(“孩子(X,Y):-父亲(Y,X)”
来添加规则。直到一年后我编辑了我的答案,我意识到您想要从Python在Prolog中创建子句,而不是使用现有谓词和创建Prolog程序的接口。对于创建类似于您想要的子句,我的答案中的链接中提供的示例非常具有解释性。