SWI-Prolog 中的未定义过程不起作用
我刚刚开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如错误消息所示,您有一个未定义的过程
mountaindew/1
。要使代码返回 true,您的选择是:dynamic(mountaindew/1)
set_prolog_flag (未知,失败)
As the error message says, you have an undefined procedure
mountaindew/1
. To make your code return true, your options are:dynamic(mountaindew/1)
set_prolog_flag(unknown, fail)
您还可以更改谓词的顺序(不能总是这样做)
但主要是卡雷尔所说的。
最后,即使您仍在开发代码,编写总是会失败的东西也没有任何意义
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
这有效,但由于我是初学者,我无法说出原因。 “未实例化”一词可能适用。尽管不知道为什么它有效,但我认为展示一种有效的方法是有帮助的。
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.