Search code examples
prologinstantiation-error

Prolog -- Arguments are not sufficiently instantiated


I am trying to check for even/odd players and even/odd columns in a game by counting the players and the columns. At some point, it seems I am not instantiating my variables correctly. Here is how I call the game:

playGame(Game):- 
    countPlayers(Game,TotalPlayers),
    colSize(Game,TotalCols),
    checkEvens(TotalPlayers,TotalCols);
    checkOdds(TotalPlayers,TotalCols).

I assume the issues lies with TotalPlayers and TotalCols not being assigned correctly.

I tried looking at other questions, however the problems seem to be different. I am new to prolog and likely making a trivial mistake somewhere.

After some tracing, it seems the error is being caused when a combination of odd/even is the input. Given an input of two even or two odd numbers, the program behaves as expected. Given an input of one odd and one even number, and it breaks.

Full error:

ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR:   [11] 1 is _34862 mod 2
ERROR:   [10] checkOdds(_34894,_34896)

Solution

  • ?- X = 3, between(1, X, 2), between(1, X, 3).
    X = 3.
    
    ?- X = 3, between(1, X, 2); between(1, X, 3).
    X = 3 ;
    ERROR: Arguments are not sufficiently instantiated
    ERROR: In:
    ERROR:    [9] between(1,_7656,3)
    ERROR:    [7] <user>
    ERROR: 
    ERROR: Note: some frames are missing due to last-call optimization.
    ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
    

    Can you spot what is going on?


    A predicate like this:

    foo :- a, b; c.
    

    is the same thing as:

    foo :- a, b.
    foo :- c.
    

    In other words, a, b is indeed in a different "context" than c.

    Often, you mean to say a, (b ; c) instead. This is why the ; is usually put at the beginning of the line, and you do use parens around it. So it would be either

    (   a,
        b
    ;   c
    )
    

    or

    a,
    (   b
    ;   c
    )
    

    (which is not the same thing!)

    This is definitely confusing:

    a,
    b;
    c
    

    You don't see "good" Prolog code written like this (say library code).

    But really, try to read a textbook or something, Stackoverflow is good for figuring out where your error is but not for really learning.