im trying to write a program in prolog that determine if there is a way from place to place. these are the relations:
road(ny,florida).
road(washington,flori
If you are treating the road/2 as a directed edge, then you can simply have:
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).
If however you have a loop in your graph, either because of the directions or you assume that road/2 is an undirected edge (and correspondingly implement this) then you need to keep track of where you have been while route finding. Otherwise you will get into an infinite loop. Remember prolog searches depth first by default so you need to make a check that when adding an item to a route it is not already there, otherwise prolog will find the same route over and over..
See chapter 5 of this book: https://www.cs.bris.ac.uk/~flach/SL/SL.pdf for a complete discussion.
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).