guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

coroutines in guile-log


From: Stefan Israelsson Tampe
Subject: coroutines in guile-log
Date: Sat, 20 Sep 2014 20:38:15 +0200

I was asked on the #prolog list to implement coroutines and attributed variables, you may 
find swi prolog docs for these at

http://www.swi-prolog.org/pldoc/man?section=extvar

now this works in guile-log,

scheme@(guile-user)> (use-modules (logic guile-log guile-prolog coroutine))
scheme@(guile-user)> (use-modules (logic guile-log iso-prolog))
scheme@(guile-user)> ,L prolog
Happy hacking with Prolog!  To switch back, type `,L scheme'.
prolog@(guile-user)> freeze(X, write(hey)), X=3.
hey
   X = 3
more (y/n/a) > n
prolog@(guile-user)> 

The idea of freeze(Var,Goal) is for variable  Var to, when it is bound execute the goal Goal,
The basic tool for this is the attributed variables that in guile-log issue a callback function 
when unification is done on a variable, hence you can mod the unification algorithm in many
many different ways, it is interesting to see how the implementation of freeze is done, 
basically in prolog you have do,



freezeId(Val, Var, Raw, Plus) :- 
    Plus,var(Val);attvar(Val);
    (
        get_attr(Var,freezeId, Atts), 
        do_all_atts(Atts),
        del_attr(Var, freezeId),
        Var = Val
    ).

freeze(Var,Goal) :-
   (var(Var),
      ( 
        get_attr(Var, freezeId, Att),!,
        Att2 = [Goal|Att],
        put_attr(Var, freezeId, Att2)
      );
      (
        put_attr(Var, freezeId, [Goal])
      )
   );
   call(Goal).

So feeezeId, is the tag to associate a variable with a custom behavior and at the same time 
the unifyier, Raw is true if unification is without occurs check and Plus is normal unification and match if false e.g. comparison without unification.

the freezeId finds if Var is bound or non attribute, the Attribute associated with freezeId and then execute all stored goals and finally removes the attribute and set the variables value.

freeze just add the attribute as a list of goals to the variable or add the goal to an already existing attributes goal list.

So it's not rocket science, but still has raw edges and the deiel is in the details, e.g. if you read the documentation you my find out issues with serializing and copying terms with associated attributes. Atm all attributes will be saved and reconstructed at term writing and reading, also everything is copied at a term copy. Hot compilation of prolog code is not working atm with attributed variables.

So that's the current state of this feature, I'm happy to get it working and just chimed in to 
share the fun, Of cause there is a scheme interface as well to this and it can be used in the kanren interface as well, so happy hacking!!

yours
Stefan



reply via email to

[Prev in Thread] Current Thread [Next in Thread]