title: Prolog: Out of local stack error
body: <p>Consider the following code (taken from <em>Introduction to Prolog</em> by RP Suri):</p>
  
  <pre><code>/* A set of father-child pairs declared */
  father("Motilal", "Jawaharlal").
  father("Motilal", "Vijayalakshmi").
  father("Motilal", "Krishna").
  father("Jawaharlal", "Indira").
  father("Ranjit", "Tara").
  father("Ranjit", "Lekha").
  father("Ranjit", "Rita").
  father("Feroz", "Sanjay").
  father("Feroz", "Rajiv").
  father("Sanjay", "Varun").
  father("Rajiv", "Rahul").
  father("Rajiv", "Priyanka").
  
  /* Mothers declared */
  wife_of("Swaruprani", "Motilal").
  wife_of("Kamla", "Jawaharlal").
  wife_of("Vijayalakshmi", "Ranjit").
  wife_of("Indira", "Feroz").
  wife_of("Maneka", "Sanjay").
  wife_of("Sonia", "Rajiv").
  wife_of("Priyanka", "Robert").
  
  /* Predicates declared */
  mother(M, C) :- wife_of(M, Hus) , father(Hus, C).
  
  parent(P, C) :- mother(P, C) ; father(P, C).
  
  ancestor(X, Y) :- parent(X, Y).
  ancestor(X, Y) :- ancestor(X, Z) , parent(Z, Y).
  </code></pre>
  
  <p>Now if I query</p>
  
  <pre><code>ancestor("Motilal", X)
  </code></pre>
  
  <p>I get output as:</p>
  
  <pre><code>?- ancestor("Motilal", X).
  X = "Jawaharlal" ;
  X = "Vijayalakshmi" ;
  .
  .
  .
  X = "Priyanka" ;
  </code></pre>
  
  <p>(which is the correct output), after which the program stops and a few seconds later I get the message:</p>
  
  <pre><code>ERROR: Out of local stack
  </code></pre>
  
  <p>A similar thing happens when I query</p>
  
  <pre><code>ancestor(X, "Motilal").
  </code></pre>
  
  <p>This should return nothing, but the prompt again disappears for a few seconds followed by the same error message. What is the problem in the code?</p>