Reputation: 415
I would like to create a rule in Prolog that defines daughter_of and son_of in my database as follows. However I am getting a singleton error, with Y, M, and F in the new rules. I thought this only came up if the variables have not been used elsewhere? Also is this the right way to write the new rules?
Any guidance would be greatly appreciated.
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).
Upvotes: 0
Views: 260
Reputation: 7209
After you get mother and father with parents(X,M,F)
you forgot to check if mother or father = Y.
Here is corrected code:
daughter_of(X,Y):-
female(X),
parents(X,M,F),
(Y = M ; Y = F).
son_of(X,Y):-
male(X),
parents(X,M,F),
(Y = M ; Y = F).
Test run:
?- son_of(X, Y).
X = edward,
Y = victoria ;
X = edward,
Y = albert.
?- daughter_of(X, Y).
X = alice,
Y = victoria ;
X = alice,
Y = albert ;
false.
Upvotes: 1