Question

Hello I am a newbie to Prolog with basic question. I would like to know why these Prolog predicates daughter_of/2 and son_of/2 do not work as I want them to. For example, if I ask

16 ?- daughter_of(alice, X).
true.

(Why true? What predicate would I need to get Victoria and Albert?)

Thank you.

male(albert).
male(edward).

female(alice).
female(victoria).

parents(edward, victoria, albert).
parents(alice, victoria, albert).

sister_of(X,Y):-
    female(X),
    parents(X,M,F),
    parents(Y,M,F).

brother_of(X,Y):-
    male(X),
    parents(X,M,F),
    parents(Y,M,F).

daughter_of(X,Y):-
    female(X),
    parents(X,M,F).

son_of(X,_Y):-
    male(X),
    parents(X,M,F).
Was it helpful?

Solution

The problem is the definition of your doughter_of predicate.

daughter_of(X,Y):-
female(X),
parents(X,M,F).

It should be

daughter_of(X,Y):-
female(X),
parents(X,Y,_).

daughter_of(X,Y):-
female(X),
parents(X,_,Y).

So that the Y parameter is passed on to the parents predicate. Otherwise it would not be used (and even gives me a warning when loading the file). Then prolog could only tell you that X is female and X has parents, but the actual parents would be discarded because M and F are no output parameters of your predicate.

Notice that i have defined the daughter_of predicate twice to work with the father as well as with the mother.

The same applies to son_of.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top