动态查询序言
Querying prolog dynamically
有没有动态查询序言知识库的方法?
我在文件 family.pl 中有序言逻辑(我在此处找到的示例 http://www.techytalk.info/prolog-programming-gprolog-linux/)。这是它的内容:
mother_child(trude, sally).
father_child(tom, sally).
father_child(tom, erica).
father_child(mike, tom).
sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).
parent_child(X, Y) :- father_child(X, Y).
parent_child(X, Y) :- mother_child(X, Y).
我希望能够在不进入 prolog 解释器的情况下进行查询。
这个命令对我不起作用:
swipl -f family.pl -g "father_child(Father, Child)"
谢谢
查询确实有效:只是如果您以这种方式调用程序,您看不到结果。
因此,您可以自己打印结果,例如:
swipl -f family.pl -g "father_child(Father,Child), \
portray_clause(father_child(Father, Child))"
这产生:
father_child(tom, sally).
?-
这当然不是全部,所以你可以使用false/0
强制回溯:
swipl -f family.pl -g "father_child(Father,Child), \
portray_clause(father_child(Father, Child)), \
false"
这会产生:
father_child(tom, sally).
father_child(tom, erica).
father_child(mike, tom).
?-
使用-t halt
暂停程序而不是返回顶层:
swipl -f family.pl -g "father_child(Father,Child), \
portray_clause(father_child(Father, Child)), \
false" -t halt
现在我们终于有了:
father_child(tom, sally).
father_child(tom, erica).
father_child(mike, tom).
P.S.: 这是一个非常好的谓词命名约定!
有没有动态查询序言知识库的方法?
我在文件 family.pl 中有序言逻辑(我在此处找到的示例 http://www.techytalk.info/prolog-programming-gprolog-linux/)。这是它的内容:
mother_child(trude, sally).
father_child(tom, sally).
father_child(tom, erica).
father_child(mike, tom).
sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).
parent_child(X, Y) :- father_child(X, Y).
parent_child(X, Y) :- mother_child(X, Y).
我希望能够在不进入 prolog 解释器的情况下进行查询。
这个命令对我不起作用:
swipl -f family.pl -g "father_child(Father, Child)"
谢谢
查询确实有效:只是如果您以这种方式调用程序,您看不到结果。
因此,您可以自己打印结果,例如:
swipl -f family.pl -g "father_child(Father,Child), \ portray_clause(father_child(Father, Child))"
这产生:
father_child(tom, sally). ?-
这当然不是全部,所以你可以使用false/0
强制回溯:
swipl -f family.pl -g "father_child(Father,Child), \ portray_clause(father_child(Father, Child)), \ false"
这会产生:
father_child(tom, sally). father_child(tom, erica). father_child(mike, tom). ?-
使用-t halt
暂停程序而不是返回顶层:
swipl -f family.pl -g "father_child(Father,Child), \ portray_clause(father_child(Father, Child)), \ false" -t halt
现在我们终于有了:
father_child(tom, sally). father_child(tom, erica). father_child(mike, tom).
P.S.: 这是一个非常好的谓词命名约定!