packages feed

mwc-random-monad (empty) → 0.1

raw patch · 4 files changed

+182/−0 lines, 4 filesdep +basedep +mwc-randomdep +primitivesetup-changed

Dependencies added: base, mwc-random, primitive, vector

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2010, Alexey Khudyakov <alexey.skladnoy@gmail.com>++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Alexey Khudyakov <alexey.skladnoy@gmail.com> nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ System/Random/MWC/Monad.hs view
@@ -0,0 +1,127 @@+module System.Random.MWC.Monad ( -- * Random monad+                                 Rand+                               , RandIO+                               , asRandIO+                               , RandST+                               , asRandST+                               , Seed+                               , runRand+                               , runWithSeed+                               , runWithVector+                               , runWithSystemRandom+                                 -- * Random numbers generation+                               , toRand+                               , uniform+                               , uniformR+                               , standard+                               , normal+                                 -- * Seed management+                               , save+                               ) where++import Control.Applicative+import Control.Monad           (ap)+import Control.Monad.ST        (ST)+import Control.Monad.Primitive (PrimMonad, PrimState)++import Data.Vector.Unboxed (Vector)+import Data.Word           (Word32)++import qualified System.Random.MWC as MWC+import           System.Random.MWC   (Gen,Variate,Seed)++----------------------------------------------------------------++-- | Random monad for mwc-random package+newtype Rand m a = Rand {+  -- | Run random monad+  runRand :: Gen (PrimState m) -> m a+  }++-- | Type synonim for ST-based Rand monad+type RandST s a = Rand (ST s) a++-- | Fix type of monad to ST+asRandST :: RandST s a -> RandST s a+asRandST = id+{-# INLINE asRandST #-}++-- | Type synonim for IO-based Rand monad+type RandIO a   = Rand IO a++-- | Fix type of monad to IO+asRandIO :: RandIO a -> RandIO a+asRandIO = id+{-# INLINE asRandIO #-}++-- | Run monad using fixed seed+runWithCreate :: PrimMonad m => Rand m a -> m a+runWithCreate m = runRand m =<< MWC.create+{-# INLINE runWithCreate #-}++-- | By creating seed from vector of values+runWithVector :: PrimMonad m => Rand m a -> Vector Word32 -> m a+runWithVector m v = runRand m =<< 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+{-# INLINE runWithSeed #-}++-- | Run monad using system random+runWithSystemRandom :: PrimMonad m => Rand m a -> IO a+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 x         = Rand (\_ -> return x)+  (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 (<*>) #-}++----------------------------------------------------------------++-- | Convert function to Rand monad+toRand :: PrimMonad m => (Gen (PrimState m) -> m a) -> Rand m a+toRand = Rand+{-# INLINE toRand #-}++-- | Uniformly distributed values+uniform :: (PrimMonad m, Variate a) => Rand m a+uniform = Rand MWC.uniform+{-# INLINE uniform #-}++-- | Uniformly distributed values in range+uniformR :: (PrimMonad m, Variate a) => (a,a) -> Rand m a+uniformR rng = Rand (MWC.uniformR rng)+{-# INLINE uniformR #-}++-- | Normally distributed variables with mean 0 and 1 standard deviation+standard :: PrimMonad m => Rand m Double+standard = Rand MWC.normal+{-# INLINE normal #-}++-- | Normally distributed variable+normal :: PrimMonad m => +          Double                -- Mean+       -> Double                -- Variance+       -> Rand m Double+normal m s = (+ m) . (* s) <$> standard+{-# INLINE standard #-}++----------------------------------------------------------------++-- | Save current seed for future reuse+save :: PrimMonad m => Rand m Seed+save = Rand MWC.save
+ mwc-random-monad.cabal view
@@ -0,0 +1,23 @@+Name:                mwc-random-monad+Version:             0.1+Synopsis:            Monadic interface for mwc-random+License:             BSD3+License-file:        LICENSE+Author:              Alexey Khudyakov <alexey.skladnoy@gmail.com>+Maintainer:          Alexey Khudyakov <alexey.skladnoy@gmail.com>+Category:            Math, Statistics+Build-type:          Simple+Cabal-version:       >=1.6+Description:         +  Simple monadit interface for mwc-random.++Library+  build-depends:     base >= 3 && < 5,+                     primitive,+                     vector >= 0.6.0.2,+                     mwc-random >= 0.8+  Exposed-modules:   System.Random.MWC.Monad+  +source-repository head+  type:     mercurial+  location: http://bitbucket.org/Shimuuar/mwc-random-monad