Safe Haskell | Safe-Infered |
---|

This module defines classes of monads that can perform multiple computations in parallel and, more importantly, combine the results of those parallel computations.

There are two classes exported by this module, `Parallel`

and `Fork`

. The former is more generic, but the
latter is easier to use: when invoking any expensive computation that could be performed in parallel, simply wrap the
call in `forkExec`

. The function immediately returns a handle to the running computation. The handle can be used to
obtain the result of the computation when needed:

do child <- forkExec expensive otherStuff result <- child

In this example, the computations *expensive* and *otherStuff* would be performed in parallel. When using the
`Parallel`

class, both parallel computations must be specified at once:

bindM2 (\ childResult otherResult -> ...) expensive otherStuff

In either case, for best results the costs of the two computations should be roughly equal.

Any monad that is an instance of the `Fork`

class can also be instance of the `Parallel`

class
by the following rule:

bindM2 f ma mb = do {a' <- forkExec ma; b <- mb; a <- a'; f a b}

When operating with monads free of side-effects, such as `Identity`

or `Maybe`

, `forkExec`

is equivalent to `return`

and `bindM2`

is equivalent to ` \ f ma mb -> do {a <- ma; b <- mb; f a b} `

— the only difference is in the
resource utilisation. With the `IO`

monad, on the other hand, there may be visible difference in the results because
the side effects of *ma* and *mb* may be arbitrarily reordered.

- class Monad m => Parallel m where
- bindM2 :: (a -> b -> m c) -> m a -> m b -> m c

- class Monad m => Fork m where
- forkExec :: m a -> m (m a)

- bindM3 :: Parallel m => (a -> b -> c -> m d) -> m a -> m b -> m c -> m d
- liftM2 :: Parallel m => (a -> b -> c) -> m a -> m b -> m c
- liftM3 :: Parallel m => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
- ap :: Parallel m => m (a -> b) -> m a -> m b
- sequence :: Parallel m => [m a] -> m [a]
- sequence_ :: Fork m => [m a] -> m ()
- mapM :: Parallel m => (a -> m b) -> [a] -> m [b]
- replicateM :: Parallel m => Int -> m a -> m [a]
- replicateM_ :: Fork m => Int -> m a -> m ()
- defaultForkExec :: Monad m => m a -> m (m a)
- defaultBindM2 :: Monad m => (a -> b -> m c) -> m a -> m b -> m c
- bindTrans :: (MonadTransControl t, Parallel m, Monad m, Monad (t m)) => (a -> b -> t m c) -> t m a -> t m b -> t m c
- forkTrans :: (MonadTransControl t, Fork m) => t m a -> t m (t m a)
- parallelIO :: Fork m => (a -> b -> m c) -> m a -> m b -> m c

# Classes

class Monad m => Parallel m whereSource

Class of types that can perform two computations in parallel and bind their results together.

bindM2 :: (a -> b -> m c) -> m a -> m b -> m cSource

Perform two monadic computations in parallel; when they are both finished, pass the results to the function.
Apart from the possible ordering of side effects, this function is equivalent to
`\f ma mb-> do {a <- ma; b <- mb; f a b}`

Parallel [] | |

Parallel IO | |

Parallel Maybe | |

Parallel Identity | Any monad that allows the result value to be extracted, such as |

Parallel ((->) r) | |

Parallel m => Parallel (ListT m) | |

Parallel (ResourceT IO) | |

Parallel m => Parallel (MaybeT m) | |

Parallel m => Parallel (IdentityT m) | |

(Parallel m, Error e) => Parallel (ErrorT e m) | |

Parallel m => Parallel (ReaderT r m) |

class Monad m => Fork m whereSource

Class of monads that can fork a parallel computation.

bindM3 :: Parallel m => (a -> b -> c -> m d) -> m a -> m b -> m c -> m dSource

Perform three monadic computations in parallel; when they are all finished, pass their results to the function.

# Control.Monad equivalents

liftM2 :: Parallel m => (a -> b -> c) -> m a -> m b -> m cSource

Like `liftM2`

, but evaluating its two monadic arguments in parallel.

liftM3 :: Parallel m => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m rSource

Like `liftM3`

, but evaluating its three monadic arguments in parallel.

ap :: Parallel m => m (a -> b) -> m a -> m bSource

Like `ap`

, but evaluating the function and its argument in parallel.

mapM :: Parallel m => (a -> m b) -> [a] -> m [b]Source

Like `mapM`

, but applying the function to the individual list items in parallel.

replicateM :: Parallel m => Int -> m a -> m [a]Source

Like `replicateM`

, but executing the action multiple times in parallel.

replicateM_ :: Fork m => Int -> m a -> m ()Source

Like `replicateM_`

, but executing the action multiple times in parallel.

# Default instances

defaultForkExec :: Monad m => m a -> m (m a)Source

defaultBindM2 :: Monad m => (a -> b -> m c) -> m a -> m b -> m cSource

bindTrans :: (MonadTransControl t, Parallel m, Monad m, Monad (t m)) => (a -> b -> t m c) -> t m a -> t m b -> t m cSource

Common bind pattern for monad transformers

forkTrans :: (MonadTransControl t, Fork m) => t m a -> t m (t m a)Source

parallelIO :: Fork m => (a -> b -> m c) -> m a -> m b -> m cSource