【问题标题】:Dealing with unknown procedure error in SWI-Prolog处理 SWI-Prolog 中的未知程序错误
【发布时间】:2021-02-02 08:47:57
【问题描述】:

我正在尝试使用 Swi-Prolog 创建一个简单的知识库,但在运行查询 animal(X). 时遇到“未知过程”错误。据我所知,它应该根据我在下面写的事实打印“鳄鱼”。我做错了什么?

species(reptile):-
    scales(present),
    body_temperature(cold_blooded),
    reproduction(lays_eggs).

animal(alligator):-
    species(reptile),
    color(dark_green),
    habitat(water).

species(reptile).
color(dark_green).
habitat(water).

【问题讨论】:

  • 在 Prolog 中,变量以大写字母或下划线开头。例如。 Reptile,不是reptile
  • 以下 Logtalk 示例(可以在大多数 Prolog 系统上运行)展示了如何使用知识库并对其进行查询,类似于您要完成的工作:github.com/LogtalkDotOrg/logtalk3/tree/master/examples/birds

标签: prolog


【解决方案1】:

你的程序不包含变量,只有常量,这里是原子的形式(全小写字母字符串)

错误:

ERROR: Unknown procedure: scales/1
ERROR: In:
ERROR:   [12] scales(present)
ERROR:   [11] species(reptile) at user://1:8
ERROR:   [10] animal(alligator) at user://1:13
ERROR:    [9] toplevel_call(user:user: ...) 

解释器正在寻找谓词scales/1 以使用参数present 调用。没有这样的谓词(找不到以这种方式命名的事实或规则)。

也不清楚你的程序应该做什么。

你可能想表达这个:

“键入”关于某些原子的事实(这些通常没有人们想象的那么有用。

animal(alligator).     % alligator is an animal
species(reptile).      % reptile is a species    
color(dark_green).     % dark_green is a color
habitat(water).        % water is a habitat

alligator指定的事物的属性:

species(alligator,reptile).   % species of alligator is reptile
color(alligator,dark_green)   % color of alligator is dark_green
habitat(alligator,water).     % habitat of alligator is water

species指定的事物的属性:

surface(reptile,scales).
body_temperature(reptile,cold_blooded),
reproduction(reptile,lays_eggs).

Prolog 正确建模对象的能力有点弱。缺乏基本有用的数据结构“地图”也无济于事。 (Prolog 是“本体的组装器”吗?是的!)

【讨论】:

  • 我觉得它更像...... Prolog 是本体应该如何从一开始就完成......
  • @CapelliC 与 F-Logic 相比,例如 Flora-2(我在哪里可以争取时间来精通所有这些事情?)
  • 这么一团糟...我认为您实际上精通大多数构建块...对于我自己来说,我发现自己走上了一条奇怪的道路,艰难地学习了被视为语言的价值作为“便宜”或无趣,如 PHP 或 JS(现代的)......所以,请接受我的建议 - 很少......无论如何,看看 APE,并继续探索 SWI-Prolog 中的表格.不知道其他 Prologs...
  • @CapelliC 我会我会的。战斗还在继续。这些仍然是 IT 的黑暗时代!
【解决方案2】:

鉴于您的问题需要表示分类学知识,让我们使用可以自然表达它的 Logtalk 解决方案,提供知识与其代码表示之间的一对一关系。该解决方案是可移植的,因此可以在大多数 Prolog 系统中运行:

:- object(species).

    :- public([
        scales/1, body_temperature/1, reproduction/1,
        color/1, habitat/1
    ]).

:- end_object.


:- object(reptile, extends(species)).

    scales(present).
    body_temperature(cold_blooded).
    reproduction(lays_eggs).

:- end_object.


:- object(alligator, extends(reptile)).

    species(reptile).
    color(dark_green).
    habitat(water).

:- end_object.

将此代码保存在 kb.lgt 文件中,使用您喜欢的 Prolog 系统启动 Logtalk 并尝试一些查询。例如:

$ swilgt
...
{kb}.
...
?- alligator::habitat(Habitat).
Habitat = water.

?- alligator::reproduction(Reproduction).
Reproduction = lays_eggs.

但你也想列举例如动物。 IE。层次结构的推理。有一个图书馆。首先,我们需要将必要的谓词导入根对象:

:- object(species, imports(proto_hierarchy)).

然后我们可以试试:

?- {hierarchies(loader)}.
...
?- reptile::descendant(Reptile).
Reptile = alligator ;
false.

希望这个示例足够清晰,以便您可以将其扩展为更加逼真,从而避免您的老生物老师尖叫和拉扯头发。将此解决方案发展为专家系统也很容易,该系统可用于对给出的描述进行分类。示例见:

https://github.com/LogtalkDotOrg/logtalk3/tree/master/examples/birds

【讨论】:

  • 也不错。 Logtalk和Ergolite(即F-Logic子集)的应用范围有比较吗?
  • @DavidTonhofer 不熟悉 Ergolite。但是单句对比 Logtalk 和 Flora-2 就是 Logtalk 专注于软件工程,而 Flora-2 专注于知识表示。
猜你喜欢
  • 2015-01-13
  • 1970-01-01
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多