system-random-effect (empty) → 0.1.0.0
raw patch · 4 files changed
+142/−0 lines, 4 filesdep +basedep +extensible-effectsdep +mersenne-random-pure64setup-changed
Dependencies added: base, extensible-effects, mersenne-random-pure64
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- System/Random/Effect.hs +90/−0
- system-random-effect.cabal +20/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2013, Clark Gaebel++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 Clark Gaebel 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/Effect.hs view
@@ -0,0 +1,90 @@+{-# LANGUAGE DeriveDataTypeable #-}+{-# LANGUAGE TypeOperators #-}+{-# LANGUAGE FlexibleContexts #-}+-- | A random number effect, using a pure mersenne twister under+-- the hood. This should be plug-and-play with any application+-- making use of extensible effects.+--+-- Patches, even for the smallest of documentation bugs, are+-- always welcome!+module System.Random.Effect ( Random+ , mkRandom+ , mkRandomIO+ , randomInt+ , randomInt64+ , randomWord+ , randomWord64+ , randomDouble+ ) where++import Data.Int+import Data.Typeable+import Data.Word+import qualified System.Random.Mersenne.Pure64 as SR++import Control.Eff+import Control.Eff.State+import Control.Eff.Lift++-- | A pure mersenne twister pseudo-random number generator.+data Random = Random {-# UNPACK #-} !SR.PureMT+ deriving Typeable++-- | Create a random number generator from a 'Word64' seed.+mkRandom :: Word64 -> Random+mkRandom = Random . SR.pureMT+{-# INLINE mkRandom #-}++-- | Create a new random number generator, using the clocktime as the base for+-- the seed. This must be called from a computation with a lifted base effect+-- of 'IO'.+mkRandomIO :: Member (Lift IO) r+ => Eff r Random+mkRandomIO = lift (fmap Random SR.newPureMT)+{-# INLINE mkRandomIO #-}++-- | Runs an effectful random computation, returning the computation's result.+runRandomState :: Random+ -> Eff (State Random :> r) w+ -> Eff r w+runRandomState seed computation =+ fmap snd (runState seed computation)+{-# INLINE runRandomState #-}++-- | A generalized form of generating a random number of the correct type+-- from System.Random.Mersenne.Pure64.+randomF :: Member (State Random) r+ => (SR.PureMT -> (a, SR.PureMT))+ -> Eff r a+randomF f = do+ (Random old) <- getState+ let (val, new) = f old+ putState (Random new)+ return val+{-# INLINE randomF #-}++-- | Yield a new 'Int' value from the generator. The full 64 bits will be used+-- on a 64 bit machine.+randomInt :: Member (State Random) r => Eff r Int+randomInt = randomF SR.randomInt+{-# INLINE randomInt #-}++-- | Yield a new 'Word' value from the generator.+randomWord :: Member (State Random) r => Eff r Word+randomWord = randomF SR.randomWord+{-# INLINE randomWord #-}++-- | Yield a new 'Int64' value from the generator.+randomInt64 :: Member (State Random) r => Eff r Int64+randomInt64 = randomF SR.randomInt64+{-# INLINE randomInt64 #-}++-- | Yield a new 'Word64' value from the generator.+randomWord64 :: Member (State Random) r => Eff r Word64+randomWord64 = randomF SR.randomWord64+{-# INLINE randomWord64 #-}++-- | Yield a new 53-bit precise 'Double' value from the generator.+randomDouble :: Member (State Random) r => Eff r Double+randomDouble = randomF SR.randomDouble+{-# INLINE randomDouble #-}
+ system-random-effect.cabal view
@@ -0,0 +1,20 @@+name: system-random-effect+version: 0.1.0.0+synopsis: Random number generation for extensible effects.+homepage: https://github.com/wowus/system-random-effect+license: BSD3+license-file: LICENSE+author: Clark Gaebel+maintainer: cgaebel@uwaterloo.ca+category: System+build-type: Simple+cabal-version: >=1.10++library+ exposed-modules: System.Random.Effect++ build-depends: base >=4.6 && <4.7+ , extensible-effects == 1.1.*+ , mersenne-random-pure64 == 0.2.*++ default-language: Haskell2010