title: Prolog infinite loop
body: <p>I'm fairly new to Prolog and I hope this question hasn't been asked and answered but if it has I apologize, I can't make sense of any of the other similar questions and answers.</p>
  
  <p>My problem is that I have 3 towns, connected by roads. Most are one way, but there are two towns connected by a two way street. i.e.  </p>
  
  <pre><code>facts:  
  road(a, b, 1).  
  road(b, a, 1).  
  road(b, c, 3). 
  </code></pre>
  
  <p>where a, b and c are towns, and the numbers are the distances  </p>
  
  <p>I need to be able to go from town a to c without getting stuck between a and b  </p>
  
  <p>Up to here I can solve with the predicates: (where r is a list of towns on the route)</p>
  
  <pre><code>route(A, B, R, N) :- 
    road(A, B, N), 
    R1 = [B],
    R = [A|R1],
    !.
  
  route(A, B, R, N) :- 
    road(A, C, N1), 
    route(C, B, R1, N2), 
    \+ member(A, R1), 
    R = [A | R1], 
    N is N1+N2.
  </code></pre>
  
  <p>however if I add a town d like so</p>
  
  <pre><code>facts:
  road(b, d, 10)
  </code></pre>
  
  <p>I can't get Prolog to recognize this is a second possible route. I know that this is because I have used a cut, but without the cut it doesn't stop and ends in stack overflow.</p>
  
  <p>Furthermore I will then need to be able to write a new predicate that returns true when R is given as the shortest route between a and c.</p>
  
  <p>Sorry for the long description. I hope someone can help me!</p>