Prolog - Help fixing a rule <p>I have a database full of facts such as:</p> <pre><code>overground(newcrossgate,brockley,2). overground(brockley,honoroakpark,3). overground(honoroakpark,foresthill,3). overground(foresthill,sydenham,2). overground(sydenham,pengewest,3). overground(pengewest,anerley,2). overground(anerley,norwoodjunction,3). overground(norwoodjunction,westcroydon,8). overground(sydenham,crystalpalace,5). overground(highburyandislington,canonbury,2). overground(canonbury,dalstonjunction,3). overground(dalstonjunction,haggerston,1). overground(haggerston,hoxton,2). overground(hoxton,shoreditchhighstreet,3). </code></pre> <p>example: newcrossgate to brockley takes 2 minutes.</p> <p>I then created a rule so that if I enter the query istime(newcrossgate,honoroakpark,Z). then prolog should give me the time it takes to travel between those two stations. (The rule I made is designed to calculate the distance between any two stations at all, not just adjacent ones). </p> <pre><code>istime(X,Y,Z):- istime(X,Y,0,Z); istime(Y,X,0,Z). istime(X,Y,T,Z):- overground(X,Y,Z), T1 is T + Z. istime(X,Y,Z):- overground(X,A,T), istime(A,Y,T1), Z is T + T1. istime(X,Y,Z):- overground(X,B,T), istime(B,X,T1), Z is T + T1. </code></pre> <p>it seems to work perfectly for newcrossgate to the first couple stations, e.g newcrossgate to foresthill or sydenham. However, after testing newcrossgate to westcroydon which takes 26mins, I tried newcrossgate to crystalpalace and prolog said it should take 15 mins...despite the fact its the next station after westcroydon. Clearly somethings wrong here, however it works for most of the stations while coming up with a occasional error in time every now and again, can anyone tell me whats wrong? :S</p>