| | 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 |
| | 37 | |
| | 38 | {{{ |
| | 39 | bindM2 :: MonadParallel m => (a -> b -> m c) -> m a -> m b -> m c |
| | 40 | }}} |
| | 41 | |