prologswi-prologfailure-sliceb-prologxsb

Prolog infinite loop


This is a program that should find out who is compatible with john. I am new to Prolog. In order to let Prolog know eg. met(X,Y) = met (Y,X) lots of code has been written. Now when I start the query

?- compatible(john, X)

it goes into infinite looping...

Source code:

compatible(X,Y) :- reading(X), reading(Y).
compatible(X,Y) :- football(X), football(Y).
compatible(X,Y) :- friends(X,Y).
compatible(X,Y) :- mutual(X,Y).
friends(X,Y) :- havemet(X,Y), compatible(X,Y).
havemet(X,Y) :- met(X,Y).
havemet(X,Y) :- met(Y,X).
mutual(X,Y) :- friends(X,Temp), friends(Y,Temp).
mutual(X,Y) :- friends(Temp,X), friends(Y,Temp).
mutual(X,Y) :- friends(X,Temp), friends(Temp,Y).
mutual(X,Y) :- friends(Temp,X), friends(Temp,Y).

football(john).
football(james).
friends(john, carl).
friends(carl, john).
reading(carl).
reading(fred).
reading(emily).
met(carl, emily).
met(fred, james).
met(fred, emily).

I have been researching so much but I still do not understand what is the problem and how to fix that. It would be great for me to help me.


Solution

  • I am not surprised you got an infinite loop. compatible depends on friends while friends depends on compatible. Are you sure this is what you want?

    Please note that if you really wanted your rules to be recursive, you would need a stopping condition. But I don't see why you would need recursion for such a simple problem as affinity matching.