A module containing the ALT constructs. An ALT (a term inherited from occam) is a choice between several events. In CHP, we say that an event must support alting to be a valid choice. Events that do support alting are:
- Reading from a channel (including extended reads)
- Writing to a channel
- Synchronising on a barrier
- An alting construct (that is, you can nest alts)
- A sequential composition, if the first event supports alting
Examples of events that do NOT support alting are:
- Enrolling and resigning with a barrier
- Poisoning a channel
- Processes composed in parallel
- Any lifted IO event
- Creating channels, barriers, etc
- Claiming a shared channel (yet...)
It is not easily possible to represent this at the type level (while still making CHP easy to use). Therefore it is left to you to not try to alt over something that does not support it. Given how much of the library does support alting, that should hopefully be straightforward.
Here are some examples of using alting:
- Wait for an integer channel, or 1 second to elapse:
liftM Just (readChannel c) <-> (waitFor 1000000 >> return Nothing)
- Check if a channel is ready, otherwise return immediately. Note that you must use the alt operator with priority here, otherwise your skip guard might be chosen, even when the channel is ready.
liftM Just (readChannel c) </> (skip >> return Nothing)
- Wait for input from one of two (identically typed) channels
readChannel c0 <-> readChannel c1
- Check if a channel is ready; if so send, it on, otherwise return immediately:
(readChannel c >>= writeChannel d) </> skip
An alt between several actions, with arbitrary priority. The first available action is chosen (with an arbitrary choice if many guards are available at the same time), its body run, and its value returned.
An alt between several actions, with arbitrary priority. The first available action is chosen (biased towards actions nearest the beginning of the list), its body run, and its value returned.