CHR:如何在规则中调用Prolog代码?



CHR:如何在规则中调用Prolog代码?我猜它就像DCG !!但似乎不对

:- use_module(library(chr)).
:- chr_constraint move/2,pos/2, next/0.
:- [utils].

main :- pos(5,5).
rand(X,Y) :- random(0,2,X), random(0,2,Y), say("~w,~w",[X,Y]).
pos(X,Y), move(A,B) <=> pos(6,6).%X1 > 0, X1 < 10, Y1 > 0, Y1 < 10 |  pos(X1,Y1), {X1 is X + A, Y1 is Y + B}.
next <=> move(X,Y), {rand(X,Y)}.  <<<-----

错误:

?- pos(5,5),next.
ERROR: Unknown procedure: {}/1
ERROR: In:
ERROR:   [13] {rand(_35671298,_35671300)}
ERROR:   [12] next___0__0(suspension(470241,removed,_35671332,0,user: ...,next)) at grid.pl:12
ERROR:    [9] <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.
Exception: (13) {rand(_35670676, _35670678)} ? abort
% Execution Aborted

======================

这个工作,但必须设置位置LAST !!并且不能使用GUARD !

:- use_module(library(chr)).
:- chr_constraint move/2,pos/2, next/0.
:- [utils].
rand(X,Y) :- random(0,2,X), random(0,2,Y), say("~w,~w",[X,Y]).
next <=> move(X,Y), rand(X,Y).  
move(A,B),pos(X,Y) <=> X1 is X + A, Y1 is Y + B , X1 > 0, X1 < 10, Y1 > 0, Y1 < 10,  pos(X1,Y1).

首先生成移动!!奇怪的……每当我在pos()后面加上next时,它就会失败。

?- next,next,next,pos(5,5).
1,0
1,1
0,0
pos(7, 6).

似乎我明白了…!!在这种情况下,prolog调用必须是第一个rand(),即

next <=>  rand(X,Y), move(X,Y).  

除了守卫,仍然不能使用更新后的值…可能是因为它们是事后计算的。

最新更新