0

Grandparent(X,Y):- Parent(X,Mother(Y)).

Normally grandparent would be defined as Parent(X,Z) , Parent(Z,Y)....but it looks more natural to me as the Parent of Parent Y of X, what I defined above (can't explain myself any better than that, sorry) My question is: would that definition still be considered valid, if not, why not?

1 Answers1

2

The best I can recover of your intent from your syntactically incorrect Prolog is this:

grandparent(X, Y) :- parent(X, Y), mother(Y).

This is only half true though: X is a grandparent, but Y is not a grandchild, just a female child with her own child. In order for grandparent(X,Y) to have the expected definition, we must logically unify something with the middle generation. That's what Z is for in the traditional conception. If you defined mother/2 instead of mother/1, so that mother(Mother, Child) means Mother is the mother of Child, then you'd basically have a synonym for parent/2, and your solution would be no different from the normal definition, except that it would fail to generate the half of the solutions where the child is male. So really, it's only 1/4 correct.

Do note that Prolog does not have implicit return values in the sense of other programming languages, so a notation like parent(X, mother(Y)) is almost certainly not what you mean.

Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77
  • to be honest this was only part of the full code aside from this there would be paternalgrandparent which would be defined as parent(X,father(Y)) and parent is defined as father (X,Y) ; mother (X,Y) I just took a code sample as an example so that you wouldn't have to read through lines of code which you wouldn't need – Edward Bamber Apr 16 '13 at 22:06
  • Aside from that I had a definition for Male and Female too. I'm starting to think I should've pasted the whole code... – Edward Bamber Apr 16 '13 at 22:09
  • A minimum working example (or minimum non-working example, if the problem is a bug) is usually the best tack. Barring that, the whole code minus whatever isn't needed to run the example is good too. – Daniel Lyons Apr 16 '13 at 22:20