title: Prolog recursion in sum of n natural numbers
body: <p>I am having trouble to understand how value of Z is constantly changing. The specific step has been indicated in the stack trace output.</p>
  
  <p>Here is the code I am using for finding sum of N natural numbers - </p>
  
  <pre><code>sum1(1,1).
  sum1(N, Sum) :-
     Next is N-1,
     sum1(Next, Z),
     Sum is Z + N.
  </code></pre>
  
  <p>Here is the stack trace -</p>
  
  <pre><code>?- sum1(3,_).
     Call: (8) sum1(3, _2668) ? creep
     Call: (9) _2860 is 3+ -1 ? creep
     Exit: (9) 2 is 3+ -1 ? creep
     Call: (9) sum1(2, _2862) ? creep
     Call: (10) _2866 is 2+ -1 ? creep
     Exit: (10) 1 is 2+ -1 ? creep
     Call: (10) sum1(1, _2868) ? creep
     Exit: (10) sum1(1, 1) ? creep
     Call: (10) _2872 is 1+2 ? creep
     Exit: (10) 3 is 1+2 ? creep
     Exit: (9) sum1(2, 3) ? creep **%How is Z assigned value 3 ?**
     Call: (9) _2668 is 3+3 ? creep
      Exit: (9) 6 is 3+3 ? EOF: exit
  </code></pre>
  
  <p>Thanks in advance!</p>