{-# LANGUAGE BangPatterns #-}
module Control.Seq
(
Strategy
, using
, withStrategy
, r0
, rseq
, rdeepseq
, seqList
, seqListN
, seqListNth
, seqFoldable
, seqMap
, seqArray
, seqArrayBounds
, seqTuple2
, seqTuple3
, seqTuple4
, seqTuple5
, seqTuple6
, seqTuple7
, seqTuple8
, seqTuple9
) where
import Prelude
import Control.DeepSeq (NFData, deepseq)
import Data.Foldable (Foldable, toList)
import Data.Map (Map)
import qualified Data.Map (toList)
import Data.Ix (Ix)
import Data.Array (Array)
import qualified Data.Array (bounds, elems)
infixl 0 `using`
type Strategy a = a -> ()
using :: a -> Strategy a -> a
x `using` strat = strat x `seq` x
withStrategy :: Strategy a -> a -> a
withStrategy = flip using
r0 :: Strategy a
r0 _ = ()
rseq :: Strategy a
rseq x = x `seq` ()
rdeepseq :: NFData a => Strategy a
rdeepseq x = x `deepseq` ()
seqList :: Strategy a -> Strategy [a]
seqList _strat [] = ()
seqList strat (x:xs) = strat x `seq` seqList strat xs
seqListN :: Int -> Strategy a -> Strategy [a]
seqListN 0 _strat _ = ()
seqListN !_ _strat [] = ()
seqListN !n strat (x:xs) = strat x `seq` seqListN (n-1) strat xs
seqListNth :: Int -> Strategy a -> Strategy [a]
seqListNth 0 strat (x:_) = strat x
seqListNth !_ _strat [] = ()
seqListNth !n strat (_:xs) = seqListNth (n-1) strat xs
seqFoldable :: Foldable t => Strategy a -> Strategy (t a)
seqFoldable strat = seqList strat . toList
{-# SPECIALISE seqFoldable :: Strategy a -> Strategy [a] #-}
seqArray :: Ix i => Strategy a -> Strategy (Array i a)
seqArray strat = seqList strat . Data.Array.elems
seqArrayBounds :: Ix i => Strategy i -> Strategy (Array i a)
seqArrayBounds strat = seqTuple2 strat strat . Data.Array.bounds
seqMap :: Strategy k -> Strategy v -> Strategy (Map k v)
seqMap stratK stratV = seqList (seqTuple2 stratK stratV) . Data.Map.toList
seqTuple2 :: Strategy a -> Strategy b -> Strategy (a,b)
seqTuple2 strat1 strat2 (x1,x2) =
strat1 x1 `seq` strat2 x2
seqTuple3 :: Strategy a -> Strategy b -> Strategy c -> Strategy (a,b,c)
seqTuple3 strat1 strat2 strat3 (x1,x2,x3) =
strat1 x1 `seq` strat2 x2 `seq` strat3 x3
seqTuple4 :: Strategy a -> Strategy b -> Strategy c -> Strategy d -> Strategy (a,b,c,d)
seqTuple4 strat1 strat2 strat3 strat4 (x1,x2,x3,x4) =
strat1 x1 `seq` strat2 x2 `seq` strat3 x3 `seq` strat4 x4
seqTuple5 :: Strategy a -> Strategy b -> Strategy c -> Strategy d -> Strategy e -> Strategy (a,b,c,d,e)
seqTuple5 strat1 strat2 strat3 strat4 strat5 (x1,x2,x3,x4,x5) =
strat1 x1 `seq` strat2 x2 `seq` strat3 x3 `seq` strat4 x4 `seq` strat5 x5
seqTuple6 :: Strategy a -> Strategy b -> Strategy c -> Strategy d -> Strategy e -> Strategy f -> Strategy (a,b,c,d,e,f)
seqTuple6 strat1 strat2 strat3 strat4 strat5 strat6 (x1,x2,x3,x4,x5,x6) =
strat1 x1 `seq` strat2 x2 `seq` strat3 x3 `seq` strat4 x4 `seq` strat5 x5 `seq` strat6 x6
seqTuple7 :: Strategy a -> Strategy b -> Strategy c -> Strategy d -> Strategy e -> Strategy f -> Strategy g -> Strategy (a,b,c,d,e,f,g)
seqTuple7 strat1 strat2 strat3 strat4 strat5 strat6 strat7 (x1,x2,x3,x4,x5,x6,x7) =
strat1 x1 `seq` strat2 x2 `seq` strat3 x3 `seq` strat4 x4 `seq` strat5 x5 `seq` strat6 x6 `seq` strat7 x7
seqTuple8 :: Strategy a -> Strategy b -> Strategy c -> Strategy d -> Strategy e -> Strategy f -> Strategy g -> Strategy h -> Strategy (a,b,c,d,e,f,g,h)
seqTuple8 strat1 strat2 strat3 strat4 strat5 strat6 strat7 strat8 (x1,x2,x3,x4,x5,x6,x7,x8) =
strat1 x1 `seq` strat2 x2 `seq` strat3 x3 `seq` strat4 x4 `seq` strat5 x5 `seq` strat6 x6 `seq` strat7 x7 `seq` strat8 x8
seqTuple9 :: Strategy a -> Strategy b -> Strategy c -> Strategy d -> Strategy e -> Strategy f -> Strategy g -> Strategy h -> Strategy i -> Strategy (a,b,c,d,e,f,g,h,i)
seqTuple9 strat1 strat2 strat3 strat4 strat5 strat6 strat7 strat8 strat9 (x1,x2,x3,x4,x5,x6,x7,x8,x9) =
strat1 x1 `seq` strat2 x2 `seq` strat3 x3 `seq` strat4 x4 `seq` strat5 x5 `seq` strat6 x6 `seq` strat7 x7 `seq` strat8 x8 `seq` strat9 x9