Infinite loop inference Prolog

110 views Asked by At

An infinite loop is generated when the inferential motor is activated to make the necessary inferences.

The rules and facts have been defined according to a specific syntax for the meta-interpreter I am using.

The rule is a quintuple in which the second field is the consequence of the rule while the third field are the conditions for activating a rule.

The cycle is caused by the updating of the id (I1) through the term nextID, which I used to make sure that each assert the id is incremented This is my knowledge base:

Rules:

rule(1,[gn(Name,Surname,I1),retract(nextID(I)),nextID(I1)],
and([person(Name,Surname),nextID(I),call_p(I1 is I+1),Name=='john']),1,1).

Facts:

fact(1,nextID(0),1).
fact(2,person(john,black),1).

How should I modify the rule in such a way that the infinite loop is not created?

1

There are 1 answers

4
Paulo Moura On

From your description of the rule fields, I would expect a different formulation for that rule:

rule(
    1,
    [retract(nextID(I)), call_p(I1 is I+1), assertz(nextID(I1), gn(Name,Surname,I1)], 
    and([person(Name,Surname), Name == 'john']),
    1,
    1
).

This would be equivalent to:

IF
    person(Name,Surname), Name == 'john'
THEN
    retract(nextID(I)), call_p(I1 is I+1),
    assertz(nextID(I1), gn(Name,Surname,I1)

But it's hard to tell exactly without knowing the details of your rule engine.

Update

Given the formulation of facts, notably fact(1,nextID(0),1), I wouldn't expect, however, that the embedded calls to retract/1 and assertz/1 would work. Isn't call_p/1 the escape mechanism to call Prolog goals from rules?