module Data.Random.Extras
(
shuffle
, shuffleSeq
, sample
, sampleSeq
, choiceExtract
, choiceExtractSeq
, choice
, safeChoice
, iterativeChoice
, choiceSeq
, safeChoiceSeq
, choiceArray
, choices
, safeChoices
, choicesArray
)
where
import Control.Monad
import Data.Random.RVar
import Data.Random.Distribution
import Data.Random.Distribution.Uniform
import Data.List
import Data.Maybe
import qualified Data.Sequence as Seq
import Data.Sequence ((><), ViewL((:<)))
import qualified Data.Array.IArray as Arr
import qualified Data.Array
import Data.Array.IArray ((!))
moduleError :: String -> String -> a
moduleError n s = error $ "Data.Random.Extras." ++ n ++ ": " ++ s
(.:) :: (c -> c') -> (a -> b -> c) -> (a -> b -> c')
(.:) = (.).(.)
extract :: [a] -> Int -> Maybe ([a], a)
extract s i | null r = Nothing
| otherwise = Just (a ++ c, b)
where (a, r) = splitAt i s
(b : c) = r
extractSeq :: Seq.Seq a -> Int -> Maybe (Seq.Seq a, a)
extractSeq s i | Seq.null r = Nothing
| otherwise = Just (a >< c, b)
where (a, r) = Seq.splitAt i s
(b :< c) = Seq.viewl r
backsaw :: Int -> [Int]
backsaw n = [n 1, n 2 .. 0]
shuffle :: [a] -> RVar [a]
shuffle = shuffleSeq . Seq.fromList
shuffleSeq :: Seq.Seq a -> RVar [a]
shuffleSeq s = do
samples <- mapM (uniform 0) . backsaw $ Seq.length s
return (shuffleSeq' s samples)
shuffleSeq' :: Seq.Seq a -> [Int] -> [a]
shuffleSeq' = snd .: mapAccumL (fromJust .: extractSeq)
sample :: Int -> [a] -> RVar [a]
sample m = sampleSeq m . Seq.fromList
sampleSeq :: Int -> Seq.Seq a -> RVar [a]
sampleSeq m s = do
samples <- mapM (uniform 0) . take m . backsaw $ Seq.length s
return (shuffleSeq' s samples)
choiceExtract :: [a] -> Maybe (RVar ([a], a))
choiceExtract [] = Nothing
choiceExtract xs = Just $ (fromJust . extract xs) `liftM` uniform 0 (length xs 1)
choiceExtractSeq :: Seq.Seq a -> Maybe (RVar (Seq.Seq a, a))
choiceExtractSeq s | Seq.null s = Nothing
| otherwise = Just $ (fromJust . extractSeq s) `liftM` uniform 0 (Seq.length s 1)
choice :: [a] -> RVar a
choice [] = moduleError "choice" "empty list"
choice xs = (xs !!) `liftM` uniform 0 (length xs 1)
safeChoice :: [a] -> Maybe (RVar a)
safeChoice [] = Nothing
safeChoice xs = Just $ choice xs
iterativeChoice :: [a] -> RVar a
iterativeChoice xs = fst `liftM` foldl' stepM (return start) xs
where stepM x y = x >>= step y
step offered (old, n) = do
i <- uniform 0 n
let new | i == 0 = offered
| otherwise = old
return $! new `seq` (new, n + 1)
start = (err, 0 :: Int)
err = moduleError "iterativeChoice" "empty list"
choiceSeq :: Seq.Seq a -> RVar a
choiceSeq s | Seq.null s = moduleError "choiceSeq" "empty sequence"
| otherwise = Seq.index s `liftM` uniform 0 (Seq.length s 1)
safeChoiceSeq :: Seq.Seq a -> Maybe (RVar a)
safeChoiceSeq s | Seq.null s = Nothing
| otherwise = Just $ choiceSeq s
choiceArray :: (Arr.IArray arr a, Arr.Ix i, Distribution Uniform i) => arr i a -> RVar a
choiceArray v = (v !) `liftM` uncurry uniform (Arr.bounds v)
choices :: Int -> [a] -> RVar [a]
choices _ [] = moduleError "choices" "empty list"
choices n xs = choicesArray n $ Data.Array.listArray (1, length xs) xs
safeChoices :: Int -> [a] -> Maybe (RVar [a])
safeChoices _ [] = Nothing
safeChoices n xs = Just $ choices n xs
choicesArray :: (Arr.IArray arr a, Arr.Ix i, Distribution Uniform i) => Int -> arr i a -> RVar [a]
choicesArray n v = map (v !) `liftM` replicateM n (uncurry uniform (Arr.bounds v))