chp-plus-1.3.1.2: A set of high-level concurrency utilities built on Communicating Haskell Processes

Safe HaskellSafe-Inferred

Control.Concurrent.CHP.Composed

Description

A module containing a Composed monad.

The Composed monad can be thought of as an equivalent to functions elsewhere in chp-plus (especially the Control.Concurrent.CHP.Connect module) that support partial application of processes when wiring them up.

Binding in this monad can be thought of as "and then wire that like this". You compose your processes together with a series of monadic actions, feeding processes into each function that wires up the next parameter, then taking the results of that action and further wiring it up another way. At the end of the monadic block you should return the full list of wired-up processes, to be run in parallel using the run (or run_) functions.

Here is a simple example. You have a list of processes that take an incoming and outgoing channel end and a barrier, and you want to wire them into a cycle and enroll them all on the barrier:

 processes :: [Chanin a -> Chanout a -> EnrolledBarrier -> CHP ()]

 runProcesses = do b <- newBarrier
                   run $ cycleR processes >>= enrollAllR b

The order of the actions in this monad tends not to matter (it is a commutative monad for the most part) so you could equally have written:

 processes :: [EnrolledBarrier -> Chanin a -> Chanout a -> CHP ()]

 runProcesses = do b <- newBarrier
                   run $ enrollAllR b processes >>= cycleR

Remember with this monad to return all the processes to be run in parallel; if they are not returned, they will not be run and you will likely get deadlock.

A little more background on the monad is available in this blog post: http://chplib.wordpress.com/2010/01/19/the-process-composition-monad/

Synopsis

Documentation

data Composed a Source

A monad for composing together CHP processes in cross-cutting ways; e.g. wiring together a list of processes into a pipeline, but also enrolling them all on a barrier.

runWith :: Composed a -> forall b. (a -> CHP b) -> CHP bSource

See run and run_

run :: Composed [CHP a] -> CHP [a]Source

Given a list of CHP processes composed using the Composed monad, runs them as a parallel bunch of CHP results (with runParallel) and returns the results.

run_ :: Composed [CHP a] -> CHP ()Source

Like run but discards the results (uses runParallel_).

enrollR :: Enrollable b p => b p -> (Enrolled b p -> a) -> Composed aSource

Like enroll, this takes a barrier and a process wanting a barrier, and enrolls it for the duration, but operates using the Composed monad.

enrollAllR :: Enrollable b p => b p -> [Enrolled b p -> a] -> Composed [a]Source

Given an Enrollable item (such as a Barrier), and a list of processes, composes them by enrolling them all on the given barrier.

connectR :: Connectable l r => ((l, r) -> a) -> Composed aSource

Like connect but operates in the Composed monad.

pipelineR :: Connectable l r => [r -> l -> a] -> Composed (r -> l -> [a])Source

Wires a list of processes into a pipeline that takes the two channels for the ends of the pipeline and returns the list of wired-up processes.

pipelineCompleteR :: Connectable l r => (l -> a) -> [r -> l -> a] -> (r -> a) -> Composed [a]Source

cycleR :: Connectable l r => [r -> l -> a] -> Composed [a]Source

Connects together a list of processes into a cycle.

wrappedGridFourR :: (Connectable below above, Connectable right left) => [[FourWay above below left right -> a]] -> Composed [[a]]Source

Like wrappedGridFour, but in the Composed monad.