title: Count the ups and downs
body: <p>In SWI-Prolog, I want to count how many times the user types <code>up</code> and <code>down</code>. Every time he types <code>up</code>, the up counter (xu) will be increased and the down counter (xd) will be decreased. The same will happen when the user types down.This what I have:</p>
  
  <pre><code>:- dynamic xd/1, xu/1.
  
  xd(0).
  xu(0).
  
  up :- 
     retract(xu(Xu)),
     succ(Xu, Xu1),
     asserta(xu(Xu1)), 
     retract(xd(Xd)),
     succ(Xd1,Xd),
     asserta(xd(Xd1)).
  
  down :- 
     retract(xd(Xd)),
     succ(Xd, Xd1),
     asserta(xd(Xd1)),
     retract(xu(Xu)),
     succ(Xu1,Xu),
     asserta(xu(Xu1)).
  </code></pre>
  
  <p>But I have 2 problems:</p>
  
  <p>Whenever the user types <code>up.</code> or <code>down.</code>, he gets <code>false.</code> but the xu or xd (respectively) <em>is</em> being increased. For instance</p>
  
  <pre><code>   1 ?- down.
     false.
  
     2 ?- down.
     false.
  
     3 ?- xd(X).
     X = 2.
  </code></pre>
  
  <p>The second problem I have is that if the user starts typing <code>up.</code> a couple of times, and then <code>down.</code>, the xd counter won't increase. For instance:</p>
  
  <pre><code>1 ?- down.
  false.
  
  2 ?- down.
  false.
  
  3 ?- xd(X).
  X = 2.
  
  4 ?- up.
  false.
  
  5 ?- xu(X).
  false.
  </code></pre>
  
  <p>What am I doing wrong?</p>