Prolog program query not terminating

73 Views Asked by At

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).
1

There are 1 best solutions below

0
On

To understand why your program loops, it is sufficient to look at the following . Because this fragment does not terminate, also your full program does not terminate.

loves(X, Y) :- married(X, Y), false.
loves(X, Y):- false, child(X, Y , Z).
loves(X, Z):- false, child(X, Y , Z).

married(samita, rahul) :- false.
married(X, Y) :- married(Y, X), false.

?- loves(sita, any), false.
   loops.

To fix this problem replace the clauses for married/2 by

married(X, Y) :- husband_wife(X,Y).
married(X, Y) :- husband_wife(Y,X).

husband_wife(rahul,samita).

?- loves(sita, X).
   X = samita
;  X = rahul.