Question

I'm new to Prolog and I wanted to write a program that can do some computations on a cellular space. First of all, I've defined the cellular space by some facts:

board_size(3).

cell(0,0,0).
cell(0,1,0).
cell(0,2,0).

cell(1,0,0).
cell(1,1,0).
cell(1,2,0).

cell(2,0,0).
cell(2,1,0).
cell(2,2,0).

cell(X,Y,Z) means a cell in position (X,Y) and the value Z. And for finding the top cell of another cell, I've wrote this rule:

top(cell(X1,Y1,_),cell(X2,Y1,_)) :- board_size(Size), X1 is (X2-1) mod Size.

Finally I've tested my code by some queries:

1 ?- top(cell(0,0,0),cell(1,0,0)).
true.

2 ?- top(cell(0,0,0),cell(X,0,0)).
ERROR: is/2: Arguments are not sufficiently instantiated

What is the cause of this error?

Was it helpful?

Solution 2

X2 isn't instantiated in second query.

faisal answer seems appropriate to me (+1), because it's not apparent from your answer what's the purpose of top/2. You should refer to your database of actual cells, as he suggested.

Then you could try

top(cell(X1,Y1,_),cell(X2,Y1,_)) :-
   cell(X1,Y1,_),cell(X2,Y1,_), board_size(Size), X1 is (X2-1) mod Size.

OTHER TIPS

Try this:

top(X1,Y1,X2,Y1) :- cell(X1,Y1,_), cell(X2,Y1,_), board_size(Size), X1 is (X2-1) mod Size.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top