Definition of Grandparent in Prolog, would any prolog programmers care to help clear this up?

StackOverflow https://stackoverflow.com/questions/16045121

  •  04-04-2022
  •  | 
  •  

Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top