Portability | portable |
---|---|

Stability | provisional |

Maintainer | Edward Kmett <ekmett@gmail.com> |

Safe Haskell | None |

- spec :: Eq a => a -> (a -> b) -> a -> b
- spec' :: Eq a => a -> (a -> b) -> a -> b
- specBy :: (a -> a -> Bool) -> a -> (a -> b) -> a -> b
- specBy' :: (a -> a -> Bool) -> a -> (a -> b) -> a -> b
- specOn :: Eq c => (a -> c) -> a -> (a -> b) -> a -> b
- specOn' :: Eq c => (a -> c) -> a -> (a -> b) -> a -> b
- specSTM :: Eq a => STM a -> (a -> STM b) -> a -> STM b
- specSTM' :: Eq a => STM a -> (a -> STM b) -> a -> STM b
- specOnSTM :: Eq c => (a -> STM c) -> STM a -> (a -> STM b) -> a -> STM b
- specOnSTM' :: Eq c => (a -> STM c) -> STM a -> (a -> STM b) -> a -> STM b
- specBySTM :: (a -> a -> STM Bool) -> STM a -> (a -> STM b) -> a -> STM b
- specBySTM' :: (a -> a -> STM Bool) -> STM a -> (a -> STM b) -> a -> STM b

# Speculative application

spec :: Eq a => a -> (a -> b) -> a -> bSource

evaluates `spec`

g f a`f g`

while forcing `a`

, if `g == a`

then `f g`

is returned, otherwise `f a`

is evaluated and returned. Furthermore, if the argument has already been evaluated or are not running on the threaded runtime, we skip the `f g`

computation entirely. If a good guess at the value of `a`

is available, this is one way to induce parallelism in an otherwise sequential task. However, if the guess isn't available more cheaply than the actual answer, then this saves no work and if the guess is wrong, you risk evaluating the function twice. Under high load or in a runtime with access to a single capability, since 'f g' is computed via the spark queue, the speculation will be skipped and you will obtain the same answer as 'f $! a'.

The best-case timeline looks like:

foreground: [----- a -----] foreground: [-] (check g == a) spark: [----- f g -----] overall: [--- spec g f a ---]

The worst-case timeline looks like:

foreground: [----- a -----] foreground: [-] (check g == a) foreground: [---- f a ----] spark: [----- f g -----] overall: [-------- spec g f a ---------]

Note that, if `f g`

takes longer than a to compute, in the HEAD release of GHC, `f g`

will be collected and killed during garbage collection.

foreground: [----- a -----] foreground: [-] (check g == a) foreground: [---- f a ----] spark: [---- f g ----###### (#'s mark when this spark is collectable) overall: [--------- spec g f a --------]

Under high load:

foreground: [----- a -----] foreground: [-] (check g == a) foreground: [---- f a ----] overall: [-------- spec g f a ---------]

Compare these to the timeline of `f $! a`

:

foreground: [----- a -----] foreground: [---- f a ----] orverall: [---------- f $! a ---------]

spec' :: Eq a => a -> (a -> b) -> a -> bSource

Unlike `spec`

, this version does not check to see if the argument has already been evaluated. This can save
a small amount of work when you know the argument will always require computation.

specBy :: (a -> a -> Bool) -> a -> (a -> b) -> a -> bSource

`spec`

with a user defined comparison function

specBy' :: (a -> a -> Bool) -> a -> (a -> b) -> a -> bSource

`spec'`

with a user defined comparison function

specOn :: Eq c => (a -> c) -> a -> (a -> b) -> a -> bSource

`spec`

comparing by projection onto another type

specOn' :: Eq c => (a -> c) -> a -> (a -> b) -> a -> bSource

`spec'`

comparing by projection onto another type

# Speculative application with transactional rollback

specSTM :: Eq a => STM a -> (a -> STM b) -> a -> STM bSource

evaluates `specSTM`

g f a`fg = do g' <- g; f g'`

, while forcing `a`

, then if `g' == a`

then `fg`

is returned. Otherwise the side-effects of `fg`

are rolled back and `f a`

is evaluated. `g`

is allowed to be a monadic action, so that we can kickstart the computation of `a`

earlier. Under high load, or when we are not using the parallel runtime, the speculation is avoided, to enable this to more closely approximate the runtime profile of spec.

If the argument `a`

is already evaluated, we don't bother to perform `f g`

at all.

If a good guess at the value of `a`

is available, this is one way to induce parallelism in an otherwise sequential task.

However, if the guess isn't available more cheaply than the actual answer then this saves no work, and if the guess is wrong, you risk evaluating the function twice.

The best-case timeline looks like:

foreground: [--- g >>= f ---] spark: [------- a -------] foreground: [-] (compare g' == a) overall: [---- specSTM g f a ----]

The worst-case timeline looks like:

foreground: [---- g >>= f ----] spark: [------- a -------] foreground: [-] (check if g' == a) foreground: [--] (rollback) foreground: [------ f a ------] overall: [------------ specSTM g f a ----------------]

Under high load, `specSTM`

degrades less gracefully than `spec`

:

foreground: [---- g >>= f ----] spark: [------- a -------] foreground: [-] (check if g' == a) foreground: [--] (rollback) foreground: [------ f a ------] overall: [--------------------specSTM g f a ------------------------]

Compare these to the timeline of `f $! a`

:

foreground: [------- a -------] foreground: [------ f a ------]

specOnSTM' :: Eq c => (a -> STM c) -> STM a -> (a -> STM b) -> a -> STM bSource

`specBySTM'`

.`on`

(==)