0

my question is similar to what has been asked here: determine the old line of succession prolog and this Prolog - recursing down family tree As the solutions stated there are not really I wanted, can anyone give an idea on how to solve the following ? Thanks!

Facts:

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).

Rules:

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)). 

Results that i have gotten so far when querying successor(X,Y).

 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 ;

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!

The actual answer would be something like

 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 ;
Community
  • 1
  • 1
james
  • 1
  • 2
  • 2
    What do you mean by `no solution to the questions` I see answers. Also what is your specific problem and where do yo need help, i.e. show some Prolog code where you attempted to solve your problem. – Guy Coder Apr 08 '17 at 12:28
  • Hi Guy Coder, I have updated my question. Thanks! – james Apr 08 '17 at 13:37
  • Can you list out the exact answer or answers you expect. I can take a guess but would prefer not to. – Guy Coder Apr 08 '17 at 14:40
  • Hi Guy Coder, I have updated my question with the expected result. – james Apr 08 '17 at 14:59
  • Thanks. I am looking at it in more detail now. – Guy Coder Apr 08 '17 at 15:18
  • I see that your desired answer is grouped by child of `Elizabeth`. I also read `Is there a way that I can make it execute after anne is found as a result?` but what does that question mean? – Guy Coder Apr 08 '17 at 15:28
  • Referring to the Anne part of the answer, I wanted the X=Peter, Y=Anne and X=Peter, Y =Elizabeth be after X=Anne, Y=Elizabeth , originally it was before – james Apr 08 '17 at 15:36
  • If you are using `SWI-Prolog` have you used `:- gtrace,successor(X,Y).`? This will start a graphical tracers so that you can better see what is happening. – Guy Coder Apr 08 '17 at 16:21
  • Yup I have tried using but still have no idea on how to get the final result. Thanks for the hint though! – james Apr 09 '17 at 01:00
  • I looked at this some more and without implanting the code my guess is that you want to solve this with a `breath-first search` for the first level, then a `depth-first search` for the next levels, but Prolog does depth-first search by default. Also you are mixing the order based on `male` and `female` for the different levels. Since BFS Prolog code does not roll off my finger tips I will leave it at that and maybe either you or someone else will be able to provide the answer. – Guy Coder Apr 09 '17 at 15:24

0 Answers0