Prolog - error 1, full stack stack

im trying to write a program in the prolog that determines if there is a way from place to place. these are the ratios:

road(ny,florida).
road(washington,florida).
road(washington,texas).
road(vegas,california).

I want to write: there_is_way(X,Y)that determine if there is a way or not. eg:

?- road(florida,ny).
no
?-there_is_way(florida,ny).
yes

This is my code:

there_is_way(X,Y):- road(X,Y);
                        road(Y,X);
                        road(X,Z),road(Z,Y),X\=Y;
                        road(X,Z),road(Y,Z),X\=Y;
                        road(Z,X),road(Z,Y),X\=Y;
                        road(Z,X),road(Y,Z),X\=Y.
there_is_way(X,Y):-
                        road(Z,Y),there_is_way(X,Z).
there_is_way(X,Y):-
                        road(Y,Z),there_is_way(X,Z).

but unfortunately I get the message "Error 1, Backtrack Stack Full".

somebody?

Thank you

+2
source share
2 answers

First, we need a symmetric definition:

:- meta_predicate symm(2, ?,  ?).

symm(P_2, A, B) :-
   call(P_2, A, B).
symm(P_2, A, B) :-
   call(P_2, B, A).

Now using closure0/3

there_is_way(A, B) :-
   closure0(symm(road), A, B).
+2
source

If you are considering the / 2 road as a directional edge, you can simply:

road(ny,florida).
road(washington,florida).
road(washington,texas).
road(vegas,california).

there_is_way(X,Y):- road(X,Y).
there_is_way(X,Y):- road(X,Z),there_is_way(Z,Y).

, , , - , , /2 ( ), , , . . , , , .

. 5 : https://www.cs.bris.ac.uk/~flach/SL/SL.pdf .

+1

Source: https://habr.com/ru/post/1624320/


All Articles