{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
#if __GLASGOW_HASKELL__ >= 707
{-# LANGUAGE RoleAnnotations #-}
#endif
module System.Random.PCG.Fast
(
Gen, GenIO, GenST
, create, createSystemRandom, initialize
, withSystemRandom, withFrozen
, Variate (..)
, advance, retract
, FrozenGen, save, restore, seed, initFrozen
, uniformW8, uniformW16, uniformW32, uniformW64
, uniformI8, uniformI16, uniformI32, uniformI64
, uniformF, uniformD, uniformBool
, uniformRW8, uniformRW16, uniformRW32, uniformRW64
, uniformRI8, uniformRI16, uniformRI32, uniformRI64
, uniformRF, uniformRD, uniformRBool
, uniformBW8, uniformBW16, uniformBW32, uniformBW64
, uniformBI8, uniformBI16, uniformBI32, uniformBI64
, uniformBF, uniformBD, uniformBBool
) where
import Control.Applicative
import Control.Monad.Primitive
import Control.Monad.ST
import Data.Data
import Foreign
import GHC.Generics
import System.IO.Unsafe
import System.Random
import System.Random.PCG.Class
newtype FrozenGen = FrozenGen Word64
deriving (Int -> FrozenGen -> ShowS
[FrozenGen] -> ShowS
FrozenGen -> String
(Int -> FrozenGen -> ShowS)
-> (FrozenGen -> String)
-> ([FrozenGen] -> ShowS)
-> Show FrozenGen
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FrozenGen] -> ShowS
$cshowList :: [FrozenGen] -> ShowS
show :: FrozenGen -> String
$cshow :: FrozenGen -> String
showsPrec :: Int -> FrozenGen -> ShowS
$cshowsPrec :: Int -> FrozenGen -> ShowS
Show, FrozenGen -> FrozenGen -> Bool
(FrozenGen -> FrozenGen -> Bool)
-> (FrozenGen -> FrozenGen -> Bool) -> Eq FrozenGen
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FrozenGen -> FrozenGen -> Bool
$c/= :: FrozenGen -> FrozenGen -> Bool
== :: FrozenGen -> FrozenGen -> Bool
$c== :: FrozenGen -> FrozenGen -> Bool
Eq, Eq FrozenGen
Eq FrozenGen
-> (FrozenGen -> FrozenGen -> Ordering)
-> (FrozenGen -> FrozenGen -> Bool)
-> (FrozenGen -> FrozenGen -> Bool)
-> (FrozenGen -> FrozenGen -> Bool)
-> (FrozenGen -> FrozenGen -> Bool)
-> (FrozenGen -> FrozenGen -> FrozenGen)
-> (FrozenGen -> FrozenGen -> FrozenGen)
-> Ord FrozenGen
FrozenGen -> FrozenGen -> Bool
FrozenGen -> FrozenGen -> Ordering
FrozenGen -> FrozenGen -> FrozenGen
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: FrozenGen -> FrozenGen -> FrozenGen
$cmin :: FrozenGen -> FrozenGen -> FrozenGen
max :: FrozenGen -> FrozenGen -> FrozenGen
$cmax :: FrozenGen -> FrozenGen -> FrozenGen
>= :: FrozenGen -> FrozenGen -> Bool
$c>= :: FrozenGen -> FrozenGen -> Bool
> :: FrozenGen -> FrozenGen -> Bool
$c> :: FrozenGen -> FrozenGen -> Bool
<= :: FrozenGen -> FrozenGen -> Bool
$c<= :: FrozenGen -> FrozenGen -> Bool
< :: FrozenGen -> FrozenGen -> Bool
$c< :: FrozenGen -> FrozenGen -> Bool
compare :: FrozenGen -> FrozenGen -> Ordering
$ccompare :: FrozenGen -> FrozenGen -> Ordering
$cp1Ord :: Eq FrozenGen
Ord, Ptr b -> Int -> IO FrozenGen
Ptr b -> Int -> FrozenGen -> IO ()
Ptr FrozenGen -> IO FrozenGen
Ptr FrozenGen -> Int -> IO FrozenGen
Ptr FrozenGen -> Int -> FrozenGen -> IO ()
Ptr FrozenGen -> FrozenGen -> IO ()
FrozenGen -> Int
(FrozenGen -> Int)
-> (FrozenGen -> Int)
-> (Ptr FrozenGen -> Int -> IO FrozenGen)
-> (Ptr FrozenGen -> Int -> FrozenGen -> IO ())
-> (forall b. Ptr b -> Int -> IO FrozenGen)
-> (forall b. Ptr b -> Int -> FrozenGen -> IO ())
-> (Ptr FrozenGen -> IO FrozenGen)
-> (Ptr FrozenGen -> FrozenGen -> IO ())
-> Storable FrozenGen
forall b. Ptr b -> Int -> IO FrozenGen
forall b. Ptr b -> Int -> FrozenGen -> IO ()
forall a.
(a -> Int)
-> (a -> Int)
-> (Ptr a -> Int -> IO a)
-> (Ptr a -> Int -> a -> IO ())
-> (forall b. Ptr b -> Int -> IO a)
-> (forall b. Ptr b -> Int -> a -> IO ())
-> (Ptr a -> IO a)
-> (Ptr a -> a -> IO ())
-> Storable a
poke :: Ptr FrozenGen -> FrozenGen -> IO ()
$cpoke :: Ptr FrozenGen -> FrozenGen -> IO ()
peek :: Ptr FrozenGen -> IO FrozenGen
$cpeek :: Ptr FrozenGen -> IO FrozenGen
pokeByteOff :: Ptr b -> Int -> FrozenGen -> IO ()
$cpokeByteOff :: forall b. Ptr b -> Int -> FrozenGen -> IO ()
peekByteOff :: Ptr b -> Int -> IO FrozenGen
$cpeekByteOff :: forall b. Ptr b -> Int -> IO FrozenGen
pokeElemOff :: Ptr FrozenGen -> Int -> FrozenGen -> IO ()
$cpokeElemOff :: Ptr FrozenGen -> Int -> FrozenGen -> IO ()
peekElemOff :: Ptr FrozenGen -> Int -> IO FrozenGen
$cpeekElemOff :: Ptr FrozenGen -> Int -> IO FrozenGen
alignment :: FrozenGen -> Int
$calignment :: FrozenGen -> Int
sizeOf :: FrozenGen -> Int
$csizeOf :: FrozenGen -> Int
Storable, Typeable FrozenGen
DataType
Constr
Typeable FrozenGen
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FrozenGen -> c FrozenGen)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FrozenGen)
-> (FrozenGen -> Constr)
-> (FrozenGen -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FrozenGen))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c FrozenGen))
-> ((forall b. Data b => b -> b) -> FrozenGen -> FrozenGen)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FrozenGen -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FrozenGen -> r)
-> (forall u. (forall d. Data d => d -> u) -> FrozenGen -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> FrozenGen -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> FrozenGen -> m FrozenGen)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FrozenGen -> m FrozenGen)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FrozenGen -> m FrozenGen)
-> Data FrozenGen
FrozenGen -> DataType
FrozenGen -> Constr
(forall b. Data b => b -> b) -> FrozenGen -> FrozenGen
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FrozenGen -> c FrozenGen
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FrozenGen
forall a.
Typeable a
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> FrozenGen -> u
forall u. (forall d. Data d => d -> u) -> FrozenGen -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FrozenGen -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FrozenGen -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> FrozenGen -> m FrozenGen
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FrozenGen -> m FrozenGen
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FrozenGen
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FrozenGen -> c FrozenGen
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FrozenGen)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c FrozenGen)
$cFrozenGen :: Constr
$tFrozenGen :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> FrozenGen -> m FrozenGen
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FrozenGen -> m FrozenGen
gmapMp :: (forall d. Data d => d -> m d) -> FrozenGen -> m FrozenGen
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> FrozenGen -> m FrozenGen
gmapM :: (forall d. Data d => d -> m d) -> FrozenGen -> m FrozenGen
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> FrozenGen -> m FrozenGen
gmapQi :: Int -> (forall d. Data d => d -> u) -> FrozenGen -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> FrozenGen -> u
gmapQ :: (forall d. Data d => d -> u) -> FrozenGen -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> FrozenGen -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FrozenGen -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> FrozenGen -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FrozenGen -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> FrozenGen -> r
gmapT :: (forall b. Data b => b -> b) -> FrozenGen -> FrozenGen
$cgmapT :: (forall b. Data b => b -> b) -> FrozenGen -> FrozenGen
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c FrozenGen)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c FrozenGen)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c FrozenGen)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c FrozenGen)
dataTypeOf :: FrozenGen -> DataType
$cdataTypeOf :: FrozenGen -> DataType
toConstr :: FrozenGen -> Constr
$ctoConstr :: FrozenGen -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FrozenGen
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c FrozenGen
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FrozenGen -> c FrozenGen
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> FrozenGen -> c FrozenGen
$cp1Data :: Typeable FrozenGen
Data, Typeable, (forall x. FrozenGen -> Rep FrozenGen x)
-> (forall x. Rep FrozenGen x -> FrozenGen) -> Generic FrozenGen
forall x. Rep FrozenGen x -> FrozenGen
forall x. FrozenGen -> Rep FrozenGen x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep FrozenGen x -> FrozenGen
$cfrom :: forall x. FrozenGen -> Rep FrozenGen x
Generic)
save :: PrimMonad m => Gen (PrimState m) -> m FrozenGen
save :: Gen (PrimState m) -> m FrozenGen
save (Gen Ptr FrozenGen
p) = IO FrozenGen -> m FrozenGen
forall (m1 :: * -> *) (m2 :: * -> *) a.
(PrimBase m1, PrimMonad m2) =>
m1 a -> m2 a
unsafePrimToPrim (Ptr FrozenGen -> IO FrozenGen
forall a. Storable a => Ptr a -> IO a
peek Ptr FrozenGen
p)
{-# INLINE save #-}
restore :: PrimMonad m => FrozenGen -> m (Gen (PrimState m))
restore :: FrozenGen -> m (Gen (PrimState m))
restore FrozenGen
s = IO (Gen (PrimState m)) -> m (Gen (PrimState m))
forall (m1 :: * -> *) (m2 :: * -> *) a.
(PrimBase m1, PrimMonad m2) =>
m1 a -> m2 a
unsafePrimToPrim (IO (Gen (PrimState m)) -> m (Gen (PrimState m)))
-> IO (Gen (PrimState m)) -> m (Gen (PrimState m))
forall a b. (a -> b) -> a -> b
$ do
Ptr FrozenGen
p <- IO (Ptr FrozenGen)
forall a. Storable a => IO (Ptr a)
malloc
Ptr FrozenGen -> FrozenGen -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr FrozenGen
p FrozenGen
s
Gen (PrimState m) -> IO (Gen (PrimState m))
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr FrozenGen -> Gen (PrimState m)
forall s. Ptr FrozenGen -> Gen s
Gen Ptr FrozenGen
p)
{-# INLINE restore #-}
initFrozen :: Word64 -> FrozenGen
initFrozen :: Word64 -> FrozenGen
initFrozen Word64
w = IO FrozenGen -> FrozenGen
forall a. IO a -> a
unsafeDupablePerformIO (IO FrozenGen -> FrozenGen) -> IO FrozenGen -> FrozenGen
forall a b. (a -> b) -> a -> b
$ do
Ptr FrozenGen
p <- IO (Ptr FrozenGen)
forall a. Storable a => IO (Ptr a)
malloc
Ptr FrozenGen -> Word64 -> IO ()
pcg32f_srandom_r Ptr FrozenGen
p Word64
w
Ptr FrozenGen -> IO FrozenGen
forall a. Storable a => Ptr a -> IO a
peek Ptr FrozenGen
p IO FrozenGen -> IO () -> IO FrozenGen
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Ptr FrozenGen -> IO ()
forall a. Ptr a -> IO ()
free Ptr FrozenGen
p
{-# INLINE initFrozen #-}
seed :: FrozenGen
seed :: FrozenGen
seed = Word64 -> FrozenGen
FrozenGen Word64
0xcafef00dd15ea5e5
create :: PrimMonad m => m (Gen (PrimState m))
create :: m (Gen (PrimState m))
create = FrozenGen -> m (Gen (PrimState m))
forall (m :: * -> *).
PrimMonad m =>
FrozenGen -> m (Gen (PrimState m))
restore FrozenGen
seed
newtype Gen s = Gen (Ptr FrozenGen)
deriving (Gen s -> Gen s -> Bool
(Gen s -> Gen s -> Bool) -> (Gen s -> Gen s -> Bool) -> Eq (Gen s)
forall s. Gen s -> Gen s -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Gen s -> Gen s -> Bool
$c/= :: forall s. Gen s -> Gen s -> Bool
== :: Gen s -> Gen s -> Bool
$c== :: forall s. Gen s -> Gen s -> Bool
Eq, Eq (Gen s)
Eq (Gen s)
-> (Gen s -> Gen s -> Ordering)
-> (Gen s -> Gen s -> Bool)
-> (Gen s -> Gen s -> Bool)
-> (Gen s -> Gen s -> Bool)
-> (Gen s -> Gen s -> Bool)
-> (Gen s -> Gen s -> Gen s)
-> (Gen s -> Gen s -> Gen s)
-> Ord (Gen s)
Gen s -> Gen s -> Bool
Gen s -> Gen s -> Ordering
Gen s -> Gen s -> Gen s
forall s. Eq (Gen s)
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall s. Gen s -> Gen s -> Bool
forall s. Gen s -> Gen s -> Ordering
forall s. Gen s -> Gen s -> Gen s
min :: Gen s -> Gen s -> Gen s
$cmin :: forall s. Gen s -> Gen s -> Gen s
max :: Gen s -> Gen s -> Gen s
$cmax :: forall s. Gen s -> Gen s -> Gen s
>= :: Gen s -> Gen s -> Bool
$c>= :: forall s. Gen s -> Gen s -> Bool
> :: Gen s -> Gen s -> Bool
$c> :: forall s. Gen s -> Gen s -> Bool
<= :: Gen s -> Gen s -> Bool
$c<= :: forall s. Gen s -> Gen s -> Bool
< :: Gen s -> Gen s -> Bool
$c< :: forall s. Gen s -> Gen s -> Bool
compare :: Gen s -> Gen s -> Ordering
$ccompare :: forall s. Gen s -> Gen s -> Ordering
$cp1Ord :: forall s. Eq (Gen s)
Ord)
#if __GLASGOW_HASKELL__ >= 707
type role Gen representational
#endif
type GenIO = Gen RealWorld
type GenST = Gen
initialize :: PrimMonad m => Word64 -> m (Gen (PrimState m))
initialize :: Word64 -> m (Gen (PrimState m))
initialize Word64
a = IO (Gen (PrimState m)) -> m (Gen (PrimState m))
forall (m1 :: * -> *) (m2 :: * -> *) a.
(PrimBase m1, PrimMonad m2) =>
m1 a -> m2 a
unsafePrimToPrim (IO (Gen (PrimState m)) -> m (Gen (PrimState m)))
-> IO (Gen (PrimState m)) -> m (Gen (PrimState m))
forall a b. (a -> b) -> a -> b
$ do
Ptr FrozenGen
p <- IO (Ptr FrozenGen)
forall a. Storable a => IO (Ptr a)
malloc
Ptr FrozenGen -> Word64 -> IO ()
pcg32f_srandom_r Ptr FrozenGen
p Word64
a
Gen (PrimState m) -> IO (Gen (PrimState m))
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr FrozenGen -> Gen (PrimState m)
forall s. Ptr FrozenGen -> Gen s
Gen Ptr FrozenGen
p)
withSystemRandom :: (GenIO -> IO a) -> IO a
withSystemRandom :: (GenIO -> IO a) -> IO a
withSystemRandom GenIO -> IO a
f = do
Word64
w <- IO Word64
sysRandom
Word64 -> IO (Gen (PrimState IO))
forall (m :: * -> *).
PrimMonad m =>
Word64 -> m (Gen (PrimState m))
initialize Word64
w IO GenIO -> (GenIO -> IO a) -> IO a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= GenIO -> IO a
f
withFrozen :: FrozenGen -> (forall s. Gen s -> ST s a) -> (a, FrozenGen)
withFrozen :: FrozenGen -> (forall s. Gen s -> ST s a) -> (a, FrozenGen)
withFrozen FrozenGen
s forall s. Gen s -> ST s a
f = (forall s. ST s (a, FrozenGen)) -> (a, FrozenGen)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (a, FrozenGen)) -> (a, FrozenGen))
-> (forall s. ST s (a, FrozenGen)) -> (a, FrozenGen)
forall a b. (a -> b) -> a -> b
$ FrozenGen -> ST s (Gen (PrimState (ST s)))
forall (m :: * -> *).
PrimMonad m =>
FrozenGen -> m (Gen (PrimState m))
restore FrozenGen
s ST s (Gen s)
-> (Gen s -> ST s (a, FrozenGen)) -> ST s (a, FrozenGen)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Gen s
g -> (a -> FrozenGen -> (a, FrozenGen))
-> ST s a -> ST s FrozenGen -> ST s (a, FrozenGen)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (,) (Gen s -> ST s a
forall s. Gen s -> ST s a
f Gen s
g) (Gen (PrimState (ST s)) -> ST s FrozenGen
forall (m :: * -> *).
PrimMonad m =>
Gen (PrimState m) -> m FrozenGen
save Gen s
Gen (PrimState (ST s))
g)
createSystemRandom :: IO GenIO
createSystemRandom :: IO GenIO
createSystemRandom = (GenIO -> IO GenIO) -> IO GenIO
forall a. (GenIO -> IO a) -> IO a
withSystemRandom (GenIO -> IO GenIO
forall (m :: * -> *) a. Monad m => a -> m a
return :: GenIO -> IO GenIO)
advance :: PrimMonad m => Word64 -> Gen (PrimState m) -> m ()
advance :: Word64 -> Gen (PrimState m) -> m ()
advance Word64
u (Gen Ptr FrozenGen
p) = IO () -> m ()
forall (m1 :: * -> *) (m2 :: * -> *) a.
(PrimBase m1, PrimMonad m2) =>
m1 a -> m2 a
unsafePrimToPrim (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Ptr FrozenGen -> Word64 -> IO ()
pcg32f_advance_r Ptr FrozenGen
p Word64
u
{-# INLINE advance #-}
retract :: PrimMonad m => Word64 -> Gen (PrimState m) -> m ()
retract :: Word64 -> Gen (PrimState m) -> m ()
retract Word64
u Gen (PrimState m)
g = Word64 -> Gen (PrimState m) -> m ()
forall (m :: * -> *).
PrimMonad m =>
Word64 -> Gen (PrimState m) -> m ()
advance (-Word64
u) Gen (PrimState m)
g
{-# INLINE retract #-}
foreign import ccall unsafe "pcg_mcg_64_srandom_r"
pcg32f_srandom_r :: Ptr FrozenGen -> Word64 -> IO ()
foreign import ccall unsafe "pcg_mcg_64_xsh_rs_32_random_r"
pcg32f_random_r :: Ptr FrozenGen -> IO Word32
foreign import ccall unsafe "pcg_mcg_64_xsh_rs_32_boundedrand_r"
pcg32f_boundedrand_r :: Ptr FrozenGen -> Word32 -> IO Word32
foreign import ccall unsafe "pcg_mcg_64_advance_r"
pcg32f_advance_r :: Ptr FrozenGen -> Word64 -> IO ()
instance (PrimMonad m, s ~ PrimState m) => Generator (Gen s) m where
uniform1 :: (Word32 -> a) -> Gen s -> m a
uniform1 Word32 -> a
f (Gen Ptr FrozenGen
p) = IO a -> m a
forall (m1 :: * -> *) (m2 :: * -> *) a.
(PrimBase m1, PrimMonad m2) =>
m1 a -> m2 a
unsafePrimToPrim (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ Word32 -> a
f (Word32 -> a) -> IO Word32 -> IO a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr FrozenGen -> IO Word32
pcg32f_random_r Ptr FrozenGen
p
{-# INLINE uniform1 #-}
uniform2 :: (Word32 -> Word32 -> a) -> Gen s -> m a
uniform2 Word32 -> Word32 -> a
f (Gen Ptr FrozenGen
p) = IO a -> m a
forall (m1 :: * -> *) (m2 :: * -> *) a.
(PrimBase m1, PrimMonad m2) =>
m1 a -> m2 a
unsafePrimToPrim (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ do
Word32
w1 <- Ptr FrozenGen -> IO Word32
pcg32f_random_r Ptr FrozenGen
p
Word32
w2 <- Ptr FrozenGen -> IO Word32
pcg32f_random_r Ptr FrozenGen
p
a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> IO a) -> a -> IO a
forall a b. (a -> b) -> a -> b
$ Word32 -> Word32 -> a
f Word32
w1 Word32
w2
{-# INLINE uniform2 #-}
uniform1B :: (Word32 -> a) -> Word32 -> Gen s -> m a
uniform1B Word32 -> a
f Word32
b (Gen Ptr FrozenGen
p) = IO a -> m a
forall (m1 :: * -> *) (m2 :: * -> *) a.
(PrimBase m1, PrimMonad m2) =>
m1 a -> m2 a
unsafePrimToPrim (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ Word32 -> a
f (Word32 -> a) -> IO Word32 -> IO a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr FrozenGen -> Word32 -> IO Word32
pcg32f_boundedrand_r Ptr FrozenGen
p Word32
b
{-# INLINE uniform1B #-}
instance RandomGen FrozenGen where
next :: FrozenGen -> (Int, FrozenGen)
next FrozenGen
s = IO (Int, FrozenGen) -> (Int, FrozenGen)
forall a. IO a -> a
unsafeDupablePerformIO (IO (Int, FrozenGen) -> (Int, FrozenGen))
-> IO (Int, FrozenGen) -> (Int, FrozenGen)
forall a b. (a -> b) -> a -> b
$ do
Ptr FrozenGen
p <- IO (Ptr FrozenGen)
forall a. Storable a => IO (Ptr a)
malloc
Ptr FrozenGen -> FrozenGen -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr FrozenGen
p FrozenGen
s
Word32
w1 <- Ptr FrozenGen -> IO Word32
pcg32f_random_r Ptr FrozenGen
p
Word32
w2 <- Ptr FrozenGen -> IO Word32
pcg32f_random_r Ptr FrozenGen
p
FrozenGen
s' <- Ptr FrozenGen -> IO FrozenGen
forall a. Storable a => Ptr a -> IO a
peek Ptr FrozenGen
p
Ptr FrozenGen -> IO ()
forall a. Ptr a -> IO ()
free Ptr FrozenGen
p
(Int, FrozenGen) -> IO (Int, FrozenGen)
forall (m :: * -> *) a. Monad m => a -> m a
return (Word32 -> Word32 -> Int
forall a. Integral a => Word32 -> Word32 -> a
wordsTo64Bit Word32
w1 Word32
w2, FrozenGen
s')
{-# INLINE next #-}
split :: FrozenGen -> (FrozenGen, FrozenGen)
split FrozenGen
s = IO (FrozenGen, FrozenGen) -> (FrozenGen, FrozenGen)
forall a. IO a -> a
unsafeDupablePerformIO (IO (FrozenGen, FrozenGen) -> (FrozenGen, FrozenGen))
-> IO (FrozenGen, FrozenGen) -> (FrozenGen, FrozenGen)
forall a b. (a -> b) -> a -> b
$ do
Ptr FrozenGen
p <- IO (Ptr FrozenGen)
forall a. Storable a => IO (Ptr a)
malloc
Ptr FrozenGen -> FrozenGen -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr FrozenGen
p FrozenGen
s
Word32
w1 <- Ptr FrozenGen -> IO Word32
pcg32f_random_r Ptr FrozenGen
p
Word32
w2 <- Ptr FrozenGen -> IO Word32
pcg32f_random_r Ptr FrozenGen
p
Word32
w3 <- Ptr FrozenGen -> IO Word32
pcg32f_random_r Ptr FrozenGen
p
Word32
w4 <- Ptr FrozenGen -> IO Word32
pcg32f_random_r Ptr FrozenGen
p
Ptr FrozenGen -> Word64 -> IO ()
pcg32f_srandom_r Ptr FrozenGen
p (Word32 -> Word32 -> Word64
forall a. Integral a => Word32 -> Word32 -> a
wordsTo64Bit Word32
w1 Word32
w2)
FrozenGen
s1 <- Ptr FrozenGen -> IO FrozenGen
forall a. Storable a => Ptr a -> IO a
peek Ptr FrozenGen
p
Ptr FrozenGen -> Word64 -> IO ()
pcg32f_srandom_r Ptr FrozenGen
p (Word32 -> Word32 -> Word64
forall a. Integral a => Word32 -> Word32 -> a
wordsTo64Bit Word32
w3 Word32
w4)
FrozenGen
s2 <- Ptr FrozenGen -> IO FrozenGen
forall a. Storable a => Ptr a -> IO a
peek Ptr FrozenGen
p
Ptr FrozenGen -> IO ()
forall a. Ptr a -> IO ()
free Ptr FrozenGen
p
(FrozenGen, FrozenGen) -> IO (FrozenGen, FrozenGen)
forall (m :: * -> *) a. Monad m => a -> m a
return (FrozenGen
s1,FrozenGen
s2)