Legend:

The package ''monad-parallel'' defines classes of monads that are capable of:

3

The low-level libraries available with GHC provide two completely different ways to execute tasks in parallel: [http://hackage.haskell.org/packages/archive/parallel/1.1.0.1/doc/html/Control-Parallel.html#v:par par] and [http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.2.0.0/Control-Concurrent.html#v%3AforkIO forkIO]. The former can be applied to any pure computation, while the latter works only in the IO monad.

4

5

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:

4

6

* splitting or forking monadic computations to be performed in parallel, and

5

7

* combining the results of those computations when they complete.

8

9

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.

10

11

== Class `MonadFork` ==

12

13

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

14

15

{{{

16

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

17

}}}

18

19

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:

20

21

{{{

22

task1 :: Monad Int

23

task2 :: Monad Int

24

task3 :: Monad Int

25

26

example = do handle1 <- forkExec task1

27

handle2 <- forkExec task2

28

result3 <- task3

29

result1 <- handle1

30

result2 <- handle2

31

return (result1 + result2 + result3)

32

}}}

33

34

== Class `MonadParallel` ==

35

36

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