0

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?

4

2 回答 2

1

尝试这个:

top(X1,Y1,X2,Y1) :- cell(X1,Y1,_), cell(X2,Y1,_), board_size(Size), X1 is (X2-1) mod Size.
于 2013-05-01T06:32:00.080 回答
0

X2 未在第二个查询中实例化。

faisal 的答案对我来说似乎很合适(+1),因为从您的答案中看不出 top/2 的目的是什么。正如他所建议的,您应该参考您的实际细胞数据库。

那你可以试试

top(cell(X1,Y1,_),cell(X2,Y1,_)) :-
   cell(X1,Y1,_),cell(X2,Y1,_), board_size(Size), X1 is (X2-1) mod Size.
于 2013-05-01T07:56:20.143 回答