2

The contents of the pl file.

isabove(g,b1).
isabove(b1,w1).
isabove(w2,b2).
isabove(b2,b3).
isabove(X,Z):-isabove(X,Y),isabove(Y,Z).
color(g,gray).`enter code here`
color(b1,blue).
color(b2,blue).
color(b3,blue).
color(w1,white).
color(w2,white).

then When the following command is executed ?- isabove(X,Y).

results
X = g,
Y = b1 ;
X = b1,
Y = w1 ;
X = w2,
Y = b2 ;
X = b2,
Y = b3 ;
X = g,
Y = w1 ;

ERROR: Stack limit (2.0Gb) exceeded
ERROR:   Stack sizes: local: 2.0Gb, global: 2Kb, trail: 0Kb
ERROR:   Stack depth: 24,399,533, last-call: 0%, Choice points: 5
ERROR:   Probable infinite recursion (cycle):
ERROR:     [24,399,533] user:isabove(w1, _814)
ERROR:     [24,399,532] user:isabove(w1, _834)
?-  =

'isabove(X,Z):-isabove(X,Y),isabove(Y,Z). 

This part seems to cause the problem, but I am wondering why it is a problem and how to fix it to get the answer you want. Answers I'll wait. Thank you.

1 Answers1

2

Definining isabove as:

isabove(X,Z) :- isabove(X,Y), isabove(Y,Z).

Will result in an infinite loop once the cases are enumerated, since Prolog will keep making recursive calls.

What one usually does is making a predicate for knowledge, and one that determines the transitive closure. Indeed:

above(g,b1).
above(b1,w1).
above(w2,b2).
above(b2,b3).

isabove(X, Y) :-
    above(X, Y).
isabove(X,Z) :-
    above(X,Y),
    isabove(Y,Z).

Here we guarantee progress, since above/2 will unify X and Y, and eventually we have reached the top element, so it will fail to find an element more above.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555