%%% The London Underground example %%%

connected0(bond_street,oxford_circus,central).
connected0(oxford_circus,tottenham_court_road,central).
connected0(bond_street,green_park,jubilee).
connected0(green_park,charing_cross,jubilee).
connected0(green_park,piccadilly_circus,piccadilly).
connected0(piccadilly_circus,leicester_square,piccadilly).
connected0(green_park,oxford_circus,victoria).
connected0(oxford_circus,piccadilly_circus,bakerloo).
connected0(piccadilly_circus,charing_cross,bakerloo).
connected0(tottenham_court_road,leicester_square,northern).
connected0(leicester_square,charing_cross,northern).

connected(X,Y,L) :- connected0(X,Y,L).
connected(X,Y,L) :- connected0(Y,X,L).

nearby(X,Y) :- connected(X,Y,_).
nearby(X,Y) :- connected(X,Z,L),connected(Z,Y,L),X\=Y.

line(central,[bond_street,oxford_circus,tottenham_court_road]).
line(jubilee,[bond_street,green_park,charing_cross]).
line(piccadilly,[green_park,piccadilly_circus,leicester_square]).
line(victoria,[green_park,oxford_circus]).
line(bakerloo,[oxford_circus,piccadilly_circus,charing_cross]).
line(northern,[tottenham_court_road,leicester_square,charing_cross]).

reachable(X,Y,[],[L]) :- connected(X,Y,L).
reachable(X,Y,[Z|R],[L|Ls]) :- connected(X,Z,L),reachable(Z,Y,R,Ls).

query1(Station) :-
	reachable(piccadilly_circus,S,_,[L1]),
	reachable(S,Station,_,[L2]),
	L1 \= L2.

query2(Station) :-
	reachable(bond_street,Station,[S,T|_],[central|_]),
	S \= T.

stations0(Line,Start,[Start|Stations]) :-
	connected0(Start,Start2,Line),
	stations0(Line,Start2,Stations).
stations0(Line,Station,[Station]) :-
	connected0(_,Station,Line),
	not(connected0(Station,_,Line)).
stations(Line,Start,Stations) :-
	stations0(Line,Start,Stations),
	not(connected0(_,Start,Line)).
stations(Line,Stations) :-
	stations(Line,_,Stations).

startswithpair(X,Y,[X,Y|_]).
startswithpair(X,Y,[Y,X|_]).
newconnected(X,Y,L) :- line(L,Stations),
	startswithpair(X,Y,Stations).
newconnected(X,Y,L) :- line(L,[_|Stations]),
	startswithpair(X,Y,Stations).

