HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulemonad-parallel-0.8Haskell2010

Control.Monad.Parallel

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, MonadParallel and MonadFork. 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 MonadParallel 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 MonadFork class is also an instance of the MonadParallel class, and the following law should hold:

 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.

  • 2 classes
  • 12 values

Classes

3 declarations
classclass Monad m => MonadParallel (m :: Type -> Type) where
#

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

Methods

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

    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}

Instances9MonadParallel, …
classclass MonadParallel m => MonadFork (m :: Type -> Type) where
#

Class of monads that can fork a parallel computation.

Methods

  • forkExec :: m a -> m (m a)

    Fork a child monadic computation to be performed in parallel with the current one.

Instances8MonadFork, …
valuebindM3
  1. :: MonadParallel m
  2. => a -> b -> c -> m d
  3. -> m a
  4. -> m b
  5. -> m c
  6. -> m d
#

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

Control.Monad equivalents

11 declarations
valueap :: MonadParallel m => m (a -> b) -> m a -> m b
#

Like ap, but evaluating the function and its argument in parallel.

valueforM :: MonadParallel m => [a] -> (a -> m b) -> m [b]
#

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

valueforM_ :: MonadParallel m => [a] -> (a -> m b) -> m ()
#

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

valueliftM2 :: MonadParallel m => (a -> b -> c) -> m a -> m b -> m c
#

Like liftM2, but evaluating its two monadic arguments in parallel.

valueliftM3
  1. :: MonadParallel m
  2. => a1 -> a2 -> a3 -> r
  3. -> m a1
  4. -> m a2
  5. -> m a3
  6. -> m r
#

Like liftM3, but evaluating its three monadic arguments in parallel.

valuemapM :: MonadParallel m => (a -> m b) -> [a] -> m [b]
#

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

valuemapM_ :: MonadParallel m => (a -> m b) -> [a] -> m ()
#

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