prolog - 为什么我应该使用 'dif' 而不是 '=\='
prolog - why should I use 'dif' instead of '=\='
我正在研究 'Prolog by Example' 书中的一个例子(作者是 Helder Coelho 和 Jose C Cotta)。
Write a program for designing an architectural unit
obeying the following specifications:
+ Two rectangular rooms
+ Each room has a window and interior door
+ Rooms are connected by interior door
+ One room also has an exterior door
+ A wall can have only one door or window
+ No window can face north
+ Windows cannot be on opposite sides of the unit
有一个解决方案,但它在 SWI-Prolog 中不起作用,甚至无法编译。
我不得不更改书中的解决方案:使用 'dif(X,Y)' 而不是 'X =\= Y'。
谁能解释一下:为什么原来的解决方案不起作用?
这是一个代码:
plan(FD,D1,W1,D2,W2) :-
frontroom(FD,D1,W1),
opposite(D1,D2),
room(D2,W2),
notopposite(W1,W2).
frontroom(FD,D,W) :-
room(D,W),
direction(FD),
dif(FD,D), % FD =\= D, % <- original was commented
dif(FD,W). % FD =\= W. % <- original was commented
room(D,W) :-
direction(D),
direction(W),
dif(D,W), % D =\= W, % <- original was commented
dif(W,north). % W =\= north. % <- original was commented
direction(north).
direction(south).
direction(east).
direction(west).
opposite(north,south).
opposite(south,north).
opposite(east,west).
opposite(west,east).
notopposite(D1,D2) :-
opposite(D1,D3),
dif(D2,D3). % D2 =\= D3. % <- original was commented
我可以查询并得到答案:
?- plan(west,D1,W1,D2,W2).
D1 = east,
W1 = south,
D2 = west,
W2 = south
我试图追踪原始解决方案(使用 D2 =\= D3 而不是 dif(D2,D3))并得到这个:
[trace] ?- plan(west,D1,W1,D2,W2).
Call: (8) plan(west, _9632, _9634, _9636, _9638) ? creep
Call: (9) frontroom(west, _9632, _9634) ? creep
Call: (10) room(_9632, _9634) ? creep
Call: (11) direction(_9632) ? creep
Exit: (11) direction(north) ? creep
Call: (11) direction(_9634) ? creep
Exit: (11) direction(north) ? creep
Call: (11) north=\=north ? creep
ERROR: Arithmetic: `north/0' is not a function
为什么我不能使用'=\='?
应该是 \==
而不是不正确的 =\=
。我不知道 =\=
除了算术不等式之外还有其他含义。
1982年第3版是online,=\=
是
even documented 在同一份文档中作为 1978 年 DECsystem 10 Prolog 的一部分。因此即使在上述书籍的先前版本中也存在矛盾。
文本到此为止。但是你说 dif/2
优于 (\==)/2
是完全正确的。有关更多信息,请参阅 this。
我正在研究 'Prolog by Example' 书中的一个例子(作者是 Helder Coelho 和 Jose C Cotta)。
Write a program for designing an architectural unit
obeying the following specifications:
+ Two rectangular rooms
+ Each room has a window and interior door
+ Rooms are connected by interior door
+ One room also has an exterior door
+ A wall can have only one door or window
+ No window can face north
+ Windows cannot be on opposite sides of the unit
有一个解决方案,但它在 SWI-Prolog 中不起作用,甚至无法编译。
我不得不更改书中的解决方案:使用 'dif(X,Y)' 而不是 'X =\= Y'。
谁能解释一下:为什么原来的解决方案不起作用?
这是一个代码:
plan(FD,D1,W1,D2,W2) :-
frontroom(FD,D1,W1),
opposite(D1,D2),
room(D2,W2),
notopposite(W1,W2).
frontroom(FD,D,W) :-
room(D,W),
direction(FD),
dif(FD,D), % FD =\= D, % <- original was commented
dif(FD,W). % FD =\= W. % <- original was commented
room(D,W) :-
direction(D),
direction(W),
dif(D,W), % D =\= W, % <- original was commented
dif(W,north). % W =\= north. % <- original was commented
direction(north).
direction(south).
direction(east).
direction(west).
opposite(north,south).
opposite(south,north).
opposite(east,west).
opposite(west,east).
notopposite(D1,D2) :-
opposite(D1,D3),
dif(D2,D3). % D2 =\= D3. % <- original was commented
我可以查询并得到答案:
?- plan(west,D1,W1,D2,W2).
D1 = east,
W1 = south,
D2 = west,
W2 = south
我试图追踪原始解决方案(使用 D2 =\= D3 而不是 dif(D2,D3))并得到这个:
[trace] ?- plan(west,D1,W1,D2,W2).
Call: (8) plan(west, _9632, _9634, _9636, _9638) ? creep
Call: (9) frontroom(west, _9632, _9634) ? creep
Call: (10) room(_9632, _9634) ? creep
Call: (11) direction(_9632) ? creep
Exit: (11) direction(north) ? creep
Call: (11) direction(_9634) ? creep
Exit: (11) direction(north) ? creep
Call: (11) north=\=north ? creep
ERROR: Arithmetic: `north/0' is not a function
为什么我不能使用'=\='?
应该是 \==
而不是不正确的 =\=
。我不知道 =\=
除了算术不等式之外还有其他含义。
1982年第3版是online,=\=
是
even documented 在同一份文档中作为 1978 年 DECsystem 10 Prolog 的一部分。因此即使在上述书籍的先前版本中也存在矛盾。
文本到此为止。但是你说 dif/2
优于 (\==)/2
是完全正确的。有关更多信息,请参阅 this。