Next: , Previous: , Up: prolog-libraries   [Index]


16.13.7 Prolog coroutines framework

Guile prolog coroutining is a development ontop of attributed variables, it contains code that can be used from the library (logic guile-log guile-prolog coroutine).

prolog freeze(Var,Goal), evaluate Goal when Var is bound.

prolog freezeBig(Var, Goal, Unwinder), the same as above, but Unwinder, will be called at backtracking over this signal.

prolog@(guile-user)> freeze(X,write(bound)),X=1.
bound
   X = #(<#gp X0>)
   Y = X1
more (y/n/a/s) >

prolog@(guile-user)> freeze(X,write(bound)),X=Y.
   X = #(<#gp X0>)
   Y = X1
more (y/n/a/s) >

prolog frozen(Var, Goal), find the goals Goal associated to fromzen var Var

freezeId, the identity of the attribute associated with freeze

prolog when(Signal, Goal), Signal is an expression using operators “,” “;” “=”, nonvar(X) and ground(X). When an binding appears this expression will be checked for validity and if so Goal is executed.

prolog when(Signal, Goal, Off), the same as above but Off is true a no signals will be applied if the variable is reseted to a new value.

prolog whenBig(Signal, Goal, PreGoal, Unwinder), the same as above, but PreGoal is executed before the binding is done, and Unwinder is code that is executed at the unwinding of an successful signaled.

prolog@(guile-user)> when(X=1,write(bang)),X=2.

   X = 2
more (y/n/a/s) > n
prolog@(guile-user)> when(X=1,write(bang)),X=1.
bang
   X = 1
more (y/n/a/s) > n

prolog whenBig(Signal, Goal, Off, PreGoal, Unwinder) The same as above but with an Off variable.

with_coroutines(X,Constructor, Code), this will apply a coroutine constructor Constructor, and in that there is a reference to X, that needs to be an attribute, this will then be a model for variables that are created under an auto atributize environmentm kike using a functor adaptable_vars. inside the Code goal.

prolog with_coroutines(X,[References,...],Constr,Code), the same as above, but all variables inside References will not be copied but referenced. The default is to copy.

prolog auto(Var), this will asume that Var is a variable and will bless is throug binding it to an auto coroutine variable as descrived above.

/* code -----------------------*/
scm[(use-modules (logic guile-log))].
scm[(use-modules (logic guile-log iso-prolog))].
scm[(use-modules (logic guile-log guile-prolog coroutine))].

- adaptable_vars.
f1(X) :- X=[1,Y],Y=1.

- adaptable_vars.
f2(X) :- X=[1,Y],Y=2.
/* --------------------------- */
/* interaction */
prolog@(guile-user)> with_coroutines(X,[Z],when((nonvar(X),X=1,X=Z), 
                                                  write(ok))
                                            ,(Z=2,f1(Y))).

   X = #(<#gp X0>)
   Z = 2
   Y = [1, 1]


prolog@(guile-user)> with_coroutines(X,[Z],when((nonvar(X),X=1,X=Z), 
                                                  write(ok))
                                            ,(Z=1,f1(Y))).
ok
   X = #(<#gp X0>)
   Z = 1
   Y = [1, 1]

Next: , Previous: , Up: prolog-libraries   [Index]