简体   繁体   English

Prolog初学者错误:超出本地堆栈

[英]Prolog beginner ERROR: Out of local stack

I am very new to Prolog programming.我对 Prolog 编程很陌生。 Any help is greatly appreciated.任何帮助是极大的赞赏。 I have the following program,我有以下程序,

bigger(elephant, horse).
bigger(horse, donkey).
bigger(donkey, dog).
bigger(donkey, monkey).

is_bigger(X, Y) :- bigger(X, Y).
is_bigger(X, Y) :- is_bigger(Z, Y), bigger(X,Z).

On running the query,在运行查询时,

?- is_bigger(A, donkey)

I get the following output,我得到以下输出,

A = horse ;
A = elephant ;
ERROR: Out of local stack

While I do somewhat understand how A = horse and A = elephant, I am having a difficult time understanding why it's recursing infinitely (I used the builtin trace trace predicate but couldn't understand it after A = elephant ).虽然我确实有点理解 A = horse 和 A = pixel 的方式,但我很难理解它为什么无限递归(我使用了内置的跟踪跟踪谓词,但在A = pixel之后无法理解它)。

Thank you.谢谢你。

is_bigger(X, Y) :- is_bigger(Z, Y), bigger(X,Z).

The above line is the one causing the out of local stack message.上面一行是导致本地堆栈消息外的那一行。 You're calling "is_bigger" again which calls "is_bigger" again and so on, recursively您再次调用“is_bigger”,再次递归地调用“is_bigger”,依此类推


Your input: is_bigger(A, donkey)您的输入: is_bigger(A, donkey)

First, you want to find something that is bigger than the donkey or/and something that is bigger than the thing that's bigger than the donkey.首先,您要找到比驴大的东西或/和比驴大的东西大的东西。

Therefore:所以:

is_bigger(X, Y) :- bigger(Z, Y), bigger(X,Z).

Why?为什么?

Y binds to donkey. Y 绑定到驴。

Z bind to horse. Z 绑定到马。

As a last step, you're looking for bigger(X, horse)作为最后一步,您正在寻找bigger(X, horse)

Does that make sense?那有意义吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM