title: Stackoverflow in Prolog Peano Arithmetic
body: <p>I'm writing some Peano arithmetic to better learn Prolog. The following is the code I've come up with, and it seems equivalent of what I've seen elsewhere online:</p>
  
  <pre><code>add(X,z,X).
  add(X,s(Y),s(Z)) :- add(X,Y,Z).
  mult(_,z,z).
  mult(X,s(Y),W) :- mult(X,Y,Z), add(X,Z,W).
  </code></pre>
  
  <p>However if I try to make a simple query, like the divisor pairs of <code>0</code>, I run into problems:</p>
  
  <pre><code>| ?- mult(X,Y,z).
  
  Y = z ? ;
  
  X = z
  Y = s(z) ? ;
  
  X = z
  Y = s(s(z)) ? ;
  
  X = z
  Y = s(s(s(z))) ? ;
  
  Fatal Error: global stack overflow (size: 32768 Kb, reached: 32765 Kb, environment variable used: GLOBALSZ)
  </code></pre>
  
  <p>It really bugs me, as to how can it get all the way to <code>Y = 3</code>, but not to <code>Y = 4</code>?</p>