python 中的 prolog 代码与 pyswip 不工作

prolog code in python with pyswip not working

我的问题与 this question 有关。

我想将以下序言代码嵌入到 python 程序中:

myEquation(A, B, C):- 
    var(A),number(B),number(C) -> A is B+C;
    var(B),number(A),number(C) -> B is A-C;
    var(C),number(A),number(B) -> C is A-B;
    A =:= B + C.

我安装了 pyswip 包并尝试了以下代码:

from pyswip import Prolog
prolog = Prolog()
prolog.assertz("myEquation(A, B, C):- var(A),number(B),number(C) -> A is B+C; var(B),number(A),number(C) -> B is A-C; var(C),number(A),number(B) -> C is A-B; A =:= B + C.")
prolog.assertz("myEquation(3,1,C).")

但它给我错误:

ERROR: Syntax error: Operator priority clash
ERROR: assertz((myEquation(A, B, C):- var(A),number(B),number(C) -> A is B+C; var(B),number(A),number(C) -> B is A-C; var(C),number(A),number(B) -> C is A-B; A
ERROR: ** here **
ERROR:  =:= B + C.)). 
Traceback (most recent call last):
  File "prologpy.py", line 4, in <module>
    prolog.assertz("myEquation(A, B, C):- var(A),number(B),number(C) -> A is B+C; var(B),number(A),number(C) -> B is A-C; var(C),number(A),number(B) -> C is A-B; A =:= B + C.")
  File "/home/cardio/.local/lib/python3.5/site-packages/pyswip/prolog.py", line 140, in assertz
    next(cls.query(assertion.join(["assertz((", "))."]), catcherrors=catcherrors))
  File "/home/cardio/.local/lib/python3.5/site-packages/pyswip/prolog.py", line 127, in __call__
    "Returned: '", str(term), "'."]))
pyswip.prolog.PrologError: Caused by: 'assertz((myEquation(A, B, C):- var(A),number(B),number(C) -> A is B+C; var(B),number(A),number(C) -> B is A-C; var(C),number(A),number(B) -> C is A-B; A =:= B + C.)).'. Returned: 'error(syntax_error(operator_clash), string(b'assertz((myEquation(A, B, C):- var(A),number(B),number(C) -> A is B+C; var(B),number(A),number(C) -> B is A-C; var(C),number(A),number(B) -> C is A-B; A =:= B + C.)). ', 152))'.

我该如何解决这个问题。我可以使用其他一些与 prolog 相关的 python 包,也可以使用 listed here

谓词定义末尾的.太多了。摆脱它。

例如,这是正确的:

?- assertz((foo :- bar)).

但这不正确:

?- assertz((foo :- bar.)).

如果您阅读示例 provided with pyswip,您应该注意到上面的原生 Prolog 代码转换为:

from pyswip import Prolog
prolog = Prolog()
prolog.assertz("(foo :- bar)")

不是你写的,

prolog.assertz("(foo :- bar.)")