title: Prolog addition excercise
body: <p>I have this very simple code as a representation of numerals. The problem is when I use the add2 function.</p>
  
  <p>Example: <code>add2(s(0)+s(s(0)), s(s(0)), Z).</code> returns <code>s(s(s(s(s(0)))))</code> correctly. However <code>add2(0, s(0)+s(s(0)), Z).</code> always <code>returns s(0)+s(s(0))</code>. Can anyone see why this is happening?</p>
  
  <pre><code>numeral(0).
  numeral(s(X)) :- numeral(X).
  numeral(X+Y) :- numeral(X), numeral(Y).
  
  add(0,X,X).
  add(s(X),Y,s(Z)) :- add(X,Y,Z).
  
  %%  exercise 1
  add2(X,Y,R) :- add(X,Y,R).
  add2(X+Y,Z,R) :- add(X,Y,A),add2(A,Z,R).
  add2(X,Y+Z,R) :- add(Y,Z,A),add2(X,A,R).
  </code></pre>