abstract-par-0.3.3: Type classes generalizing the functionality of the 'monad-par' library.

Safe Haskell | Safe
Language | Haskell98

Control.Monad.Par.Class

Contents

- Futures
- IVars

Description

This module establishes a class hierarchy that captures the interfaces of `Par` monads. There are two layers: simple futures (`ParFuture`) and full `IVars` (`ParIVar`). All `Par` monads are expected to implement the former, some also implement the latter.

For more documentation of the programming model, see

- The Control.Monad.Par module in the `monad-par` package.
- The wiki/tutorial (http://www.haskell.org/haskellwiki/Par_Monad:_A_Parallelism_Tutorial)
- The original paper (http://www.cs.indiana.edu/~rrnewton/papers/haskell2011_monad-par.pdf)
- Tutorial slides (http://community.haskell.org/~simonmar/slides/CUFP.pdf)
- Other slides (http://www.cs.ox.ac.uk/ralf.hinze/WG2.8/28/slides/simon.pdf, http://www.cs.indiana.edu/~rrnewton/talks/2011_HaskellSymposium_ParMonad.pdf)

Synopsis

- class Monad m => ParFuture future m | m -> future where
  - spawn :: NFData a => m a -> m (future a)
  - spawn_ :: m a -> m (future a)
  - get :: future a -> m a
  - spawnP :: NFData a => a -> m (future a)
- class ParFuture ivar m => ParIVar ivar m | m -> ivar where
  - fork :: m () -> m ()
  - new :: m (ivar a)
  - put :: NFData a => ivar a -> a -> m ()
  - put_ :: ivar a -> a -> m ()
  - newFull :: NFData a => a -> m (ivar a)
  - newFull_ :: a -> m (ivar a)
- class NFData a

# Futures

class Monad m => ParFuture future m | m -> future where Source

`ParFuture` captures the class of Par monads which support futures. This level of functionality subsumes `par`/`pseq` and is similar to the Control.Parallel.Strategies.Eval monad.

A minimal implementation consists of `spawn_` and `get`. However, for monads that are also a member of `ParIVar` it is typical to simply define `spawn` in terms of `fork`, `new`, and `put`.

Minimal complete definition

spawn_, get

Methods

spawn :: NFData a => m a -> m (future a) Source

Create a potentially-parallel computation, and return a future (or promise) that can be used to query the result of the forked computataion.

     spawn p = do
       r <- new
       fork (p >>= put r)
       return r

spawn_ :: m a -> m (future a) Source

Like `spawn`, but the result is only head-strict, not fully-strict.

get :: future a -> m a Source

Wait for the result of a future, and then return it.

spawnP :: NFData a => a -> m (future a) Source

Spawn a pure (rather than monadic) computation. Fully-strict.

     spawnP = spawn . return

# IVars

class ParFuture ivar m => ParIVar ivar m | m -> ivar where Source

`ParIVar` builds on futures by adding full anyone-writes, anyone-reads IVars. These are more expressive but may not be supported by all distributed schedulers.

A minimal implementation consists of `fork`, `put_`, and `new`.

Minimal complete definition

fork, new, put_

Methods

fork :: m () -> m () Source

Forks a computation to happen in parallel. The forked computation may exchange values with other computations using `IVar`s.

new :: m (ivar a) Source

creates a new `IVar`

put :: NFData a => ivar a -> a -> m () Source

put a value into a `IVar`. Multiple `put`s to the same `IVar` are not allowed, and result in a runtime error.

`put` fully evaluates its argument, which therefore must be an instance of `NFData`. The idea is that this forces the work to happen when we expect it, rather than being passed to the consumer of the `IVar` and performed later, which often results in less parallelism than expected.

Sometimes partial strictness is more appropriate: see `put_`.

put_ :: ivar a -> a -> m () Source

like `put`, but only head-strict rather than fully-strict.

newFull :: NFData a => a -> m (ivar a) Source

creates a new `IVar` that contains a value

newFull_ :: a -> m (ivar a) Source

creates a new `IVar` that contains a value (head-strict only)

class NFData a

A class of types that can be fully evaluated.

Since: 1.1.0.0

Instances

NFData Bool |
NFData Char |
NFData Double |
NFData Float |
NFData Int |
NFData Int8 |
NFData Int16 |
NFData Int32 |
NFData Int64 |
NFData Integer |
NFData Word |
NFData Word8 |
NFData Word16 |
NFData Word32 |
NFData Word64 |
NFData TypeRep | NOTE: Only defined for `base-4.8.0.0` and later Since: 1.4.0.0
NFData () |
NFData Void | Defined as ``rnf` = `absurd``. Since: 1.4.0.0
NFData Unique | Since: 1.4.0.0
NFData Natural | Since: 1.4.0.0
NFData Version | Since: 1.3.0.0
NFData ThreadId | Since: 1.4.0.0
NFData CChar | Since: 1.4.0.0
NFData CSChar | Since: 1.4.0.0
NFData CUChar | Since: 1.4.0.0
NFData CShort | Since: 1.4.0.0
NFData CUShort | Since: 1.4.0.0
NFData CInt | Since: 1.4.0.0
NFData CUInt | Since: 1.4.0.0
NFData CLong | Since: 1.4.0.0
NFData CULong | Since: 1.4.0.0
NFData CLLong | Since: 1.4.0.0
NFData CULLong | Since: 1.4.0.0
NFData CFloat | Since: 1.4.0.0
NFData CDouble | Since: 1.4.0.0
NFData CPtrdiff | Since: 1.4.0.0
NFData CSize | Since: 1.4.0.0
NFData CWchar | Since: 1.4.0.0
NFData CSigAtomic | Since: 1.4.0.0
NFData CClock | Since: 1.4.0.0
NFData CTime | Since: 1.4.0.0
NFData CUSeconds | Since: 1.4.0.0
NFData CSUSeconds | Since: 1.4.0.0
NFData CFile | Since: 1.4.0.0
NFData CFpos | Since: 1.4.0.0
NFData CJmpBuf | Since: 1.4.0.0
NFData CIntPtr | Since: 1.4.0.0
NFData CUIntPtr | Since: 1.4.0.0
NFData CIntMax | Since: 1.4.0.0
NFData CUIntMax | Since: 1.4.0.0
NFData All | Since: 1.4.0.0
NFData Any | Since: 1.4.0.0
NFData TyCon | NOTE: Only defined for `base-4.8.0.0` and later Since: 1.4.0.0
NFData Fingerprint | Since: 1.4.0.0
NFData a => NFData [a] |
(Integral a, NFData a) => NFData (Ratio a) |
NFData (StableName a) | Since: 1.4.0.0
NFData a => NFData (Identity a) | Since: 1.4.0.0
NFData (Fixed a) | Since: 1.3.0.0
NFData a => NFData (Complex a) |
NFData a => NFData (ZipList a) | Since: 1.4.0.0
NFData a => NFData (Dual a) | Since: 1.4.0.0
NFData a => NFData (Sum a) | Since: 1.4.0.0
NFData a => NFData (Product a) | Since: 1.4.0.0
NFData a => NFData (First a) | Since: 1.4.0.0
NFData a => NFData (Last a) | Since: 1.4.0.0
NFData a => NFData (Down a) | Since: 1.4.0.0
NFData a => NFData (Maybe a) |
NFData (a -> b) | This instance is for convenience and consistency with `seq`. This assumes that WHNF is equivalent to NF for functions. Since: 1.3.0.0
(NFData a, NFData b) => NFData (Either a b) |
(NFData a, NFData b) => NFData (a, b) |
(Ix a, NFData a, NFData b) => NFData (Array a b) |
NFData a => NFData (Const a b) | Since: 1.4.0.0
NFData (Proxy * a) | Since: 1.4.0.0
(NFData a, NFData b, NFData c) => NFData (a, b, c) |
(NFData a, NFData b, NFData c, NFData d) => NFData (a, b, c, d) |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5) => NFData (a1, a2, a3, a4, a5) |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6) => NFData (a1, a2, a3, a4, a5, a6) |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7) => NFData (a1, a2, a3, a4, a5, a6, a7) |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7, NFData a8) => NFData (a1, a2, a3, a4, a5, a6, a7, a8) |
(NFData a1, NFData a2, NFData a3, NFData a4, NFData a5, NFData a6, NFData a7, NFData a8, NFData a9) => NFData (a1, a2, a3, a4, a5, a6, a7, a8, a9) |