I aa new to prolog. I tried this simple program.
man(rahul).
person(X) :- man(X).
male(X) :- man(X).
female(X) :- \+ man(x).
female(samita).
married(samita, rahul).
loves(X, Y) :- married(X, Y).
married(X, Y) :- married(Y, X).
child(sita, samita, rahul).
loves(X, Y):- child(X, Y , Z).
loves(X, Z):- child(X, Y , Z).
my query is.
loves(sita, X).
According to me. the answer must be.
X = rahul.
X = samita.
but in swi prolog the program is not terminating. can someone please help me with understanding where I did wrong.
code
man(rahul).
person(X) :- man(X).
male(X) :- man(X).
female(X) :- \+ man(x).
female(samita).
married(samita, rahul).
loves(X, Y) :- married(X, Y).
married(X, Y) :- married(Y, X).
child(sita, samita, rahul).
loves(X, Y):- child(X, Y , Z).
loves(X, Z):- child(X, Y , Z).
query:
loves(sita, X).
To understand why your program loops, it is sufficient to look at the following failure-slice. Because this fragment does not terminate, also your full program does not terminate.
To fix this problem replace the clauses for
married/2
by