title: Meta-interpreter for calculating max recursion depth
body: <p>I'm trying to write a meta-interpreter in prolog for prolog, which would return maximal reached recursion depth in a given prolog program.</p>
  
  <p>This code actually counts number of all recursive calls in a program:</p>
  
  <pre><code>rc( true, 0) :- !.
  rc( ( Goal1, Goal2), N) :- !, %we have multiple goals
    rc( Goal1, N1), %count recursive calls in Goal1
    rc( Goal2, N2), %count recursive calls in goals Goal2
    N is N1 + N2. %add both counters
  
  rc( Goal, N) :-
    clause( Goal, Body),
    functor( Goal, F, A), %get functor and airity
    rcount( F/A, Body, NF), %count calls of that functor/airity in the body
    rc( Body, NB), %recursively process the body
    N is NF + NB. %add counters
  </code></pre>
  
  <p>I must somehow keep track of each individual recursion path and compare their depths, but have problems defining this in prolog. Can somebody point me into the right direction?</p>
  
  <p>Thanks.</p>