0
votes

I have a recursive predicate:

something(A, Object, Value, Complete) :-
     member(Value, Complete), Object = Value.

something(A, Object, _, Complete) :-
    objects(Object),
    \+ member(Object, Complete),
    !,
    <Predicates to get return values>,
    something(A, NewObject, Object, [Object|Complete]).

The function of this predicate is to iterate through all facts of Object, run some further logic processing on it and then output the results for each Object fact that is apart of objects.

The issue I'm having is that all the return values I get (upon pressing ;) are all of the first iteration, so the first value that this recursive function returned for Object.

EG: The output I see:

Object: obj1
Object: obj1

Where it should really be

Object: obj1
Object: obj2

(Along with the other variables, however I've left them out to keep the post cleaner).

Upon using the visual debugger in SWI Prolog implementation, I can see that once the inner recursive calls return, the values of the inner recursions are lost and that's why I'm seeing obj1 again for the second return value.

I'm not too sure how I can save these inner values, I've looked at lots of examples of recursive functions but I can't seem to apply the concepts to this specific instance.

1
seems a job for findall(Object, <Predicates to get return values>, Complete)CapelliC

1 Answers

1
votes

I think there's a simple misunderstanding of variables going on here. Prolog has variables, not "assignables." There's no difference between

something(A, Object, Value, Complete) :-
    member(Value, Complete), Object = Value.

And this:

something(A, Value, Value, Complete) :-
    member(Value, Complete).

A natural ramification of this is that this line:

something(A, NewObject, Object, [Object|Complete]).

is the same as this:

something(A, Object, Object, [Object|Complete]).

which is almost certainly where you're seeing unexpected behavior. If your mind rejects this and you feel there should be a difference between my simplified versions and your code, you have misunderstood the way variables work in Prolog. They cannot under any circumstances be "overwritten." In a recursive call, they can be instantiated differently, but this is really no different from formal parameters in any other language.