title: Prolog order of clauses causes pathfinding to fail
body: <p>I am developing a path finding algorithm in Prolog, giving all nodes accessible by a path from a starting node. To avoid duplicate paths, visited nodes are kept in a list.</p>
  
  <p>Nodes and neighbors are defined as below:</p>
  
  <pre><code>node(a).
  node(b).
  node(c).
  node(d).
  node(e).
  
  edge(a,b).
  edge(b,c).
  edge(c,d).
  edge(b,d).
  
  neighbor(X,Y) :- edge(X,Y).
  neighbor(X,Y) :- edge(Y,X).
  </code></pre>
  
  <p>The original algorithm below works fine:</p>
  
  <pre><code>path2(X,Y) :-
      pathHelper(X,Y,[X]).
  
  pathHelper(X,Y,L) :-
      neighbor(X,Y),
      \+ member(Y,L).
  pathHelper(X,Y,H) :-
      neighbor(X,Z),
      \+ member(Z,H),
      pathHelper(Z,Y,[Z|H]).
  </code></pre>
  
  <p>This works fine</p>
  
  <pre><code>[debug]  ?- path2(a,X).
  X = b ;
  X = c ;
  X = d ;
  X = d ;
  X = c ;
  false.
  </code></pre>
  
  <p>however, when changing the order of the two clauses in the second definition, such as below</p>
  
  <pre><code>pathHelper(X,Y,L) :-
      \+ member(Y,L),
      neighbor(X,Y).
  </code></pre>
  
  <p>When trying the same here, swipl returns the following:</p>
  
  <pre><code>[debug]  ?- path2(a,X).
  false.
  </code></pre>
  
  <p>The query doesn't work anymore, and only returns <code>false</code>. I have tried to understand this through the <code>trace</code>command, but still can't make sense of what exactly is wrong.</p>
  
  <p>In other words, I am failing to understand why the order of <code>neighbor(X,Y)</code>and <code>\+ member(Y,L)</code>is crucial here. It makes sense to have <code>neighbor(X,Y)</code> first in terms of efficiency, but not in terms of correctness to me.</p>