Re: Trying to setup a hierarchy of classes of things (SWI prolog)



On Oct 12, 11:29 pm, mearvk <mearvk@xxxxxxxxx> wrote:
The following file generates the error below:

[START FILE]

instanceOf(X,X).

instanceOf(produce,tomatoe).
instanceOf(produce,lettuce).
instanceOf(produce,cabbage).
instanceOf(produce,potatoe).
instanceOf(produce,apple).
instanceOf(produce,cucumber).

instanceOf(meats,steaks).
instanceOf(meats,sausage).
instanceOf(meats,ham).
instanceOf(meats,salami).
instanceOf(meats,beef).

instanceOf(beef,hamburger).

instanceOf(A,B):- instanceOf(C,B), instanceOf(A,C), A\==B, B\==C, A
\==C.

[END FILE]

?- instanceOf(meats,hamburger).
ERROR: Out of local stack

Just trying to set up a kind of inheritance structure for types of
things. Beef is a type of meat and hamburger is a type of beef so
asking if hamburger is a type of meat should return yes.

Thanks!


This is what you're looking for:


isA(produce,tomatoe).
isA(produce,lettuce).
isA(produce,cabbage).
isA(produce,potatoe).
isA(produce,apple).
isA(produce,cucumber).

isA(meats,steaks).
isA(meats,sausage).
isA(meats,ham).
isA(meats,salami).
isA(meats,beef).

isA(beef,hamburger).

instanceOf(A,B):- isA(A,B).

instanceOf(X,X).

instanceOf(A,B):- isA(C,B), instanceOf(A,C).


The problem is that your prolog interpreter, for some reason, is
trying to get all the possible results before printing any of them.
If you do a hand trace of what the prolog interpreter does, with the
rules you tried first, you find that the cause of the stack overflow
is the infinite recursion of your last definition of instanceOf:

instanceOf(meats,hamburger) -> instanceOf(C1,hamburger) ->
instanceOf(C2,hamburger) [by the same rule] ->
instanceOf(C3,hamburger) [idem] -> ...

.