Suppose I have the below code in my familyTree.pl file:
male(tom).
male(bob).
female(lisa).
female(emily).
parent(tom, bob).
parent(lisa, bob).
morethanfriends(emily, bob).
father(X,Y) :- male(X), parent(X,Y).
mother(X,Y) :- female(X), parent(X,Y).
girlfriend(X,Y) :- female(X), (morethanfriends(X,Y); morethanfriends(Y,X)).
boyfriend(X,Y) :- male(X), (morethanfriends(X,Y); morethanfriends(Y,X)).
Now, I want to get the answer to the questions like:
What is the relationship between Tom and Bob ?
What is the relationship between Lisa and Emily ?
How can I ask the above questions to prolog?
The only solution I could come up with was to iterate over the known relation types giving (Tom, Bob) or (Lisa, Emily) as paremeter and check which one returns true. But; this solution seems to be a waste of time, when the number of known relation types are more than a few and/or there is a chain relation between the given two people (i.e.: Lisa and Emily: Lisa is Emily's boyfriend's mother).
This program, a simplified version of above program, will work for direct relations present in the database.
Query: qus(Y,how,is,a1,related,to,b3).
Facts
Rules
Program
I have come up with this solution (not checked thoroughly but it seems to be ok):
Test cases: