0

I don't understand how to remove the infinite loop.When trying to find ancestors.

spouse(george,mel).
spouse(mel,george).
parent(george,elizabeth).
parent(george,margaret).
spouse(elizabeth,philip).
spouse(philip,elizabeth).
parent(elizabeth,charles).
parent(spencer,diana).
spouse(diana,charles).
spouse(charles,diana).

parent(X,Y):-spouse(X,Z),parent(Z,Y).
ancestor(X,Y):-parent(X,Y).
ancestor(X,Y):-parent(X,Z),ancestor(Z,Y).

Prolog seems to go in an infinite loop due to the first rule. I would really like to avoid hardcoding the facts like I had to for spouse(reflexive relation). But I can't seem to figure out how this problem is similar to other problems regarding transitive relations.

false
  • 10,264
  • 13
  • 101
  • 209
Sidwa
  • 41
  • 1
  • 10
  • In Prolog, you don't want to have same facts / predicates separated by other named facts / predicates. I assume you saw the warning about discontiguous predicates or facts? Secondly, use a different name for your rule for `parents` versus your facts for `parents`. You definitely want to distinguish when making facts and rules like these. – lurker Mar 19 '18 at 10:48
  • Thank you for your second suggestion. It fixed my problem. If only this was mentioned explicitly in the online prolog [tutorial](http://lpn.swi-prolog.org/lpnpage.php?pagetype=html&pageid=lpn-htmlse9). – Sidwa Mar 19 '18 at 19:59
  • The discontiguous predicates can cause a problem, too. Many Prologs will ignore later defined predicates if there are different ones defined in between. – lurker Mar 20 '18 at 00:18
  • Noted, I fixed the issue in my code. – Sidwa Mar 20 '18 at 15:27
  • Having the same functor for facts and rules is **required** for some situation. You should try to understand this aspect of **logic** to progress with your study. – CapelliC Mar 21 '18 at 07:54

0 Answers0