CHR: How do you call Prolog code inside a rule ?
I assume it is like DCG !!! but doesn't seem right
:- 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)}. <<<-----
error :
?- 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
======================
this works, but have to set the position LAST !!! and cant use the 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).
first generate the moves !! weird ... any time I put next after pos() it fails..
?- next,next,next,pos(5,5).
1,0
1,1
0,0
pos(7, 6).
question from:
https://stackoverflow.com/questions/65947830/chr-how-do-you-call-prolog-code-inside-a-rule