0
votes

I am very new to Prolog programming. 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).

Thank you.

1

1 Answers

0
votes
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


Your input: 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.

Z bind to horse.

As a last step, you're looking for bigger(X, horse)

Does that make sense?