% Dining philosophers

phi(R1,L1), fork{+R1,+L2},
phi(L2,R2), fork{+R2,+L3},
phi(L3,R3), fork{+R3,+L4},
phi(L4,R4), fork{+R4,+L5},
phi(L5,R5), fork{+R5,+L1}.

l@@ %% grab a left fork
fork{+X,+L}, phi(L,R),  :-
fork{-X,+L}, phi(L,R).

r@@ %% grab a right fork
fork{-X,+L}, phi(L,R), fork{+R,+Y} :-
fork{-X,+L}, phi(L,R), fork{+R,-Y}.

lr@@ %% release forks
fork{-X,+L}, phi(L,R), fork{+R,-Y} :-
fork{+X,+L}, phi(L,R), fork{+R,+Y}.
