packages feed

speculation 1.1.0.0 → 1.2.0.0

raw patch · 16 files changed

+1164/−1162 lines, 16 files

Files

− Control/Concurrent/Speculation.hs
@@ -1,195 +0,0 @@-{-# LANGUAGE CPP, BangPatterns, DeriveDataTypeable, MagicHash #-}-module Control.Concurrent.Speculation-    (-    -- * Speculative application-      spec-    , spec'-    , specBy-    , specBy'-    , specOn-    , specOn'-    -- * Speculative application with transactional rollback-    , specSTM-    , specSTM'-    , specOnSTM-    , specOnSTM'-    , specBySTM-    , specBySTM'-    ) where--import Control.Concurrent.STM-import Control.Concurrent.Speculation.Internal (returning)-import Data.TagBits (unsafeIsEvaluated)-import Control.Monad (liftM2, unless)-import Data.Function (on)-import GHC.Conc---- * Basic speculation---- | @'spec' g f a@ evaluates @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 -> b-spec = specBy (==)-{-# INLINE spec #-}---- | 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.--spec' :: Eq a => a -> (a -> b) -> a -> b-spec' = specBy' (==)-{-# INLINE spec' #-}---- | 'spec' with a user defined comparison function-specBy :: (a -> a -> Bool) -> a -> (a -> b) -> a -> b-specBy cmp guess f a-    | unsafeIsEvaluated a = f a-    | otherwise = specBy' cmp guess f a-{-# INLINE specBy #-}---- | 'spec'' with a user defined comparison function-specBy' :: (a -> a -> Bool) -> a -> (a -> b) -> a -> b-specBy' cmp guess f a-  | numCapabilities == 1 = f $! a-  | otherwise = speculation `par` -    if cmp guess a-    then speculation-    else f a-  where speculation = f guess-{-# INLINE specBy' #-}---- | 'spec' comparing by projection onto another type-specOn :: Eq c => (a -> c) -> a -> (a -> b) -> a -> b-specOn = specBy . on (==)-{-# INLINE specOn #-}---- | 'spec'' comparing by projection onto another type-specOn' :: Eq c => (a -> c) -> a -> (a -> b) -> a -> b-specOn' = specBy' . on (==)-{-# INLINE specOn' #-}---- * STM-based speculation---- | @'specSTM' g f a@ evaluates @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 ------]-----specSTM :: Eq a => STM a -> (a -> STM b) -> a -> STM b-specSTM = specBySTM (returning (==))-{-# INLINE specSTM #-}---- | Unlike 'specSTM', 'specSTM'' doesn't check if the argument has already been evaluated.--specSTM' :: Eq a => STM a -> (a -> STM b) -> a -> STM b-specSTM' = specBySTM' (returning (==))-{-# INLINE specSTM' #-}---- | 'specSTM' using a user defined comparison function-specBySTM :: (a -> a -> STM Bool) -> STM a -> (a -> STM b) -> a -> STM b-specBySTM cmp guess f a-    | unsafeIsEvaluated a = f a-    | otherwise   = specBySTM' cmp guess f a-{-# INLINE specBySTM #-}--#ifndef HAS_NUM_SPARKS-numSparks :: IO Int-numSparks = return 0-#endif---- | 'specSTM'' using a user defined comparison function-specBySTM' :: (a -> a -> STM Bool) -> STM a -> (a -> STM b) -> a -> STM b-specBySTM' cmp mguess f a = do-  sparks <- unsafeIOToSTM numSparks-  if sparks < numCapabilities -    then a `par` do-      guess <- mguess-      result <- f guess-      -- rendezvous with a-      matching <- cmp guess a-      unless matching retry-      return result-     `orElse`-      f a-    else f $! a -{-# INLINE specBySTM' #-}---- | @'specBySTM' . 'on' (==)@-specOnSTM :: Eq c => (a -> STM c) -> STM a -> (a -> STM b) -> a -> STM b-specOnSTM = specBySTM . on (liftM2 (==))-{-# INLINE specOnSTM #-}---- | @'specBySTM'' . 'on' (==)@-specOnSTM' :: Eq c => (a -> STM c) -> STM a -> (a -> STM b) -> a -> STM b-specOnSTM' = specBySTM' . on (liftM2 (==))-{-# INLINE specOnSTM' #-}
− Control/Concurrent/Speculation/Internal.hs
@@ -1,53 +0,0 @@-module Control.Concurrent.Speculation.Internal -    ( Acc(..)-    , extractAcc-    , MaybeAcc(..)-    , fromMaybeAcc-    , errorEmptyStructure-    , returning-    ) where--import Data.Foldable-import Data.Traversable-import Control.Applicative---- comonad!-data Acc a = Acc {-# UNPACK #-} !Int a--instance Functor Acc where-    fmap f (Acc n a) = Acc n (f a)--instance Foldable Acc where-    foldMap = foldMapDefault--instance Traversable Acc where-    traverse f (Acc n a) = Acc n <$> f a--extractAcc :: Acc a -> a-extractAcc (Acc _ a) = a-{-# INLINE extractAcc #-}--data MaybeAcc a = JustAcc {-# UNPACK #-} !Int a | NothingAcc--instance Functor MaybeAcc where-    fmap f (JustAcc n a) = JustAcc n (f a)-    fmap _ NothingAcc = NothingAcc--instance Foldable MaybeAcc where-    foldMap = foldMapDefault--instance Traversable MaybeAcc where-    traverse f (JustAcc n a) = JustAcc n <$> f a-    traverse _ NothingAcc    = pure NothingAcc--fromMaybeAcc :: a -> MaybeAcc a -> a-fromMaybeAcc _ (JustAcc _ a) = a-fromMaybeAcc a _ = a-{-# INLINE fromMaybeAcc #-}--errorEmptyStructure :: String -> a-errorEmptyStructure f = error $ f ++ ": error empty structure"--returning :: Monad m => (a -> b -> c) -> a -> b -> m c-returning f a b = return (f a b)-{-# INLINE returning #-}
− Control/Monad/Trans/Cont/Speculation.hs
@@ -1,70 +0,0 @@--------------------------------------------------------------------------------- |--- Module      :  Control.Monad.Trans.Cont.Speculation--- Copyright   :  (C) 2011 Edward Kmett, Jake McArthur--- License     :  BSD-style (see the file LICENSE)------ Maintainer  :  Edward Kmett <ekmett@gmail.com>--- Stability   :  provisional--- Portability :  portable------ Versions of the combinators from the 'speculation' package--- with the signature rearranged to enable them to be used--- directly as actions in the 'Cont' and 'ContT' monads.------------------------------------------------------------------------------module Control.Monad.Trans.Cont.Speculation where--import Control.Monad.Trans.Cont-import qualified Control.Concurrent.Speculation as Prim-import Control.Concurrent.STM---- * Basic speculation---- | When a is unevaluated, @'spec' g a@ evaluates the current continuation --- with @g@ while testing if @g@ '==' @a@, if they differ, it re-evalutes the--- continuation with @a@. If @a@ was already evaluated, the continuation is--- just directly applied to @a@ instead.-spec :: Eq a => a -> a -> ContT r m a-spec g a = ContT $ \k -> Prim.spec g k a ---- | As per 'spec', without the check for whether or not the second argument--- is already evaluated.-spec' :: Eq a => a -> a -> ContT r m a-spec' g a = ContT $ \k -> Prim.spec' g k a---- | @spec@ with a user supplied comparison function-specBy :: (a -> a -> Bool) -> a -> a -> ContT r m a-specBy f g a = ContT $ \k -> Prim.specBy f g k a---- | @spec'@ with a user supplied comparison function-specBy' :: (a -> a -> Bool) -> a -> a -> ContT r m a-specBy' f g a = ContT $ \k -> Prim.specBy' f g k a---- | @spec'@ with a user supplied comparison function-specOn :: Eq c => (a -> c) -> a -> a -> ContT r m a-specOn f g a = ContT $ \k -> Prim.specOn f g k a---- | @spec'@ with a user supplied comparison function-specOn' :: Eq c => (a -> c) -> a -> a -> ContT r m a-specOn' f g a = ContT $ \k -> Prim.specOn' f g k a---- * STM-based speculation--specSTM :: Eq a => STM a -> a -> ContT r STM a-specSTM g a = ContT $ \k -> Prim.specSTM g k a --specSTM' :: Eq a => STM a -> a -> ContT r STM a-specSTM' g a = ContT $ \k -> Prim.specSTM' g k a --specOnSTM :: Eq c => (a -> STM c) -> STM a -> a -> ContT r STM a-specOnSTM f g a = ContT $ \k -> Prim.specOnSTM f g k a --specOnSTM' :: Eq c => (a -> STM c) -> STM a -> a -> ContT r STM a-specOnSTM' f g a = ContT $ \k -> Prim.specOnSTM' f g k a --specBySTM :: (a -> a -> STM Bool) -> STM a -> a -> ContT r STM a-specBySTM f g a = ContT $ \k -> Prim.specBySTM f g k a --specBySTM' :: (a -> a -> STM Bool) -> STM a -> a -> ContT r STM a-specBySTM' f g a = ContT $ \k -> Prim.specBySTM' f g k a -
− Control/Morphism/Speculation.hs
@@ -1,31 +0,0 @@-{-# LANGUAGE BangPatterns, MagicHash #-}-module Control.Morphism.Speculation-    ( hylo-    ) where--import GHC.Prim-import GHC.Types--import Control.Concurrent.Speculation--{--newtype Mu f = In { out :: f (Mu f) } --ana :: (Functor f, Eq a) => (Int -> a) -> (a -> f a) -> a -> Mu f-ana g psi = go 0# -  where-    go n = In . fmap (go (n +# 1#)) . spec (g (I# n)) psi--apo :: (Functor f, Eq a) => (Int -> a) -> (a -> f (Either (Mu f) a)) -> a -> Mu f-apo g psi = go 0#-    where-        go n = In . fmap (either id (go (n +# 1#))) . spec (g (I# n)) psi --}-        --- | @'hylo' g phi psi@ is a hylomorphism using a speculative anamorphism, where--- @g n@ estimates the seed after n iterations of 'psi'.--hylo :: (Functor f, Eq a) => (Int -> a) -> (f b -> b) -> (a -> f a) -> a -> b-hylo g phi psi = go 0#-    where-        go n = phi . fmap (go (n +# 1#)) . spec (g (I# n)) psi
− Data/Foldable/Speculation.hs
@@ -1,416 +0,0 @@-{-# LANGUAGE BangPatterns #-}-module Data.Foldable.Speculation-    ( -    -- * Speculative folds-      fold, foldBy-    , foldMap, foldMapBy-    , foldr, foldrBy-    , foldl, foldlBy-    , foldr1, foldr1By-    , foldl1, foldl1By-    -- ** Speculative monadic folds-    , foldrM, foldrByM-    , foldlM, foldlByM-    -- * Speculative transactional monadic folds-    , foldrSTM, foldrBySTM-    , foldlSTM, foldlBySTM-    -- * Folding actions-    -- ** Applicative actions-    , traverse_, traverseBy_-    , for_, forBy_-    , sequenceA_, sequenceByA_-    , asum, asumBy-    -- ** Monadic actions-    , mapM_, mapByM_-    , forM_, forByM_-    , sequence_, sequenceBy_-    , msum, msumBy-    -- ** Speculative transactional monadic actions-    , mapSTM_, forSTM_, sequenceSTM_-    -- * Specialized folds-    , toList, toListBy-    , concat, concatBy-    , concatMap, concatMapBy-    , all, any, and, or-    , sum, sumBy-    , product, productBy-    , maximum, maximumBy-    , minimum, minimumBy-    -- * Searches-    , elem, elemBy-    , notElem, notElemBy-    , find, findBy-    ) where--import Prelude hiding -    (foldl, foldl1, foldr, foldr1-    , any, all, and, or, mapM_, sequence_-    , elem, notElem, sum, product-    , minimum, maximum, concat, concatMap-    )--import Data.Monoid-import Data.Ix ()-import Data.Function (on)-import Data.Foldable (Foldable)-import qualified Data.Foldable as Foldable-import Control.Concurrent.STM-import Control.Concurrent.Speculation-import Control.Concurrent.Speculation.Internal-import Control.Applicative-import Control.Monad hiding (mapM_, msum, forM_, sequence_)---- | Given a valid estimator @g@, @'fold' g f xs@ yields the same answer as @'fold' f xs@.--- --- @g n@ should supply an estimate of the value of the monoidal summation over the last @n@ elements of the container.--- --- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the fold, then this can--- provide increased opportunities for parallelism.--fold :: (Foldable f, Monoid m, Eq m) => (Int -> m) -> f m -> m-fold = foldBy (==)-{-# INLINE fold #-}---- | 'fold' using 'specBy'-foldBy :: (Foldable f, Monoid m) => (m -> m -> Bool) -> (Int -> m) -> f m -> m-foldBy cmp g = foldrBy cmp g mappend mempty-{-# INLINE foldBy #-}---- | Given a valid estimator @g@, @'foldMap' g f xs@ yields the same answer as @'foldMap' f xs@.--- --- @g n@ should supply an estimate of the value of the monoidal summation over the last @n@ elements of the container.--- --- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the fold, then this can--- provide increased opportunities for parallelism.--foldMap :: (Foldable f, Monoid m, Eq m) => (Int -> m) -> (a -> m) -> f a -> m-foldMap = foldMapBy (==)-{-# INLINE foldMap #-}---- | 'foldMap' using 'specBy'-foldMapBy :: (Foldable f, Monoid m) => (m -> m -> Bool) -> (Int -> m) -> (a -> m) -> f a -> m-foldMapBy cmp g f = foldrBy cmp g (mappend . f) mempty-{-# INLINE foldMapBy #-}---foldr :: (Foldable f, Eq b) => (Int -> b) -> (a -> b -> b) -> b -> f a -> b-foldr = foldrBy (==)-{-# INLINE foldr #-}---- | Given a valid estimator @g@, @'foldr' g f z xs@ yields the same answer as @'foldr'' f z xs@.------ @g n@ should supply an estimate of the value returned from folding over the last @n@ elements of the container.------ If @g n@ is accurate a reasonable percentage of the time and faster to compute than the fold, then this can--- provide increased opportunities for parallelism.-foldrBy :: Foldable f => (b -> b -> Bool) -> (Int -> b) -> (a -> b -> b) -> b -> f a -> b-foldrBy cmp g f z = extractAcc . Foldable.foldr mf (Acc 0 z)-  where -    mf a (Acc n b) = Acc (n + 1) (specBy' cmp (g n) (f a) b)-{-# INLINE foldrBy #-}---{---- Variations:--- These variations are not used because the values ot the left shouldn't affect the intermediate state of a right fold.------ this version receiveds both the number of values remaining and the number so far--foldrBy :: Foldable f => (b -> b -> Bool) -> (Int -> Int -> b) -> (a -> b -> b) -> b -> f a -> b-foldrBy cmp g f z xs = Foldable.foldr mf (Acc 0 (const z)) xs 0-  where -    mf a (Acc r b) !l = let l' = l + 1 in Acc (r + 1) (specBy' cmp (g l') (f a) (b l'))-{-# INLINE foldrBy #-}---- this estimator receives the number of values to the left of the summation. -foldrBy :: Foldable f => (b -> b -> Bool) -> (Int -> b) -> (a -> b -> b) -> b -> f a -> b-foldrBy cmp g f z xs = Foldable.foldr mf (const z) xs 0-  where -    mf a b !i = let i' = i + 1 in specBy' cmp (g i') (f a) (b i')-{-# INLINE foldrBy #-}--}--foldlM :: (Foldable f, Monad m, Eq (m b)) => (Int -> m b) -> (b -> a -> m b) -> m b -> f a -> m b-foldlM = foldlByM (==)-{-# INLINE foldlM #-}--foldlByM ::  (Foldable f, Monad m) => (m b -> m b -> Bool) -> (Int -> m b) -> (b -> a -> m b) -> m b -> f a -> m b-foldlByM  cmp g f mz = liftM extractAcc . Foldable.foldl go (liftM (Acc 0) mz) -  where-    go mia b = do-      Acc n a <- mia-      a' <- specBy' cmp (g n) (>>= (`f` b)) (return a)-      return (Acc (n + 1) a')-{-# INLINE foldlByM #-}--foldrM :: (Foldable f, Monad m, Eq (m b)) => (Int -> m b) -> (a -> b -> m b) -> m b -> f a -> m b-foldrM = foldrByM (==)-{-# INLINE foldrM #-}--foldrByM :: (Foldable f, Monad m) => (m b -> m b -> Bool) -> (Int -> m b) -> (a -> b -> m b) -> m b -> f a -> m b-foldrByM cmp g f mz = liftM extractAcc . Foldable.foldr go (liftM (Acc 0) mz) -  where-    go a mib = do-      Acc n b <- mib-      b' <- specBy' cmp (g n) (>>= f a) (return b)-      return (Acc (n + 1) b')-{-# INLINE foldrByM #-}--foldlSTM :: (Foldable f, Eq a) => (Int -> STM a) -> (a -> b -> STM a) -> STM a -> f b -> STM a-foldlSTM = foldlBySTM (returning (==))-{-# INLINE foldlSTM #-}--foldlBySTM :: Foldable f => (a -> a -> STM Bool) -> (Int -> STM a) -> (a -> b -> STM a) -> STM a -> f b -> STM a-foldlBySTM cmp g f mz = liftM extractAcc . Foldable.foldl go (liftM (Acc 0) mz)-  where-    go mia b = do-      Acc n a <- mia-      a' <- specBySTM' cmp (g n) (`f` b) a-      return (Acc (n + 1) a')-{-# INLINE foldlBySTM #-}--foldrSTM :: (Foldable f, Eq b) => (Int -> STM b) -> (a -> b -> STM b) -> STM b -> f a -> STM b-foldrSTM = foldrBySTM (returning (==))-{-# INLINE foldrSTM #-}--foldrBySTM :: Foldable f => (b -> b -> STM Bool) -> (Int -> STM b) -> (a -> b -> STM b) -> STM b -> f a -> STM b-foldrBySTM cmp g f mz = liftM extractAcc . Foldable.foldr go (liftM (Acc 0) mz)-  where-    go a mib = do-      Acc n b <- mib-      b' <- specBySTM' cmp (g n) (f a) b-      return (Acc (n + 1) b')-{-# INLINE foldrBySTM #-}---- | Given a valid estimator @g@, @'foldl' g f z xs@ yields the same answer as @'foldl'' f z xs@.------ @g n@ should supply an estimate of the value returned from folding over the first @n@ elements of the container.------ If @g n@ is accurate a reasonable percentage of the time and faster to compute than the fold, then this can--- provide increased opportunities for parallelism.--foldl  :: (Foldable f, Eq b) => (Int -> b) -> (b -> a -> b) -> b -> f a -> b-foldl = foldlBy (==) -{-# INLINE foldl #-}--foldlBy  :: Foldable f => (b -> b -> Bool) -> (Int -> b) -> (b -> a -> b) -> b -> f a -> b-foldlBy cmp g f z = extractAcc . Foldable.foldl mf (Acc 0 z)-  where-    mf (Acc n a) b = Acc (n + 1) (specBy' cmp (g n) (`f` b) a)-{-# INLINE foldlBy #-}--foldr1 :: (Foldable f, Eq a) => (Int -> a) -> (a -> a -> a) -> f a -> a-foldr1 = foldr1By (==) -{-# INLINE foldr1 #-}--foldr1By :: Foldable f => (a -> a -> Bool) -> (Int -> a) -> (a -> a -> a) -> f a -> a-foldr1By cmp g f xs = fromMaybeAcc (errorEmptyStructure "foldr1")-                                   (Foldable.foldr mf NothingAcc xs)-  where-    mf a (JustAcc n b) = JustAcc (n + 1) (specBy' cmp (g n) (f a) b)-    mf a NothingAcc = JustAcc 1 a-{-# INLINE foldr1By #-}--foldl1 :: (Foldable f, Eq a) => (Int -> a) -> (a -> a -> a) -> f a -> a-foldl1 = foldl1By (==)-{-# INLINE foldl1 #-}--foldl1By :: Foldable f => (a -> a -> Bool) -> (Int -> a) -> (a -> a -> a) -> f a -> a-foldl1By cmp g f xs = fromMaybeAcc (errorEmptyStructure "foldl1")-                               (Foldable.foldl mf NothingAcc xs)-  where-    mf (JustAcc n a) b = JustAcc (n + 1) (specBy' cmp (g n) (`f` b) a)-    mf NothingAcc b    = JustAcc 1 b-{-# INLINE foldl1By #-}---- | Map each element of a structure to an action, evaluate these actions--- from left to right and ignore the results.-traverse_ :: (Foldable t, Applicative f, Eq (f ())) => (Int -> f c) -> (a -> f b) -> t a -> f ()-traverse_ = traverseBy_ (==)-{-# INLINE traverse_ #-}--traverseBy_ :: (Foldable t, Applicative f) => (f () -> f () -> Bool) -> (Int -> f c) -> (a -> f b) -> t a -> f ()-traverseBy_ cmp g f = foldrBy cmp ((() <$) . g) ((*>) . f) (pure ())-{-# INLINE traverseBy_ #-}---- | 'for_' is 'traverse_' with its arguments flipped.-for_ :: (Foldable t, Applicative f, Eq (f ())) => (Int -> f c) -> t a -> (a -> f b) -> f ()-for_ g = flip (traverse_ g)-{-# INLINE for_ #-}--forBy_ :: (Foldable t, Applicative f) => (f () -> f () -> Bool) -> (Int -> f c) -> t a -> (a -> f b) -> f ()-forBy_ cmp g = flip (traverseBy_ cmp g)-{-# INLINE forBy_ #-}---- | Map each element of the structure to a monadic action, evaluating these actions--- from left to right and ignoring the results.-mapM_ :: (Foldable t, Monad m, Eq (m ())) => (Int -> m c) -> (a -> m b) -> t a -> m ()-mapM_ = mapByM_ (==)-{-# INLINE mapM_ #-}---- | Map each element of the structure to a monadic action, evaluating these actions--- from left to right and ignoring the results, while transactional side-effects from --- mis-speculated actions are rolled back.-mapSTM_ :: Foldable t => STM Bool -> (Int -> STM c) -> (a -> STM b) -> t a -> STM ()-mapSTM_ chk g f = foldrBySTM (\_ _ -> chk) (\n -> () <$ g n) (\a _ -> () <$ f a) (return ())-{-# INLINE mapSTM_ #-}--mapByM_ :: (Foldable t, Monad m) => (m () -> m () -> Bool) -> (Int -> m c) -> (a -> m b) -> t a -> m ()-mapByM_ cmp g f = foldrBy cmp (\n -> g n >> return ()) ((>>) . f) (return ())-{-# INLINE mapByM_ #-}---- | 'for_' is 'mapM_' with its arguments flipped.-forM_ :: (Foldable t, Monad m, Eq (m ())) => (Int -> m c) -> t a -> (a -> m b) -> m ()-forM_ g = flip (mapM_ g)-{-# INLINE forM_ #-}---- | 'for_' is 'mapM_' with its arguments flipped.-forSTM_ :: Foldable t => STM Bool -> (Int -> STM c) -> t a -> (a -> STM b) -> STM ()-forSTM_ chk g = flip (mapSTM_ chk g)-{-# INLINE forSTM_ #-}--forByM_ :: (Foldable t, Monad m) => (m () -> m () -> Bool) -> (Int -> m c) -> t a -> (a -> m b) -> m ()-forByM_ cmp g = flip (mapByM_ cmp g)-{-# INLINE forByM_ #-}--sequenceA_ :: (Foldable t, Applicative f, Eq (f ())) => (Int -> f b) -> t (f a) -> f ()-sequenceA_ = sequenceByA_ (==)-{-# INLINE sequenceA_ #-}--sequenceByA_ :: (Foldable t, Applicative f, Eq (f ())) => (f () -> f () -> Bool) -> (Int -> f b) -> t (f a) -> f ()-sequenceByA_ cmp g = foldrBy cmp ((()<$) . g) (*>) (pure ())-{-# INLINE sequenceByA_ #-}--sequence_ :: (Foldable t, Monad m, Eq (m ())) => (Int -> m b) -> t (m a) -> m ()-sequence_ = sequenceBy_ (==) -{-# INLINE sequence_ #-}--sequenceSTM_:: Foldable t => STM Bool -> (Int -> STM a) -> t (STM b) -> STM ()-sequenceSTM_ chk g = foldrBySTM (\_ _ -> chk) (\n -> () <$ g n) (\a _ -> () <$ a) (return ())-{-# INLINE sequenceSTM_ #-}--sequenceBy_ :: (Foldable t, Monad m) => (m () -> m () -> Bool) -> (Int -> m b) -> t (m a) -> m ()-sequenceBy_ cmp g = foldrBy cmp (\n -> g n >> return ()) (>>) (return ())-{-# INLINE sequenceBy_ #-}--asum :: (Foldable t, Alternative f, Eq (f a)) => (Int -> f a) -> t (f a) -> f a-asum = asumBy (==)-{-# INLINE asum #-}--asumBy :: (Foldable t, Alternative f) => (f a -> f a -> Bool) -> (Int -> f a) -> t (f a) -> f a-asumBy cmp g = foldrBy cmp g (<|>) empty-{-# INLINE asumBy #-}--msum  :: (Foldable t, MonadPlus m, Eq (m a)) => (Int -> m a) -> t (m a) -> m a-msum = msumBy (==) -{-# INLINE msum #-}--msumBy  :: (Foldable t, MonadPlus m) => (m a -> m a -> Bool) -> (Int -> m a) -> t (m a) -> m a-msumBy cmp g = foldrBy cmp g mplus mzero -{-# INLINE msumBy #-}--toList :: (Foldable t, Eq a) => (Int -> [a]) -> t a -> [a]-toList = toListBy (==)-{-# INLINE toList #-}--toListBy :: Foldable t => ([a] -> [a] -> Bool) -> (Int -> [a]) -> t a -> [a]-toListBy cmp g = foldrBy cmp g (:) []-{-# INLINE toListBy #-}--concat :: (Foldable t, Eq a) => (Int -> [a]) -> t [a] -> [a]-concat = fold-{-# INLINE concat #-}--concatBy :: Foldable t => ([a] -> [a] -> Bool) -> (Int -> [a]) -> t [a] -> [a]-concatBy = foldBy-{-# INLINE concatBy #-}--concatMap :: (Foldable t, Eq b) => (Int -> [b]) -> (a -> [b]) -> t a -> [b]-concatMap = foldMap-{-# INLINE concatMap #-}--concatMapBy :: (Foldable t) => ([b] -> [b] -> Bool) -> (Int -> [b]) -> (a -> [b]) -> t a -> [b]-concatMapBy = foldMapBy-{-# INLINE concatMapBy #-}--and :: Foldable t => (Int -> Bool) -> t Bool -> Bool-and g = getAll . foldMap (All . g) All-{-# INLINE and #-}--or :: Foldable t => (Int -> Bool) -> t Bool -> Bool-or g = getAny . foldMap (Any . g) Any-{-# INLINE or #-}--all :: Foldable t => (Int -> Bool) -> (a -> Bool) -> t a -> Bool-all g p = getAll . foldMap (All . g) (All . p)-{-# INLINE all #-}--any :: Foldable t => (Int -> Bool) -> (a -> Bool) -> t a -> Bool-any g p = getAny . foldMap (Any . g) (Any . p)-{-# INLINE any #-}--sum :: (Foldable t, Num a) => (Int -> a) -> t a -> a-sum = sumBy (==)-{-# INLINE sum #-}--sumBy :: (Foldable t, Num a) => (a -> a -> Bool) -> (Int -> a) -> t a -> a-sumBy cmp g = getSum . foldMapBy (on cmp getSum) (Sum . g) Sum-{-# INLINE sumBy #-}--product :: (Foldable t, Num a) => (Int -> a) -> t a -> a-product = productBy (==)-{-# INLINE product #-}--productBy :: (Foldable t, Num a) => (a -> a -> Bool) -> (Int -> a) -> t a -> a-productBy cmp g = getProduct . foldMapBy (on cmp getProduct) (Product . g) Product-{-# INLINE productBy #-}--maximum :: (Foldable t, Ord a) => (Int -> a) -> t a -> a-maximum g = foldr1 g max-{-# INLINE maximum #-}---- TODO: allow for patching?-maximumBy :: Foldable t => (a -> a -> Ordering) -> (Int -> a) -> t a -> a-maximumBy cmp g = foldr1By cmp' g max'-  where -    max' x y = case cmp x y of -        GT -> x -        _  -> y-    cmp' x y = cmp x y == EQ-{-# INLINE maximumBy #-}-        -minimum :: (Foldable t, Ord a) => (Int -> a) -> t a -> a-minimum g = foldr1 g min-{-# INLINE minimum #-}--minimumBy :: Foldable t => (a -> a -> Ordering) -> (Int -> a) -> t a -> a-minimumBy cmp g = foldr1By cmp' g min'-  where -    min' x y = case cmp x y of -        GT -> x -        _  -> y-    cmp' x y = cmp x y == EQ-{-# INLINE minimumBy #-}-        -elem :: (Foldable t, Eq a) => (Int -> Bool) -> a -> t a -> Bool-elem g = any g . (==)-{-# INLINE elem #-}--elemBy :: Foldable t => (a -> a -> Bool) -> (Int -> Bool) -> a -> t a -> Bool-elemBy cmp g = any g . cmp-{-# INLINE elemBy #-}--notElem :: (Foldable t, Eq a) => (Int -> Bool) -> a -> t a -> Bool-notElem g a = not . elem g a-{-# INLINE notElem #-}--notElemBy :: Foldable t => (a -> a -> Bool) -> (Int -> Bool) -> a -> t a -> Bool-notElemBy cmp g a = not . elemBy cmp g a-{-# INLINE notElemBy #-}--find :: (Foldable t, Eq a) => (Int -> Maybe a) -> (a -> Bool) -> t a -> Maybe a-find = findBy (==) --findBy :: Foldable t => (Maybe a -> Maybe a -> Bool) -> (Int -> Maybe a) -> (a -> Bool) -> t a -> Maybe a -findBy cmp g p = getFirst . foldMapBy (on cmp getFirst) (First . g) (\x -> if p x then First (Just x) else First (Nothing))-
− Data/List/Speculation.hs
@@ -1,181 +0,0 @@-{-# LANGUAGE BangPatterns #-}-module Data.List.Speculation -    ( -    -- * Speculative scans-      scan, scanBy-    , scanMap, scanMapBy-    , scanr, scanrBy-    , scanl, scanlBy-    , scanr1, scanr1By-    , scanl1, scanl1By-    {--    -- ** Speculative monadic scans-    , scanrM, scanrByM-    , scanlM, scanlByM-    -- * Speculative transactional monadic scans-    , scanrSTM, scanrBySTM-    , scanlSTM, scanlBySTM-    -}-    ) where---import Prelude hiding -    (foldl, foldl1, foldr, foldr1-    , any, all, and, or, mapM_, sequence_-    , elem, notElem, sum, product-    , minimum, maximum, concat, concatMap-    , scanr, scanl, scanr1, scanl1-    )--import Data.Monoid-import qualified Data.List as List-import Control.Concurrent.Speculation-import Control.Concurrent.Speculation.Internal---- | Given a valid estimator @g@, @'scan' g xs@ converts @xs@ into a list of the prefix sums.--- --- @g n@ should supply an estimate of the value of the monoidal summation over the first @n@ elements of the container.--- --- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the prefix sum, then this can--- provide increased opportunities for parallelism.--scan :: (Monoid m, Eq m) => (Int -> m) -> [m] -> [m]-scan = scanBy (==)-{-# INLINE scan #-}---- | 'scan' using 'specBy'-scanBy :: Monoid m => (m -> m -> Bool) -> (Int -> m) -> [m] -> [m]-scanBy cmp g = scanrBy cmp g mappend mempty-{-# INLINE scanBy #-}---- | Given a valid estimator @g@, @'scanMap' g f xs@ converts @xs@ into a list of the prefix sums.--- --- @g n@ should supply an estimate of the value of the monoidal summation over the first @n@ elements of the container.--- --- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the scan, then this can--- provide increased opportunities for parallelism.------ > scan = scanMap id--- > scanMap = scanMapBy (==)--scanMap :: (Monoid m, Eq m) => (Int -> m) -> (a -> m) -> [a] -> [m]-scanMap = scanMapBy (==)-{-# INLINE scanMap #-}--scanMapBy :: Monoid m => (m -> m -> Bool) -> (Int -> m) -> (a -> m) -> [a] -> [m]-scanMapBy cmp g f = scanrBy cmp g (mappend . f) mempty-{-# INLINE scanMapBy #-}---- | Given a valid estimator @g@, @'scanr' g f z xs@ yields the same answer as @'scanr'' f z xs@.------ @g n@ should supply an estimate of the value returned from scanning over the last @n@ elements of the container.------ If @g n@ is accurate a reasonable percentage of the time and faster to compute than the scan, then this can--- provide increased opportunities for parallelism.--scanr :: Eq b => (Int -> b) -> (a -> b -> b) -> b -> [a] -> [b]-scanr = scanrBy (==)-{-# INLINE scanr #-}--scanrBy :: (b -> b -> Bool) -> (Int -> b) -> (a -> b -> b) -> b -> [a] -> [b]-scanrBy cmp g f z = map extractAcc . List.scanr mf (Acc 0 z)-  where -    mf a (Acc n b) = let n' = n + 1 in Acc n' (specBy' cmp (g n') (f a) b)-{-# INLINE scanrBy #-}---scanl  :: Eq b => (Int -> b) -> (b -> a -> b) -> b -> [a] -> [b]-scanl = scanlBy (==) -{-# INLINE scanl #-}--scanlBy  :: (b -> b -> Bool) -> (Int -> b) -> (b -> a -> b) -> b -> [a] -> [b]-scanlBy cmp g f z = map extractAcc . List.scanl mf (Acc 0 z)-  where-    mf (Acc n a) b = let n' = n + 1 in Acc n' (specBy' cmp (g n') (`f` b) a)-{-# INLINE scanlBy #-}--scanr1 :: Eq a => (Int -> a) -> (a -> a -> a) -> [a] -> [a]-scanr1 = scanr1By (==) -{-# INLINE scanr1 #-}--scanr1By :: (a -> a -> Bool) -> (Int -> a) -> (a -> a -> a) -> [a] -> [a]-scanr1By cmp g f xs = map (fromMaybeAcc undefined) $ List.scanr mf NothingAcc xs-  where-    mf a (JustAcc n b) = let n' = n + 1 in JustAcc n' (specBy' cmp (g n') (f a) b)-    mf a NothingAcc = JustAcc 1 a-{-# INLINE scanr1By #-}--scanl1 :: Eq a => (Int -> a) -> (a -> a -> a) -> [a] -> [a]-scanl1 = scanl1By (==)-{-# INLINE scanl1 #-}--scanl1By :: (a -> a -> Bool) -> (Int -> a) -> (a -> a -> a) -> [a] -> [a]-scanl1By cmp g f xs = map (fromMaybeAcc undefined) $ List.scanl mf NothingAcc xs-  where-    mf (JustAcc n a) b = let n' = n + 1 in JustAcc n' (specBy' cmp (g n') (`f` b) a)-    mf NothingAcc b    = JustAcc 1 b-{-# INLINE scanl1By #-}--{--scanlM :: (Monad m, Eq b) => (Int -> b) -> (b -> a -> m b) -> b -> [a] -> m [b]-scanlM = scanlByM (==)-{-# INLINE scanlM #-}--scanlByM ::  Monad m => (b -> b -> Bool) -> (Int -> b) -> (b -> a -> m b) -> b -> [a] -> m [b]-scanlByM  cmp g f mz = liftM (map extractAcc) . List.scanl go (liftM (map (Acc 0)) mz) -  where-    go mia b = do-      Acc n a <- mia-      a' <- specBy' cmp (g n) (`f` b) a-      return (Acc (n + 1) a')-{-# INLINE scanlByM #-}--scanrM :: (Monad m, Eq (m b)) => (Int -> m b) -> (a -> b -> m b) -> m b -> [a] -> m [b]-scanrM = scanrByM (==)-{-# INLINE scanrM #-}--scanrByM :: Monad m => (m b -> m b -> Bool) -> (Int -> m b) -> (a -> b -> m b) -> m b -> [a] -> m [b]-scanrByM cmp g f mz = liftM (map extractAcc) . List.scanr go (liftM (map (Acc 0)) mz) -  where-    go a mib = do-      Acc n b <- mib-      let !n' = n + 1-      b' <- specBy' cmp (g n') (>>= f a) (return b)-      return (Acc n' b')-{-# INLINE scanrByM #-}--scanlSTM :: Eq a => (Int -> STM a) -> (a -> b -> STM a) -> STM a -> [b] -> STM [a]-scanlSTM = scanlBySTM (returning (==))-{-# INLINE scanlSTM #-}--scanlBySTM :: (a -> a -> STM Bool) -> (Int -> STM a) -> (a -> b -> STM a) -> STM a -> [b] -> STM [a]-scanlBySTM cmp g f mz = liftM (map extractAcc) . List.scanl go (liftM (Acc 0) mz)-  where-    go mia b = do-      Acc n a <- mia-      let !n' = n + 1-      a' <- specBySTM' cmp (g n') (`f` b) a-      return (Acc n' a')-{-# INLINE scanlBySTM #-}--scanrSTM :: Eq b => (Int -> STM b) -> (a -> b -> STM b) -> STM b -> [a] -> STM [b]-scanrSTM = scanrBySTM (returning (==))-{-# INLINE scanrSTM #-}--scanrBySTM :: (b -> b -> STM Bool) -> (Int -> STM b) -> (a -> b -> STM b) -> STM b -> [a] -> STM [b]-scanrBySTM cmp g f mz = liftM (map extractAcc) . List.scanr go (liftM (Acc 0) mz)-  where-    go a mib = do-      Acc n b <- mib-      let !n' = n + 1-      b' <- specBySTM' cmp (g n') (f a) b-      return (Acc n' b')-{-# INLINE scanrBySTM #-}---- | Given a valid estimator @g@, @'scanl' g f z xs@ yields the same answer as @'scanl'' f z xs@.------ @g n@ should supply an estimate of the value returned from scaning over the first @n@ elements of the container.------ If @g n@ is accurate a reasonable percentage of the time and faster to compute than the scan, then this can--- provide increased opportunities for parallelism.--}
+ Data/Speculation.hs view
@@ -0,0 +1,195 @@+{-# LANGUAGE CPP, BangPatterns, DeriveDataTypeable, MagicHash #-}+module Data.Speculation+    (+    -- * Speculative application+      spec+    , spec'+    , specBy+    , specBy'+    , specOn+    , specOn'+    -- * Speculative application with transactional rollback+    , specSTM+    , specSTM'+    , specOnSTM+    , specOnSTM'+    , specBySTM+    , specBySTM'+    ) where++import Control.Concurrent.STM+import Data.Speculation.Internal (returning)+import Data.TagBits (unsafeIsEvaluated)+import Control.Monad (liftM2, unless)+import Data.Function (on)+import GHC.Conc++-- * Basic speculation++-- | @'spec' g f a@ evaluates @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 -> b+spec = specBy (==)+{-# INLINE spec #-}++-- | 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.++spec' :: Eq a => a -> (a -> b) -> a -> b+spec' = specBy' (==)+{-# INLINE spec' #-}++-- | 'spec' with a user defined comparison function+specBy :: (a -> a -> Bool) -> a -> (a -> b) -> a -> b+specBy cmp guess f a+    | unsafeIsEvaluated a = f a+    | otherwise = specBy' cmp guess f a+{-# INLINE specBy #-}++-- | 'spec'' with a user defined comparison function+specBy' :: (a -> a -> Bool) -> a -> (a -> b) -> a -> b+specBy' cmp guess f a+  | numCapabilities == 1 = f $! a+  | otherwise = speculation `par` +    if cmp guess a+    then speculation+    else f a+  where speculation = f guess+{-# INLINE specBy' #-}++-- | 'spec' comparing by projection onto another type+specOn :: Eq c => (a -> c) -> a -> (a -> b) -> a -> b+specOn = specBy . on (==)+{-# INLINE specOn #-}++-- | 'spec'' comparing by projection onto another type+specOn' :: Eq c => (a -> c) -> a -> (a -> b) -> a -> b+specOn' = specBy' . on (==)+{-# INLINE specOn' #-}++-- * STM-based speculation++-- | @'specSTM' g f a@ evaluates @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 ------]+--++specSTM :: Eq a => STM a -> (a -> STM b) -> a -> STM b+specSTM = specBySTM (returning (==))+{-# INLINE specSTM #-}++-- | Unlike 'specSTM', 'specSTM'' doesn't check if the argument has already been evaluated.++specSTM' :: Eq a => STM a -> (a -> STM b) -> a -> STM b+specSTM' = specBySTM' (returning (==))+{-# INLINE specSTM' #-}++-- | 'specSTM' using a user defined comparison function+specBySTM :: (a -> a -> STM Bool) -> STM a -> (a -> STM b) -> a -> STM b+specBySTM cmp guess f a+    | unsafeIsEvaluated a = f a+    | otherwise   = specBySTM' cmp guess f a+{-# INLINE specBySTM #-}++#ifndef HAS_NUM_SPARKS+numSparks :: IO Int+numSparks = return 0+#endif++-- | 'specSTM'' using a user defined comparison function+specBySTM' :: (a -> a -> STM Bool) -> STM a -> (a -> STM b) -> a -> STM b+specBySTM' cmp mguess f a = do+  sparks <- unsafeIOToSTM numSparks+  if sparks < numCapabilities +    then a `par` do+      guess <- mguess+      result <- f guess+      -- rendezvous with a+      matching <- cmp guess a+      unless matching retry+      return result+     `orElse`+      f a+    else f $! a +{-# INLINE specBySTM' #-}++-- | @'specBySTM' . 'on' (==)@+specOnSTM :: Eq c => (a -> STM c) -> STM a -> (a -> STM b) -> a -> STM b+specOnSTM = specBySTM . on (liftM2 (==))+{-# INLINE specOnSTM #-}++-- | @'specBySTM'' . 'on' (==)@+specOnSTM' :: Eq c => (a -> STM c) -> STM a -> (a -> STM b) -> a -> STM b+specOnSTM' = specBySTM' . on (liftM2 (==))+{-# INLINE specOnSTM' #-}
+ Data/Speculation/Cont.hs view
@@ -0,0 +1,70 @@+-----------------------------------------------------------------------------+-- |+-- Module      :  Data.Speculation.Cont+-- Copyright   :  (C) 2011 Edward Kmett, Jake McArthur+-- License     :  BSD-style (see the file LICENSE)+--+-- Maintainer  :  Edward Kmett <ekmett@gmail.com>+-- Stability   :  provisional+-- Portability :  portable+--+-- Versions of the combinators from the 'speculation' package+-- with the signature rearranged to enable them to be used+-- directly as actions in the 'Cont' and 'ContT' monads.+----------------------------------------------------------------------------+module Data.Speculation.Cont where++import Control.Monad.Trans.Cont+import qualified Data.Speculation as Prim+import Control.Concurrent.STM++-- * Basic speculation++-- | When a is unevaluated, @'spec' g a@ evaluates the current continuation +-- with @g@ while testing if @g@ '==' @a@, if they differ, it re-evalutes the+-- continuation with @a@. If @a@ was already evaluated, the continuation is+-- just directly applied to @a@ instead.+spec :: Eq a => a -> a -> ContT r m a+spec g a = ContT $ \k -> Prim.spec g k a ++-- | As per 'spec', without the check for whether or not the second argument+-- is already evaluated.+spec' :: Eq a => a -> a -> ContT r m a+spec' g a = ContT $ \k -> Prim.spec' g k a++-- | @spec@ with a user supplied comparison function+specBy :: (a -> a -> Bool) -> a -> a -> ContT r m a+specBy f g a = ContT $ \k -> Prim.specBy f g k a++-- | @spec'@ with a user supplied comparison function+specBy' :: (a -> a -> Bool) -> a -> a -> ContT r m a+specBy' f g a = ContT $ \k -> Prim.specBy' f g k a++-- | @spec'@ with a user supplied comparison function+specOn :: Eq c => (a -> c) -> a -> a -> ContT r m a+specOn f g a = ContT $ \k -> Prim.specOn f g k a++-- | @spec'@ with a user supplied comparison function+specOn' :: Eq c => (a -> c) -> a -> a -> ContT r m a+specOn' f g a = ContT $ \k -> Prim.specOn' f g k a++-- * STM-based speculation++specSTM :: Eq a => STM a -> a -> ContT r STM a+specSTM g a = ContT $ \k -> Prim.specSTM g k a ++specSTM' :: Eq a => STM a -> a -> ContT r STM a+specSTM' g a = ContT $ \k -> Prim.specSTM' g k a ++specOnSTM :: Eq c => (a -> STM c) -> STM a -> a -> ContT r STM a+specOnSTM f g a = ContT $ \k -> Prim.specOnSTM f g k a ++specOnSTM' :: Eq c => (a -> STM c) -> STM a -> a -> ContT r STM a+specOnSTM' f g a = ContT $ \k -> Prim.specOnSTM' f g k a ++specBySTM :: (a -> a -> STM Bool) -> STM a -> a -> ContT r STM a+specBySTM f g a = ContT $ \k -> Prim.specBySTM f g k a ++specBySTM' :: (a -> a -> STM Bool) -> STM a -> a -> ContT r STM a+specBySTM' f g a = ContT $ \k -> Prim.specBySTM' f g k a +
+ Data/Speculation/Foldable.hs view
@@ -0,0 +1,416 @@+{-# LANGUAGE BangPatterns #-}+module Data.Speculation.Foldable+    ( +    -- * Speculative folds+      fold, foldBy+    , foldMap, foldMapBy+    , foldr, foldrBy+    , foldl, foldlBy+    , foldr1, foldr1By+    , foldl1, foldl1By+    -- ** Speculative monadic folds+    , foldrM, foldrByM+    , foldlM, foldlByM+    -- * Speculative transactional monadic folds+    , foldrSTM, foldrBySTM+    , foldlSTM, foldlBySTM+    -- * Folding actions+    -- ** Applicative actions+    , traverse_, traverseBy_+    , for_, forBy_+    , sequenceA_, sequenceByA_+    , asum, asumBy+    -- ** Monadic actions+    , mapM_, mapByM_+    , forM_, forByM_+    , sequence_, sequenceBy_+    , msum, msumBy+    -- ** Speculative transactional monadic actions+    , mapSTM_, forSTM_, sequenceSTM_+    -- * Specialized folds+    , toList, toListBy+    , concat, concatBy+    , concatMap, concatMapBy+    , all, any, and, or+    , sum, sumBy+    , product, productBy+    , maximum, maximumBy+    , minimum, minimumBy+    -- * Searches+    , elem, elemBy+    , notElem, notElemBy+    , find, findBy+    ) where++import Prelude hiding +    (foldl, foldl1, foldr, foldr1+    , any, all, and, or, mapM_, sequence_+    , elem, notElem, sum, product+    , minimum, maximum, concat, concatMap+    )++import Data.Monoid+import Data.Ix ()+import Data.Function (on)+import Data.Foldable (Foldable)+import qualified Data.Foldable as Foldable+import Control.Concurrent.STM+import Data.Speculation+import Data.Speculation.Internal+import Control.Applicative+import Control.Monad hiding (mapM_, msum, forM_, sequence_)++-- | Given a valid estimator @g@, @'fold' g f xs@ yields the same answer as @'fold' f xs@.+-- +-- @g n@ should supply an estimate of the value of the monoidal summation over the last @n@ elements of the container.+-- +-- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the fold, then this can+-- provide increased opportunities for parallelism.++fold :: (Foldable f, Monoid m, Eq m) => (Int -> m) -> f m -> m+fold = foldBy (==)+{-# INLINE fold #-}++-- | 'fold' using 'specBy'+foldBy :: (Foldable f, Monoid m) => (m -> m -> Bool) -> (Int -> m) -> f m -> m+foldBy cmp g = foldrBy cmp g mappend mempty+{-# INLINE foldBy #-}++-- | Given a valid estimator @g@, @'foldMap' g f xs@ yields the same answer as @'foldMap' f xs@.+-- +-- @g n@ should supply an estimate of the value of the monoidal summation over the last @n@ elements of the container.+-- +-- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the fold, then this can+-- provide increased opportunities for parallelism.++foldMap :: (Foldable f, Monoid m, Eq m) => (Int -> m) -> (a -> m) -> f a -> m+foldMap = foldMapBy (==)+{-# INLINE foldMap #-}++-- | 'foldMap' using 'specBy'+foldMapBy :: (Foldable f, Monoid m) => (m -> m -> Bool) -> (Int -> m) -> (a -> m) -> f a -> m+foldMapBy cmp g f = foldrBy cmp g (mappend . f) mempty+{-# INLINE foldMapBy #-}+++foldr :: (Foldable f, Eq b) => (Int -> b) -> (a -> b -> b) -> b -> f a -> b+foldr = foldrBy (==)+{-# INLINE foldr #-}++-- | Given a valid estimator @g@, @'foldr' g f z xs@ yields the same answer as @'foldr'' f z xs@.+--+-- @g n@ should supply an estimate of the value returned from folding over the last @n@ elements of the container.+--+-- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the fold, then this can+-- provide increased opportunities for parallelism.+foldrBy :: Foldable f => (b -> b -> Bool) -> (Int -> b) -> (a -> b -> b) -> b -> f a -> b+foldrBy cmp g f z = extractAcc . Foldable.foldr mf (Acc 0 z)+  where +    mf a (Acc n b) = Acc (n + 1) (specBy' cmp (g n) (f a) b)+{-# INLINE foldrBy #-}+++{-+-- Variations:+-- These variations are not used because the values ot the left shouldn't affect the intermediate state of a right fold.+--+-- this version receiveds both the number of values remaining and the number so far++foldrBy :: Foldable f => (b -> b -> Bool) -> (Int -> Int -> b) -> (a -> b -> b) -> b -> f a -> b+foldrBy cmp g f z xs = Foldable.foldr mf (Acc 0 (const z)) xs 0+  where +    mf a (Acc r b) !l = let l' = l + 1 in Acc (r + 1) (specBy' cmp (g l') (f a) (b l'))+{-# INLINE foldrBy #-}++-- this estimator receives the number of values to the left of the summation. +foldrBy :: Foldable f => (b -> b -> Bool) -> (Int -> b) -> (a -> b -> b) -> b -> f a -> b+foldrBy cmp g f z xs = Foldable.foldr mf (const z) xs 0+  where +    mf a b !i = let i' = i + 1 in specBy' cmp (g i') (f a) (b i')+{-# INLINE foldrBy #-}+-}++foldlM :: (Foldable f, Monad m, Eq (m b)) => (Int -> m b) -> (b -> a -> m b) -> m b -> f a -> m b+foldlM = foldlByM (==)+{-# INLINE foldlM #-}++foldlByM ::  (Foldable f, Monad m) => (m b -> m b -> Bool) -> (Int -> m b) -> (b -> a -> m b) -> m b -> f a -> m b+foldlByM  cmp g f mz = liftM extractAcc . Foldable.foldl go (liftM (Acc 0) mz) +  where+    go mia b = do+      Acc n a <- mia+      a' <- specBy' cmp (g n) (>>= (`f` b)) (return a)+      return (Acc (n + 1) a')+{-# INLINE foldlByM #-}++foldrM :: (Foldable f, Monad m, Eq (m b)) => (Int -> m b) -> (a -> b -> m b) -> m b -> f a -> m b+foldrM = foldrByM (==)+{-# INLINE foldrM #-}++foldrByM :: (Foldable f, Monad m) => (m b -> m b -> Bool) -> (Int -> m b) -> (a -> b -> m b) -> m b -> f a -> m b+foldrByM cmp g f mz = liftM extractAcc . Foldable.foldr go (liftM (Acc 0) mz) +  where+    go a mib = do+      Acc n b <- mib+      b' <- specBy' cmp (g n) (>>= f a) (return b)+      return (Acc (n + 1) b')+{-# INLINE foldrByM #-}++foldlSTM :: (Foldable f, Eq a) => (Int -> STM a) -> (a -> b -> STM a) -> STM a -> f b -> STM a+foldlSTM = foldlBySTM (returning (==))+{-# INLINE foldlSTM #-}++foldlBySTM :: Foldable f => (a -> a -> STM Bool) -> (Int -> STM a) -> (a -> b -> STM a) -> STM a -> f b -> STM a+foldlBySTM cmp g f mz = liftM extractAcc . Foldable.foldl go (liftM (Acc 0) mz)+  where+    go mia b = do+      Acc n a <- mia+      a' <- specBySTM' cmp (g n) (`f` b) a+      return (Acc (n + 1) a')+{-# INLINE foldlBySTM #-}++foldrSTM :: (Foldable f, Eq b) => (Int -> STM b) -> (a -> b -> STM b) -> STM b -> f a -> STM b+foldrSTM = foldrBySTM (returning (==))+{-# INLINE foldrSTM #-}++foldrBySTM :: Foldable f => (b -> b -> STM Bool) -> (Int -> STM b) -> (a -> b -> STM b) -> STM b -> f a -> STM b+foldrBySTM cmp g f mz = liftM extractAcc . Foldable.foldr go (liftM (Acc 0) mz)+  where+    go a mib = do+      Acc n b <- mib+      b' <- specBySTM' cmp (g n) (f a) b+      return (Acc (n + 1) b')+{-# INLINE foldrBySTM #-}++-- | Given a valid estimator @g@, @'foldl' g f z xs@ yields the same answer as @'foldl'' f z xs@.+--+-- @g n@ should supply an estimate of the value returned from folding over the first @n@ elements of the container.+--+-- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the fold, then this can+-- provide increased opportunities for parallelism.++foldl  :: (Foldable f, Eq b) => (Int -> b) -> (b -> a -> b) -> b -> f a -> b+foldl = foldlBy (==) +{-# INLINE foldl #-}++foldlBy  :: Foldable f => (b -> b -> Bool) -> (Int -> b) -> (b -> a -> b) -> b -> f a -> b+foldlBy cmp g f z = extractAcc . Foldable.foldl mf (Acc 0 z)+  where+    mf (Acc n a) b = Acc (n + 1) (specBy' cmp (g n) (`f` b) a)+{-# INLINE foldlBy #-}++foldr1 :: (Foldable f, Eq a) => (Int -> a) -> (a -> a -> a) -> f a -> a+foldr1 = foldr1By (==) +{-# INLINE foldr1 #-}++foldr1By :: Foldable f => (a -> a -> Bool) -> (Int -> a) -> (a -> a -> a) -> f a -> a+foldr1By cmp g f xs = fromMaybeAcc (errorEmptyStructure "foldr1")+                                   (Foldable.foldr mf NothingAcc xs)+  where+    mf a (JustAcc n b) = JustAcc (n + 1) (specBy' cmp (g n) (f a) b)+    mf a NothingAcc = JustAcc 1 a+{-# INLINE foldr1By #-}++foldl1 :: (Foldable f, Eq a) => (Int -> a) -> (a -> a -> a) -> f a -> a+foldl1 = foldl1By (==)+{-# INLINE foldl1 #-}++foldl1By :: Foldable f => (a -> a -> Bool) -> (Int -> a) -> (a -> a -> a) -> f a -> a+foldl1By cmp g f xs = fromMaybeAcc (errorEmptyStructure "foldl1")+                               (Foldable.foldl mf NothingAcc xs)+  where+    mf (JustAcc n a) b = JustAcc (n + 1) (specBy' cmp (g n) (`f` b) a)+    mf NothingAcc b    = JustAcc 1 b+{-# INLINE foldl1By #-}++-- | Map each element of a structure to an action, evaluate these actions+-- from left to right and ignore the results.+traverse_ :: (Foldable t, Applicative f, Eq (f ())) => (Int -> f c) -> (a -> f b) -> t a -> f ()+traverse_ = traverseBy_ (==)+{-# INLINE traverse_ #-}++traverseBy_ :: (Foldable t, Applicative f) => (f () -> f () -> Bool) -> (Int -> f c) -> (a -> f b) -> t a -> f ()+traverseBy_ cmp g f = foldrBy cmp ((() <$) . g) ((*>) . f) (pure ())+{-# INLINE traverseBy_ #-}++-- | 'for_' is 'traverse_' with its arguments flipped.+for_ :: (Foldable t, Applicative f, Eq (f ())) => (Int -> f c) -> t a -> (a -> f b) -> f ()+for_ g = flip (traverse_ g)+{-# INLINE for_ #-}++forBy_ :: (Foldable t, Applicative f) => (f () -> f () -> Bool) -> (Int -> f c) -> t a -> (a -> f b) -> f ()+forBy_ cmp g = flip (traverseBy_ cmp g)+{-# INLINE forBy_ #-}++-- | Map each element of the structure to a monadic action, evaluating these actions+-- from left to right and ignoring the results.+mapM_ :: (Foldable t, Monad m, Eq (m ())) => (Int -> m c) -> (a -> m b) -> t a -> m ()+mapM_ = mapByM_ (==)+{-# INLINE mapM_ #-}++-- | Map each element of the structure to a monadic action, evaluating these actions+-- from left to right and ignoring the results, while transactional side-effects from +-- mis-speculated actions are rolled back.+mapSTM_ :: Foldable t => STM Bool -> (Int -> STM c) -> (a -> STM b) -> t a -> STM ()+mapSTM_ chk g f = foldrBySTM (\_ _ -> chk) (\n -> () <$ g n) (\a _ -> () <$ f a) (return ())+{-# INLINE mapSTM_ #-}++mapByM_ :: (Foldable t, Monad m) => (m () -> m () -> Bool) -> (Int -> m c) -> (a -> m b) -> t a -> m ()+mapByM_ cmp g f = foldrBy cmp (\n -> g n >> return ()) ((>>) . f) (return ())+{-# INLINE mapByM_ #-}++-- | 'for_' is 'mapM_' with its arguments flipped.+forM_ :: (Foldable t, Monad m, Eq (m ())) => (Int -> m c) -> t a -> (a -> m b) -> m ()+forM_ g = flip (mapM_ g)+{-# INLINE forM_ #-}++-- | 'for_' is 'mapM_' with its arguments flipped.+forSTM_ :: Foldable t => STM Bool -> (Int -> STM c) -> t a -> (a -> STM b) -> STM ()+forSTM_ chk g = flip (mapSTM_ chk g)+{-# INLINE forSTM_ #-}++forByM_ :: (Foldable t, Monad m) => (m () -> m () -> Bool) -> (Int -> m c) -> t a -> (a -> m b) -> m ()+forByM_ cmp g = flip (mapByM_ cmp g)+{-# INLINE forByM_ #-}++sequenceA_ :: (Foldable t, Applicative f, Eq (f ())) => (Int -> f b) -> t (f a) -> f ()+sequenceA_ = sequenceByA_ (==)+{-# INLINE sequenceA_ #-}++sequenceByA_ :: (Foldable t, Applicative f, Eq (f ())) => (f () -> f () -> Bool) -> (Int -> f b) -> t (f a) -> f ()+sequenceByA_ cmp g = foldrBy cmp ((()<$) . g) (*>) (pure ())+{-# INLINE sequenceByA_ #-}++sequence_ :: (Foldable t, Monad m, Eq (m ())) => (Int -> m b) -> t (m a) -> m ()+sequence_ = sequenceBy_ (==) +{-# INLINE sequence_ #-}++sequenceSTM_:: Foldable t => STM Bool -> (Int -> STM a) -> t (STM b) -> STM ()+sequenceSTM_ chk g = foldrBySTM (\_ _ -> chk) (\n -> () <$ g n) (\a _ -> () <$ a) (return ())+{-# INLINE sequenceSTM_ #-}++sequenceBy_ :: (Foldable t, Monad m) => (m () -> m () -> Bool) -> (Int -> m b) -> t (m a) -> m ()+sequenceBy_ cmp g = foldrBy cmp (\n -> g n >> return ()) (>>) (return ())+{-# INLINE sequenceBy_ #-}++asum :: (Foldable t, Alternative f, Eq (f a)) => (Int -> f a) -> t (f a) -> f a+asum = asumBy (==)+{-# INLINE asum #-}++asumBy :: (Foldable t, Alternative f) => (f a -> f a -> Bool) -> (Int -> f a) -> t (f a) -> f a+asumBy cmp g = foldrBy cmp g (<|>) empty+{-# INLINE asumBy #-}++msum  :: (Foldable t, MonadPlus m, Eq (m a)) => (Int -> m a) -> t (m a) -> m a+msum = msumBy (==) +{-# INLINE msum #-}++msumBy  :: (Foldable t, MonadPlus m) => (m a -> m a -> Bool) -> (Int -> m a) -> t (m a) -> m a+msumBy cmp g = foldrBy cmp g mplus mzero +{-# INLINE msumBy #-}++toList :: (Foldable t, Eq a) => (Int -> [a]) -> t a -> [a]+toList = toListBy (==)+{-# INLINE toList #-}++toListBy :: Foldable t => ([a] -> [a] -> Bool) -> (Int -> [a]) -> t a -> [a]+toListBy cmp g = foldrBy cmp g (:) []+{-# INLINE toListBy #-}++concat :: (Foldable t, Eq a) => (Int -> [a]) -> t [a] -> [a]+concat = fold+{-# INLINE concat #-}++concatBy :: Foldable t => ([a] -> [a] -> Bool) -> (Int -> [a]) -> t [a] -> [a]+concatBy = foldBy+{-# INLINE concatBy #-}++concatMap :: (Foldable t, Eq b) => (Int -> [b]) -> (a -> [b]) -> t a -> [b]+concatMap = foldMap+{-# INLINE concatMap #-}++concatMapBy :: (Foldable t) => ([b] -> [b] -> Bool) -> (Int -> [b]) -> (a -> [b]) -> t a -> [b]+concatMapBy = foldMapBy+{-# INLINE concatMapBy #-}++and :: Foldable t => (Int -> Bool) -> t Bool -> Bool+and g = getAll . foldMap (All . g) All+{-# INLINE and #-}++or :: Foldable t => (Int -> Bool) -> t Bool -> Bool+or g = getAny . foldMap (Any . g) Any+{-# INLINE or #-}++all :: Foldable t => (Int -> Bool) -> (a -> Bool) -> t a -> Bool+all g p = getAll . foldMap (All . g) (All . p)+{-# INLINE all #-}++any :: Foldable t => (Int -> Bool) -> (a -> Bool) -> t a -> Bool+any g p = getAny . foldMap (Any . g) (Any . p)+{-# INLINE any #-}++sum :: (Foldable t, Num a) => (Int -> a) -> t a -> a+sum = sumBy (==)+{-# INLINE sum #-}++sumBy :: (Foldable t, Num a) => (a -> a -> Bool) -> (Int -> a) -> t a -> a+sumBy cmp g = getSum . foldMapBy (on cmp getSum) (Sum . g) Sum+{-# INLINE sumBy #-}++product :: (Foldable t, Num a) => (Int -> a) -> t a -> a+product = productBy (==)+{-# INLINE product #-}++productBy :: (Foldable t, Num a) => (a -> a -> Bool) -> (Int -> a) -> t a -> a+productBy cmp g = getProduct . foldMapBy (on cmp getProduct) (Product . g) Product+{-# INLINE productBy #-}++maximum :: (Foldable t, Ord a) => (Int -> a) -> t a -> a+maximum g = foldr1 g max+{-# INLINE maximum #-}++-- TODO: allow for patching?+maximumBy :: Foldable t => (a -> a -> Ordering) -> (Int -> a) -> t a -> a+maximumBy cmp g = foldr1By cmp' g max'+  where +    max' x y = case cmp x y of +        GT -> x +        _  -> y+    cmp' x y = cmp x y == EQ+{-# INLINE maximumBy #-}+        +minimum :: (Foldable t, Ord a) => (Int -> a) -> t a -> a+minimum g = foldr1 g min+{-# INLINE minimum #-}++minimumBy :: Foldable t => (a -> a -> Ordering) -> (Int -> a) -> t a -> a+minimumBy cmp g = foldr1By cmp' g min'+  where +    min' x y = case cmp x y of +        GT -> x +        _  -> y+    cmp' x y = cmp x y == EQ+{-# INLINE minimumBy #-}+        +elem :: (Foldable t, Eq a) => (Int -> Bool) -> a -> t a -> Bool+elem g = any g . (==)+{-# INLINE elem #-}++elemBy :: Foldable t => (a -> a -> Bool) -> (Int -> Bool) -> a -> t a -> Bool+elemBy cmp g = any g . cmp+{-# INLINE elemBy #-}++notElem :: (Foldable t, Eq a) => (Int -> Bool) -> a -> t a -> Bool+notElem g a = not . elem g a+{-# INLINE notElem #-}++notElemBy :: Foldable t => (a -> a -> Bool) -> (Int -> Bool) -> a -> t a -> Bool+notElemBy cmp g a = not . elemBy cmp g a+{-# INLINE notElemBy #-}++find :: (Foldable t, Eq a) => (Int -> Maybe a) -> (a -> Bool) -> t a -> Maybe a+find = findBy (==) ++findBy :: Foldable t => (Maybe a -> Maybe a -> Bool) -> (Int -> Maybe a) -> (a -> Bool) -> t a -> Maybe a +findBy cmp g p = getFirst . foldMapBy (on cmp getFirst) (First . g) (\x -> if p x then First (Just x) else First (Nothing))+
+ Data/Speculation/Internal.hs view
@@ -0,0 +1,53 @@+module Data.Speculation.Internal +    ( Acc(..)+    , extractAcc+    , MaybeAcc(..)+    , fromMaybeAcc+    , errorEmptyStructure+    , returning+    ) where++import Data.Foldable+import Data.Traversable+import Control.Applicative++-- comonad!+data Acc a = Acc {-# UNPACK #-} !Int a++instance Functor Acc where+    fmap f (Acc n a) = Acc n (f a)++instance Foldable Acc where+    foldMap = foldMapDefault++instance Traversable Acc where+    traverse f (Acc n a) = Acc n <$> f a++extractAcc :: Acc a -> a+extractAcc (Acc _ a) = a+{-# INLINE extractAcc #-}++data MaybeAcc a = JustAcc {-# UNPACK #-} !Int a | NothingAcc++instance Functor MaybeAcc where+    fmap f (JustAcc n a) = JustAcc n (f a)+    fmap _ NothingAcc = NothingAcc++instance Foldable MaybeAcc where+    foldMap = foldMapDefault++instance Traversable MaybeAcc where+    traverse f (JustAcc n a) = JustAcc n <$> f a+    traverse _ NothingAcc    = pure NothingAcc++fromMaybeAcc :: a -> MaybeAcc a -> a+fromMaybeAcc _ (JustAcc _ a) = a+fromMaybeAcc a _ = a+{-# INLINE fromMaybeAcc #-}++errorEmptyStructure :: String -> a+errorEmptyStructure f = error $ f ++ ": error empty structure"++returning :: Monad m => (a -> b -> c) -> a -> b -> m c+returning f a b = return (f a b)+{-# INLINE returning #-}
+ Data/Speculation/List.hs view
@@ -0,0 +1,181 @@+{-# LANGUAGE BangPatterns #-}+module Data.Speculation.List+    ( +    -- * Speculative scans+      scan, scanBy+    , scanMap, scanMapBy+    , scanr, scanrBy+    , scanl, scanlBy+    , scanr1, scanr1By+    , scanl1, scanl1By+    {-+    -- ** Speculative monadic scans+    , scanrM, scanrByM+    , scanlM, scanlByM+    -- * Speculative transactional monadic scans+    , scanrSTM, scanrBySTM+    , scanlSTM, scanlBySTM+    -}+    ) where+++import Prelude hiding +    (foldl, foldl1, foldr, foldr1+    , any, all, and, or, mapM_, sequence_+    , elem, notElem, sum, product+    , minimum, maximum, concat, concatMap+    , scanr, scanl, scanr1, scanl1+    )++import Data.Monoid+import qualified Data.List as List+import Data.Speculation+import Data.Speculation.Internal++-- | Given a valid estimator @g@, @'scan' g xs@ converts @xs@ into a list of the prefix sums.+-- +-- @g n@ should supply an estimate of the value of the monoidal summation over the first @n@ elements of the container.+-- +-- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the prefix sum, then this can+-- provide increased opportunities for parallelism.++scan :: (Monoid m, Eq m) => (Int -> m) -> [m] -> [m]+scan = scanBy (==)+{-# INLINE scan #-}++-- | 'scan' using 'specBy'+scanBy :: Monoid m => (m -> m -> Bool) -> (Int -> m) -> [m] -> [m]+scanBy cmp g = scanrBy cmp g mappend mempty+{-# INLINE scanBy #-}++-- | Given a valid estimator @g@, @'scanMap' g f xs@ converts @xs@ into a list of the prefix sums.+-- +-- @g n@ should supply an estimate of the value of the monoidal summation over the first @n@ elements of the container.+-- +-- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the scan, then this can+-- provide increased opportunities for parallelism.+--+-- > scan = scanMap id+-- > scanMap = scanMapBy (==)++scanMap :: (Monoid m, Eq m) => (Int -> m) -> (a -> m) -> [a] -> [m]+scanMap = scanMapBy (==)+{-# INLINE scanMap #-}++scanMapBy :: Monoid m => (m -> m -> Bool) -> (Int -> m) -> (a -> m) -> [a] -> [m]+scanMapBy cmp g f = scanrBy cmp g (mappend . f) mempty+{-# INLINE scanMapBy #-}++-- | Given a valid estimator @g@, @'scanr' g f z xs@ yields the same answer as @'scanr'' f z xs@.+--+-- @g n@ should supply an estimate of the value returned from scanning over the last @n@ elements of the container.+--+-- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the scan, then this can+-- provide increased opportunities for parallelism.++scanr :: Eq b => (Int -> b) -> (a -> b -> b) -> b -> [a] -> [b]+scanr = scanrBy (==)+{-# INLINE scanr #-}++scanrBy :: (b -> b -> Bool) -> (Int -> b) -> (a -> b -> b) -> b -> [a] -> [b]+scanrBy cmp g f z = map extractAcc . List.scanr mf (Acc 0 z)+  where +    mf a (Acc n b) = let n' = n + 1 in Acc n' (specBy' cmp (g n') (f a) b)+{-# INLINE scanrBy #-}+++scanl  :: Eq b => (Int -> b) -> (b -> a -> b) -> b -> [a] -> [b]+scanl = scanlBy (==) +{-# INLINE scanl #-}++scanlBy  :: (b -> b -> Bool) -> (Int -> b) -> (b -> a -> b) -> b -> [a] -> [b]+scanlBy cmp g f z = map extractAcc . List.scanl mf (Acc 0 z)+  where+    mf (Acc n a) b = let n' = n + 1 in Acc n' (specBy' cmp (g n') (`f` b) a)+{-# INLINE scanlBy #-}++scanr1 :: Eq a => (Int -> a) -> (a -> a -> a) -> [a] -> [a]+scanr1 = scanr1By (==) +{-# INLINE scanr1 #-}++scanr1By :: (a -> a -> Bool) -> (Int -> a) -> (a -> a -> a) -> [a] -> [a]+scanr1By cmp g f xs = map (fromMaybeAcc undefined) $ List.scanr mf NothingAcc xs+  where+    mf a (JustAcc n b) = let n' = n + 1 in JustAcc n' (specBy' cmp (g n') (f a) b)+    mf a NothingAcc = JustAcc 1 a+{-# INLINE scanr1By #-}++scanl1 :: Eq a => (Int -> a) -> (a -> a -> a) -> [a] -> [a]+scanl1 = scanl1By (==)+{-# INLINE scanl1 #-}++scanl1By :: (a -> a -> Bool) -> (Int -> a) -> (a -> a -> a) -> [a] -> [a]+scanl1By cmp g f xs = map (fromMaybeAcc undefined) $ List.scanl mf NothingAcc xs+  where+    mf (JustAcc n a) b = let n' = n + 1 in JustAcc n' (specBy' cmp (g n') (`f` b) a)+    mf NothingAcc b    = JustAcc 1 b+{-# INLINE scanl1By #-}++{-+scanlM :: (Monad m, Eq b) => (Int -> b) -> (b -> a -> m b) -> b -> [a] -> m [b]+scanlM = scanlByM (==)+{-# INLINE scanlM #-}++scanlByM ::  Monad m => (b -> b -> Bool) -> (Int -> b) -> (b -> a -> m b) -> b -> [a] -> m [b]+scanlByM  cmp g f mz = liftM (map extractAcc) . List.scanl go (liftM (map (Acc 0)) mz) +  where+    go mia b = do+      Acc n a <- mia+      a' <- specBy' cmp (g n) (`f` b) a+      return (Acc (n + 1) a')+{-# INLINE scanlByM #-}++scanrM :: (Monad m, Eq (m b)) => (Int -> m b) -> (a -> b -> m b) -> m b -> [a] -> m [b]+scanrM = scanrByM (==)+{-# INLINE scanrM #-}++scanrByM :: Monad m => (m b -> m b -> Bool) -> (Int -> m b) -> (a -> b -> m b) -> m b -> [a] -> m [b]+scanrByM cmp g f mz = liftM (map extractAcc) . List.scanr go (liftM (map (Acc 0)) mz) +  where+    go a mib = do+      Acc n b <- mib+      let !n' = n + 1+      b' <- specBy' cmp (g n') (>>= f a) (return b)+      return (Acc n' b')+{-# INLINE scanrByM #-}++scanlSTM :: Eq a => (Int -> STM a) -> (a -> b -> STM a) -> STM a -> [b] -> STM [a]+scanlSTM = scanlBySTM (returning (==))+{-# INLINE scanlSTM #-}++scanlBySTM :: (a -> a -> STM Bool) -> (Int -> STM a) -> (a -> b -> STM a) -> STM a -> [b] -> STM [a]+scanlBySTM cmp g f mz = liftM (map extractAcc) . List.scanl go (liftM (Acc 0) mz)+  where+    go mia b = do+      Acc n a <- mia+      let !n' = n + 1+      a' <- specBySTM' cmp (g n') (`f` b) a+      return (Acc n' a')+{-# INLINE scanlBySTM #-}++scanrSTM :: Eq b => (Int -> STM b) -> (a -> b -> STM b) -> STM b -> [a] -> STM [b]+scanrSTM = scanrBySTM (returning (==))+{-# INLINE scanrSTM #-}++scanrBySTM :: (b -> b -> STM Bool) -> (Int -> STM b) -> (a -> b -> STM b) -> STM b -> [a] -> STM [b]+scanrBySTM cmp g f mz = liftM (map extractAcc) . List.scanr go (liftM (Acc 0) mz)+  where+    go a mib = do+      Acc n b <- mib+      let !n' = n + 1+      b' <- specBySTM' cmp (g n') (f a) b+      return (Acc n' b')+{-# INLINE scanrBySTM #-}++-- | Given a valid estimator @g@, @'scanl' g f z xs@ yields the same answer as @'scanl'' f z xs@.+--+-- @g n@ should supply an estimate of the value returned from scaning over the first @n@ elements of the container.+--+-- If @g n@ is accurate a reasonable percentage of the time and faster to compute than the scan, then this can+-- provide increased opportunities for parallelism.+-}
+ Data/Speculation/Morphism.hs view
@@ -0,0 +1,31 @@+{-# LANGUAGE BangPatterns, MagicHash #-}+module Data.Speculation.Morphism+    ( hylo+    ) where++import GHC.Prim+import GHC.Types++import Data.Speculation++{-+newtype Mu f = In { out :: f (Mu f) } ++ana :: (Functor f, Eq a) => (Int -> a) -> (a -> f a) -> a -> Mu f+ana g psi = go 0# +  where+    go n = In . fmap (go (n +# 1#)) . spec (g (I# n)) psi++apo :: (Functor f, Eq a) => (Int -> a) -> (a -> f (Either (Mu f) a)) -> a -> Mu f+apo g psi = go 0#+    where+        go n = In . fmap (either id (go (n +# 1#))) . spec (g (I# n)) psi +-}+        +-- | @'hylo' g phi psi@ is a hylomorphism using a speculative anamorphism, where+-- @g n@ estimates the seed after n iterations of 'psi'.++hylo :: (Functor f, Eq a) => (Int -> a) -> (f b -> b) -> (a -> f a) -> a -> b+hylo g phi psi = go 0#+    where+        go n = phi . fmap (go (n +# 1#)) . spec (g (I# n)) psi
+ Data/Speculation/Traversable.hs view
@@ -0,0 +1,195 @@+{-# LANGUAGE MagicHash, Rank2Types, UnboxedTuples, BangPatterns #-}+module Data.Speculation.Traversable+    (+    -- * Traversable+    -- ** Applicative Traversals+      traverse, traverseBy+    , for, forBy+    , sequenceA, sequenceByA+    -- ** Monadic traversals+    , mapM, mapByM+    , sequence, sequenceBy+    , forM, forByM+    -- ** STM-based traversals with transactional rollback+    , mapSTM, mapBySTM+    , forSTM, forBySTM+--    , sequenceSTM, sequenceBySTM+    -- * Accumulating parameters+    , mapAccumL, mapAccumLBy+    , mapAccumR, mapAccumRBy+    ) where++import Prelude hiding (mapM, sequence)+import GHC.Prim+import GHC.Types+import Data.Traversable (Traversable)+import qualified Data.Traversable as Traversable+import Control.Applicative+import Control.Concurrent.STM+import Data.Speculation+import Data.Speculation.Internal++mapAccumL :: (Traversable t, Eq a) => (Int -> a) -> (a -> b -> (a, c)) -> a -> t b -> (a, t c)+mapAccumL = mapAccumLBy (==)+{-# INLINE mapAccumL #-}++mapAccumLBy :: Traversable t => (a -> a -> Bool) -> (Int -> a) -> (a -> b -> (a, c)) -> a -> t b -> (a, t c)+mapAccumLBy cmp g f z xs = runIntAccumL (Traversable.traverse go xs) 0 z+  where+    go b = IntAccumL (\n a -> +            let ~(a', c) = specBy' cmp (g (I# n)) (`f` b) a+            in (# n +# 1#, a', c #))+{-# INLINE mapAccumLBy #-}++mapAccumR :: (Traversable t, Eq a) => (Int -> a) -> (a -> b -> (a, c)) -> a -> t b -> (a, t c)+mapAccumR = mapAccumRBy (==)+{-# INLINE mapAccumR #-}++mapAccumRBy :: Traversable t => (a -> a -> Bool) -> (Int -> a) -> (a -> b -> (a, c)) -> a -> t b -> (a, t c)+mapAccumRBy cmp g f z xs = runIntAccumR (Traversable.traverse go xs) 0 z+  where+    go b = IntAccumR (\n a -> +            let ~(a', c) = specBy' cmp (g (I# n)) (`f` b) a+            in (# n +# 1#, a', c #))+{-# INLINE mapAccumRBy #-}++traverse  :: (Traversable t, Applicative f, Eq a) => (Int -> a) -> (a -> f b) -> t a -> f (t b)+traverse = traverseBy (==)+{-# INLINE traverse #-}++traverseBy :: (Traversable t, Applicative f) => (a -> a -> Bool) -> (Int -> a) -> (a -> f b) -> t a -> f (t b)+traverseBy cmp g f xs = runAccT (Traversable.traverse go xs) 0+  where+    -- go :: a -> AccT f a+    go a = AccT $ \i -> acc (i +# 1#) $ specBy cmp (g (I# i)) f a+{-# INLINE traverseBy #-}++mapM :: (Traversable t, Monad m, Eq a) => (Int -> a) -> (a -> m b) -> t a -> m (t b)+mapM = mapByM (==)+{-# INLINE mapM #-}++mapByM :: (Traversable t, Monad m) => (a -> a -> Bool) -> (Int -> a) -> (a -> m b) -> t a -> m (t b)+mapByM cmp g f = unwrapMonad . traverseBy cmp g (WrapMonad . f)+{-# INLINE mapByM #-}++mapSTM :: (Traversable t, Eq a) => (Int -> STM a) -> (a -> STM b) -> t a -> STM (t b)+mapSTM = mapBySTM (returning (==))+{-# INLINE mapSTM #-}++mapBySTM :: Traversable t => (a -> a -> STM Bool) -> (Int -> STM a) -> (a -> STM b) -> t a -> STM (t b)+mapBySTM cmp g f xs = unwrapMonad (runAccT (Traversable.traverse go xs) 0)+  where+    go a = AccT $ \i -> acc (i +# 1#) $ WrapMonad $ specBySTM cmp (g (I# i)) f a+{-# INLINE mapBySTM #-}++      +sequenceA :: (Traversable t, Applicative f, Eq (f a)) => (Int -> f a) -> t (f a) -> f (t a)+sequenceA g = traverse g id+{-# INLINE sequenceA #-}++sequenceByA :: (Traversable t, Applicative f) => (f a -> f a -> Bool) -> (Int -> f a) -> t (f a) -> f (t a)+sequenceByA cmp g = traverseBy cmp g id+{-# INLINE sequenceByA #-}++sequence   :: (Traversable t, Monad m, Eq (m a)) => (Int -> m a) -> t (m a) -> m (t a)+sequence g = mapM g id+{-# INLINE sequence #-}++sequenceBy :: (Traversable t, Monad m) => (m a -> m a -> Bool) -> (Int -> m a) -> t (m a) -> m (t a)+sequenceBy cmp g = mapByM cmp g id+{-# INLINE sequenceBy #-}++{-+sequenceSTM   :: (Traversable t, Eq a) => (Int -> STM a) -> t (STM a) -> STM (t a)+sequenceSTM g = mapSTM g id+{-# INLINE sequenceSTM #-}++sequenceBySTM :: Traversable t => (a -> a -> STM Bool) -> (Int -> STM a) -> t (STM a) -> STM (t a)+sequenceBySTM cmp g = mapBySTM cmp g id+{-# INLINE sequenceBySTM #-}+-}++for :: (Traversable t, Applicative f, Eq a) => (Int -> a) -> t a -> (a -> f b) -> f (t b)+for g = flip (traverse g)+{-# INLINE for #-}++forBy :: (Traversable t, Applicative f) => (a -> a -> Bool) -> (Int -> a) -> t a -> (a -> f b) -> f (t b)+forBy cmp g = flip (traverseBy cmp g)+{-# INLINE forBy #-}++forM :: (Traversable t, Monad m, Eq a) => (Int -> a) -> t a -> (a -> m b) -> m (t b)+forM g = flip (mapM g)+{-# INLINE forM #-}++forByM :: (Traversable t, Monad m) => (a -> a -> Bool) -> (Int -> a) -> t a -> (a -> m b) -> m (t b)+forByM cmp g = flip (mapByM cmp g)+{-# INLINE forByM #-}++forSTM :: (Traversable t, Eq a) => (Int -> STM a) -> t a -> (a -> STM b) -> STM (t b)+forSTM g = flip (mapSTM g)+{-# INLINE forSTM #-}++forBySTM :: Traversable t => (a -> a -> STM Bool) -> (Int -> STM a) -> t a -> (a -> STM b) -> STM (t b)+forBySTM cmp g = flip (mapBySTM cmp g)+{-# INLINE forBySTM #-}++-- Utilities++acc :: Int# -> a -> Acc a+acc i a = Acc (I# i) a+{-# INLINE acc #-}++data IntAccumL s a = IntAccumL (Int# -> s -> (# Int#, s, a #))++runIntAccumL :: IntAccumL s a -> Int -> s -> (s, a)+runIntAccumL (IntAccumL m) (I# i) s = case m i s of+    (# _, s1, a #) -> (s1, a)+{-# INLINE runIntAccumL #-}++instance Functor (IntAccumL s) where+    fmap f (IntAccumL m) = IntAccumL  (\i s -> case m i s of+        (# i1, s1, a #) -> (# i1, s1, f a #))++instance Applicative (IntAccumL s) where+    pure a = IntAccumL (\i s -> (# i, s, a #))+    IntAccumL mf <*> IntAccumL ma = IntAccumL (\i s ->+        case mf i s of +            (# i1, s1, f #) ->+                case ma i1 s1 of +                    (# i2, s2, a #) -> (# i2, s2, f a #))++data IntAccumR s a = IntAccumR (Int# -> s -> (# Int#, s, a #))++runIntAccumR :: IntAccumR s a -> Int -> s -> (s, a)+runIntAccumR (IntAccumR m) (I# i) s = case m i s of+    (# _, s1, a #) -> (s1, a)+{-# INLINE runIntAccumR #-}++instance Functor (IntAccumR s) where+    fmap f (IntAccumR m) = IntAccumR  (\i s -> case m i s of+        (# i1, s1, a #) -> (# i1, s1, f a #))++instance Applicative (IntAccumR s) where+    pure a = IntAccumR (\i s -> (# i, s, a #))+    IntAccumR mf <*> IntAccumR ma = IntAccumR (\i s ->+        case ma i s of +            (# i1, s1, a #) ->+                case mf i1 s1 of +                    (# i2, s2, f #) -> (# i2, s2, f a #))++-- applicative composition with a strict integer state applicative+newtype AccT m a = AccT (Int# -> Acc (m a))++runAccT :: Applicative m => AccT m a -> Int -> m a+runAccT (AccT m) (I# i) = extractAcc (m i)+{-# INLINE runAccT #-}++instance Functor f => Functor (AccT f) where+    fmap f (AccT m) = AccT (\i# -> case m i# of Acc i a -> Acc i (fmap f a))++instance Applicative f => Applicative (AccT f) where+    pure a = AccT (\i -> Acc (I# i) (pure a))+    AccT mf <*> AccT ma = AccT (\i0# -> +        let !(Acc !(I# i1#) f) = mf i0#+            !(Acc i2 a) = ma i1#+        in  Acc i2 (f <*> a))
− Data/Traversable/Speculation.hs
@@ -1,195 +0,0 @@-{-# LANGUAGE MagicHash, Rank2Types, UnboxedTuples, BangPatterns #-}-module Data.Traversable.Speculation-    (-    -- * Traversable-    -- ** Applicative Traversals-      traverse, traverseBy-    , for, forBy-    , sequenceA, sequenceByA-    -- ** Monadic traversals-    , mapM, mapByM-    , sequence, sequenceBy-    , forM, forByM-    -- ** STM-based traversals with transactional rollback-    , mapSTM, mapBySTM-    , forSTM, forBySTM---    , sequenceSTM, sequenceBySTM-    -- * Accumulating parameters-    , mapAccumL, mapAccumLBy-    , mapAccumR, mapAccumRBy-    ) where--import Prelude hiding (mapM, sequence)-import GHC.Prim-import GHC.Types-import Data.Traversable (Traversable)-import qualified Data.Traversable as Traversable-import Control.Applicative-import Control.Concurrent.STM-import Control.Concurrent.Speculation-import Control.Concurrent.Speculation.Internal--mapAccumL :: (Traversable t, Eq a) => (Int -> a) -> (a -> b -> (a, c)) -> a -> t b -> (a, t c)-mapAccumL = mapAccumLBy (==)-{-# INLINE mapAccumL #-}--mapAccumLBy :: Traversable t => (a -> a -> Bool) -> (Int -> a) -> (a -> b -> (a, c)) -> a -> t b -> (a, t c)-mapAccumLBy cmp g f z xs = runIntAccumL (Traversable.traverse go xs) 0 z-  where-    go b = IntAccumL (\n a -> -            let ~(a', c) = specBy' cmp (g (I# n)) (`f` b) a-            in (# n +# 1#, a', c #))-{-# INLINE mapAccumLBy #-}--mapAccumR :: (Traversable t, Eq a) => (Int -> a) -> (a -> b -> (a, c)) -> a -> t b -> (a, t c)-mapAccumR = mapAccumRBy (==)-{-# INLINE mapAccumR #-}--mapAccumRBy :: Traversable t => (a -> a -> Bool) -> (Int -> a) -> (a -> b -> (a, c)) -> a -> t b -> (a, t c)-mapAccumRBy cmp g f z xs = runIntAccumR (Traversable.traverse go xs) 0 z-  where-    go b = IntAccumR (\n a -> -            let ~(a', c) = specBy' cmp (g (I# n)) (`f` b) a-            in (# n +# 1#, a', c #))-{-# INLINE mapAccumRBy #-}--traverse  :: (Traversable t, Applicative f, Eq a) => (Int -> a) -> (a -> f b) -> t a -> f (t b)-traverse = traverseBy (==)-{-# INLINE traverse #-}--traverseBy :: (Traversable t, Applicative f) => (a -> a -> Bool) -> (Int -> a) -> (a -> f b) -> t a -> f (t b)-traverseBy cmp g f xs = runAccT (Traversable.traverse go xs) 0-  where-    -- go :: a -> AccT f a-    go a = AccT $ \i -> acc (i +# 1#) $ specBy cmp (g (I# i)) f a-{-# INLINE traverseBy #-}--mapM :: (Traversable t, Monad m, Eq a) => (Int -> a) -> (a -> m b) -> t a -> m (t b)-mapM = mapByM (==)-{-# INLINE mapM #-}--mapByM :: (Traversable t, Monad m) => (a -> a -> Bool) -> (Int -> a) -> (a -> m b) -> t a -> m (t b)-mapByM cmp g f = unwrapMonad . traverseBy cmp g (WrapMonad . f)-{-# INLINE mapByM #-}--mapSTM :: (Traversable t, Eq a) => (Int -> STM a) -> (a -> STM b) -> t a -> STM (t b)-mapSTM = mapBySTM (returning (==))-{-# INLINE mapSTM #-}--mapBySTM :: Traversable t => (a -> a -> STM Bool) -> (Int -> STM a) -> (a -> STM b) -> t a -> STM (t b)-mapBySTM cmp g f xs = unwrapMonad (runAccT (Traversable.traverse go xs) 0)-  where-    go a = AccT $ \i -> acc (i +# 1#) $ WrapMonad $ specBySTM cmp (g (I# i)) f a-{-# INLINE mapBySTM #-}--      -sequenceA :: (Traversable t, Applicative f, Eq (f a)) => (Int -> f a) -> t (f a) -> f (t a)-sequenceA g = traverse g id-{-# INLINE sequenceA #-}--sequenceByA :: (Traversable t, Applicative f) => (f a -> f a -> Bool) -> (Int -> f a) -> t (f a) -> f (t a)-sequenceByA cmp g = traverseBy cmp g id-{-# INLINE sequenceByA #-}--sequence   :: (Traversable t, Monad m, Eq (m a)) => (Int -> m a) -> t (m a) -> m (t a)-sequence g = mapM g id-{-# INLINE sequence #-}--sequenceBy :: (Traversable t, Monad m) => (m a -> m a -> Bool) -> (Int -> m a) -> t (m a) -> m (t a)-sequenceBy cmp g = mapByM cmp g id-{-# INLINE sequenceBy #-}--{--sequenceSTM   :: (Traversable t, Eq a) => (Int -> STM a) -> t (STM a) -> STM (t a)-sequenceSTM g = mapSTM g id-{-# INLINE sequenceSTM #-}--sequenceBySTM :: Traversable t => (a -> a -> STM Bool) -> (Int -> STM a) -> t (STM a) -> STM (t a)-sequenceBySTM cmp g = mapBySTM cmp g id-{-# INLINE sequenceBySTM #-}--}--for :: (Traversable t, Applicative f, Eq a) => (Int -> a) -> t a -> (a -> f b) -> f (t b)-for g = flip (traverse g)-{-# INLINE for #-}--forBy :: (Traversable t, Applicative f) => (a -> a -> Bool) -> (Int -> a) -> t a -> (a -> f b) -> f (t b)-forBy cmp g = flip (traverseBy cmp g)-{-# INLINE forBy #-}--forM :: (Traversable t, Monad m, Eq a) => (Int -> a) -> t a -> (a -> m b) -> m (t b)-forM g = flip (mapM g)-{-# INLINE forM #-}--forByM :: (Traversable t, Monad m) => (a -> a -> Bool) -> (Int -> a) -> t a -> (a -> m b) -> m (t b)-forByM cmp g = flip (mapByM cmp g)-{-# INLINE forByM #-}--forSTM :: (Traversable t, Eq a) => (Int -> STM a) -> t a -> (a -> STM b) -> STM (t b)-forSTM g = flip (mapSTM g)-{-# INLINE forSTM #-}--forBySTM :: Traversable t => (a -> a -> STM Bool) -> (Int -> STM a) -> t a -> (a -> STM b) -> STM (t b)-forBySTM cmp g = flip (mapBySTM cmp g)-{-# INLINE forBySTM #-}---- Utilities--acc :: Int# -> a -> Acc a-acc i a = Acc (I# i) a-{-# INLINE acc #-}--data IntAccumL s a = IntAccumL (Int# -> s -> (# Int#, s, a #))--runIntAccumL :: IntAccumL s a -> Int -> s -> (s, a)-runIntAccumL (IntAccumL m) (I# i) s = case m i s of-    (# _, s1, a #) -> (s1, a)-{-# INLINE runIntAccumL #-}--instance Functor (IntAccumL s) where-    fmap f (IntAccumL m) = IntAccumL  (\i s -> case m i s of-        (# i1, s1, a #) -> (# i1, s1, f a #))--instance Applicative (IntAccumL s) where-    pure a = IntAccumL (\i s -> (# i, s, a #))-    IntAccumL mf <*> IntAccumL ma = IntAccumL (\i s ->-        case mf i s of -            (# i1, s1, f #) ->-                case ma i1 s1 of -                    (# i2, s2, a #) -> (# i2, s2, f a #))--data IntAccumR s a = IntAccumR (Int# -> s -> (# Int#, s, a #))--runIntAccumR :: IntAccumR s a -> Int -> s -> (s, a)-runIntAccumR (IntAccumR m) (I# i) s = case m i s of-    (# _, s1, a #) -> (s1, a)-{-# INLINE runIntAccumR #-}--instance Functor (IntAccumR s) where-    fmap f (IntAccumR m) = IntAccumR  (\i s -> case m i s of-        (# i1, s1, a #) -> (# i1, s1, f a #))--instance Applicative (IntAccumR s) where-    pure a = IntAccumR (\i s -> (# i, s, a #))-    IntAccumR mf <*> IntAccumR ma = IntAccumR (\i s ->-        case ma i s of -            (# i1, s1, a #) ->-                case mf i1 s1 of -                    (# i2, s2, f #) -> (# i2, s2, f a #))---- applicative composition with a strict integer state applicative-newtype AccT m a = AccT (Int# -> Acc (m a))--runAccT :: Applicative m => AccT m a -> Int -> m a-runAccT (AccT m) (I# i) = extractAcc (m i)-{-# INLINE runAccT #-}--instance Functor f => Functor (AccT f) where-    fmap f (AccT m) = AccT (\i# -> case m i# of Acc i a -> Acc i (fmap f a))--instance Applicative f => Applicative (AccT f) where-    pure a = AccT (\i -> Acc (I# i) (pure a))-    AccT mf <*> AccT ma = AccT (\i0# -> -        let !(Acc !(I# i1#) f) = mf i0#-            !(Acc i2 a) = ma i1#-        in  Acc i2 (f <*> a))
Test.hs view
@@ -8,7 +8,7 @@ import Test.Framework.Providers.QuickCheck (testProperty) import Test.QuickCheck hiding ((==>)) -- import Test.HUnit hiding (Test)-import Control.Concurrent.Speculation+import Data.Speculation  main :: IO ()  main = defaultMain tests
speculation.cabal view
@@ -1,5 +1,5 @@ name:           speculation-version:        1.1.0.0+version:        1.2.0.0 license:        BSD3 license-file:   LICENSE author:         Edward A. Kmett@@ -113,14 +113,14 @@     stm >= 2.1 && < 2.2    exposed-modules:-    Control.Concurrent.Speculation-    Control.Monad.Trans.Cont.Speculation-    Control.Morphism.Speculation-    Data.Foldable.Speculation-    Data.Traversable.Speculation-    Data.List.Speculation+    Data.Speculation+    Data.Speculation.Cont+    Data.Speculation.Morphism+    Data.Speculation.Foldable+    Data.Speculation.Traversable+    Data.Speculation.List   other-modules:-    Control.Concurrent.Speculation.Internal+    Data.Speculation.Internal  executable test-speculation   main-is: Test.hs@@ -149,12 +149,13 @@       QuickCheck >= 1.2.0.0 && < 1.3,       HUnit >= 1.2.2.1 && < 1.3     other-modules:-      Control.Concurrent.Speculation.Internal-      Control.Concurrent.Speculation-      Control.Morphism.Speculation-      Data.Foldable.Speculation-      Data.Traversable.Speculation-      Data.List.Speculation+      Data.Speculation+      Data.Speculation.Cont+      Data.Speculation.Morphism+      Data.Speculation.Foldable+      Data.Speculation.Traversable+      Data.Speculation.List+      Data.Speculation.Internal  executable benchmark-speculation   main-is: Benchmark.hs@@ -178,9 +179,10 @@       containers >= 0.3.0 && < 0.4,       criterion >= 0.5 && < 0.6     other-modules:-      Control.Concurrent.Speculation.Internal-      Control.Concurrent.Speculation-      Control.Morphism.Speculation-      Data.Foldable.Speculation-      Data.Traversable.Speculation-      Data.List.Speculation+      Data.Speculation+      Data.Speculation.Cont+      Data.Speculation.Morphism+      Data.Speculation.Foldable+      Data.Speculation.Traversable+      Data.Speculation.List+      Data.Speculation.Internal