/*-------------------------------------------------------------------------
    program: producer/consumer problem (bounded single-buffer)
    file:    pcbsb
    date:    December 97
    author:  Pablo Lopez (lopez@lcc.uma.es)
             Dept. of CS
             University of Malaga, SPAIN
 
    sample goal:
        go([1,2,3],N,O)
-------------------------------------------------------------------------*/

module pcbsb.

% linearity assures mutual exclusion access to "buffer" and "free"

producer([I|Is]) # buffer(B) # free(N)
      <= N > 0
      <= N_1 is N-1
      <= insert(I,B,BI)
      *- producer(Is) # buffer(BI) # free(N_1).

producer([]) *- bot.

consumer(Os) # buffer([I|B]) # free(N)
      <= N1 is N+1
      <= O is 2*I
      <= insert(O,Os,OsO)
      *- consumer(OsO) # buffer(B) # free(N1).

consumer(Os) # buffer([]) # free(_) # result(Os).

insert(X,[],[X]).
insert(X,[Y|Ys],[Y|Zs]) <= insert(X,Ys,Zs).

go(I,N,O) *- producer(I) # consumer([]) #
             buffer([]) # free(N) #
             result(O).
