Prolog style loop does not overflow? <p>A typical loop code in Prolog is something like:</p> <pre><code>loop_test :- format('loop...~n'), loop_test. </code></pre> <p>as discussed in another question: <a href="http://stackoverflow.com/questions/22885622/how-can-i-simulate-a-while-loop-in-prolog-with-unchangeable-conditions">How Can I simulate a while loop in Prolog with unchangeable conditions?</a></p> <p>Here's my question: Is this recursive form safe against overflow? Typically such a recursive code of C,C++,Python would overflow as in each function call a new stack is added. I executed above <code>loop_test</code> under <code>trace</code>. It was something like:</p> <pre><code>?- trace. ?- loop_test. Call: (6) loop_test2 ? creep Call: (7) format('loop...~n') ? creep loop... Exit: (7) format('loop...~n') ? creep Call: (7) loop_test2 ? creep Call: (8) format('loop...~n') ? creep ... loop... Exit: (158) format('loop...~n') ? creep Call: (158) loop_test2 ? creep Call: (159) format('loop...~n') ? creep ... </code></pre> <p>Clearly the <strong>number</strong> is increasing. With this simple code, I couldn't find the memory increase; I executed <code>loop_test</code> for more than 5 min and tracked VmPeak (peak size of virtual memory) written in /proc/xxx/status, but no change was observed.</p> <p>I want to understand if the above code (<code>loop_test</code>) is really safe? and Why is it safe? (or safer way to implement loops in Prolog).</p> <p>Many thanks.</p>