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


16.13.3 Zip like constructs

This is a construct that let you execute several goals in paralell. currently this is not the same as executing it on multiple threads or processors, it’s more that you want to synch solutions to several goal at the same time. The interface sports two commands, one simpler zip, for use, when the goal’s are synchronized, and another one usr_zip, for the case when one would like to test the output and in depending on the outcome take the next solution for a subset of the branches.

16.13.3.1 Api

zip(lane(V,Goal),...), will execute Goal ..., in parallel and output the values of the variables V..., for the different branches.

usr_zip(lane(tag,V,Goal),...,guard), this is the same as zip, but we will tag the lane as well. Inside guard, one can execute a command

update(tag,...), where the lanes associated to the tags tag ... is backtracked and updated with new solutions.

16.13.3.2 Examples

  c(X,A)   :- c(X,A,0). 
  c(X,A,N) :- N < 10, 
                (X = A ; (B is A + 1, M is N + 1, c(X,B,M))).

  f(X,Y) :- zip(lane(X, c(X,0)    ),
                lane(Y, c(Y,100)  )).

  (prolog-run * (X Y) (f X Y))

$1 = ((0 100) (1 101) (2 102) (3 103) (4 104) 
      (5 105) (6 106) (7 107) (8 108) (9 109))


  g(X,Y) :- usr_zip(lane(Tag_a,X,c(X,0)),
                    lane(Tag_b,Y,c(Y,0)),
                    (((X mod 2) == 0 -> update(Tag_a)),
                     ((Y mod 1) == 0 -> update(Tag_b)))).

$2 = ((1 100) (3 102) (5 104) (7 106) (9 108))