title: Need Help Understanding Somethings In Prolog
body: <p>Am a beginner in Prolog and finding it hard to understand how backtracking works when using rules. I don't even know if backtracking works in rules(want to know it properly).</p>
  
  <p>I have the following program that sums all the even integers in a list. I wrote it myself but finding it hard to understand the steps it takes to find the solution.</p>
  
  <pre><code>evenN(X):- (X mod 2) =:= 0.
  
  sumEven([], 0).
  sumEven([H|T], X):- evenN(H), sumEven(T,Y), X is Y+H.
  sumEven([H|T], X):- \+evenN(H), sumEven(T,X).
  </code></pre>
  
  <p>output:::</p>
  
  <pre><code>?- sumEven([1,2,3,4,5,6],X).
  X = 12
  </code></pre>
  
  <p>Need help in understand it better. I try to use the trace utility to understand the output but i don't understand it, that is why am asking it here. </p>
  
  <p><strong>QUESTIONS:</strong></p>
  
  <p>1)</p>
  
  <p>When i comment out the second rule(last line), it gives me fail as an answer because 1 is not an even number and the whole sumEven() fail because evenN() fails which i understand. My question is: what happens afterwards? Does it go back to the top and try the sumEven([], 0) fact or ? I just want to know what happens.</p>
  
  <p>2)</p>
  
  <p>When the last line(second rule) is included, and the first rule fails, when it backtracks, does it look for another sumEven() that follows it(like the way the second rule follows the first rule which fails) or it goes back to the top and test sumEven([], 0) fact and start it from there?</p>
  
  <p>I need to understand how it backtracks when rules are being used in prolog specifically in recursive situations like this one.</p>
  
  <p>3)
  I found the following code(recursive) on the net. It divides a list into positive and negative lists. </p>
  
  <pre><code>% predicates
      split(list,list,list)
  
  % clauses
      split([],[],[]).
      split([X|L],[X|L1],L2):-
          X&gt;= 0,
          !,    
          split(L,L1,L2).
  
      split([X|L],L1,[X|L2]):-
          split(L,L1,L2).
  
  Output :        
  
  Goal: split([1,2,-3,4,-5,2],X,Y)
  
  X=[1,2,4,2], Y=[-3,-5]
  </code></pre>
  
  <p>Can someone help me to understand the way it works to find the solution? I mean i want to understand step by step how it executes to come up with the solution.</p>