MyNixOS website logo
Description

Parallel execution of monadic computations.

This package defines classes of monads that can perform multiple executions in parallel and combine their results. For any monad that's an instance of the class, the package re-implements a subset of the Control.Monad interface, but with parallel execution.

The low-level libraries available with GHC provide two completely different ways to execute tasks in parallel: par and forkIO. The former can be applied to any pure computation, while the latter works only in the IO monad.

The purpose of the monad-parallel library package is to unify the two approaches. For this purpose, it defines classes of monads that are capable of:

  • splitting or forking monadic computations to be performed in parallel, and
  • combining the results of those computations when they complete.

The way these classes are implemented by the IO monad (as well as any MonadIO instance) is different from the way they're implemented by stateless monads, but the classes abstract these implementation details away.

Class MonadFork

A monad that's an instance of the MonadFork class supports method

forkExec :: MonadFork m => m a -> m (m a)

This function launches the argument computation in a parallel thread and returns a handle to it. The main thread is free to perform some other task and then obtain the result of the parallel computation:

task1 :: MonadFork m => m Int
task2 :: MonadFork m => m Int
task3 :: MonadFork m => m Int

example = do handle1 <- forkExec task1
             handle2 <- forkExec task2
             result3 <- task3
             result1 <- handle1
             result2 <- handle2
             return (result1 + result2 + result3)

Class MonadParallel

A monad that's an instance of the MonadParallel class supports method

bindM2 :: MonadParallel m => (a -> b -> m c) -> m a -> m b -> m c

This method executes its m a and m b arguments in parallel. Once they're both finished, bindM2 runs their results through its first argument and returns the final result. Note that bindM2 can be implemented using forkExec, but not vice versa.

Based on this foundation, the library defines and exports other useful functions. These replicate the functionality of the Control.Monad module, but they execute their monadic arguments in parallel.

bindM3      :: MonadParallel m => (a -> b -> c -> m d) -> m a -> m b -> m c -> m d
liftM2      :: MonadParallel m => (a -> b -> c) -> m a -> m b -> m c
liftM3      :: MonadParallel m => (a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
ap          :: MonadParallel m => m (a -> b) -> m a -> m b
sequence    :: MonadParallel m => [m a] -> m [a]
sequence_   :: MonadParallel m => [m a] -> m () 
mapM        :: MonadParallel m => (a -> m b) -> [a] -> m [b]
replicateM  :: MonadParallel m => Int -> m a -> m [a]
replicateM_ :: MonadParallel m => Int -> m a -> m ()

Links

Metadata

Version

0.8

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows