title: prolog royal family tree
body: <p>my question is similar to what has been asked here: <a href="https://stackoverflow.com/questions/16085748/determine-the-old-line-of-succession-prolog">determine the old line of succession prolog</a> and this <a href="https://stackoverflow.com/questions/1877952/prolog-recursing-down-family-tree">Prolog - recursing down family tree</a>
  As the solutions stated there are not really I wanted, can anyone give an idea on how to solve the following ?  Thanks!</p>
  
  <p>Facts:</p>
  
  <pre><code>male(charles).
  male(william).
  male(peter).
  male(henry).
  male(andrew).
  male(edward).
  
  
  female(elizabeth).
  female(anne).
  female(zara).
  female(eugenie).
  
  parent(elizabeth,charles).
  parent(elizabeth,anne).
  parent(elizabeth,andrew).
  parent(elizabeth,edward).
  parent(charles,william).
  parent(charles,henry).
  parent(anne,peter).
  parent(anne,zara).
  parent(andrew,eugenie).
  </code></pre>
  
  <p>Rules:</p>
  
  <pre><code>son(X,Y) :- parent(Y,X), male(X).
  daughter(X,Y) :- parent(Y,X), female(X).    
  successor(X,Y):- (son(X,Z);daughter(X,Z)) , (Y=Z;successor(Z,Y)). 
  </code></pre>
  
  <p>Results that i have gotten so far when querying successor(X,Y).</p>
  
  <pre><code> X = charles,
   Y = elizabeth ;
   X = andrew,
   Y = elizabeth ;
   X = edward,
   Y = elizabeth ;
   X = william,
   Y = charles ;
   X = william,
   Y = elizabeth ;
   X = henry,
   Y = charles ;
   X = henry,
   Y = elizabeth ;
   X = peter,
   Y = anne ;
   X = peter,
   Y = elizabeth ;
   X = anne,
   Y = elizabeth ;
   X = zara,
   Y = anne ;
   X = zara,
   Y = elizabeth ;
  </code></pre>
  
  <p>The actual result that I wanted to show is charles' family, andrew's family , edward's family and lastly anne's family but the son of anne appeared first. I am guessing that it is because the son(X,Y) rule executes first and finds all the matches and there is a problem with successor(X,Y). Is there a way that I can make it execute after anne is found as a result? Thanks!</p>
  
  <p>The actual answer would be something like </p>
  
  <pre><code> X = charles,
   Y = elizabeth;
   X = william,
   Y = charles;
   X = william,
   Y = elizabeth ;
   X = henry,
   Y = charles ;
   X = henry,
   Y = elizabeth ;
  
   X = andrew,
   Y = elizabeth ;
   X = eugenie,
   Y = andrew ;
   X = eugenie,
   Y = elizabeth ;
  
   X = edward,
   Y = elizabeth ;
  
  
   X = anne,
   Y = elizabeth ;
   X = peter,
   Y = anne ;
   X = peter,
   Y = elizabeth ;
   X = zara,
   Y = anne ;
   X = zara,
   Y = elizabeth ;
  </code></pre>