packages feed

MonadRandomLazy (empty) → 0.1

raw patch · 4 files changed

+143/−0 lines, 4 filesdep +MonadRandomdep +basedep +mtlsetup-changed

Dependencies added: MonadRandom, base, mtl, random

Files

+ Control/Monad/LazyRandom.hs view
@@ -0,0 +1,99 @@+{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}+{-# OPTIONS -Wall -Werror #-}++{- |+Portability  : non-portable (multi-parameter type classes, undecidable instances)+License : BSD3++A lazy monad for random-number generation.  This monad allows, for example,+computation of infinite random lists.++This monad respects the interface defined by +'Control.Monad.Random.Class.MonadRandom'.++A monadic computation is one that consumes random values.+The bind operation works like the 'Gen' monad in 'Test.QuickCheck':+it does not thread the random seed; instead it *splits* the random seed.++-}++module Control.Monad.LazyRandom +  ( module System.Random+  , module Control.Monad.Random.Class+  , evalRand, runRand, evalRandIO+  , Rand+  )+where+  +import Control.Monad.Random.Class++--------------------------------------------------------------------------+-- imports++import System.Random+  ( Random(..)+  , RandomGen(..)+  , StdGen, mkStdGen, getStdRandom+  )++import Control.Monad+  ( ap+  )++import Control.Applicative+  ( Applicative(..)+  )++--------------------------------------------------------------------------+-- ** Based on quickcheck generator type++newtype Rand g a = MkRand (g -> a)+-- | A value of type 'Rand g a' is a monadic computation which, when+-- run, consumes random values from an applicative random-number generator +-- of type 'g' and produces a result of type 'a'.++instance Functor (Rand g) where+  fmap f (MkRand h) = MkRand (f . h)++instance RandomGen g => Applicative (Rand g) where+  pure  = return+  (<*>) = ap++instance RandomGen g => Monad (Rand g) where+  return x = MkRand (const x)+  +  MkRand m >>= k =+    MkRand (\r ->+      let (r1,r2)  = split r+          MkRand m' = k (m r1)+       in m' r2+    )++++-- | Evaluate a random computation using the generator @g@.  The+-- new @g@ is discarded.+evalRand :: (RandomGen g) => Rand g a -> g -> a+evalRand (MkRand f) g = f g+ +-- | Run a random computation using the generator @g@, returning the result+-- and a new generator.+runRand :: (RandomGen g) => Rand g a -> g -> (a, g)+runRand (MkRand f) g = (f g1, g2)+  where (g1, g2) = split g+ +-- | Evaluate a random computation in the IO monad, using the random number+-- generator supplied by 'System.Random.getStdRandom'.+evalRandIO :: Rand StdGen a -> IO a+evalRandIO m = getStdRandom (runRand m)++++instance (RandomGen g) => MonadRandom (Rand g) where+    getRandom = MkRand $ fst . random+    getRandoms = MkRand $ randoms+    getRandomR range = MkRand $ fst . randomR range+    getRandomRs range = MkRand $ randomRs range++instance (RandomGen g) => MonadSplit g (Rand g) where+    getSplit = MkRand id  -- I think this has to be right ---NR
+ LICENSE view
@@ -0,0 +1,29 @@+Copyright (c) 2000-2012, Koen Claessen+Copyright (c) 2006-2008, Björn Bringert+Copyright (c) 2009-2012, Nick Smallbone+Copyright (c) 2012,      Norman Ramsey+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 names of the copyright owners nor the names of the+  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.
+ MonadRandomLazy.cabal view
@@ -0,0 +1,13 @@+Name:                MonadRandomLazy+Build-type:          Simple+Version:             0.1+Synopsis:            Lazy monad for psuedo random-number generation.+Description:         Support for lazy computations which consume random values.+License:             BSD3+License-file:        LICENSE+Category:            Control+Author:              Norman Ramsey (following Koen Claessen)+Maintainer:          Norman Ramsey <nr@cs.tufts.edu>+Build-Depends:       base >=2 && < 5, mtl, random, MonadRandom+Exposed-modules:     Control.Monad.LazyRandom+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain