Prologue infinite loop

This is a program to find out who is compatible with John. I am new to Prolog. To inform Prolog for example. met (X, Y) = met (Y, X) a lot of code written. Now when I run the query

?- compatible(john, X)

      

it goes into an endless loop ...

Source:

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 understand so much, but I still don't understand what the problem is and how to fix it. It would be great to help me.

+3


source to share


3 answers


I'm not surprised you got an infinite loop. compatible

depends on friends

, but friends

depends on compatible

. Are you sure this is what you want?



Note that if you really want your rules to be recursive, you need a stop condition. But I don't understand why you need recursion for such a simple problem as affinity matching.

+2


source


So what's the problem with your program? Here's a way to localize the problems you have. By entering goals , we get a bounce fragment. This is a snippet that still has many of the same properties as the original program. Specifically: if the failure-cut loops as well as the original software loop. Thus, the failure fragment shows us the part of the program that needs to be changed to overcome the original problem. For your request, I am getting the following snippet, which still does not complete: false

? - compatible (john, X), false .

compatible (X, Y): - false , reading (X), reading (Y) .
compatible (X, Y): - false , football (X), football (Y) .
compatible (X, Y): - false , friends (X, Y) .
compatible (X, Y): - mutual (X, Y), false .

friends (X, Y): - havemet (X, Y), compatible (X, Y).
friends (john, carl): - false .
friends (carl, john).

havemet (X, Y): - false , met (X, Y) .
havemet (X, Y): - met (Y, X).

mutual (X, Y): false , friends (X, Temp), friends (Y, Temp) .
mutual (X, Y): - friends (Temp, X), friends (Y, Temp), false .
mutual (X, Y): - friends (X, Temp), false , friends (Temp, Y) .
mutual (X, Y): false , friends (Temp, X), friends (Temp, Y) .

met (carl, emily).
met (fred, james): - false .
met (fred, emily): - false .

But shouldn't just any request end for compatible/2

? For the most general query, the failure slice can be reduced even further:



? - compatible (X, Y), false .

compatible (X, Y): - false , reading (X), reading (Y) .
compatible (X, Y): - false , football (X), football (Y) .
compatible (X, Y): - false , friends (X, Y) .
compatible (X, Y): - mutual (X, Y), false .

friends (X, Y): - havemet (X, Y), compatible (X, Y).
friends (john, carl): - false .
friends (carl, john): - false .

havemet (X, Y): - false , met (X, Y) .
havemet (X, Y): - met (Y, X).

mutual (X, Y): false , friends (X, Temp), friends (Y, Temp) .
mutual (X, Y): false , friends (Temp, X), friends (Y, Temp) .
mutual (X, Y): - friends (X, Temp), false , friends (Temp, Y) .
mutual (X, Y): false , friends (Temp, X), friends (Temp, Y) .

met (carl, emily).
met (fred, james): - false .
met (fred, emily): - false .

For the rest of here, you need to fix the problem somehow. There may be additional reasons for refusal. But you have to fix it anyway.

And if that's not enough, you can add goals V = const

to the program. But I think that should be enough ...

+2


source


Which Prolog system are you using? Do you need to use a specific system?

Your program, as it is, will not terminate in standard Prolog, but will be in Tab-enabled Prolog, such as XSB-Prolog http://xsb.sourceforge.net/ or B-Prolog http://www.probp.com/ - just add :- auto_table.

as the first line of your program.

| ?- compatible(john, X).
compatible(john, X).
X = john ?;
X = james ?;
X = carl ?;
X = emily ?;
no

      

+1


source







All Articles