chp-1.0.2: An implementation of concurrency ideas from Communicating Sequential ProcessesSource codeContentsIndex
Control.Concurrent.CHP.Alt
Description

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:

  • Control.Concurrent.CHP.Monad.skip
  • Control.Concurrent.CHP.Monad.waitFor
  • 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
Synopsis
alt :: [CHP a] -> CHP a
(<->) :: CHP a -> CHP a -> CHP a
priAlt :: [CHP a] -> CHP a
(</>) :: CHP a -> CHP a -> CHP a
Documentation
alt :: [CHP a] -> CHP aSource
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.
(<->) :: CHP a -> CHP a -> CHP aSource
A useful operator to perform an alt. This operator is associative, and has arbitrary priority. When you have lots of guards, it is probably easier to use the alt function. alt may be more efficent than foldl1 (<->)
priAlt :: [CHP a] -> CHP aSource
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.
(</>) :: CHP a -> CHP a -> CHP aSource
A useful operator to perform a priAlt. This operator is associative, and has descending priority (that is, it is left-biased). When you have lots of actions, it is probably easier to use the priAlt function. priAlt may be more efficent than foldl1 (</>)
Produced by Haddock version 2.3.0