title: How Can I simulate a while loop in Prolog with unchangeable conditions?
body: <p>So basically I am trying to simulate some C code in Prolog.</p>
  
  <p>It is easy to simulate while loop in Prolog Here is the case:</p>
  
  <p>C code:</p>
  
  <pre><code>int a = 1;
  
  while(N)
  {
    N--;
    a++; 
  }
  </code></pre>
  
  <p>Prolog code:</p>
  
  <pre><code>prolog_while(0) : !
  prolog_while(N, A) :
     N1 is N -1,
     A1 is A + 1,
  prolog_while(N1, A1).
  </code></pre>
  
  <p>One problem is how to simulate a while loop in Prolog with unchangeable conditions?</p>
  
  <p>Here is the case:</p>
  
  <pre><code>int n = 1;
  int a = 1;
  
  while(1)
  {
     if (N==0)
       goto while_end;
     else
      {
          N--; A++;
      }       
  }
  </code></pre>
  
  <p>Or </p>
  
  <pre><code>while(1)
  {
     if (N==0)
       break;
     else
      {
          N--; A++;
      }       
  }
  </code></pre>
  
  <p>I know it is kinda of weird but basically these kind of C code is automatically generated by a source code analysis tool, so I have to deal with it...</p>
  
  <p>Then basically how can I simulate in Prolog? Is it doable? </p>
  
  <p>Could anyone give me some help?</p>
  
  <p>===================update============</p>
  
  <p>I tried to write some Prolog code in this way, but basically I still don't know how to handle test clause.</p>
  
  <pre><code>main :- loop_entry(2, 1), write(N), nl, write(A), nl.
  
  do(A, N, A1, N1) :- A1 is (A + 1), N1 is (N - 1).
  test(N) :- ...                 &lt;----- # How to write this part?
  loop_entry(N, A) :-
      test(N),
      do(A, N, A1, N1),
      loop_entry(N1,A1).
  </code></pre>