Aleksey Khudyakov avatar Aleksey Khudyakov committed bceae1d

Add MonadTrans and MonadIO instances

Comments (0)

Files changed (2)

System/Random/MWC/Monad.hs

     -- * Random monad
     Rand
   , toRand
-  , liftR
     -- ** Type syhnonims
   , RandIO
   , asRandIO
   ) where
 
 import Control.Applicative
-import Control.Monad           (ap)
-import Control.Monad.ST        (ST)
-import Control.Monad.Primitive (PrimMonad, PrimState)
+import Control.Monad             (ap)
+import Control.Monad.Trans.Class (MonadTrans(..))
+import Control.Monad.IO.Class    (MonadIO(..))
+import Control.Monad.ST          (ST)
+import Control.Monad.Primitive   (PrimMonad, PrimState)
 
 import Data.Word               (Word32)
 import qualified Data.Vector.Generic as G
   runRand :: Gen (PrimState m) -> m a
   }
 
--- | Lift monadic action into 'Rand' monad
-liftR :: PrimMonad m => m a -> Rand m a
-liftR m = Rand $ const m
-{-# INLINE liftR #-}
+instance (Monad m) => Functor (Rand m) where
+  fmap f (Rand rnd) = Rand $ \g -> (return . f) =<< (rnd g)
+  {-# INLINE fmap #-}
+
+instance (Monad m) => Monad (Rand m) where
+  return           = Rand . const . return
+  (Rand rnd) >>= f = Rand $ \g -> (\x -> runRand (f x) g) =<< rnd g
+  {-# INLINE return #-}
+  {-# INLINE (>>=)  #-}
+
+instance (Monad m) => Applicative (Rand m) where
+  pure  = return
+  (<*>) = ap
+  {-# INLINE pure  #-}
+  {-# INLINE (<*>) #-}
+
+instance MonadTrans Rand where
+  lift = Rand . const
+  {-# INLINE lift #-}
+
+instance MonadIO m => MonadIO (Rand m) where
+  liftIO = lift . liftIO
+  {-# INLINE liftIO #-}
+
+
 
 -- | Type synonim for ST-based Rand monad
 type RandST s a = Rand (ST s) a
 asRandIO = id
 {-# INLINE asRandIO #-}
 
+
+
+----------------------------------------------------------------
+
 -- | Run monad using fixed seed
 runWithCreate :: PrimMonad m => Rand m a -> m a
 runWithCreate m = runRand m =<< MWC.create
 runWithSystemRandom = MWC.withSystemRandom . runRand
 {-# INLINE runWithSystemRandom #-}
 
-instance PrimMonad m => Functor (Rand m) where
-  fmap f (Rand rnd) = Rand $ \g -> (return . f) =<< (rnd g)
-  {-# INLINE fmap #-}
 
-instance PrimMonad m => Monad (Rand m) where
-  return           = Rand . const . return
-  (Rand rnd) >>= f = Rand $ \g -> (\x -> runRand (f x) g) =<< rnd g
-  {-# INLINE return #-}
-  {-# INLINE (>>=)  #-}
-
-instance (PrimMonad m) => Applicative (Rand m) where
-  pure  = return
-  (<*>) = ap
-  {-# INLINE pure  #-}
-  {-# INLINE (<*>) #-}
 
 ----------------------------------------------------------------
 
 {-# INLINE uniformR #-}
 
 
+
 ----------------------------------------------------------------
 
 -- | Save current seed for future reuse

mwc-random-monad.cabal

 Library
   build-depends:
     base >= 3 && < 5,
+    transformers,
     primitive,
     vector >= 0.7,
     mwc-random >= 0.11
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.