Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

constraints - CHR: How do you call Prolog code inside a rule

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Seems like I got it ..!! the prolog calls have to be first in this case rand() i.e.

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

except the guard, still cant use the updated values ... probably cause they are calculated afterwards.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...