Commits

Aleksey Khudyakov committed fc4f181

Switch to monad-prim in order to allow transformer stacks
below Rand

  • Participants
  • Parent commits f53a7b8
  • Tags v0.6

Comments (0)

Files changed (4)

System/Random/MWC/CondensedTable/Monad.hs

   , MWC.tableBinomial
   ) where
 
-import Control.Monad.Primitive         (PrimMonad)
+import Control.Monad.Primitive.Class (MonadPrim)
 import qualified Data.Vector.Generic as G
 import System.Random.MWC.Monad
 import qualified System.Random.MWC.CondensedTable as MWC
 
 
-genFromTable :: (PrimMonad m, G.Vector v a) => MWC.CondensedTable v a -> Rand m a
+genFromTable :: (MonadPrim m, G.Vector v a) => MWC.CondensedTable v a -> Rand m a
 genFromTable tbl = toRand $ \g -> MWC.genFromTable tbl g
 {-# INLINE genFromTable #-}

System/Random/MWC/Distributions/Monad.hs

   , chiSquare
   ) where
 
-import Control.Monad.Primitive (PrimMonad)
+import Control.Monad.Primitive.Class (MonadPrim(..))
 
 import qualified System.Random.MWC.Distributions as MWC
 import System.Random.MWC.Monad
 
 
 -- | Normally distributed variables with mean 0 and 1 standard deviation
-standard :: PrimMonad m => Rand m Double
+standard :: MonadPrim m => Rand m Double
 standard = toRand $ \g -> MWC.standard g
 {-# INLINE normal #-}
 
 -- | Normally distributed variable
-normal :: PrimMonad m => 
+normal :: MonadPrim m =>
           Double                -- ^ Mean
        -> Double                -- ^ Standard deviation
        -> Rand m Double
 {-# INLINE standard #-}
 
 -- | Generate exponentially distributed random variate. 
-exponential :: PrimMonad m =>
+exponential :: MonadPrim m =>
                Double           -- ^ Scale parameter
             -> Rand m Double
 exponential x = toRand $ \g -> MWC.exponential x g
 {-# INLINE exponential #-}
 
 -- | Random variate generator for gamma distribution.
-gamma :: PrimMonad m
+gamma :: MonadPrim m
       => Double                 -- ^ Shape parameter
       -> Double                 -- ^ Scale parameter
       -> Rand m Double
 {-# INLINE gamma #-}
 
 -- | Random variate generator for chi square distribution.
-chiSquare :: PrimMonad m
+chiSquare :: MonadPrim m
           => Int                -- ^ Number of degrees of freedom
           -> Rand m Double
 chiSquare n = toRand $ \g -> MWC.chiSquare n g

System/Random/MWC/Monad.hs

 {-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE TypeFamilies     #-}
 -- |
 -- Module    : System.Random.MWC.Monad
 -- Copyright : (c) 2010-2012 Aleksey Khudyakov
 import Control.Monad.IO.Class    (MonadIO(..))
 import Control.Monad.ST          (ST)
 import Control.Monad.Primitive   (PrimMonad, PrimState)
+import Control.Monad.Primitive.Class (MonadPrim(..))
 
 import Data.Word               (Word32)
 import qualified Data.Vector.Generic as G
 -- | Random monad for mwc-random package
 newtype Rand m a = Rand {
   -- | Run random monad
-  runRand :: Gen (PrimState m) -> m a
+  runRand :: Gen (PrimState (BasePrimMonad m)) -> m a
   }
 
 instance (Monad m) => Functor (Rand m) where
   liftIO = lift . liftIO
   {-# INLINE liftIO #-}
 
-
+instance MonadPrim m => MonadPrim (Rand m) where
+  type BasePrimMonad (Rand m) = BasePrimMonad m
+  liftPrim = lift . liftPrim
+  {-# INLINE liftPrim #-}
 
 -- | Type synonim for ST-based Rand monad
 type RandST s a = Rand (ST s) a
 ----------------------------------------------------------------
 
 -- | Run monad using fixed seed
-runWithCreate :: PrimMonad m => Rand m a -> m a
-runWithCreate m = runRand m =<< MWC.create
+runWithCreate :: MonadPrim m => Rand m a -> m a
+runWithCreate m = runRand m =<< liftPrim MWC.create
 {-# INLINE runWithCreate #-}
 
 -- | By creating seed from vector of values
-runWithVector :: (G.Vector v Word32, PrimMonad m) => Rand m a -> v Word32 -> m a
-runWithVector m v = runRand m =<< MWC.initialize v
+runWithVector :: (G.Vector v Word32, MonadPrim m) => Rand m a -> v Word32 -> m a
+runWithVector m v = runRand m =<< liftPrim (MWC.initialize v)
 {-# INLINE runWithVector #-}
 
 -- | Run monad using seed
-runWithSeed :: PrimMonad m => Seed -> Rand m a -> m a
-runWithSeed seed m = runRand m =<< MWC.restore seed
+runWithSeed :: MonadPrim m => Seed -> Rand m a -> m a
+runWithSeed seed m = runRand m =<< liftPrim (MWC.restore seed)
 {-# INLINE runWithSeed #-}
 
 -- | Run monad using system random
-runWithSystemRandom :: PrimMonad m => Rand m a -> IO a
+runWithSystemRandom :: (MonadPrim m, BasePrimMonad m ~ m) => Rand m a -> IO a
 runWithSystemRandom = MWC.withSystemRandom . runRand
 {-# INLINE runWithSystemRandom #-}
 
 ----------------------------------------------------------------
 
 -- | Convert function to Rand monad
-toRand :: PrimMonad m => (Gen (PrimState m) -> m a) -> Rand m a
-toRand = Rand
+toRand :: MonadPrim m => (Gen (PrimState (BasePrimMonad m)) -> BasePrimMonad m a) -> Rand m a
+toRand generator = Rand $ \g -> liftPrim (generator g)
 {-# INLINE toRand #-}
 
 -- | Uniformly distributed values
-uniform :: (PrimMonad m, Variate a) => Rand m a
-uniform = Rand $ \g -> MWC.uniform g
+uniform :: (MonadPrim m, Variate a) => Rand m a
+uniform = toRand $ \g -> MWC.uniform g
 {-# INLINE uniform #-}
 
 -- | Uniformly distributed values in range
-uniformR :: (PrimMonad m, Variate a) => (a,a) -> Rand m a
-uniformR rng = Rand $ \g -> MWC.uniformR rng g
+uniformR :: (MonadPrim m, Variate a) => (a,a) -> Rand m a
+uniformR rng = toRand $ \g -> MWC.uniformR rng g
 {-# INLINE uniformR #-}
 
 
 ----------------------------------------------------------------
 
 -- | Save current seed for future reuse
-save :: PrimMonad m => Rand m Seed
-save = Rand MWC.save
+save :: MonadPrim m => Rand m Seed
+save = Rand $ liftPrim . MWC.save

mwc-random-monad.cabal

 Name:                mwc-random-monad
-Version:             0.5
+Version:             0.6
 License:             BSD3
 License-file:        LICENSE
 Author:              Alexey Khudyakov <alexey.skladnoy@gmail.com>
 
 Library
   build-depends:
-    base >= 3 && < 5,
-    transformers,
+    base         >= 3 && < 5,
+    transformers >= 0.3,
     primitive,
-    vector >= 0.7,
+    monad-primitive,
+    vector     >= 0.7,
     mwc-random >= 0.11
   Exposed-modules:
     System.Random.MWC.Monad
     System.Random.MWC.Distributions.Monad
     System.Random.MWC.CondensedTable.Monad
-  Ghc-options:       -O2 -Wall
+  Ghc-options:       -O2 -Wall -fsimpl-tick-factor=500
   Ghc-prof-options:  -auto-all
 
 source-repository head