Search code examples
prologinstantiation-error

Prolog instantiation error when using two "is/2" predicates


I'm new to Prolog and wanted to try some basic examples. I saw multiple questions about the instantiation error that Prolog throws, but I couldn't find a solution for my example.

My rule is for updating a location after a forward action is executed in a certain direction. I just removed the heading for simplification, it keeps failing:

% loc(x, y, t)
loc(1, 1, 1) :- !.  % location at time t = 1
loc(X2, Y, T2) :-  % X1 incremented by 1
    T2 > 1,
    T1 is T2 - 1,
    forward(T1),  % a forward action was executed at the previous time step
    X1 is X2 - 1,
    loc(X1, Y, T1).

As I said, this throws the dreaded error:

ERROR: Arguments are not sufficiently instantiated

Can anyone point me in the right direction?


Solution

  • It looks like X2 is an output argument. If this is indeed the case, the code should be modified as follows:

    loc(1, 1, 1) :- !. 
    loc(X2, Y, T2) :- 
        T2 > 1,
        T1 is T2 - 1,
        loc(X1, Y, T1),
        X2 is X1 + 1.
    

    Example:

    ?- loc(X,Y,3).
    X = 3,
    Y = 1.