【问题标题】:Querying prolog dynamically动态查询prolog
【发布时间】:2016-08-30 15:25:21
【问题描述】:

有没有办法动态查询 prolog 知识库?

我在文件 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)"

谢谢

【问题讨论】:

标签: prolog swi-prolog


【解决方案1】:

查询确实有效:只是如果您以这种方式调用程序,您看不到结果。

因此,您可以自己打印结果,例如:

swipl -f family.pl -g "father_child(父亲,孩子),\ portray_clause(father_child(父亲,孩子))"

这会产生:

父亲孩子(汤姆,莎莉)。 ?-

当然不是全部,所以你可以使用false/0强制回溯:

swipl -f family.pl -g "father_child(父亲,孩子),\ 描绘子句(父亲孩子(父亲,孩子)),\ "

这就产生了:

父亲孩子(汤姆,莎莉)。 父亲孩子(汤姆,艾丽卡)。 父亲孩子(迈克,汤姆)。 ?-

使用 -t halt停止程序而不是返回顶层:

swipl -f family.pl -g "father_child(父亲,孩子),\ 描绘子句(父亲孩子(父亲,孩子)),\ 假" -t 停止

现在我们终于有了:

父亲孩子(汤姆,莎莉)。 父亲孩子(汤姆,艾丽卡)。 父亲孩子(迈克,汤姆)。

P.S.:这是一个非常好的谓词命名约定!

【讨论】:

  • 这正是我想要的。不,我可以从任何其他编程语言调用 prolog 查询。非常感谢。
  • 太棒了!请注意,尽管这样做有点……过时的风格。现在,您可以例如在 Prolog 中实现 REST 服务或使用 Pengines(基于 HTTP 的通信),以便其他程序可以轻松查询 Prolog 引擎。
  • 太棒了。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多