SWI-Prolog 中的未定义过程不起作用

发布于 2024-11-06 19:34:54 字数 778 浏览 0 评论 0原文

我刚刚开始使用 Prolog,并且已经在一个看似简单的示例中遇到了问题。这是我的 .pl 文件:

hacker(P) :- mountaindew(P), doesntsleep(P).
hacker(P) :- writesgoodcode(P).
writesgoodcode(jeff).

然后,在将程序加载到 swipl 中后,我在提示符下用这一行测试它

writesgoodcode(jeff).

,我认为它会显示 true,但我收到此错误:

?- hacker(jeff).
ERROR: hacker/1: Undefined procedure: mountaindew/1
   Exception: (7) hacker(jeff) ? 

This program Worksfine, 但这并不能解决我的问题:

hacker(P) :- writesgoodcode(P).
writesgoodcode(jeff).

$ swipl -s dumb.pl
% dumb.pl compiled 0.00 sec, 1,112 bytes

?- hacker(jeff).
true.

谁能解释为什么我原来的程序不起作用?根据我的理解,Prolog 应该“跳过”第一条语句,因为它没有足够的信息,并检查下一行。它确实有足够的信息用于第二行,因此它应该评估为 true。任何帮助或正确方向的观点都会很棒。谢谢。

I am just starting to use Prolog, and already I've run into problem with a seemingly simple example. Here is my .pl file:

hacker(P) :- mountaindew(P), doesntsleep(P).
hacker(P) :- writesgoodcode(P).
writesgoodcode(jeff).

Then, after I load the program into swipl, I test it with this line at the prompt

writesgoodcode(jeff).

I thought it would display true, but I get this error:

?- hacker(jeff).
ERROR: hacker/1: Undefined procedure: mountaindew/1
   Exception: (7) hacker(jeff) ? 

This program works fine, but this doesn't solve my problems:

hacker(P) :- writesgoodcode(P).
writesgoodcode(jeff).

$ swipl -s dumb.pl
% dumb.pl compiled 0.00 sec, 1,112 bytes

?- hacker(jeff).
true.

Can anyone explain why my original program doesn't work? From my understanding, Prolog should "skip" the first statement since it doesn't have enough information, and check the next line. It does have enough info for that second line, and thus it should evaluate true. Any help or a point in the right direction would be great. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女七分熟 2024-11-13 19:34:54

正如错误消息所示,您有一个未定义的过程mountaindew/1。要使代码返回 true,您的选择是:

  1. 定义此谓词
  2. 声明此谓词是动态的:dynamic(mountaindew/1)
  3. 声明所有未知谓词都应失败(不推荐):set_prolog_flag (未知,失败)

As the error message says, you have an undefined procedure mountaindew/1. To make your code return true, your options are:

  1. Define this predicate
  2. Declare that this predicate is dynamic: dynamic(mountaindew/1)
  3. Declare that all unknown predicates should fail (not recommended): set_prolog_flag(unknown, fail)
烟织青萝梦 2024-11-13 19:34:54

您还可以更改谓词的顺序(不能总是这样做)
但主要是卡雷尔所说的。

最后,即使您仍在开发代码,编写总是会失败的东西也没有任何意义

you could also change the order of the predicates (cannot be done always ofc)
but mostly what Kaarel said.

in the end there is not really a point in writing something that will always fail, even if you are still developing the code

不知所踪 2024-11-13 19:34:54

这有效,但由于我是初学者,我无法说出原因。 “未实例化”一词可能适用。尽管不知道为什么它有效,但我认为展示一种有效的方法是有帮助的。

hacker(P) :- mountaindew(P), doesntsleep(P).
hacker(P) :- writesgoodcode(P).
mountaindew(john).
doesntsleep(john).
writesgoodcode(jeff).

This works but as I am a beginner I can't say why. The word "uninstantiated" may apply. Despite not knowing why it works, I think it's helpful to show one way that works.

hacker(P) :- mountaindew(P), doesntsleep(P).
hacker(P) :- writesgoodcode(P).
mountaindew(john).
doesntsleep(john).
writesgoodcode(jeff).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文