## 5.2. Hygienic Dining Philosophers

Here we implement a different solution to the Dining Philosophers problem, described in "The Drinking Philosophers Problem", by K. M. Chandy and J. Misra [4]. Briefly, this algorithm efficiently and fairly solves the dining philosophers problem for philosophers connected in an arbitrary graph (as opposed to a simple ring). The algorithm works by augmenting each fork with a clean/dirty state. Initially, all forks are dirty. A philosopher is only obliged to relinquish a fork to its neighbor if the fork is dirty. On receiving a fork, the philosopher cleans it. On eating, the philosopher dirties all forks. For full details of the algorithm, consult the original paper.

```{- The "hygenic solution to the diners problem",
described in "The Drinking Philosophers Problem", by
K. M. Chandy and J. Misra.
-}

{-
Use a Scala set implementation.
Operations on this set are not synchronized.
-}
import class ScalaSet = "scala.collection.mutable.HashSet"

{-
Make a set initialized to contain
the items in the given list.
-}
def Set(items) = ScalaSet() >s> joinMap(s.add, items) >> s

{-
Start a philosopher process; never publishes.

name: identify this process in status messages
mbox: our mailbox
missing: set of neighboring philosophers holding our fork
-}
def philosopher(name, mbox, missing) =
val send = mbox.put
-- deferred requests for forks
val deferred = Channel()
-- forks we hold which are clean
val clean = Set([])

def sendFork(p) =
p(("fork", send))

def requestFork(p) =
p(("request", send))

-- While thinking, start a timer which
-- will tell us when we're hungry
def digesting() =
Println(name + " thinking") >>
thinking()
| Rwait(Random(30)) >>
send(("rumble", send)) >>
stop

def thinking() =
def on(("rumble", _)) =
Println(name + " hungry") >>
map(requestFork, missing.toList()) >>
hungry()
def on(("request", p)) =
sendFork(p) >>
thinking()

def hungry() =
def on(("fork", p)) =
missing.remove(p) >>
(
if missing.isEmpty() then
Println(name + " eating") >>
eating()
else hungry()
)
def on(("request", p)) =
if clean.contains(p) then
deferred.put(p) >>
hungry()
else
sendFork(p) >>
requestFork(p) >>
hungry()

def eating() =
clean.clear() >>
Rwait(Random(10)) >>
map(sendFork, deferred.getAll()) >>
digesting()

digesting()

{-
Create an NxN 4-connected grid of philosophers.  Each philosopher holds the
fork for the connections below and to the right (so the top left philosopher
holds both its forks).
-}
def philosophers(n) =
{- channels -}
val cs = uncurry(Table(n, lambda (_) = Table(n, ignore(Channel))))

{- first row -}
philosopher((0,0), cs(0,0), Set([]))
| for(1, n) >j>
philosopher((0,j), cs(0,j), Set([cs(0,j-1).put]))

{- remaining rows -}
| for(1, n) >i> (
philosopher((i,0), cs(i,0), Set([cs(i-1,0).put]))
| for(1, n) >j>
philosopher((i,j), cs(i,j), Set([cs(i-1,j).put, cs(i,j-1).put]))
)

philosophers(2)

{-
OUTPUT:EXAMPLE
(0, 0) thinking
(0, 1) thinking
(1, 0) thinking
(1, 1) thinking
(1, 0) hungry
(0, 0) hungry
(0, 1) hungry
(1, 1) hungry
(1, 1) eating
(1, 1) thinking
...
-}
```

Our implementation is based on the actor model of concurrency. An actor is a state machine which reacts to messages. On receiving a message, an actor can send asynchronous messages to other actors, change its state, or create new actors. Each actor is single-threaded and processes messages sequentially, which makes some concurrent programs easier to reason about and avoids explicit locking. Erlang is one popular language based on the actor model.

Orc emulates the actor model very naturally. In Orc, an actor is an Orc thread of execution, together with a `Channel` which serves as a mailbox. To send a message to an actor, you place it in the actor's mailbox, and to receive a message, the actor gets the next item from the mailbox. The internal states of the actor are represented by functions: while an actor's thread of execution is evaluating a function, it is considered to be in the corresponding state. Because Orc implements tail-call optimization, state transitions can be encoded as function calls without running out of stack space.

In this program, a philosopher is implemented by an actor with three primary states: `eating`, `thinking`, and `hungry`. An additional transient state, `digesting`, is used to start a timer which will trigger the state change from `thinking` to `hungry`. Each state is implemented by a function which reads a message from the mailbox, selects the appropriate action using pattern matching, performs the action, and finally transitions to the next state (possibly the same as the current state) by calling the corresponding function.

Forks are never represented explicitly. Instead each philosopher identifies a fork with the "address" (sending end of a mailbox) of the neighbor who shares the fork. Every message sent includes the sender's address. Therefore when a philosopher receives a request for a fork, it knows who requested it and therefore which fork to relinquish. Likewise when a philosopher receives a fork, it knows who sent it and therefore which fork was received.

[4] K. Mani Chandy and Jayadev Misra. 1984. The drinking philosophers problem. ACM Trans. Program. Lang. Syst. 6, 4 (October 1984), 632-646.